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 SkGLContext_DEFINED
      9 #define SkGLContext_DEFINED
     10 
     11 #include "GrGLInterface.h"
     12 #include "../private/SkGpuFenceSync.h"
     13 
     14 /**
     15  * Create an offscreen opengl context with an RGBA8 / 8bit stencil FBO.
     16  * Provides a GrGLInterface struct of function pointers for the context.
     17  * This class is intended for Skia's testing needs and not for general
     18  * use.
     19  */
     20 class SK_API SkGLContext : public SkNoncopyable {
     21 public:
     22     virtual ~SkGLContext();
     23 
     24     bool isValid() const { return NULL != gl(); }
     25 
     26     const GrGLInterface* gl() const { return fGL.get(); }
     27 
     28     bool fenceSyncSupport() const { return SkToBool(fFenceSync); }
     29 
     30     bool getMaxGpuFrameLag(int* maxFrameLag) const {
     31         if (!fFenceSync) {
     32             return false;
     33         }
     34         *maxFrameLag = kMaxFrameLag;
     35         return true;
     36     }
     37 
     38     void makeCurrent() const;
     39 
     40     /** Used for testing EGLImage integration. Take a GL_TEXTURE_2D and wraps it in an EGL Image */
     41     virtual GrEGLImage texture2DToEGLImage(GrGLuint /*texID*/) const { return 0; }
     42     virtual void destroyEGLImage(GrEGLImage) const {}
     43 
     44     /** Used for testing GL_TEXTURE_RECTANGLE integration. */
     45     GrGLint createTextureRectangle(int width, int height, GrGLenum internalFormat,
     46                                    GrGLenum externalFormat, GrGLenum externalType,
     47                                    GrGLvoid* data);
     48 
     49     /**
     50      * Used for testing EGLImage integration. Takes a EGLImage and wraps it in a
     51      * GL_TEXTURE_EXTERNAL_OES.
     52      */
     53     virtual GrGLuint eglImageToExternalTexture(GrEGLImage) const { return 0; }
     54 
     55     void swapBuffers();
     56 
     57     /**
     58      * The only purpose of this function it to provide a means of scheduling
     59      * work on the GPU (since all of the subclasses create primary buffers for
     60      * testing that are small and not meant to be rendered to the screen).
     61      *
     62      * If the platform supports fence sync (OpenGL 3.2+ or EGL_KHR_fence_sync),
     63      * this will not swap any buffers, but rather emulate triple buffer
     64      * synchronization using fences.
     65      *
     66      * Otherwise it will call the platform SwapBuffers method. This may or may
     67      * not perform some sort of synchronization, depending on whether the
     68      * drawing surface provided by the platform is double buffered.
     69      */
     70     void waitOnSyncOrSwap();
     71 
     72     /**
     73      * This notifies the context that we are deliberately testing abandoning
     74      * the context. It is useful for debugging contexts that would otherwise
     75      * test that GPU resources are properly deleted. It also allows a debugging
     76      * context to test that further GL calls are not made by Skia GPU code.
     77      */
     78     void testAbandon();
     79 
     80     /**
     81      * Creates a new GL context of the same type and makes the returned context current
     82      * (if not null).
     83      */
     84     virtual SkGLContext* createNew() const { return nullptr; }
     85 
     86     class GLFenceSync;  // SkGpuFenceSync implementation that uses the OpenGL functionality.
     87 
     88     /*
     89      * returns the fencesync object owned by this SkGLContext
     90      */
     91     SkGpuFenceSync* fenceSync() { return fFenceSync.get(); }
     92 
     93 protected:
     94     SkGLContext();
     95 
     96     /*
     97      * Methods that sublcasses must call from their constructors and destructors.
     98      */
     99     void init(const GrGLInterface*, SkGpuFenceSync* = NULL);
    100     void teardown();
    101 
    102     /*
    103      * Operations that have a platform-dependent implementation.
    104      */
    105     virtual void onPlatformMakeCurrent() const = 0;
    106     virtual void onPlatformSwapBuffers() const = 0;
    107     virtual GrGLFuncPtr onPlatformGetProcAddress(const char*) const = 0;
    108 
    109 private:
    110     enum { kMaxFrameLag = 3 };
    111 
    112     SkAutoTDelete<SkGpuFenceSync> fFenceSync;
    113     SkPlatformGpuFence            fFrameFences[kMaxFrameLag - 1];
    114     int                           fCurrentFenceIdx;
    115 
    116     /** Subclass provides the gl interface object if construction was
    117      *  successful. */
    118     SkAutoTUnref<const GrGLInterface> fGL;
    119 
    120     friend class GLFenceSync;  // For onPlatformGetProcAddress.
    121 };
    122 
    123 /** Creates platform-dependent GL context object.  The shareContext parameter is in an optional
    124  * context with which to share display lists. This should be a pointer to an SkGLContext created
    125  * with SkCreatePlatformGLContext.  NULL indicates that no sharing is to take place. Returns a valid
    126  * gl context object or NULL if such can not be created.
    127  * Note: If Skia embedder needs a custom GL context that sets up the GL interface, this function
    128  * should be implemented by the embedder. Otherwise, the default implementation for the platform
    129  * should be compiled in the library.
    130  */
    131 SK_API SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI,
    132                                               SkGLContext* shareContext = nullptr);
    133 
    134 /**
    135  * Helper macros for using the GL context through the GrGLInterface. Example:
    136  * SK_GL(glCtx, GenTextures(1, &texID));
    137  */
    138 #define SK_GL(ctx, X) (ctx).gl()->fFunctions.f ## X;    \
    139                       SkASSERT(0 == (ctx).gl()->fFunctions.fGetError())
    140 #define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X;    \
    141                   SkASSERT(0 == (ctx).gl()->fFunctions.fGetError())
    142 #define SK_GL_NOERRCHECK(ctx, X) (ctx).gl()->fFunctions.f ## X
    143 #define SK_GL_RET_NOERRCHECK(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X
    144 
    145 #endif
    146