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