epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 3 | * Copyright 2013 Google Inc. |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 8 | #ifndef SkGLContextHelper_DEFINED |
| 9 | #define SkGLContextHelper_DEFINED |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 11 | #include "GrGLInterface.h" |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 12 | |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 13 | /** |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 14 | * Create an offscreen opengl context with an RGBA8 / 8bit stencil FBO. |
| 15 | * Provides a GrGLInterface struct of function pointers for the context. |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 16 | */ |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 17 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 18 | class SK_API SkGLContextHelper : public SkRefCnt { |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 19 | public: |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 20 | SK_DECLARE_INST_COUNT(SkGLContextHelper) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 21 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 22 | SkGLContextHelper(); |
| 23 | virtual ~SkGLContextHelper(); |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 24 | |
bsalomon@google.com | 57f5d98 | 2011-10-24 21:17:53 +0000 | [diff] [blame] | 25 | /** |
| 26 | * Initializes the context and makes it current. |
| 27 | */ |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 28 | bool init(GrGLStandard forcedGpuAPI, const int width, const int height); |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 29 | |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 30 | int getFBOID() const { return fFBO; } |
| 31 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 32 | const GrGLInterface* gl() const { return fGL; } |
bungeman@google.com | 16bab87 | 2011-05-17 14:24:46 +0000 | [diff] [blame] | 33 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 34 | virtual void makeCurrent() const = 0; |
| 35 | |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 36 | /** |
| 37 | * The primary purpose of this function it to provide a means of scheduling |
| 38 | * work on the GPU (since all of the subclasses create primary buffers for |
| 39 | * testing that are small and not meant to be rendered to the screen). |
| 40 | * |
| 41 | * If the drawing surface provided by the platform is double buffered this |
| 42 | * call will cause the platform to swap which buffer is currently being |
| 43 | * targeted. If the current surface does not include a back buffer, this |
| 44 | * call has no effect. |
| 45 | */ |
| 46 | virtual void swapBuffers() const = 0; |
| 47 | |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 48 | bool hasExtension(const char* extensionName) const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 49 | SkASSERT(fGL); |
commit-bot@chromium.org | 90313cc | 2014-01-17 15:05:38 +0000 | [diff] [blame] | 50 | return fGL->hasExtension(extensionName); |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 51 | } |
bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 52 | |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 53 | /** |
| 54 | * This notifies the context that we are deliberately testing abandoning |
| 55 | * the context. It is useful for debugging contexts that would otherwise |
| 56 | * test that GPU resources are properly deleted. It also allows a debugging |
| 57 | * context to test that further GL calls are not made by Skia GPU code. |
| 58 | */ |
| 59 | void testAbandon(); |
| 60 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 61 | protected: |
| 62 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 63 | * Subclass implements this to make a GL context. The returned GrGLInterface |
| 64 | * should be populated with functions compatible with the context. The |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 65 | * format and size of backbuffers does not matter since an FBO will be |
| 66 | * created. |
| 67 | */ |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 68 | virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) = 0; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Subclass should destroy the underlying GL context. |
| 72 | */ |
| 73 | virtual void destroyGLContext() = 0; |
| 74 | |
| 75 | private: |
| 76 | GrGLuint fFBO; |
robertphillips@google.com | 7c95942 | 2012-03-22 20:43:56 +0000 | [diff] [blame] | 77 | GrGLuint fColorBufferID; |
| 78 | GrGLuint fDepthStencilBufferID; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 79 | const GrGLInterface* fGL; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 80 | |
| 81 | typedef SkRefCnt INHERITED; |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 84 | /** |
robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 85 | * Helper macros for using the GL context through the GrGLInterface. Example: |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 86 | * SK_GL(glCtx, GenTextures(1, &texID)); |
| 87 | */ |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 88 | #define SK_GL(ctx, X) (ctx).gl()->fFunctions.f ## X; \ |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 89 | SkASSERT(0 == (ctx).gl()->fFunctions.fGetError()) |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 90 | #define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X; \ |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 91 | SkASSERT(0 == (ctx).gl()->fFunctions.fGetError()) |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 92 | #define SK_GL_NOERRCHECK(ctx, X) (ctx).gl()->fFunctions.f ## X |
| 93 | #define SK_GL_RET_NOERRCHECK(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 94 | |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 95 | #endif |