1/*
2 * Copyright (C) 2012 Intel Corporation. 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 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef GLPlatformContext_h
27#define GLPlatformContext_h
28
29#include "GLDefs.h"
30#include "GLPlatformSurface.h"
31#include "GraphicsContext3D.h"
32#include <wtf/Noncopyable.h>
33
34// Encapsulates an OpenGL context, hiding platform specific management.
35namespace WebCore {
36
37class GLPlatformContext {
38    WTF_MAKE_NONCOPYABLE(GLPlatformContext);
39
40public:
41    // From http://www.khronos.org/registry/gles/extensions/EXT/EXT_robustness.txt
42    enum PlatformContextReset {
43        PLATFORMCONTEXT_NO_ERROR = 0x0000,
44        PLATFORMCONTEXT_GUILTY_CONTEXT_RESET = 0x8253,
45        PLATFORMCONTEXT_INNOCENT_CONTEXT_RESET = 0x8254,
46        PLATFORMCONTEXT_UNKNOWN_CONTEXT_RESET = 0x8255,
47    };
48
49    static std::unique_ptr<GLPlatformContext> createContext(GraphicsContext3D::RenderStyle);
50
51    static bool supportsGLExtension(const String&);
52
53#if USE(EGL)
54    static bool supportsEGLExtension(EGLDisplay, const String&);
55#endif
56
57#if USE(GLX)
58    static bool supportsGLXExtension(Display*, const String&);
59#endif
60
61    virtual ~GLPlatformContext();
62
63    virtual bool initialize(GLPlatformSurface*, PlatformContext = 0);
64
65    // Makes this and surface as current context and drawable.
66    // Calling this function with no surface is same as calling releaseCurrent.
67    // Does nothing if this is already current Context.
68    bool makeCurrent(GLPlatformSurface* = 0);
69
70    // Sets Current Context and Drawable as Null.
71    // Doesn't have any effect if this is not the current Context.
72    void releaseCurrent();
73
74    virtual PlatformContext handle() const;
75
76    virtual bool isCurrentContext() const;
77
78    bool isValid() const;
79
80    // Destroys any GL resources associated with this context.
81    virtual void destroy();
82
83    static GLPlatformContext* getCurrent();
84
85protected:
86    GLPlatformContext();
87    virtual bool platformMakeCurrent(GLPlatformSurface*);
88    virtual void platformReleaseCurrent();
89    PlatformContext m_contextHandle;
90    bool m_resetLostContext;
91    bool m_contextLost;
92};
93
94} // namespace WebCore
95
96#endif // GLNativeContext_H
97