blob: 5f0928cf1f57845256bf7f3aa2778371fb4e5ba8 [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"
Adlai Hollerc2bfcff2020-11-06 15:39:36 -050015#include "src/core/SkSpan.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrSamplePatternDictionary.h"
Brian Salomon3ec1f542019-06-17 17:54:57 +000022#include "src/gpu/GrSwizzle.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrTextureProducer.h"
24#include "src/gpu/GrXferProcessor.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000025
Brian Salomon05487ab2020-12-23 20:32:22 -050026class GrAttachment;
Greg Danielbcf612b2017-05-01 13:50:58 +000027class GrBackendRenderTarget;
Greg Daniela5cb7812017-06-16 09:45:32 -040028class GrBackendSemaphore;
Brian Salomon05487ab2020-12-23 20:32:22 -050029struct GrContextOptions;
Adlai Holler3d0359a2020-07-09 15:35:55 -040030class GrDirectContext;
Brian Salomondbf70722019-02-07 11:31:24 -050031class GrGpuBuffer;
reedf9ad5582015-06-25 21:29:25 -070032class GrGLContext;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000033class GrPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000034class GrPathRenderer;
35class GrPathRendererChain;
kkinnunencabe20c2015-06-01 01:37:26 -070036class GrPathRendering;
egdaniel8dd688b2015-01-22 10:16:09 -080037class GrPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -080038class GrPrimitiveProcessor;
kkinnunencabe20c2015-06-01 01:37:26 -070039class GrRenderTarget;
Jim Van Verth7e829b22020-07-30 17:04:35 -040040class GrRingBuffer;
Greg Daniel6be35232017-03-01 17:01:09 -050041class GrSemaphore;
Greg Daniela58db7f2020-07-15 09:17:59 -040042class GrStagingBufferManager;
cdalton93a379b2016-05-11 13:58:08 -070043class GrStencilSettings;
kkinnunencabe20c2015-06-01 01:37:26 -070044class GrSurface;
45class GrTexture;
Brian Osman71a18892017-08-10 10:23:25 -040046class SkJSONWriter;
reed@google.comac10a2d2010-12-22 21:39:39 +000047
Brian Osmancbdc2612020-11-23 15:30:48 -050048namespace SkSL {
49 class Compiler;
50}
51
joshualitt3322fa42014-11-07 08:48:51 -080052class GrGpu : public SkRefCnt {
reed@google.comac10a2d2010-12-22 21:39:39 +000053public:
Adlai Holler3d0359a2020-07-09 15:35:55 -040054 GrGpu(GrDirectContext* direct);
mtklein36352bf2015-03-25 18:17:31 -070055 ~GrGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000056
Adlai Holler3d0359a2020-07-09 15:35:55 -040057 GrDirectContext* getContext() { return fContext; }
58 const GrDirectContext* getContext() const { return fContext; }
joshualitt3322fa42014-11-07 08:48:51 -080059
60 /**
61 * Gets the capabilities of the draw target.
62 */
bsalomon4b91f762015-05-19 09:29:46 -070063 const GrCaps* caps() const { return fCaps.get(); }
Robert Phillipse42edcc2017-12-13 11:50:22 -050064 sk_sp<const GrCaps> refCaps() const { return fCaps; }
joshualitt3322fa42014-11-07 08:48:51 -080065
kkinnunencabe20c2015-06-01 01:37:26 -070066 GrPathRendering* pathRendering() { return fPathRendering.get(); }
kkinnunenccdaa042014-08-20 01:36:23 -070067
Greg Daniela58db7f2020-07-15 09:17:59 -040068 virtual GrStagingBufferManager* stagingBufferManager() { return nullptr; }
69
Jim Van Verth7e829b22020-07-30 17:04:35 -040070 virtual GrRingBuffer* uniformsRingBuffer() { return nullptr; }
71
Brian Osmancbdc2612020-11-23 15:30:48 -050072 SkSL::Compiler* shaderCompiler() const { return fCompiler.get(); }
73
bsalomon6e2aad42016-04-01 11:54:31 -070074 enum class DisconnectType {
75 // No cleanup should be attempted, immediately cease making backend API calls
76 kAbandon,
77 // Free allocated resources (not known by GrResourceCache) before returning and
78 // ensure no backend backend 3D API calls will be made after disconnect() returns.
79 kCleanup,
80 };
81
Adlai Holler3d0359a2020-07-09 15:35:55 -040082 // Called by context when the underlying backend context is already or will be destroyed
83 // before GrDirectContext.
bsalomon6e2aad42016-04-01 11:54:31 -070084 virtual void disconnect(DisconnectType);
bsalomonc8dc1f72014-08-21 13:02:13 -070085
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,
134 GrRenderable renderable,
135 int renderTargetSampleCnt,
Robert Phillips3a833922020-01-21 15:25:58 -0500136 SkBudgeted budgeted,
Brian Salomona90382f2019-09-17 09:01:56 -0400137 GrProtected isProtected,
138 GrColorType textureColorType,
139 GrColorType srcColorType,
140 const GrMipLevel texels[],
Brian Salomond2a8ae22019-09-10 16:03:59 -0400141 int texelLevelCount);
cblume55f2d2d2016-02-26 13:20:48 -0800142
Brian Salomonc7dced52019-07-18 15:02:01 +0000143 /**
144 * Simplified createTexture() interface for when there is no initial texel data to upload.
145 */
Brian Salomona56a7462020-02-07 14:17:25 -0500146 sk_sp<GrTexture> createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400147 const GrBackendFormat& format,
148 GrRenderable renderable,
149 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400150 GrMipmapped mipMapped,
Brian Salomona90382f2019-09-17 09:01:56 -0400151 SkBudgeted budgeted,
152 GrProtected isProtected);
Brian Salomonc7dced52019-07-18 15:02:01 +0000153
Robert Phillips3a833922020-01-21 15:25:58 -0500154 sk_sp<GrTexture> createCompressedTexture(SkISize dimensions,
155 const GrBackendFormat& format,
156 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400157 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500158 GrProtected isProtected,
Robert Phillipse4720c62020-01-14 14:33:24 -0500159 const void* data, size_t dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400160
bsalomon@google.come269f212011-11-07 13:29:52 +0000161 /**
Brian Osman32342f02017-03-04 08:12:46 -0500162 * Implements GrResourceProvider::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000163 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400164 sk_sp<GrTexture> wrapBackendTexture(const GrBackendTexture&,
165 GrWrapOwnership,
166 GrWrapCacheable,
167 GrIOType);
Brian Salomond17f6582017-07-19 18:28:58 -0400168
Robert Phillipsb915c942019-12-17 14:44:37 -0500169 sk_sp<GrTexture> wrapCompressedBackendTexture(const GrBackendTexture&,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400170 GrWrapOwnership,
171 GrWrapCacheable);
Robert Phillipsb915c942019-12-17 14:44:37 -0500172
Brian Salomond17f6582017-07-19 18:28:58 -0400173 /**
174 * Implements GrResourceProvider::wrapRenderableBackendTexture
175 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400176 sk_sp<GrTexture> wrapRenderableBackendTexture(const GrBackendTexture&,
177 int sampleCnt,
178 GrWrapOwnership,
179 GrWrapCacheable);
bsalomon@google.come269f212011-11-07 13:29:52 +0000180
181 /**
Brian Osman32342f02017-03-04 08:12:46 -0500182 * Implements GrResourceProvider::wrapBackendRenderTarget
bsalomon@google.come269f212011-11-07 13:29:52 +0000183 */
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400184 sk_sp<GrRenderTarget> wrapBackendRenderTarget(const GrBackendRenderTarget&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000185
186 /**
Greg Danielb46add82019-01-02 14:51:29 -0500187 * Implements GrResourceProvider::wrapVulkanSecondaryCBAsRenderTarget
188 */
189 sk_sp<GrRenderTarget> wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
190 const GrVkDrawableInfo&);
191
192 /**
csmartdalton485a1202016-07-13 10:16:32 -0700193 * Creates a buffer in GPU memory. For a client-side buffer use GrBuffer::CreateCPUBacked.
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 *
cdaltone2e71c22016-04-07 18:13:29 -0700195 * @param size size of buffer to create.
196 * @param intendedType hint to the graphics subsystem about what the buffer will be used for.
197 * @param accessPattern hint to the graphics subsystem about how the data will be accessed.
cdalton1bf3e712016-04-19 10:00:02 -0700198 * @param data optional data with which to initialize the buffer.
cdaltone2e71c22016-04-07 18:13:29 -0700199 *
cdalton397536c2016-03-25 12:15:03 -0700200 * @return the buffer if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 */
Brian Salomondbf70722019-02-07 11:31:24 -0500202 sk_sp<GrGpuBuffer> createBuffer(size_t size, GrGpuBufferType intendedType,
203 GrAccessPattern accessPattern, const void* data = nullptr);
halcanary9d524f22016-03-29 09:03:52 -0700204
reed@google.comac10a2d2010-12-22 21:39:39 +0000205 /**
Greg Daniel242536f2020-02-13 14:12:46 -0500206 * Resolves MSAA. The resolveRect must already be in the native destination space.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000207 */
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400208 void resolveRenderTarget(GrRenderTarget*, const SkIRect& resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000209
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000210 /**
Brian Salomon930f9392018-06-20 16:25:26 -0400211 * Uses the base of the texture to recompute the contents of the other levels.
212 */
213 bool regenerateMipMapLevels(GrTexture*);
214
215 /**
Brian Salomon1f05d452019-02-08 12:33:08 -0500216 * If the backend API has stateful texture bindings, this resets them back to defaults.
217 */
218 void resetTextureBindings();
219
220 /**
Brian Salomon9b009bb2018-02-14 13:53:55 -0500221 * Reads a rectangle of pixels from a render target. No sRGB/linear conversions are performed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000222 *
Brian Salomonf77c1462019-08-01 15:19:29 -0400223 * @param surface The surface to read from
224 * @param left left edge of the rectangle to read (inclusive)
225 * @param top top edge of the rectangle to read (inclusive)
226 * @param width width of rectangle to read in pixels.
227 * @param height height of rectangle to read in pixels.
228 * @param surfaceColorType the color type for this use of the surface.
229 * @param dstColorType the color type of the destination buffer.
230 * @param buffer memory to read the rectangle into.
231 * @param rowBytes the number of bytes between consecutive rows. Must be a multiple of
232 * dstColorType's bytes-per-pixel. Must be tight to width if
233 * !caps->readPixelsRowBytesSupport().
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000234 *
235 * @return true if the read succeeded, false if not. The read can fail
Brian Salomon1d435302019-07-01 13:05:28 -0400236 * because of the surface doesn't support reading, the color type
237 * is not allowed for the format of the surface or if the rectangle
238 * read is not contained in the surface.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000239 */
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400240 bool readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400241 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
242 size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243
bsalomon@google.com6f379512011-11-16 20:36:03 +0000244 /**
Brian Salomon9b009bb2018-02-14 13:53:55 -0500245 * Updates the pixels in a rectangle of a surface. No sRGB/linear conversions are performed.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000246 *
Greg Danielb20d7e52019-09-03 13:54:39 -0400247 * @param surface The surface to write to.
248 * @param left left edge of the rectangle to write (inclusive)
249 * @param top top edge of the rectangle to write (inclusive)
250 * @param width width of rectangle to write in pixels.
251 * @param height height of rectangle to write in pixels.
252 * @param surfaceColorType the color type for this use of the surface.
253 * @param srcColorType the color type of the source buffer.
254 * @param texels array of mipmap levels containing texture data. Row bytes must be a
255 * multiple of srcColorType's bytes-per-pixel. Must be tight to level
256 * width if !caps->writePixelsRowBytesSupport().
257 * @param mipLevelCount number of levels in 'texels'
258 * @param prepForTexSampling After doing write pixels should the surface be prepared for texture
259 * sampling. This is currently only used by Vulkan for inline uploads
260 * to set that layout back to sampled after doing the upload. Inline
261 * uploads currently can happen between draws in a single op so it is
262 * not trivial to break up the GrOpsTask into two tasks when we see
263 * an inline upload. However, once we are able to support doing that
264 * we can remove this parameter.
Brian Salomon1d435302019-07-01 13:05:28 -0400265 *
266 * @return true if the write succeeded, false if not. The read can fail
267 * because of the surface doesn't support writing (e.g. read only),
268 * the color type is not allowed for the format of the surface or
269 * if the rectangle written is not contained in the surface.
cblume55f2d2d2016-02-26 13:20:48 -0800270 */
Brian Salomona9b04b92018-06-01 15:04:28 -0400271 bool writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400272 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400273 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling = false);
cblume55f2d2d2016-02-26 13:20:48 -0800274
275 /**
Brian Salomona9b04b92018-06-01 15:04:28 -0400276 * Helper for the case of a single level.
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500277 */
278 bool writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400279 GrColorType surfaceColorType, GrColorType srcColorType, const void* buffer,
Greg Danielb20d7e52019-09-03 13:54:39 -0400280 size_t rowBytes, bool prepForTexSampling = false) {
Brian Salomona9b04b92018-06-01 15:04:28 -0400281 GrMipLevel mipLevel = {buffer, rowBytes};
Brian Salomonf77c1462019-08-01 15:19:29 -0400282 return this->writePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400283 &mipLevel, 1, prepForTexSampling);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500284 }
285
286 /**
Brian Salomone05ba5a2019-04-08 11:59:07 -0400287 * Updates the pixels in a rectangle of a texture using a buffer. If the texture is MIP mapped,
288 * the base level is written to.
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400289 *
290 * @param texture The texture to write to.
cdalton397536c2016-03-25 12:15:03 -0700291 * @param left left edge of the rectangle to write (inclusive)
292 * @param top top edge of the rectangle to write (inclusive)
293 * @param width width of rectangle to write in pixels.
294 * @param height height of rectangle to write in pixels.
Brian Salomonf77c1462019-08-01 15:19:29 -0400295 * @param textureColorType the color type for this use of the surface.
Brian Salomonc320b152018-02-20 14:05:36 -0500296 * @param bufferColorType the color type of the transfer buffer's pixel data
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400297 * @param transferBuffer GrBuffer to read pixels from (type must be "kXferCpuToGpu")
cdalton397536c2016-03-25 12:15:03 -0700298 * @param offset offset from the start of the buffer
Brian Salomon1047a492019-07-02 12:25:21 -0400299 * @param rowBytes number of bytes between consecutive rows in the buffer. Must be a
300 * multiple of bufferColorType's bytes-per-pixel. Must be tight to width
301 * if !caps->writePixelsRowBytesSupport().
jvanverth17aa0472016-01-05 10:41:27 -0800302 */
Brian Salomone05ba5a2019-04-08 11:59:07 -0400303 bool transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400304 GrColorType textureColorType, GrColorType bufferColorType,
305 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400306
307 /**
308 * Reads the pixels from a rectangle of a surface into a buffer. Use
Greg Danielba88ab62019-07-26 09:14:01 -0400309 * GrCaps::SupportedRead::fOffsetAlignmentForTransferBuffer to determine the requirements for
310 * the buffer offset alignment. If the surface is a MIP mapped texture, the base level is read.
Brian Salomone05ba5a2019-04-08 11:59:07 -0400311 *
Brian Salomon26de56e2019-04-10 12:14:26 -0400312 * If successful the row bytes in the buffer is always:
313 * GrColorTypeBytesPerPixel(bufferColorType) * width
314 *
315 * Asserts that the caller has passed a properly aligned offset and that the buffer is
316 * large enough to hold the result
Brian Salomone05ba5a2019-04-08 11:59:07 -0400317 *
318 * @param surface The surface to read from.
319 * @param left left edge of the rectangle to read (inclusive)
320 * @param top top edge of the rectangle to read (inclusive)
321 * @param width width of rectangle to read in pixels.
322 * @param height height of rectangle to read in pixels.
Brian Salomonf77c1462019-08-01 15:19:29 -0400323 * @param surfaceColorType the color type for this use of the surface.
Brian Salomone05ba5a2019-04-08 11:59:07 -0400324 * @param bufferColorType the color type of the transfer buffer's pixel data
325 * @param transferBuffer GrBuffer to write pixels to (type must be "kXferGpuToCpu")
326 * @param offset offset from the start of the buffer
327 */
Brian Salomon26de56e2019-04-10 12:14:26 -0400328 bool transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400329 GrColorType surfaceColorType, GrColorType bufferColorType,
330 GrGpuBuffer* transferBuffer, size_t offset);
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000331
bsalomonf90a02b2014-11-26 12:28:00 -0800332 // 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 -0400333 // take place at higher levels and this function implement faster copy paths. The rect
bsalomonf90a02b2014-11-26 12:28:00 -0800334 // 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 -0400335 // src/dst bounds and non-empty. They must also be in their exact device space coords, including
336 // already being transformed for origin if need be. If canDiscardOutsideDstRect is set to true
337 // then we don't need to preserve any data on the dst surface outside of the copy.
338 bool copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400339 const SkIPoint& dstPoint);
joshualitt3322fa42014-11-07 08:48:51 -0800340
Chris Daltond7291ba2019-03-07 14:17:03 -0700341 // Queries the per-pixel HW sample locations for the given render target, and then finds or
342 // assigns a key that uniquely identifies the sample pattern. The actual sample locations can be
343 // retrieved with retrieveSampleLocations().
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600344 int findOrAssignSamplePatternKey(GrRenderTarget*);
Chris Daltond7291ba2019-03-07 14:17:03 -0700345
346 // Retrieves the per-pixel HW sample locations for the given sample pattern key, and, as a
347 // by-product, the actual number of samples in use. (This may differ from the number of samples
348 // requested by the render target.) Sample locations are returned as 0..1 offsets relative to
349 // the top-left corner of the pixel.
350 const SkTArray<SkPoint>& retrieveSampleLocations(int samplePatternKey) const {
351 return fSamplePatternDictionary.retrieveSampleLocations(samplePatternKey);
352 }
353
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400354 // Returns a GrOpsRenderPass which GrOpsTasks send draw commands to instead of directly
355 // 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,
360 GrAttachment* stencil,
361 GrSurfaceOrigin,
362 const SkIRect& bounds,
363 const GrOpsRenderPass::LoadAndStoreInfo&,
364 const GrOpsRenderPass::StencilLoadAndStoreInfo&,
365 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
366 GrXferBarrierFlags renderPassXferBarriers);
bsalomon3e791242014-12-17 13:43:13 -0800367
Robert Phillips18e94842017-05-15 13:06:44 -0400368 // Called by GrDrawingManager when flushing.
Greg Daniel51316782017-08-02 15:10:09 +0000369 // Provides a hook for post-flush actions (e.g. Vulkan command buffer submits). This will also
370 // insert any numSemaphore semaphores on the gpu and set the backendSemaphores to match the
371 // inserted semaphores.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500372 void executeFlushInfo(SkSpan<GrSurfaceProxy*>,
Greg Daniel9efe3862020-06-11 11:51:06 -0400373 SkSurface::BackendSurfaceAccess access,
374 const GrFlushInfo&,
375 const GrBackendSurfaceMutableState* newState);
Greg Danielfe159622020-04-10 17:43:51 +0000376
377 bool submitToGpu(bool syncCpu);
ethannicholas22793252016-01-30 09:59:10 -0800378
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400379 virtual void submit(GrOpsRenderPass*) = 0;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400380
Greg Daniel6be35232017-03-01 17:01:09 -0500381 virtual GrFence SK_WARN_UNUSED_RESULT insertFence() = 0;
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400382 virtual bool waitFence(GrFence) = 0;
jvanverth84741b32016-09-30 08:39:02 -0700383 virtual void deleteFence(GrFence) const = 0;
384
Greg Daniel301015c2019-11-18 14:06:46 -0500385 virtual std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(
386 bool isOwned = true) = 0;
387 virtual std::unique_ptr<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
388 GrResourceProvider::SemaphoreWrapType wrapType, GrWrapOwnership ownership) = 0;
389 virtual void insertSemaphore(GrSemaphore* semaphore) = 0;
390 virtual void waitSemaphore(GrSemaphore* semaphore) = 0;
Greg Daniel6be35232017-03-01 17:01:09 -0500391
Jim Van Verth1aaf41b2020-07-29 09:24:29 -0400392 virtual void addFinishedProc(GrGpuFinishedProc finishedProc,
393 GrGpuFinishedContext finishedContext) = 0;
Brian Salomonb0d8b762019-05-06 16:58:22 -0400394 virtual void checkFinishProcs() = 0;
395
Jim Van Verth1aaf41b2020-07-29 09:24:29 -0400396 virtual void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) {}
Greg Daniela58db7f2020-07-15 09:17:59 -0400397
Brian Osman13dddce2017-05-09 13:19:50 -0400398 /**
Brian Salomon24069eb2020-06-24 10:19:52 -0400399 * Checks if we detected an OOM from the underlying 3D API and if so returns true and resets
400 * the internal OOM state to false. Otherwise, returns false.
401 */
402 bool checkAndResetOOMed();
403
404 /**
Adlai Holler3d0359a2020-07-09 15:35:55 -0400405 * Put this texture in a safe and known state for use across multiple contexts. Depending on
Brian Osman13dddce2017-05-09 13:19:50 -0400406 * the backend, this may return a GrSemaphore. If so, other contexts should wait on that
407 * semaphore before using this texture.
408 */
Greg Daniel301015c2019-11-18 14:06:46 -0500409 virtual std::unique_ptr<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) = 0;
Brian Osman13dddce2017-05-09 13:19:50 -0400410
mtkleinb9eb4ac2015-02-02 18:26:03 -0800411 ///////////////////////////////////////////////////////////////////////////
412 // Debugging and Stats
413
414 class Stats {
415 public:
Robert Phillips19f466d2020-02-26 10:27:07 -0500416 enum class ProgramCacheResult {
417 kHit, // the program was found in the cache
418 kMiss, // the program was not found in the cache (and was, thus, compiled)
419 kPartial, // a precompiled version was found in the persistent cache
420
421 kLast = kPartial
422 };
423
424 static const int kNumProgramCacheResults = (int)ProgramCacheResult::kLast + 1;
425
mtkleinb9eb4ac2015-02-02 18:26:03 -0800426#if GR_GPU_STATS
Brian Salomone05ba5a2019-04-08 11:59:07 -0400427 Stats() = default;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800428
Brian Salomone05ba5a2019-04-08 11:59:07 -0400429 void reset() { *this = {}; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800430
431 int renderTargetBinds() const { return fRenderTargetBinds; }
432 void incRenderTargetBinds() { fRenderTargetBinds++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400433
mtkleinb9eb4ac2015-02-02 18:26:03 -0800434 int shaderCompilations() const { return fShaderCompilations; }
435 void incShaderCompilations() { fShaderCompilations++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400436
bsalomonb12ea412015-02-02 21:19:50 -0800437 int textureCreates() const { return fTextureCreates; }
438 void incTextureCreates() { fTextureCreates++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400439
bsalomonb12ea412015-02-02 21:19:50 -0800440 int textureUploads() const { return fTextureUploads; }
441 void incTextureUploads() { fTextureUploads++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400442
jvanverth17aa0472016-01-05 10:41:27 -0800443 int transfersToTexture() const { return fTransfersToTexture; }
444 void incTransfersToTexture() { fTransfersToTexture++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400445
446 int transfersFromSurface() const { return fTransfersFromSurface; }
Brian Salomone05ba5a2019-04-08 11:59:07 -0400447 void incTransfersFromSurface() { fTransfersFromSurface++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400448
449 int stencilAttachmentCreates() const { return fStencilAttachmentCreates; }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700450 void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400451
Greg Daniel5d0330e2020-10-12 16:05:21 -0400452 int msaaAttachmentCreates() const { return fMSAAAttachmentCreates; }
453 void incMSAAAttachmentCreates() { fMSAAAttachmentCreates++; }
454
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400455 int numDraws() const { return fNumDraws; }
joshualitt87a5c9f2015-09-08 13:42:05 -0700456 void incNumDraws() { fNumDraws++; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400457
458 int numFailedDraws() const { return fNumFailedDraws; }
bsalomon1d417a82016-03-23 11:50:26 -0700459 void incNumFailedDraws() { ++fNumFailedDraws; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400460
Greg Danielfe159622020-04-10 17:43:51 +0000461 int numSubmitToGpus() const { return fNumSubmitToGpus; }
462 void incNumSubmitToGpus() { ++fNumSubmitToGpus; }
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400463
464 int numScratchTexturesReused() const { return fNumScratchTexturesReused; }
465 void incNumScratchTexturesReused() { ++fNumScratchTexturesReused; }
466
Greg Daniel5d0330e2020-10-12 16:05:21 -0400467 int numScratchMSAAAttachmentsReused() const { return fNumScratchMSAAAttachmentsReused; }
468 void incNumScratchMSAAAttachmentsReused() { ++fNumScratchMSAAAttachmentsReused; }
469
Robert Phillips19f466d2020-02-26 10:27:07 -0500470 int numInlineCompilationFailures() const { return fNumInlineCompilationFailures; }
471 void incNumInlineCompilationFailures() { ++fNumInlineCompilationFailures; }
472
473 int numInlineProgramCacheResult(ProgramCacheResult stat) const {
474 return fInlineProgramCacheStats[(int) stat];
475 }
476 void incNumInlineProgramCacheResult(ProgramCacheResult stat) {
477 ++fInlineProgramCacheStats[(int) stat];
478 }
479
480 int numPreCompilationFailures() const { return fNumPreCompilationFailures; }
481 void incNumPreCompilationFailures() { ++fNumPreCompilationFailures; }
482
483 int numPreProgramCacheResult(ProgramCacheResult stat) const {
484 return fPreProgramCacheStats[(int) stat];
485 }
486 void incNumPreProgramCacheResult(ProgramCacheResult stat) {
487 ++fPreProgramCacheStats[(int) stat];
488 }
489
490 int numCompilationFailures() const { return fNumCompilationFailures; }
491 void incNumCompilationFailures() { ++fNumCompilationFailures; }
492
493 int numPartialCompilationSuccesses() const { return fNumPartialCompilationSuccesses; }
494 void incNumPartialCompilationSuccesses() { ++fNumPartialCompilationSuccesses; }
495
496 int numCompilationSuccesses() const { return fNumCompilationSuccesses; }
497 void incNumCompilationSuccesses() { ++fNumCompilationSuccesses; }
498
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500499#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800500 void dump(SkString*);
joshualitte45c81c2015-12-02 09:05:37 -0800501 void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500502#endif
mtkleinb9eb4ac2015-02-02 18:26:03 -0800503 private:
Brian Salomone05ba5a2019-04-08 11:59:07 -0400504 int fRenderTargetBinds = 0;
505 int fShaderCompilations = 0;
506 int fTextureCreates = 0;
507 int fTextureUploads = 0;
508 int fTransfersToTexture = 0;
509 int fTransfersFromSurface = 0;
510 int fStencilAttachmentCreates = 0;
Greg Daniel5d0330e2020-10-12 16:05:21 -0400511 int fMSAAAttachmentCreates = 0;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400512 int fNumDraws = 0;
513 int fNumFailedDraws = 0;
Greg Danielfe159622020-04-10 17:43:51 +0000514 int fNumSubmitToGpus = 0;
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400515 int fNumScratchTexturesReused = 0;
Greg Daniel5d0330e2020-10-12 16:05:21 -0400516 int fNumScratchMSAAAttachmentsReused = 0;
Robert Phillips19f466d2020-02-26 10:27:07 -0500517
518 int fNumInlineCompilationFailures = 0;
519 int fInlineProgramCacheStats[kNumProgramCacheResults] = { 0 };
520
521 int fNumPreCompilationFailures = 0;
522 int fPreProgramCacheStats[kNumProgramCacheResults] = { 0 };
523
524 int fNumCompilationFailures = 0;
525 int fNumPartialCompilationSuccesses = 0;
526 int fNumCompilationSuccesses = 0;
527
mtkleinb9eb4ac2015-02-02 18:26:03 -0800528#else
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500529
530#if GR_TEST_UTILS
joshualitte45c81c2015-12-02 09:05:37 -0800531 void dump(SkString*) {}
532 void dumpKeyValuePairs(SkTArray<SkString>*, SkTArray<double>*) {}
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500533#endif
mtkleinb9eb4ac2015-02-02 18:26:03 -0800534 void incRenderTargetBinds() {}
535 void incShaderCompilations() {}
bsalomonb12ea412015-02-02 21:19:50 -0800536 void incTextureCreates() {}
537 void incTextureUploads() {}
jvanverth17aa0472016-01-05 10:41:27 -0800538 void incTransfersToTexture() {}
Greg Daniela28ea672020-09-25 11:12:56 -0400539 void incTransfersFromSurface() {}
egdaniel8dc7c3a2015-04-16 11:22:42 -0700540 void incStencilAttachmentCreates() {}
Greg Daniel5d0330e2020-10-12 16:05:21 -0400541 void incMSAAAttachmentCreates() {}
joshualitt87a5c9f2015-09-08 13:42:05 -0700542 void incNumDraws() {}
bsalomon1d417a82016-03-23 11:50:26 -0700543 void incNumFailedDraws() {}
Greg Danielfe159622020-04-10 17:43:51 +0000544 void incNumSubmitToGpus() {}
Greg Daniela28ea672020-09-25 11:12:56 -0400545 void incNumScratchTexturesReused() {}
Greg Daniel5d0330e2020-10-12 16:05:21 -0400546 void incNumScratchMSAAAttachmentsReused() {}
Robert Phillips19f466d2020-02-26 10:27:07 -0500547 void incNumInlineCompilationFailures() {}
548 void incNumInlineProgramCacheResult(ProgramCacheResult stat) {}
549 void incNumPreCompilationFailures() {}
550 void incNumPreProgramCacheResult(ProgramCacheResult stat) {}
551 void incNumCompilationFailures() {}
552 void incNumPartialCompilationSuccesses() {}
553 void incNumCompilationSuccesses() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800554#endif
555 };
556
557 Stats* stats() { return &fStats; }
Brian Osman71a18892017-08-10 10:23:25 -0400558 void dumpJSON(SkJSONWriter*) const;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800559
Robert Phillips42716d42019-12-16 12:19:54 -0500560 /** Used to initialize a backend texture with either a constant color, pixmaps or
561 * compressed data.
562 */
Brian Salomon85c3d682019-11-04 15:04:54 -0500563 class BackendTextureData {
564 public:
Robert Phillips42716d42019-12-16 12:19:54 -0500565 enum class Type { kColor, kPixmaps, kCompressed };
Brian Salomon85c3d682019-11-04 15:04:54 -0500566 BackendTextureData() = default;
567 BackendTextureData(const SkColor4f& color) : fType(Type::kColor), fColor(color) {}
Brian Salomon05487ab2020-12-23 20:32:22 -0500568 BackendTextureData(const GrPixmap pixmaps[]) : fType(Type::kPixmaps), fPixmaps(pixmaps) {
Brian Salomon85c3d682019-11-04 15:04:54 -0500569 SkASSERT(pixmaps);
570 }
Robert Phillips42716d42019-12-16 12:19:54 -0500571 BackendTextureData(const void* data, size_t size) : fType(Type::kCompressed) {
572 SkASSERT(data);
573 fCompressed.fData = data;
574 fCompressed.fSize = size;
575 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500576
577 Type type() const { return fType; }
578 SkColor4f color() const {
579 SkASSERT(this->type() == Type::kColor);
580 return fColor;
581 }
582
Brian Salomon05487ab2020-12-23 20:32:22 -0500583 const GrPixmap& pixmap(int i) const {
Robert Phillips42716d42019-12-16 12:19:54 -0500584 SkASSERT(this->type() == Type::kPixmaps);
585 return fPixmaps[i];
586 }
Brian Salomon05487ab2020-12-23 20:32:22 -0500587 const GrPixmap* pixmaps() const {
Robert Phillips42716d42019-12-16 12:19:54 -0500588 SkASSERT(this->type() == Type::kPixmaps);
589 return fPixmaps;
590 }
591
592 const void* compressedData() const {
593 SkASSERT(this->type() == Type::kCompressed);
594 return fCompressed.fData;
595 }
596 size_t compressedSize() const {
597 SkASSERT(this->type() == Type::kCompressed);
598 return fCompressed.fSize;
599 }
600
Brian Salomon85c3d682019-11-04 15:04:54 -0500601 private:
602 Type fType = Type::kColor;
603 union {
604 SkColor4f fColor = {0, 0, 0, 0};
Brian Salomon05487ab2020-12-23 20:32:22 -0500605 const GrPixmap* fPixmaps;
Robert Phillips42716d42019-12-16 12:19:54 -0500606 struct {
607 const void* fData;
608 size_t fSize;
609 } fCompressed;
Brian Salomon85c3d682019-11-04 15:04:54 -0500610 };
611 };
612
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400613 /**
614 * Creates a texture directly in the backend API without wrapping it in a GrTexture.
615 * Must be matched with a call to deleteBackendTexture().
Robert Phillips57ef6802019-09-23 10:12:47 -0400616 *
Brian Salomon85c3d682019-11-04 15:04:54 -0500617 * If data is null the texture is uninitialized.
Robert Phillips57ef6802019-09-23 10:12:47 -0400618 *
Brian Salomon85c3d682019-11-04 15:04:54 -0500619 * If data represents a color then all texture levels are cleared to that color.
620 *
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500621 * If data represents pixmaps then it must have a either one pixmap or, if mipmapping
622 * is specified, a complete MIP hierarchy of pixmaps. Additionally, if provided, the mip
623 * levels must be sized correctly according to the MIP sizes implied by dimensions. They
624 * must all have the same color type and that color type must be compatible with the
625 * texture format.
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400626 */
Brian Salomon85c3d682019-11-04 15:04:54 -0500627 GrBackendTexture createBackendTexture(SkISize dimensions,
628 const GrBackendFormat&,
629 GrRenderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400630 GrMipmapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400631 GrProtected);
632
633 bool updateBackendTexture(const GrBackendTexture&,
Greg Daniel25597782020-06-11 13:15:08 -0400634 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400635 const BackendTextureData*);
Robert Phillipsb915c942019-12-17 14:44:37 -0500636
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500637 /**
638 * Same as the createBackendTexture case except compressed backend textures can
639 * never be renderable.
640 */
Robert Phillipsb915c942019-12-17 14:44:37 -0500641 GrBackendTexture createCompressedBackendTexture(SkISize dimensions,
642 const GrBackendFormat&,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400643 GrMipmapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400644 GrProtected);
645
646 bool updateCompressedBackendTexture(const GrBackendTexture&,
647 sk_sp<GrRefCntedCallback> finishedCallback,
648 const BackendTextureData*);
Robert Phillips646f6372018-09-25 09:31:10 -0400649
Greg Daniel1db8e792020-06-09 17:29:32 -0400650 virtual bool setBackendTextureState(const GrBackendTexture&,
651 const GrBackendSurfaceMutableState&,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400652 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400653 sk_sp<GrRefCntedCallback> finishedCallback) {
654 return false;
655 }
656
657 virtual bool setBackendRenderTargetState(const GrBackendRenderTarget&,
658 const GrBackendSurfaceMutableState&,
Greg Daniel1d3c8c12020-09-23 14:23:36 -0400659 GrBackendSurfaceMutableState* previousState,
Greg Daniel1db8e792020-06-09 17:29:32 -0400660 sk_sp<GrRefCntedCallback> finishedCallback) {
661 return false;
662 }
663
Brian Salomone64b0642018-03-07 11:47:54 -0500664 /**
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400665 * Frees a texture created by createBackendTexture(). If ownership of the backend
Adlai Holler3d0359a2020-07-09 15:35:55 -0400666 * texture has been transferred to a context using adopt semantics this should not be called.
Brian Salomone64b0642018-03-07 11:47:54 -0500667 */
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400668 virtual void deleteBackendTexture(const GrBackendTexture&) = 0;
jvanverth672bb7f2015-07-13 07:19:57 -0700669
Robert Phillips979b2232020-02-20 10:47:29 -0500670 /**
671 * In this case we have a program descriptor and a program info but no render target.
672 */
673 virtual bool compile(const GrProgramDesc&, const GrProgramInfo&) = 0;
674
Brian Osmaned58e002019-09-06 14:42:43 -0400675 virtual bool precompileShader(const SkData& key, const SkData& data) { return false; }
676
Robert Phillipsf0ced622019-05-16 09:06:25 -0400677#if GR_TEST_UTILS
678 /** Check a handle represents an actual texture in the backend API that has not been freed. */
679 virtual bool isTestingOnlyBackendTexture(const GrBackendTexture&) const = 0;
680
Brian Salomon72c7b982020-10-06 10:07:38 -0400681 /**
682 * Creates a GrBackendRenderTarget that can be wrapped using
683 * SkSurface::MakeFromBackendRenderTarget. Ideally this is a non-textureable allocation to
684 * differentiate from testing with SkSurface::MakeFromBackendTexture. When sampleCnt > 1 this
685 * is used to test client wrapped allocations with MSAA where Skia does not allocate a separate
686 * buffer for resolving. If the color is non-null the backing store should be cleared to the
687 * passed in color.
688 */
Brian Salomonf9b00422020-10-08 16:00:14 -0400689 virtual GrBackendRenderTarget createTestingOnlyBackendRenderTarget(
690 SkISize dimensions,
691 GrColorType,
692 int sampleCount = 1,
693 GrProtected = GrProtected::kNo) = 0;
Brian Salomonf865b052018-03-09 09:01:53 -0500694
Brian Salomon72c7b982020-10-06 10:07:38 -0400695 /**
696 * Deletes a GrBackendRenderTarget allocated with the above. Synchronization to make this safe
697 * is up to the caller.
698 */
Brian Salomonf865b052018-03-09 09:01:53 -0500699 virtual void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) = 0;
700
701 // This is only to be used in GL-specific tests.
702 virtual const GrGLContext* glContextForTesting() const { return nullptr; }
703
704 // This is only to be used by testing code
705 virtual void resetShaderCacheForTesting() const {}
706
Greg Daniel26b50a42018-03-08 09:49:58 -0500707 /**
708 * Flushes all work to the gpu and forces the GPU to wait until all the gpu work has completed.
709 * This is for testing purposes only.
710 */
711 virtual void testingOnly_flushGpuAndSync() = 0;
Jim Van Verth052761f2019-09-27 15:05:51 -0400712
713 /**
714 * Inserted as a pair around a block of code to do a GPU frame capture.
715 * Currently only works with the Metal backend.
716 */
717 virtual void testingOnly_startCapture() {}
718 virtual void testingOnly_endCapture() {}
Brian Salomonf865b052018-03-09 09:01:53 -0500719#endif
Greg Daniel26b50a42018-03-08 09:49:58 -0500720
egdanielec00d942015-09-14 12:56:10 -0700721 // width and height may be larger than rt (if underlying API allows it).
722 // Returns nullptr if compatible sb could not be created, otherwise the caller owns the ref on
Greg Danielc0d69152020-10-08 14:59:00 -0400723 // the GrAttachment.
724 virtual sk_sp<GrAttachment> makeStencilAttachmentForRenderTarget(const GrRenderTarget*,
725 SkISize dimensions,
726 int numStencilSamples) = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800727
Greg Danielb8949bd2020-10-12 15:21:02 -0400728 virtual GrBackendFormat getPreferredStencilFormat(const GrBackendFormat&) = 0;
729
Greg Daniel5d0330e2020-10-12 16:05:21 -0400730 // Creates an MSAA surface to be used as an MSAA attachment on a framebuffer.
731 virtual sk_sp<GrAttachment> makeMSAAAttachment(SkISize dimensions,
732 const GrBackendFormat& format,
733 int numSamples,
734 GrProtected isProtected) = 0;
735
egdaniel9cb63402016-06-23 08:37:05 -0700736 void handleDirtyContext() {
737 if (fResetBits) {
738 this->resetContext();
739 }
740 }
741
Greg Daniela870b462019-01-08 15:49:46 -0500742 virtual void storeVkPipelineCacheData() {}
743
Chris Daltone1196c52019-12-28 14:31:09 -0700744 // http://skbug.com/9739
745 virtual void insertManualFramebufferBarrier() {
746 SkASSERT(!this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
747 SK_ABORT("Manual framebuffer barrier not supported.");
748 }
749
Chris Dalton2e7ed262020-02-21 15:17:59 -0700750 // Called before certain draws in order to guarantee coherent results from dst reads.
751 virtual void xferBarrier(GrRenderTarget*, GrXferBarrierType) = 0;
752
joshualittd53a8272014-11-10 16:03:14 -0800753protected:
Brian Salomon7e67dca2020-07-21 09:27:25 -0400754 static bool MipMapsAreCorrect(SkISize dimensions, GrMipmapped, const BackendTextureData*);
Robert Phillipsb915c942019-12-17 14:44:37 -0500755 static bool CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400756 GrMipmapped, const BackendTextureData*);
Robert Phillips57ef6802019-09-23 10:12:47 -0400757
Brian Salomon1fabd512018-02-09 09:54:25 -0500758 // Handles cases where a surface will be updated without a call to flushRenderTarget.
759 void didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
760 uint32_t mipLevels = 1) const;
jvanverth900bd4a2016-04-29 13:53:12 -0700761
Brian Salomon24069eb2020-06-24 10:19:52 -0400762 void setOOMed() { fOOMed = true; }
763
Ben Wagner145dbcd2016-11-03 14:40:50 -0400764 Stats fStats;
765 std::unique_ptr<GrPathRendering> fPathRendering;
Brian Osmancbdc2612020-11-23 15:30:48 -0500766
767 // Subclass must call this to initialize caps & compiler in its constructor.
768 void initCapsAndCompiler(sk_sp<const GrCaps> caps);
joshualitt3322fa42014-11-07 08:48:51 -0800769
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000770private:
Brian Salomon85c3d682019-11-04 15:04:54 -0500771 virtual GrBackendTexture onCreateBackendTexture(SkISize dimensions,
772 const GrBackendFormat&,
773 GrRenderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400774 GrMipmapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400775 GrProtected) = 0;
Robert Phillips57ef6802019-09-23 10:12:47 -0400776
Greg Danielc1ad77c2020-05-06 11:40:03 -0400777 virtual GrBackendTexture onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400778 SkISize dimensions, const GrBackendFormat&, GrMipmapped, GrProtected) = 0;
Robert Phillipsb915c942019-12-17 14:44:37 -0500779
Greg Daniel16032b32020-05-06 15:31:10 -0400780 virtual bool onUpdateBackendTexture(const GrBackendTexture&,
781 sk_sp<GrRefCntedCallback> finishedCallback,
782 const BackendTextureData*) = 0;
783
Greg Danielaaf738c2020-07-10 09:30:33 -0400784 virtual bool onUpdateCompressedBackendTexture(const GrBackendTexture&,
785 sk_sp<GrRefCntedCallback> finishedCallback,
786 const BackendTextureData*) = 0;
787
bsalomon@google.comb635d392011-11-05 12:47:43 +0000788 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000789 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000790 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000791
Brian Salomon1f05d452019-02-08 12:33:08 -0500792 // Implementation of resetTextureBindings.
793 virtual void onResetTextureBindings() {}
794
Chris Daltond7291ba2019-03-07 14:17:03 -0700795 // Queries the effective number of samples in use by the hardware for the given render target,
796 // and queries the individual sample locations.
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600797 virtual void querySampleLocations(GrRenderTarget*, SkTArray<SkPoint>*) = 0;
Chris Daltond7291ba2019-03-07 14:17:03 -0700798
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000799 // overridden by backend-specific derived class to create objects.
Brian Salomond2a8ae22019-09-10 16:03:59 -0400800 // Texture size, renderablility, format support, sample count will have already been validated
801 // in base class before onCreateTexture is called.
802 // If the ith bit is set in levelClearMask then the ith MIP level should be cleared.
Brian Salomona56a7462020-02-07 14:17:25 -0500803 virtual sk_sp<GrTexture> onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -0400804 const GrBackendFormat&,
805 GrRenderable,
806 int renderTargetSampleCnt,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400807 SkBudgeted,
808 GrProtected,
809 int mipLevelCoont,
810 uint32_t levelClearMask) = 0;
Robert Phillips9f744f72019-12-19 19:14:33 -0500811 virtual sk_sp<GrTexture> onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400812 const GrBackendFormat&,
Robert Phillips3a833922020-01-21 15:25:58 -0500813 SkBudgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400814 GrMipmapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500815 GrProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -0500816 const void* data, size_t dataSize) = 0;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400817 virtual sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&,
818 GrWrapOwnership,
819 GrWrapCacheable,
820 GrIOType) = 0;
Robert Phillipsb915c942019-12-17 14:44:37 -0500821
822 virtual sk_sp<GrTexture> onWrapCompressedBackendTexture(const GrBackendTexture&,
823 GrWrapOwnership,
824 GrWrapCacheable) = 0;
825
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400826 virtual sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
827 int sampleCnt,
828 GrWrapOwnership,
Robert Phillips0902c982019-07-16 07:47:56 -0400829 GrWrapCacheable) = 0;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400830 virtual sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) = 0;
Greg Danielb46add82019-01-02 14:51:29 -0500831 virtual sk_sp<GrRenderTarget> onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
832 const GrVkDrawableInfo&);
833
Brian Salomondbf70722019-02-07 11:31:24 -0500834 virtual sk_sp<GrGpuBuffer> onCreateBuffer(size_t size, GrGpuBufferType intendedType,
835 GrAccessPattern, const void* data) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000836
bsalomon6cb3cbe2015-07-30 07:34:27 -0700837 // overridden by backend-specific derived class to perform the surface read
Brian Salomonf77c1462019-08-01 15:19:29 -0400838 virtual bool onReadPixels(GrSurface*, int left, int top, int width, int height,
839 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
840 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000841
bsalomon6cb3cbe2015-07-30 07:34:27 -0700842 // overridden by backend-specific derived class to perform the surface write
Brian Salomonf77c1462019-08-01 15:19:29 -0400843 virtual bool onWritePixels(GrSurface*, int left, int top, int width, int height,
844 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400845 const GrMipLevel texels[], int mipLevelCount,
846 bool prepForTexSampling) = 0;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000847
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400848 // overridden by backend-specific derived class to perform the texture transfer
Brian Salomone05ba5a2019-04-08 11:59:07 -0400849 virtual bool onTransferPixelsTo(GrTexture*, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400850 GrColorType textiueColorType, GrColorType bufferColorType,
851 GrGpuBuffer* transferBuffer, size_t offset,
852 size_t rowBytes) = 0;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400853 // overridden by backend-specific derived class to perform the surface transfer
Brian Salomon26de56e2019-04-10 12:14:26 -0400854 virtual bool onTransferPixelsFrom(GrSurface*, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400855 GrColorType surfaceColorType, GrColorType bufferColorType,
856 GrGpuBuffer* transferBuffer, size_t offset) = 0;
jvanverth17aa0472016-01-05 10:41:27 -0800857
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000858 // overridden by backend-specific derived class to perform the resolve
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400859 virtual void onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) = 0;
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000860
Brian Salomon930f9392018-06-20 16:25:26 -0400861 // overridden by backend specific derived class to perform mip map level regeneration.
862 virtual bool onRegenerateMipMapLevels(GrTexture*) = 0;
863
joshualitt1cbdcde2015-08-21 11:53:29 -0700864 // overridden by backend specific derived class to perform the copy surface
Greg Daniel46cfbc62019-06-07 11:43:30 -0400865 virtual bool onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400866 const SkIPoint& dstPoint) = 0;
joshualitt1cbdcde2015-08-21 11:53:29 -0700867
Greg Daniel65476e02020-10-27 09:20:20 -0400868 virtual GrOpsRenderPass* onGetOpsRenderPass(
869 GrRenderTarget* renderTarget,
870 GrAttachment* stencil,
871 GrSurfaceOrigin,
872 const SkIRect& bounds,
873 const GrOpsRenderPass::LoadAndStoreInfo&,
874 const GrOpsRenderPass::StencilLoadAndStoreInfo&,
875 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
876 GrXferBarrierFlags renderPassXferBarriers) = 0;
877
Greg Daniel9efe3862020-06-11 11:51:06 -0400878 virtual void prepareSurfacesForBackendAccessAndStateUpdates(
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500879 SkSpan<GrSurfaceProxy*> proxies,
Greg Daniel9efe3862020-06-11 11:51:06 -0400880 SkSurface::BackendSurfaceAccess access,
881 const GrBackendSurfaceMutableState* newState) {}
Greg Danielfe159622020-04-10 17:43:51 +0000882
883 virtual bool onSubmitToGpu(bool syncCpu) = 0;
Greg Daniel51316782017-08-02 15:10:09 +0000884
Greg Danielb4327542020-11-20 10:50:50 -0500885 void reportSubmitHistograms();
886 virtual void onReportSubmitHistograms() {}
887
Kevin Lubickf4def342018-10-04 12:52:50 -0400888#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400889 virtual void onDumpJSON(SkJSONWriter*) const {}
Kevin Lubickf4def342018-10-04 12:52:50 -0400890#endif
Brian Osman71a18892017-08-10 10:23:25 -0400891
Brian Salomona56a7462020-02-07 14:17:25 -0500892 sk_sp<GrTexture> createTextureCommon(SkISize,
Robert Phillips3a833922020-01-21 15:25:58 -0500893 const GrBackendFormat&,
894 GrRenderable,
Brian Salomona90382f2019-09-17 09:01:56 -0400895 int renderTargetSampleCnt,
Robert Phillips3a833922020-01-21 15:25:58 -0500896 SkBudgeted,
897 GrProtected,
Brian Salomona90382f2019-09-17 09:01:56 -0400898 int mipLevelCnt,
899 uint32_t levelClearMask);
900
bsalomon@google.comb635d392011-11-05 12:47:43 +0000901 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000902 this->onResetContext(fResetBits);
903 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000904 }
905
Greg Daniel55822f12020-05-26 11:26:45 -0400906 void callSubmittedProcs(bool success);
907
Brian Osmancbdc2612020-11-23 15:30:48 -0500908 sk_sp<const GrCaps> fCaps;
909 // Compiler used for compiling SkSL into backend shader code. We only want to create the
910 // compiler once, as there is significant overhead to the first compile.
911 std::unique_ptr<SkSL::Compiler> fCompiler;
912
Chris Dalton535ba8d2018-02-20 09:51:59 -0700913 uint32_t fResetBits;
joshualitt3322fa42014-11-07 08:48:51 -0800914 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
Adlai Holler3d0359a2020-07-09 15:35:55 -0400915 GrDirectContext* fContext;
Chris Daltond7291ba2019-03-07 14:17:03 -0700916 GrSamplePatternDictionary fSamplePatternDictionary;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000917
Greg Daniel55822f12020-05-26 11:26:45 -0400918 struct SubmittedProc {
919 SubmittedProc(GrGpuSubmittedProc proc, GrGpuSubmittedContext context)
920 : fProc(proc), fContext(context) {}
921
922 GrGpuSubmittedProc fProc;
923 GrGpuSubmittedContext fContext;
924 };
925 SkSTArray<4, SubmittedProc> fSubmittedProcs;
926
Brian Salomon24069eb2020-06-24 10:19:52 -0400927 bool fOOMed = false;
928
Greg Daniel65476e02020-10-27 09:20:20 -0400929#if SK_HISTOGRAMS_ENABLED
930 int fCurrentSubmitRenderPassCount = 0;
931#endif
932
kkinnunencabe20c2015-06-01 01:37:26 -0700933 friend class GrPathRendering;
John Stiles7571f9e2020-09-02 22:42:33 -0400934 using INHERITED = SkRefCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000935};
936
937#endif