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