reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrGpu_DEFINED |
| 9 | #define GrGpu_DEFINED |
| 10 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 12 | #include "GrPathRendering.h" |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 13 | #include "GrProgramDesc.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 14 | #include "SkPath.h" |
| 15 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 16 | class GrContext; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 17 | class GrIndexBufferAllocPool; |
bsalomon | 04ddf89 | 2014-11-19 12:36:22 -0800 | [diff] [blame] | 18 | class GrOptDrawState; |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 19 | class GrPath; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 20 | class GrPathRange; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 21 | class GrPathRenderer; |
| 22 | class GrPathRendererChain; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 23 | class GrStencilBuffer; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 24 | class GrVertexBufferAllocPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 26 | class GrGpu : public SkRefCnt { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | public: |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 28 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | /** |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 30 | * Additional blend coefficients for dual source blending, not exposed |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 31 | * through GrPaint/GrContext. |
| 32 | */ |
| 33 | enum ExtendedBlendCoeffs { |
| 34 | // source 2 refers to second output color when |
| 35 | // using dual source blending. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 36 | kS2C_GrBlendCoeff = kPublicGrBlendCoeffCount, |
| 37 | kIS2C_GrBlendCoeff, |
| 38 | kS2A_GrBlendCoeff, |
| 39 | kIS2A_GrBlendCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 40 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 41 | kTotalGrBlendCoeffCount |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /** |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 45 | * Create an instance of GrGpu that matches the specified backend. If the requested backend is |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 46 | * not supported (at compile-time or run-time) this returns NULL. The context will not be |
| 47 | * fully constructed and should not be used by GrGpu until after this function returns. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | */ |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 49 | static GrGpu* Create(GrBackend, GrBackendContext, GrContext* context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | |
| 51 | //////////////////////////////////////////////////////////////////////////// |
| 52 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 53 | GrGpu(GrContext* context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | virtual ~GrGpu(); |
| 55 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 56 | GrContext* getContext() { return fContext; } |
| 57 | const GrContext* getContext() const { return fContext; } |
| 58 | |
| 59 | /** |
| 60 | * Gets the capabilities of the draw target. |
| 61 | */ |
| 62 | const GrDrawTargetCaps* caps() const { return fCaps.get(); } |
| 63 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 64 | GrPathRendering* pathRendering() { |
| 65 | return fPathRendering.get(); |
| 66 | } |
| 67 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 68 | // Called by GrContext when the underlying backend context has been destroyed. |
| 69 | // GrGpu should use this to ensure that no backend API calls will be made from |
| 70 | // here onward, including in its destructor. Subclasses should call |
robertphillips | e337130 | 2014-09-17 06:01:06 -0700 | [diff] [blame] | 71 | // INHERITED::contextAbandoned() if they override this. |
| 72 | virtual void contextAbandoned(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 73 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 74 | /** |
| 75 | * The GrGpu object normally assumes that no outsider is setting state |
| 76 | * 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] | 77 | * the GrGpu that the state was modified and it shouldn't make assumptions |
| 78 | * about the state. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | */ |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 80 | void markContextDirty(uint32_t state = kAll_GrBackendState) { |
| 81 | fResetBits |= state; |
| 82 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | |
| 84 | void unimpl(const char[]); |
| 85 | |
| 86 | /** |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 87 | * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can |
| 88 | * be used as a render target by calling GrTexture::asRenderTarget(). Not all |
| 89 | * pixel configs can be used as render targets. Support for configs as textures |
| 90 | * or render targets can be checked using GrDrawTargetCaps. |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 91 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | * @param desc describes the texture to be created. |
| 93 | * @param srcData texel data to load texture. Begins with full-size |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 94 | * palette data for paletted textures. For compressed |
| 95 | * formats it contains the compressed pixel data. Otherwise, |
| 96 | * it contains width*height texels. If NULL texture data |
| 97 | * is uninitialized. |
| 98 | * @param rowBytes the number of bytes between consecutive rows. Zero |
| 99 | * means rows are tightly packed. This field is ignored |
| 100 | * for compressed formats. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 101 | * |
| 102 | * @return The texture object if successful, otherwise NULL. |
| 103 | */ |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 104 | GrTexture* createTexture(const GrSurfaceDesc& desc, const void* srcData, size_t rowBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | |
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 | * Implements GrContext::wrapBackendTexture |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 108 | */ |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 109 | GrTexture* wrapBackendTexture(const GrBackendTextureDesc&); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 110 | |
| 111 | /** |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 112 | * Implements GrContext::wrapBackendTexture |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 113 | */ |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 114 | GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | * Creates a vertex buffer. |
| 118 | * |
| 119 | * @param size size in bytes of the vertex buffer |
| 120 | * @param dynamic hints whether the data will be frequently changed |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 121 | * by either GrVertexBuffer::map() or |
| 122 | * GrVertexBuffer::updateData(). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 123 | * |
| 124 | * @return The vertex buffer if successful, otherwise NULL. |
| 125 | */ |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 126 | GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Creates an index buffer. |
| 130 | * |
| 131 | * @param size size in bytes of the index buffer |
| 132 | * @param dynamic hints whether the data will be frequently changed |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 133 | * by either GrIndexBuffer::map() or |
| 134 | * GrIndexBuffer::updateData(). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 135 | * |
| 136 | * @return The index buffer if successful, otherwise NULL. |
| 137 | */ |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 138 | GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | |
| 140 | /** |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 141 | * Creates an index buffer for instance drawing with a specific pattern. |
| 142 | * |
| 143 | * @param pattern the pattern to repeat |
| 144 | * @param patternSize size in bytes of the pattern |
| 145 | * @param reps number of times to repeat the pattern |
| 146 | * @param vertCount number of vertices the pattern references |
| 147 | * @param dynamic hints whether the data will be frequently changed |
| 148 | * by either GrIndexBuffer::map() or |
| 149 | * GrIndexBuffer::updateData(). |
| 150 | * |
| 151 | * @return The index buffer if successful, otherwise NULL. |
| 152 | */ |
| 153 | GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern, |
| 154 | int patternSize, |
| 155 | int reps, |
| 156 | int vertCount, |
| 157 | bool isDynamic = false); |
| 158 | |
| 159 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 160 | * Returns an index buffer that can be used to render quads. |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 161 | * Six indices per quad: 0, 1, 2, 0, 2, 3, etc. |
| 162 | * The max number of quads can be queried using GrIndexBuffer::maxQuads(). |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 163 | * Draw with kTriangles_GrPrimitiveType |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 164 | * @ return the quad index buffer |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 165 | */ |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 166 | const GrIndexBuffer* getQuadIndexBuffer() const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | |
| 168 | /** |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 169 | * Resolves MSAA. |
| 170 | */ |
| 171 | void resolveRenderTarget(GrRenderTarget* target); |
| 172 | |
| 173 | /** |
commit-bot@chromium.org | 5d1d79a | 2013-05-24 18:52:52 +0000 | [diff] [blame] | 174 | * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with |
| 175 | * config surfaceConfig. The returned config must have at least as many bits per channel as the |
| 176 | * readConfig or writeConfig param. |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 177 | */ |
commit-bot@chromium.org | 5d1d79a | 2013-05-24 18:52:52 +0000 | [diff] [blame] | 178 | virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig, |
| 179 | GrPixelConfig surfaceConfig) const { |
| 180 | return readConfig; |
| 181 | } |
| 182 | virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig, |
| 183 | GrPixelConfig surfaceConfig) const { |
| 184 | return writeConfig; |
| 185 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 186 | |
| 187 | /** |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 188 | * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't |
| 189 | * match the texture's config. |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 190 | */ |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 191 | virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0; |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 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 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 200 | * 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] | 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 | /** |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 223 | * Reads a rectangle of pixels from a render target. |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 224 | * |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 225 | * @param renderTarget the render target to read from. NULL means the |
| 226 | * current render target. |
| 227 | * @param left left edge of the rectangle to read (inclusive) |
| 228 | * @param top top edge of the rectangle to read (inclusive) |
| 229 | * @param width width of rectangle to read in pixels. |
| 230 | * @param height height of rectangle to read in pixels. |
| 231 | * @param config the pixel config of the destination buffer |
| 232 | * @param buffer memory to read the rectangle into. |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 233 | * @param rowBytes the number of bytes between consecutive rows. Zero |
| 234 | * means rows are tightly packed. |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 235 | * @param invertY buffer should be populated bottom-to-top as opposed |
| 236 | * to top-to-bottom (skia's usual order) |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 237 | * |
| 238 | * @return true if the read succeeded, false if not. The read can fail |
| 239 | * because of a unsupported pixel config or because no render |
| 240 | * target is currently set. |
| 241 | */ |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 242 | bool readPixels(GrRenderTarget* renderTarget, |
| 243 | int left, int top, int width, int height, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 244 | GrPixelConfig config, void* buffer, size_t rowBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 245 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 246 | /** |
| 247 | * Updates the pixels in a rectangle of a texture. |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 248 | * |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 249 | * @param left left edge of the rectangle to write (inclusive) |
| 250 | * @param top top edge of the rectangle to write (inclusive) |
| 251 | * @param width width of rectangle to write in pixels. |
| 252 | * @param height height of rectangle to write in pixels. |
| 253 | * @param config the pixel config of the source buffer |
| 254 | * @param buffer memory to read pixels from |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 255 | * @param rowBytes number of bytes between consecutive rows. Zero |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 256 | * means rows are tightly packed. |
| 257 | */ |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 258 | bool writeTexturePixels(GrTexture* texture, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 259 | int left, int top, int width, int height, |
| 260 | GrPixelConfig config, const void* buffer, |
| 261 | size_t rowBytes); |
| 262 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 263 | /** |
| 264 | * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if |
| 265 | * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target |
| 266 | * can be optionally cleared. |
| 267 | */ |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 268 | void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,GrRenderTarget* renderTarget); |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 269 | |
| 270 | |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 271 | void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* renderTarget); |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 272 | |
| 273 | /** |
| 274 | * Discards the contents render target. NULL indicates that the current render target should |
| 275 | * be discarded. |
| 276 | **/ |
| 277 | virtual void discard(GrRenderTarget* = NULL) = 0; |
| 278 | |
| 279 | /** |
| 280 | * This is can be called before allocating a texture to be a dst for copySurface. It will |
bsalomon | 8ee4e60 | 2014-11-26 10:20:45 -0800 | [diff] [blame^] | 281 | * populate the origin, config, and flags fields of the desc such that copySurface is more |
| 282 | * likely to succeed and be efficient. |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 283 | */ |
bsalomon | 8ee4e60 | 2014-11-26 10:20:45 -0800 | [diff] [blame^] | 284 | virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc); |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 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. |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 299 | ResetTimestamp getResetTimestamp() const { return fResetTimestamp; } |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 300 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 301 | enum DrawType { |
| 302 | kDrawPoints_DrawType, |
| 303 | kDrawLines_DrawType, |
| 304 | kDrawTriangles_DrawType, |
| 305 | kStencilPath_DrawType, |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 306 | kDrawPath_DrawType, |
commit-bot@chromium.org | 9b62aa1 | 2014-03-25 11:59:40 +0000 | [diff] [blame] | 307 | kDrawPaths_DrawType, |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
kkinnunen | ec56e45 | 2014-08-25 22:21:16 -0700 | [diff] [blame] | 310 | static bool IsPathRenderingDrawType(DrawType type) { |
| 311 | return kDrawPath_DrawType == type || kDrawPaths_DrawType == type; |
| 312 | } |
| 313 | |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 314 | GrContext::GPUStats* gpuStats() { return &fGPUStats; } |
| 315 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 316 | virtual void buildProgramDesc(const GrOptDrawState&, |
| 317 | const GrProgramDesc::DescInfo&, |
| 318 | GrGpu::DrawType, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 319 | GrProgramDesc*) = 0; |
| 320 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 321 | /** |
| 322 | * Called at start and end of gpu trace marking |
| 323 | * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start |
| 324 | * and end of a code block respectively |
| 325 | */ |
| 326 | void addGpuTraceMarker(const GrGpuTraceMarker* marker); |
| 327 | void removeGpuTraceMarker(const GrGpuTraceMarker* marker); |
| 328 | |
| 329 | /** |
| 330 | * Takes the current active set of markers and stores them for later use. Any current marker |
| 331 | * in the active set is removed from the active set and the targets remove function is called. |
| 332 | * These functions do not work as a stack so you cannot call save a second time before calling |
| 333 | * restore. Also, it is assumed that when restore is called the current active set of markers |
| 334 | * is empty. When the stored markers are added back into the active set, the targets add marker |
| 335 | * is called. |
| 336 | */ |
| 337 | void saveActiveTraceMarkers(); |
| 338 | void restoreActiveTraceMarkers(); |
| 339 | |
bsalomon | 8ee4e60 | 2014-11-26 10:20:45 -0800 | [diff] [blame^] | 340 | // Called to determine whether an onCopySurface call would succeed or not. This is useful for |
| 341 | // proxy subclasses to test whether the copy would succeed without executing it yet. Derived |
| 342 | // classes must keep this consistent with their implementation of onCopySurface(). The inputs |
| 343 | // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src |
| 344 | // and dst bounds. |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 345 | virtual bool canCopySurface(const GrSurface* dst, |
| 346 | const GrSurface* src, |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 347 | const SkIRect& srcRect, |
| 348 | const SkIPoint& dstPoint) = 0; |
| 349 | |
bsalomon | 8ee4e60 | 2014-11-26 10:20:45 -0800 | [diff] [blame^] | 350 | // This method is called by copySurface The srcRect is guaranteed to be entirely within the |
| 351 | // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls |
| 352 | // entirely within the dst. The default implementation will draw a rect from the src to the |
| 353 | // dst if the src is a texture and the dst is a render target and fail otherwise. |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 354 | virtual bool copySurface(GrSurface* dst, |
| 355 | GrSurface* src, |
| 356 | const SkIRect& srcRect, |
| 357 | const SkIPoint& dstPoint) = 0; |
| 358 | |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 359 | virtual void draw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&); |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 360 | virtual void stencilPath(const GrOptDrawState&, |
| 361 | const GrPath*, |
joshualitt | 4b68ec0 | 2014-11-07 14:11:45 -0800 | [diff] [blame] | 362 | const GrStencilSettings&); |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 363 | virtual void drawPath(const GrOptDrawState&, |
| 364 | const GrPath*, |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 365 | const GrStencilSettings&); |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 366 | virtual void drawPaths(const GrOptDrawState&, |
| 367 | const GrPathRange*, |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 368 | const void* indices, |
| 369 | GrDrawTarget::PathIndexType, |
| 370 | const float transformValues[], |
joshualitt | 4b68ec0 | 2014-11-07 14:11:45 -0800 | [diff] [blame] | 371 | GrDrawTarget::PathTransformType, |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 372 | int count, |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 373 | const GrStencilSettings&); |
joshualitt | 4b68ec0 | 2014-11-07 14:11:45 -0800 | [diff] [blame] | 374 | |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 375 | static DrawType PrimTypeToDrawType(GrPrimitiveType type) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 376 | switch (type) { |
| 377 | case kTriangles_GrPrimitiveType: |
| 378 | case kTriangleStrip_GrPrimitiveType: |
| 379 | case kTriangleFan_GrPrimitiveType: |
| 380 | return kDrawTriangles_DrawType; |
| 381 | case kPoints_GrPrimitiveType: |
| 382 | return kDrawPoints_DrawType; |
| 383 | case kLines_GrPrimitiveType: |
| 384 | case kLineStrip_GrPrimitiveType: |
| 385 | return kDrawLines_DrawType; |
| 386 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 387 | SkFAIL("Unexpected primitive type"); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 388 | return kDrawTriangles_DrawType; |
| 389 | } |
| 390 | } |
| 391 | |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 392 | protected: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 393 | // Functions used to map clip-respecting stencil tests into normal |
| 394 | // stencil funcs supported by GPUs. |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 395 | static GrStencilFunc ConvertStencilFunc(bool stencilInClip, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 396 | GrStencilFunc func); |
| 397 | static void ConvertStencilFuncAndMask(GrStencilFunc func, |
| 398 | bool clipInStencil, |
| 399 | unsigned int clipBit, |
| 400 | unsigned int userBits, |
| 401 | unsigned int* ref, |
| 402 | unsigned int* mask); |
| 403 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 404 | const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; } |
| 405 | |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 406 | GrContext::GPUStats fGPUStats; |
| 407 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 408 | SkAutoTDelete<GrPathRendering> fPathRendering; |
| 409 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 410 | // Subclass must initialize this in its constructor. |
| 411 | SkAutoTUnref<const GrDrawTargetCaps> fCaps; |
| 412 | |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 413 | private: |
bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 414 | // 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] | 415 | // assumed 3D context state and dirty any state cache. |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 416 | virtual void onResetContext(uint32_t resetBits) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 417 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 418 | // overridden by backend-specific derived class to create objects. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 419 | virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc, |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 420 | const void* srcData, |
| 421 | size_t rowBytes) = 0; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 422 | virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 423 | const void* srcData) = 0; |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 424 | virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0; |
| 425 | virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0; |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 426 | virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0; |
| 427 | virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 428 | |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 429 | // overridden by backend-specific derived class to perform the clear. |
joshualitt | 4b68ec0 | 2014-11-07 14:11:45 -0800 | [diff] [blame] | 430 | virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color, |
| 431 | bool canIgnoreRect) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 432 | |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 433 | |
| 434 | // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is |
| 435 | // ONLY used by the the clip target |
| 436 | virtual void onClearStencilClip(GrRenderTarget*, |
| 437 | const SkIRect& rect, |
| 438 | bool insideClip) = 0; |
| 439 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 440 | // overridden by backend-specific derived class to perform the draw call. |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 441 | virtual void onDraw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&) = 0; |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 442 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 443 | // overridden by backend-specific derived class to perform the read pixels. |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 444 | virtual bool onReadPixels(GrRenderTarget* target, |
| 445 | int left, int top, int width, int height, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 446 | GrPixelConfig, |
| 447 | void* buffer, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 448 | size_t rowBytes) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 449 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 450 | // overridden by backend-specific derived class to perform the texture update |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 451 | virtual bool onWriteTexturePixels(GrTexture* texture, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 452 | int left, int top, int width, int height, |
| 453 | GrPixelConfig config, const void* buffer, |
| 454 | size_t rowBytes) = 0; |
| 455 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 456 | // overridden by backend-specific derived class to perform the resolve |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 457 | virtual void onResolveRenderTarget(GrRenderTarget* target) = 0; |
| 458 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 459 | // width and height may be larger than rt (if underlying API allows it). |
| 460 | // Should attach the SB to the RT. Returns false if compatible sb could |
| 461 | // not be created. |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 462 | virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 463 | |
| 464 | // attaches an existing SB to an existing RT. |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 465 | virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 466 | |
| 467 | // The GrGpu typically records the clients requested state and then flushes |
| 468 | // deltas from previous state at draw time. This function does the |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 469 | // backend-specific flush of the state. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 470 | // returns false if current state is unsupported. |
joshualitt | 9176e2c | 2014-11-20 07:28:52 -0800 | [diff] [blame] | 471 | virtual bool flushGraphicsState(const GrOptDrawState&, DrawType) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 472 | |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 473 | // clears target's entire stencil buffer to 0 |
| 474 | virtual void clearStencil(GrRenderTarget* target) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 475 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 476 | // Given a rt, find or create a stencil buffer and attach it |
| 477 | bool attachStencilBufferToRenderTarget(GrRenderTarget* target); |
| 478 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 479 | virtual void didAddGpuTraceMarker() = 0; |
| 480 | virtual void didRemoveGpuTraceMarker() = 0; |
| 481 | |
bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 482 | void resetContext() { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 483 | this->onResetContext(fResetBits); |
| 484 | fResetBits = 0; |
bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 485 | ++fResetTimestamp; |
| 486 | } |
| 487 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 488 | void handleDirtyContext() { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 489 | if (fResetBits) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 490 | this->resetContext(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 494 | ResetTimestamp fResetTimestamp; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 495 | uint32_t fResetBits; |
bsalomon@google.com | 6438695 | 2013-02-08 21:22:44 +0000 | [diff] [blame] | 496 | // these are mutable so they can be created on-demand |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 497 | mutable GrIndexBuffer* fQuadIndexBuffer; |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 498 | // To keep track that we always have at least as many debug marker adds as removes |
| 499 | int fGpuTraceMarkerCount; |
| 500 | GrTraceMarkerSet fActiveTraceMarkers; |
| 501 | GrTraceMarkerSet fStoredTraceMarkers; |
| 502 | // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu. |
| 503 | GrContext* fContext; |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 504 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 505 | typedef SkRefCnt INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | #endif |