| 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 | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame^] | 29 | float 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 | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 126 | // subclass should call this to flush the blend state |
| 127 | void flushBlend(GrPrimitiveType type, |
| 128 | GrBlendCoeff srcCoeff, |
| 129 | GrBlendCoeff dstCoeff); |
| 130 | |
| bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 131 | bool hasExtension(const char* ext) { |
| 132 | return has_gl_extension_from_string(ext, fExtensionString.c_str()); |
| 133 | } |
| 134 | |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 135 | // adjusts texture matrix to account for orientation, size, and npotness |
| 136 | static void AdjustTextureMatrix(const GrGLTexture* texture, |
| 137 | GrSamplerState::SampleMode mode, |
| 138 | GrMatrix* matrix); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | |
| bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 140 | // subclass may try to take advantage of identity tex matrices. |
| 141 | // This helper determines if matrix will be identity after all |
| 142 | // adjustments are applied. |
| 143 | static bool TextureMatrixIsIdentity(const GrGLTexture* texture, |
| 144 | const GrSamplerState& sampler); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 146 | static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff); |
| bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 147 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 148 | private: |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 149 | |
| bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 150 | // determines valid stencil formats |
| 151 | void setupStencilFormats(); |
| 152 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | // notify callbacks to update state tracking when related |
| 154 | // objects are bound to GL or deleted outside of the class |
| 155 | void notifyVertexBufferBind(const GrGLVertexBuffer* buffer); |
| 156 | void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer); |
| 157 | void notifyIndexBufferBind(const GrGLIndexBuffer* buffer); |
| 158 | void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | void notifyTextureDelete(GrGLTexture* texture); |
| 160 | void notifyRenderTargetDelete(GrRenderTarget* renderTarget); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 161 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 162 | void setSpareTextureUnit(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 163 | |
| bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 164 | // bound is region that may be modified and therefore has to be resolved. |
| 165 | // NULL means whole target. Can be an empty rect. |
| 166 | void flushRenderTarget(const GrIRect* bound); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | void flushStencil(); |
| bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 168 | void flushAAState(GrPrimitiveType type); |
| bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 169 | |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 170 | void resolveRenderTarget(GrGLRenderTarget* texture); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 171 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 172 | bool canBeTexture(GrPixelConfig config, |
| twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 173 | GrGLenum* internalFormat, |
| 174 | GrGLenum* format, |
| 175 | GrGLenum* type); |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 176 | // helpers for onCreateTexture |
| bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 177 | void allocateAndUploadTexData(const GrGLTexture::Desc& desc, |
| 178 | GrGLenum internalFormat, |
| 179 | const void* data, |
| 180 | size_t rowBytes); |
| bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 181 | |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 182 | bool createRenderTargetObjects(int width, int height, |
| 183 | GrGLuint texID, |
| 184 | GrGLRenderTarget::Desc* desc); |
| 185 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 186 | bool fboInternalFormat(GrPixelConfig config, GrGLenum* format); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 187 | |
| 188 | friend class GrGLVertexBuffer; |
| 189 | friend class GrGLIndexBuffer; |
| 190 | friend class GrGLTexture; |
| 191 | friend class GrGLRenderTarget; |
| 192 | |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame^] | 193 | // read these once at begining and then never again |
| 194 | SkString fExtensionString; |
| 195 | float fGLVersion; |
| bsalomon@google.com | 2c17fcd | 2011-07-06 17:47:02 +0000 | [diff] [blame] | 196 | |
| bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 197 | SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats; |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 198 | // we want to clear stencil buffers when they are created. We want to clear |
| 199 | // the entire buffer even if it is larger than the color attachment. We |
| 200 | // attach it to this fbo with no color attachment to do the initial clear. |
| 201 | GrGLuint fStencilClearFBO; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame^] | 202 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 203 | bool fHWBlendDisabled; |
| 204 | |
| twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 205 | GrGLuint fAASamples[4]; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 206 | enum { |
| bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 207 | kNone_MSFBO = 0, //<! no support for MSAA FBOs |
| 208 | kDesktopARB_MSFBO,//<! GL3.0-style MSAA FBO (GL_ARB_framebuffer_object) |
| 209 | kDesktopEXT_MSFBO,//<! earlier GL_EXT_framebuffer* extensions |
| 210 | kAppleES_MSFBO, //<! GL_APPLE_framebuffer_multisample ES extension |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 211 | } fMSFBOType; |
| 212 | |
| 213 | // Do we have stencil wrap ops. |
| 214 | bool fHasStencilWrap; |
| 215 | |
| senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 216 | // The maximum number of fragment uniform vectors (GLES has min. 16). |
| 217 | int fMaxFragmentUniformVectors; |
| 218 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 219 | // ES requires an extension to support RGBA8 in RenderBufferStorage |
| 220 | bool fRGBA8Renderbuffer; |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 221 | |
| bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 222 | int fActiveTextureUnitIdx; |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 223 | |
| bsalomon@google.com | fe67652 | 2011-06-17 18:12:21 +0000 | [diff] [blame] | 224 | // we record what stencil format worked last time to hopefully exit early |
| 225 | // from our loop that tries stencil formats and calls check fb status. |
| 226 | int fLastSuccessfulStencilFmtIdx; |
| 227 | |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 228 | const GrGLInterface* fGL; |
| 229 | GrGLBinding fGLBinding; |
| 230 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | typedef GrGpu INHERITED; |
| 232 | }; |
| 233 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 234 | #endif |
| bsalomon@google.com | fe67652 | 2011-06-17 18:12:21 +0000 | [diff] [blame] | 235 | |