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 | */ |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [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 | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 11 | #include "GrGLExtensions.h" |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 12 | #include "GrGLInterface.h" |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 13 | |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 14 | /** |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 15 | * Create an offscreen opengl context with an RGBA8 / 8bit stencil FBO. |
| 16 | * Provides a GrGLInterface struct of function pointers for the context. |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 17 | */ |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 18 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 19 | class SkGLContextHelper : public SkRefCnt { |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 20 | public: |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 21 | SK_DECLARE_INST_COUNT(SkGLContextHelper) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 22 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 23 | SkGLContextHelper(); |
| 24 | virtual ~SkGLContextHelper(); |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 57f5d98 | 2011-10-24 21:17:53 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Initializes the context and makes it current. |
| 28 | */ |
bungeman@google.com | 0e45441 | 2011-05-19 17:47:02 +0000 | [diff] [blame] | 29 | bool init(const int width, const int height); |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | 971d0c8 | 2011-08-19 17:22:05 +0000 | [diff] [blame] | 31 | int getFBOID() const { return fFBO; } |
| 32 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 33 | const GrGLInterface* gl() const { return fGL; } |
bungeman@google.com | 16bab87 | 2011-05-17 14:24:46 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 35 | virtual void makeCurrent() const = 0; |
| 36 | |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 37 | bool hasExtension(const char* extensionName) const { |
| 38 | GrAssert(NULL != fGL); |
| 39 | return fExtensions.has(extensionName); |
| 40 | } |
bsalomon@google.com | 6e85937 | 2012-02-09 15:25:13 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 42 | protected: |
| 43 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 44 | * Subclass implements this to make a GL context. The returned GrGLInterface |
| 45 | * should be populated with functions compatible with the context. The |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 46 | * format and size of backbuffers does not matter since an FBO will be |
| 47 | * created. |
| 48 | */ |
| 49 | virtual const GrGLInterface* createGLContext() = 0; |
| 50 | |
| 51 | /** |
| 52 | * Subclass should destroy the underlying GL context. |
| 53 | */ |
| 54 | virtual void destroyGLContext() = 0; |
| 55 | |
| 56 | private: |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 57 | GrGLExtensions fExtensions; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 58 | GrGLuint fFBO; |
robertphillips@google.com | 7c95942 | 2012-03-22 20:43:56 +0000 | [diff] [blame] | 59 | GrGLuint fColorBufferID; |
| 60 | GrGLuint fDepthStencilBufferID; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 61 | const GrGLInterface* fGL; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 62 | |
| 63 | typedef SkRefCnt INHERITED; |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 66 | /** |
robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 67 | * Helper macros for using the GL context through the GrGLInterface. Example: |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 68 | * SK_GL(glCtx, GenTextures(1, &texID)); |
| 69 | */ |
robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 70 | #define SK_GL(ctx, X) (ctx).gl()->f ## X; \ |
| 71 | SkASSERT(GR_GL_NO_ERROR == (ctx).gl()->fGetError()) |
| 72 | #define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->f ## X; \ |
| 73 | SkASSERT(GR_GL_NO_ERROR == (ctx).gl()->fGetError()) |
| 74 | #define SK_GL_NOERRCHECK(ctx, X) (ctx).gl()->f ## X |
| 75 | #define SK_GL_RET_NOERRCHECK(ctx, RET, X) (RET) = (ctx).gl()->f ## X |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 76 | |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 77 | #endif |