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