1 
2 /*
3  * Copyright 2013 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #ifndef GLTestContext_DEFINED
9 #define GLTestContext_DEFINED
10 
11 #include "TestContext.h"
12 #include "gl/GrGLInterface.h"
13 
14 namespace sk_gpu_test {
15 /**
16  * An offscreen OpenGL context. Provides a GrGLInterface struct of function pointers for the context
17  * This class is intended for Skia's internal testing needs and not for general use.
18  */
19 class GLTestContext : public TestContext {
20 public:
21     ~GLTestContext() override;
22 
backend()23     virtual GrBackendApi backend() override { return GrBackendApi::kOpenGL; }
24 
isValid()25     bool isValid() const { return SkToBool(this->gl()); }
26 
gl()27     const GrGLInterface* gl() const { return fGL.get(); }
28 
29     /** Used for testing EGLImage integration. Take a GL_TEXTURE_2D and wraps it in an EGL Image */
texture2DToEGLImage(GrGLuint)30     virtual GrEGLImage texture2DToEGLImage(GrGLuint /*texID*/) const { return nullptr; }
31 
destroyEGLImage(GrEGLImage)32     virtual void destroyEGLImage(GrEGLImage) const { }
33 
34     /** Used for testing GL_TEXTURE_RECTANGLE integration. */
35     GrGLint createTextureRectangle(int width, int height, GrGLenum internalFormat,
36                                    GrGLenum externalFormat, GrGLenum externalType,
37                                    GrGLvoid *data);
38 
39     /**
40      * Used for testing EGLImage integration. Takes a EGLImage and wraps it in a
41      * GL_TEXTURE_EXTERNAL_OES.
42      */
eglImageToExternalTexture(GrEGLImage)43     virtual GrGLuint eglImageToExternalTexture(GrEGLImage) const { return 0; }
44 
45     void testAbandon() override;
46 
47     /** Ensures all work is submitted to the GPU for execution. */
48     void submit() override;
49 
50     /** Wait until all GPU work is finished. */
51     void finish() override;
52 
53     /**
54      * Creates a new GL context of the same type and makes the returned context current
55      * (if not null).
56      */
makeNew()57     virtual std::unique_ptr<GLTestContext> makeNew() const { return nullptr; }
58 
59     template<typename Ret, typename... Args>
Ret(GR_GL_FUNCTION_TYPE ** out)60     void getGLProcAddress(Ret(GR_GL_FUNCTION_TYPE** out)(Args...),
61                           const char* name, const char* ext = nullptr) const {
62         using Proc = Ret(GR_GL_FUNCTION_TYPE*)(Args...);
63         if (!SkStrStartsWith(name, "gl")) {
64             SK_ABORT("getGLProcAddress: proc name must have 'gl' prefix");
65             *out = nullptr;
66         } else if (ext) {
67             SkString fullname(name);
68             fullname.append(ext);
69             *out = reinterpret_cast<Proc>(this->onPlatformGetProcAddress(fullname.c_str()));
70         } else {
71             *out = reinterpret_cast<Proc>(this->onPlatformGetProcAddress(name));
72         }
73     }
74 
75     sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override;
76 
77 protected:
78     GLTestContext();
79 
80     /*
81      * Methods that sublcasses must call from their constructors and destructors.
82      */
83     void init(sk_sp<const GrGLInterface>, std::unique_ptr<FenceSync> = nullptr);
84 
85     void teardown() override;
86 
87     virtual GrGLFuncPtr onPlatformGetProcAddress(const char *) const = 0;
88 
89 private:
90     /** Subclass provides the gl interface object if construction was
91      *  successful. */
92     sk_sp<const GrGLInterface> fGL;
93 
94     typedef TestContext INHERITED;
95 };
96 
97 /**
98  * Creates platform-dependent GL context object.  The shareContext parameter is in an optional
99  * context with which to share display lists. This should be a pointer to an GLTestContext created
100  * with SkCreatePlatformGLTestContext.  NULL indicates that no sharing is to take place. Returns a
101  * valid gl context object or NULL if such can not be created.
102  */
103 GLTestContext* CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,
104                                            GLTestContext *shareContext = nullptr);
105 
106 }  // namespace sk_gpu_test
107 #endif
108