blob: 5b4c05a0fd6b86d46d534d4743c5ef9fe1d15baa [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrGpu_DEFINED
9#define GrGpu_DEFINED
10
kkinnunencabe20c2015-06-01 01:37:26 -070011#include "GrPipelineBuilder.h"
joshualitt79f8fae2014-10-28 17:59:26 -070012#include "GrProgramDesc.h"
kkinnunencabe20c2015-06-01 01:37:26 -070013#include "GrStencil.h"
bsalomon6c9cd552016-01-22 07:17:34 -080014#include "GrSwizzle.h"
bsalomon045802d2015-10-20 07:58:01 -070015#include "GrTextureParamsAdjuster.h"
cblume55f2d2d2016-02-26 13:20:48 -080016#include "GrTypes.h"
kkinnunencabe20c2015-06-01 01:37:26 -070017#include "GrXferProcessor.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000018#include "SkPath.h"
cblume55f2d2d2016-02-26 13:20:48 -080019#include "SkTArray.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000020
kkinnunencabe20c2015-06-01 01:37:26 -070021class GrBatchTracker;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000022class GrContext;
reedf9ad5582015-06-25 21:29:25 -070023class GrGLContext;
kkinnunencabe20c2015-06-01 01:37:26 -070024class GrIndexBuffer;
bsalomone64eb572015-05-07 11:35:55 -070025class GrNonInstancedVertices;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000026class GrPath;
cdaltonb85a0aa2014-07-21 15:32:44 -070027class GrPathRange;
bsalomon@google.com30085192011-08-19 15:42:31 +000028class GrPathRenderer;
29class GrPathRendererChain;
kkinnunencabe20c2015-06-01 01:37:26 -070030class GrPathRendering;
egdaniel8dd688b2015-01-22 10:16:09 -080031class GrPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -080032class GrPrimitiveProcessor;
kkinnunencabe20c2015-06-01 01:37:26 -070033class GrRenderTarget;
egdaniel8dc7c3a2015-04-16 11:22:42 -070034class GrStencilAttachment;
kkinnunencabe20c2015-06-01 01:37:26 -070035class GrSurface;
36class GrTexture;
jvanverth73063dc2015-12-03 09:15:47 -080037class GrTransferBuffer;
kkinnunencabe20c2015-06-01 01:37:26 -070038class GrVertexBuffer;
bsalomoncb8979d2015-05-05 09:51:38 -070039class GrVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +000040
joshualitt3322fa42014-11-07 08:48:51 -080041class GrGpu : public SkRefCnt {
reed@google.comac10a2d2010-12-22 21:39:39 +000042public:
43 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000044 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
halcanary96fcdcc2015-08-27 07:41:13 -070045 * not supported (at compile-time or run-time) this returns nullptr. The context will not be
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000046 * fully constructed and should not be used by GrGpu until after this function returns.
reed@google.comac10a2d2010-12-22 21:39:39 +000047 */
bsalomon682c2692015-05-22 14:01:46 -070048 static GrGpu* Create(GrBackend, GrBackendContext, const GrContextOptions&, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000049
50 ////////////////////////////////////////////////////////////////////////////
51
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000052 GrGpu(GrContext* context);
mtklein36352bf2015-03-25 18:17:31 -070053 ~GrGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000054
joshualitt3322fa42014-11-07 08:48:51 -080055 GrContext* getContext() { return fContext; }
56 const GrContext* getContext() const { return fContext; }
57
58 /**
59 * Gets the capabilities of the draw target.
60 */
bsalomon4b91f762015-05-19 09:29:46 -070061 const GrCaps* caps() const { return fCaps.get(); }
joshualitt3322fa42014-11-07 08:48:51 -080062
kkinnunencabe20c2015-06-01 01:37:26 -070063 GrPathRendering* pathRendering() { return fPathRendering.get(); }
kkinnunenccdaa042014-08-20 01:36:23 -070064
bsalomonc8dc1f72014-08-21 13:02:13 -070065 // Called by GrContext when the underlying backend context has been destroyed.
66 // GrGpu should use this to ensure that no backend API calls will be made from
67 // here onward, including in its destructor. Subclasses should call
robertphillipse3371302014-09-17 06:01:06 -070068 // INHERITED::contextAbandoned() if they override this.
69 virtual void contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -070070
reed@google.comac10a2d2010-12-22 21:39:39 +000071 /**
72 * The GrGpu object normally assumes that no outsider is setting state
73 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000074 * the GrGpu that the state was modified and it shouldn't make assumptions
75 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000076 */
mtkleinb9eb4ac2015-02-02 18:26:03 -080077 void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
reed@google.comac10a2d2010-12-22 21:39:39 +000078
79 /**
bsalomon6d467ec2014-11-18 07:36:19 -080080 * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can
81 * be used as a render target by calling GrTexture::asRenderTarget(). Not all
82 * pixel configs can be used as render targets. Support for configs as textures
bsalomon4b91f762015-05-19 09:29:46 -070083 * or render targets can be checked using GrCaps.
bsalomon@google.com1da07462011-03-10 14:51:57 +000084 *
reed@google.comac10a2d2010-12-22 21:39:39 +000085 * @param desc describes the texture to be created.
bsalomon5236cf42015-01-14 10:42:08 -080086 * @param budgeted does this texture count against the resource cache budget?
cblume55f2d2d2016-02-26 13:20:48 -080087 * @param texels array of mipmap levels containing texel data to load.
88 * Each level begins with full-size palette data for paletted textures.
89 * For compressed formats the level contains the compressed pixel data.
90 * Otherwise, it contains width*height texels. If there is only one
91 * element and it contains nullptr fPixels, texture data is
92 * uninitialized.
halcanary96fcdcc2015-08-27 07:41:13 -070093 * @return The texture object if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +000094 */
bsalomon5ec26ae2016-02-25 08:33:02 -080095 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -080096 const SkTArray<GrMipLevel>& texels);
97
98 /**
bsalomon8ee78f32016-03-04 06:23:36 -080099 * Simplified createTexture() interface when there is no initial texel data
100 * to upload.
cblume55f2d2d2016-02-26 13:20:48 -0800101 */
bsalomon8ee78f32016-03-04 06:23:36 -0800102 GrTexture* createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
103 return this->createTexture(desc, budgeted, SkTArray<GrMipLevel>());
104 }
105
bsalomon@google.come269f212011-11-07 13:29:52 +0000106 /**
ericrkf7b8b8a2016-02-24 14:49:51 -0800107 * Implements GrTextureProvider::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000108 */
bsalomon6dc6f5f2015-06-18 09:12:16 -0700109 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000110
111 /**
brianosman436d9852016-02-25 13:40:42 -0800112 * Implements GrTextureProvider::wrapBackendRenderTarget
bsalomon@google.come269f212011-11-07 13:29:52 +0000113 */
bsalomon6dc6f5f2015-06-18 09:12:16 -0700114 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&, GrWrapOwnership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000115
116 /**
ericrkf7b8b8a2016-02-24 14:49:51 -0800117 * Implements GrTextureProvider::wrapBackendTextureAsRenderTarget
118 */
119 GrRenderTarget* wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&, GrWrapOwnership);
120
121 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 * Creates a vertex buffer.
123 *
124 * @param size size in bytes of the vertex buffer
125 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000126 * by either GrVertexBuffer::map() or
127 * GrVertexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 *
halcanary96fcdcc2015-08-27 07:41:13 -0700129 * @return The vertex buffer if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000131 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
133 /**
134 * Creates an index buffer.
135 *
136 * @param size size in bytes of the index buffer
137 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000138 * by either GrIndexBuffer::map() or
139 * GrIndexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 *
halcanary96fcdcc2015-08-27 07:41:13 -0700141 * @return The index buffer if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000143 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
jvanverth73063dc2015-12-03 09:15:47 -0800145 /**
146 * Creates a transfer buffer.
147 *
148 * @param size size in bytes of the index buffer
149 * @param toGpu true if used to transfer from the cpu to the gpu
150 * otherwise to be used to transfer from the gpu to the cpu
151 *
152 * @return The transfer buffer if successful, otherwise nullptr.
153 */
154 GrTransferBuffer* createTransferBuffer(size_t size, TransferType type);
155
reed@google.comac10a2d2010-12-22 21:39:39 +0000156 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000157 * Resolves MSAA.
158 */
159 void resolveRenderTarget(GrRenderTarget* target);
160
bsalomonf0674512015-07-28 13:26:15 -0700161 /** Info struct returned by getReadPixelsInfo about performing intermediate draws before
bsalomon39826022015-07-23 08:07:21 -0700162 reading pixels for performance or correctness. */
163 struct ReadPixelTempDrawInfo {
164 /** If the GrGpu is requesting that the caller do a draw to an intermediate surface then
165 this is descriptor for the temp surface. The draw should always be a rect with
166 dst 0,0,w,h. */
167 GrSurfaceDesc fTempSurfaceDesc;
168 /** Indicates whether there is a performance advantage to using an exact match texture
169 (in terms of width and height) for the intermediate texture instead of approximate. */
170 bool fUseExactScratch;
bsalomon6c9cd552016-01-22 07:17:34 -0800171 /** Swizzle to apply during the draw. This is used to compensate for either feature or
172 performance limitations in the underlying 3D API. */
173 GrSwizzle fSwizzle;
174 /** The config that should be used to read from the temp surface after the draw. This may be
175 different than the original read config in order to compensate for swizzling. The
176 read data will effectively be in the original read config. */
177 GrPixelConfig fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700178 };
bsalomon6c9cd552016-01-22 07:17:34 -0800179
bsalomon39826022015-07-23 08:07:21 -0700180 /** Describes why an intermediate draw must/should be performed before readPixels. */
181 enum DrawPreference {
182 /** On input means that the caller would proceed without draw if the GrGpu doesn't request
183 one.
184 On output means that the GrGpu is not requesting a draw. */
185 kNoDraw_DrawPreference,
186 /** Means that the client would prefer a draw for performance of the readback but
187 can satisfy a straight readPixels call on the inputs without an intermediate draw.
188 getReadPixelsInfo will never set the draw preference to this value but may leave
189 it set. */
190 kCallerPrefersDraw_DrawPreference,
191 /** On output means that GrGpu would prefer a draw for performance of the readback but
192 can satisfy a straight readPixels call on the inputs without an intermediate draw. The
193 caller of getReadPixelsInfo should never specify this on intput. */
194 kGpuPrefersDraw_DrawPreference,
195 /** On input means that the caller requires a draw to do a transformation and there is no
196 CPU fallback.
197 On output means that GrGpu can only satisfy the readPixels request if the intermediate
198 draw is performed.
199 */
200 kRequireDraw_DrawPreference
201 };
202
bsalomonf0674512015-07-28 13:26:15 -0700203 /**
204 * Used to negotiate whether and how an intermediate draw should or must be performed before
205 * a readPixels call. If this returns false then GrGpu could not deduce an intermediate draw
206 * that would allow a successful readPixels call. The passed width, height, and rowBytes,
207 * must be non-zero and already reflect clipping to the src bounds.
208 */
209 bool getReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
210 GrPixelConfig readConfig, DrawPreference*, ReadPixelTempDrawInfo*);
211
cblume61214052016-01-26 09:10:48 -0800212 /** Info struct returned by getWritePixelsInfo about performing an intermediate draw in order
bsalomonf0674512015-07-28 13:26:15 -0700213 to write pixels to a GrSurface for either performance or correctness reasons. */
214 struct WritePixelTempDrawInfo {
215 /** If the GrGpu is requesting that the caller upload to an intermediate surface and draw
216 that to the dst then this is the descriptor for the intermediate surface. The caller
217 should upload the pixels such that the upper left pixel of the upload rect is at 0,0 in
218 the intermediate surface.*/
219 GrSurfaceDesc fTempSurfaceDesc;
bsalomon6c9cd552016-01-22 07:17:34 -0800220 /** Swizzle to apply during the draw. This is used to compensate for either feature or
221 performance limitations in the underlying 3D API. */
222 GrSwizzle fSwizzle;
223 /** The config that should be specified when uploading the *original* data to the temp
224 surface before the draw. This may be different than the original src data config in
225 order to compensate for swizzling that will occur when drawing. */
226 GrPixelConfig fWriteConfig;
bsalomonf0674512015-07-28 13:26:15 -0700227 };
bsalomon39826022015-07-23 08:07:21 -0700228
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000229 /**
bsalomonf0674512015-07-28 13:26:15 -0700230 * Used to negotiate whether and how an intermediate surface should be used to write pixels to
231 * a GrSurface. If this returns false then GrGpu could not deduce an intermediate draw
232 * that would allow a successful transfer of the src pixels to the dst. The passed width,
233 * height, and rowBytes, must be non-zero and already reflect clipping to the dst bounds.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000234 */
cblumeed828002016-02-16 13:00:01 -0800235 bool getWritePixelsInfo(GrSurface* dstSurface, int width, int height,
bsalomonf0674512015-07-28 13:26:15 -0700236 GrPixelConfig srcConfig, DrawPreference*, WritePixelTempDrawInfo*);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000237
238 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000239 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000240 *
bsalomon6cb3cbe2015-07-30 07:34:27 -0700241 * @param surface The surface to read from
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000242 * @param left left edge of the rectangle to read (inclusive)
243 * @param top top edge of the rectangle to read (inclusive)
244 * @param width width of rectangle to read in pixels.
245 * @param height height of rectangle to read in pixels.
246 * @param config the pixel config of the destination buffer
247 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000248 * @param rowBytes the number of bytes between consecutive rows. Zero
249 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000250 * @param invertY buffer should be populated bottom-to-top as opposed
251 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000252 *
253 * @return true if the read succeeded, false if not. The read can fail
254 * because of a unsupported pixel config or because no render
255 * target is currently set.
256 */
bsalomon6cb3cbe2015-07-30 07:34:27 -0700257 bool readPixels(GrSurface* surface,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000258 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000259 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000260
bsalomon@google.com6f379512011-11-16 20:36:03 +0000261 /**
bsalomon6cb3cbe2015-07-30 07:34:27 -0700262 * Updates the pixels in a rectangle of a surface.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000263 *
bsalomon6cb3cbe2015-07-30 07:34:27 -0700264 * @param surface The surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000265 * @param left left edge of the rectangle to write (inclusive)
266 * @param top top edge of the rectangle to write (inclusive)
267 * @param width width of rectangle to write in pixels.
268 * @param height height of rectangle to write in pixels.
269 * @param config the pixel config of the source buffer
cblume55f2d2d2016-02-26 13:20:48 -0800270 * @param texels array of mipmap levels containing texture data
271 */
272 bool writePixels(GrSurface* surface,
273 int left, int top, int width, int height,
274 GrPixelConfig config,
275 const SkTArray<GrMipLevel>& texels);
276
277 /**
278 * This function is a shim which creates a SkTArray<GrMipLevel> of size 1.
279 * It then calls writePixels with that SkTArray.
280 *
281 * @param buffer memory to read pixels from.
282 * @param rowBytes number of bytes between consecutive rows. Zero
283 * means rows are tightly packed.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000284 */
bsalomon6cb3cbe2015-07-30 07:34:27 -0700285 bool writePixels(GrSurface* surface,
286 int left, int top, int width, int height,
287 GrPixelConfig config, const void* buffer,
288 size_t rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000289
joshualitt3322fa42014-11-07 08:48:51 -0800290 /**
jvanverth17aa0472016-01-05 10:41:27 -0800291 * Updates the pixels in a rectangle of a surface using a GrTransferBuffer
292 *
293 * @param surface The surface to write to.
294 * @param left left edge of the rectangle to write (inclusive)
295 * @param top top edge of the rectangle to write (inclusive)
296 * @param width width of rectangle to write in pixels.
297 * @param height height of rectangle to write in pixels.
298 * @param config the pixel config of the source buffer
299 * @param buffer GrTransferBuffer to read pixels from
300 * @param offset offset from the start of the buffer
301 * @param rowBytes number of bytes between consecutive rows. Zero
302 * means rows are tightly packed.
303 */
304 bool transferPixels(GrSurface* surface,
305 int left, int top, int width, int height,
306 GrPixelConfig config, GrTransferBuffer* buffer,
307 size_t offset, size_t rowBytes);
308
309 /**
egdaniel51c8d402015-08-06 10:54:13 -0700310 * Clear the passed in render target. Ignores the draw state and clip.
joshualitt3322fa42014-11-07 08:48:51 -0800311 */
egdaniel51c8d402015-08-06 10:54:13 -0700312 void clear(const SkIRect& rect, GrColor color, GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800313
314
bsalomon6d467ec2014-11-18 07:36:19 -0800315 void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800316
317 /**
halcanary96fcdcc2015-08-27 07:41:13 -0700318 * Discards the contents render target. nullptr indicates that the current render target should
joshualitt3322fa42014-11-07 08:48:51 -0800319 * be discarded.
320 **/
halcanary96fcdcc2015-08-27 07:41:13 -0700321 virtual void discard(GrRenderTarget* = nullptr) = 0;
joshualitt3322fa42014-11-07 08:48:51 -0800322
323 /**
324 * This is can be called before allocating a texture to be a dst for copySurface. It will
bsalomonf90a02b2014-11-26 12:28:00 -0800325 * populate the origin, config, and flags fields of the desc such that copySurface can
326 * efficiently succeed. It should only succeed if it can allow copySurface to perform a copy
327 * that would be more effecient than drawing the src to a dst render target.
joshualitt3322fa42014-11-07 08:48:51 -0800328 */
joshualitt1c735482015-07-13 08:08:25 -0700329 virtual bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const = 0;
joshualitt6db519c2014-10-29 08:48:18 -0700330
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000331 // After the client interacts directly with the 3D context state the GrGpu
332 // must resync its internal state and assumptions about 3D context state.
333 // Each time this occurs the GrGpu bumps a timestamp.
334 // state of the 3D context
335 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
336 // a billion years.
337 typedef uint64_t ResetTimestamp;
338
339 // This timestamp is always older than the current timestamp
340 static const ResetTimestamp kExpiredTimestamp = 0;
341 // Returns a timestamp based on the number of times the context was reset.
342 // This timestamp can be used to lazily detect when cached 3D context state
343 // is dirty.
bsalomon6d467ec2014-11-18 07:36:19 -0800344 ResetTimestamp getResetTimestamp() const { return fResetTimestamp; }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000345
joshualitt873ad0e2015-01-20 09:08:51 -0800346 virtual void buildProgramDesc(GrProgramDesc*,
347 const GrPrimitiveProcessor&,
joshualitt465283c2015-09-11 08:19:35 -0700348 const GrPipeline&) const = 0;
joshualitt79f8fae2014-10-28 17:59:26 -0700349
bsalomonf90a02b2014-11-26 12:28:00 -0800350 // 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.
joshualitt1cbdcde2015-08-21 11:53:29 -0700354 bool copySurface(GrSurface* dst,
355 GrSurface* src,
356 const SkIRect& srcRect,
357 const SkIPoint& dstPoint);
joshualitt3322fa42014-11-07 08:48:51 -0800358
joshualitt873ad0e2015-01-20 09:08:51 -0800359 struct DrawArgs {
joshualitt873ad0e2015-01-20 09:08:51 -0800360 DrawArgs(const GrPrimitiveProcessor* primProc,
egdaniel8dd688b2015-01-22 10:16:09 -0800361 const GrPipeline* pipeline,
joshualitt465283c2015-09-11 08:19:35 -0700362 const GrProgramDesc* desc)
joshualitt873ad0e2015-01-20 09:08:51 -0800363 : fPrimitiveProcessor(primProc)
egdaniel8dd688b2015-01-22 10:16:09 -0800364 , fPipeline(pipeline)
joshualitt465283c2015-09-11 08:19:35 -0700365 , fDesc(desc) {
366 SkASSERT(primProc && pipeline && desc);
joshualitt873ad0e2015-01-20 09:08:51 -0800367 }
368 const GrPrimitiveProcessor* fPrimitiveProcessor;
egdaniel8dd688b2015-01-22 10:16:09 -0800369 const GrPipeline* fPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -0800370 const GrProgramDesc* fDesc;
joshualitt873ad0e2015-01-20 09:08:51 -0800371 };
372
bsalomoncb8979d2015-05-05 09:51:38 -0700373 void draw(const DrawArgs&, const GrVertices&);
bsalomon3e791242014-12-17 13:43:13 -0800374
jvanverthd2d2eb92016-02-17 14:04:46 -0800375 // Called by drawtarget when flushing.
376 // Provides a hook for post-flush actions (e.g. PLS reset and Vulkan command buffer submits).
377 virtual void finishDrawTarget() {}
ethannicholas22793252016-01-30 09:59:10 -0800378
mtkleinb9eb4ac2015-02-02 18:26:03 -0800379 ///////////////////////////////////////////////////////////////////////////
380 // Debugging and Stats
381
382 class Stats {
383 public:
384#if GR_GPU_STATS
385 Stats() { this->reset(); }
386
bsalomonb12ea412015-02-02 21:19:50 -0800387 void reset() {
388 fRenderTargetBinds = 0;
389 fShaderCompilations = 0;
390 fTextureCreates = 0;
391 fTextureUploads = 0;
jvanverth17aa0472016-01-05 10:41:27 -0800392 fTransfersToTexture = 0;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700393 fStencilAttachmentCreates = 0;
joshualitt87a5c9f2015-09-08 13:42:05 -0700394 fNumDraws = 0;
bsalomonb12ea412015-02-02 21:19:50 -0800395 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800396
397 int renderTargetBinds() const { return fRenderTargetBinds; }
398 void incRenderTargetBinds() { fRenderTargetBinds++; }
399 int shaderCompilations() const { return fShaderCompilations; }
400 void incShaderCompilations() { fShaderCompilations++; }
bsalomonb12ea412015-02-02 21:19:50 -0800401 int textureCreates() const { return fTextureCreates; }
402 void incTextureCreates() { fTextureCreates++; }
403 int textureUploads() const { return fTextureUploads; }
404 void incTextureUploads() { fTextureUploads++; }
jvanverth17aa0472016-01-05 10:41:27 -0800405 int transfersToTexture() const { return fTransfersToTexture; }
406 void incTransfersToTexture() { fTransfersToTexture++; }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700407 void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
joshualitt87a5c9f2015-09-08 13:42:05 -0700408 void incNumDraws() { fNumDraws++; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800409 void dump(SkString*);
joshualitte45c81c2015-12-02 09:05:37 -0800410 void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800411
412 private:
413 int fRenderTargetBinds;
414 int fShaderCompilations;
bsalomonb12ea412015-02-02 21:19:50 -0800415 int fTextureCreates;
416 int fTextureUploads;
jvanverth17aa0472016-01-05 10:41:27 -0800417 int fTransfersToTexture;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700418 int fStencilAttachmentCreates;
joshualitt87a5c9f2015-09-08 13:42:05 -0700419 int fNumDraws;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800420#else
joshualitte45c81c2015-12-02 09:05:37 -0800421 void dump(SkString*) {}
422 void dumpKeyValuePairs(SkTArray<SkString>*, SkTArray<double>*) {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800423 void incRenderTargetBinds() {}
424 void incShaderCompilations() {}
bsalomonb12ea412015-02-02 21:19:50 -0800425 void incTextureCreates() {}
426 void incTextureUploads() {}
jvanverth17aa0472016-01-05 10:41:27 -0800427 void incTransfersToTexture() {}
egdaniel8dc7c3a2015-04-16 11:22:42 -0700428 void incStencilAttachmentCreates() {}
joshualitt87a5c9f2015-09-08 13:42:05 -0700429 void incNumDraws() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800430#endif
431 };
432
433 Stats* stats() { return &fStats; }
434
bsalomon67d76202015-11-11 12:40:42 -0800435 /** Creates a texture directly in the backend API without wrapping it in a GrTexture. This is
436 only to be used for testing (particularly for testing the methods that import an externally
437 created texture into Skia. Must be matched with a call to deleteTestingOnlyTexture(). */
jvanverth88957922015-07-14 11:02:52 -0700438 virtual GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomone63ffef2016-02-05 07:17:34 -0800439 GrPixelConfig config) = 0;
bsalomon67d76202015-11-11 12:40:42 -0800440 /** Check a handle represents an actual texture in the backend API that has not been freed. */
441 virtual bool isTestingOnlyBackendTexture(GrBackendObject) const = 0;
442 /** If ownership of the backend texture has been transferred pass true for abandonTexture. This
443 will do any necessary cleanup of the handle without freeing the texture in the backend
444 API. */
445 virtual void deleteTestingOnlyBackendTexture(GrBackendObject,
bsalomone63ffef2016-02-05 07:17:34 -0800446 bool abandonTexture = false) = 0;
jvanverth672bb7f2015-07-13 07:19:57 -0700447
egdanielec00d942015-09-14 12:56:10 -0700448 // width and height may be larger than rt (if underlying API allows it).
449 // Returns nullptr if compatible sb could not be created, otherwise the caller owns the ref on
450 // the GrStencilAttachment.
451 virtual GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
452 int width,
453 int height) = 0;
454 // clears target's entire stencil buffer to 0
455 virtual void clearStencil(GrRenderTarget* target) = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800456
bsalomon6dea83f2015-12-03 12:58:06 -0800457 // draws an outline rectangle for debugging/visualization purposes.
458 virtual void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) = 0;
bsalomon045802d2015-10-20 07:58:01 -0700459
bsalomone179a912016-01-20 06:18:10 -0800460 // Determines whether a texture will need to be rescaled in order to be used with the
461 // GrTextureParams. This variation is called when the caller will create a new texture using the
462 // texture provider from a non-texture src (cpu-backed image, ...).
bsalomon045802d2015-10-20 07:58:01 -0700463 bool makeCopyForTextureParams(int width, int height, const GrTextureParams&,
bsalomone179a912016-01-20 06:18:10 -0800464 GrTextureProducer::CopyParams*) const;
465
466 // Like the above but this variation should be called when the caller is not creating the
467 // original texture but rather was handed the original texture. It adds additional checks
468 // relevant to original textures that were created external to Skia via
469 // GrTextureProvider::wrap methods.
470 bool makeCopyForTextureParams(GrTexture* texture, const GrTextureParams& params,
471 GrTextureProducer::CopyParams* copyParams) const {
472 if (this->makeCopyForTextureParams(texture->width(), texture->height(), params,
473 copyParams)) {
474 return true;
475 }
476 return this->onMakeCopyForTextureParams(texture, params, copyParams);
477 }
bsalomon045802d2015-10-20 07:58:01 -0700478
jvanverth672bb7f2015-07-13 07:19:57 -0700479 // This is only to be used in GL-specific tests.
halcanary96fcdcc2015-08-27 07:41:13 -0700480 virtual const GrGLContext* glContextForTesting() const { return nullptr; }
bsalomon993a4212015-05-29 11:37:25 -0700481
joshualitt8fd844f2015-12-02 13:36:47 -0800482 // This is only to be used by testing code
483 virtual void resetShaderCacheForTesting() const {}
484
joshualittd53a8272014-11-10 16:03:14 -0800485protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000486 // Functions used to map clip-respecting stencil tests into normal
487 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000488 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000489 GrStencilFunc func);
490 static void ConvertStencilFuncAndMask(GrStencilFunc func,
491 bool clipInStencil,
492 unsigned int clipBit,
493 unsigned int userBits,
494 unsigned int* ref,
495 unsigned int* mask);
496
bsalomonf0674512015-07-28 13:26:15 -0700497 static void ElevateDrawPreference(GrGpu::DrawPreference* preference,
498 GrGpu::DrawPreference elevation) {
499 GR_STATIC_ASSERT(GrGpu::kCallerPrefersDraw_DrawPreference > GrGpu::kNoDraw_DrawPreference);
500 GR_STATIC_ASSERT(GrGpu::kGpuPrefersDraw_DrawPreference >
501 GrGpu::kCallerPrefersDraw_DrawPreference);
502 GR_STATIC_ASSERT(GrGpu::kRequireDraw_DrawPreference >
503 GrGpu::kGpuPrefersDraw_DrawPreference);
504 *preference = SkTMax(*preference, elevation);
505 }
506
joshualitt93316b92015-10-23 09:08:08 -0700507 void handleDirtyContext() {
508 if (fResetBits) {
509 this->resetContext();
510 }
511 }
512
mtkleinb9eb4ac2015-02-02 18:26:03 -0800513 Stats fStats;
514 SkAutoTDelete<GrPathRendering> fPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800515 // Subclass must initialize this in its constructor.
bsalomon4b91f762015-05-19 09:29:46 -0700516 SkAutoTUnref<const GrCaps> fCaps;
joshualitt3322fa42014-11-07 08:48:51 -0800517
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000518private:
bsalomon@google.comb635d392011-11-05 12:47:43 +0000519 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000520 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000521 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000522
bsalomoncb02b382015-08-12 11:14:50 -0700523 // Called before certain draws in order to guarantee coherent results from dst reads.
524 virtual void xferBarrier(GrRenderTarget*, GrXferBarrierType) = 0;
525
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000526 // overridden by backend-specific derived class to create objects.
egdanielb0e1be22015-04-22 13:27:39 -0700527 // Texture size and sample size will have already been validated in base class before
528 // onCreateTexture/CompressedTexture are called.
529 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
530 GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -0800531 const SkTArray<GrMipLevel>& texels) = 0;
egdanielb0e1be22015-04-22 13:27:39 -0700532 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
533 GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -0800534 const SkTArray<GrMipLevel>& texels) = 0;
535
bsalomon6dc6f5f2015-06-18 09:12:16 -0700536 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) = 0;
537 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
538 GrWrapOwnership) = 0;
ericrkf7b8b8a2016-02-24 14:49:51 -0800539 virtual GrRenderTarget* onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&,
540 GrWrapOwnership) = 0;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000541 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
542 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
jvanverth73063dc2015-12-03 09:15:47 -0800543 virtual GrTransferBuffer* onCreateTransferBuffer(size_t size, TransferType type) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000544
bsalomon63b21962014-11-05 07:05:34 -0800545 // overridden by backend-specific derived class to perform the clear.
egdaniel51c8d402015-08-06 10:54:13 -0700546 virtual void onClear(GrRenderTarget*, const SkIRect& rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000547
joshualitt6db519c2014-10-29 08:48:18 -0700548
549 // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is
550 // ONLY used by the the clip target
mtkleinb9eb4ac2015-02-02 18:26:03 -0800551 virtual void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) = 0;
joshualitt6db519c2014-10-29 08:48:18 -0700552
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000553 // overridden by backend-specific derived class to perform the draw call.
bsalomone64eb572015-05-07 11:35:55 -0700554 virtual void onDraw(const DrawArgs&, const GrNonInstancedVertices&) = 0;
bsalomon3e791242014-12-17 13:43:13 -0800555
bsalomone179a912016-01-20 06:18:10 -0800556 virtual bool onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams&,
557 GrTextureProducer::CopyParams*) const { return false; }
558
bsalomonf0674512015-07-28 13:26:15 -0700559 virtual bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight,
560 size_t rowBytes, GrPixelConfig readConfig, DrawPreference*,
561 ReadPixelTempDrawInfo*) = 0;
cblumeed828002016-02-16 13:00:01 -0800562 virtual bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
bsalomonf0674512015-07-28 13:26:15 -0700563 GrPixelConfig srcConfig, DrawPreference*,
564 WritePixelTempDrawInfo*) = 0;
565
bsalomon6cb3cbe2015-07-30 07:34:27 -0700566 // overridden by backend-specific derived class to perform the surface read
567 virtual bool onReadPixels(GrSurface*,
egdaniel6d901da2015-07-30 12:02:15 -0700568 int left, int top,
569 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000570 GrPixelConfig,
571 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000572 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000573
bsalomon6cb3cbe2015-07-30 07:34:27 -0700574 // overridden by backend-specific derived class to perform the surface write
575 virtual bool onWritePixels(GrSurface*,
576 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800577 GrPixelConfig config,
578 const SkTArray<GrMipLevel>& texels) = 0;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000579
jvanverth17aa0472016-01-05 10:41:27 -0800580 // overridden by backend-specific derived class to perform the surface write
581 virtual bool onTransferPixels(GrSurface*,
582 int left, int top, int width, int height,
583 GrPixelConfig config, GrTransferBuffer* buffer,
584 size_t offset, size_t rowBytes) = 0;
585
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000586 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000587 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
588
joshualitt1cbdcde2015-08-21 11:53:29 -0700589 // overridden by backend specific derived class to perform the copy surface
590 virtual bool onCopySurface(GrSurface* dst,
591 GrSurface* src,
592 const SkIRect& srcRect,
593 const SkIPoint& dstPoint) = 0;
594
bsalomon@google.comb635d392011-11-05 12:47:43 +0000595 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000596 this->onResetContext(fResetBits);
597 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000598 ++fResetTimestamp;
599 }
600
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000601 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000602 uint32_t fResetBits;
joshualitt3322fa42014-11-07 08:48:51 -0800603 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
604 GrContext* fContext;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000605
kkinnunencabe20c2015-06-01 01:37:26 -0700606 friend class GrPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800607 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000608};
609
610#endif