blob: 40d766431a0662d4f1e6c96f75dc8613719a58a6 [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
Robert Phillipse42edcc2017-12-13 11:50:22 -050011#include "GrCaps.h"
egdaniel066df7c2016-06-08 14:02:27 -070012#include "GrGpuCommandBuffer.h"
joshualitt79f8fae2014-10-28 17:59:26 -070013#include "GrProgramDesc.h"
bsalomon6c9cd552016-01-22 07:17:34 -080014#include "GrSwizzle.h"
cdalton28f45b92016-03-07 13:58:26 -080015#include "GrAllocator.h"
Brian Osmane8e54582016-11-28 10:06:27 -050016#include "GrTextureProducer.h"
cblume55f2d2d2016-02-26 13:20:48 -080017#include "GrTypes.h"
kkinnunencabe20c2015-06-01 01:37:26 -070018#include "GrXferProcessor.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000019#include "SkPath.h"
cblume55f2d2d2016-02-26 13:20:48 -080020#include "SkTArray.h"
csmartdalton0d28e572016-07-06 09:59:43 -070021#include <map>
sugoi@google.com12b4e272012-12-06 20:13:11 +000022
Greg Danielbcf612b2017-05-01 13:50:58 +000023class GrBackendRenderTarget;
Greg Daniela5cb7812017-06-16 09:45:32 -040024class GrBackendSemaphore;
cdalton397536c2016-03-25 12:15:03 -070025class GrBuffer;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000026class GrContext;
robertphillips28a838e2016-06-23 14:07:00 -070027struct GrContextOptions;
reedf9ad5582015-06-25 21:29:25 -070028class GrGLContext;
Chris Daltonbca46e22017-05-15 11:03:26 -060029class GrMesh;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000030class GrPath;
cdaltonb85a0aa2014-07-21 15:32:44 -070031class GrPathRange;
bsalomon@google.com30085192011-08-19 15:42:31 +000032class GrPathRenderer;
33class GrPathRendererChain;
kkinnunencabe20c2015-06-01 01:37:26 -070034class GrPathRendering;
egdaniel8dd688b2015-01-22 10:16:09 -080035class GrPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -080036class GrPrimitiveProcessor;
kkinnunencabe20c2015-06-01 01:37:26 -070037class GrRenderTarget;
Greg Daniel6be35232017-03-01 17:01:09 -050038class GrSemaphore;
egdaniel8dc7c3a2015-04-16 11:22:42 -070039class GrStencilAttachment;
cdalton93a379b2016-05-11 13:58:08 -070040class GrStencilSettings;
kkinnunencabe20c2015-06-01 01:37:26 -070041class GrSurface;
42class GrTexture;
Brian Osman71a18892017-08-10 10:23:25 -040043class SkJSONWriter;
reed@google.comac10a2d2010-12-22 21:39:39 +000044
joshualitt3322fa42014-11-07 08:48:51 -080045class GrGpu : public SkRefCnt {
reed@google.comac10a2d2010-12-22 21:39:39 +000046public:
47 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000048 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
halcanary96fcdcc2015-08-27 07:41:13 -070049 * not supported (at compile-time or run-time) this returns nullptr. The context will not be
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000050 * fully constructed and should not be used by GrGpu until after this function returns.
reed@google.comac10a2d2010-12-22 21:39:39 +000051 */
Robert Phillipse42edcc2017-12-13 11:50:22 -050052 static sk_sp<GrGpu> Make(GrBackend, GrBackendContext, const GrContextOptions&, GrContext*);
reed@google.comac10a2d2010-12-22 21:39:39 +000053
54 ////////////////////////////////////////////////////////////////////////////
55
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000056 GrGpu(GrContext* context);
mtklein36352bf2015-03-25 18:17:31 -070057 ~GrGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000058
joshualitt3322fa42014-11-07 08:48:51 -080059 GrContext* getContext() { return fContext; }
60 const GrContext* getContext() const { return fContext; }
61
62 /**
63 * Gets the capabilities of the draw target.
64 */
bsalomon4b91f762015-05-19 09:29:46 -070065 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipse42edcc2017-12-13 11:50:22 -050066 sk_sp<const GrCaps> refCaps() const { return fCaps; }
joshualitt3322fa42014-11-07 08:48:51 -080067
kkinnunencabe20c2015-06-01 01:37:26 -070068 GrPathRendering* pathRendering() { return fPathRendering.get(); }
kkinnunenccdaa042014-08-20 01:36:23 -070069
bsalomon6e2aad42016-04-01 11:54:31 -070070 enum class DisconnectType {
71 // No cleanup should be attempted, immediately cease making backend API calls
72 kAbandon,
73 // Free allocated resources (not known by GrResourceCache) before returning and
74 // ensure no backend backend 3D API calls will be made after disconnect() returns.
75 kCleanup,
76 };
77
78 // Called by GrContext when the underlying backend context is already or will be destroyed
79 // before GrContext.
80 virtual void disconnect(DisconnectType);
bsalomonc8dc1f72014-08-21 13:02:13 -070081
reed@google.comac10a2d2010-12-22 21:39:39 +000082 /**
83 * The GrGpu object normally assumes that no outsider is setting state
84 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000085 * the GrGpu that the state was modified and it shouldn't make assumptions
86 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000087 */
mtkleinb9eb4ac2015-02-02 18:26:03 -080088 void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
reed@google.comac10a2d2010-12-22 21:39:39 +000089
90 /**
bsalomon6d467ec2014-11-18 07:36:19 -080091 * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can
92 * be used as a render target by calling GrTexture::asRenderTarget(). Not all
93 * pixel configs can be used as render targets. Support for configs as textures
bsalomon4b91f762015-05-19 09:29:46 -070094 * or render targets can be checked using GrCaps.
bsalomon@google.com1da07462011-03-10 14:51:57 +000095 *
Brian Salomon58389b92018-03-07 13:01:25 -050096 * @param desc describes the texture to be created.
97 * @param budgeted does this texture count against the resource cache budget?
98 * @param texels array of mipmap levels containing texel data to load.
99 * Each level begins with full-size palette data for paletted textures.
100 * It contains width*height texels. If there is only one
101 * element and it contains nullptr fPixels, texture data is
102 * uninitialized.
103 * @param mipLevelCount the number of levels in 'texels'
104 * @return The texture object if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 */
Brian Salomon58389b92018-03-07 13:01:25 -0500106 sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, const GrMipLevel texels[],
107 int mipLevelCount);
cblume55f2d2d2016-02-26 13:20:48 -0800108
109 /**
bsalomone699d0c2016-03-09 06:25:15 -0800110 * Simplified createTexture() interface for when there is no initial texel data to upload.
cblume55f2d2d2016-02-26 13:20:48 -0800111 */
Robert Phillips16d8ec62017-07-27 16:16:25 -0400112 sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc, SkBudgeted);
bsalomone699d0c2016-03-09 06:25:15 -0800113
bsalomon@google.come269f212011-11-07 13:29:52 +0000114 /**
Brian Osman32342f02017-03-04 08:12:46 -0500115 * Implements GrResourceProvider::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000116 */
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400117 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture&, GrWrapOwnership);
Brian Salomond17f6582017-07-19 18:28:58 -0400118
119 /**
120 * Implements GrResourceProvider::wrapRenderableBackendTexture
121 */
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400122 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture&,
Brian Salomond17f6582017-07-19 18:28:58 -0400123 int sampleCnt, GrWrapOwnership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000124
125 /**
Brian Osman32342f02017-03-04 08:12:46 -0500126 * Implements GrResourceProvider::wrapBackendRenderTarget
bsalomon@google.come269f212011-11-07 13:29:52 +0000127 */
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400128 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000129
130 /**
Brian Osman32342f02017-03-04 08:12:46 -0500131 * Implements GrResourceProvider::wrapBackendTextureAsRenderTarget
ericrkf7b8b8a2016-02-24 14:49:51 -0800132 */
Greg Daniel7ef28f32017-04-20 16:41:55 +0000133 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000134 int sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800135
136 /**
csmartdalton485a1202016-07-13 10:16:32 -0700137 * Creates a buffer in GPU memory. For a client-side buffer use GrBuffer::CreateCPUBacked.
reed@google.comac10a2d2010-12-22 21:39:39 +0000138 *
cdaltone2e71c22016-04-07 18:13:29 -0700139 * @param size size of buffer to create.
140 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
141 * @param accessPattern hint to the graphics subsystem about how the data will be accessed.
cdalton1bf3e712016-04-19 10:00:02 -0700142 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700143 *
cdalton397536c2016-03-25 12:15:03 -0700144 * @return the buffer if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 */
cdalton1bf3e712016-04-19 10:00:02 -0700146 GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern accessPattern,
147 const void* data = nullptr);
halcanary9d524f22016-03-29 09:03:52 -0700148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000150 * Resolves MSAA.
151 */
Brian Salomon1fabd512018-02-09 09:54:25 -0500152 void resolveRenderTarget(GrRenderTarget*);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000153
bsalomonf0674512015-07-28 13:26:15 -0700154 /** Info struct returned by getReadPixelsInfo about performing intermediate draws before
bsalomon39826022015-07-23 08:07:21 -0700155 reading pixels for performance or correctness. */
156 struct ReadPixelTempDrawInfo {
Brian Salomonc320b152018-02-20 14:05:36 -0500157 /**
158 * If the GrGpu is requesting that the caller do a draw to an intermediate surface then
159 * this is descriptor for the temp surface. The draw should always be a rect with dst
160 * 0,0,w,h.
161 */
bsalomon39826022015-07-23 08:07:21 -0700162 GrSurfaceDesc fTempSurfaceDesc;
Brian Salomonc320b152018-02-20 14:05:36 -0500163 /**
164 * Indicates whether there is a performance advantage to using an exact match texture
165 * (in terms of width and height) for the intermediate texture instead of approximate.
166 */
bsalomonb117ff12016-07-19 07:24:40 -0700167 SkBackingFit fTempSurfaceFit;
Brian Salomonc320b152018-02-20 14:05:36 -0500168 /**
169 * Swizzle to apply during the draw. This is used to compensate for either feature or
170 * performance limitations in the underlying 3D API.
171 */
bsalomon6c9cd552016-01-22 07:17:34 -0800172 GrSwizzle fSwizzle;
Brian Salomonc320b152018-02-20 14:05:36 -0500173 /**
174 * The color type that should be used to read from the temp surface after the draw. This
175 * may be different than the original read color type in order to compensate for swizzling.
176 * The read data will effectively be in the original color type. The original gamma
177 * encoding is always used.
178 */
179 GrColorType fReadColorType;
bsalomon39826022015-07-23 08:07:21 -0700180 };
bsalomon6c9cd552016-01-22 07:17:34 -0800181
bsalomon39826022015-07-23 08:07:21 -0700182 /** Describes why an intermediate draw must/should be performed before readPixels. */
183 enum DrawPreference {
Brian Salomonc320b152018-02-20 14:05:36 -0500184 /**
185 * On input means that the caller would proceed without draw if the GrGpu doesn't request
186 * one. On output means that the GrGpu is not requesting a draw.
187 */
bsalomon39826022015-07-23 08:07:21 -0700188 kNoDraw_DrawPreference,
Brian Salomonc320b152018-02-20 14:05:36 -0500189 /**
190 * Means that the client would prefer a draw for performance of the readback but
191 * can satisfy a straight readPixels call on the inputs without an intermediate draw.
192 * getReadPixelsInfo will never set the draw preference to this value but may leave
193 * it set.
194 */
bsalomon39826022015-07-23 08:07:21 -0700195 kCallerPrefersDraw_DrawPreference,
Brian Salomonc320b152018-02-20 14:05:36 -0500196 /**
197 * On output means that GrGpu would prefer a draw for performance of the readback but
198 * can satisfy a straight readPixels call on the inputs without an intermediate draw. The
199 * caller of getReadPixelsInfo should never specify this on intput.
200 */
bsalomon39826022015-07-23 08:07:21 -0700201 kGpuPrefersDraw_DrawPreference,
Brian Salomonc320b152018-02-20 14:05:36 -0500202 /**
203 * On input means that the caller requires a draw to do a transformation and there is no
204 * CPU fallback. On output means that GrGpu can only satisfy the readPixels request if the
205 * intermediate draw is performed.
206 */
bsalomon39826022015-07-23 08:07:21 -0700207 kRequireDraw_DrawPreference
208 };
209
bsalomonf0674512015-07-28 13:26:15 -0700210 /**
211 * Used to negotiate whether and how an intermediate draw should or must be performed before
212 * a readPixels call. If this returns false then GrGpu could not deduce an intermediate draw
213 * that would allow a successful readPixels call. The passed width, height, and rowBytes,
214 * must be non-zero and already reflect clipping to the src bounds.
215 */
Brian Salomonc320b152018-02-20 14:05:36 -0500216 bool getReadPixelsInfo(GrSurface*, GrSurfaceOrigin, int width, int height, size_t rowBytes,
217 GrColorType, GrSRGBConversion, DrawPreference*, ReadPixelTempDrawInfo*);
bsalomonf0674512015-07-28 13:26:15 -0700218
Brian Salomonc320b152018-02-20 14:05:36 -0500219 /**
220 * Info struct returned by getWritePixelsInfo about performing an intermediate draw in order
221 * to write pixels to a GrSurface for either performance or correctness reasons.
222 */
bsalomonf0674512015-07-28 13:26:15 -0700223 struct WritePixelTempDrawInfo {
Brian Salomonc320b152018-02-20 14:05:36 -0500224 /**
225 * If the GrGpu is requesting that the caller upload to an intermediate surface and draw
226 * that to the dst then this is the descriptor for the intermediate surface. The caller
227 * should upload the pixels such that the upper left pixel of the upload rect is at 0,0 in
228 * the intermediate surface
229 */
bsalomonf0674512015-07-28 13:26:15 -0700230 GrSurfaceDesc fTempSurfaceDesc;
Brian Salomonc320b152018-02-20 14:05:36 -0500231 /**
232 * Swizzle to apply during the draw. This is used to compensate for either feature or
233 * performance limitations in the underlying 3D API.
234 */
bsalomon6c9cd552016-01-22 07:17:34 -0800235 GrSwizzle fSwizzle;
Brian Salomonc320b152018-02-20 14:05:36 -0500236 /**
237 * The color type that should be specified when uploading the *original* data to the temp
238 * surface before the draw. This may be different than the original src color type in
239 * order to compensate for swizzling that will occur when drawing. The original gamma
240 * encoding is always used.
241 */
242 GrColorType fWriteColorType;
bsalomonf0674512015-07-28 13:26:15 -0700243 };
bsalomon39826022015-07-23 08:07:21 -0700244
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000245 /**
bsalomonf0674512015-07-28 13:26:15 -0700246 * Used to negotiate whether and how an intermediate surface should be used to write pixels to
247 * a GrSurface. If this returns false then GrGpu could not deduce an intermediate draw
248 * that would allow a successful transfer of the src pixels to the dst. The passed width,
249 * height, and rowBytes, must be non-zero and already reflect clipping to the dst bounds.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000250 */
Brian Salomonc320b152018-02-20 14:05:36 -0500251 bool getWritePixelsInfo(GrSurface*, GrSurfaceOrigin, int width, int height, GrColorType,
252 GrSRGBConversion, DrawPreference*, WritePixelTempDrawInfo*);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000253
254 /**
Brian Salomon9b009bb2018-02-14 13:53:55 -0500255 * Reads a rectangle of pixels from a render target. No sRGB/linear conversions are performed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000256 *
bsalomon6cb3cbe2015-07-30 07:34:27 -0700257 * @param surface The surface to read from
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000258 * @param left left edge of the rectangle to read (inclusive)
259 * @param top top edge of the rectangle to read (inclusive)
260 * @param width width of rectangle to read in pixels.
261 * @param height height of rectangle to read in pixels.
Brian Salomonc320b152018-02-20 14:05:36 -0500262 * @param dstColorType the color type of the destination buffer.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000263 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000264 * @param rowBytes the number of bytes between consecutive rows. Zero
265 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000266 * @param invertY buffer should be populated bottom-to-top as opposed
267 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000268 *
269 * @return true if the read succeeded, false if not. The read can fail
270 * because of a unsupported pixel config or because no render
271 * target is currently set.
272 */
Brian Salomon9b009bb2018-02-14 13:53:55 -0500273 bool readPixels(GrSurface* surface, GrSurfaceOrigin, int left, int top, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500274 GrColorType dstColorType, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000275
bsalomon@google.com6f379512011-11-16 20:36:03 +0000276 /**
Brian Salomon9b009bb2018-02-14 13:53:55 -0500277 * Updates the pixels in a rectangle of a surface. No sRGB/linear conversions are performed.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000278 *
bsalomon6cb3cbe2015-07-30 07:34:27 -0700279 * @param surface The surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000280 * @param left left edge of the rectangle to write (inclusive)
281 * @param top top edge of the rectangle to write (inclusive)
282 * @param width width of rectangle to write in pixels.
283 * @param height height of rectangle to write in pixels.
Brian Salomonc320b152018-02-20 14:05:36 -0500284 * @param srcColorType the color type of the source buffer.
cblume55f2d2d2016-02-26 13:20:48 -0800285 * @param texels array of mipmap levels containing texture data
Robert Phillips590533f2017-07-11 14:22:35 -0400286 * @param mipLevelCount number of levels in 'texels'
cblume55f2d2d2016-02-26 13:20:48 -0800287 */
Brian Salomonc320b152018-02-20 14:05:36 -0500288 bool writePixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, int width,
289 int height, GrColorType srcColorType, const GrMipLevel texels[],
290 int mipLevelCount);
cblume55f2d2d2016-02-26 13:20:48 -0800291
292 /**
293 * This function is a shim which creates a SkTArray<GrMipLevel> of size 1.
294 * It then calls writePixels with that SkTArray.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000295 */
Brian Salomonc320b152018-02-20 14:05:36 -0500296 bool writePixels(GrSurface*, GrSurfaceOrigin, int left, int top, int width, int height,
297 GrColorType, const void* buffer, size_t rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000298
joshualitt3322fa42014-11-07 08:48:51 -0800299 /**
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500300 * This version of writePixels doesn't take an origin. TODO: Remove origin handling from
301 * GrGpu::writePixels entirely.
302 */
303 bool writePixels(GrSurface* surface, int left, int top, int width, int height,
304 GrColorType srcColorType, const void* buffer, size_t rowBytes) {
305 return this->writePixels(surface, kTopLeft_GrSurfaceOrigin, left, top, width, height,
306 srcColorType, buffer, rowBytes);
307 }
308
309 /**
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400310 * Updates the pixels in a rectangle of a texture using a buffer
jvanverth17aa0472016-01-05 10:41:27 -0800311 *
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400312 * There are a couple of assumptions here. First, we only update the top miplevel.
313 * And second, that any y flip needed has already been done in the buffer.
314 *
315 * @param texture The texture to write to.
cdalton397536c2016-03-25 12:15:03 -0700316 * @param left left edge of the rectangle to write (inclusive)
317 * @param top top edge of the rectangle to write (inclusive)
318 * @param width width of rectangle to write in pixels.
319 * @param height height of rectangle to write in pixels.
Brian Salomonc320b152018-02-20 14:05:36 -0500320 * @param bufferColorType the color type of the transfer buffer's pixel data
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400321 * @param transferBuffer GrBuffer to read pixels from (type must be "kXferCpuToGpu")
cdalton397536c2016-03-25 12:15:03 -0700322 * @param offset offset from the start of the buffer
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400323 * @param rowBytes number of bytes between consecutive rows in the buffer. Zero
cdalton397536c2016-03-25 12:15:03 -0700324 * means rows are tightly packed.
jvanverth17aa0472016-01-05 10:41:27 -0800325 */
Brian Salomon9b009bb2018-02-14 13:53:55 -0500326 bool transferPixels(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500327 GrColorType bufferColorType, GrBuffer* transferBuffer, size_t offset,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500328 size_t rowBytes);
jvanverth17aa0472016-01-05 10:41:27 -0800329
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000330 // After the client interacts directly with the 3D context state the GrGpu
331 // must resync its internal state and assumptions about 3D context state.
332 // Each time this occurs the GrGpu bumps a timestamp.
333 // state of the 3D context
334 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
335 // a billion years.
336 typedef uint64_t ResetTimestamp;
337
338 // This timestamp is always older than the current timestamp
339 static const ResetTimestamp kExpiredTimestamp = 0;
340 // Returns a timestamp based on the number of times the context was reset.
341 // This timestamp can be used to lazily detect when cached 3D context state
342 // is dirty.
bsalomon6d467ec2014-11-18 07:36:19 -0800343 ResetTimestamp getResetTimestamp() const { return fResetTimestamp; }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000344
bsalomonf90a02b2014-11-26 12:28:00 -0800345 // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst
Robert Phillipsf2361d22016-10-25 14:20:06 -0400346 // take place at the GrOpList level and this function implement faster copy paths. The rect
bsalomonf90a02b2014-11-26 12:28:00 -0800347 // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the
348 // src/dst bounds and non-empty.
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400349 bool copySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
350 GrSurface* src, GrSurfaceOrigin srcOrigin,
joshualitt1cbdcde2015-08-21 11:53:29 -0700351 const SkIRect& srcRect,
352 const SkIPoint& dstPoint);
joshualitt3322fa42014-11-07 08:48:51 -0800353
Greg Daniel500d58b2017-08-24 15:59:33 -0400354 // Creates a GrGpuRTCommandBuffer which GrOpLists send draw commands to instead of directly
355 // to the Gpu object.
356 virtual GrGpuRTCommandBuffer* createCommandBuffer(
Robert Phillips95214472017-08-08 18:00:03 -0400357 GrRenderTarget*, GrSurfaceOrigin,
Greg Daniel500d58b2017-08-24 15:59:33 -0400358 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
359 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) = 0;
360
361 // Creates a GrGpuTextureCommandBuffer which GrOpLists send texture commands to instead of
362 // directly to the Gpu object.
363 virtual GrGpuTextureCommandBuffer* createCommandBuffer(GrTexture*, GrSurfaceOrigin) = 0;
bsalomon3e791242014-12-17 13:43:13 -0800364
Robert Phillips18e94842017-05-15 13:06:44 -0400365 // Called by GrDrawingManager when flushing.
Greg Daniel51316782017-08-02 15:10:09 +0000366 // Provides a hook for post-flush actions (e.g. Vulkan command buffer submits). This will also
367 // insert any numSemaphore semaphores on the gpu and set the backendSemaphores to match the
368 // inserted semaphores.
369 GrSemaphoresSubmitted finishFlush(int numSemaphores, GrBackendSemaphore backendSemaphores[]);
ethannicholas22793252016-01-30 09:59:10 -0800370
Greg Daniel6be35232017-03-01 17:01:09 -0500371 virtual GrFence SK_WARN_UNUSED_RESULT insertFence() = 0;
372 virtual bool waitFence(GrFence, uint64_t timeout = 1000) = 0;
jvanverth84741b32016-09-30 08:39:02 -0700373 virtual void deleteFence(GrFence) const = 0;
374
Greg Daniela5cb7812017-06-16 09:45:32 -0400375 virtual sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true) = 0;
Greg Daniel48661b82018-01-22 16:11:35 -0500376 virtual sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
377 GrResourceProvider::SemaphoreWrapType wrapType,
378 GrWrapOwnership ownership) = 0;
379 virtual void insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush = false) = 0;
380 virtual void waitSemaphore(sk_sp<GrSemaphore> semaphore) = 0;
Greg Daniel6be35232017-03-01 17:01:09 -0500381
Brian Osman13dddce2017-05-09 13:19:50 -0400382 /**
383 * Put this texture in a safe and known state for use across multiple GrContexts. Depending on
384 * the backend, this may return a GrSemaphore. If so, other contexts should wait on that
385 * semaphore before using this texture.
386 */
387 virtual sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) = 0;
388
mtkleinb9eb4ac2015-02-02 18:26:03 -0800389 ///////////////////////////////////////////////////////////////////////////
390 // Debugging and Stats
391
392 class Stats {
393 public:
394#if GR_GPU_STATS
395 Stats() { this->reset(); }
396
bsalomonb12ea412015-02-02 21:19:50 -0800397 void reset() {
398 fRenderTargetBinds = 0;
399 fShaderCompilations = 0;
400 fTextureCreates = 0;
401 fTextureUploads = 0;
jvanverth17aa0472016-01-05 10:41:27 -0800402 fTransfersToTexture = 0;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700403 fStencilAttachmentCreates = 0;
joshualitt87a5c9f2015-09-08 13:42:05 -0700404 fNumDraws = 0;
bsalomon1d417a82016-03-23 11:50:26 -0700405 fNumFailedDraws = 0;
bsalomonb12ea412015-02-02 21:19:50 -0800406 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800407
408 int renderTargetBinds() const { return fRenderTargetBinds; }
409 void incRenderTargetBinds() { fRenderTargetBinds++; }
410 int shaderCompilations() const { return fShaderCompilations; }
411 void incShaderCompilations() { fShaderCompilations++; }
bsalomonb12ea412015-02-02 21:19:50 -0800412 int textureCreates() const { return fTextureCreates; }
413 void incTextureCreates() { fTextureCreates++; }
414 int textureUploads() const { return fTextureUploads; }
415 void incTextureUploads() { fTextureUploads++; }
jvanverth17aa0472016-01-05 10:41:27 -0800416 int transfersToTexture() const { return fTransfersToTexture; }
417 void incTransfersToTexture() { fTransfersToTexture++; }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700418 void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
joshualitt87a5c9f2015-09-08 13:42:05 -0700419 void incNumDraws() { fNumDraws++; }
bsalomon1d417a82016-03-23 11:50:26 -0700420 void incNumFailedDraws() { ++fNumFailedDraws; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800421 void dump(SkString*);
joshualitte45c81c2015-12-02 09:05:37 -0800422 void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values);
bsalomon1d417a82016-03-23 11:50:26 -0700423 int numDraws() const { return fNumDraws; }
424 int numFailedDraws() const { return fNumFailedDraws; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800425 private:
426 int fRenderTargetBinds;
427 int fShaderCompilations;
bsalomonb12ea412015-02-02 21:19:50 -0800428 int fTextureCreates;
429 int fTextureUploads;
jvanverth17aa0472016-01-05 10:41:27 -0800430 int fTransfersToTexture;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700431 int fStencilAttachmentCreates;
joshualitt87a5c9f2015-09-08 13:42:05 -0700432 int fNumDraws;
bsalomon1d417a82016-03-23 11:50:26 -0700433 int fNumFailedDraws;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800434#else
joshualitte45c81c2015-12-02 09:05:37 -0800435 void dump(SkString*) {}
436 void dumpKeyValuePairs(SkTArray<SkString>*, SkTArray<double>*) {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800437 void incRenderTargetBinds() {}
438 void incShaderCompilations() {}
bsalomonb12ea412015-02-02 21:19:50 -0800439 void incTextureCreates() {}
440 void incTextureUploads() {}
jvanverth17aa0472016-01-05 10:41:27 -0800441 void incTransfersToTexture() {}
egdaniel8dc7c3a2015-04-16 11:22:42 -0700442 void incStencilAttachmentCreates() {}
joshualitt87a5c9f2015-09-08 13:42:05 -0700443 void incNumDraws() {}
bsalomon1d417a82016-03-23 11:50:26 -0700444 void incNumFailedDraws() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800445#endif
446 };
447
448 Stats* stats() { return &fStats; }
Brian Osman71a18892017-08-10 10:23:25 -0400449 void dumpJSON(SkJSONWriter*) const;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800450
Brian Salomonf865b052018-03-09 09:01:53 -0500451#if GR_TEST_UTILS
bsalomon67d76202015-11-11 12:40:42 -0800452 /** Creates a texture directly in the backend API without wrapping it in a GrTexture. This is
453 only to be used for testing (particularly for testing the methods that import an externally
454 created texture into Skia. Must be matched with a call to deleteTestingOnlyTexture(). */
Brian Salomonbdecacf2018-02-02 20:32:49 -0500455 GrBackendTexture createTestingOnlyBackendTexture(void* pixels, int w, int h, SkColorType,
456 bool isRenderTarget, GrMipMapped);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500457 /** Older version based on GrPixelConfig. Currently the preferred one above devolves to this. */
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500458 virtual GrBackendTexture createTestingOnlyBackendTexture(
459 void* pixels, int w, int h,
460 GrPixelConfig config,
461 bool isRenderTarget,
462 GrMipMapped mipMapped) = 0;
463 /** Check a handle represents an actual texture in the backend API that has not been freed. */
464 virtual bool isTestingOnlyBackendTexture(const GrBackendTexture&) const = 0;
Brian Salomone64b0642018-03-07 11:47:54 -0500465 /**
466 * Frees a texture created by createTestingOnlyBackendTexture(). If ownership of the backend
467 * texture has been transferred to a GrContext using adopt semantics this should not be called.
468 */
Brian Salomon26102cb2018-03-09 09:33:19 -0500469 virtual void deleteTestingOnlyBackendTexture(const GrBackendTexture&) = 0;
jvanverth672bb7f2015-07-13 07:19:57 -0700470
Brian Salomonf865b052018-03-09 09:01:53 -0500471 virtual GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h, GrColorType,
472 GrSRGBEncoded) = 0;
473
474 virtual void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) = 0;
475
476 // This is only to be used in GL-specific tests.
477 virtual const GrGLContext* glContextForTesting() const { return nullptr; }
478
479 // This is only to be used by testing code
480 virtual void resetShaderCacheForTesting() const {}
481
Greg Daniel26b50a42018-03-08 09:49:58 -0500482 /**
483 * Flushes all work to the gpu and forces the GPU to wait until all the gpu work has completed.
484 * This is for testing purposes only.
485 */
486 virtual void testingOnly_flushGpuAndSync() = 0;
Brian Salomonf865b052018-03-09 09:01:53 -0500487#endif
Greg Daniel26b50a42018-03-08 09:49:58 -0500488
egdanielec00d942015-09-14 12:56:10 -0700489 // width and height may be larger than rt (if underlying API allows it).
490 // Returns nullptr if compatible sb could not be created, otherwise the caller owns the ref on
491 // the GrStencilAttachment.
492 virtual GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
493 int width,
494 int height) = 0;
495 // clears target's entire stencil buffer to 0
Robert Phillips95214472017-08-08 18:00:03 -0400496 virtual void clearStencil(GrRenderTarget* target, int clearValue) = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800497
bsalomone179a912016-01-20 06:18:10 -0800498 // Determines whether a texture will need to be rescaled in order to be used with the
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400499 // GrSamplerState. This variation is called when the caller will create a new texture using the
Brian Osman32342f02017-03-04 08:12:46 -0500500 // resource provider from a non-texture src (cpu-backed image, ...).
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500501 static bool IsACopyNeededForTextureParams(const GrCaps*, int width, int height,
502 const GrSamplerState&, GrTextureProducer::CopyParams*,
503 SkScalar scaleAdjust[2]);
bsalomone179a912016-01-20 06:18:10 -0800504
505 // Like the above but this variation should be called when the caller is not creating the
506 // original texture but rather was handed the original texture. It adds additional checks
507 // relevant to original textures that were created external to Skia via
Brian Osman32342f02017-03-04 08:12:46 -0500508 // GrResourceProvider::wrap methods.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400509 bool isACopyNeededForTextureParams(GrTextureProxy* proxy, const GrSamplerState& params,
Robert Phillips81444fb2017-03-21 09:14:35 -0400510 GrTextureProducer::CopyParams* copyParams,
511 SkScalar scaleAdjust[2]) const {
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500512 if (IsACopyNeededForTextureParams(this->caps(), proxy->width(), proxy->height(), params,
513 copyParams, scaleAdjust)) {
bsalomone179a912016-01-20 06:18:10 -0800514 return true;
515 }
Robert Phillips3798c862017-03-27 11:08:16 -0400516 return this->onIsACopyNeededForTextureParams(proxy, params, copyParams, scaleAdjust);
bsalomone179a912016-01-20 06:18:10 -0800517 }
bsalomon045802d2015-10-20 07:58:01 -0700518
egdaniel9cb63402016-06-23 08:37:05 -0700519 void handleDirtyContext() {
520 if (fResetBits) {
521 this->resetContext();
522 }
523 }
524
joshualittd53a8272014-11-10 16:03:14 -0800525protected:
bsalomonf0674512015-07-28 13:26:15 -0700526 static void ElevateDrawPreference(GrGpu::DrawPreference* preference,
527 GrGpu::DrawPreference elevation) {
528 GR_STATIC_ASSERT(GrGpu::kCallerPrefersDraw_DrawPreference > GrGpu::kNoDraw_DrawPreference);
529 GR_STATIC_ASSERT(GrGpu::kGpuPrefersDraw_DrawPreference >
530 GrGpu::kCallerPrefersDraw_DrawPreference);
531 GR_STATIC_ASSERT(GrGpu::kRequireDraw_DrawPreference >
532 GrGpu::kGpuPrefersDraw_DrawPreference);
533 *preference = SkTMax(*preference, elevation);
534 }
535
Brian Salomon1fabd512018-02-09 09:54:25 -0500536 // Handles cases where a surface will be updated without a call to flushRenderTarget.
537 void didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
538 uint32_t mipLevels = 1) const;
jvanverth900bd4a2016-04-29 13:53:12 -0700539
Ben Wagner145dbcd2016-11-03 14:40:50 -0400540 Stats fStats;
541 std::unique_ptr<GrPathRendering> fPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800542 // Subclass must initialize this in its constructor.
Hal Canary144caf52016-11-07 17:57:18 -0500543 sk_sp<const GrCaps> fCaps;
joshualitt3322fa42014-11-07 08:48:51 -0800544
csmartdalton0d28e572016-07-06 09:59:43 -0700545 typedef SkTArray<SkPoint, true> SamplePattern;
546
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000547private:
bsalomon@google.comb635d392011-11-05 12:47:43 +0000548 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000549 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000550 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000551
bsalomoncb02b382015-08-12 11:14:50 -0700552 // Called before certain draws in order to guarantee coherent results from dst reads.
553 virtual void xferBarrier(GrRenderTarget*, GrXferBarrierType) = 0;
554
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000555 // overridden by backend-specific derived class to create objects.
egdanielb0e1be22015-04-22 13:27:39 -0700556 // Texture size and sample size will have already been validated in base class before
Robert Phillips92de6312017-05-23 07:43:48 -0400557 // onCreateTexture is called.
Robert Phillips16d8ec62017-07-27 16:16:25 -0400558 virtual sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc&, SkBudgeted,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500559 const GrMipLevel texels[], int mipLevelCount) = 0;
cblume55f2d2d2016-02-26 13:20:48 -0800560
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400561 virtual sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership) = 0;
Brian Salomond17f6582017-07-19 18:28:58 -0400562 virtual sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
Brian Salomond17f6582017-07-19 18:28:58 -0400563 int sampleCnt,
564 GrWrapOwnership) = 0;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400565 virtual sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) = 0;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000566 virtual sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400567 int sampleCnt) = 0;
cdalton1bf3e712016-04-19 10:00:02 -0700568 virtual GrBuffer* onCreateBuffer(size_t size, GrBufferType intendedType, GrAccessPattern,
569 const void* data) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000570
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400571 virtual bool onIsACopyNeededForTextureParams(GrTextureProxy* proxy, const GrSamplerState&,
Robert Phillips81444fb2017-03-21 09:14:35 -0400572 GrTextureProducer::CopyParams*,
573 SkScalar scaleAdjust[2]) const {
574 return false;
575 }
bsalomone179a912016-01-20 06:18:10 -0800576
Brian Salomon9b009bb2018-02-14 13:53:55 -0500577 virtual bool onGetReadPixelsInfo(GrSurface*, GrSurfaceOrigin, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500578 size_t rowBytes, GrColorType, DrawPreference*,
bsalomonf0674512015-07-28 13:26:15 -0700579 ReadPixelTempDrawInfo*) = 0;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500580 virtual bool onGetWritePixelsInfo(GrSurface*, GrSurfaceOrigin, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500581 GrColorType, DrawPreference*, WritePixelTempDrawInfo*) = 0;
bsalomonf0674512015-07-28 13:26:15 -0700582
bsalomon6cb3cbe2015-07-30 07:34:27 -0700583 // overridden by backend-specific derived class to perform the surface read
Brian Salomonc320b152018-02-20 14:05:36 -0500584 virtual bool onReadPixels(GrSurface*, GrSurfaceOrigin, int left, int top, int width, int height,
585 GrColorType, void* buffer, size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000586
bsalomon6cb3cbe2015-07-30 07:34:27 -0700587 // overridden by backend-specific derived class to perform the surface write
Brian Salomonc320b152018-02-20 14:05:36 -0500588 virtual bool onWritePixels(GrSurface*, GrSurfaceOrigin, int left, int top, int width,
589 int height, GrColorType, const GrMipLevel texels[],
590 int mipLevelCount) = 0;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000591
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400592 // overridden by backend-specific derived class to perform the texture transfer
Brian Salomonc320b152018-02-20 14:05:36 -0500593 virtual bool onTransferPixels(GrTexture*, int left, int top, int width, int height,
594 GrColorType colorType, GrBuffer* transferBuffer, size_t offset,
595 size_t rowBytes) = 0;
jvanverth17aa0472016-01-05 10:41:27 -0800596
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000597 // overridden by backend-specific derived class to perform the resolve
Brian Salomon1fabd512018-02-09 09:54:25 -0500598 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000599
joshualitt1cbdcde2015-08-21 11:53:29 -0700600 // overridden by backend specific derived class to perform the copy surface
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400601 virtual bool onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
602 GrSurface* src, GrSurfaceOrigin srcOrigin,
603 const SkIRect& srcRect, const SkIPoint& dstPoint) = 0;
joshualitt1cbdcde2015-08-21 11:53:29 -0700604
Greg Daniel51316782017-08-02 15:10:09 +0000605 virtual void onFinishFlush(bool insertedSemaphores) = 0;
606
Brian Osman71a18892017-08-10 10:23:25 -0400607 virtual void onDumpJSON(SkJSONWriter*) const {}
608
bsalomon@google.comb635d392011-11-05 12:47:43 +0000609 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 this->onResetContext(fResetBits);
611 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000612 ++fResetTimestamp;
613 }
614
Chris Dalton535ba8d2018-02-20 09:51:59 -0700615 ResetTimestamp fResetTimestamp;
616 uint32_t fResetBits;
joshualitt3322fa42014-11-07 08:48:51 -0800617 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
Chris Dalton535ba8d2018-02-20 09:51:59 -0700618 GrContext* fContext;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000619
kkinnunencabe20c2015-06-01 01:37:26 -0700620 friend class GrPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800621 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000622};
623
624#endif