| 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 | |
| bsalomon@google.com | 1dcf506 | 2011-11-14 19:29:53 +0000 | [diff] [blame] | 34 | uint32_t fProgChngCnt;//<! Number of program changes |
| bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 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: |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 57 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | /** |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 59 | * Additional blend coeffecients for dual source blending, not exposed |
| 60 | * through GrPaint/GrContext. |
| 61 | */ |
| 62 | enum ExtendedBlendCoeffs { |
| 63 | // source 2 refers to second output color when |
| 64 | // using dual source blending. |
| 65 | kS2C_BlendCoeff = kPublicBlendCoeffCount, |
| 66 | kIS2C_BlendCoeff, |
| 67 | kS2A_BlendCoeff, |
| 68 | kIS2A_BlendCoeff, |
| 69 | |
| 70 | kTotalBlendCoeffCount |
| 71 | }; |
| 72 | |
| 73 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 74 | * Create an instance of GrGpu that matches the specified Engine backend. |
| 75 | * If the requested engine is not supported (at compile-time or run-time) |
| 76 | * this returns NULL. |
| 77 | */ |
| bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 78 | static GrGpu* Create(GrEngine, GrPlatform3DContext context3D); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | |
| 80 | //////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | GrGpu(); |
| 83 | virtual ~GrGpu(); |
| 84 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 85 | // The GrContext sets itself as the owner of this Gpu object |
| 86 | void setContext(GrContext* context) { |
| 87 | GrAssert(NULL == fContext); |
| 88 | fContext = context; |
| 89 | } |
| 90 | GrContext* getContext() { return fContext; } |
| 91 | const GrContext* getContext() const { return fContext; } |
| 92 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | /** |
| 94 | * The GrGpu object normally assumes that no outsider is setting state |
| 95 | * 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] | 96 | * the GrGpu that the state was modified and it shouldn't make assumptions |
| 97 | * about the state. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 99 | void markContextDirty() { fContextIsDirty = true; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | |
| 101 | void unimpl(const char[]); |
| 102 | |
| 103 | /** |
| bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 104 | * Creates a texture object. If desc width or height is not a power of |
| 105 | * two but underlying API requires a power of two texture then srcData |
| 106 | * will be embedded in a power of two texture. The extra width and height |
| 107 | * is filled as though srcData were rendered clamped into the texture. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | * |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 109 | * If kRenderTarget_TextureFlag is specified the GrRenderTarget is |
| bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 110 | * accessible via GrTexture::asRenderTarget(). The texture will hold a ref |
| 111 | * on the render target until its releaseRenderTarget() is called or it is |
| 112 | * destroyed. |
| 113 | * |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | * @param desc describes the texture to be created. |
| 115 | * @param srcData texel data to load texture. Begins with full-size |
| 116 | * palette data for paletted textures. Contains width* |
| 117 | * height texels. If NULL texture data is uninitialized. |
| 118 | * |
| 119 | * @return The texture object if successful, otherwise NULL. |
| 120 | */ |
| bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 121 | GrTexture* createTexture(const GrTextureDesc& desc, |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 122 | const void* srcData, size_t rowBytes); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | |
| bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Implements GrContext::createPlatformTexture |
| 126 | */ |
| 127 | GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc); |
| 128 | |
| 129 | /** |
| 130 | * Implements GrContext::createPlatformTexture |
| 131 | */ |
| 132 | GrRenderTarget* createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc); |
| 133 | |
| 134 | /** |
| 135 | * DEPRECATED. This will be removed. |
| 136 | */ |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 137 | GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc); |
| 138 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Creates a vertex buffer. |
| 141 | * |
| 142 | * @param size size in bytes of the vertex buffer |
| 143 | * @param dynamic hints whether the data will be frequently changed |
| 144 | * by either GrVertexBuffer::lock or |
| 145 | * GrVertexBuffer::updateData. |
| 146 | * |
| 147 | * @return The vertex buffer if successful, otherwise NULL. |
| 148 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 149 | GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * Creates an index buffer. |
| 153 | * |
| 154 | * @param size size in bytes of the index buffer |
| 155 | * @param dynamic hints whether the data will be frequently changed |
| 156 | * by either GrIndexBuffer::lock or |
| 157 | * GrIndexBuffer::updateData. |
| 158 | * |
| 159 | * @return The index buffer if successful, otherwise NULL. |
| 160 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 161 | GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | |
| 163 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 164 | * Returns an index buffer that can be used to render quads. |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 165 | * Six indices per quad: 0, 1, 2, 0, 2, 3, etc. |
| 166 | * The max number of quads can be queried using GrIndexBuffer::maxQuads(). |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | * Draw with kTriangles_PrimitiveType |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 168 | * @ return the quad index buffer |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 169 | */ |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 170 | const GrIndexBuffer* getQuadIndexBuffer() const; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 171 | |
| 172 | /** |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 173 | * 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] | 174 | * (1,1), (0,1)]. |
| 175 | * @ return unit square vertex buffer |
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 176 | */ |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 177 | const GrVertexBuffer* getUnitSquareVertexBuffer() const; |
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 178 | |
| 179 | /** |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 180 | * Ensures that the current render target is actually set in the |
| 181 | * underlying 3D API. Used when client wants to use 3D API to directly |
| 182 | * render to the RT. |
| 183 | */ |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 184 | void forceRenderTargetFlush(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 185 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 186 | /** |
| bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 187 | * readPixels with some configs may be slow. Given a desired config this |
| 188 | * function returns a fast-path config. The returned config must have the |
| 189 | * same components, component sizes, and not require conversion between |
| 190 | * pre- and unpremultiplied alpha. The caller is free to ignore the result |
| 191 | * and call readPixels with the original config. |
| 192 | */ |
| bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 193 | virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config) |
| 194 | const { |
| bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 195 | return config; |
| 196 | } |
| 197 | |
| 198 | /** |
| bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 199 | * Same as above but applies to writeTexturePixels |
| 200 | */ |
| bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 201 | virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config) |
| 202 | const { |
| bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 203 | return config; |
| 204 | } |
| 205 | |
| 206 | /** |
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 207 | * OpenGL's readPixels returns the result bottom-to-top while the skia |
| 208 | * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious |
| 209 | * solution is to have the subclass do the flip using either the CPU or GPU. |
| 210 | * However, the caller (GrContext) may have transformations to apply and can |
| 211 | * simply fold in the y-flip for free. On the other hand, the subclass may |
| 212 | * be able to do it for free itself. For example, the subclass may have to |
| 213 | * do memcpys to handle rowBytes that aren't tight. It could do the y-flip |
| 214 | * concurrently. |
| 215 | * |
| 216 | * This function returns true if a y-flip is required to put the pixels in |
| 217 | * top-to-bottom order and the subclass cannot do it for free. |
| 218 | * |
| 219 | * See read pixels for the params |
| 220 | * @return true if calling readPixels with the same set of params will |
| 221 | * produce bottom-to-top data |
| 222 | */ |
| 223 | virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget, |
| 224 | int left, int top, |
| 225 | int width, int height, |
| 226 | GrPixelConfig config, |
| bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 227 | size_t rowBytes) const = 0; |
| 228 | /** |
| 229 | * This should return true if reading a NxM rectangle of pixels from a |
| 230 | * render target is faster if the target has dimensons N and M and the read |
| 231 | * rectangle has its top-left at 0,0. |
| 232 | */ |
| 233 | virtual bool fullReadPixelsIsFasterThanPartial() const { return false; }; |
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 234 | |
| 235 | /** |
| 236 | * Reads a rectangle of pixels from a render target. Fails if read requires |
| 237 | * conversion between premultiplied and unpremultiplied configs. The caller |
| 238 | * should do the conversion by rendering to a target with the desire config |
| 239 | * first. |
| 240 | * |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 241 | * @param renderTarget the render target to read from. NULL means the |
| 242 | * current render target. |
| 243 | * @param left left edge of the rectangle to read (inclusive) |
| 244 | * @param top top edge of the rectangle to read (inclusive) |
| 245 | * @param width width of rectangle to read in pixels. |
| 246 | * @param height height of rectangle to read in pixels. |
| 247 | * @param config the pixel config of the destination buffer |
| 248 | * @param buffer memory to read the rectangle into. |
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 249 | * @param rowBytes the number of bytes between consecutive rows. Zero |
| 250 | * means rows are tightly packed. |
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 251 | * @param invertY buffer should be populated bottom-to-top as opposed |
| 252 | * to top-to-bottom (skia's usual order) |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 253 | * |
| 254 | * @return true if the read succeeded, false if not. The read can fail |
| 255 | * because of a unsupported pixel config or because no render |
| 256 | * target is currently set. |
| 257 | */ |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 258 | bool readPixels(GrRenderTarget* renderTarget, |
| 259 | int left, int top, int width, int height, |
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 260 | GrPixelConfig config, void* buffer, size_t rowBytes, |
| 261 | bool invertY); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 262 | |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 263 | /** |
| 264 | * Updates the pixels in a rectangle of a texture. |
| bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 265 | * |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 266 | * @param left left edge of the rectangle to write (inclusive) |
| 267 | * @param top top edge of the rectangle to write (inclusive) |
| 268 | * @param width width of rectangle to write in pixels. |
| 269 | * @param height height of rectangle to write in pixels. |
| 270 | * @param config the pixel config of the source buffer |
| 271 | * @param buffer memory to read pixels from |
| 272 | * @param rowBytes number of bytes bewtween consecutive rows. Zero |
| 273 | * means rows are tightly packed. |
| 274 | */ |
| 275 | void writeTexturePixels(GrTexture* texture, |
| 276 | int left, int top, int width, int height, |
| 277 | GrPixelConfig config, const void* buffer, |
| 278 | size_t rowBytes); |
| 279 | |
| bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 280 | const GrGpuStats& getStats() const; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 281 | void resetStats(); |
| 282 | void printStats() const; |
| 283 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 284 | /** |
| 285 | * 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] | 286 | * be abandoned. Overrides must call INHERITED::abandonResources(). |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 287 | */ |
| junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 288 | virtual void abandonResources(); |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 289 | |
| 290 | /** |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 291 | * Called to tell Gpu object to release all GrResources. Overrides must call |
| 292 | * INHERITED::releaseResources(). |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 293 | */ |
| 294 | void releaseResources(); |
| 295 | |
| 296 | /** |
| 297 | * Add resource to list of resources. Should only be called by GrResource. |
| 298 | * @param resource the resource to add. |
| 299 | */ |
| 300 | void insertResource(GrResource* resource); |
| 301 | |
| 302 | /** |
| 303 | * Remove resource from list of resources. Should only be called by |
| 304 | * GrResource. |
| 305 | * @param resource the resource to remove. |
| 306 | */ |
| 307 | void removeResource(GrResource* resource); |
| 308 | |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 309 | // GrDrawTarget overrides |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 310 | virtual void clear(const GrIRect* rect, GrColor color); |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 311 | |
| bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 312 | // After the client interacts directly with the 3D context state the GrGpu |
| 313 | // must resync its internal state and assumptions about 3D context state. |
| 314 | // Each time this occurs the GrGpu bumps a timestamp. |
| 315 | // state of the 3D context |
| 316 | // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about |
| 317 | // a billion years. |
| 318 | typedef uint64_t ResetTimestamp; |
| 319 | |
| 320 | // This timestamp is always older than the current timestamp |
| 321 | static const ResetTimestamp kExpiredTimestamp = 0; |
| 322 | // Returns a timestamp based on the number of times the context was reset. |
| 323 | // This timestamp can be used to lazily detect when cached 3D context state |
| 324 | // is dirty. |
| 325 | ResetTimestamp getResetTimestamp() const { |
| 326 | return fResetTimestamp; |
| 327 | } |
| 328 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 329 | protected: |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame^] | 330 | enum PrivateDrawStateStateBits { |
| 331 | kFirstBit = (GrDrawState::kLastPublicStateBit << 1), |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 332 | |
| 333 | kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify |
| 334 | // stencil bits used for |
| 335 | // clipping. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
| bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 338 | // keep track of whether we are using stencil clipping (as opposed to |
| 339 | // scissor). |
| 340 | bool fClipInStencil; |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 341 | |
| 342 | // prepares clip flushes gpu state before a draw |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 343 | bool setupClipAndFlushState(GrPrimitiveType type); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 344 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 345 | // Functions used to map clip-respecting stencil tests into normal |
| 346 | // stencil funcs supported by GPUs. |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 347 | static GrStencilFunc ConvertStencilFunc(bool stencilInClip, |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 348 | GrStencilFunc func); |
| 349 | static void ConvertStencilFuncAndMask(GrStencilFunc func, |
| 350 | bool clipInStencil, |
| 351 | unsigned int clipBit, |
| 352 | unsigned int userBits, |
| 353 | unsigned int* ref, |
| 354 | unsigned int* mask); |
| 355 | |
| 356 | // stencil settings to clip drawing when stencil clipping is in effect |
| 357 | // and the client isn't using the stencil test. |
| 358 | static const GrStencilSettings gClipStencilSettings; |
| 359 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 360 | |
| bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 361 | GrGpuStats fStats; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 362 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 363 | struct GeometryPoolState { |
| 364 | const GrVertexBuffer* fPoolVertexBuffer; |
| 365 | int fPoolStartVertex; |
| 366 | |
| 367 | const GrIndexBuffer* fPoolIndexBuffer; |
| 368 | int fPoolStartIndex; |
| 369 | }; |
| 370 | const GeometryPoolState& getGeomPoolState() { |
| 371 | return fGeomPoolStateStack.back(); |
| 372 | } |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 373 | |
| 374 | // GrDrawTarget overrides |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 375 | virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 376 | int vertexCount, |
| 377 | void** vertices); |
| 378 | virtual bool onReserveIndexSpace(int indexCount, void** indices); |
| 379 | virtual void releaseReservedVertexSpace(); |
| 380 | virtual void releaseReservedIndexSpace(); |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 381 | virtual void onSetVertexSourceToArray(const void* vertexArray, |
| 382 | int vertexCount); |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 383 | virtual void onSetIndexSourceToArray(const void* indexArray, |
| 384 | int indexCount); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 385 | virtual void releaseVertexArray(); |
| 386 | virtual void releaseIndexArray(); |
| 387 | virtual void geometrySourceWillPush(); |
| 388 | virtual void geometrySourceWillPop(const GeometrySrcState& restoredState); |
| 389 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 390 | // Helpers for setting up geometry state |
| 391 | void finalizeReservedVertices(); |
| 392 | void finalizeReservedIndices(); |
| 393 | |
| bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 394 | // called when the 3D context state is unknown. Subclass should emit any |
| 395 | // assumed 3D context state and dirty any state cache |
| bsalomon@google.com | 1bf1c21 | 2011-11-05 12:18:58 +0000 | [diff] [blame] | 396 | virtual void onResetContext() = 0; |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 397 | |
| bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 398 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 399 | // overridden by API-specific derived class to create objects. |
| bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 400 | virtual GrTexture* onCreateTexture(const GrTextureDesc& desc, |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 401 | const void* srcData, |
| 402 | size_t rowBytes) = 0; |
| bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 403 | virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) = 0; |
| 404 | virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) = 0; |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 405 | virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0; |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 406 | virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size, |
| 407 | bool dynamic) = 0; |
| 408 | virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size, |
| 409 | bool dynamic) = 0; |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 410 | |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 411 | // overridden by API-specific derivated class to perform the clear and |
| 412 | // clearRect. NULL rect means clear whole target. |
| 413 | virtual void onClear(const GrIRect* rect, GrColor color) = 0; |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 414 | |
| 415 | // overridden by API-specific derived class to perform the draw call. |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 416 | virtual void onGpuDrawIndexed(GrPrimitiveType type, |
| 417 | uint32_t startVertex, |
| 418 | uint32_t startIndex, |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 419 | uint32_t vertexCount, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 420 | uint32_t indexCount) = 0; |
| 421 | |
| 422 | virtual void onGpuDrawNonIndexed(GrPrimitiveType type, |
| 423 | uint32_t vertexCount, |
| 424 | uint32_t numVertices) = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 425 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 426 | // overridden by API-specific derived class to perform flush |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 427 | virtual void onForceRenderTargetFlush() = 0; |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 428 | |
| 429 | // overridden by API-specific derived class to perform the read pixels. |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 430 | virtual bool onReadPixels(GrRenderTarget* target, |
| 431 | int left, int top, int width, int height, |
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 432 | GrPixelConfig, |
| 433 | void* buffer, |
| 434 | size_t rowBytes, |
| 435 | bool invertY) = 0; |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 436 | |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 437 | // overridden by API-specific derived class to perform the texture update |
| 438 | virtual void onWriteTexturePixels(GrTexture* texture, |
| 439 | int left, int top, int width, int height, |
| 440 | GrPixelConfig config, const void* buffer, |
| 441 | size_t rowBytes) = 0; |
| 442 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 443 | // 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] | 444 | // indexed geometry. The subclass may adjust the startVertex and/or |
| 445 | // startIndex since it may have already accounted for these in the setup. |
| 446 | virtual void setupGeometry(int* startVertex, |
| 447 | int* startIndex, |
| 448 | int vertexCount, |
| 449 | int indexCount) = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 450 | |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 451 | // width and height may be larger than rt (if underlying API allows it). |
| 452 | // Should attach the SB to the RT. Returns false if compatible sb could |
| 453 | // not be created. |
| 454 | virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt, |
| 455 | int width, |
| 456 | int height) = 0; |
| 457 | |
| 458 | // attaches an existing SB to an existing RT. |
| 459 | virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb, |
| 460 | GrRenderTarget* rt) = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 461 | |
| 462 | // The GrGpu typically records the clients requested state and then flushes |
| 463 | // deltas from previous state at draw time. This function does the |
| 464 | // API-specific flush of the state |
| 465 | // returns false if current state is unsupported. |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 466 | virtual bool flushGraphicsState(GrPrimitiveType type) = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 467 | |
| 468 | // Sets the scissor rect, or disables if rect is NULL. |
| 469 | virtual void flushScissor(const GrIRect* rect) = 0; |
| 470 | |
| bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 471 | // GrGpu subclass sets clip bit in the stencil buffer. The subclass is |
| 472 | // free to clear the remaining bits to zero if masked clears are more |
| 473 | // expensive than clearing all bits. |
| 474 | virtual void clearStencilClip(const GrIRect& rect, bool insideClip) = 0; |
| 475 | |
| bsalomon@google.com | edc177d | 2011-08-05 15:46:40 +0000 | [diff] [blame] | 476 | // clears the entire stencil buffer to 0 |
| 477 | virtual void clearStencil() = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 478 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 479 | private: |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 480 | GrContext* fContext; // not reffed (context refs gpu) |
| bsalomon@google.com | 1bf1c21 | 2011-11-05 12:18:58 +0000 | [diff] [blame] | 481 | |
| bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 482 | ResetTimestamp fResetTimestamp; |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 483 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 484 | GrVertexBufferAllocPool* fVertexPool; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 485 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 486 | GrIndexBufferAllocPool* fIndexPool; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 487 | |
| 488 | // counts number of uses of vertex/index pool in the geometry stack |
| 489 | int fVertexPoolUseCnt; |
| 490 | int fIndexPoolUseCnt; |
| 491 | |
| 492 | enum { |
| 493 | kPreallocGeomPoolStateStackCnt = 4, |
| 494 | }; |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 495 | SkSTArray<kPreallocGeomPoolStateStackCnt, |
| 496 | GeometryPoolState, true> fGeomPoolStateStack; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 497 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 498 | mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be |
| 499 | // created on-demand |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 500 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 501 | mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be |
| 502 | // created on-demand |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 503 | |
| bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 504 | // must be instantiated after GrGpu object has been given its owning |
| 505 | // GrContext ptr. (GrGpu is constructed first then handed off to GrContext). |
| 506 | GrPathRendererChain* fPathRendererChain; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 507 | |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 508 | bool fContextIsDirty; |
| 509 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 510 | GrResource* fResourceHead; |
| 511 | |
| bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 512 | // Given a rt, find or create a stencil buffer and attach it |
| 513 | bool attachStencilBufferToRenderTarget(GrRenderTarget* target); |
| 514 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 515 | // GrDrawTarget overrides |
| 516 | virtual void onDrawIndexed(GrPrimitiveType type, |
| 517 | int startVertex, |
| 518 | int startIndex, |
| 519 | int vertexCount, |
| 520 | int indexCount); |
| 521 | virtual void onDrawNonIndexed(GrPrimitiveType type, |
| 522 | int startVertex, |
| 523 | int vertexCount); |
| 524 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 525 | // readies the pools to provide vertex/index data. |
| 526 | void prepareVertexPool(); |
| 527 | void prepareIndexPool(); |
| 528 | |
| 529 | // determines the path renderer used to draw a clip path element. |
| reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 530 | GrPathRenderer* getClipPathRenderer(const SkPath& path, GrPathFill fill); |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 531 | |
| bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 532 | void resetContext() { |
| 533 | this->onResetContext(); |
| 534 | ++fResetTimestamp; |
| 535 | } |
| 536 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 537 | void handleDirtyContext() { |
| 538 | if (fContextIsDirty) { |
| 539 | this->resetContext(); |
| 540 | fContextIsDirty = false; |
| 541 | } |
| 542 | } |
| 543 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 544 | typedef GrDrawTarget INHERITED; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 545 | }; |
| 546 | |
| 547 | #endif |