blob: 80165448e643b1bc3ba0fb872f06e97444e79a23 [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"
Brian Osmana5842bc2021-05-11 13:41:46 -040012#include "include/core/SkSpan.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkSurface.h"
14#include "include/gpu/GrTypes.h"
15#include "include/private/SkTArray.h"
Stephen Whitef3d5d442020-04-08 10:35:58 -040016#include "src/core/SkTInternalLList.h"
Greg Daniel5d0330e2020-10-12 16:05:21 -040017#include "src/gpu/GrAttachment.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrCaps.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040019#include "src/gpu/GrOpsRenderPass.h"
Brian Salomon05487ab2020-12-23 20:32:22 -050020#include "src/gpu/GrPixmap.h"
Brian Salomon3ec1f542019-06-17 17:54:57 +000021#include "src/gpu/GrSwizzle.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrXferProcessor.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000023
Brian Salomon05487ab2020-12-23 20:32:22 -050024class GrAttachment;
Greg Danielbcf612b2017-05-01 13:50:58 +000025class GrBackendRenderTarget;
Greg Daniela5cb7812017-06-16 09:45:32 -040026class GrBackendSemaphore;
Brian Salomon05487ab2020-12-23 20:32:22 -050027struct GrContextOptions;
Adlai Holler3d0359a2020-07-09 15:35:55 -040028class GrDirectContext;
Brian Salomondbf70722019-02-07 11:31:24 -050029class GrGpuBuffer;
reedf9ad5582015-06-25 21:29:25 -070030class GrGLContext;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000031class GrPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000032class GrPathRenderer;
33class GrPathRendererChain;
egdaniel8dd688b2015-01-22 10:16:09 -080034class GrPipeline;
Robert Phillips787fd9d2021-03-22 14:48:09 -040035class GrGeometryProcessor;
kkinnunencabe20c2015-06-01 01:37:26 -070036class GrRenderTarget;
Jim Van Verth7e829b22020-07-30 17:04:35 -040037class GrRingBuffer;
Greg Daniel6be35232017-03-01 17:01:09 -050038class GrSemaphore;
Greg Daniela58db7f2020-07-15 09:17:59 -040039class GrStagingBufferManager;
cdalton93a379b2016-05-11 13:58:08 -070040class GrStencilSettings;
kkinnunencabe20c2015-06-01 01:37:26 -070041class GrSurface;
42class GrTexture;
Robert Phillipsae67c522021-03-03 11:03:38 -050043class GrThreadSafePipelineBuilder;
Greg Daniel84261652021-09-19 17:53:40 -040044struct GrVkDrawableInfo;
Brian Osman71a18892017-08-10 10:23:25 -040045class SkJSONWriter;
reed@google.comac10a2d2010-12-22 21:39:39 +000046
Brian Osmancbdc2612020-11-23 15:30:48 -050047namespace SkSL {
48 class Compiler;
49}
50
joshualitt3322fa42014-11-07 08:48:51 -080051class GrGpu : public SkRefCnt {
reed@google.comac10a2d2010-12-22 21:39:39 +000052public:
Adlai Holler3d0359a2020-07-09 15:35:55 -040053 GrGpu(GrDirectContext* direct);
mtklein36352bf2015-03-25 18:17:31 -070054 ~GrGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000055
Adlai Holler3d0359a2020-07-09 15:35:55 -040056 GrDirectContext* getContext() { return fContext; }
57 const GrDirectContext* getContext() const { return fContext; }
joshualitt3322fa42014-11-07 08:48:51 -080058
59 /**
60 * Gets the capabilities of the draw target.
61 */
bsalomon4b91f762015-05-19 09:29:46 -070062 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipse42edcc2017-12-13 11:50:22 -050063 sk_sp<const GrCaps> refCaps() const { return fCaps; }
joshualitt3322fa42014-11-07 08:48:51 -080064
Greg Daniela58db7f2020-07-15 09:17:59 -040065 virtual GrStagingBufferManager* stagingBufferManager() { return nullptr; }
66
Jim Van Verth7e829b22020-07-30 17:04:35 -040067 virtual GrRingBuffer* uniformsRingBuffer() { return nullptr; }
68
Brian Osmancbdc2612020-11-23 15:30:48 -050069 SkSL::Compiler* shaderCompiler() const { return fCompiler.get(); }
70
bsalomon6e2aad42016-04-01 11:54:31 -070071 enum class DisconnectType {
72 // No cleanup should be attempted, immediately cease making backend API calls
73 kAbandon,
74 // Free allocated resources (not known by GrResourceCache) before returning and
75 // ensure no backend backend 3D API calls will be made after disconnect() returns.
76 kCleanup,
77 };
78
Adlai Holler3d0359a2020-07-09 15:35:55 -040079 // Called by context when the underlying backend context is already or will be destroyed
80 // before GrDirectContext.
bsalomon6e2aad42016-04-01 11:54:31 -070081 virtual void disconnect(DisconnectType);
bsalomonc8dc1f72014-08-21 13:02:13 -070082
Robert Phillipsae67c522021-03-03 11:03:38 -050083 virtual GrThreadSafePipelineBuilder* pipelineBuilder() = 0;
84 virtual sk_sp<GrThreadSafePipelineBuilder> refPipelineBuilder() = 0;
85
Adlai Holler3d0359a2020-07-09 15:35:55 -040086 // Called by GrDirectContext::isContextLost. Returns true if the backend Gpu object has gotten
87 // into an unrecoverable, lost state.
Greg Daniel6e35a002020-04-01 13:29:59 -040088 virtual bool isDeviceLost() const { return false; }
89
reed@google.comac10a2d2010-12-22 21:39:39 +000090 /**
91 * The GrGpu object normally assumes that no outsider is setting state
92 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000093 * the GrGpu that the state was modified and it shouldn't make assumptions
94 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000095 */
mtkleinb9eb4ac2015-02-02 18:26:03 -080096 void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
reed@google.comac10a2d2010-12-22 21:39:39 +000097
98 /**
Brian Salomonf2c2ba92019-07-17 09:59:59 -040099 * Creates a texture object. If renderable is kYes then the returned texture can
bsalomon6d467ec2014-11-18 07:36:19 -0800100 * be used as a render target by calling GrTexture::asRenderTarget(). Not all
101 * pixel configs can be used as render targets. Support for configs as textures
bsalomon4b91f762015-05-19 09:29:46 -0700102 * or render targets can be checked using GrCaps.
bsalomon@google.com1da07462011-03-10 14:51:57 +0000103 *
Brian Salomona56a7462020-02-07 14:17:25 -0500104 * @param dimensions dimensions of the texture to be created.
Brian Salomon4eb38b72019-08-05 12:58:39 -0400105 * @param format the format for the texture (not currently used).
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400106 * @param renderable should the resulting texture be renderable
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400107 * @param renderTargetSampleCnt The number of samples to use for rendering if renderable is
108 * kYes. If renderable is kNo then this must be 1.
Brian Salomon58389b92018-03-07 13:01:25 -0500109 * @param budgeted does this texture count against the resource cache budget?
Brian Salomone8a766b2019-07-19 14:24:36 -0400110 * @param isProtected should the texture be created as protected.
Brian Salomon58389b92018-03-07 13:01:25 -0500111 * @param texels array of mipmap levels containing texel data to load.
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400112 * If level i has pixels then it is assumed that its dimensions are
Brian Salomona56a7462020-02-07 14:17:25 -0500113 * max(1, floor(dimensions.fWidth / 2)) by
114 * max(1, floor(dimensions.fHeight / 2)).
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400115 * If texels[i].fPixels == nullptr for all i <= mipLevelCount or
116 * mipLevelCount is 0 then the texture's contents are uninitialized.
117 * If a level has non-null pixels, its row bytes must be a multiple of the
118 * config's bytes-per-pixel. The row bytes must be tight to the
119 * level width if !caps->writePixelsRowBytesSupport().
120 * If mipLevelCount > 1 and texels[i].fPixels != nullptr for any i > 0
121 * then all levels must have non-null pixels. All levels must have
122 * non-null pixels if GrCaps::createTextureMustSpecifyAllLevels() is true.
Brian Salomona90382f2019-09-17 09:01:56 -0400123 * @param textureColorType The color type interpretation of the texture for the purpose of
124 * of uploading texel data.
125 * @param srcColorType The color type of data in texels[].
Brian Salomond2a8ae22019-09-10 16:03:59 -0400126 * @param texelLevelCount the number of levels in 'texels'. May be 0, 1, or
Brian Salomona56a7462020-02-07 14:17:25 -0500127 * floor(max((log2(dimensions.fWidth), log2(dimensions.fHeight)))). It
128 * must be the latter if GrCaps::createTextureMustSpecifyAllLevels() is
129 * true.
Brian Salomon58389b92018-03-07 13:01:25 -0500130 * @return The texture object if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000131 */
Brian Salomona56a7462020-02-07 14:17:25 -0500132 sk_sp<GrTexture> createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400133 const GrBackendFormat& format,
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400134 GrTextureType textureType,
Brian Salomona90382f2019-09-17 09:01:56 -0400135 GrRenderable renderable,
136 int renderTargetSampleCnt,
Robert Phillips3a833922020-01-21 15:25:58 -0500137 SkBudgeted budgeted,
Brian Salomona90382f2019-09-17 09:01:56 -0400138 GrProtected isProtected,
139 GrColorType textureColorType,
140 GrColorType srcColorType,
141 const GrMipLevel texels[],
Brian Salomond2a8ae22019-09-10 16:03:59 -0400142 int texelLevelCount);
cblume55f2d2d2016-02-26 13:20:48 -0800143
Brian Salomonc7dced52019-07-18 15:02:01 +0000144 /**
145 * Simplified createTexture() interface for when there is no initial texel data to upload.
146 */
Brian Salomona56a7462020-02-07 14:17:25 -0500147 sk_sp<GrTexture> createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400148 const GrBackendFormat& format,
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400149 GrTextureType textureType,
Brian Salomona90382f2019-09-17 09:01:56 -0400150 GrRenderable renderable,
151 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400152 GrMipmapped mipMapped,
Brian Salomona90382f2019-09-17 09:01:56 -0400153 SkBudgeted budgeted,
154 GrProtected isProtected);
Brian Salomonc7dced52019-07-18 15:02:01 +0000155
Robert Phillips3a833922020-01-21 15:25:58 -0500156 sk_sp<GrTexture> createCompressedTexture(SkISize dimensions,
157 const GrBackendFormat& format,
158 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400159 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500160 GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500161 const void* data, size_t dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400162
bsalomon@google.come269f212011-11-07 13:29:52 +0000163 /**
Brian Osman32342f02017-03-04 08:12:46 -0500164 * Implements GrResourceProvider::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000165 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400166 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture&,
167 GrWrapOwnership,
168 GrWrapCacheable,
169 GrIOType);
Brian Salomond17f6582017-07-19 18:28:58 -0400170
Robert Phillipsb915c942019-12-17 14:44:37 -0500171 sk_sp<GrTexture> wrapCompressedBackendTexture(const GrBackendTexture&,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400172 GrWrapOwnership,
173 GrWrapCacheable);
Robert Phillipsb915c942019-12-17 14:44:37 -0500174
Brian Salomond17f6582017-07-19 18:28:58 -0400175 /**
176 * Implements GrResourceProvider::wrapRenderableBackendTexture
177 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400178 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture&,
179 int sampleCnt,
180 GrWrapOwnership,
181 GrWrapCacheable);
bsalomon@google.come269f212011-11-07 13:29:52 +0000182
183 /**
Brian Osman32342f02017-03-04 08:12:46 -0500184 * Implements GrResourceProvider::wrapBackendRenderTarget
bsalomon@google.come269f212011-11-07 13:29:52 +0000185 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400186 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000187
188 /**
Greg Danielb46add82019-01-02 14:51:29 -0500189 * Implements GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget
190 */
191 sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
192 const GrVkDrawableInfo&);
193
194 /**
csmartdalton485a1202016-07-13 10:16:32 -0700195 * Creates a buffer in GPU memory. For a client-side buffer use GrBuffer::CreateCPUBacked.
reed@google.comac10a2d2010-12-22 21:39:39 +0000196 *
cdaltone2e71c22016-04-07 18:13:29 -0700197 * @param size size of buffer to create.
198 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
199 * @param accessPattern hint to the graphics subsystem about how the data will be accessed.
cdalton1bf3e712016-04-19 10:00:02 -0700200 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700201 *
cdalton397536c2016-03-25 12:15:03 -0700202 * @return the buffer if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000203 */
Brian Salomondbf70722019-02-07 11:31:24 -0500204 sk_sp<GrGpuBuffer> createBuffer(size_t size, GrGpuBufferType intendedType,
205 GrAccessPattern accessPattern, const void* data = nullptr);
halcanary9d524f22016-03-29 09:03:52 -0700206
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 /**
Greg Daniel242536f2020-02-13 14:12:46 -0500208 * Resolves MSAA. The resolveRect must already be in the native destination space.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000209 */
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400210 void resolveRenderTarget(GrRenderTarget*, const SkIRect& resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000211
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000212 /**
Brian Salomon930f9392018-06-20 16:25:26 -0400213 * Uses the base of the texture to recompute the contents of the other levels.
214 */
215 bool regenerateMipMapLevels(GrTexture*);
216
217 /**
Brian Salomon1f05d452019-02-08 12:33:08 -0500218 * If the backend API has stateful texture bindings, this resets them back to defaults.
219 */
220 void resetTextureBindings();
221
222 /**
Brian Salomon9b009bb2018-02-14 13:53:55 -0500223 * Reads a rectangle of pixels from a render target. No sRGB/linear conversions are performed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000224 *
Brian Salomone2078f12021-05-24 12:40:46 -0400225 * @param surface the surface to read from
226 * @param rect the rectangle of pixels to read
Brian Salomonf77c1462019-08-01 15:19:29 -0400227 * @param surfaceColorType the color type for this use of the surface.
228 * @param dstColorType the color type of the destination buffer.
229 * @param buffer memory to read the rectangle into.
230 * @param rowBytes the number of bytes between consecutive rows. Must be a multiple of
231 * dstColorType's bytes-per-pixel. Must be tight to width if
232 * !caps->readPixelsRowBytesSupport().
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000233 *
234 * @return true if the read succeeded, false if not. The read can fail
Brian Salomon1d435302019-07-01 13:05:28 -0400235 * because of the surface doesn't support reading, the color type
236 * is not allowed for the format of the surface or if the rectangle
237 * read is not contained in the surface.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000238 */
Brian Salomone2078f12021-05-24 12:40:46 -0400239 bool readPixels(GrSurface* surface,
240 SkIRect rect,
241 GrColorType surfaceColorType,
242 GrColorType dstColorType,
243 void* buffer,
Brian Salomonf77c1462019-08-01 15:19:29 -0400244 size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
bsalomon@google.com6f379512011-11-16 20:36:03 +0000246 /**
Brian Salomon9b009bb2018-02-14 13:53:55 -0500247 * Updates the pixels in a rectangle of a surface. No sRGB/linear conversions are performed.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000248 *
Brian Salomone2078f12021-05-24 12:40:46 -0400249 * @param surface the surface to write to.
250 * @param rect the rectangle of pixels to overwrite
Greg Danielb20d7e52019-09-03 13:54:39 -0400251 * @param surfaceColorType the color type for this use of the surface.
252 * @param srcColorType the color type of the source buffer.
253 * @param texels array of mipmap levels containing texture data. Row bytes must be a
254 * multiple of srcColorType's bytes-per-pixel. Must be tight to level
255 * width if !caps->writePixelsRowBytesSupport().
256 * @param mipLevelCount number of levels in 'texels'
257 * @param prepForTexSampling After doing write pixels should the surface be prepared for texture
258 * sampling. This is currently only used by Vulkan for inline uploads
259 * to set that layout back to sampled after doing the upload. Inline
260 * uploads currently can happen between draws in a single op so it is
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400261 * not trivial to break up the OpsTask into two tasks when we see
Greg Danielb20d7e52019-09-03 13:54:39 -0400262 * an inline upload. However, once we are able to support doing that
263 * we can remove this parameter.
Brian Salomon1d435302019-07-01 13:05:28 -0400264 *
265 * @return true if the write succeeded, false if not. The read can fail
266 * because of the surface doesn't support writing (e.g. read only),
267 * the color type is not allowed for the format of the surface or
268 * if the rectangle written is not contained in the surface.
cblume55f2d2d2016-02-26 13:20:48 -0800269 */
Brian Salomone2078f12021-05-24 12:40:46 -0400270 bool writePixels(GrSurface* surface,
271 SkIRect rect,
272 GrColorType surfaceColorType,
273 GrColorType srcColorType,
274 const GrMipLevel texels[],
275 int mipLevelCount,
276 bool prepForTexSampling = false);
cblume55f2d2d2016-02-26 13:20:48 -0800277
278 /**
Brian Salomona9b04b92018-06-01 15:04:28 -0400279 * Helper for the case of a single level.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500280 */
Brian Salomone2078f12021-05-24 12:40:46 -0400281 bool writePixels(GrSurface* surface,
282 SkIRect rect,
283 GrColorType surfaceColorType,
284 GrColorType srcColorType,
285 const void* buffer,
286 size_t rowBytes,
287 bool prepForTexSampling = false) {
Brian Salomon2c673402021-04-02 14:36:58 -0400288 GrMipLevel mipLevel = {buffer, rowBytes, nullptr};
Brian Salomone2078f12021-05-24 12:40:46 -0400289 return this->writePixels(surface,
290 rect,
291 surfaceColorType,
292 srcColorType,
293 &mipLevel,
294 1,
295 prepForTexSampling);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500296 }
297
298 /**
Brian Salomone05ba5a2019-04-08 11:59:07 -0400299 * Updates the pixels in a rectangle of a texture using a buffer. If the texture is MIP mapped,
300 * the base level is written to.
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400301 *
Brian Salomone2078f12021-05-24 12:40:46 -0400302 * @param texture the texture to write to.
303 * @param rect the rectangle of pixels in the texture to overwrite
Brian Salomonf77c1462019-08-01 15:19:29 -0400304 * @param textureColorType the color type for this use of the surface.
Brian Salomonc320b152018-02-20 14:05:36 -0500305 * @param bufferColorType the color type of the transfer buffer's pixel data
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400306 * @param transferBuffer GrBuffer to read pixels from (type must be "kXferCpuToGpu")
cdalton397536c2016-03-25 12:15:03 -0700307 * @param offset offset from the start of the buffer
Brian Salomon1047a492019-07-02 12:25:21 -0400308 * @param rowBytes number of bytes between consecutive rows in the buffer. Must be a
Brian Salomone2078f12021-05-24 12:40:46 -0400309 * multiple of bufferColorType's bytes-per-pixel. Must be tight to
310 * rect.width() if !caps->writePixelsRowBytesSupport().
jvanverth17aa0472016-01-05 10:41:27 -0800311 */
Brian Salomone2078f12021-05-24 12:40:46 -0400312 bool transferPixelsTo(GrTexture* texture,
313 SkIRect rect,
314 GrColorType textureColorType,
315 GrColorType bufferColorType,
316 sk_sp<GrGpuBuffer> transferBuffer,
317 size_t offset,
318 size_t rowBytes);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400319
320 /**
321 * Reads the pixels from a rectangle of a surface into a buffer. Use
Greg Danielba88ab62019-07-26 09:14:01 -0400322 * GrCaps::SupportedRead::fOffsetAlignmentForTransferBuffer to determine the requirements for
323 * the buffer offset alignment. If the surface is a MIP mapped texture, the base level is read.
Brian Salomone05ba5a2019-04-08 11:59:07 -0400324 *
Brian Salomon26de56e2019-04-10 12:14:26 -0400325 * If successful the row bytes in the buffer is always:
Brian Salomone2078f12021-05-24 12:40:46 -0400326 * GrColorTypeBytesPerPixel(bufferColorType) * rect.width()
Brian Salomon26de56e2019-04-10 12:14:26 -0400327 *
328 * Asserts that the caller has passed a properly aligned offset and that the buffer is
329 * large enough to hold the result
Brian Salomone05ba5a2019-04-08 11:59:07 -0400330 *
Brian Salomone2078f12021-05-24 12:40:46 -0400331 * @param surface the surface to read from.
332 * @param rect the rectangle of pixels to read
Brian Salomonf77c1462019-08-01 15:19:29 -0400333 * @param surfaceColorType the color type for this use of the surface.
Brian Salomone05ba5a2019-04-08 11:59:07 -0400334 * @param bufferColorType the color type of the transfer buffer's pixel data
335 * @param transferBuffer GrBuffer to write pixels to (type must be "kXferGpuToCpu")
336 * @param offset offset from the start of the buffer
337 */
Brian Salomone2078f12021-05-24 12:40:46 -0400338 bool transferPixelsFrom(GrSurface* surface,
339 SkIRect rect,
340 GrColorType surfaceColorType,
341 GrColorType bufferColorType,
342 sk_sp<GrGpuBuffer> transferBuffer,
343 size_t offset);
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
Greg Danielf41b2bd2019-08-22 16:19:24 -0400346 // take place at higher levels 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
Greg Daniel46cfbc62019-06-07 11:43:30 -0400348 // src/dst bounds and non-empty. They must also be in their exact device space coords, including
349 // already being transformed for origin if need be. If canDiscardOutsideDstRect is set to true
350 // then we don't need to preserve any data on the dst surface outside of the copy.
351 bool copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400352 const SkIPoint& dstPoint);
joshualitt3322fa42014-11-07 08:48:51 -0800353
Robert Phillips3e87a8e2021-08-25 13:22:24 -0400354 // Returns a GrOpsRenderPass which OpsTasks send draw commands to instead of directly
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400355 // to the Gpu object. The 'bounds' rect is the content rect of the renderTarget.
Robert Phillips96f22372020-05-20 12:31:18 -0400356 // If a 'stencil' is provided it will be the one bound to 'renderTarget'. If one is not
357 // provided but 'renderTarget' has a stencil buffer then that is a signal that the
358 // render target's stencil buffer should be ignored.
Greg Daniel65476e02020-10-27 09:20:20 -0400359 GrOpsRenderPass* getOpsRenderPass(GrRenderTarget* renderTarget,
Chris Daltonda2b0f42021-04-13 00:19:45 -0600360 bool useMSAASurface,
Greg Daniel65476e02020-10-27 09:20:20 -0400361 GrAttachment* stencil,
362 GrSurfaceOrigin,
363 const SkIRect& bounds,
364 const GrOpsRenderPass::LoadAndStoreInfo&,
365 const GrOpsRenderPass::StencilLoadAndStoreInfo&,
366 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
367 GrXferBarrierFlags renderPassXferBarriers);
bsalomon3e791242014-12-17 13:43:13 -0800368
Robert Phillips18e94842017-05-15 13:06:44 -0400369 // Called by GrDrawingManager when flushing.
Greg Daniel51316782017-08-02 15:10:09 +0000370 // Provides a hook for post-flush actions (e.g. Vulkan command buffer submits). This will also
371 // insert any numSemaphore semaphores on the gpu and set the backendSemaphores to match the
372 // inserted semaphores.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500373 void executeFlushInfo(SkSpan<GrSurfaceProxy*>,
Greg Daniel9efe3862020-06-11 11:51:06 -0400374 SkSurface::BackendSurfaceAccess access,
375 const GrFlushInfo&,
376 const GrBackendSurfaceMutableState* newState);
Greg Danielfe159622020-04-10 17:43:51 +0000377
378 bool submitToGpu(bool syncCpu);
ethannicholas22793252016-01-30 09:59:10 -0800379
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400380 virtual void submit(GrOpsRenderPass*) = 0;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400381
Greg Daniel6be35232017-03-01 17:01:09 -0500382 virtual GrFence SK_WARN_UNUSED_RESULT insertFence() = 0;
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400383 virtual bool waitFence(GrFence) = 0;
jvanverth84741b32016-09-30 08:39:02 -0700384 virtual void deleteFence(GrFence) const = 0;
385
Greg Daniel301015c2019-11-18 14:06:46 -0500386 virtual std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(
387 bool isOwned = true) = 0;
Robert Phillips1a82a4e2021-07-01 10:27:44 -0400388 virtual std::unique_ptr<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
389 GrSemaphoreWrapType,
390 GrWrapOwnership) = 0;
Greg Daniel301015c2019-11-18 14:06:46 -0500391 virtual void insertSemaphore(GrSemaphore* semaphore) = 0;
392 virtual void waitSemaphore(GrSemaphore* semaphore) = 0;
Greg Daniel6be35232017-03-01 17:01:09 -0500393
Jim Van Verth1aaf41b2020-07-29 09:24:29 -0400394 virtual void addFinishedProc(GrGpuFinishedProc finishedProc,
395 GrGpuFinishedContext finishedContext) = 0;
Brian Salomonb0d8b762019-05-06 16:58:22 -0400396 virtual void checkFinishProcs() = 0;
Greg Daniela89b4302021-01-29 10:48:40 -0500397 virtual void finishOutstandingGpuWork() = 0;
Brian Salomonb0d8b762019-05-06 16:58:22 -0400398
Jim Van Verth1aaf41b2020-07-29 09:24:29 -0400399 virtual void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) {}
Greg Daniela58db7f2020-07-15 09:17:59 -0400400
Brian Osman13dddce2017-05-09 13:19:50 -0400401 /**
Brian Salomon24069eb2020-06-24 10:19:52 -0400402 * Checks if we detected an OOM from the underlying 3D API and if so returns true and resets
403 * the internal OOM state to false. Otherwise, returns false.
404 */
405 bool checkAndResetOOMed();
406
407 /**
Adlai Holler3d0359a2020-07-09 15:35:55 -0400408 * Put this texture in a safe and known state for use across multiple contexts. Depending on
Brian Osman13dddce2017-05-09 13:19:50 -0400409 * the backend, this may return a GrSemaphore. If so, other contexts should wait on that
410 * semaphore before using this texture.
411 */
Greg Daniel301015c2019-11-18 14:06:46 -0500412 virtual std::unique_ptr<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) = 0;
Brian Osman13dddce2017-05-09 13:19:50 -0400413
Greg Daniel428523f2021-03-30 14:22:54 -0400414 /**
415 * Frees any backend specific objects that are not currently in use by the GPU. This is called
416 * when the client is trying to free up as much GPU memory as possible. We will not release
417 * resources connected to programs/pipelines since the cost to recreate those is significantly
418 * higher that other resources.
419 */
420 virtual void releaseUnlockedBackendObjects() {}
421
mtkleinb9eb4ac2015-02-02 18:26:03 -0800422 ///////////////////////////////////////////////////////////////////////////
423 // Debugging and Stats
424
425 class Stats {
426 public:
427#if GR_GPU_STATS
Brian Salomone05ba5a2019-04-08 11:59:07 -0400428 Stats() = default;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800429
Brian Salomone05ba5a2019-04-08 11:59:07 -0400430 void reset() { *this = {}; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800431
bsalomonb12ea412015-02-02 21:19:50 -0800432 int textureCreates() const { return fTextureCreates; }
433 void incTextureCreates() { fTextureCreates++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400434
bsalomonb12ea412015-02-02 21:19:50 -0800435 int textureUploads() const { return fTextureUploads; }
436 void incTextureUploads() { fTextureUploads++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400437
jvanverth17aa0472016-01-05 10:41:27 -0800438 int transfersToTexture() const { return fTransfersToTexture; }
439 void incTransfersToTexture() { fTransfersToTexture++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400440
441 int transfersFromSurface() const { return fTransfersFromSurface; }
Brian Salomone05ba5a2019-04-08 11:59:07 -0400442 void incTransfersFromSurface() { fTransfersFromSurface++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400443
444 int stencilAttachmentCreates() const { return fStencilAttachmentCreates; }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700445 void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400446
Greg Daniel5d0330e2020-10-12 16:05:21 -0400447 int msaaAttachmentCreates() const { return fMSAAAttachmentCreates; }
448 void incMSAAAttachmentCreates() { fMSAAAttachmentCreates++; }
449
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400450 int numDraws() const { return fNumDraws; }
joshualitt87a5c9f2015-09-08 13:42:05 -0700451 void incNumDraws() { fNumDraws++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400452
453 int numFailedDraws() const { return fNumFailedDraws; }
bsalomon1d417a82016-03-23 11:50:26 -0700454 void incNumFailedDraws() { ++fNumFailedDraws; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400455
Greg Danielfe159622020-04-10 17:43:51 +0000456 int numSubmitToGpus() const { return fNumSubmitToGpus; }
457 void incNumSubmitToGpus() { ++fNumSubmitToGpus; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400458
459 int numScratchTexturesReused() const { return fNumScratchTexturesReused; }
460 void incNumScratchTexturesReused() { ++fNumScratchTexturesReused; }
461
Greg Daniel5d0330e2020-10-12 16:05:21 -0400462 int numScratchMSAAAttachmentsReused() const { return fNumScratchMSAAAttachmentsReused; }
463 void incNumScratchMSAAAttachmentsReused() { ++fNumScratchMSAAAttachmentsReused; }
464
Adlai Holler78d51ff2021-03-12 10:51:44 -0500465 int renderPasses() const { return fRenderPasses; }
466 void incRenderPasses() { fRenderPasses++; }
467
Adlai Hollerfe919742021-04-10 07:42:15 -0400468 int numReorderedDAGsOverBudget() const { return fNumReorderedDAGsOverBudget; }
469 void incNumReorderedDAGsOverBudget() { fNumReorderedDAGsOverBudget++; }
470
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500471#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800472 void dump(SkString*);
joshualitte45c81c2015-12-02 09:05:37 -0800473 void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500474#endif
mtkleinb9eb4ac2015-02-02 18:26:03 -0800475 private:
Brian Salomone05ba5a2019-04-08 11:59:07 -0400476 int fTextureCreates = 0;
477 int fTextureUploads = 0;
478 int fTransfersToTexture = 0;
479 int fTransfersFromSurface = 0;
480 int fStencilAttachmentCreates = 0;
Greg Daniel5d0330e2020-10-12 16:05:21 -0400481 int fMSAAAttachmentCreates = 0;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400482 int fNumDraws = 0;
483 int fNumFailedDraws = 0;
Greg Danielfe159622020-04-10 17:43:51 +0000484 int fNumSubmitToGpus = 0;
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400485 int fNumScratchTexturesReused = 0;
Greg Daniel5d0330e2020-10-12 16:05:21 -0400486 int fNumScratchMSAAAttachmentsReused = 0;
Adlai Holler78d51ff2021-03-12 10:51:44 -0500487 int fRenderPasses = 0;
Adlai Hollerfe919742021-04-10 07:42:15 -0400488 int fNumReorderedDAGsOverBudget = 0;
Robert Phillips19f466d2020-02-26 10:27:07 -0500489
Adlai Holler78d51ff2021-03-12 10:51:44 -0500490#else // !GR_GPU_STATS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500491
492#if GR_TEST_UTILS
joshualitte45c81c2015-12-02 09:05:37 -0800493 void dump(SkString*) {}
494 void dumpKeyValuePairs(SkTArray<SkString>*, SkTArray<double>*) {}
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500495#endif
bsalomonb12ea412015-02-02 21:19:50 -0800496 void incTextureCreates() {}
497 void incTextureUploads() {}
jvanverth17aa0472016-01-05 10:41:27 -0800498 void incTransfersToTexture() {}
Greg Daniela28ea672020-09-25 11:12:56 -0400499 void incTransfersFromSurface() {}
egdaniel8dc7c3a2015-04-16 11:22:42 -0700500 void incStencilAttachmentCreates() {}
Greg Daniel5d0330e2020-10-12 16:05:21 -0400501 void incMSAAAttachmentCreates() {}
joshualitt87a5c9f2015-09-08 13:42:05 -0700502 void incNumDraws() {}
bsalomon1d417a82016-03-23 11:50:26 -0700503 void incNumFailedDraws() {}
Greg Danielfe159622020-04-10 17:43:51 +0000504 void incNumSubmitToGpus() {}
Greg Daniela28ea672020-09-25 11:12:56 -0400505 void incNumScratchTexturesReused() {}
Greg Daniel5d0330e2020-10-12 16:05:21 -0400506 void incNumScratchMSAAAttachmentsReused() {}
Adlai Holler78d51ff2021-03-12 10:51:44 -0500507 void incRenderPasses() {}
Adlai Hollerfe919742021-04-10 07:42:15 -0400508 void incNumReorderedDAGsOverBudget() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800509#endif
510 };
511
512 Stats* stats() { return &fStats; }
Brian Osman71a18892017-08-10 10:23:25 -0400513 void dumpJSON(SkJSONWriter*) const;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800514
Brian Salomon85c3d682019-11-04 15:04:54 -0500515
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400516 /**
517 * Creates a texture directly in the backend API without wrapping it in a GrTexture.
518 * Must be matched with a call to deleteBackendTexture().
Robert Phillips57ef6802019-09-23 10:12:47 -0400519 *
Brian Salomon85c3d682019-11-04 15:04:54 -0500520 * If data is null the texture is uninitialized.
Robert Phillips57ef6802019-09-23 10:12:47 -0400521 *
Brian Salomon85c3d682019-11-04 15:04:54 -0500522 * If data represents a color then all texture levels are cleared to that color.
523 *
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500524 * If data represents pixmaps then it must have a either one pixmap or, if mipmapping
525 * is specified, a complete MIP hierarchy of pixmaps. Additionally, if provided, the mip
526 * levels must be sized correctly according to the MIP sizes implied by dimensions. They
527 * must all have the same color type and that color type must be compatible with the
528 * texture format.
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400529 */
Brian Salomon85c3d682019-11-04 15:04:54 -0500530 GrBackendTexture createBackendTexture(SkISize dimensions,
531 const GrBackendFormat&,
532 GrRenderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400533 GrMipmapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400534 GrProtected);
535
Brian Salomon71283232021-04-08 12:45:58 -0400536 bool clearBackendTexture(const GrBackendTexture&,
537 sk_sp<GrRefCntedCallback> finishedCallback,
538 std::array<float, 4> color);
Robert Phillipsb915c942019-12-17 14:44:37 -0500539
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500540 /**
541 * Same as the createBackendTexture case except compressed backend textures can
542 * never be renderable.
543 */
Robert Phillipsb915c942019-12-17 14:44:37 -0500544 GrBackendTexture createCompressedBackendTexture(SkISize dimensions,
545 const GrBackendFormat&,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400546 GrMipmapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400547 GrProtected);
548
549 bool updateCompressedBackendTexture(const GrBackendTexture&,
550 sk_sp<GrRefCntedCallback> finishedCallback,
Brian Salomon71283232021-04-08 12:45:58 -0400551 const void* data,
552 size_t length);
Robert Phillips646f6372018-09-25 09:31:10 -0400553
Greg Daniel1db8e792020-06-09 17:29:32 -0400554 virtual bool setBackendTextureState(const GrBackendTexture&,
555 const GrBackendSurfaceMutableState&,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400556 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400557 sk_sp<GrRefCntedCallback> finishedCallback) {
558 return false;
559 }
560
561 virtual bool setBackendRenderTargetState(const GrBackendRenderTarget&,
562 const GrBackendSurfaceMutableState&,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400563 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400564 sk_sp<GrRefCntedCallback> finishedCallback) {
565 return false;
566 }
567
Brian Salomone64b0642018-03-07 11:47:54 -0500568 /**
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400569 * Frees a texture created by createBackendTexture(). If ownership of the backend
Adlai Holler3d0359a2020-07-09 15:35:55 -0400570 * texture has been transferred to a context using adopt semantics this should not be called.
Brian Salomone64b0642018-03-07 11:47:54 -0500571 */
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400572 virtual void deleteBackendTexture(const GrBackendTexture&) = 0;
jvanverth672bb7f2015-07-13 07:19:57 -0700573
Robert Phillips979b2232020-02-20 10:47:29 -0500574 /**
575 * In this case we have a program descriptor and a program info but no render target.
576 */
577 virtual bool compile(const GrProgramDesc&, const GrProgramInfo&) = 0;
578
Brian Osmaned58e002019-09-06 14:42:43 -0400579 virtual bool precompileShader(const SkData& key, const SkData& data) { return false; }
580
Robert Phillipsf0ced622019-05-16 09:06:25 -0400581#if GR_TEST_UTILS
582 /** Check a handle represents an actual texture in the backend API that has not been freed. */
583 virtual bool isTestingOnlyBackendTexture(const GrBackendTexture&) const = 0;
584
Brian Salomon72c7b982020-10-06 10:07:38 -0400585 /**
586 * Creates a GrBackendRenderTarget that can be wrapped using
587 * SkSurface::MakeFromBackendRenderTarget. Ideally this is a non-textureable allocation to
588 * differentiate from testing with SkSurface::MakeFromBackendTexture. When sampleCnt > 1 this
589 * is used to test client wrapped allocations with MSAA where Skia does not allocate a separate
590 * buffer for resolving. If the color is non-null the backing store should be cleared to the
591 * passed in color.
592 */
Brian Salomonf9b00422020-10-08 16:00:14 -0400593 virtual GrBackendRenderTarget createTestingOnlyBackendRenderTarget(
594 SkISize dimensions,
595 GrColorType,
596 int sampleCount = 1,
597 GrProtected = GrProtected::kNo) = 0;
Brian Salomonf865b052018-03-09 09:01:53 -0500598
Brian Salomon72c7b982020-10-06 10:07:38 -0400599 /**
600 * Deletes a GrBackendRenderTarget allocated with the above. Synchronization to make this safe
601 * is up to the caller.
602 */
Brian Salomonf865b052018-03-09 09:01:53 -0500603 virtual void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) = 0;
604
605 // This is only to be used in GL-specific tests.
606 virtual const GrGLContext* glContextForTesting() const { return nullptr; }
607
608 // This is only to be used by testing code
609 virtual void resetShaderCacheForTesting() const {}
610
Greg Daniel26b50a42018-03-08 09:49:58 -0500611 /**
Jim Van Verth052761f2019-09-27 15:05:51 -0400612 * Inserted as a pair around a block of code to do a GPU frame capture.
613 * Currently only works with the Metal backend.
614 */
615 virtual void testingOnly_startCapture() {}
616 virtual void testingOnly_endCapture() {}
Brian Salomonf865b052018-03-09 09:01:53 -0500617#endif
Greg Daniel26b50a42018-03-08 09:49:58 -0500618
egdanielec00d942015-09-14 12:56:10 -0700619 // width and height may be larger than rt (if underlying API allows it).
620 // Returns nullptr if compatible sb could not be created, otherwise the caller owns the ref on
Greg Danielc0d69152020-10-08 14:59:00 -0400621 // the GrAttachment.
Chris Daltone0fe23a2021-04-23 13:11:44 -0600622 virtual sk_sp<GrAttachment> makeStencilAttachment(const GrBackendFormat& colorFormat,
623 SkISize dimensions,
624 int numStencilSamples) = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800625
Greg Danielb8949bd2020-10-12 15:21:02 -0400626 virtual GrBackendFormat getPreferredStencilFormat(const GrBackendFormat&) = 0;
627
Greg Daniel5d0330e2020-10-12 16:05:21 -0400628 // Creates an MSAA surface to be used as an MSAA attachment on a framebuffer.
629 virtual sk_sp<GrAttachment> makeMSAAAttachment(SkISize dimensions,
630 const GrBackendFormat& format,
631 int numSamples,
Jim Van Verth8d59fe02021-09-20 11:48:21 -0400632 GrProtected isProtected,
633 GrMemoryless isMemoryless) = 0;
Greg Daniel5d0330e2020-10-12 16:05:21 -0400634
egdaniel9cb63402016-06-23 08:37:05 -0700635 void handleDirtyContext() {
636 if (fResetBits) {
637 this->resetContext();
638 }
639 }
640
Greg Daniela870b462019-01-08 15:49:46 -0500641 virtual void storeVkPipelineCacheData() {}
642
Chris Daltone1196c52019-12-28 14:31:09 -0700643 // http://skbug.com/9739
644 virtual void insertManualFramebufferBarrier() {
645 SkASSERT(!this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
646 SK_ABORT("Manual framebuffer barrier not supported.");
647 }
648
Chris Dalton2e7ed262020-02-21 15:17:59 -0700649 // Called before certain draws in order to guarantee coherent results from dst reads.
650 virtual void xferBarrier(GrRenderTarget*, GrXferBarrierType) = 0;
651
joshualittd53a8272014-11-10 16:03:14 -0800652protected:
Brian Salomon71283232021-04-08 12:45:58 -0400653 static bool CompressedDataIsCorrect(SkISize dimensions,
654 SkImage::CompressionType,
655 GrMipmapped,
656 const void* data,
657 size_t length);
Robert Phillips57ef6802019-09-23 10:12:47 -0400658
Brian Salomon1fabd512018-02-09 09:54:25 -0500659 // Handles cases where a surface will be updated without a call to flushRenderTarget.
660 void didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
661 uint32_t mipLevels = 1) const;
jvanverth900bd4a2016-04-29 13:53:12 -0700662
Brian Salomon24069eb2020-06-24 10:19:52 -0400663 void setOOMed() { fOOMed = true; }
664
Ben Wagner145dbcd2016-11-03 14:40:50 -0400665 Stats fStats;
Brian Osmancbdc2612020-11-23 15:30:48 -0500666
667 // Subclass must call this to initialize caps & compiler in its constructor.
668 void initCapsAndCompiler(sk_sp<const GrCaps> caps);
joshualitt3322fa42014-11-07 08:48:51 -0800669
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000670private:
Brian Salomon85c3d682019-11-04 15:04:54 -0500671 virtual GrBackendTexture onCreateBackendTexture(SkISize dimensions,
672 const GrBackendFormat&,
673 GrRenderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400674 GrMipmapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400675 GrProtected) = 0;
Robert Phillips57ef6802019-09-23 10:12:47 -0400676
Greg Danielc1ad77c2020-05-06 11:40:03 -0400677 virtual GrBackendTexture onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400678 SkISize dimensions, const GrBackendFormat&, GrMipmapped, GrProtected) = 0;
Robert Phillipsb915c942019-12-17 14:44:37 -0500679
Brian Salomon71283232021-04-08 12:45:58 -0400680 virtual bool onClearBackendTexture(const GrBackendTexture&,
681 sk_sp<GrRefCntedCallback> finishedCallback,
682 std::array<float, 4> color) = 0;
Greg Daniel16032b32020-05-06 15:31:10 -0400683
Greg Danielaaf738c2020-07-10 09:30:33 -0400684 virtual bool onUpdateCompressedBackendTexture(const GrBackendTexture&,
685 sk_sp<GrRefCntedCallback> finishedCallback,
Brian Salomon71283232021-04-08 12:45:58 -0400686 const void* data,
687 size_t length) = 0;
Greg Danielaaf738c2020-07-10 09:30:33 -0400688
bsalomon@google.comb635d392011-11-05 12:47:43 +0000689 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000690 // assumed 3D context state and dirty any state cache.
Robert Phillips15f6d742021-01-26 13:03:39 -0500691 virtual void onResetContext(uint32_t resetBits) {}
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000692
Brian Salomon1f05d452019-02-08 12:33:08 -0500693 // Implementation of resetTextureBindings.
694 virtual void onResetTextureBindings() {}
695
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000696 // overridden by backend-specific derived class to create objects.
Brian Salomond2a8ae22019-09-10 16:03:59 -0400697 // Texture size, renderablility, format support, sample count will have already been validated
698 // in base class before onCreateTexture is called.
699 // If the ith bit is set in levelClearMask then the ith MIP level should be cleared.
Brian Salomona56a7462020-02-07 14:17:25 -0500700 virtual sk_sp<GrTexture> onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -0400701 const GrBackendFormat&,
702 GrRenderable,
703 int renderTargetSampleCnt,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400704 SkBudgeted,
705 GrProtected,
706 int mipLevelCoont,
707 uint32_t levelClearMask) = 0;
Robert Phillips9f744f72019-12-19 19:14:33 -0500708 virtual sk_sp<GrTexture> onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400709 const GrBackendFormat&,
Robert Phillips3a833922020-01-21 15:25:58 -0500710 SkBudgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400711 GrMipmapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500712 GrProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -0500713 const void* data, size_t dataSize) = 0;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400714 virtual sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&,
715 GrWrapOwnership,
716 GrWrapCacheable,
717 GrIOType) = 0;
Robert Phillipsb915c942019-12-17 14:44:37 -0500718
719 virtual sk_sp<GrTexture> onWrapCompressedBackendTexture(const GrBackendTexture&,
720 GrWrapOwnership,
721 GrWrapCacheable) = 0;
722
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400723 virtual sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
724 int sampleCnt,
725 GrWrapOwnership,
Robert Phillips0902c982019-07-16 07:47:56 -0400726 GrWrapCacheable) = 0;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400727 virtual sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) = 0;
Greg Danielb46add82019-01-02 14:51:29 -0500728 virtual sk_sp<GrRenderTarget> onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
729 const GrVkDrawableInfo&);
730
Brian Salomondbf70722019-02-07 11:31:24 -0500731 virtual sk_sp<GrGpuBuffer> onCreateBuffer(size_t size, GrGpuBufferType intendedType,
732 GrAccessPattern, const void* data) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000733
bsalomon6cb3cbe2015-07-30 07:34:27 -0700734 // overridden by backend-specific derived class to perform the surface read
Brian Salomone2078f12021-05-24 12:40:46 -0400735 virtual bool onReadPixels(GrSurface*,
736 SkIRect,
737 GrColorType surfaceColorType,
738 GrColorType dstColorType,
739 void*,
Brian Salomonf77c1462019-08-01 15:19:29 -0400740 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000741
bsalomon6cb3cbe2015-07-30 07:34:27 -0700742 // overridden by backend-specific derived class to perform the surface write
Brian Salomone2078f12021-05-24 12:40:46 -0400743 virtual bool onWritePixels(GrSurface*,
744 SkIRect,
745 GrColorType surfaceColorType,
746 GrColorType srcColorType,
747 const GrMipLevel[],
748 int mipLevelCount,
Greg Danielb20d7e52019-09-03 13:54:39 -0400749 bool prepForTexSampling) = 0;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000750
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400751 // overridden by backend-specific derived class to perform the texture transfer
Brian Salomone2078f12021-05-24 12:40:46 -0400752 virtual bool onTransferPixelsTo(GrTexture*,
753 SkIRect,
754 GrColorType textiueColorType,
755 GrColorType bufferColorType,
756 sk_sp<GrGpuBuffer> transferBuffer,
757 size_t offset,
Brian Salomonf77c1462019-08-01 15:19:29 -0400758 size_t rowBytes) = 0;
Brian Salomone2078f12021-05-24 12:40:46 -0400759
Brian Salomone05ba5a2019-04-08 11:59:07 -0400760 // overridden by backend-specific derived class to perform the surface transfer
Brian Salomone2078f12021-05-24 12:40:46 -0400761 virtual bool onTransferPixelsFrom(GrSurface*,
762 SkIRect,
763 GrColorType surfaceColorType,
764 GrColorType bufferColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500765 sk_sp<GrGpuBuffer> transferBuffer,
766 size_t offset) = 0;
jvanverth17aa0472016-01-05 10:41:27 -0800767
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000768 // overridden by backend-specific derived class to perform the resolve
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400769 virtual void onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) = 0;
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000770
Brian Salomon930f9392018-06-20 16:25:26 -0400771 // overridden by backend specific derived class to perform mip map level regeneration.
772 virtual bool onRegenerateMipMapLevels(GrTexture*) = 0;
773
joshualitt1cbdcde2015-08-21 11:53:29 -0700774 // overridden by backend specific derived class to perform the copy surface
Greg Daniel46cfbc62019-06-07 11:43:30 -0400775 virtual bool onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400776 const SkIPoint& dstPoint) = 0;
joshualitt1cbdcde2015-08-21 11:53:29 -0700777
Greg Daniel65476e02020-10-27 09:20:20 -0400778 virtual GrOpsRenderPass* onGetOpsRenderPass(
779 GrRenderTarget* renderTarget,
Chris Daltonda2b0f42021-04-13 00:19:45 -0600780 bool useMSAASurface,
Greg Daniel65476e02020-10-27 09:20:20 -0400781 GrAttachment* stencil,
782 GrSurfaceOrigin,
783 const SkIRect& bounds,
784 const GrOpsRenderPass::LoadAndStoreInfo&,
785 const GrOpsRenderPass::StencilLoadAndStoreInfo&,
786 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
787 GrXferBarrierFlags renderPassXferBarriers) = 0;
788
Greg Daniel9efe3862020-06-11 11:51:06 -0400789 virtual void prepareSurfacesForBackendAccessAndStateUpdates(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500790 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400791 SkSurface::BackendSurfaceAccess access,
792 const GrBackendSurfaceMutableState* newState) {}
Greg Danielfe159622020-04-10 17:43:51 +0000793
794 virtual bool onSubmitToGpu(bool syncCpu) = 0;
Greg Daniel51316782017-08-02 15:10:09 +0000795
Greg Danielb4327542020-11-20 10:50:50 -0500796 void reportSubmitHistograms();
797 virtual void onReportSubmitHistograms() {}
798
Kevin Lubickf4def342018-10-04 12:52:50 -0400799#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400800 virtual void onDumpJSON(SkJSONWriter*) const {}
Kevin Lubickf4def342018-10-04 12:52:50 -0400801#endif
Brian Osman71a18892017-08-10 10:23:25 -0400802
Brian Salomona56a7462020-02-07 14:17:25 -0500803 sk_sp<GrTexture> createTextureCommon(SkISize,
Robert Phillips3a833922020-01-21 15:25:58 -0500804 const GrBackendFormat&,
Greg Daniel0e9d34d2021-08-13 16:20:18 -0400805 GrTextureType textureType,
Robert Phillips3a833922020-01-21 15:25:58 -0500806 GrRenderable,
Brian Salomona90382f2019-09-17 09:01:56 -0400807 int renderTargetSampleCnt,
Robert Phillips3a833922020-01-21 15:25:58 -0500808 SkBudgeted,
809 GrProtected,
Brian Salomona90382f2019-09-17 09:01:56 -0400810 int mipLevelCnt,
811 uint32_t levelClearMask);
812
bsalomon@google.comb635d392011-11-05 12:47:43 +0000813 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000814 this->onResetContext(fResetBits);
815 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000816 }
817
Greg Daniel55822f12020-05-26 11:26:45 -0400818 void callSubmittedProcs(bool success);
819
Brian Osmancbdc2612020-11-23 15:30:48 -0500820 sk_sp<const GrCaps> fCaps;
821 // Compiler used for compiling SkSL into backend shader code. We only want to create the
822 // compiler once, as there is significant overhead to the first compile.
823 std::unique_ptr<SkSL::Compiler> fCompiler;
824
Chris Dalton535ba8d2018-02-20 09:51:59 -0700825 uint32_t fResetBits;
joshualitt3322fa42014-11-07 08:48:51 -0800826 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
Adlai Holler3d0359a2020-07-09 15:35:55 -0400827 GrDirectContext* fContext;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000828
Greg Daniel55822f12020-05-26 11:26:45 -0400829 struct SubmittedProc {
830 SubmittedProc(GrGpuSubmittedProc proc, GrGpuSubmittedContext context)
831 : fProc(proc), fContext(context) {}
832
833 GrGpuSubmittedProc fProc;
834 GrGpuSubmittedContext fContext;
835 };
836 SkSTArray<4, SubmittedProc> fSubmittedProcs;
837
Brian Salomon24069eb2020-06-24 10:19:52 -0400838 bool fOOMed = false;
839
Greg Daniel65476e02020-10-27 09:20:20 -0400840#if SK_HISTOGRAMS_ENABLED
841 int fCurrentSubmitRenderPassCount = 0;
842#endif
843
kkinnunencabe20c2015-06-01 01:37:26 -0700844 friend class GrPathRendering;
John Stiles7571f9e2020-09-02 22:42:33 -0400845 using INHERITED = SkRefCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000846};
847
848#endif