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