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