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