| 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 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | #ifndef GrGpu_DEFINED |
| 18 | #define GrGpu_DEFINED |
| 19 | |
| 20 | #include "GrRect.h" |
| 21 | #include "GrRefCnt.h" |
| 22 | #include "GrDrawTarget.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | #include "GrTexture.h" |
| bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 24 | #include "GrPathRenderer.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 26 | class GrVertexBufferAllocPool; |
| 27 | class GrIndexBufferAllocPool; |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 28 | class GrResource; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | |
| 30 | class GrGpu : public GrDrawTarget { |
| 31 | |
| 32 | public: |
| 33 | /** |
| 34 | * Possible 3D APIs that may be used by Ganesh. |
| 35 | */ |
| 36 | enum Engine { |
| 37 | kOpenGL_Shaders_Engine, |
| 38 | kOpenGL_Fixed_Engine, |
| 39 | kDirect3D9_Engine |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Platform specific 3D context. |
| 44 | * For |
| 45 | * kOpenGL_Shaders_Engine use NULL |
| 46 | * kOpenGL_Fixed_Engine use NULL |
| 47 | * kDirect3D9_Engine use an IDirect3DDevice9* |
| 48 | */ |
| 49 | typedef void* Platform3DContext; |
| 50 | |
| 51 | /** |
| 52 | * Create an instance of GrGpu that matches the specified Engine backend. |
| 53 | * If the requested engine is not supported (at compile-time or run-time) |
| 54 | * this returns NULL. |
| 55 | */ |
| 56 | static GrGpu* Create(Engine, Platform3DContext context3D); |
| 57 | |
| 58 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | * Used to control the level of antialiasing available for a rendertarget. |
| 60 | * Anti-alias quality levels depend on the underlying API/GPU capabilities. |
| 61 | */ |
| 62 | enum AALevels { |
| 63 | kNone_AALevel, //<! No antialiasing available. |
| 64 | kLow_AALevel, //<! Low quality antialiased rendering. Actual |
| 65 | // interpretation is platform-dependent. |
| 66 | kMed_AALevel, //<! Medium quality antialiased rendering. Actual |
| 67 | // interpretation is platform-dependent. |
| 68 | kHigh_AALevel, //<! High quality antialiased rendering. Actual |
| 69 | // interpretation is platform-dependent. |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Optional bitfield flags that can be passed to createTexture. |
| 75 | */ |
| 76 | enum TextureFlags { |
| 77 | kRenderTarget_TextureFlag = 0x1, //<! Creates a texture that can be |
| 78 | // rendered to by calling |
| 79 | // GrGpu::setRenderTarget() with |
| 80 | // GrTexture::asRenderTarget(). |
| bsalomon@google.com | f6a7c11 | 2011-03-24 16:14:10 +0000 | [diff] [blame] | 81 | kNoStencil_TextureFlag = 0x2, //<! If the texture is used as a |
| 82 | // rendertarget but a stencil |
| 83 | // buffer is not required. Stencil |
| 84 | // may be required for clipping and |
| 85 | // path rendering. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | kDynamicUpdate_TextureFlag = 0x4 //!< Hint that the CPU may modify |
| 87 | // this texture after creation |
| 88 | }; |
| 89 | |
| 90 | enum { |
| 91 | /** |
| 92 | * For Index8 pixel config, the colortable must be 256 entries |
| 93 | */ |
| 94 | kColorTableSize = 256 * sizeof(GrColor) |
| 95 | }; |
| 96 | /** |
| 97 | * Describes a texture to be created. |
| 98 | */ |
| 99 | struct TextureDesc { |
| 100 | uint32_t fFlags; //!< bitfield of TextureFlags |
| 101 | GrGpu::AALevels fAALevel;//!< The level of antialiasing available |
| 102 | // for a rendertarget texture. Only |
| 103 | // flags contains |
| 104 | // kRenderTarget_TextureFlag. |
| 105 | uint32_t fWidth; //!< Width of the texture |
| 106 | uint32_t fHeight; //!< Height of the texture |
| 107 | GrTexture::PixelConfig fFormat; //!< Format of source data of the |
| 108 | // texture. Not guaraunteed to be the |
| 109 | // same as internal format used by |
| 110 | // 3D API. |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * Gpu usage statistics. |
| 115 | */ |
| 116 | struct Stats { |
| 117 | uint32_t fVertexCnt; //<! Number of vertices drawn |
| 118 | uint32_t fIndexCnt; //<! Number of indices drawn |
| 119 | uint32_t fDrawCnt; //<! Number of draws |
| 120 | |
| 121 | uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed) |
| 122 | |
| 123 | /* |
| 124 | * Number of times the texture is set in 3D API |
| 125 | */ |
| 126 | uint32_t fTextureChngCnt; |
| 127 | /* |
| 128 | * Number of times the render target is set in 3D API |
| 129 | */ |
| 130 | uint32_t fRenderTargetChngCnt; |
| 131 | /* |
| 132 | * Number of textures created (includes textures that are rendertargets). |
| 133 | */ |
| 134 | uint32_t fTextureCreateCnt; |
| 135 | /* |
| 136 | * Number of rendertargets created. |
| 137 | */ |
| 138 | uint32_t fRenderTargetCreateCnt; |
| 139 | }; |
| 140 | |
| 141 | //////////////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | GrGpu(); |
| 144 | virtual ~GrGpu(); |
| 145 | |
| 146 | /** |
| 147 | * The GrGpu object normally assumes that no outsider is setting state |
| 148 | * within the underlying 3D API's context/device/whatever. This call informs |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 149 | * the GrGpu that the state was modified and it shouldn't make assumptions |
| 150 | * about the state. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 152 | void markContextDirty() { fContextIsDirty = true; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | |
| 154 | void unimpl(const char[]); |
| 155 | |
| 156 | /** |
| bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 157 | * Creates a texture object. If desc width or height is not a power of |
| 158 | * two but underlying API requires a power of two texture then srcData |
| 159 | * will be embedded in a power of two texture. The extra width and height |
| 160 | * is filled as though srcData were rendered clamped into the texture. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | * |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 162 | * If kRenderTarget_TextureFlag is specified the GrRenderTarget is |
| bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 163 | * accessible via GrTexture::asRenderTarget(). The texture will hold a ref |
| 164 | * on the render target until its releaseRenderTarget() is called or it is |
| 165 | * destroyed. |
| 166 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | * @param desc describes the texture to be created. |
| 168 | * @param srcData texel data to load texture. Begins with full-size |
| 169 | * palette data for paletted textures. Contains width* |
| 170 | * height texels. If NULL texture data is uninitialized. |
| 171 | * |
| 172 | * @return The texture object if successful, otherwise NULL. |
| 173 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 174 | GrTexture* createTexture(const TextureDesc& desc, |
| 175 | const void* srcData, size_t rowBytes); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 176 | /** |
| 177 | * Wraps an externally-created rendertarget in a GrRenderTarget. |
| 178 | * @param platformRenderTarget handle to the the render target in the |
| 179 | * underlying 3D API. Interpretation depends on |
| 180 | * GrGpu subclass in use. |
| bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 181 | * @param stencilBits number of stencil bits the target has |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | * @param width width of the render target |
| 183 | * @param height height of the render target |
| 184 | */ |
| 185 | virtual GrRenderTarget* createPlatformRenderTarget( |
| 186 | intptr_t platformRenderTarget, |
| bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 187 | int stencilBits, |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 188 | int width, int height); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 189 | |
| 190 | /** |
| bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 191 | * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and |
| 192 | * viewport state from the underlying 3D API and wraps it in a |
| 193 | * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the |
| 194 | * underlying object in its destructor and it is up to caller to guarantee |
| 195 | * that it remains valid while the GrRenderTarget is used. |
| 196 | * |
| 197 | * @return the newly created GrRenderTarget |
| 198 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 199 | GrRenderTarget* createRenderTargetFrom3DApiState(); |
| bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 200 | |
| 201 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 202 | * Creates a vertex buffer. |
| 203 | * |
| 204 | * @param size size in bytes of the vertex buffer |
| 205 | * @param dynamic hints whether the data will be frequently changed |
| 206 | * by either GrVertexBuffer::lock or |
| 207 | * GrVertexBuffer::updateData. |
| 208 | * |
| 209 | * @return The vertex buffer if successful, otherwise NULL. |
| 210 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 211 | GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * Creates an index buffer. |
| 215 | * |
| 216 | * @param size size in bytes of the index buffer |
| 217 | * @param dynamic hints whether the data will be frequently changed |
| 218 | * by either GrIndexBuffer::lock or |
| 219 | * GrIndexBuffer::updateData. |
| 220 | * |
| 221 | * @return The index buffer if successful, otherwise NULL. |
| 222 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 223 | GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 224 | |
| 225 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | * Erase the entire render target, ignoring any clips/scissors. |
| 227 | * |
| 228 | * This is issued to the GPU driver immediately. |
| 229 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 230 | void eraseColor(GrColor color); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * Are 8 bit paletted textures supported. |
| 234 | * |
| 235 | * @return true if 8bit palette textures are supported, false otherwise |
| 236 | */ |
| 237 | bool supports8BitPalette() const { return f8bitPaletteSupport; } |
| 238 | |
| 239 | /** |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 240 | * returns true if two sided stenciling is supported. If false then only |
| 241 | * the front face values of the GrStencilSettings |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 242 | * @return true if only a single stencil pass is needed. |
| 243 | */ |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 244 | bool supportsTwoSidedStencil() const |
| 245 | { return fTwoSidedStencilSupport; } |
| 246 | |
| 247 | /** |
| 248 | * returns true if stencil wrap is supported. If false then |
| 249 | * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as |
| 250 | * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively. |
| 251 | * @return true if stencil wrap ops are supported. |
| 252 | */ |
| 253 | bool supportsStencilWrapOps() const |
| 254 | { return fStencilWrapOpsSupport; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * Checks whether locking vertex and index buffers is supported. |
| 258 | * |
| 259 | * @return true if locking is supported. |
| 260 | */ |
| 261 | bool supportsBufferLocking() const { return fBufferLockSupport; } |
| 262 | |
| 263 | /** |
| 264 | * Gets the minimum width of a render target. If a texture/rt is created |
| 265 | * with a width less than this size the GrGpu object will clamp it to this |
| 266 | * value. |
| 267 | */ |
| 268 | int minRenderTargetWidth() const { return fMinRenderTargetWidth; } |
| 269 | |
| 270 | /** |
| 271 | * Gets the minimum width of a render target. If a texture/rt is created |
| 272 | * with a height less than this size the GrGpu object will clamp it to this |
| 273 | * value. |
| 274 | */ |
| 275 | int minRenderTargetHeight() const { return fMinRenderTargetHeight; } |
| 276 | |
| 277 | /** |
| bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 278 | * Returns true if NPOT textures can be created |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 279 | * |
| bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 280 | * @return true if NPOT textures can be created |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 281 | */ |
| bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 282 | bool npotTextureSupport() const { return fNPOTTextureSupport; } |
| 283 | |
| 284 | /** |
| 285 | * Returns true if NPOT textures can be repeat/mirror tiled. |
| 286 | * |
| 287 | * @return true if NPOT textures can be tiled |
| 288 | */ |
| 289 | bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; } |
| 290 | |
| 291 | /** |
| 292 | * Returns true if a NPOT texture can be a rendertarget |
| 293 | * |
| 294 | * @return the true if NPOT texture/rendertarget can be created. |
| 295 | */ |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 296 | bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | |
| reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 298 | int maxTextureDimension() const { return fMaxTextureDimension; } |
| 299 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 300 | // GrDrawTarget overrides |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 301 | virtual void drawIndexed(GrPrimitiveType type, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 302 | int startVertex, |
| 303 | int startIndex, |
| 304 | int vertexCount, |
| 305 | int indexCount); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 306 | |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 307 | virtual void drawNonIndexed(GrPrimitiveType type, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 308 | int startVertex, |
| 309 | int vertexCount); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 310 | |
| 311 | /** |
| bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 312 | * Installs a path renderer that will be used to draw paths that are |
| 313 | * part of the clip. |
| 314 | */ |
| 315 | void setClipPathRenderer(GrPathRenderer* pathRenderer) { |
| 316 | GrSafeAssign(fClientPathRenderer, pathRenderer); |
| 317 | } |
| 318 | |
| 319 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 320 | * Returns an index buffer that can be used to render quads. |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 321 | * Six indices per quad: 0, 1, 2, 0, 2, 3, etc. |
| 322 | * The max number of quads can be queried using GrIndexBuffer::maxQuads(). |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 323 | * Draw with kTriangles_PrimitiveType |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 324 | * @ return the quad index buffer |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 325 | */ |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 326 | const GrIndexBuffer* getQuadIndexBuffer() const; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 327 | |
| 328 | /** |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 329 | * Returns a vertex buffer with four position-only vertices [(0,0), (1,0), |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 330 | * (1,1), (0,1)]. |
| 331 | * @ return unit square vertex buffer |
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 332 | */ |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 333 | const GrVertexBuffer* getUnitSquareVertexBuffer() const; |
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 334 | |
| 335 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 336 | * Ensures that the current render target is actually set in the |
| 337 | * underlying 3D API. Used when client wants to use 3D API to directly |
| 338 | * render to the RT. |
| 339 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 340 | void forceRenderTargetFlush(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 341 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 342 | /** |
| 343 | * Reads a rectangle of pixels from the current render target. |
| 344 | * @param left left edge of the rectangle to read (inclusive) |
| 345 | * @param top top edge of the rectangle to read (inclusive) |
| 346 | * @param width width of rectangle to read in pixels. |
| 347 | * @param height height of rectangle to read in pixels. |
| 348 | * @param buffer memory to read the rectangle into. |
| 349 | * |
| 350 | * @return true if the read succeeded, false if not. The read can fail |
| 351 | * because of a unsupported pixel config or because no render |
| 352 | * target is currently set. |
| 353 | */ |
| 354 | bool readPixels(int left, int top, int width, int height, |
| 355 | GrTexture::PixelConfig, void* buffer); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 356 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 357 | const Stats& getStats() const; |
| 358 | void resetStats(); |
| 359 | void printStats() const; |
| 360 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 361 | /** |
| 362 | * Called to tell Gpu object that all GrResources have been lost and should |
| 363 | * be abandoned. |
| 364 | */ |
| 365 | void abandonResources(); |
| 366 | |
| 367 | /** |
| 368 | * Called to tell Gpu object to release all GrResources. |
| 369 | */ |
| 370 | void releaseResources(); |
| 371 | |
| 372 | /** |
| 373 | * Add resource to list of resources. Should only be called by GrResource. |
| 374 | * @param resource the resource to add. |
| 375 | */ |
| 376 | void insertResource(GrResource* resource); |
| 377 | |
| 378 | /** |
| 379 | * Remove resource from list of resources. Should only be called by |
| 380 | * GrResource. |
| 381 | * @param resource the resource to remove. |
| 382 | */ |
| 383 | void removeResource(GrResource* resource); |
| 384 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 385 | protected: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 386 | enum PrivateStateBits { |
| 387 | kFirstBit = (kLastPublicStateBit << 1), |
| 388 | |
| 389 | kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify |
| 390 | // stencil bits used for |
| 391 | // clipping. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | /** |
| 395 | * Extensions to GrDrawTarget::StateBits to implement stencil clipping |
| 396 | */ |
| 397 | struct ClipState { |
| 398 | bool fClipInStencil; |
| 399 | bool fClipIsDirty; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 400 | } fClipState; |
| 401 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 402 | // GrDrawTarget override |
| 403 | virtual void clipWillBeSet(const GrClip& newClip); |
| 404 | |
| 405 | // prepares clip flushes gpu state before a draw |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 406 | bool setupClipAndFlushState(GrPrimitiveType type); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 407 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 408 | // Functions used to map clip-respecting stencil tests into normal |
| 409 | // stencil funcs supported by GPUs. |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 410 | static GrStencilFunc ConvertStencilFunc(bool stencilInClip, |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 411 | GrStencilFunc func); |
| 412 | static void ConvertStencilFuncAndMask(GrStencilFunc func, |
| 413 | bool clipInStencil, |
| 414 | unsigned int clipBit, |
| 415 | unsigned int userBits, |
| 416 | unsigned int* ref, |
| 417 | unsigned int* mask); |
| 418 | |
| 419 | // stencil settings to clip drawing when stencil clipping is in effect |
| 420 | // and the client isn't using the stencil test. |
| 421 | static const GrStencilSettings gClipStencilSettings; |
| 422 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 423 | // defaults to false, subclass can set true to support palleted textures |
| 424 | bool f8bitPaletteSupport; |
| 425 | |
| bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 426 | // set by subclass |
| 427 | bool fNPOTTextureSupport; |
| 428 | bool fNPOTTextureTileSupport; |
| 429 | bool fNPOTRenderTargetSupport; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 430 | bool fTwoSidedStencilSupport; |
| 431 | bool fStencilWrapOpsSupport; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 432 | |
| 433 | // set by subclass to true if index and vertex buffers can be locked, false |
| 434 | // otherwise. |
| 435 | bool fBufferLockSupport; |
| 436 | |
| 437 | // set by subclass |
| 438 | int fMinRenderTargetWidth; |
| 439 | int fMinRenderTargetHeight; |
| reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 440 | int fMaxTextureDimension; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 441 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 442 | Stats fStats; |
| 443 | |
| 444 | const GrVertexBuffer* fCurrPoolVertexBuffer; |
| 445 | int fCurrPoolStartVertex; |
| 446 | |
| 447 | const GrIndexBuffer* fCurrPoolIndexBuffer; |
| 448 | int fCurrPoolStartIndex; |
| 449 | |
| 450 | // GrDrawTarget overrides |
| 451 | virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout, |
| 452 | void** vertices, |
| 453 | void** indices); |
| 454 | virtual void releaseGeometryHelper(); |
| 455 | |
| 456 | virtual void setVertexSourceToArrayHelper(const void* vertexArray, |
| 457 | int vertexCount); |
| 458 | |
| 459 | virtual void setIndexSourceToArrayHelper(const void* indexArray, |
| 460 | int indexCount); |
| 461 | // Helpers for setting up geometry state |
| 462 | void finalizeReservedVertices(); |
| 463 | void finalizeReservedIndices(); |
| 464 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 465 | // overridden by API-specific derived class to handle re-emitting 3D API |
| 466 | // preample and dirtying state cache. |
| 467 | virtual void resetContext() = 0; |
| 468 | |
| 469 | // overridden by API-specific derived class to create objects. |
| 470 | virtual GrTexture* createTextureHelper(const TextureDesc& desc, |
| 471 | const void* srcData, |
| 472 | size_t rowBytes) = 0; |
| 473 | virtual GrRenderTarget* createPlatformRenderTargetHelper( |
| 474 | intptr_t platformRenderTarget, |
| 475 | int stencilBits, |
| 476 | int width, int height) = 0; |
| 477 | virtual GrRenderTarget* createRenderTargetFrom3DApiStateHelper() = 0; |
| 478 | virtual GrVertexBuffer* createVertexBufferHelper(uint32_t size, |
| 479 | bool dynamic) = 0; |
| 480 | virtual GrIndexBuffer* createIndexBufferHelper(uint32_t size, |
| 481 | bool dynamic) = 0; |
| 482 | |
| 483 | // overridden by API-specific derivated class to perform the erase. |
| 484 | virtual void eraseColorHelper(GrColor color) = 0; |
| 485 | |
| 486 | // overridden by API-specific derived class to perform the draw call. |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 487 | virtual void drawIndexedHelper(GrPrimitiveType type, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 488 | uint32_t startVertex, |
| 489 | uint32_t startIndex, |
| 490 | uint32_t vertexCount, |
| 491 | uint32_t indexCount) = 0; |
| 492 | |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 493 | virtual void drawNonIndexedHelper(GrPrimitiveType type, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 494 | uint32_t vertexCount, |
| 495 | uint32_t numVertices) = 0; |
| 496 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 497 | // overridden by API-specific derived class to perform flush |
| 498 | virtual void forceRenderTargetFlushHelper() = 0; |
| 499 | |
| 500 | // overridden by API-specific derived class to perform the read pixels. |
| 501 | virtual bool readPixelsHelper(int left, int top, int width, int height, |
| 502 | GrTexture::PixelConfig, void* buffer) = 0; |
| 503 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 504 | // called to program the vertex data, indexCount will be 0 if drawing non- |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 505 | // indexed geometry. The subclass may adjust the startVertex and/or |
| 506 | // startIndex since it may have already accounted for these in the setup. |
| 507 | virtual void setupGeometry(int* startVertex, |
| 508 | int* startIndex, |
| 509 | int vertexCount, |
| 510 | int indexCount) = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 511 | |
| 512 | |
| 513 | // The GrGpu typically records the clients requested state and then flushes |
| 514 | // deltas from previous state at draw time. This function does the |
| 515 | // API-specific flush of the state |
| 516 | // returns false if current state is unsupported. |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 517 | virtual bool flushGraphicsState(GrPrimitiveType type) = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 518 | |
| 519 | // Sets the scissor rect, or disables if rect is NULL. |
| 520 | virtual void flushScissor(const GrIRect* rect) = 0; |
| 521 | |
| 522 | // GrGpu subclass removes the clip from the stencil buffer |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 523 | virtual void eraseStencilClip(const GrIRect& rect) = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 524 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 525 | private: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 526 | // readies the pools to provide vertex/index data. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 527 | void prepareVertexPool(); |
| 528 | void prepareIndexPool(); |
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 529 | |
| bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 530 | // determines the path renderer used to draw a clip path element. |
| 531 | GrPathRenderer* getClipPathRenderer(GrPathIter* path, |
| 532 | GrPathFill fill); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 533 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 534 | void handleDirtyContext() { |
| 535 | if (fContextIsDirty) { |
| 536 | this->resetContext(); |
| 537 | fContextIsDirty = false; |
| 538 | } |
| 539 | } |
| 540 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 541 | GrVertexBufferAllocPool* fVertexPool; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 542 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 543 | GrIndexBufferAllocPool* fIndexPool; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 544 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 545 | mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be |
| 546 | // created on-demand |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 547 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 548 | mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be |
| 549 | // created on-demand |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 550 | |
| bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 551 | GrDefaultPathRenderer* fDefaultPathRenderer; |
| 552 | GrPathRenderer* fClientPathRenderer; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 553 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 554 | bool fContextIsDirty; |
| 555 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 556 | // when in an internal draw these indicate whether the pools are in use |
| 557 | // by one of the outer draws. If false then it is safe to reset the |
| 558 | // pool. |
| 559 | bool fVertexPoolInUse; |
| 560 | bool fIndexPoolInUse; |
| 561 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 562 | GrResource* fResourceHead; |
| 563 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 564 | // used to save and restore state when the GrGpu needs |
| 565 | // to make its geometry pools available internally |
| 566 | class AutoInternalDrawGeomRestore { |
| 567 | public: |
| 568 | AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) { |
| 569 | fGpu = gpu; |
| 570 | |
| 571 | fVertexPoolWasInUse = gpu->fVertexPoolInUse; |
| 572 | fIndexPoolWasInUse = gpu->fIndexPoolInUse; |
| 573 | |
| 574 | gpu->fVertexPoolInUse = fVertexPoolWasInUse || |
| 575 | (kBuffer_GeometrySrcType != |
| 576 | gpu->fGeometrySrc.fVertexSrc); |
| 577 | gpu->fIndexPoolInUse = fIndexPoolWasInUse || |
| 578 | (kBuffer_GeometrySrcType != |
| 579 | gpu->fGeometrySrc.fIndexSrc);; |
| 580 | |
| 581 | fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer; |
| 582 | fSavedPoolStartVertex = gpu->fCurrPoolStartVertex; |
| 583 | fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer; |
| 584 | fSavedPoolStartIndex = gpu->fCurrPoolStartIndex; |
| 585 | |
| 586 | fSavedReservedGeometry = gpu->fReservedGeometry; |
| 587 | gpu->fReservedGeometry.fLocked = false; |
| 588 | } |
| 589 | ~AutoInternalDrawGeomRestore() { |
| 590 | fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer; |
| 591 | fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex; |
| 592 | fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer; |
| 593 | fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex; |
| 594 | fGpu->fVertexPoolInUse = fVertexPoolWasInUse; |
| 595 | fGpu->fIndexPoolInUse = fIndexPoolWasInUse; |
| 596 | fGpu->fReservedGeometry = fSavedReservedGeometry; |
| 597 | } |
| 598 | private: |
| 599 | AutoGeometrySrcRestore fAgsr; |
| 600 | GrGpu* fGpu; |
| 601 | const GrVertexBuffer* fSavedPoolVertexBuffer; |
| 602 | int fSavedPoolStartVertex; |
| 603 | const GrIndexBuffer* fSavedPoolIndexBuffer; |
| 604 | int fSavedPoolStartIndex; |
| 605 | bool fVertexPoolWasInUse; |
| 606 | bool fIndexPoolWasInUse; |
| 607 | ReservedGeometry fSavedReservedGeometry; |
| 608 | }; |
| 609 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 610 | typedef GrDrawTarget INHERITED; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 611 | }; |
| 612 | |
| 613 | #endif |