blob: e28a989d94dae8b8d49c0323633894721cb4ae6d [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
Adlai Holler3d0359a2020-07-09 15:35:55 -040013#include "include/gpu/GrDirectContext.h"
Robert Phillips99dead92020-01-27 16:11:57 -050014#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkMathPriv.h"
Mike Reed13711eb2020-07-14 17:16:32 -040016#include "src/core/SkMipmap.h"
Greg Danielc0d69152020-10-08 14:59:00 -040017#include "src/gpu/GrAttachment.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrAuditTrail.h"
Greg Daniel01f278c2020-06-12 16:58:17 -040019#include "src/gpu/GrBackendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrCaps.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040021#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040022#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060024#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrPathRendering.h"
26#include "src/gpu/GrPipeline.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040027#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrResourceCache.h"
29#include "src/gpu/GrResourceProvider.h"
Jim Van Verth7e829b22020-07-30 17:04:35 -040030#include "src/gpu/GrRingBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrSemaphore.h"
Greg Daniela58db7f2020-07-15 09:17:59 -040032#include "src/gpu/GrStagingBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrStencilSettings.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrTextureProxyPriv.h"
35#include "src/gpu/GrTracing.h"
Brian Osmancbdc2612020-11-23 15:30:48 -050036#include "src/sksl/SkSLCompiler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070038
bsalomon@google.comd302f142011-03-03 13:54:13 +000039////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000040
Adlai Holler3d0359a2020-07-09 15:35:55 -040041GrGpu::GrGpu(GrDirectContext* direct) : fResetBits(kAll_GrBackendState), fContext(direct) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000042
Stephen Whitef3d5d442020-04-08 10:35:58 -040043GrGpu::~GrGpu() {
Greg Daniel55822f12020-05-26 11:26:45 -040044 this->callSubmittedProcs(false);
Stephen Whitef3d5d442020-04-08 10:35:58 -040045}
bsalomon1d89ddc2014-08-19 14:20:58 -070046
Brian Osmancbdc2612020-11-23 15:30:48 -050047void GrGpu::initCapsAndCompiler(sk_sp<const GrCaps> caps) {
48 fCaps = std::move(caps);
49 fCompiler = std::make_unique<SkSL::Compiler>(fCaps->shaderCaps());
50}
51
Greg Daniela58db7f2020-07-15 09:17:59 -040052void GrGpu::disconnect(DisconnectType type) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000053
bsalomon@google.comd302f142011-03-03 13:54:13 +000054////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000055
Brian Salomona56a7462020-02-07 14:17:25 -050056static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040057 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040058 SkASSERT(mipLevelCount > 0);
59 bool hasBasePixels = texels[0].fPixels;
60 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040061 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050062 int w = dimensions.fWidth;
63 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040064 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
65 if (texels[currentMipLevel].fPixels) {
66 const size_t minRowBytes = w * bpp;
67 if (caps->writePixelsRowBytesSupport()) {
68 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
69 return false;
70 }
71 if (texels[currentMipLevel].fRowBytes % bpp) {
72 return false;
73 }
74 } else {
75 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
76 return false;
77 }
78 }
79 ++levelsWithPixelsCnt;
80 }
81 if (w == 1 && h == 1) {
82 if (currentMipLevel != mipLevelCount - 1) {
83 return false;
84 }
85 } else {
86 w = std::max(w / 2, 1);
87 h = std::max(h / 2, 1);
88 }
89 }
90 // Either just a base layer or a full stack is required.
91 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
92 return false;
93 }
94 // Can specify just the base, all levels, or no levels.
95 if (!hasBasePixels) {
96 return levelsWithPixelsCnt == 0;
97 }
Brian Salomond2a8ae22019-09-10 16:03:59 -040098 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -040099}
100
Brian Salomona56a7462020-02-07 14:17:25 -0500101sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400102 const GrBackendFormat& format,
103 GrRenderable renderable,
104 int renderTargetSampleCnt,
105 SkBudgeted budgeted,
106 GrProtected isProtected,
107 int mipLevelCount,
108 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400109 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400110 // Call GrGpu::createCompressedTexture.
111 return nullptr;
112 }
cblume55f2d2d2016-02-26 13:20:48 -0800113
Brian Salomon7e67dca2020-07-21 09:27:25 -0400114 GrMipmapped mipMapped = mipLevelCount > 1 ? GrMipmapped::kYes : GrMipmapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500115 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
116 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700117 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700118 }
119
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400120 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400121 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400122 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500123 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400124 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400125 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400126 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500127 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400128 format,
129 renderable,
130 renderTargetSampleCnt,
131 budgeted,
132 isProtected,
133 mipLevelCount,
134 levelClearMask);
135 if (tex) {
136 SkASSERT(tex->backendFormat() == format);
137 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
138 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
139 tex->resourcePriv().removeScratchKey();
140 }
141 fStats.incTextureCreates();
142 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
143 SkASSERT(GrRenderable::kYes == renderable);
144 tex->asRenderTarget()->setRequiresManualMSAAResolve();
145 }
146 }
147 return tex;
148}
149
Brian Salomona56a7462020-02-07 14:17:25 -0500150sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400151 const GrBackendFormat& format,
152 GrRenderable renderable,
153 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400154 GrMipmapped mipMapped,
Brian Salomona90382f2019-09-17 09:01:56 -0400155 SkBudgeted budgeted,
156 GrProtected isProtected) {
157 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400158 if (mipMapped == GrMipmapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500159 mipLevelCount =
160 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400161 }
162 uint32_t levelClearMask =
163 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500164 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
165 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400166 if (tex && mipMapped == GrMipmapped::kYes && levelClearMask) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400167 tex->markMipmapsClean();
Brian Salomona90382f2019-09-17 09:01:56 -0400168 }
169 return tex;
170}
171
Brian Salomona56a7462020-02-07 14:17:25 -0500172sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400173 const GrBackendFormat& format,
174 GrRenderable renderable,
175 int renderTargetSampleCnt,
176 SkBudgeted budgeted,
177 GrProtected isProtected,
178 GrColorType textureColorType,
179 GrColorType srcColorType,
180 const GrMipLevel texels[],
181 int texelLevelCount) {
182 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400183 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500184 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400185 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400186 return nullptr;
187 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400188 }
189
Brian Osman788b9162020-02-07 10:36:46 -0500190 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400191 uint32_t levelClearMask = 0;
192 if (this->caps()->shouldInitializeTextures()) {
193 if (texelLevelCount) {
194 for (int i = 0; i < mipLevelCount; ++i) {
195 if (!texels->fPixels) {
196 levelClearMask |= static_cast<uint32_t>(1 << i);
197 }
198 }
199 } else {
200 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
201 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400202 }
203
Brian Salomona56a7462020-02-07 14:17:25 -0500204 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
205 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800206 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400207 bool markMipLevelsClean = false;
208 // Currently if level 0 does not have pixels then no other level may, as enforced by
209 // validate_texel_levels.
210 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500211 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
212 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400213 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800214 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400215 // Currently if level[1] of mip map has pixel data then so must all other levels.
216 // as enforced by validate_texel_levels.
217 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
218 fStats.incTextureUploads();
219 } else if (levelClearMask && mipLevelCount > 1) {
220 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800221 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400222 if (markMipLevelsClean) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400223 tex->markMipmapsClean();
Brian Salomond2a8ae22019-09-10 16:03:59 -0400224 }
bsalomonb12ea412015-02-02 21:19:50 -0800225 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000226 return tex;
227}
228
Robert Phillips9f744f72019-12-19 19:14:33 -0500229sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400230 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500231 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400232 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500233 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500234 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400235 size_t dataSize) {
236 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500237 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
238 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400239 return nullptr;
240 }
Brian Salomona3e29962019-07-16 11:52:08 -0400241 // Note if we relax the requirement that data must be provided then we must check
242 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400243 if (!data) {
244 return nullptr;
245 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400246 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400247 return nullptr;
248 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500249
250 // TODO: expand CompressedDataIsCorrect to work here too
Greg Daniel01f278c2020-06-12 16:58:17 -0400251 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillips9f744f72019-12-19 19:14:33 -0500252
Robert Phillips99dead92020-01-27 16:11:57 -0500253 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400254 mipMapped == GrMipmapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400255 return nullptr;
256 }
Robert Phillips3a833922020-01-21 15:25:58 -0500257 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
258 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400259}
260
Greg Daniel7ef28f32017-04-20 16:41:55 +0000261sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400262 GrWrapOwnership ownership,
263 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500264 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500265 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000266 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400267
268 const GrCaps* caps = this->caps();
269 SkASSERT(caps);
270
Greg Daniel7bfc9132019-08-14 14:23:53 -0400271 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800272 return nullptr;
273 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400274 if (backendTex.width() > caps->maxTextureSize() ||
275 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800276 return nullptr;
277 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400278
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400279 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400280}
Eric Karl5c779752017-05-08 12:02:07 -0700281
Robert Phillipsb915c942019-12-17 14:44:37 -0500282sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
283 GrWrapOwnership ownership,
284 GrWrapCacheable cacheable) {
285 this->handleDirtyContext();
286
287 const GrCaps* caps = this->caps();
288 SkASSERT(caps);
289
290 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
291 return nullptr;
292 }
293 if (backendTex.width() > caps->maxTextureSize() ||
294 backendTex.height() > caps->maxTextureSize()) {
295 return nullptr;
296 }
297
298 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
299}
300
Brian Salomond17f6582017-07-19 18:28:58 -0400301sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400302 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400303 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500304 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400305 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500306 if (sampleCnt < 1) {
307 return nullptr;
308 }
Robert Phillips0902c982019-07-16 07:47:56 -0400309
310 const GrCaps* caps = this->caps();
311
Greg Daniel7bfc9132019-08-14 14:23:53 -0400312 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400313 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400314 return nullptr;
315 }
316
Robert Phillips0902c982019-07-16 07:47:56 -0400317 if (backendTex.width() > caps->maxRenderTargetSize() ||
318 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400319 return nullptr;
320 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400321 sk_sp<GrTexture> tex =
322 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400323 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600324 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
325 tex->asRenderTarget()->setRequiresManualMSAAResolve();
326 }
bungeman6bd52842016-10-27 09:30:08 -0700327 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000328}
329
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400330sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400331 this->handleDirtyContext();
332
333 const GrCaps* caps = this->caps();
334
Greg Daniel6fa62e22019-08-07 15:52:37 -0400335 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800336 return nullptr;
337 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400338
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400339 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500340 if (backendRT.isFramebufferOnly()) {
341 rt->setFramebufferOnly();
342 }
343 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000344}
345
Greg Danielb46add82019-01-02 14:51:29 -0500346sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
347 const GrVkDrawableInfo& vkInfo) {
348 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
349}
350
351sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
352 const GrVkDrawableInfo& vkInfo) {
353 // This is only supported on Vulkan so we default to returning nullptr here
354 return nullptr;
355}
356
Brian Salomondbf70722019-02-07 11:31:24 -0500357sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
358 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400359 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000360 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500361 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700362 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700363 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700364 }
cdalton397536c2016-03-25 12:15:03 -0700365 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800366}
367
Greg Daniel46cfbc62019-06-07 11:43:30 -0400368bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400369 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400370 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700371 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500372 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500373
374 if (dst->readOnly()) {
375 return false;
376 }
377
joshualitt1cbdcde2015-08-21 11:53:29 -0700378 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500379
Greg Daniele227fe42019-08-21 13:52:24 -0400380 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700381}
382
Brian Salomona6948702018-06-01 15:33:20 -0400383bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400384 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
385 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400386 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500387 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500388 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400389 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500390
Brian Salomon1d435302019-07-01 13:05:28 -0400391 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
392 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
393 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700394 return false;
395 }
396
Brian Salomon1047a492019-07-02 12:25:21 -0400397 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
398 if (!this->caps()->readPixelsRowBytesSupport()) {
399 if (rowBytes != minRowBytes) {
400 return false;
401 }
402 } else {
403 if (rowBytes < minRowBytes) {
404 return false;
405 }
406 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
407 return false;
408 }
409 }
410
Brian Salomonbf7b6202016-11-11 16:08:03 -0500411 this->handleDirtyContext();
412
Brian Salomonf77c1462019-08-01 15:19:29 -0400413 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
414 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000415}
416
Brian Salomona9b04b92018-06-01 15:04:28 -0400417bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400418 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400419 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400420 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Leon Scroggins III569f07c2020-07-24 16:10:58 -0400421 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Upload %ix%i Texture", width, height);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500422 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500423 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500424
425 if (surface->readOnly()) {
426 return false;
427 }
428
Brian Salomon1047a492019-07-02 12:25:21 -0400429 if (mipLevelCount == 0) {
430 return false;
431 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400432 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400433 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
434 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400435 if (!bounds.contains(subRect)) {
436 return false;
437 }
438 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
439 // We require that if the texels are mipped, than the write region is the entire surface
440 return false;
441 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400442
Brian Salomona56a7462020-02-07 14:17:25 -0500443 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
444 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400445 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800446 }
jvanverth2dc29942015-09-01 07:16:46 -0700447
bsalomon@google.com6f379512011-11-16 20:36:03 +0000448 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400449 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400450 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700451 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400452 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800453 fStats.incTextureUploads();
454 return true;
455 }
456 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000457}
458
Brian Salomone05ba5a2019-04-08 11:59:07 -0400459bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400460 GrColorType textureColorType, GrColorType bufferColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500461 sk_sp<GrGpuBuffer> transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400462 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500463 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700464 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800465
Brian Salomonc67c31c2018-12-06 10:00:03 -0500466 if (texture->readOnly()) {
467 return false;
468 }
469
Greg Daniel660cc992017-06-26 14:55:05 -0400470 // We require that the write region is contained in the texture
471 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
472 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
473 if (!bounds.contains(subRect)) {
474 return false;
475 }
476
Brian Salomonb28cb682019-07-26 12:48:47 -0400477 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400478 if (this->caps()->writePixelsRowBytesSupport()) {
479 if (rowBytes < SkToSizeT(bpp * width)) {
480 return false;
481 }
482 if (rowBytes % bpp) {
483 return false;
484 }
485 } else {
486 if (rowBytes != SkToSizeT(bpp * width)) {
487 return false;
488 }
489 }
490
jvanverth17aa0472016-01-05 10:41:27 -0800491 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400492 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500493 bufferColorType, std::move(transferBuffer), offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700494 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500495 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800496 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700497
jvanverth17aa0472016-01-05 10:41:27 -0800498 return true;
499 }
500 return false;
501}
502
Brian Salomon26de56e2019-04-10 12:14:26 -0400503bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400504 GrColorType surfaceColorType, GrColorType bufferColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500505 sk_sp<GrGpuBuffer> transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400506 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400507 SkASSERT(surface);
508 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400509 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400510
Greg Danielba88ab62019-07-26 09:14:01 -0400511#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400512 auto supportedRead = this->caps()->supportedReadPixelsColorType(
513 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400514 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
515 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
516#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400517
518 // We require that the write region is contained in the texture
519 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
520 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
521 if (!bounds.contains(subRect)) {
522 return false;
523 }
524
525 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400526 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500527 bufferColorType, std::move(transferBuffer), offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400528 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400529 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400530 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400531 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400532}
533
Brian Salomon930f9392018-06-20 16:25:26 -0400534bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400535 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400536 SkASSERT(texture);
Brian Salomon69100f02020-07-21 10:49:25 -0400537 SkASSERT(this->caps()->mipmapSupport());
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400538 SkASSERT(texture->mipmapped() == GrMipmapped::kYes);
539 if (!texture->mipmapsAreDirty()) {
Chris Dalton16a33c62019-09-24 22:19:17 -0600540 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
541 // actual target. This may be caused by things that the drawingManager could not predict,
542 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
543 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
544 return true;
545 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500546 if (texture->readOnly()) {
547 return false;
548 }
Brian Salomon930f9392018-06-20 16:25:26 -0400549 if (this->onRegenerateMipMapLevels(texture)) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400550 texture->markMipmapsClean();
Brian Salomon930f9392018-06-20 16:25:26 -0400551 return true;
552 }
553 return false;
554}
555
Brian Salomon1f05d452019-02-08 12:33:08 -0500556void GrGpu::resetTextureBindings() {
557 this->handleDirtyContext();
558 this->onResetTextureBindings();
559}
560
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400561void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000562 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000563 this->handleDirtyContext();
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400564 this->onResolveRenderTarget(target, resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000565}
566
Brian Salomon1fabd512018-02-09 09:54:25 -0500567void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
568 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700569 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500570 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700571 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
572 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700573 GrTexture* texture = surface->asTexture();
574 if (texture && 1 == mipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400575 texture->markMipmapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700576 }
577 }
578}
579
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600580int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700581 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600582 SkASSERT(renderTarget->numSamples() > 1 ||
Brian Salomonf7f54332020-07-28 09:23:35 -0400583 (renderTarget->getStencilAttachment() &&
584 renderTarget->getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700585
586 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600587 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700588 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
589}
590
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500591void GrGpu::executeFlushInfo(SkSpan<GrSurfaceProxy*> proxies,
Greg Danielfe159622020-04-10 17:43:51 +0000592 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400593 const GrFlushInfo& info,
594 const GrBackendSurfaceMutableState* newState) {
Brian Salomone39526b2019-06-24 16:35:53 -0400595 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000596
Robert Phillips9da87e02019-02-04 13:26:26 -0500597 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500598
Greg Daniel8561fc22020-04-08 12:52:51 -0400599 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
600 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500601 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400602 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400603 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400604 semaphores[i] = resourceProvider->wrapBackendSemaphore(
605 info.fSignalSemaphores[i],
606 GrResourceProvider::SemaphoreWrapType::kWillSignal,
607 kBorrow_GrWrapOwnership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400608 // If we failed to wrap the semaphore it means the client didn't give us a valid
609 // semaphore to begin with. Therefore, it is fine to not signal it.
610 if (semaphores[i]) {
611 this->insertSemaphore(semaphores[i].get());
612 }
Greg Daniel51316782017-08-02 15:10:09 +0000613 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400614 semaphores[i] = resourceProvider->makeSemaphore(false);
615 if (semaphores[i]) {
616 this->insertSemaphore(semaphores[i].get());
617 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
618 }
Greg Daniel51316782017-08-02 15:10:09 +0000619 }
620 }
621 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500622
Greg Danielfe159622020-04-10 17:43:51 +0000623 if (info.fFinishedProc) {
624 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
625 }
Greg Daniel55822f12020-05-26 11:26:45 -0400626
627 if (info.fSubmittedProc) {
628 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
629 }
630
Greg Daniel9efe3862020-06-11 11:51:06 -0400631 // We currently don't support passing in new surface state for multiple proxies here. The only
632 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
633 // updates anyways.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500634 SkASSERT(!newState || proxies.count() == 1);
Greg Daniel9efe3862020-06-11 11:51:06 -0400635 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500636 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000637}
638
Greg Daniel65476e02020-10-27 09:20:20 -0400639GrOpsRenderPass* GrGpu::getOpsRenderPass(
640 GrRenderTarget* renderTarget,
641 GrAttachment* stencil,
642 GrSurfaceOrigin origin,
643 const SkIRect& bounds,
644 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
645 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
646 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
647 GrXferBarrierFlags renderPassXferBarriers) {
648#if SK_HISTOGRAMS_ENABLED
649 fCurrentSubmitRenderPassCount++;
650#endif
651 return this->onGetOpsRenderPass(renderTarget, stencil, origin, bounds, colorInfo, stencilInfo,
652 sampledProxies, renderPassXferBarriers);
653}
654
Greg Danielfe159622020-04-10 17:43:51 +0000655bool GrGpu::submitToGpu(bool syncCpu) {
656 this->stats()->incNumSubmitToGpus();
657
Greg Daniela58db7f2020-07-15 09:17:59 -0400658 if (auto manager = this->stagingBufferManager()) {
659 manager->detachBuffers();
660 }
Stephen Whitef3d5d442020-04-08 10:35:58 -0400661
Jim Van Verth7e829b22020-07-30 17:04:35 -0400662 if (auto uniformsBuffer = this->uniformsRingBuffer()) {
663 uniformsBuffer->startSubmit(this);
664 }
665
Greg Danielfe159622020-04-10 17:43:51 +0000666 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400667
Greg Daniel55822f12020-05-26 11:26:45 -0400668 this->callSubmittedProcs(submitted);
669
Greg Danielb4327542020-11-20 10:50:50 -0500670 this->reportSubmitHistograms();
671
672 return submitted;
673}
674
675void GrGpu::reportSubmitHistograms() {
Greg Daniel65476e02020-10-27 09:20:20 -0400676#if SK_HISTOGRAMS_ENABLED
677 // The max allowed value for SK_HISTOGRAM_EXACT_LINEAR is 100. If we want to support higher
678 // values we can add SK_HISTOGRAM_CUSTOM_COUNTS but this has a number of buckets that is less
679 // than the number of actual values
680 static constexpr int kMaxRenderPassBucketValue = 100;
681 SK_HISTOGRAM_EXACT_LINEAR("SubmitRenderPasses",
Greg Danielca107f32020-10-28 09:36:20 -0400682 std::min(fCurrentSubmitRenderPassCount, kMaxRenderPassBucketValue),
Greg Daniel65476e02020-10-27 09:20:20 -0400683 kMaxRenderPassBucketValue);
684 fCurrentSubmitRenderPassCount = 0;
685#endif
686
Greg Danielb4327542020-11-20 10:50:50 -0500687 this->onReportSubmitHistograms();
Greg Daniel51316782017-08-02 15:10:09 +0000688}
Brian Osman71a18892017-08-10 10:23:25 -0400689
Brian Salomon24069eb2020-06-24 10:19:52 -0400690bool GrGpu::checkAndResetOOMed() {
691 if (fOOMed) {
692 fOOMed = false;
693 return true;
694 }
695 return false;
696}
697
Greg Daniel55822f12020-05-26 11:26:45 -0400698void GrGpu::callSubmittedProcs(bool success) {
699 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
700 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
701 }
702 fSubmittedProcs.reset();
703}
704
Kevin Lubickf4def342018-10-04 12:52:50 -0400705#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400706void GrGpu::dumpJSON(SkJSONWriter* writer) const {
707 writer->beginObject();
708
709 // TODO: Is there anything useful in the base class to dump here?
710
711 this->onDumpJSON(writer);
712
713 writer->endObject();
714}
Kevin Lubickf4def342018-10-04 12:52:50 -0400715#else
716void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
717#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400718
Robert Phillipsf0ced622019-05-16 09:06:25 -0400719#if GR_TEST_UTILS
720
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500721#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500722static const char* cache_result_to_str(int i) {
723 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
724 "hits",
725 "misses",
726 "partials"
727 };
728 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
729 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
730 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
731 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
732 return kCacheResultStrings[i];
733}
734
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500735void GrGpu::Stats::dump(SkString* out) {
736 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
737 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
738 out->appendf("Textures Created: %d\n", fTextureCreates);
739 out->appendf("Texture Uploads: %d\n", fTextureUploads);
740 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400741 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500742 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
Greg Daniel5d0330e2020-10-12 16:05:21 -0400743 out->appendf("MSAA Attachment Creates: %d\n", fMSAAAttachmentCreates);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500744 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400745 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Greg Daniel5d0330e2020-10-12 16:05:21 -0400746 out->appendf("Number of Scratch MSAA Attachments reused %d\n",
747 fNumScratchMSAAAttachmentsReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500748
749 SkASSERT(fNumInlineCompilationFailures == 0);
750 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
751 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
752 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
753 fInlineProgramCacheStats[i]);
754 }
755
756 SkASSERT(fNumPreCompilationFailures == 0);
757 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
758 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
759 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
760 fPreProgramCacheStats[i]);
761 }
762
763 SkASSERT(fNumCompilationFailures == 0);
764 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
765 out->appendf("Total number of partial compilation successes %d\n",
766 fNumPartialCompilationSuccesses);
767 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500768
769 // enable this block to output CSV-style stats for program pre-compilation
770#if 0
771 SkASSERT(fNumInlineCompilationFailures == 0);
772 SkASSERT(fNumPreCompilationFailures == 0);
773 SkASSERT(fNumCompilationFailures == 0);
774 SkASSERT(fNumPartialCompilationSuccesses == 0);
775
776 SkDebugf("%d, %d, %d, %d, %d\n",
777 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
778 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
779 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
780 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
781 fNumCompilationSuccesses);
782#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500783}
784
785void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
786 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
787 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500788}
789
Robert Phillips57ef6802019-09-23 10:12:47 -0400790#endif // GR_GPU_STATS
791#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500792
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500793bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
Brian Salomon05487ab2020-12-23 20:32:22 -0500794 GrMipmapped mipmapped,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500795 const BackendTextureData* data) {
796 int numMipLevels = 1;
Brian Salomon05487ab2020-12-23 20:32:22 -0500797 if (mipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400798 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500799 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400800
Robert Phillips42716d42019-12-16 12:19:54 -0500801 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400802 return true;
803 }
804
Robert Phillips42716d42019-12-16 12:19:54 -0500805 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500806 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500807 }
808
809 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
810
Brian Salomon85c3d682019-11-04 15:04:54 -0500811 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400812 return false;
813 }
814
Brian Salomon05487ab2020-12-23 20:32:22 -0500815 GrColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500816 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500817 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500818 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400819 return false;
820 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500821 if (colorType != data->pixmap(i).colorType()) {
822 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400823 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400824 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400825 return true;
826}
827
Robert Phillipsb915c942019-12-17 14:44:37 -0500828bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400829 GrMipmapped mipMapped, const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500830
831 if (!data || data->type() == BackendTextureData::Type::kColor) {
832 return true;
833 }
834
835 if (data->type() == BackendTextureData::Type::kPixmaps) {
836 return false;
837 }
838
839 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
840
Robert Phillips99dead92020-01-27 16:11:57 -0500841 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400842 nullptr, mipMapped == GrMipmapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500843
844 return computedSize == data->compressedSize();
845}
846
Brian Salomon85c3d682019-11-04 15:04:54 -0500847GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
848 const GrBackendFormat& format,
849 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400850 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400851 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400852 const GrCaps* caps = this->caps();
853
854 if (!format.isValid()) {
855 return {};
856 }
857
Robert Phillipsd34691b2019-09-24 13:38:43 -0400858 if (caps->isFormatCompressed(format)) {
859 // Compressed formats must go through the createCompressedBackendTexture API
860 return {};
861 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400862
Brian Salomon85c3d682019-11-04 15:04:54 -0500863 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
864 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400865 return {};
866 }
867
Brian Salomon69100f02020-07-21 10:49:25 -0400868 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400869 return {};
870 }
871
Greg Daniel16032b32020-05-06 15:31:10 -0400872 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
873}
874
875bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400876 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400877 const BackendTextureData* data) {
878 SkASSERT(data);
879 const GrCaps* caps = this->caps();
880
Greg Daniel16032b32020-05-06 15:31:10 -0400881 if (!backendTexture.isValid()) {
882 return false;
883 }
884
885 if (data->type() == BackendTextureData::Type::kPixmaps) {
Brian Salomon05487ab2020-12-23 20:32:22 -0500886 auto ct = data->pixmap(0).colorType();
Greg Daniel16032b32020-05-06 15:31:10 -0400887 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
888 return false;
889 }
890 }
891
Brian Salomon69100f02020-07-21 10:49:25 -0400892 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Daniel16032b32020-05-06 15:31:10 -0400893 return false;
894 }
895
Brian Salomon05487ab2020-12-23 20:32:22 -0500896 GrMipmapped mipmapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
897 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipmapped, data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400898 return false;
899 }
900
Greg Daniel25597782020-06-11 13:15:08 -0400901 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400902}
Robert Phillipsb915c942019-12-17 14:44:37 -0500903
904GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
905 const GrBackendFormat& format,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400906 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400907 GrProtected isProtected) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500908 const GrCaps* caps = this->caps();
909
910 if (!format.isValid()) {
911 return {};
912 }
913
Greg Daniel01f278c2020-06-12 16:58:17 -0400914 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillipsb915c942019-12-17 14:44:37 -0500915 if (compressionType == SkImage::CompressionType::kNone) {
916 // Uncompressed formats must go through the createBackendTexture API
917 return {};
918 }
919
920 if (dimensions.isEmpty() ||
921 dimensions.width() > caps->maxTextureSize() ||
922 dimensions.height() > caps->maxTextureSize()) {
923 return {};
924 }
925
Brian Salomon69100f02020-07-21 10:49:25 -0400926 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500927 return {};
928 }
929
Greg Danielaaf738c2020-07-10 09:30:33 -0400930 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped, isProtected);
931}
932
933bool GrGpu::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
934 sk_sp<GrRefCntedCallback> finishedCallback,
935 const BackendTextureData* data) {
936 SkASSERT(data);
937
938 if (!backendTexture.isValid()) {
939 return false;
Robert Phillipsb915c942019-12-17 14:44:37 -0500940 }
941
Greg Danielaaf738c2020-07-10 09:30:33 -0400942 GrBackendFormat format = backendTexture.getBackendFormat();
943
944 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
945 if (compressionType == SkImage::CompressionType::kNone) {
946 // Uncompressed formats must go through the createBackendTexture API
947 return false;
948 }
949
Brian Salomon69100f02020-07-21 10:49:25 -0400950 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Danielaaf738c2020-07-10 09:30:33 -0400951 return false;
952 }
953
Brian Salomon40a40622020-07-21 10:32:07 -0400954 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -0400955
956 if (!CompressedDataIsCorrect(backendTexture.dimensions(), compressionType, mipMapped, data)) {
957 return false;
958 }
959
960 return this->onUpdateCompressedBackendTexture(backendTexture, std::move(finishedCallback),
961 data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500962}