1 /* 2 Copyright 2011 Google Inc. 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" 22 #include "GrGLIRect.h" 23 #include "GrGLTexture.h" 24 25 #include "GrGLVertexBuffer.h" 26 #include "GrGLIndexBuffer.h" 27 28 class GrGpuGL : public GrGpu { 29 public: 30 virtual ~GrGpuGL(); 31 32 protected: 33 GrGpuGL(); 34 35 struct { 36 size_t fVertexOffset; 37 GrVertexLayout fVertexLayout; 38 const GrVertexBuffer* fVertexBuffer; 39 const GrIndexBuffer* fIndexBuffer; 40 bool fArrayPtrsDirty; 41 } fHWGeometryState; 42 43 struct AAState { 44 bool fMSAAEnabled; 45 bool fSmoothLineEnabled; 46 } fHWAAState; 47 48 DrState fHWDrawState; 49 bool fHWStencilClip; 50 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; 58 int fTextureChangedMask; 59 } fDirtyFlags; 60 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages); 61 62 // clears the dirty flags 63 void resetDirtyFlags(); 64 65 // last scissor / viewport scissor state seen by the GL. 66 struct { 67 bool fScissorEnabled; 68 GrGLIRect fScissorRect; 69 GrGLIRect fViewportRect; 70 } fHWBounds; 71 72 // GrGpu overrides 73 // overrides from GrGpu 74 virtual void resetContext(); 75 76 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc, 77 const void* srcData, 78 size_t rowBytes); 79 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size, 80 bool dynamic); 81 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size, 82 bool dynamic); 83 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc); 84 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState(); 85 86 virtual void onClear(const GrIRect* rect, GrColor color); 87 88 virtual void onForceRenderTargetFlush(); 89 90 virtual bool onReadPixels(GrRenderTarget* target, 91 int left, int top, int width, int height, 92 GrPixelConfig, void* buffer); 93 94 virtual void onDrawIndexed(GrPrimitiveType type, 95 uint32_t startVertex, 96 uint32_t startIndex, 97 uint32_t vertexCount, 98 uint32_t indexCount); 99 virtual void onDrawNonIndexed(GrPrimitiveType type, 100 uint32_t vertexCount, 101 uint32_t numVertices); 102 virtual void flushScissor(const GrIRect* rect); 103 void clearStencil(uint32_t value, uint32_t mask); 104 virtual void clearStencilClip(const GrIRect& rect); 105 virtual int getMaxEdges() const; 106 107 // binds texture unit in GL 108 void setTextureUnit(int unitIdx); 109 110 // binds appropriate vertex and index buffers, also returns any extra 111 // extra verts or indices to offset by. 112 void setBuffers(bool indexed, 113 int* extraVertexOffset, 114 int* extraIndexOffset); 115 116 // flushes state that is common to fixed and programmable GL 117 // dither 118 // line smoothing 119 // texture binding 120 // sampler state (filtering, tiling) 121 // FBO binding 122 // line width 123 bool flushGLStateCommon(GrPrimitiveType type); 124 125 // subclass should call this to flush the blend state 126 void flushBlend(GrPrimitiveType type, 127 GrBlendCoeff srcCoeff, 128 GrBlendCoeff dstCoeff); 129 130 // adjusts texture matrix to account for orientation, size, and npotness 131 static void AdjustTextureMatrix(const GrGLTexture* texture, 132 GrSamplerState::SampleMode mode, 133 GrMatrix* matrix); 134 135 // subclass may try to take advantage of identity tex matrices. 136 // This helper determines if matrix will be identity after all 137 // adjustments are applied. 138 static bool TextureMatrixIsIdentity(const GrGLTexture* texture, 139 const GrSamplerState& sampler); 140 141 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff); 142 143 private: 144 145 // notify callbacks to update state tracking when related 146 // objects are bound to GL or deleted outside of the class 147 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer); 148 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer); 149 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer); 150 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer); 151 void notifyTextureDelete(GrGLTexture* texture); 152 void notifyRenderTargetDelete(GrRenderTarget* renderTarget); 153 154 void setSpareTextureUnit(); 155 156 bool useSmoothLines(); 157 158 // bound is region that may be modified and therefore has to be resolved. 159 // NULL means whole target. Can be an empty rect. 160 void flushRenderTarget(const GrIRect* bound); 161 void flushStencil(); 162 void flushAAState(GrPrimitiveType type); 163 164 void resolveRenderTarget(GrGLRenderTarget* texture); 165 166 bool canBeTexture(GrPixelConfig config, 167 GrGLenum* internalFormat, 168 GrGLenum* format, 169 GrGLenum* type); 170 171 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format); 172 173 friend class GrGLVertexBuffer; 174 friend class GrGLIndexBuffer; 175 friend class GrGLTexture; 176 friend class GrGLRenderTarget; 177 178 bool fHWBlendDisabled; 179 180 GrGLuint fAASamples[4]; 181 enum { 182 kNone_MSFBO = 0, //<! no support for MSAA FBOs 183 kDesktopARB_MSFBO,//<! GL3.0-style MSAA FBO (GL_ARB_framebuffer_object) 184 kDesktopEXT_MSFBO,//<! earlier GL_EXT_framebuffer* extensions 185 kAppleES_MSFBO, //<! GL_APPLE_framebuffer_multisample ES extension 186 } fMSFBOType; 187 188 // Do we have stencil wrap ops. 189 bool fHasStencilWrap; 190 191 // The maximum number of fragment uniform vectors (GLES has min. 16). 192 int fMaxFragmentUniformVectors; 193 194 // ES requires an extension to support RGBA8 in RenderBufferStorage 195 bool fRGBA8Renderbuffer; 196 197 int fActiveTextureUnitIdx; 198 199 typedef GrGpu INHERITED; 200 }; 201 202 #endif 203