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