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