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); |
bsalomon | 830d206 | 2014-12-15 13:12:08 -0800 | [diff] [blame] | 54 | ~GrGpu() SK_OVERRIDE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 281 | * populate the origin, config, and flags fields of the desc such that copySurface can |
| 282 | * efficiently succeed. It should only succeed if it can allow copySurface to perform a copy |
| 283 | * that would be more effecient than drawing the src to a dst render target. |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 284 | */ |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 285 | virtual bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) = 0; |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 286 | |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 287 | // After the client interacts directly with the 3D context state the GrGpu |
| 288 | // must resync its internal state and assumptions about 3D context state. |
| 289 | // Each time this occurs the GrGpu bumps a timestamp. |
| 290 | // state of the 3D context |
| 291 | // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about |
| 292 | // a billion years. |
| 293 | typedef uint64_t ResetTimestamp; |
| 294 | |
| 295 | // This timestamp is always older than the current timestamp |
| 296 | static const ResetTimestamp kExpiredTimestamp = 0; |
| 297 | // Returns a timestamp based on the number of times the context was reset. |
| 298 | // This timestamp can be used to lazily detect when cached 3D context state |
| 299 | // is dirty. |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 300 | ResetTimestamp getResetTimestamp() const { return fResetTimestamp; } |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 301 | |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 302 | enum DrawType { |
| 303 | kDrawPoints_DrawType, |
| 304 | kDrawLines_DrawType, |
| 305 | kDrawTriangles_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 | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 340 | // Called to determine whether a copySurface call would succeed or not. Derived |
| 341 | // classes must keep this consistent with their implementation of onCopySurface(). Fallbacks |
| 342 | // to issuing a draw from the src to dst take place at the GrDrawTarget level and this function |
| 343 | // should only return true if a faster copy path exists. The rect and point are pre-clipped. The |
| 344 | // src rect and implied dst rect are guaranteed to be within the src/dst bounds and non-empty. |
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 | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 350 | // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst |
| 351 | // take place at the GrDrawTarget level and this function implement faster copy paths. The rect |
| 352 | // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the |
| 353 | // src/dst bounds and non-empty. |
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 | |
bsalomon | 830d206 | 2014-12-15 13:12:08 -0800 | [diff] [blame] | 359 | void draw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&); |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame^] | 360 | |
| 361 | /** None of these params are optional, pointers used just to avoid making copies. */ |
| 362 | struct StencilPathState { |
| 363 | bool fUseHWAA; |
| 364 | GrRenderTarget* fRenderTarget; |
| 365 | const SkMatrix* fViewMatrix; |
| 366 | const GrStencilSettings* fStencil; |
| 367 | const GrScissorState* fScissor; |
| 368 | }; |
| 369 | |
| 370 | void stencilPath(const GrPath*, const StencilPathState&); |
| 371 | |
bsalomon | 830d206 | 2014-12-15 13:12:08 -0800 | [diff] [blame] | 372 | void drawPath(const GrOptDrawState&, const GrPath*, const GrStencilSettings&); |
| 373 | void drawPaths(const GrOptDrawState&, |
| 374 | const GrPathRange*, |
| 375 | const void* indices, |
| 376 | GrDrawTarget::PathIndexType, |
| 377 | const float transformValues[], |
| 378 | GrDrawTarget::PathTransformType, |
| 379 | int count, |
| 380 | const GrStencilSettings&); |
joshualitt | 4b68ec0 | 2014-11-07 14:11:45 -0800 | [diff] [blame] | 381 | |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 382 | static DrawType PrimTypeToDrawType(GrPrimitiveType type) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 383 | switch (type) { |
| 384 | case kTriangles_GrPrimitiveType: |
| 385 | case kTriangleStrip_GrPrimitiveType: |
| 386 | case kTriangleFan_GrPrimitiveType: |
| 387 | return kDrawTriangles_DrawType; |
| 388 | case kPoints_GrPrimitiveType: |
| 389 | return kDrawPoints_DrawType; |
| 390 | case kLines_GrPrimitiveType: |
| 391 | case kLineStrip_GrPrimitiveType: |
| 392 | return kDrawLines_DrawType; |
| 393 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 394 | SkFAIL("Unexpected primitive type"); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 395 | return kDrawTriangles_DrawType; |
| 396 | } |
| 397 | } |
| 398 | |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 399 | protected: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 400 | // Functions used to map clip-respecting stencil tests into normal |
| 401 | // stencil funcs supported by GPUs. |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 402 | static GrStencilFunc ConvertStencilFunc(bool stencilInClip, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 403 | GrStencilFunc func); |
| 404 | static void ConvertStencilFuncAndMask(GrStencilFunc func, |
| 405 | bool clipInStencil, |
| 406 | unsigned int clipBit, |
| 407 | unsigned int userBits, |
| 408 | unsigned int* ref, |
| 409 | unsigned int* mask); |
| 410 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 411 | const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; } |
| 412 | |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 413 | GrContext::GPUStats fGPUStats; |
| 414 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 415 | SkAutoTDelete<GrPathRendering> fPathRendering; |
| 416 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 417 | // Subclass must initialize this in its constructor. |
| 418 | SkAutoTUnref<const GrDrawTargetCaps> fCaps; |
| 419 | |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 420 | private: |
bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 421 | // 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] | 422 | // assumed 3D context state and dirty any state cache. |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 423 | virtual void onResetContext(uint32_t resetBits) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 424 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 425 | // overridden by backend-specific derived class to create objects. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 426 | virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc, |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 427 | const void* srcData, |
| 428 | size_t rowBytes) = 0; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 429 | virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 430 | const void* srcData) = 0; |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 431 | virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0; |
| 432 | virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0; |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 433 | virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0; |
| 434 | virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 435 | |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 436 | // overridden by backend-specific derived class to perform the clear. |
joshualitt | 4b68ec0 | 2014-11-07 14:11:45 -0800 | [diff] [blame] | 437 | virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color, |
| 438 | bool canIgnoreRect) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 439 | |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 440 | |
| 441 | // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is |
| 442 | // ONLY used by the the clip target |
| 443 | virtual void onClearStencilClip(GrRenderTarget*, |
| 444 | const SkIRect& rect, |
| 445 | bool insideClip) = 0; |
| 446 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 447 | // overridden by backend-specific derived class to perform the draw call. |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 448 | virtual void onDraw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&) = 0; |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame^] | 449 | virtual void onStencilPath(const GrPath*, const StencilPathState&) = 0; |
| 450 | |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 451 | virtual void onDrawPath(const GrOptDrawState&, const GrPath*, const GrStencilSettings&) = 0; |
| 452 | virtual void onDrawPaths(const GrOptDrawState&, |
| 453 | const GrPathRange*, |
| 454 | const void* indices, |
| 455 | GrDrawTarget::PathIndexType, |
| 456 | const float transformValues[], |
| 457 | GrDrawTarget::PathTransformType, |
| 458 | int count, |
| 459 | const GrStencilSettings&) = 0; |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +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 the read pixels. |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 462 | virtual bool onReadPixels(GrRenderTarget* target, |
| 463 | int left, int top, int width, int height, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 464 | GrPixelConfig, |
| 465 | void* buffer, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 466 | size_t rowBytes) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 467 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 468 | // overridden by backend-specific derived class to perform the texture update |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 469 | virtual bool onWriteTexturePixels(GrTexture* texture, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 470 | int left, int top, int width, int height, |
| 471 | GrPixelConfig config, const void* buffer, |
| 472 | size_t rowBytes) = 0; |
| 473 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 474 | // overridden by backend-specific derived class to perform the resolve |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 475 | virtual void onResolveRenderTarget(GrRenderTarget* target) = 0; |
| 476 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 477 | // width and height may be larger than rt (if underlying API allows it). |
| 478 | // Should attach the SB to the RT. Returns false if compatible sb could |
| 479 | // not be created. |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 480 | virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 481 | |
| 482 | // attaches an existing SB to an existing RT. |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 483 | virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 484 | |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 485 | // clears target's entire stencil buffer to 0 |
| 486 | virtual void clearStencil(GrRenderTarget* target) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 487 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 488 | // Given a rt, find or create a stencil buffer and attach it |
| 489 | bool attachStencilBufferToRenderTarget(GrRenderTarget* target); |
| 490 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 491 | virtual void didAddGpuTraceMarker() = 0; |
| 492 | virtual void didRemoveGpuTraceMarker() = 0; |
| 493 | |
bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 494 | void resetContext() { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 495 | this->onResetContext(fResetBits); |
| 496 | fResetBits = 0; |
bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 497 | ++fResetTimestamp; |
| 498 | } |
| 499 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 500 | void handleDirtyContext() { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 501 | if (fResetBits) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 502 | this->resetContext(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 506 | ResetTimestamp fResetTimestamp; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 507 | uint32_t fResetBits; |
bsalomon@google.com | 6438695 | 2013-02-08 21:22:44 +0000 | [diff] [blame] | 508 | // these are mutable so they can be created on-demand |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 509 | mutable GrIndexBuffer* fQuadIndexBuffer; |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 510 | // To keep track that we always have at least as many debug marker adds as removes |
| 511 | int fGpuTraceMarkerCount; |
| 512 | GrTraceMarkerSet fActiveTraceMarkers; |
| 513 | GrTraceMarkerSet fStoredTraceMarkers; |
| 514 | // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu. |
| 515 | GrContext* fContext; |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 516 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 517 | typedef SkRefCnt INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | #endif |