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