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 { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 64 | size_t fVertexOffset; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 65 | GrVertexLayout fVertexLayout; |
| 66 | const GrVertexBuffer* fVertexBuffer; |
| 67 | const GrIndexBuffer* fIndexBuffer; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 68 | bool fArrayPtrsDirty; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | } fHWGeometryState; |
| 70 | |
reed@google.com | 8195f67 | 2011-01-12 18:14:28 +0000 | [diff] [blame] | 71 | DrState fHWDrawState; |
| 72 | bool fHWStencilClip; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 74 | // GrGpu overrides |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | virtual void drawIndexedHelper(PrimitiveType type, |
| 76 | uint32_t startVertex, |
| 77 | uint32_t startIndex, |
| 78 | uint32_t vertexCount, |
| 79 | uint32_t indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | virtual void drawNonIndexedHelper(PrimitiveType type, |
| 81 | uint32_t vertexCount, |
| 82 | uint32_t numVertices); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | virtual void flushScissor(const GrIRect* rect); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | void eraseStencil(uint32_t value, uint32_t mask); |
| 85 | virtual void eraseStencilClip(); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 86 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 87 | // binds texture unit in GL |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 88 | void setTextureUnit(int unitIdx); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 90 | // binds appropriate vertex and index buffers, also returns any extra |
| 91 | // extra verts or indices to offset by. |
| 92 | void setBuffers(bool indexed, |
| 93 | int* extraVertexOffset, |
| 94 | int* extraIndexOffset); |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 95 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 96 | // flushes state that is common to fixed and programmable GL |
| 97 | // dither |
| 98 | // line smoothing |
| 99 | // blend func |
| 100 | // texture binding |
| 101 | // sampler state (filtering, tiling) |
| 102 | // FBO binding |
| 103 | // line width |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 104 | bool flushGLStateCommon(PrimitiveType type); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | // set when this class changes the rendertarget. |
| 107 | // Subclass should notice at flush time, take appropriate action, |
| 108 | // and set false. |
| 109 | bool fRenderTargetChanged; |
| 110 | |
| 111 | // set by eraseColor or eraseStencil. Picked up in in flushStencil. |
| 112 | bool fWriteMaskChanged; |
| 113 | |
| 114 | // last scissor / viewport scissor state seen by the GL. |
| 115 | BoundsState fHWBounds; |
| 116 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | GrGLExts fExts; |
| 118 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 119 | private: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 120 | void resetContextHelper(); |
| 121 | |
| 122 | // notify callbacks to update state tracking when related |
| 123 | // objects are bound to GL or deleted outside of the class |
| 124 | void notifyVertexBufferBind(const GrGLVertexBuffer* buffer); |
| 125 | void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer); |
| 126 | void notifyIndexBufferBind(const GrGLIndexBuffer* buffer); |
| 127 | void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | void notifyTextureDelete(GrGLTexture* texture); |
| 129 | void notifyRenderTargetDelete(GrRenderTarget* renderTarget); |
| 130 | void notifyTextureRemoveRenderTarget(GrGLTexture* texture); |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 131 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 132 | void setSpareTextureUnit(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | |
| 134 | void flushRenderTarget(); |
| 135 | void flushStencil(); |
| 136 | void resolveTextureRenderTarget(GrGLTexture* texture); |
| 137 | |
| 138 | bool canBeTexture(GrTexture::PixelConfig config, |
| 139 | GLenum* internalFormat, |
| 140 | GLenum* format, |
| 141 | GLenum* type); |
| 142 | bool fboInternalFormat(GrTexture::PixelConfig config, GLenum* format); |
| 143 | |
| 144 | friend class GrGLVertexBuffer; |
| 145 | friend class GrGLIndexBuffer; |
| 146 | friend class GrGLTexture; |
| 147 | friend class GrGLRenderTarget; |
| 148 | |
| 149 | bool fHWBlendDisabled; |
| 150 | |
| 151 | GLuint fAASamples[4]; |
| 152 | enum { |
| 153 | kNone_MSFBO = 0, |
| 154 | kDesktop_MSFBO, |
| 155 | kApple_MSFBO, |
| 156 | kIMG_MSFBO |
| 157 | } fMSFBOType; |
| 158 | |
| 159 | // Do we have stencil wrap ops. |
| 160 | bool fHasStencilWrap; |
| 161 | |
| 162 | // ES requires an extension to support RGBA8 in RenderBufferStorage |
| 163 | bool fRGBA8Renderbuffer; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 164 | |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 165 | int fActiveTextureUnitIdx; |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 166 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | typedef GrGpu INHERITED; |
| 168 | }; |
| 169 | |
| 170 | bool has_gl_extension(const char* ext); |
| 171 | void gl_version(int* major, int* minor); |
| 172 | |
| 173 | /** |
| 174 | * GrGL_RestoreResetRowLength() will reset GL_UNPACK_ROW_LENGTH to 0. We write |
| 175 | * this wrapper, since GL_UNPACK_ROW_LENGTH is not available on all GL versions |
| 176 | */ |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 177 | #if GR_SUPPORT_GLDESKTOP |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | static inline void GrGL_RestoreResetRowLength() { |
| 179 | GR_GL(PixelStorei(GL_UNPACK_ROW_LENGTH, 0)); |
| 180 | } |
| 181 | #else |
| 182 | #define GrGL_RestoreResetRowLength() |
| 183 | #endif |
| 184 | |
reed@google.com | ac20fb9 | 2011-01-12 17:14:53 +0000 | [diff] [blame] | 185 | /* |
| 186 | * Some drivers want the var-int arg to be zero-initialized on input. |
| 187 | */ |
| 188 | #define GR_GL_INIT_ZERO 0 |
| 189 | #define GR_GL_GetIntegerv(e, p) \ |
| 190 | do { \ |
| 191 | *(p) = GR_GL_INIT_ZERO; \ |
| 192 | GR_GL(GetIntegerv(e, p)); \ |
| 193 | } while (0) |
| 194 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
| 197 | |