reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 2 | Copyright 2011 Google Inc. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef GrGpuGL_DEFINED |
| 19 | #define GrGpuGL_DEFINED |
| 20 | |
| 21 | #include "GrGpu.h" |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 22 | #include "GrGLIRect.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | #include "GrGLTexture.h" |
| 24 | |
| 25 | #include "GrGLVertexBuffer.h" |
| 26 | #include "GrGLIndexBuffer.h" |
| 27 | |
| 28 | class GrGpuGL : public GrGpu { |
| 29 | public: |
| 30 | GrGpuGL(); |
| 31 | virtual ~GrGpuGL(); |
| 32 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | protected: |
| 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 | // overrides from GrGpu |
| 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 | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 83 | virtual GrRenderTarget* onCreatePlatformRenderTarget( |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 84 | intptr_t platformRenderTarget, |
| 85 | int stencilBits, |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 86 | bool isMultisampled, |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 87 | int width, int height); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 88 | virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 89 | |
bsalomon@google.com | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 90 | virtual void onClear(GrColor color); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 92 | virtual void onForceRenderTargetFlush(); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 94 | virtual bool onReadPixels(GrRenderTarget* target, |
| 95 | int left, int top, int width, int height, |
| 96 | GrPixelConfig, void* buffer); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 97 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 98 | virtual void onDrawIndexed(GrPrimitiveType type, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | uint32_t startVertex, |
| 100 | uint32_t startIndex, |
| 101 | uint32_t vertexCount, |
| 102 | uint32_t indexCount); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 103 | virtual void onDrawNonIndexed(GrPrimitiveType type, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | 398109c | 2011-04-14 18:40:27 +0000 | [diff] [blame] | 107 | void clearStencil(uint32_t value, uint32_t mask); |
| 108 | virtual void clearStencilClip(const GrIRect& rect); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 109 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 110 | // binds texture unit in GL |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 111 | void setTextureUnit(int unitIdx); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 112 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 113 | // binds appropriate vertex and index buffers, also returns any extra |
| 114 | // extra verts or indices to offset by. |
| 115 | void setBuffers(bool indexed, |
| 116 | int* extraVertexOffset, |
| 117 | int* extraIndexOffset); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 118 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | // flushes state that is common to fixed and programmable GL |
| 120 | // dither |
| 121 | // line smoothing |
| 122 | // blend func |
| 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 | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 129 | // adjusts texture matrix to account for orientation, size, and npotness |
| 130 | static void AdjustTextureMatrix(const GrGLTexture* texture, |
| 131 | GrSamplerState::SampleMode mode, |
| 132 | GrMatrix* matrix); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 134 | // subclass may try to take advantage of identity tex matrices. |
| 135 | // This helper determines if matrix will be identity after all |
| 136 | // adjustments are applied. |
| 137 | static bool TextureMatrixIsIdentity(const GrGLTexture* texture, |
| 138 | const GrSamplerState& sampler); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 140 | static bool BlendCoefReferencesConstant(GrBlendCoeff coeff); |
| 141 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 142 | private: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 143 | // notify callbacks to update state tracking when related |
| 144 | // objects are bound to GL or deleted outside of the class |
| 145 | void notifyVertexBufferBind(const GrGLVertexBuffer* buffer); |
| 146 | void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer); |
| 147 | void notifyIndexBufferBind(const GrGLIndexBuffer* buffer); |
| 148 | void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | void notifyTextureDelete(GrGLTexture* texture); |
| 150 | void notifyRenderTargetDelete(GrRenderTarget* renderTarget); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 151 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 152 | void setSpareTextureUnit(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 154 | bool useSmoothLines(); |
| 155 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 156 | void flushRenderTarget(); |
| 157 | void flushStencil(); |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 158 | void flushAAState(GrPrimitiveType type); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 159 | void flushBlend(GrPrimitiveType type); |
| 160 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 161 | void resolveRenderTarget(GrGLRenderTarget* texture); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 163 | bool canBeTexture(GrPixelConfig config, |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 164 | GrGLenum* internalFormat, |
| 165 | GrGLenum* format, |
| 166 | GrGLenum* type); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 167 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 168 | bool fboInternalFormat(GrPixelConfig config, GrGLenum* format); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 169 | |
| 170 | friend class GrGLVertexBuffer; |
| 171 | friend class GrGLIndexBuffer; |
| 172 | friend class GrGLTexture; |
| 173 | friend class GrGLRenderTarget; |
| 174 | |
| 175 | bool fHWBlendDisabled; |
| 176 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 177 | GrGLuint fAASamples[4]; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | enum { |
bsalomon@google.com | a9ecdad | 2011-03-23 13:50:34 +0000 | [diff] [blame] | 179 | kNone_MSFBO = 0, //<! no support for MSAA FBOs |
| 180 | kDesktopARB_MSFBO,//<! GL3.0-style MSAA FBO (GL_ARB_framebuffer_object) |
| 181 | kDesktopEXT_MSFBO,//<! earlier GL_EXT_framebuffer* extensions |
| 182 | kAppleES_MSFBO, //<! GL_APPLE_framebuffer_multisample ES extension |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 183 | } fMSFBOType; |
| 184 | |
| 185 | // Do we have stencil wrap ops. |
| 186 | bool fHasStencilWrap; |
| 187 | |
| 188 | // ES requires an extension to support RGBA8 in RenderBufferStorage |
| 189 | bool fRGBA8Renderbuffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 190 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 191 | int fActiveTextureUnitIdx; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 192 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 193 | typedef GrGpu INHERITED; |
| 194 | }; |
| 195 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 196 | #endif |