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@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 18 | class GrPath; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 19 | class GrPathRange; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 20 | class GrPathRenderer; |
| 21 | class GrPathRendererChain; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 22 | class GrPipeline; |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 23 | class GrPrimitiveProcessor; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 24 | class GrStencilBuffer; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 25 | class GrVertexBufferAllocPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 27 | class GrGpu : public SkRefCnt { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | public: |
| 29 | /** |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 30 | * 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] | 31 | * not supported (at compile-time or run-time) this returns NULL. The context will not be |
| 32 | * 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] | 33 | */ |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 34 | static GrGpu* Create(GrBackend, GrBackendContext, GrContext* context); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | |
| 36 | //////////////////////////////////////////////////////////////////////////// |
| 37 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 38 | GrGpu(GrContext* context); |
bsalomon | 830d206 | 2014-12-15 13:12:08 -0800 | [diff] [blame] | 39 | ~GrGpu() SK_OVERRIDE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 41 | GrContext* getContext() { return fContext; } |
| 42 | const GrContext* getContext() const { return fContext; } |
| 43 | |
| 44 | /** |
| 45 | * Gets the capabilities of the draw target. |
| 46 | */ |
| 47 | const GrDrawTargetCaps* caps() const { return fCaps.get(); } |
| 48 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 49 | GrPathRendering* pathRendering() { return fPathRendering.get(); } |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 50 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 51 | // Called by GrContext when the underlying backend context has been destroyed. |
| 52 | // GrGpu should use this to ensure that no backend API calls will be made from |
| 53 | // here onward, including in its destructor. Subclasses should call |
robertphillips | e337130 | 2014-09-17 06:01:06 -0700 | [diff] [blame] | 54 | // INHERITED::contextAbandoned() if they override this. |
| 55 | virtual void contextAbandoned(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 56 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | /** |
| 58 | * The GrGpu object normally assumes that no outsider is setting state |
| 59 | * 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] | 60 | * the GrGpu that the state was modified and it shouldn't make assumptions |
| 61 | * about the state. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | */ |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 63 | void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 66 | * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can |
| 67 | * be used as a render target by calling GrTexture::asRenderTarget(). Not all |
| 68 | * pixel configs can be used as render targets. Support for configs as textures |
| 69 | * or render targets can be checked using GrDrawTargetCaps. |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 70 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 71 | * @param desc describes the texture to be created. |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 72 | * @param budgeted does this texture count against the resource cache budget? |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 73 | * @param srcData texel data to load texture. Begins with full-size |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 74 | * palette data for paletted textures. For compressed |
| 75 | * formats it contains the compressed pixel data. Otherwise, |
| 76 | * it contains width*height texels. If NULL texture data |
| 77 | * is uninitialized. |
| 78 | * @param rowBytes the number of bytes between consecutive rows. Zero |
| 79 | * means rows are tightly packed. This field is ignored |
| 80 | * for compressed formats. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 81 | * |
| 82 | * @return The texture object if successful, otherwise NULL. |
| 83 | */ |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 84 | GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted, |
| 85 | const void* srcData, size_t rowBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 87 | /** |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 88 | * Implements GrContext::wrapBackendTexture |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 89 | */ |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 90 | GrTexture* wrapBackendTexture(const GrBackendTextureDesc&); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 93 | * Implements GrContext::wrapBackendTexture |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 94 | */ |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 95 | GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 96 | |
| 97 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | * Creates a vertex buffer. |
| 99 | * |
| 100 | * @param size size in bytes of the vertex buffer |
| 101 | * @param dynamic hints whether the data will be frequently changed |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 102 | * by either GrVertexBuffer::map() or |
| 103 | * GrVertexBuffer::updateData(). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | * |
| 105 | * @return The vertex buffer if successful, otherwise NULL. |
| 106 | */ |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 107 | GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Creates an index buffer. |
| 111 | * |
| 112 | * @param size size in bytes of the index buffer |
| 113 | * @param dynamic hints whether the data will be frequently changed |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 114 | * by either GrIndexBuffer::map() or |
| 115 | * GrIndexBuffer::updateData(). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | * |
| 117 | * @return The index buffer if successful, otherwise NULL. |
| 118 | */ |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 119 | GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 120 | |
| 121 | /** |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 122 | * Creates an index buffer for instance drawing with a specific pattern. |
| 123 | * |
| 124 | * @param pattern the pattern to repeat |
| 125 | * @param patternSize size in bytes of the pattern |
| 126 | * @param reps number of times to repeat the pattern |
| 127 | * @param vertCount number of vertices the pattern references |
| 128 | * @param dynamic hints whether the data will be frequently changed |
| 129 | * by either GrIndexBuffer::map() or |
| 130 | * GrIndexBuffer::updateData(). |
| 131 | * |
| 132 | * @return The index buffer if successful, otherwise NULL. |
| 133 | */ |
| 134 | GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern, |
| 135 | int patternSize, |
| 136 | int reps, |
| 137 | int vertCount, |
| 138 | bool isDynamic = false); |
| 139 | |
| 140 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 141 | * Returns an index buffer that can be used to render quads. |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 142 | * Six indices per quad: 0, 1, 2, 0, 2, 3, etc. |
| 143 | * The max number of quads can be queried using GrIndexBuffer::maxQuads(). |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 144 | * Draw with kTriangles_GrPrimitiveType |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 145 | * @ return the quad index buffer |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | */ |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 147 | const GrIndexBuffer* getQuadIndexBuffer() const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 148 | |
| 149 | /** |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 150 | * Resolves MSAA. |
| 151 | */ |
| 152 | void resolveRenderTarget(GrRenderTarget* target); |
| 153 | |
| 154 | /** |
commit-bot@chromium.org | 5d1d79a | 2013-05-24 18:52:52 +0000 | [diff] [blame] | 155 | * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with |
| 156 | * config surfaceConfig. The returned config must have at least as many bits per channel as the |
| 157 | * readConfig or writeConfig param. |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 158 | */ |
commit-bot@chromium.org | 5d1d79a | 2013-05-24 18:52:52 +0000 | [diff] [blame] | 159 | virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig, |
| 160 | GrPixelConfig surfaceConfig) const { |
| 161 | return readConfig; |
| 162 | } |
| 163 | virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig, |
| 164 | GrPixelConfig surfaceConfig) const { |
| 165 | return writeConfig; |
| 166 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 167 | |
| 168 | /** |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 169 | * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't |
| 170 | * match the texture's config. |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 171 | */ |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 172 | virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0; |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 173 | |
| 174 | /** |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 175 | * OpenGL's readPixels returns the result bottom-to-top while the skia |
| 176 | * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious |
| 177 | * solution is to have the subclass do the flip using either the CPU or GPU. |
| 178 | * However, the caller (GrContext) may have transformations to apply and can |
| 179 | * simply fold in the y-flip for free. On the other hand, the subclass may |
| 180 | * 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] | 181 | * 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] | 182 | * concurrently. |
| 183 | * |
| 184 | * This function returns true if a y-flip is required to put the pixels in |
| 185 | * top-to-bottom order and the subclass cannot do it for free. |
| 186 | * |
| 187 | * See read pixels for the params |
| 188 | * @return true if calling readPixels with the same set of params will |
| 189 | * produce bottom-to-top data |
| 190 | */ |
| 191 | virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget, |
| 192 | int left, int top, |
| 193 | int width, int height, |
| 194 | GrPixelConfig config, |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 195 | size_t rowBytes) const = 0; |
| 196 | /** |
| 197 | * This should return true if reading a NxM rectangle of pixels from a |
| 198 | * render target is faster if the target has dimensons N and M and the read |
| 199 | * rectangle has its top-left at 0,0. |
| 200 | */ |
| 201 | virtual bool fullReadPixelsIsFasterThanPartial() const { return false; }; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 202 | |
| 203 | /** |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 204 | * Reads a rectangle of pixels from a render target. |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 205 | * |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 206 | * @param renderTarget the render target to read from. NULL means the |
| 207 | * current render target. |
| 208 | * @param left left edge of the rectangle to read (inclusive) |
| 209 | * @param top top edge of the rectangle to read (inclusive) |
| 210 | * @param width width of rectangle to read in pixels. |
| 211 | * @param height height of rectangle to read in pixels. |
| 212 | * @param config the pixel config of the destination buffer |
| 213 | * @param buffer memory to read the rectangle into. |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 214 | * @param rowBytes the number of bytes between consecutive rows. Zero |
| 215 | * means rows are tightly packed. |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 216 | * @param invertY buffer should be populated bottom-to-top as opposed |
| 217 | * to top-to-bottom (skia's usual order) |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 218 | * |
| 219 | * @return true if the read succeeded, false if not. The read can fail |
| 220 | * because of a unsupported pixel config or because no render |
| 221 | * target is currently set. |
| 222 | */ |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 223 | bool readPixels(GrRenderTarget* renderTarget, |
| 224 | int left, int top, int width, int height, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 225 | GrPixelConfig config, void* buffer, size_t rowBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 227 | /** |
| 228 | * Updates the pixels in a rectangle of a texture. |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 229 | * |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 230 | * @param left left edge of the rectangle to write (inclusive) |
| 231 | * @param top top edge of the rectangle to write (inclusive) |
| 232 | * @param width width of rectangle to write in pixels. |
| 233 | * @param height height of rectangle to write in pixels. |
| 234 | * @param config the pixel config of the source buffer |
| 235 | * @param buffer memory to read pixels from |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 236 | * @param rowBytes number of bytes between consecutive rows. Zero |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 237 | * means rows are tightly packed. |
| 238 | */ |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 239 | bool writeTexturePixels(GrTexture* texture, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 240 | int left, int top, int width, int height, |
| 241 | GrPixelConfig config, const void* buffer, |
| 242 | size_t rowBytes); |
| 243 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 244 | /** |
| 245 | * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if |
| 246 | * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target |
| 247 | * can be optionally cleared. |
| 248 | */ |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 249 | void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,GrRenderTarget* renderTarget); |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 250 | |
| 251 | |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 252 | void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* renderTarget); |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * Discards the contents render target. NULL indicates that the current render target should |
| 256 | * be discarded. |
| 257 | **/ |
| 258 | virtual void discard(GrRenderTarget* = NULL) = 0; |
| 259 | |
| 260 | /** |
| 261 | * 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] | 262 | * populate the origin, config, and flags fields of the desc such that copySurface can |
| 263 | * efficiently succeed. It should only succeed if it can allow copySurface to perform a copy |
| 264 | * 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] | 265 | */ |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 266 | virtual bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) = 0; |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 267 | |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 268 | // After the client interacts directly with the 3D context state the GrGpu |
| 269 | // must resync its internal state and assumptions about 3D context state. |
| 270 | // Each time this occurs the GrGpu bumps a timestamp. |
| 271 | // state of the 3D context |
| 272 | // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about |
| 273 | // a billion years. |
| 274 | typedef uint64_t ResetTimestamp; |
| 275 | |
| 276 | // This timestamp is always older than the current timestamp |
| 277 | static const ResetTimestamp kExpiredTimestamp = 0; |
| 278 | // Returns a timestamp based on the number of times the context was reset. |
| 279 | // This timestamp can be used to lazily detect when cached 3D context state |
| 280 | // is dirty. |
bsalomon | 6d467ec | 2014-11-18 07:36:19 -0800 | [diff] [blame] | 281 | ResetTimestamp getResetTimestamp() const { return fResetTimestamp; } |
bsalomon@google.com | 80d09b9 | 2011-11-05 21:21:13 +0000 | [diff] [blame] | 282 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 283 | virtual void buildProgramDesc(GrProgramDesc*, |
| 284 | const GrPrimitiveProcessor&, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 285 | const GrPipeline&, |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 286 | const GrBatchTracker&) const = 0; |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 287 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 288 | // Called to determine whether a copySurface call would succeed or not. Derived |
| 289 | // classes must keep this consistent with their implementation of onCopySurface(). Fallbacks |
| 290 | // to issuing a draw from the src to dst take place at the GrDrawTarget level and this function |
| 291 | // should only return true if a faster copy path exists. The rect and point are pre-clipped. The |
| 292 | // 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] | 293 | virtual bool canCopySurface(const GrSurface* dst, |
| 294 | const GrSurface* src, |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 295 | const SkIRect& srcRect, |
| 296 | const SkIPoint& dstPoint) = 0; |
| 297 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 298 | // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst |
| 299 | // take place at the GrDrawTarget level and this function implement faster copy paths. The rect |
| 300 | // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the |
| 301 | // src/dst bounds and non-empty. |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 302 | virtual bool copySurface(GrSurface* dst, |
| 303 | GrSurface* src, |
| 304 | const SkIRect& srcRect, |
| 305 | const SkIPoint& dstPoint) = 0; |
| 306 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 307 | struct DrawArgs { |
| 308 | typedef GrDrawTarget::DrawInfo DrawInfo; |
| 309 | DrawArgs(const GrPrimitiveProcessor* primProc, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 310 | const GrPipeline* pipeline, |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 311 | const GrProgramDesc* desc, |
joshualitt | 17e7314 | 2015-01-21 11:52:36 -0800 | [diff] [blame] | 312 | const GrBatchTracker* batchTracker) |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 313 | : fPrimitiveProcessor(primProc) |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 314 | , fPipeline(pipeline) |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 315 | , fDesc(desc) |
joshualitt | 17e7314 | 2015-01-21 11:52:36 -0800 | [diff] [blame] | 316 | , fBatchTracker(batchTracker) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 317 | SkASSERT(primProc && pipeline && desc && batchTracker); |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 318 | } |
| 319 | const GrPrimitiveProcessor* fPrimitiveProcessor; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 320 | const GrPipeline* fPipeline; |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 321 | const GrProgramDesc* fDesc; |
| 322 | const GrBatchTracker* fBatchTracker; |
| 323 | }; |
| 324 | |
| 325 | void draw(const DrawArgs&, const GrDrawTarget::DrawInfo&); |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 326 | |
| 327 | /** None of these params are optional, pointers used just to avoid making copies. */ |
| 328 | struct StencilPathState { |
| 329 | bool fUseHWAA; |
| 330 | GrRenderTarget* fRenderTarget; |
| 331 | const SkMatrix* fViewMatrix; |
| 332 | const GrStencilSettings* fStencil; |
| 333 | const GrScissorState* fScissor; |
| 334 | }; |
| 335 | |
| 336 | void stencilPath(const GrPath*, const StencilPathState&); |
| 337 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 338 | void drawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&); |
| 339 | void drawPaths(const DrawArgs&, |
bsalomon | 830d206 | 2014-12-15 13:12:08 -0800 | [diff] [blame] | 340 | const GrPathRange*, |
| 341 | const void* indices, |
| 342 | GrDrawTarget::PathIndexType, |
| 343 | const float transformValues[], |
| 344 | GrDrawTarget::PathTransformType, |
| 345 | int count, |
| 346 | const GrStencilSettings&); |
joshualitt | 4b68ec0 | 2014-11-07 14:11:45 -0800 | [diff] [blame] | 347 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 348 | /////////////////////////////////////////////////////////////////////////// |
| 349 | // Debugging and Stats |
| 350 | |
| 351 | class Stats { |
| 352 | public: |
| 353 | #if GR_GPU_STATS |
| 354 | Stats() { this->reset(); } |
| 355 | |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 356 | void reset() { |
| 357 | fRenderTargetBinds = 0; |
| 358 | fShaderCompilations = 0; |
| 359 | fTextureCreates = 0; |
| 360 | fTextureUploads = 0; |
bsalomon | 9e5fc72 | 2015-02-23 10:01:36 -0800 | [diff] [blame] | 361 | fStencilBufferCreates = 0; |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 362 | } |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 363 | |
| 364 | int renderTargetBinds() const { return fRenderTargetBinds; } |
| 365 | void incRenderTargetBinds() { fRenderTargetBinds++; } |
| 366 | int shaderCompilations() const { return fShaderCompilations; } |
| 367 | void incShaderCompilations() { fShaderCompilations++; } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 368 | int textureCreates() const { return fTextureCreates; } |
| 369 | void incTextureCreates() { fTextureCreates++; } |
| 370 | int textureUploads() const { return fTextureUploads; } |
| 371 | void incTextureUploads() { fTextureUploads++; } |
bsalomon | 9e5fc72 | 2015-02-23 10:01:36 -0800 | [diff] [blame] | 372 | void incStencilBufferCreates() { fStencilBufferCreates++; } |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 373 | void dump(SkString*); |
| 374 | |
| 375 | private: |
| 376 | int fRenderTargetBinds; |
| 377 | int fShaderCompilations; |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 378 | int fTextureCreates; |
| 379 | int fTextureUploads; |
bsalomon | 9e5fc72 | 2015-02-23 10:01:36 -0800 | [diff] [blame] | 380 | int fStencilBufferCreates; |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 381 | #else |
| 382 | void dump(SkString*) {}; |
| 383 | void incRenderTargetBinds() {} |
| 384 | void incShaderCompilations() {} |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 385 | void incTextureCreates() {} |
| 386 | void incTextureUploads() {} |
bsalomon | 9e5fc72 | 2015-02-23 10:01:36 -0800 | [diff] [blame] | 387 | void incStencilBufferCreates() {} |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 388 | #endif |
| 389 | }; |
| 390 | |
| 391 | Stats* stats() { return &fStats; } |
| 392 | |
| 393 | /** |
| 394 | * Called at start and end of gpu trace marking |
| 395 | * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start |
| 396 | * and end of a code block respectively |
| 397 | */ |
| 398 | void addGpuTraceMarker(const GrGpuTraceMarker* marker); |
| 399 | void removeGpuTraceMarker(const GrGpuTraceMarker* marker); |
| 400 | |
| 401 | /** |
| 402 | * Takes the current active set of markers and stores them for later use. Any current marker |
| 403 | * in the active set is removed from the active set and the targets remove function is called. |
| 404 | * These functions do not work as a stack so you cannot call save a second time before calling |
| 405 | * restore. Also, it is assumed that when restore is called the current active set of markers |
| 406 | * is empty. When the stored markers are added back into the active set, the targets add marker |
| 407 | * is called. |
| 408 | */ |
| 409 | void saveActiveTraceMarkers(); |
| 410 | void restoreActiveTraceMarkers(); |
| 411 | |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 412 | // Given a rt, find or create a stencil buffer and attach it |
| 413 | bool attachStencilBufferToRenderTarget(GrRenderTarget* target); |
| 414 | |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 415 | protected: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 416 | // Functions used to map clip-respecting stencil tests into normal |
| 417 | // stencil funcs supported by GPUs. |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 418 | static GrStencilFunc ConvertStencilFunc(bool stencilInClip, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 419 | GrStencilFunc func); |
| 420 | static void ConvertStencilFuncAndMask(GrStencilFunc func, |
| 421 | bool clipInStencil, |
| 422 | unsigned int clipBit, |
| 423 | unsigned int userBits, |
| 424 | unsigned int* ref, |
| 425 | unsigned int* mask); |
| 426 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 427 | const GrTraceMarkerSet& getActiveTraceMarkers() const { return fActiveTraceMarkers; } |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 428 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 429 | Stats fStats; |
| 430 | SkAutoTDelete<GrPathRendering> fPathRendering; |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 431 | // Subclass must initialize this in its constructor. |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 432 | SkAutoTUnref<const GrDrawTargetCaps> fCaps; |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 433 | |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 434 | private: |
bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 435 | // 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] | 436 | // assumed 3D context state and dirty any state cache. |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 437 | virtual void onResetContext(uint32_t resetBits) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 438 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 439 | // overridden by backend-specific derived class to create objects. |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 440 | virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, |
| 441 | const void* srcData, size_t rowBytes) = 0; |
| 442 | virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted, |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 443 | const void* srcData) = 0; |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 444 | virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0; |
| 445 | virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0; |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 446 | virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0; |
| 447 | virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 448 | |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 449 | // overridden by backend-specific derived class to perform the clear. |
joshualitt | 4b68ec0 | 2014-11-07 14:11:45 -0800 | [diff] [blame] | 450 | virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color, |
| 451 | bool canIgnoreRect) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 452 | |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 453 | |
| 454 | // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is |
| 455 | // ONLY used by the the clip target |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 456 | virtual void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) = 0; |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame] | 457 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 458 | // overridden by backend-specific derived class to perform the draw call. |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 459 | virtual void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) = 0; |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 460 | virtual void onStencilPath(const GrPath*, const StencilPathState&) = 0; |
| 461 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 462 | virtual void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) = 0; |
| 463 | virtual void onDrawPaths(const DrawArgs&, |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 464 | const GrPathRange*, |
| 465 | const void* indices, |
| 466 | GrDrawTarget::PathIndexType, |
| 467 | const float transformValues[], |
| 468 | GrDrawTarget::PathTransformType, |
| 469 | int count, |
| 470 | const GrStencilSettings&) = 0; |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 471 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 472 | // overridden by backend-specific derived class to perform the read pixels. |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 473 | virtual bool onReadPixels(GrRenderTarget* target, |
| 474 | int left, int top, int width, int height, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 475 | GrPixelConfig, |
| 476 | void* buffer, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 477 | size_t rowBytes) = 0; |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 478 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 479 | // overridden by backend-specific derived class to perform the texture update |
bsalomon@google.com | 9c68058 | 2013-02-06 18:17:50 +0000 | [diff] [blame] | 480 | virtual bool onWriteTexturePixels(GrTexture* texture, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 481 | int left, int top, int width, int height, |
| 482 | GrPixelConfig config, const void* buffer, |
| 483 | size_t rowBytes) = 0; |
| 484 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 485 | // overridden by backend-specific derived class to perform the resolve |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 486 | virtual void onResolveRenderTarget(GrRenderTarget* target) = 0; |
| 487 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 488 | // width and height may be larger than rt (if underlying API allows it). |
| 489 | // Should attach the SB to the RT. Returns false if compatible sb could |
| 490 | // not be created. |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 491 | virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 492 | |
| 493 | // attaches an existing SB to an existing RT. |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 494 | virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 495 | |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 496 | // clears target's entire stencil buffer to 0 |
| 497 | virtual void clearStencil(GrRenderTarget* target) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 498 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 499 | virtual void didAddGpuTraceMarker() = 0; |
| 500 | virtual void didRemoveGpuTraceMarker() = 0; |
| 501 | |
bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 502 | void resetContext() { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 503 | this->onResetContext(fResetBits); |
| 504 | fResetBits = 0; |
bsalomon@google.com | b635d39 | 2011-11-05 12:47:43 +0000 | [diff] [blame] | 505 | ++fResetTimestamp; |
| 506 | } |
| 507 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 508 | void handleDirtyContext() { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 509 | if (fResetBits) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 510 | this->resetContext(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 514 | ResetTimestamp fResetTimestamp; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 515 | uint32_t fResetBits; |
bsalomon@google.com | 6438695 | 2013-02-08 21:22:44 +0000 | [diff] [blame] | 516 | // these are mutable so they can be created on-demand |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 517 | mutable GrIndexBuffer* fQuadIndexBuffer; |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 518 | // To keep track that we always have at least as many debug marker adds as removes |
| 519 | int fGpuTraceMarkerCount; |
| 520 | GrTraceMarkerSet fActiveTraceMarkers; |
| 521 | GrTraceMarkerSet fStoredTraceMarkers; |
| 522 | // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu. |
| 523 | GrContext* fContext; |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 524 | |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 525 | typedef SkRefCnt INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | #endif |