reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 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 "GrGLConfig.h" |
| 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 | |
| 33 | // overrides from GrGpu |
| 34 | virtual void resetContext(); |
| 35 | |
| 36 | virtual GrTexture* createTexture(const TextureDesc& desc, |
| 37 | const void* srcData, size_t rowBytes); |
| 38 | virtual GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic); |
| 39 | virtual GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic); |
| 40 | |
| 41 | virtual GrRenderTarget* createPlatformRenderTarget( |
| 42 | intptr_t platformRenderTarget, |
| 43 | int width, int height); |
| 44 | |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 45 | virtual GrRenderTarget* createRenderTargetFrom3DApiState(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | |
| 47 | virtual void eraseColor(GrColor color); |
| 48 | |
| 49 | virtual void forceRenderTargetFlush(); |
| 50 | |
| 51 | virtual bool readPixels(int left, int top, int width, int height, |
| 52 | GrTexture::PixelConfig, void* buffer); |
| 53 | |
| 54 | /** |
| 55 | * Gets the struct containing the GL extensions for the context |
| 56 | * underlying the GrGpuGL |
| 57 | * |
| 58 | * @param struct containing extension function pointers |
| 59 | */ |
| 60 | const GrGLExts& extensions() { return fExts; } |
| 61 | |
| 62 | protected: |
| 63 | struct { |
| 64 | const void* |
| 65 | fPositionPtr; |
| 66 | GrVertexLayout fVertexLayout; |
| 67 | const GrVertexBuffer* fVertexBuffer; |
| 68 | const GrIndexBuffer* fIndexBuffer; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame^] | 69 | bool fArrayPtrsDirty; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 70 | } fHWGeometryState; |
| 71 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 72 | DrState fHWDrawState; |
| 73 | bool fHWStencilClip; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 74 | |
| 75 | virtual void drawIndexedHelper(PrimitiveType type, |
| 76 | uint32_t startVertex, |
| 77 | uint32_t startIndex, |
| 78 | uint32_t vertexCount, |
| 79 | uint32_t indexCount); |
| 80 | |
| 81 | virtual void drawNonIndexedHelper(PrimitiveType type, |
| 82 | uint32_t vertexCount, |
| 83 | uint32_t numVertices); |
| 84 | |
| 85 | virtual void flushScissor(const GrIRect* rect); |
| 86 | |
| 87 | void eraseStencil(uint32_t value, uint32_t mask); |
| 88 | virtual void eraseStencilClip(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 89 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 90 | void setTextureUnit(int unitIdx); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame^] | 92 | // binds appropriate vertex and index buffers and returns either the ptr |
| 93 | // to client memory or offset into a VB of the first vertex |
| 94 | const GLvoid* setBuffersAndGetVertexStart(int vertexStride, int startVertex, |
| 95 | int startIndex, int vertexCount, |
| 96 | int indexCount); |
| 97 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | // flushes state that is common to fixed and programmable GL |
| 99 | // dither |
| 100 | // line smoothing |
| 101 | // blend func |
| 102 | // texture binding |
| 103 | // sampler state (filtering, tiling) |
| 104 | // FBO binding |
| 105 | // line width |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 106 | bool flushGLStateCommon(PrimitiveType type); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | // set when this class changes the rendertarget. |
| 109 | // Subclass should notice at flush time, take appropriate action, |
| 110 | // and set false. |
| 111 | bool fRenderTargetChanged; |
| 112 | |
| 113 | // set by eraseColor or eraseStencil. Picked up in in flushStencil. |
| 114 | bool fWriteMaskChanged; |
| 115 | |
| 116 | // last scissor / viewport scissor state seen by the GL. |
| 117 | BoundsState fHWBounds; |
| 118 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | GrGLExts fExts; |
| 120 | |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame^] | 121 | #if GR_GL_NO_CLIENT_SIDE_ARRAYS |
| 122 | void putClientVertexDataInBuffer(const void* vertexData, |
| 123 | size_t vertexDataSize); |
| 124 | void putClientIndexDataInBuffer(const void* indexData, |
| 125 | size_t indexDataSize); |
| 126 | #endif |
| 127 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 128 | private: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 129 | void resetContextHelper(); |
| 130 | |
| 131 | // notify callbacks to update state tracking when related |
| 132 | // objects are bound to GL or deleted outside of the class |
| 133 | void notifyVertexBufferBind(const GrGLVertexBuffer* buffer); |
| 134 | void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer); |
| 135 | void notifyIndexBufferBind(const GrGLIndexBuffer* buffer); |
| 136 | void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | void notifyTextureDelete(GrGLTexture* texture); |
| 138 | void notifyRenderTargetDelete(GrRenderTarget* renderTarget); |
| 139 | void notifyTextureRemoveRenderTarget(GrGLTexture* texture); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 140 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 141 | void setSpareTextureUnit(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 142 | |
| 143 | void flushRenderTarget(); |
| 144 | void flushStencil(); |
| 145 | void resolveTextureRenderTarget(GrGLTexture* texture); |
| 146 | |
| 147 | bool canBeTexture(GrTexture::PixelConfig config, |
| 148 | GLenum* internalFormat, |
| 149 | GLenum* format, |
| 150 | GLenum* type); |
| 151 | bool fboInternalFormat(GrTexture::PixelConfig config, GLenum* format); |
| 152 | |
| 153 | friend class GrGLVertexBuffer; |
| 154 | friend class GrGLIndexBuffer; |
| 155 | friend class GrGLTexture; |
| 156 | friend class GrGLRenderTarget; |
| 157 | |
| 158 | bool fHWBlendDisabled; |
| 159 | |
| 160 | GLuint fAASamples[4]; |
| 161 | enum { |
| 162 | kNone_MSFBO = 0, |
| 163 | kDesktop_MSFBO, |
| 164 | kApple_MSFBO, |
| 165 | kIMG_MSFBO |
| 166 | } fMSFBOType; |
| 167 | |
| 168 | // Do we have stencil wrap ops. |
| 169 | bool fHasStencilWrap; |
| 170 | |
| 171 | // ES requires an extension to support RGBA8 in RenderBufferStorage |
| 172 | bool fRGBA8Renderbuffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 173 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 174 | int fActiveTextureUnitIdx; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 175 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | typedef GrGpu INHERITED; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame^] | 177 | |
| 178 | #if GR_GL_NO_CLIENT_SIDE_ARRAYS |
| 179 | GrGLVertexBuffer* fClientArrayVB; |
| 180 | GrGLIndexBuffer* fClientArrayIB; |
| 181 | int fOversizeVBDrawCnt; |
| 182 | int fOversizeIBDrawCnt; |
| 183 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | bool has_gl_extension(const char* ext); |
| 187 | void gl_version(int* major, int* minor); |
| 188 | |
| 189 | /** |
| 190 | * GrGL_RestoreResetRowLength() will reset GL_UNPACK_ROW_LENGTH to 0. We write |
| 191 | * this wrapper, since GL_UNPACK_ROW_LENGTH is not available on all GL versions |
| 192 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 193 | #if GR_SUPPORT_GLDESKTOP |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | static inline void GrGL_RestoreResetRowLength() { |
| 195 | GR_GL(PixelStorei(GL_UNPACK_ROW_LENGTH, 0)); |
| 196 | } |
| 197 | #else |
| 198 | #define GrGL_RestoreResetRowLength() |
| 199 | #endif |
| 200 | |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 201 | /* |
| 202 | * Some drivers want the var-int arg to be zero-initialized on input. |
| 203 | */ |
| 204 | #define GR_GL_INIT_ZERO 0 |
| 205 | #define GR_GL_GetIntegerv(e, p) \ |
| 206 | do { \ |
| 207 | *(p) = GR_GL_INIT_ZERO; \ |
| 208 | GR_GL(GetIntegerv(e, p)); \ |
| 209 | } while (0) |
| 210 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |
| 213 | |