| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 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. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrGpuGL_DEFINED |
| 12 | #define GrGpuGL_DEFINED |
| 13 | |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 14 | #include "GrDrawState.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | #include "GrGpu.h" |
| bsalomon@google.com | 4043ae2 | 2011-08-02 14:19:11 +0000 | [diff] [blame] | 16 | #include "GrGLIndexBuffer.h" |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 17 | #include "GrGLIRect.h" |
| 18 | #include "GrGLStencilBuffer.h" |
| 19 | #include "GrGLTexture.h" |
| 20 | #include "GrGLVertexBuffer.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | |
| bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 22 | #include "SkString.h" |
| 23 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | class GrGpuGL : public GrGpu { |
| 25 | public: |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | virtual ~GrGpuGL(); |
| 27 | |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 28 | const GrGLInterface* glInterface() const { return fGL; } |
| 29 | GrGLBinding glBinding() const { return fGLBinding; } |
| bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 30 | GrGLVersion glVersion() const { return fGLVersion; } |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 31 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | protected: |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 33 | GrGpuGL(const GrGLInterface* glInterface, GrGLBinding glBinding); |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 34 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | struct { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 36 | size_t fVertexOffset; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | GrVertexLayout fVertexLayout; |
| 38 | const GrVertexBuffer* fVertexBuffer; |
| 39 | const GrIndexBuffer* fIndexBuffer; |
| bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 40 | bool fArrayPtrsDirty; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | } fHWGeometryState; |
| 42 | |
| bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 43 | struct AAState { |
| 44 | bool fMSAAEnabled; |
| 45 | bool fSmoothLineEnabled; |
| 46 | } fHWAAState; |
| 47 | |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 48 | GrDrawState fHWDrawState; |
| 49 | bool fHWStencilClip; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 51 | // As flush of GL state proceeds it updates fHDrawState |
| 52 | // to reflect the new state. Later parts of the state flush |
| 53 | // may perform cascaded changes but cannot refer to fHWDrawState. |
| 54 | // These code paths can refer to the dirty flags. Subclass should |
| 55 | // call resetDirtyFlags after its flush is complete |
| 56 | struct { |
| 57 | bool fRenderTargetChanged : 1; |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 58 | int fTextureChangedMask; |
| 59 | } fDirtyFlags; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 60 | GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages); |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 61 | |
| 62 | // clears the dirty flags |
| 63 | void resetDirtyFlags(); |
| 64 | |
| 65 | // last scissor / viewport scissor state seen by the GL. |
| bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 66 | struct { |
| 67 | bool fScissorEnabled; |
| 68 | GrGLIRect fScissorRect; |
| 69 | GrGLIRect fViewportRect; |
| 70 | } fHWBounds; |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 71 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 72 | // GrGpu overrides |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 73 | virtual void resetContext(); |
| 74 | |
| bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 75 | virtual GrTexture* onCreateTexture(const GrTextureDesc& desc, |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 76 | const void* srcData, |
| 77 | size_t rowBytes); |
| 78 | virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size, |
| 79 | bool dynamic); |
| 80 | virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size, |
| 81 | bool dynamic); |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 82 | virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc); |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 83 | virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt, |
| 84 | int width, int height); |
| 85 | virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb, |
| 86 | GrRenderTarget* rt); |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 87 | |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 88 | virtual void onClear(const GrIRect* rect, GrColor color); |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 89 | |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 90 | virtual void onForceRenderTargetFlush(); |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 91 | |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 92 | virtual bool onReadPixels(GrRenderTarget* target, |
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame^] | 93 | int left, int top, |
| 94 | int width, int height, |
| 95 | GrPixelConfig, |
| 96 | void* buffer, size_t rowBytes) SK_OVERRIDE; |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 97 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 98 | virtual void onGpuDrawIndexed(GrPrimitiveType type, |
| 99 | uint32_t startVertex, |
| 100 | uint32_t startIndex, |
| 101 | uint32_t vertexCount, |
| 102 | uint32_t indexCount); |
| 103 | virtual void onGpuDrawNonIndexed(GrPrimitiveType type, |
| 104 | uint32_t vertexCount, |
| 105 | uint32_t numVertices); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | virtual void flushScissor(const GrIRect* rect); |
| bsalomon@google.com | edc177d | 2011-08-05 15:46:40 +0000 | [diff] [blame] | 107 | virtual void clearStencil(); |
| bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 108 | virtual void clearStencilClip(const GrIRect& rect, bool insideClip); |
| senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 109 | virtual int getMaxEdges() const; |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 110 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 111 | // binds texture unit in GL |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 112 | void setTextureUnit(int unitIdx); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 114 | // binds appropriate vertex and index buffers, also returns any extra |
| 115 | // extra verts or indices to offset by. |
| 116 | void setBuffers(bool indexed, |
| 117 | int* extraVertexOffset, |
| 118 | int* extraIndexOffset); |
| bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 119 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 120 | // flushes state that is common to fixed and programmable GL |
| 121 | // dither |
| 122 | // line smoothing |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | // texture binding |
| 124 | // sampler state (filtering, tiling) |
| 125 | // FBO binding |
| 126 | // line width |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 127 | bool flushGLStateCommon(GrPrimitiveType type); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 129 | // Subclasses should call this to flush the blend state. |
| 130 | // The params should be the final coeffecients to apply |
| 131 | // (after any blending optimizations or dual source blending considerations |
| 132 | // have been accounted for). |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 133 | void flushBlend(GrPrimitiveType type, |
| 134 | GrBlendCoeff srcCoeff, |
| 135 | GrBlendCoeff dstCoeff); |
| 136 | |
| bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 137 | bool hasExtension(const char* ext) { |
| bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 138 | return GrGLHasExtensionFromString(ext, fExtensionString.c_str()); |
| bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 141 | // adjusts texture matrix to account for orientation, size, and npotness |
| 142 | static void AdjustTextureMatrix(const GrGLTexture* texture, |
| 143 | GrSamplerState::SampleMode mode, |
| 144 | GrMatrix* matrix); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 146 | // subclass may try to take advantage of identity tex matrices. |
| 147 | // This helper determines if matrix will be identity after all |
| 148 | // adjustments are applied. |
| 149 | static bool TextureMatrixIsIdentity(const GrGLTexture* texture, |
| 150 | const GrSamplerState& sampler); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 152 | static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff); |
| bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 153 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 154 | private: |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 155 | // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable |
| 156 | // additional caps. |
| 157 | void initCaps(); |
| 158 | |
| 159 | void initFSAASupport(); |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 160 | |
| bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 161 | // determines valid stencil formats |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 162 | void initStencilFormats(); |
| bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 163 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 164 | // notify callbacks to update state tracking when related |
| 165 | // objects are bound to GL or deleted outside of the class |
| 166 | void notifyVertexBufferBind(const GrGLVertexBuffer* buffer); |
| 167 | void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer); |
| 168 | void notifyIndexBufferBind(const GrGLIndexBuffer* buffer); |
| 169 | void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 170 | void notifyTextureDelete(GrGLTexture* texture); |
| 171 | void notifyRenderTargetDelete(GrRenderTarget* renderTarget); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 172 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 173 | void setSpareTextureUnit(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 174 | |
| bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 175 | // bound is region that may be modified and therefore has to be resolved. |
| 176 | // NULL means whole target. Can be an empty rect. |
| 177 | void flushRenderTarget(const GrIRect* bound); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | void flushStencil(); |
| bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 179 | void flushAAState(GrPrimitiveType type); |
| bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 180 | |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 181 | void resolveRenderTarget(GrGLRenderTarget* texture); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 183 | bool canBeTexture(GrPixelConfig config, |
| twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 184 | GrGLenum* internalFormat, |
| 185 | GrGLenum* format, |
| 186 | GrGLenum* type); |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 187 | // helpers for onCreateTexture |
| bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 188 | void allocateAndUploadTexData(const GrGLTexture::Desc& desc, |
| 189 | GrGLenum internalFormat, |
| 190 | const void* data, |
| 191 | size_t rowBytes); |
| bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 192 | |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 193 | bool createRenderTargetObjects(int width, int height, |
| 194 | GrGLuint texID, |
| 195 | GrGLRenderTarget::Desc* desc); |
| 196 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 197 | bool fboInternalFormat(GrPixelConfig config, GrGLenum* format); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 198 | |
| 199 | friend class GrGLVertexBuffer; |
| 200 | friend class GrGLIndexBuffer; |
| 201 | friend class GrGLTexture; |
| 202 | friend class GrGLRenderTarget; |
| 203 | |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 204 | // read these once at begining and then never again |
| 205 | SkString fExtensionString; |
| bsalomon@google.com | c82b889 | 2011-09-22 14:10:33 +0000 | [diff] [blame] | 206 | GrGLVersion fGLVersion; |
| bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 207 | |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 208 | struct GLCaps { |
| 209 | // prealloc space for 8 stencil formats |
| 210 | GLCaps() : fStencilFormats(8) {} |
| 211 | SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats; |
| 212 | |
| 213 | enum { |
| 214 | /** |
| 215 | * no support for MSAA FBOs |
| 216 | */ |
| 217 | kNone_MSFBO = 0, |
| 218 | /** |
| 219 | * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object) |
| 220 | */ |
| 221 | kDesktopARB_MSFBO, |
| 222 | /** |
| 223 | * earlier GL_EXT_framebuffer* extensions |
| 224 | */ |
| 225 | kDesktopEXT_MSFBO, |
| 226 | /** |
| 227 | * GL_APPLE_framebuffer_multisample ES extension |
| 228 | */ |
| 229 | kAppleES_MSFBO, |
| 230 | } fMSFBOType; |
| 231 | |
| 232 | // TODO: get rid of GrAALevel and use sample cnt directly |
| 233 | GrGLuint fAASamples[4]; |
| 234 | |
| 235 | // The maximum number of fragment uniform vectors (GLES has min. 16). |
| 236 | int fMaxFragmentUniformVectors; |
| 237 | |
| 238 | // ES requires an extension to support RGBA8 in RenderBufferStorage |
| 239 | bool fRGBA8Renderbuffer; |
| 240 | |
| 241 | void print() const; |
| 242 | } fGLCaps; |
| 243 | |
| 244 | |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 245 | // we want to clear stencil buffers when they are created. We want to clear |
| 246 | // the entire buffer even if it is larger than the color attachment. We |
| 247 | // attach it to this fbo with no color attachment to do the initial clear. |
| 248 | GrGLuint fStencilClearFBO; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 249 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 250 | bool fHWBlendDisabled; |
| 251 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 252 | int fActiveTextureUnitIdx; |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 253 | |
| bsalomon@google.com | fe67652 | 2011-06-17 18:12:21 +0000 | [diff] [blame] | 254 | // we record what stencil format worked last time to hopefully exit early |
| 255 | // from our loop that tries stencil formats and calls check fb status. |
| 256 | int fLastSuccessfulStencilFmtIdx; |
| 257 | |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 258 | const GrGLInterface* fGL; |
| 259 | GrGLBinding fGLBinding; |
| 260 | |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 261 | bool fPrintedCaps; |
| 262 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 263 | typedef GrGpu INHERITED; |
| 264 | }; |
| 265 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 266 | #endif |
| bsalomon@google.com | fe67652 | 2011-06-17 18:12:21 +0000 | [diff] [blame] | 267 | |