xref: /aosp_15_r20/external/skia/tools/gpu/gl/GLTestContext.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/gpu/ganesh/gl/GrGLInterface.h"
12 #include "src/gpu/ganesh/gl/GrGLUtil.h"
13 #include "tools/gpu/TestContext.h"
14 
15 namespace sk_gpu_test {
16 /**
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.
20  */
21 class GLTestContext : public TestContext {
22 public:
23     ~GLTestContext() override;
24 
backend()25     GrBackendApi backend() override { return GrBackendApi::kOpenGL; }
26 
27     /** Does this represent a successfully created GL context? */
28     bool isValid() const;
29 
gl()30     const GrGLInterface* gl() const { return fGLInterface.get(); }
31 
32     /** Used for testing EGLImage integration. Take a GL_TEXTURE_2D and wraps it in an EGL Image */
texture2DToEGLImage(GrGLuint)33     virtual GrEGLImage texture2DToEGLImage(GrGLuint /*texID*/) const { return nullptr; }
34 
destroyEGLImage(GrEGLImage)35     virtual void destroyEGLImage(GrEGLImage) const { }
36 
37     /**
38      * Used for testing EGLImage integration. Takes a EGLImage and wraps it in a
39      * GL_TEXTURE_EXTERNAL_OES.
40      */
eglImageToExternalTexture(GrEGLImage)41     virtual GrGLuint eglImageToExternalTexture(GrEGLImage) const { return 0; }
42 
43     void testAbandon() override;
44 
45     void overrideVersion(const char* version, const char* shadingLanguageVersion);
46 
47     /**
48      * Creates a new GL context of the same type and makes the returned context current
49      * (if not null).
50      */
makeNew()51     virtual std::unique_ptr<GLTestContext> makeNew() const { return nullptr; }
52 
53     template<typename Ret, typename... Args>
Ret(GR_GL_FUNCTION_TYPE ** out)54     void getGLProcAddress(Ret(GR_GL_FUNCTION_TYPE** out)(Args...),
55                           const char* name, const char* ext = nullptr) const {
56         using Proc = Ret(GR_GL_FUNCTION_TYPE*)(Args...);
57         if (!SkStrStartsWith(name, "gl")) {
58             SK_ABORT("getGLProcAddress: proc name must have 'gl' prefix");
59             *out = nullptr;
60         } else if (ext) {
61             SkString fullname(name);
62             fullname.append(ext);
63             *out = reinterpret_cast<Proc>(this->onPlatformGetProcAddress(fullname.c_str()));
64         } else {
65             *out = reinterpret_cast<Proc>(this->onPlatformGetProcAddress(name));
66         }
67     }
68 
69     sk_sp<GrDirectContext> makeContext(const GrContextOptions& options) override;
70 
71 protected:
72     GLTestContext();
73 
74     /*
75      * Methods that subclasses must call from their constructors and destructors.
76      */
77     void init(sk_sp<const GrGLInterface>);
78 
79     void teardown() override;
80 
81     virtual GrGLFuncPtr onPlatformGetProcAddress(const char *) const = 0;
82 
83 private:
84     /** Subclass provides the gl interface object if construction was successful. */
85     sk_sp<const GrGLInterface> fOriginalGLInterface;
86 
87     /** The same as fOriginalGLInterface unless the version has been overridden. */
88     sk_sp<const GrGLInterface> fGLInterface;
89 
90     using INHERITED = TestContext;
91 };
92 
93 /**
94  * Creates platform-dependent GL context object.  The shareContext parameter is in an optional
95  * context with which to share display lists. This should be a pointer to an GLTestContext created
96  * with SkCreatePlatformGLTestContext.  NULL indicates that no sharing is to take place. Returns a
97  * valid gl context object or NULL if such can not be created.
98  */
99 GLTestContext* CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,
100                                            GLTestContext *shareContext = nullptr);
101 
102 }  // namespace sk_gpu_test
103 #endif
104