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