3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #ifndef GLTestContext_DEFINED
9 #define GLTestContext_DEFINED
11 #include "include/gpu/gl/GrGLInterface.h"
12 #include "src/gpu/ganesh/gl/GrGLUtil.h"
13 #include "tools/gpu/TestContext.h"
15 namespace sk_gpu_test {
17 * An offscreen OpenGL context. Provides a GrGLInterface struct of function pointers for the context
18 * This class is intended for Skia's internal testing needs and not for general use.
19 * When SK_GL is not defined the GrGLInterface will always be nullptr.
21 class GLTestContext : public TestContext {
23 ~GLTestContext() override;
25 GrBackendApi backend() override { return GrBackendApi::kOpenGL; }
27 /** Does this represent a successfully created GL context? */
30 const GrGLInterface* gl() const { return fGLInterface.get(); }
32 /** Used for testing EGLImage integration. Take a GL_TEXTURE_2D and wraps it in an EGL Image */
33 virtual GrEGLImage texture2DToEGLImage(GrGLuint /*texID*/) const { return nullptr; }
35 virtual void destroyEGLImage(GrEGLImage) const { }
38 * Used for testing EGLImage integration. Takes a EGLImage and wraps it in a
39 * GL_TEXTURE_EXTERNAL_OES.
41 virtual GrGLuint eglImageToExternalTexture(GrEGLImage) const { return 0; }
43 void testAbandon() override;
45 /** Wait until all GPU work is finished. */
46 void finish() override;
48 void overrideVersion(const char* version, const char* shadingLanguageVersion);
51 * Creates a new GL context of the same type and makes the returned context current
54 virtual std::unique_ptr<GLTestContext> makeNew() const { return nullptr; }
56 template<typename Ret, typename... Args>
57 void getGLProcAddress(Ret(GR_GL_FUNCTION_TYPE** out)(Args...),
58 const char* name, const char* ext = nullptr) const {
59 using Proc = Ret(GR_GL_FUNCTION_TYPE*)(Args...);
60 if (!SkStrStartsWith(name, "gl")) {
61 SK_ABORT("getGLProcAddress: proc name must have 'gl' prefix");
64 SkString fullname(name);
66 *out = reinterpret_cast<Proc>(this->onPlatformGetProcAddress(fullname.c_str()));
68 *out = reinterpret_cast<Proc>(this->onPlatformGetProcAddress(name));
72 sk_sp<GrDirectContext> makeContext(const GrContextOptions& options) override;
78 * Methods that subclasses must call from their constructors and destructors.
80 void init(sk_sp<const GrGLInterface>);
82 void teardown() override;
84 virtual GrGLFuncPtr onPlatformGetProcAddress(const char *) const = 0;
87 /** Subclass provides the gl interface object if construction was successful. */
88 sk_sp<const GrGLInterface> fOriginalGLInterface;
90 /** The same as fOriginalGLInterface unless the version has been overridden. */
91 sk_sp<const GrGLInterface> fGLInterface;
94 bool fWasInitialized = false;
97 using INHERITED = TestContext;
101 * Creates platform-dependent GL context object. The shareContext parameter is in an optional
102 * context with which to share display lists. This should be a pointer to an GLTestContext created
103 * with SkCreatePlatformGLTestContext. NULL indicates that no sharing is to take place. Returns a
104 * valid gl context object or NULL if such can not be created.
106 GLTestContext* CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,
107 GLTestContext *shareContext = nullptr);
109 } // namespace sk_gpu_test