1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26
27#include "config.h"
28
29#include "SharedGraphicsContext3D.h"
30
31#include "Extensions3D.h"
32#include <wtf/MainThread.h>
33
34namespace WebCore {
35
36class SharedGraphicsContext3DImpl {
37public:
38    SharedGraphicsContext3DImpl() : m_context(0) { }
39    PassRefPtr<GraphicsContext3D> getOrCreateContext()
40    {
41        bool wasCreated = false;
42
43        // If we lost the context, or can't make it current, create a new one.
44        if (m_context && (!m_context->makeContextCurrent() || (m_context->getExtensions()->getGraphicsResetStatusARB() != GraphicsContext3D::NO_ERROR)))
45            m_context.clear();
46
47        if (!m_context) {
48            createContext();
49            wasCreated = true;
50        }
51
52        if (m_context && !m_context->makeContextCurrent())
53            m_context.clear();
54
55        if (m_context && wasCreated)
56            m_context->getExtensions()->pushGroupMarkerEXT("SharedGraphicsContext");
57        return m_context;
58    }
59
60    PassRefPtr<GraphicsContext3D> getContext()
61    {
62        return m_context;
63    }
64
65    PassRefPtr<GraphicsContext3D> createContext()
66    {
67        GraphicsContext3D::Attributes attributes;
68        attributes.depth = false;
69        attributes.stencil = true;
70        attributes.antialias = false;
71        attributes.shareResources = true;
72        m_context = GraphicsContext3D::create(attributes, 0);
73        return m_context;
74    }
75private:
76    RefPtr<GraphicsContext3D> m_context;
77};
78
79PassRefPtr<GraphicsContext3D> SharedGraphicsContext3D::get()
80{
81    DEFINE_STATIC_LOCAL(SharedGraphicsContext3DImpl, impl, ());
82    return impl.getOrCreateContext();
83}
84
85enum ContextOperation {
86    Get, Create
87};
88
89static PassRefPtr<GraphicsContext3D> getOrCreateContextForImplThread(ContextOperation op)
90{
91    DEFINE_STATIC_LOCAL(SharedGraphicsContext3DImpl, impl, ());
92    return op == Create ? impl.createContext() : impl.getContext();
93}
94
95PassRefPtr<GraphicsContext3D> SharedGraphicsContext3D::getForImplThread()
96{
97    return getOrCreateContextForImplThread(Get);
98}
99
100bool SharedGraphicsContext3D::haveForImplThread()
101{
102    ASSERT(isMainThread());
103    return getOrCreateContextForImplThread(Get);
104}
105
106bool SharedGraphicsContext3D::createForImplThread()
107{
108    ASSERT(isMainThread());
109    return getOrCreateContextForImplThread(Create);
110}
111
112}
113
114