Home | History | Annotate | Download | only in gl
      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 
     23     virtual GrBackendApi backend() override { return GrBackendApi::kOpenGL; }
     24 
     25     bool isValid() const { return SkToBool(this->gl()); }
     26 
     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 */
     30     virtual GrEGLImage texture2DToEGLImage(GrGLuint /*texID*/) const { return nullptr; }
     31 
     32     virtual void destroyEGLImage(GrEGLImage) const { }
     33 
     34     /** Used for testing GL_TEXTURE_RECTANGLE integration. */
     35     GrGLuint createTextureRectangle(int width, int height, GrGLenum internalFormat,
     36                                     GrGLenum externalFormat, GrGLenum externalType, GrGLvoid* data);
     37 
     38     /**
     39      * Used for testing EGLImage integration. Takes a EGLImage and wraps it in a
     40      * GL_TEXTURE_EXTERNAL_OES.
     41      */
     42     virtual GrGLuint eglImageToExternalTexture(GrEGLImage) const { return 0; }
     43 
     44     void testAbandon() override;
     45 
     46     /** Ensures all work is submitted to the GPU for execution. */
     47     void submit() override;
     48 
     49     /** Wait until all GPU work is finished. */
     50     void finish() override;
     51 
     52     /**
     53      * Creates a new GL context of the same type and makes the returned context current
     54      * (if not null).
     55      */
     56     virtual std::unique_ptr<GLTestContext> makeNew() const { return nullptr; }
     57 
     58     template<typename Ret, typename... Args>
     59     void getGLProcAddress(Ret(GR_GL_FUNCTION_TYPE** out)(Args...),
     60                           const char* name, const char* ext = nullptr) const {
     61         using Proc = Ret(GR_GL_FUNCTION_TYPE*)(Args...);
     62         if (!SkStrStartsWith(name, "gl")) {
     63             SK_ABORT("getGLProcAddress: proc name must have 'gl' prefix");
     64             *out = nullptr;
     65         } else if (ext) {
     66             SkString fullname(name);
     67             fullname.append(ext);
     68             *out = reinterpret_cast<Proc>(this->onPlatformGetProcAddress(fullname.c_str()));
     69         } else {
     70             *out = reinterpret_cast<Proc>(this->onPlatformGetProcAddress(name));
     71         }
     72     }
     73 
     74     sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override;
     75 
     76 protected:
     77     GLTestContext();
     78 
     79     /*
     80      * Methods that sublcasses must call from their constructors and destructors.
     81      */
     82     void init(sk_sp<const GrGLInterface>, std::unique_ptr<FenceSync> = nullptr);
     83 
     84     void teardown() override;
     85 
     86     virtual GrGLFuncPtr onPlatformGetProcAddress(const char *) const = 0;
     87 
     88 private:
     89     /** Subclass provides the gl interface object if construction was
     90      *  successful. */
     91     sk_sp<const GrGLInterface> fGL;
     92 
     93     typedef TestContext INHERITED;
     94 };
     95 
     96 /**
     97  * Creates platform-dependent GL context object.  The shareContext parameter is in an optional
     98  * context with which to share display lists. This should be a pointer to an GLTestContext created
     99  * with SkCreatePlatformGLTestContext.  NULL indicates that no sharing is to take place. Returns a
    100  * valid gl context object or NULL if such can not be created.
    101  */
    102 GLTestContext* CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,
    103                                            GLTestContext *shareContext = nullptr);
    104 
    105 }  // namespace sk_gpu_test
    106 #endif
    107