blob: b6aa43c0393a4e8805684e6ff271355eb0657e04 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPath.h"
12#include "include/core/SkSurface.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/SkTArray.h"
Stephen Whitef3d5d442020-04-08 10:35:58 -040015#include "src/core/SkTInternalLList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrCaps.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040017#include "src/gpu/GrOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrSamplePatternDictionary.h"
Brian Salomon3ec1f542019-06-17 17:54:57 +000019#include "src/gpu/GrSwizzle.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrTextureProducer.h"
21#include "src/gpu/GrXferProcessor.h"
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;
Adlai Holler3d0359a2020-07-09 15:35:55 -040025class GrDirectContext;
Brian Salomondbf70722019-02-07 11:31:24 -050026class GrGpuBuffer;
robertphillips28a838e2016-06-23 14:07:00 -070027struct GrContextOptions;
reedf9ad5582015-06-25 21:29:25 -070028class GrGLContext;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000029class GrPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000030class GrPathRenderer;
31class GrPathRendererChain;
kkinnunencabe20c2015-06-01 01:37:26 -070032class GrPathRendering;
egdaniel8dd688b2015-01-22 10:16:09 -080033class GrPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -080034class GrPrimitiveProcessor;
kkinnunencabe20c2015-06-01 01:37:26 -070035class GrRenderTarget;
Jim Van Verth7e829b22020-07-30 17:04:35 -040036class GrRingBuffer;
Greg Daniel6be35232017-03-01 17:01:09 -050037class GrSemaphore;
Greg Daniela58db7f2020-07-15 09:17:59 -040038class GrStagingBufferManager;
Greg Danielc0d69152020-10-08 14:59:00 -040039class GrAttachment;
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:
Adlai Holler3d0359a2020-07-09 15:35:55 -040047 GrGpu(GrDirectContext* direct);
mtklein36352bf2015-03-25 18:17:31 -070048 ~GrGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000049
Adlai Holler3d0359a2020-07-09 15:35:55 -040050 GrDirectContext* getContext() { return fContext; }
51 const GrDirectContext* getContext() const { return fContext; }
joshualitt3322fa42014-11-07 08:48:51 -080052
53 /**
54 * Gets the capabilities of the draw target.
55 */
bsalomon4b91f762015-05-19 09:29:46 -070056 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipse42edcc2017-12-13 11:50:22 -050057 sk_sp<const GrCaps> refCaps() const { return fCaps; }
joshualitt3322fa42014-11-07 08:48:51 -080058
kkinnunencabe20c2015-06-01 01:37:26 -070059 GrPathRendering* pathRendering() { return fPathRendering.get(); }
kkinnunenccdaa042014-08-20 01:36:23 -070060
Greg Daniela58db7f2020-07-15 09:17:59 -040061 virtual GrStagingBufferManager* stagingBufferManager() { return nullptr; }
62
Jim Van Verth7e829b22020-07-30 17:04:35 -040063 virtual GrRingBuffer* uniformsRingBuffer() { return nullptr; }
64
bsalomon6e2aad42016-04-01 11:54:31 -070065 enum class DisconnectType {
66 // No cleanup should be attempted, immediately cease making backend API calls
67 kAbandon,
68 // Free allocated resources (not known by GrResourceCache) before returning and
69 // ensure no backend backend 3D API calls will be made after disconnect() returns.
70 kCleanup,
71 };
72
Adlai Holler3d0359a2020-07-09 15:35:55 -040073 // Called by context when the underlying backend context is already or will be destroyed
74 // before GrDirectContext.
bsalomon6e2aad42016-04-01 11:54:31 -070075 virtual void disconnect(DisconnectType);
bsalomonc8dc1f72014-08-21 13:02:13 -070076
Adlai Holler3d0359a2020-07-09 15:35:55 -040077 // Called by GrDirectContext::isContextLost. Returns true if the backend Gpu object has gotten
78 // into an unrecoverable, lost state.
Greg Daniel6e35a002020-04-01 13:29:59 -040079 virtual bool isDeviceLost() const { return false; }
80
reed@google.comac10a2d2010-12-22 21:39:39 +000081 /**
82 * The GrGpu object normally assumes that no outsider is setting state
83 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000084 * the GrGpu that the state was modified and it shouldn't make assumptions
85 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000086 */
mtkleinb9eb4ac2015-02-02 18:26:03 -080087 void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
reed@google.comac10a2d2010-12-22 21:39:39 +000088
89 /**
Brian Salomonf2c2ba92019-07-17 09:59:59 -040090 * Creates a texture object. If renderable is kYes then the returned texture can
bsalomon6d467ec2014-11-18 07:36:19 -080091 * be used as a render target by calling GrTexture::asRenderTarget(). Not all
92 * pixel configs can be used as render targets. Support for configs as textures
bsalomon4b91f762015-05-19 09:29:46 -070093 * or render targets can be checked using GrCaps.
bsalomon@google.com1da07462011-03-10 14:51:57 +000094 *
Brian Salomona56a7462020-02-07 14:17:25 -050095 * @param dimensions dimensions of the texture to be created.
Brian Salomon4eb38b72019-08-05 12:58:39 -040096 * @param format the format for the texture (not currently used).
Brian Salomonf2c2ba92019-07-17 09:59:59 -040097 * @param renderable should the resulting texture be renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -040098 * @param renderTargetSampleCnt The number of samples to use for rendering if renderable is
99 * kYes. If renderable is kNo then this must be 1.
Brian Salomon58389b92018-03-07 13:01:25 -0500100 * @param budgeted does this texture count against the resource cache budget?
Brian Salomone8a766b2019-07-19 14:24:36 -0400101 * @param isProtected should the texture be created as protected.
Brian Salomon58389b92018-03-07 13:01:25 -0500102 * @param texels array of mipmap levels containing texel data to load.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400103 * If level i has pixels then it is assumed that its dimensions are
Brian Salomona56a7462020-02-07 14:17:25 -0500104 * max(1, floor(dimensions.fWidth / 2)) by
105 * max(1, floor(dimensions.fHeight / 2)).
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400106 * If texels[i].fPixels == nullptr for all i <= mipLevelCount or
107 * mipLevelCount is 0 then the texture's contents are uninitialized.
108 * If a level has non-null pixels, its row bytes must be a multiple of the
109 * config's bytes-per-pixel. The row bytes must be tight to the
110 * level width if !caps->writePixelsRowBytesSupport().
111 * If mipLevelCount > 1 and texels[i].fPixels != nullptr for any i > 0
112 * then all levels must have non-null pixels. All levels must have
113 * non-null pixels if GrCaps::createTextureMustSpecifyAllLevels() is true.
Brian Salomona90382f2019-09-17 09:01:56 -0400114 * @param textureColorType The color type interpretation of the texture for the purpose of
115 * of uploading texel data.
116 * @param srcColorType The color type of data in texels[].
Brian Salomond2a8ae22019-09-10 16:03:59 -0400117 * @param texelLevelCount the number of levels in 'texels'. May be 0, 1, or
Brian Salomona56a7462020-02-07 14:17:25 -0500118 * floor(max((log2(dimensions.fWidth), log2(dimensions.fHeight)))). It
119 * must be the latter if GrCaps::createTextureMustSpecifyAllLevels() is
120 * true.
Brian Salomon58389b92018-03-07 13:01:25 -0500121 * @return The texture object if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 */
Brian Salomona56a7462020-02-07 14:17:25 -0500123 sk_sp<GrTexture> createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400124 const GrBackendFormat& format,
125 GrRenderable renderable,
126 int renderTargetSampleCnt,
Robert Phillips3a833922020-01-21 15:25:58 -0500127 SkBudgeted budgeted,
Brian Salomona90382f2019-09-17 09:01:56 -0400128 GrProtected isProtected,
129 GrColorType textureColorType,
130 GrColorType srcColorType,
131 const GrMipLevel texels[],
Brian Salomond2a8ae22019-09-10 16:03:59 -0400132 int texelLevelCount);
cblume55f2d2d2016-02-26 13:20:48 -0800133
Brian Salomonc7dced52019-07-18 15:02:01 +0000134 /**
135 * Simplified createTexture() interface for when there is no initial texel data to upload.
136 */
Brian Salomona56a7462020-02-07 14:17:25 -0500137 sk_sp<GrTexture> createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400138 const GrBackendFormat& format,
139 GrRenderable renderable,
140 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400141 GrMipmapped mipMapped,
Brian Salomona90382f2019-09-17 09:01:56 -0400142 SkBudgeted budgeted,
143 GrProtected isProtected);
Brian Salomonc7dced52019-07-18 15:02:01 +0000144
Robert Phillips3a833922020-01-21 15:25:58 -0500145 sk_sp<GrTexture> createCompressedTexture(SkISize dimensions,
146 const GrBackendFormat& format,
147 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400148 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500149 GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500150 const void* data, size_t dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400151
bsalomon@google.come269f212011-11-07 13:29:52 +0000152 /**
Brian Osman32342f02017-03-04 08:12:46 -0500153 * Implements GrResourceProvider::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000154 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400155 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture&,
156 GrWrapOwnership,
157 GrWrapCacheable,
158 GrIOType);
Brian Salomond17f6582017-07-19 18:28:58 -0400159
Robert Phillipsb915c942019-12-17 14:44:37 -0500160 sk_sp<GrTexture> wrapCompressedBackendTexture(const GrBackendTexture&,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400161 GrWrapOwnership,
162 GrWrapCacheable);
Robert Phillipsb915c942019-12-17 14:44:37 -0500163
Brian Salomond17f6582017-07-19 18:28:58 -0400164 /**
165 * Implements GrResourceProvider::wrapRenderableBackendTexture
166 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400167 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture&,
168 int sampleCnt,
169 GrWrapOwnership,
170 GrWrapCacheable);
bsalomon@google.come269f212011-11-07 13:29:52 +0000171
172 /**
Brian Osman32342f02017-03-04 08:12:46 -0500173 * Implements GrResourceProvider::wrapBackendRenderTarget
bsalomon@google.come269f212011-11-07 13:29:52 +0000174 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400175 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000176
177 /**
Brian Osman32342f02017-03-04 08:12:46 -0500178 * Implements GrResourceProvider::wrapBackendTextureAsRenderTarget
ericrkf7b8b8a2016-02-24 14:49:51 -0800179 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400180 sk_sp<GrRenderTarget> wrapBackendTextureAsRenderTarget(const GrBackendTexture&, int sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800181
182 /**
Greg Danielb46add82019-01-02 14:51:29 -0500183 * Implements GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget
184 */
185 sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
186 const GrVkDrawableInfo&);
187
188 /**
csmartdalton485a1202016-07-13 10:16:32 -0700189 * Creates a buffer in GPU memory. For a client-side buffer use GrBuffer::CreateCPUBacked.
reed@google.comac10a2d2010-12-22 21:39:39 +0000190 *
cdaltone2e71c22016-04-07 18:13:29 -0700191 * @param size size of buffer to create.
192 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
193 * @param accessPattern hint to the graphics subsystem about how the data will be accessed.
cdalton1bf3e712016-04-19 10:00:02 -0700194 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700195 *
cdalton397536c2016-03-25 12:15:03 -0700196 * @return the buffer if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000197 */
Brian Salomondbf70722019-02-07 11:31:24 -0500198 sk_sp<GrGpuBuffer> createBuffer(size_t size, GrGpuBufferType intendedType,
199 GrAccessPattern accessPattern, const void* data = nullptr);
halcanary9d524f22016-03-29 09:03:52 -0700200
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 /**
Greg Daniel242536f2020-02-13 14:12:46 -0500202 * Resolves MSAA. The resolveRect must already be in the native destination space.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000203 */
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400204 void resolveRenderTarget(GrRenderTarget*, const SkIRect& resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000205
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000206 /**
Brian Salomon930f9392018-06-20 16:25:26 -0400207 * Uses the base of the texture to recompute the contents of the other levels.
208 */
209 bool regenerateMipMapLevels(GrTexture*);
210
211 /**
Brian Salomon1f05d452019-02-08 12:33:08 -0500212 * If the backend API has stateful texture bindings, this resets them back to defaults.
213 */
214 void resetTextureBindings();
215
216 /**
Brian Salomon9b009bb2018-02-14 13:53:55 -0500217 * Reads a rectangle of pixels from a render target. No sRGB/linear conversions are performed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000218 *
Brian Salomonf77c1462019-08-01 15:19:29 -0400219 * @param surface The surface to read from
220 * @param left left edge of the rectangle to read (inclusive)
221 * @param top top edge of the rectangle to read (inclusive)
222 * @param width width of rectangle to read in pixels.
223 * @param height height of rectangle to read in pixels.
224 * @param surfaceColorType the color type for this use of the surface.
225 * @param dstColorType the color type of the destination buffer.
226 * @param buffer memory to read the rectangle into.
227 * @param rowBytes the number of bytes between consecutive rows. Must be a multiple of
228 * dstColorType's bytes-per-pixel. Must be tight to width if
229 * !caps->readPixelsRowBytesSupport().
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230 *
231 * @return true if the read succeeded, false if not. The read can fail
Brian Salomon1d435302019-07-01 13:05:28 -0400232 * because of the surface doesn't support reading, the color type
233 * is not allowed for the format of the surface or if the rectangle
234 * read is not contained in the surface.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000235 */
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400236 bool readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400237 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
238 size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
bsalomon@google.com6f379512011-11-16 20:36:03 +0000240 /**
Brian Salomon9b009bb2018-02-14 13:53:55 -0500241 * Updates the pixels in a rectangle of a surface. No sRGB/linear conversions are performed.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000242 *
Greg Danielb20d7e52019-09-03 13:54:39 -0400243 * @param surface The surface to write to.
244 * @param left left edge of the rectangle to write (inclusive)
245 * @param top top edge of the rectangle to write (inclusive)
246 * @param width width of rectangle to write in pixels.
247 * @param height height of rectangle to write in pixels.
248 * @param surfaceColorType the color type for this use of the surface.
249 * @param srcColorType the color type of the source buffer.
250 * @param texels array of mipmap levels containing texture data. Row bytes must be a
251 * multiple of srcColorType's bytes-per-pixel. Must be tight to level
252 * width if !caps->writePixelsRowBytesSupport().
253 * @param mipLevelCount number of levels in 'texels'
254 * @param prepForTexSampling After doing write pixels should the surface be prepared for texture
255 * sampling. This is currently only used by Vulkan for inline uploads
256 * to set that layout back to sampled after doing the upload. Inline
257 * uploads currently can happen between draws in a single op so it is
258 * not trivial to break up the GrOpsTask into two tasks when we see
259 * an inline upload. However, once we are able to support doing that
260 * we can remove this parameter.
Brian Salomon1d435302019-07-01 13:05:28 -0400261 *
262 * @return true if the write succeeded, false if not. The read can fail
263 * because of the surface doesn't support writing (e.g. read only),
264 * the color type is not allowed for the format of the surface or
265 * if the rectangle written is not contained in the surface.
cblume55f2d2d2016-02-26 13:20:48 -0800266 */
Brian Salomona9b04b92018-06-01 15:04:28 -0400267 bool writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400268 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400269 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling = false);
cblume55f2d2d2016-02-26 13:20:48 -0800270
271 /**
Brian Salomona9b04b92018-06-01 15:04:28 -0400272 * Helper for the case of a single level.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500273 */
274 bool writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400275 GrColorType surfaceColorType, GrColorType srcColorType, const void* buffer,
Greg Danielb20d7e52019-09-03 13:54:39 -0400276 size_t rowBytes, bool prepForTexSampling = false) {
Brian Salomona9b04b92018-06-01 15:04:28 -0400277 GrMipLevel mipLevel = {buffer, rowBytes};
Brian Salomonf77c1462019-08-01 15:19:29 -0400278 return this->writePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400279 &mipLevel, 1, prepForTexSampling);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500280 }
281
282 /**
Brian Salomone05ba5a2019-04-08 11:59:07 -0400283 * Updates the pixels in a rectangle of a texture using a buffer. If the texture is MIP mapped,
284 * the base level is written to.
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400285 *
286 * @param texture The texture to write to.
cdalton397536c2016-03-25 12:15:03 -0700287 * @param left left edge of the rectangle to write (inclusive)
288 * @param top top edge of the rectangle to write (inclusive)
289 * @param width width of rectangle to write in pixels.
290 * @param height height of rectangle to write in pixels.
Brian Salomonf77c1462019-08-01 15:19:29 -0400291 * @param textureColorType the color type for this use of the surface.
Brian Salomonc320b152018-02-20 14:05:36 -0500292 * @param bufferColorType the color type of the transfer buffer's pixel data
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400293 * @param transferBuffer GrBuffer to read pixels from (type must be "kXferCpuToGpu")
cdalton397536c2016-03-25 12:15:03 -0700294 * @param offset offset from the start of the buffer
Brian Salomon1047a492019-07-02 12:25:21 -0400295 * @param rowBytes number of bytes between consecutive rows in the buffer. Must be a
296 * multiple of bufferColorType's bytes-per-pixel. Must be tight to width
297 * if !caps->writePixelsRowBytesSupport().
jvanverth17aa0472016-01-05 10:41:27 -0800298 */
Brian Salomone05ba5a2019-04-08 11:59:07 -0400299 bool transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400300 GrColorType textureColorType, GrColorType bufferColorType,
301 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400302
303 /**
304 * Reads the pixels from a rectangle of a surface into a buffer. Use
Greg Danielba88ab62019-07-26 09:14:01 -0400305 * GrCaps::SupportedRead::fOffsetAlignmentForTransferBuffer to determine the requirements for
306 * the buffer offset alignment. If the surface is a MIP mapped texture, the base level is read.
Brian Salomone05ba5a2019-04-08 11:59:07 -0400307 *
Brian Salomon26de56e2019-04-10 12:14:26 -0400308 * If successful the row bytes in the buffer is always:
309 * GrColorTypeBytesPerPixel(bufferColorType) * width
310 *
311 * Asserts that the caller has passed a properly aligned offset and that the buffer is
312 * large enough to hold the result
Brian Salomone05ba5a2019-04-08 11:59:07 -0400313 *
314 * @param surface The surface to read from.
315 * @param left left edge of the rectangle to read (inclusive)
316 * @param top top edge of the rectangle to read (inclusive)
317 * @param width width of rectangle to read in pixels.
318 * @param height height of rectangle to read in pixels.
Brian Salomonf77c1462019-08-01 15:19:29 -0400319 * @param surfaceColorType the color type for this use of the surface.
Brian Salomone05ba5a2019-04-08 11:59:07 -0400320 * @param bufferColorType the color type of the transfer buffer's pixel data
321 * @param transferBuffer GrBuffer to write pixels to (type must be "kXferGpuToCpu")
322 * @param offset offset from the start of the buffer
323 */
Brian Salomon26de56e2019-04-10 12:14:26 -0400324 bool transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400325 GrColorType surfaceColorType, GrColorType bufferColorType,
326 GrGpuBuffer* transferBuffer, size_t offset);
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000327
bsalomonf90a02b2014-11-26 12:28:00 -0800328 // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst
Greg Danielf41b2bd2019-08-22 16:19:24 -0400329 // take place at higher levels and this function implement faster copy paths. The rect
bsalomonf90a02b2014-11-26 12:28:00 -0800330 // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the
Greg Daniel46cfbc62019-06-07 11:43:30 -0400331 // src/dst bounds and non-empty. They must also be in their exact device space coords, including
332 // already being transformed for origin if need be. If canDiscardOutsideDstRect is set to true
333 // then we don't need to preserve any data on the dst surface outside of the copy.
334 bool copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400335 const SkIPoint& dstPoint);
joshualitt3322fa42014-11-07 08:48:51 -0800336
Chris Daltond7291ba2019-03-07 14:17:03 -0700337 // Queries the per-pixel HW sample locations for the given render target, and then finds or
338 // assigns a key that uniquely identifies the sample pattern. The actual sample locations can be
339 // retrieved with retrieveSampleLocations().
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600340 int findOrAssignSamplePatternKey(GrRenderTarget*);
Chris Daltond7291ba2019-03-07 14:17:03 -0700341
342 // Retrieves the per-pixel HW sample locations for the given sample pattern key, and, as a
343 // by-product, the actual number of samples in use. (This may differ from the number of samples
344 // requested by the render target.) Sample locations are returned as 0..1 offsets relative to
345 // the top-left corner of the pixel.
346 const SkTArray<SkPoint>& retrieveSampleLocations(int samplePatternKey) const {
347 return fSamplePatternDictionary.retrieveSampleLocations(samplePatternKey);
348 }
349
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400350 // Returns a GrOpsRenderPass which GrOpsTasks send draw commands to instead of directly
351 // to the Gpu object. The 'bounds' rect is the content rect of the renderTarget.
Robert Phillips96f22372020-05-20 12:31:18 -0400352 // If a 'stencil' is provided it will be the one bound to 'renderTarget'. If one is not
353 // provided but 'renderTarget' has a stencil buffer then that is a signal that the
354 // render target's stencil buffer should be ignored.
Greg Danielc0d69152020-10-08 14:59:00 -0400355 virtual GrOpsRenderPass* getOpsRenderPass(GrRenderTarget* renderTarget,
356 GrAttachment* stencil,
357 GrSurfaceOrigin,
358 const SkIRect& bounds,
359 const GrOpsRenderPass::LoadAndStoreInfo&,
360 const GrOpsRenderPass::StencilLoadAndStoreInfo&,
361 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
362 GrXferBarrierFlags renderPassXferBarriers) = 0;
bsalomon3e791242014-12-17 13:43:13 -0800363
Robert Phillips18e94842017-05-15 13:06:44 -0400364 // Called by GrDrawingManager when flushing.
Greg Daniel51316782017-08-02 15:10:09 +0000365 // Provides a hook for post-flush actions (e.g. Vulkan command buffer submits). This will also
366 // insert any numSemaphore semaphores on the gpu and set the backendSemaphores to match the
367 // inserted semaphores.
Greg Danielfe159622020-04-10 17:43:51 +0000368 void executeFlushInfo(GrSurfaceProxy*[], int numProxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400369 SkSurface::BackendSurfaceAccess access,
370 const GrFlushInfo&,
371 const GrBackendSurfaceMutableState* newState);
Greg Danielfe159622020-04-10 17:43:51 +0000372
373 bool submitToGpu(bool syncCpu);
ethannicholas22793252016-01-30 09:59:10 -0800374
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400375 virtual void submit(GrOpsRenderPass*) = 0;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400376
Greg Daniel6be35232017-03-01 17:01:09 -0500377 virtual GrFence SK_WARN_UNUSED_RESULT insertFence() = 0;
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400378 virtual bool waitFence(GrFence) = 0;
jvanverth84741b32016-09-30 08:39:02 -0700379 virtual void deleteFence(GrFence) const = 0;
380
Greg Daniel301015c2019-11-18 14:06:46 -0500381 virtual std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(
382 bool isOwned = true) = 0;
383 virtual std::unique_ptr<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
384 GrResourceProvider::SemaphoreWrapType wrapType, GrWrapOwnership ownership) = 0;
385 virtual void insertSemaphore(GrSemaphore* semaphore) = 0;
386 virtual void waitSemaphore(GrSemaphore* semaphore) = 0;
Greg Daniel6be35232017-03-01 17:01:09 -0500387
Jim Van Verth1aaf41b2020-07-29 09:24:29 -0400388 virtual void addFinishedProc(GrGpuFinishedProc finishedProc,
389 GrGpuFinishedContext finishedContext) = 0;
Brian Salomonb0d8b762019-05-06 16:58:22 -0400390 virtual void checkFinishProcs() = 0;
391
Jim Van Verth1aaf41b2020-07-29 09:24:29 -0400392 virtual void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) {}
Greg Daniela58db7f2020-07-15 09:17:59 -0400393
Brian Osman13dddce2017-05-09 13:19:50 -0400394 /**
Brian Salomon24069eb2020-06-24 10:19:52 -0400395 * Checks if we detected an OOM from the underlying 3D API and if so returns true and resets
396 * the internal OOM state to false. Otherwise, returns false.
397 */
398 bool checkAndResetOOMed();
399
400 /**
Adlai Holler3d0359a2020-07-09 15:35:55 -0400401 * Put this texture in a safe and known state for use across multiple contexts. Depending on
Brian Osman13dddce2017-05-09 13:19:50 -0400402 * the backend, this may return a GrSemaphore. If so, other contexts should wait on that
403 * semaphore before using this texture.
404 */
Greg Daniel301015c2019-11-18 14:06:46 -0500405 virtual std::unique_ptr<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) = 0;
Brian Osman13dddce2017-05-09 13:19:50 -0400406
mtkleinb9eb4ac2015-02-02 18:26:03 -0800407 ///////////////////////////////////////////////////////////////////////////
408 // Debugging and Stats
409
410 class Stats {
411 public:
Robert Phillips19f466d2020-02-26 10:27:07 -0500412 enum class ProgramCacheResult {
413 kHit, // the program was found in the cache
414 kMiss, // the program was not found in the cache (and was, thus, compiled)
415 kPartial, // a precompiled version was found in the persistent cache
416
417 kLast = kPartial
418 };
419
420 static const int kNumProgramCacheResults = (int)ProgramCacheResult::kLast + 1;
421
mtkleinb9eb4ac2015-02-02 18:26:03 -0800422#if GR_GPU_STATS
Brian Salomone05ba5a2019-04-08 11:59:07 -0400423 Stats() = default;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800424
Brian Salomone05ba5a2019-04-08 11:59:07 -0400425 void reset() { *this = {}; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800426
427 int renderTargetBinds() const { return fRenderTargetBinds; }
428 void incRenderTargetBinds() { fRenderTargetBinds++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400429
mtkleinb9eb4ac2015-02-02 18:26:03 -0800430 int shaderCompilations() const { return fShaderCompilations; }
431 void incShaderCompilations() { fShaderCompilations++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400432
bsalomonb12ea412015-02-02 21:19:50 -0800433 int textureCreates() const { return fTextureCreates; }
434 void incTextureCreates() { fTextureCreates++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400435
bsalomonb12ea412015-02-02 21:19:50 -0800436 int textureUploads() const { return fTextureUploads; }
437 void incTextureUploads() { fTextureUploads++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400438
jvanverth17aa0472016-01-05 10:41:27 -0800439 int transfersToTexture() const { return fTransfersToTexture; }
440 void incTransfersToTexture() { fTransfersToTexture++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400441
442 int transfersFromSurface() const { return fTransfersFromSurface; }
Brian Salomone05ba5a2019-04-08 11:59:07 -0400443 void incTransfersFromSurface() { fTransfersFromSurface++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400444
445 int stencilAttachmentCreates() const { return fStencilAttachmentCreates; }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700446 void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400447
448 int numDraws() const { return fNumDraws; }
joshualitt87a5c9f2015-09-08 13:42:05 -0700449 void incNumDraws() { fNumDraws++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400450
451 int numFailedDraws() const { return fNumFailedDraws; }
bsalomon1d417a82016-03-23 11:50:26 -0700452 void incNumFailedDraws() { ++fNumFailedDraws; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400453
Greg Danielfe159622020-04-10 17:43:51 +0000454 int numSubmitToGpus() const { return fNumSubmitToGpus; }
455 void incNumSubmitToGpus() { ++fNumSubmitToGpus; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400456
457 int numScratchTexturesReused() const { return fNumScratchTexturesReused; }
458 void incNumScratchTexturesReused() { ++fNumScratchTexturesReused; }
459
Robert Phillips19f466d2020-02-26 10:27:07 -0500460 int numInlineCompilationFailures() const { return fNumInlineCompilationFailures; }
461 void incNumInlineCompilationFailures() { ++fNumInlineCompilationFailures; }
462
463 int numInlineProgramCacheResult(ProgramCacheResult stat) const {
464 return fInlineProgramCacheStats[(int) stat];
465 }
466 void incNumInlineProgramCacheResult(ProgramCacheResult stat) {
467 ++fInlineProgramCacheStats[(int) stat];
468 }
469
470 int numPreCompilationFailures() const { return fNumPreCompilationFailures; }
471 void incNumPreCompilationFailures() { ++fNumPreCompilationFailures; }
472
473 int numPreProgramCacheResult(ProgramCacheResult stat) const {
474 return fPreProgramCacheStats[(int) stat];
475 }
476 void incNumPreProgramCacheResult(ProgramCacheResult stat) {
477 ++fPreProgramCacheStats[(int) stat];
478 }
479
480 int numCompilationFailures() const { return fNumCompilationFailures; }
481 void incNumCompilationFailures() { ++fNumCompilationFailures; }
482
483 int numPartialCompilationSuccesses() const { return fNumPartialCompilationSuccesses; }
484 void incNumPartialCompilationSuccesses() { ++fNumPartialCompilationSuccesses; }
485
486 int numCompilationSuccesses() const { return fNumCompilationSuccesses; }
487 void incNumCompilationSuccesses() { ++fNumCompilationSuccesses; }
488
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500489#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800490 void dump(SkString*);
joshualitte45c81c2015-12-02 09:05:37 -0800491 void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500492#endif
mtkleinb9eb4ac2015-02-02 18:26:03 -0800493 private:
Brian Salomone05ba5a2019-04-08 11:59:07 -0400494 int fRenderTargetBinds = 0;
495 int fShaderCompilations = 0;
496 int fTextureCreates = 0;
497 int fTextureUploads = 0;
498 int fTransfersToTexture = 0;
499 int fTransfersFromSurface = 0;
500 int fStencilAttachmentCreates = 0;
501 int fNumDraws = 0;
502 int fNumFailedDraws = 0;
Greg Danielfe159622020-04-10 17:43:51 +0000503 int fNumSubmitToGpus = 0;
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400504 int fNumScratchTexturesReused = 0;
Robert Phillips19f466d2020-02-26 10:27:07 -0500505
506 int fNumInlineCompilationFailures = 0;
507 int fInlineProgramCacheStats[kNumProgramCacheResults] = { 0 };
508
509 int fNumPreCompilationFailures = 0;
510 int fPreProgramCacheStats[kNumProgramCacheResults] = { 0 };
511
512 int fNumCompilationFailures = 0;
513 int fNumPartialCompilationSuccesses = 0;
514 int fNumCompilationSuccesses = 0;
515
mtkleinb9eb4ac2015-02-02 18:26:03 -0800516#else
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500517
518#if GR_TEST_UTILS
joshualitte45c81c2015-12-02 09:05:37 -0800519 void dump(SkString*) {}
520 void dumpKeyValuePairs(SkTArray<SkString>*, SkTArray<double>*) {}
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500521#endif
mtkleinb9eb4ac2015-02-02 18:26:03 -0800522 void incRenderTargetBinds() {}
523 void incShaderCompilations() {}
bsalomonb12ea412015-02-02 21:19:50 -0800524 void incTextureCreates() {}
525 void incTextureUploads() {}
jvanverth17aa0472016-01-05 10:41:27 -0800526 void incTransfersToTexture() {}
Greg Daniela28ea672020-09-25 11:12:56 -0400527 void incTransfersFromSurface() {}
egdaniel8dc7c3a2015-04-16 11:22:42 -0700528 void incStencilAttachmentCreates() {}
joshualitt87a5c9f2015-09-08 13:42:05 -0700529 void incNumDraws() {}
bsalomon1d417a82016-03-23 11:50:26 -0700530 void incNumFailedDraws() {}
Greg Danielfe159622020-04-10 17:43:51 +0000531 void incNumSubmitToGpus() {}
Greg Daniela28ea672020-09-25 11:12:56 -0400532 void incNumScratchTexturesReused() {}
Robert Phillips19f466d2020-02-26 10:27:07 -0500533 void incNumInlineCompilationFailures() {}
534 void incNumInlineProgramCacheResult(ProgramCacheResult stat) {}
535 void incNumPreCompilationFailures() {}
536 void incNumPreProgramCacheResult(ProgramCacheResult stat) {}
537 void incNumCompilationFailures() {}
538 void incNumPartialCompilationSuccesses() {}
539 void incNumCompilationSuccesses() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800540#endif
541 };
542
543 Stats* stats() { return &fStats; }
Brian Osman71a18892017-08-10 10:23:25 -0400544 void dumpJSON(SkJSONWriter*) const;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800545
Robert Phillips42716d42019-12-16 12:19:54 -0500546 /** Used to initialize a backend texture with either a constant color, pixmaps or
547 * compressed data.
548 */
Brian Salomon85c3d682019-11-04 15:04:54 -0500549 class BackendTextureData {
550 public:
Robert Phillips42716d42019-12-16 12:19:54 -0500551 enum class Type { kColor, kPixmaps, kCompressed };
Brian Salomon85c3d682019-11-04 15:04:54 -0500552 BackendTextureData() = default;
553 BackendTextureData(const SkColor4f& color) : fType(Type::kColor), fColor(color) {}
554 BackendTextureData(const SkPixmap pixmaps[]) : fType(Type::kPixmaps), fPixmaps(pixmaps) {
555 SkASSERT(pixmaps);
556 }
Robert Phillips42716d42019-12-16 12:19:54 -0500557 BackendTextureData(const void* data, size_t size) : fType(Type::kCompressed) {
558 SkASSERT(data);
559 fCompressed.fData = data;
560 fCompressed.fSize = size;
561 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500562
563 Type type() const { return fType; }
564 SkColor4f color() const {
565 SkASSERT(this->type() == Type::kColor);
566 return fColor;
567 }
568
Robert Phillips42716d42019-12-16 12:19:54 -0500569 const SkPixmap& pixmap(int i) const {
570 SkASSERT(this->type() == Type::kPixmaps);
571 return fPixmaps[i];
572 }
573 const SkPixmap* pixmaps() const {
574 SkASSERT(this->type() == Type::kPixmaps);
575 return fPixmaps;
576 }
577
578 const void* compressedData() const {
579 SkASSERT(this->type() == Type::kCompressed);
580 return fCompressed.fData;
581 }
582 size_t compressedSize() const {
583 SkASSERT(this->type() == Type::kCompressed);
584 return fCompressed.fSize;
585 }
586
Brian Salomon85c3d682019-11-04 15:04:54 -0500587
588 private:
589 Type fType = Type::kColor;
590 union {
591 SkColor4f fColor = {0, 0, 0, 0};
592 const SkPixmap* fPixmaps;
Robert Phillips42716d42019-12-16 12:19:54 -0500593 struct {
594 const void* fData;
595 size_t fSize;
596 } fCompressed;
Brian Salomon85c3d682019-11-04 15:04:54 -0500597 };
598 };
599
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400600 /**
601 * Creates a texture directly in the backend API without wrapping it in a GrTexture.
602 * Must be matched with a call to deleteBackendTexture().
Robert Phillips57ef6802019-09-23 10:12:47 -0400603 *
Brian Salomon85c3d682019-11-04 15:04:54 -0500604 * If data is null the texture is uninitialized.
Robert Phillips57ef6802019-09-23 10:12:47 -0400605 *
Brian Salomon85c3d682019-11-04 15:04:54 -0500606 * If data represents a color then all texture levels are cleared to that color.
607 *
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500608 * If data represents pixmaps then it must have a either one pixmap or, if mipmapping
609 * is specified, a complete MIP hierarchy of pixmaps. Additionally, if provided, the mip
610 * levels must be sized correctly according to the MIP sizes implied by dimensions. They
611 * must all have the same color type and that color type must be compatible with the
612 * texture format.
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400613 */
Brian Salomon85c3d682019-11-04 15:04:54 -0500614 GrBackendTexture createBackendTexture(SkISize dimensions,
615 const GrBackendFormat&,
616 GrRenderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400617 GrMipmapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400618 GrProtected);
619
620 bool updateBackendTexture(const GrBackendTexture&,
Greg Daniel25597782020-06-11 13:15:08 -0400621 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400622 const BackendTextureData*);
Robert Phillipsb915c942019-12-17 14:44:37 -0500623
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500624 /**
625 * Same as the createBackendTexture case except compressed backend textures can
626 * never be renderable.
627 */
Robert Phillipsb915c942019-12-17 14:44:37 -0500628 GrBackendTexture createCompressedBackendTexture(SkISize dimensions,
629 const GrBackendFormat&,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400630 GrMipmapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400631 GrProtected);
632
633 bool updateCompressedBackendTexture(const GrBackendTexture&,
634 sk_sp<GrRefCntedCallback> finishedCallback,
635 const BackendTextureData*);
Robert Phillips646f6372018-09-25 09:31:10 -0400636
Greg Daniel1db8e792020-06-09 17:29:32 -0400637 virtual bool setBackendTextureState(const GrBackendTexture&,
638 const GrBackendSurfaceMutableState&,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400639 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400640 sk_sp<GrRefCntedCallback> finishedCallback) {
641 return false;
642 }
643
644 virtual bool setBackendRenderTargetState(const GrBackendRenderTarget&,
645 const GrBackendSurfaceMutableState&,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400646 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400647 sk_sp<GrRefCntedCallback> finishedCallback) {
648 return false;
649 }
650
Brian Salomone64b0642018-03-07 11:47:54 -0500651 /**
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400652 * Frees a texture created by createBackendTexture(). If ownership of the backend
Adlai Holler3d0359a2020-07-09 15:35:55 -0400653 * texture has been transferred to a context using adopt semantics this should not be called.
Brian Salomone64b0642018-03-07 11:47:54 -0500654 */
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400655 virtual void deleteBackendTexture(const GrBackendTexture&) = 0;
jvanverth672bb7f2015-07-13 07:19:57 -0700656
Robert Phillips979b2232020-02-20 10:47:29 -0500657 /**
658 * In this case we have a program descriptor and a program info but no render target.
659 */
660 virtual bool compile(const GrProgramDesc&, const GrProgramInfo&) = 0;
661
Brian Osmaned58e002019-09-06 14:42:43 -0400662 virtual bool precompileShader(const SkData& key, const SkData& data) { return false; }
663
Robert Phillipsf0ced622019-05-16 09:06:25 -0400664#if GR_TEST_UTILS
665 /** Check a handle represents an actual texture in the backend API that has not been freed. */
666 virtual bool isTestingOnlyBackendTexture(const GrBackendTexture&) const = 0;
667
Brian Salomon72c7b982020-10-06 10:07:38 -0400668 /**
669 * Creates a GrBackendRenderTarget that can be wrapped using
670 * SkSurface::MakeFromBackendRenderTarget. Ideally this is a non-textureable allocation to
671 * differentiate from testing with SkSurface::MakeFromBackendTexture. When sampleCnt > 1 this
672 * is used to test client wrapped allocations with MSAA where Skia does not allocate a separate
673 * buffer for resolving. If the color is non-null the backing store should be cleared to the
674 * passed in color.
675 */
Brian Salomonf9b00422020-10-08 16:00:14 -0400676 virtual GrBackendRenderTarget createTestingOnlyBackendRenderTarget(
677 SkISize dimensions,
678 GrColorType,
679 int sampleCount = 1,
680 GrProtected = GrProtected::kNo) = 0;
Brian Salomonf865b052018-03-09 09:01:53 -0500681
Brian Salomon72c7b982020-10-06 10:07:38 -0400682 /**
683 * Deletes a GrBackendRenderTarget allocated with the above. Synchronization to make this safe
684 * is up to the caller.
685 */
Brian Salomonf865b052018-03-09 09:01:53 -0500686 virtual void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) = 0;
687
688 // This is only to be used in GL-specific tests.
689 virtual const GrGLContext* glContextForTesting() const { return nullptr; }
690
691 // This is only to be used by testing code
692 virtual void resetShaderCacheForTesting() const {}
693
Greg Daniel26b50a42018-03-08 09:49:58 -0500694 /**
695 * Flushes all work to the gpu and forces the GPU to wait until all the gpu work has completed.
696 * This is for testing purposes only.
697 */
698 virtual void testingOnly_flushGpuAndSync() = 0;
Jim Van Verth052761f2019-09-27 15:05:51 -0400699
700 /**
701 * Inserted as a pair around a block of code to do a GPU frame capture.
702 * Currently only works with the Metal backend.
703 */
704 virtual void testingOnly_startCapture() {}
705 virtual void testingOnly_endCapture() {}
Brian Salomonf865b052018-03-09 09:01:53 -0500706#endif
Greg Daniel26b50a42018-03-08 09:49:58 -0500707
egdanielec00d942015-09-14 12:56:10 -0700708 // width and height may be larger than rt (if underlying API allows it).
709 // Returns nullptr if compatible sb could not be created, otherwise the caller owns the ref on
Greg Danielc0d69152020-10-08 14:59:00 -0400710 // the GrAttachment.
711 virtual sk_sp<GrAttachment> makeStencilAttachmentForRenderTarget(const GrRenderTarget*,
712 SkISize dimensions,
713 int numStencilSamples) = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800714
egdaniel9cb63402016-06-23 08:37:05 -0700715 void handleDirtyContext() {
716 if (fResetBits) {
717 this->resetContext();
718 }
719 }
720
Greg Daniela870b462019-01-08 15:49:46 -0500721 virtual void storeVkPipelineCacheData() {}
722
Chris Daltone1196c52019-12-28 14:31:09 -0700723 // http://skbug.com/9739
724 virtual void insertManualFramebufferBarrier() {
725 SkASSERT(!this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
726 SK_ABORT("Manual framebuffer barrier not supported.");
727 }
728
Chris Dalton2e7ed262020-02-21 15:17:59 -0700729 // Called before certain draws in order to guarantee coherent results from dst reads.
730 virtual void xferBarrier(GrRenderTarget*, GrXferBarrierType) = 0;
731
joshualittd53a8272014-11-10 16:03:14 -0800732protected:
Brian Salomon7e67dca2020-07-21 09:27:25 -0400733 static bool MipMapsAreCorrect(SkISize dimensions, GrMipmapped, const BackendTextureData*);
Robert Phillipsb915c942019-12-17 14:44:37 -0500734 static bool CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400735 GrMipmapped, const BackendTextureData*);
Robert Phillips57ef6802019-09-23 10:12:47 -0400736
Brian Salomon1fabd512018-02-09 09:54:25 -0500737 // Handles cases where a surface will be updated without a call to flushRenderTarget.
738 void didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
739 uint32_t mipLevels = 1) const;
jvanverth900bd4a2016-04-29 13:53:12 -0700740
Brian Salomon24069eb2020-06-24 10:19:52 -0400741 void setOOMed() { fOOMed = true; }
742
Ben Wagner145dbcd2016-11-03 14:40:50 -0400743 Stats fStats;
744 std::unique_ptr<GrPathRendering> fPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800745 // Subclass must initialize this in its constructor.
Hal Canary144caf52016-11-07 17:57:18 -0500746 sk_sp<const GrCaps> fCaps;
joshualitt3322fa42014-11-07 08:48:51 -0800747
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000748private:
Brian Salomon85c3d682019-11-04 15:04:54 -0500749 virtual GrBackendTexture onCreateBackendTexture(SkISize dimensions,
750 const GrBackendFormat&,
751 GrRenderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400752 GrMipmapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400753 GrProtected) = 0;
Robert Phillips57ef6802019-09-23 10:12:47 -0400754
Greg Danielc1ad77c2020-05-06 11:40:03 -0400755 virtual GrBackendTexture onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400756 SkISize dimensions, const GrBackendFormat&, GrMipmapped, GrProtected) = 0;
Robert Phillipsb915c942019-12-17 14:44:37 -0500757
Greg Daniel16032b32020-05-06 15:31:10 -0400758 virtual bool onUpdateBackendTexture(const GrBackendTexture&,
759 sk_sp<GrRefCntedCallback> finishedCallback,
760 const BackendTextureData*) = 0;
761
Greg Danielaaf738c2020-07-10 09:30:33 -0400762 virtual bool onUpdateCompressedBackendTexture(const GrBackendTexture&,
763 sk_sp<GrRefCntedCallback> finishedCallback,
764 const BackendTextureData*) = 0;
765
bsalomon@google.comb635d392011-11-05 12:47:43 +0000766 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000767 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000768 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000769
Brian Salomon1f05d452019-02-08 12:33:08 -0500770 // Implementation of resetTextureBindings.
771 virtual void onResetTextureBindings() {}
772
Chris Daltond7291ba2019-03-07 14:17:03 -0700773 // Queries the effective number of samples in use by the hardware for the given render target,
774 // and queries the individual sample locations.
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600775 virtual void querySampleLocations(GrRenderTarget*, SkTArray<SkPoint>*) = 0;
Chris Daltond7291ba2019-03-07 14:17:03 -0700776
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000777 // overridden by backend-specific derived class to create objects.
Brian Salomond2a8ae22019-09-10 16:03:59 -0400778 // Texture size, renderablility, format support, sample count will have already been validated
779 // in base class before onCreateTexture is called.
780 // If the ith bit is set in levelClearMask then the ith MIP level should be cleared.
Brian Salomona56a7462020-02-07 14:17:25 -0500781 virtual sk_sp<GrTexture> onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -0400782 const GrBackendFormat&,
783 GrRenderable,
784 int renderTargetSampleCnt,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400785 SkBudgeted,
786 GrProtected,
787 int mipLevelCoont,
788 uint32_t levelClearMask) = 0;
Robert Phillips9f744f72019-12-19 19:14:33 -0500789 virtual sk_sp<GrTexture> onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400790 const GrBackendFormat&,
Robert Phillips3a833922020-01-21 15:25:58 -0500791 SkBudgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400792 GrMipmapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500793 GrProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -0500794 const void* data, size_t dataSize) = 0;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400795 virtual sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&,
796 GrWrapOwnership,
797 GrWrapCacheable,
798 GrIOType) = 0;
Robert Phillipsb915c942019-12-17 14:44:37 -0500799
800 virtual sk_sp<GrTexture> onWrapCompressedBackendTexture(const GrBackendTexture&,
801 GrWrapOwnership,
802 GrWrapCacheable) = 0;
803
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400804 virtual sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
805 int sampleCnt,
806 GrWrapOwnership,
Robert Phillips0902c982019-07-16 07:47:56 -0400807 GrWrapCacheable) = 0;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400808 virtual sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) = 0;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000809 virtual sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400810 int sampleCnt) = 0;
Greg Danielb46add82019-01-02 14:51:29 -0500811 virtual sk_sp<GrRenderTarget> onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
812 const GrVkDrawableInfo&);
813
Brian Salomondbf70722019-02-07 11:31:24 -0500814 virtual sk_sp<GrGpuBuffer> onCreateBuffer(size_t size, GrGpuBufferType intendedType,
815 GrAccessPattern, const void* data) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000816
bsalomon6cb3cbe2015-07-30 07:34:27 -0700817 // overridden by backend-specific derived class to perform the surface read
Brian Salomonf77c1462019-08-01 15:19:29 -0400818 virtual bool onReadPixels(GrSurface*, int left, int top, int width, int height,
819 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
820 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000821
bsalomon6cb3cbe2015-07-30 07:34:27 -0700822 // overridden by backend-specific derived class to perform the surface write
Brian Salomonf77c1462019-08-01 15:19:29 -0400823 virtual bool onWritePixels(GrSurface*, int left, int top, int width, int height,
824 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400825 const GrMipLevel texels[], int mipLevelCount,
826 bool prepForTexSampling) = 0;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000827
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400828 // overridden by backend-specific derived class to perform the texture transfer
Brian Salomone05ba5a2019-04-08 11:59:07 -0400829 virtual bool onTransferPixelsTo(GrTexture*, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400830 GrColorType textiueColorType, GrColorType bufferColorType,
831 GrGpuBuffer* transferBuffer, size_t offset,
832 size_t rowBytes) = 0;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400833 // overridden by backend-specific derived class to perform the surface transfer
Brian Salomon26de56e2019-04-10 12:14:26 -0400834 virtual bool onTransferPixelsFrom(GrSurface*, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400835 GrColorType surfaceColorType, GrColorType bufferColorType,
836 GrGpuBuffer* transferBuffer, size_t offset) = 0;
jvanverth17aa0472016-01-05 10:41:27 -0800837
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000838 // overridden by backend-specific derived class to perform the resolve
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400839 virtual void onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) = 0;
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000840
Brian Salomon930f9392018-06-20 16:25:26 -0400841 // overridden by backend specific derived class to perform mip map level regeneration.
842 virtual bool onRegenerateMipMapLevels(GrTexture*) = 0;
843
joshualitt1cbdcde2015-08-21 11:53:29 -0700844 // overridden by backend specific derived class to perform the copy surface
Greg Daniel46cfbc62019-06-07 11:43:30 -0400845 virtual bool onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400846 const SkIPoint& dstPoint) = 0;
joshualitt1cbdcde2015-08-21 11:53:29 -0700847
Greg Daniel9efe3862020-06-11 11:51:06 -0400848 virtual void prepareSurfacesForBackendAccessAndStateUpdates(
849 GrSurfaceProxy* proxies[],
850 int numProxies,
851 SkSurface::BackendSurfaceAccess access,
852 const GrBackendSurfaceMutableState* newState) {}
Greg Danielfe159622020-04-10 17:43:51 +0000853
854 virtual bool onSubmitToGpu(bool syncCpu) = 0;
Greg Daniel51316782017-08-02 15:10:09 +0000855
Kevin Lubickf4def342018-10-04 12:52:50 -0400856#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400857 virtual void onDumpJSON(SkJSONWriter*) const {}
Kevin Lubickf4def342018-10-04 12:52:50 -0400858#endif
Brian Osman71a18892017-08-10 10:23:25 -0400859
Brian Salomona56a7462020-02-07 14:17:25 -0500860 sk_sp<GrTexture> createTextureCommon(SkISize,
Robert Phillips3a833922020-01-21 15:25:58 -0500861 const GrBackendFormat&,
862 GrRenderable,
Brian Salomona90382f2019-09-17 09:01:56 -0400863 int renderTargetSampleCnt,
Robert Phillips3a833922020-01-21 15:25:58 -0500864 SkBudgeted,
865 GrProtected,
Brian Salomona90382f2019-09-17 09:01:56 -0400866 int mipLevelCnt,
867 uint32_t levelClearMask);
868
bsalomon@google.comb635d392011-11-05 12:47:43 +0000869 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000870 this->onResetContext(fResetBits);
871 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000872 }
873
Greg Daniel55822f12020-05-26 11:26:45 -0400874 void callSubmittedProcs(bool success);
875
Chris Dalton535ba8d2018-02-20 09:51:59 -0700876 uint32_t fResetBits;
joshualitt3322fa42014-11-07 08:48:51 -0800877 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
Adlai Holler3d0359a2020-07-09 15:35:55 -0400878 GrDirectContext* fContext;
Chris Daltond7291ba2019-03-07 14:17:03 -0700879 GrSamplePatternDictionary fSamplePatternDictionary;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000880
Greg Daniel55822f12020-05-26 11:26:45 -0400881 struct SubmittedProc {
882 SubmittedProc(GrGpuSubmittedProc proc, GrGpuSubmittedContext context)
883 : fProc(proc), fContext(context) {}
884
885 GrGpuSubmittedProc fProc;
886 GrGpuSubmittedContext fContext;
887 };
888 SkSTArray<4, SubmittedProc> fSubmittedProcs;
889
Brian Salomon24069eb2020-06-24 10:19:52 -0400890 bool fOOMed = false;
891
kkinnunencabe20c2015-06-01 01:37:26 -0700892 friend class GrPathRendering;
John Stiles7571f9e2020-09-02 22:42:33 -0400893 using INHERITED = SkRefCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000894};
895
896#endif