blob: 2a450f296fbe88e383b773a1d5670c9d96f60bd5 [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"
Adlai Holler9e2c50e2021-02-09 14:41:52 -050034#include "src/gpu/GrTextureProxyPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#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);
Herb Derby89acfe72021-01-28 10:57:40 -0500421 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Texture upload(%u) %ix%i",
422 surface->uniqueID().asUInt(), width, height);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500423 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500424 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500425
426 if (surface->readOnly()) {
427 return false;
428 }
429
Brian Salomon1047a492019-07-02 12:25:21 -0400430 if (mipLevelCount == 0) {
431 return false;
432 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400433 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400434 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
435 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400436 if (!bounds.contains(subRect)) {
437 return false;
438 }
439 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
440 // We require that if the texels are mipped, than the write region is the entire surface
441 return false;
442 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400443
Brian Salomona56a7462020-02-07 14:17:25 -0500444 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
445 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400446 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800447 }
jvanverth2dc29942015-09-01 07:16:46 -0700448
bsalomon@google.com6f379512011-11-16 20:36:03 +0000449 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400450 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400451 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700452 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400453 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800454 fStats.incTextureUploads();
455 return true;
456 }
457 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000458}
459
Brian Salomone05ba5a2019-04-08 11:59:07 -0400460bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400461 GrColorType textureColorType, GrColorType bufferColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500462 sk_sp<GrGpuBuffer> transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400463 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500464 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700465 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800466
Brian Salomonc67c31c2018-12-06 10:00:03 -0500467 if (texture->readOnly()) {
468 return false;
469 }
470
Greg Daniel660cc992017-06-26 14:55:05 -0400471 // We require that the write region is contained in the texture
472 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
473 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
474 if (!bounds.contains(subRect)) {
475 return false;
476 }
477
Brian Salomonb28cb682019-07-26 12:48:47 -0400478 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400479 if (this->caps()->writePixelsRowBytesSupport()) {
480 if (rowBytes < SkToSizeT(bpp * width)) {
481 return false;
482 }
483 if (rowBytes % bpp) {
484 return false;
485 }
486 } else {
487 if (rowBytes != SkToSizeT(bpp * width)) {
488 return false;
489 }
490 }
491
jvanverth17aa0472016-01-05 10:41:27 -0800492 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400493 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500494 bufferColorType, std::move(transferBuffer), offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700495 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500496 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800497 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700498
jvanverth17aa0472016-01-05 10:41:27 -0800499 return true;
500 }
501 return false;
502}
503
Brian Salomon26de56e2019-04-10 12:14:26 -0400504bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400505 GrColorType surfaceColorType, GrColorType bufferColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500506 sk_sp<GrGpuBuffer> transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400507 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400508 SkASSERT(surface);
509 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400510 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400511
Greg Danielba88ab62019-07-26 09:14:01 -0400512#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400513 auto supportedRead = this->caps()->supportedReadPixelsColorType(
514 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400515 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
516 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
517#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400518
519 // We require that the write region is contained in the texture
520 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
521 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
522 if (!bounds.contains(subRect)) {
523 return false;
524 }
525
526 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400527 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500528 bufferColorType, std::move(transferBuffer), offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400529 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400530 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400531 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400532 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400533}
534
Brian Salomon930f9392018-06-20 16:25:26 -0400535bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400536 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400537 SkASSERT(texture);
Brian Salomon69100f02020-07-21 10:49:25 -0400538 SkASSERT(this->caps()->mipmapSupport());
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400539 SkASSERT(texture->mipmapped() == GrMipmapped::kYes);
540 if (!texture->mipmapsAreDirty()) {
Chris Dalton16a33c62019-09-24 22:19:17 -0600541 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
542 // actual target. This may be caused by things that the drawingManager could not predict,
543 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
544 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
545 return true;
546 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500547 if (texture->readOnly()) {
548 return false;
549 }
Brian Salomon930f9392018-06-20 16:25:26 -0400550 if (this->onRegenerateMipMapLevels(texture)) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400551 texture->markMipmapsClean();
Brian Salomon930f9392018-06-20 16:25:26 -0400552 return true;
553 }
554 return false;
555}
556
Brian Salomon1f05d452019-02-08 12:33:08 -0500557void GrGpu::resetTextureBindings() {
558 this->handleDirtyContext();
559 this->onResetTextureBindings();
560}
561
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400562void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000563 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000564 this->handleDirtyContext();
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400565 this->onResolveRenderTarget(target, resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000566}
567
Brian Salomon1fabd512018-02-09 09:54:25 -0500568void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
569 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700570 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500571 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700572 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
573 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700574 GrTexture* texture = surface->asTexture();
575 if (texture && 1 == mipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400576 texture->markMipmapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700577 }
578 }
579}
580
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600581int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700582 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600583 SkASSERT(renderTarget->numSamples() > 1 ||
Brian Salomonf7f54332020-07-28 09:23:35 -0400584 (renderTarget->getStencilAttachment() &&
585 renderTarget->getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700586
587 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600588 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700589 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
590}
591
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500592void GrGpu::executeFlushInfo(SkSpan<GrSurfaceProxy*> proxies,
Greg Danielfe159622020-04-10 17:43:51 +0000593 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400594 const GrFlushInfo& info,
595 const GrBackendSurfaceMutableState* newState) {
Brian Salomone39526b2019-06-24 16:35:53 -0400596 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000597
Robert Phillips9da87e02019-02-04 13:26:26 -0500598 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500599
Greg Daniel8561fc22020-04-08 12:52:51 -0400600 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
601 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500602 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400603 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400604 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400605 semaphores[i] = resourceProvider->wrapBackendSemaphore(
606 info.fSignalSemaphores[i],
607 GrResourceProvider::SemaphoreWrapType::kWillSignal,
608 kBorrow_GrWrapOwnership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400609 // If we failed to wrap the semaphore it means the client didn't give us a valid
610 // semaphore to begin with. Therefore, it is fine to not signal it.
611 if (semaphores[i]) {
612 this->insertSemaphore(semaphores[i].get());
613 }
Greg Daniel51316782017-08-02 15:10:09 +0000614 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400615 semaphores[i] = resourceProvider->makeSemaphore(false);
616 if (semaphores[i]) {
617 this->insertSemaphore(semaphores[i].get());
618 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
619 }
Greg Daniel51316782017-08-02 15:10:09 +0000620 }
621 }
622 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500623
Greg Danielfe159622020-04-10 17:43:51 +0000624 if (info.fFinishedProc) {
625 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
626 }
Greg Daniel55822f12020-05-26 11:26:45 -0400627
628 if (info.fSubmittedProc) {
629 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
630 }
631
Greg Daniel9efe3862020-06-11 11:51:06 -0400632 // We currently don't support passing in new surface state for multiple proxies here. The only
633 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
634 // updates anyways.
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500635 SkASSERT(!newState || proxies.count() == 1);
Greg Daniel9efe3862020-06-11 11:51:06 -0400636 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
Adlai Hollerc2bfcff2020-11-06 15:39:36 -0500637 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000638}
639
Greg Daniel65476e02020-10-27 09:20:20 -0400640GrOpsRenderPass* GrGpu::getOpsRenderPass(
641 GrRenderTarget* renderTarget,
642 GrAttachment* stencil,
643 GrSurfaceOrigin origin,
644 const SkIRect& bounds,
645 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
646 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
647 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
648 GrXferBarrierFlags renderPassXferBarriers) {
649#if SK_HISTOGRAMS_ENABLED
650 fCurrentSubmitRenderPassCount++;
651#endif
652 return this->onGetOpsRenderPass(renderTarget, stencil, origin, bounds, colorInfo, stencilInfo,
653 sampledProxies, renderPassXferBarriers);
654}
655
Greg Danielfe159622020-04-10 17:43:51 +0000656bool GrGpu::submitToGpu(bool syncCpu) {
657 this->stats()->incNumSubmitToGpus();
658
Greg Daniela58db7f2020-07-15 09:17:59 -0400659 if (auto manager = this->stagingBufferManager()) {
660 manager->detachBuffers();
661 }
Stephen Whitef3d5d442020-04-08 10:35:58 -0400662
Jim Van Verth7e829b22020-07-30 17:04:35 -0400663 if (auto uniformsBuffer = this->uniformsRingBuffer()) {
664 uniformsBuffer->startSubmit(this);
665 }
666
Greg Danielfe159622020-04-10 17:43:51 +0000667 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400668
Greg Daniel55822f12020-05-26 11:26:45 -0400669 this->callSubmittedProcs(submitted);
670
Greg Danielb4327542020-11-20 10:50:50 -0500671 this->reportSubmitHistograms();
672
673 return submitted;
674}
675
676void GrGpu::reportSubmitHistograms() {
Greg Daniel65476e02020-10-27 09:20:20 -0400677#if SK_HISTOGRAMS_ENABLED
678 // The max allowed value for SK_HISTOGRAM_EXACT_LINEAR is 100. If we want to support higher
679 // values we can add SK_HISTOGRAM_CUSTOM_COUNTS but this has a number of buckets that is less
680 // than the number of actual values
681 static constexpr int kMaxRenderPassBucketValue = 100;
682 SK_HISTOGRAM_EXACT_LINEAR("SubmitRenderPasses",
Greg Danielca107f32020-10-28 09:36:20 -0400683 std::min(fCurrentSubmitRenderPassCount, kMaxRenderPassBucketValue),
Greg Daniel65476e02020-10-27 09:20:20 -0400684 kMaxRenderPassBucketValue);
685 fCurrentSubmitRenderPassCount = 0;
686#endif
687
Greg Danielb4327542020-11-20 10:50:50 -0500688 this->onReportSubmitHistograms();
Greg Daniel51316782017-08-02 15:10:09 +0000689}
Brian Osman71a18892017-08-10 10:23:25 -0400690
Brian Salomon24069eb2020-06-24 10:19:52 -0400691bool GrGpu::checkAndResetOOMed() {
692 if (fOOMed) {
693 fOOMed = false;
694 return true;
695 }
696 return false;
697}
698
Greg Daniel55822f12020-05-26 11:26:45 -0400699void GrGpu::callSubmittedProcs(bool success) {
700 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
701 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
702 }
703 fSubmittedProcs.reset();
704}
705
Kevin Lubickf4def342018-10-04 12:52:50 -0400706#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400707void GrGpu::dumpJSON(SkJSONWriter* writer) const {
708 writer->beginObject();
709
710 // TODO: Is there anything useful in the base class to dump here?
711
712 this->onDumpJSON(writer);
713
714 writer->endObject();
715}
Kevin Lubickf4def342018-10-04 12:52:50 -0400716#else
717void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
718#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400719
Robert Phillipsf0ced622019-05-16 09:06:25 -0400720#if GR_TEST_UTILS
721
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500722#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500723
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500724void GrGpu::Stats::dump(SkString* out) {
725 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500726 out->appendf("Textures Created: %d\n", fTextureCreates);
727 out->appendf("Texture Uploads: %d\n", fTextureUploads);
728 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400729 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500730 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
Greg Daniel5d0330e2020-10-12 16:05:21 -0400731 out->appendf("MSAA Attachment Creates: %d\n", fMSAAAttachmentCreates);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500732 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400733 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Greg Daniel5d0330e2020-10-12 16:05:21 -0400734 out->appendf("Number of Scratch MSAA Attachments reused %d\n",
735 fNumScratchMSAAAttachmentsReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500736
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500737 // enable this block to output CSV-style stats for program pre-compilation
738#if 0
739 SkASSERT(fNumInlineCompilationFailures == 0);
740 SkASSERT(fNumPreCompilationFailures == 0);
741 SkASSERT(fNumCompilationFailures == 0);
742 SkASSERT(fNumPartialCompilationSuccesses == 0);
743
744 SkDebugf("%d, %d, %d, %d, %d\n",
745 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
746 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
747 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
748 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
749 fNumCompilationSuccesses);
750#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500751}
752
753void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
754 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500755}
756
Robert Phillips57ef6802019-09-23 10:12:47 -0400757#endif // GR_GPU_STATS
758#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500759
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500760bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
Brian Salomon05487ab2020-12-23 20:32:22 -0500761 GrMipmapped mipmapped,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500762 const BackendTextureData* data) {
763 int numMipLevels = 1;
Brian Salomon05487ab2020-12-23 20:32:22 -0500764 if (mipmapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400765 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500766 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400767
Robert Phillips42716d42019-12-16 12:19:54 -0500768 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400769 return true;
770 }
771
Robert Phillips42716d42019-12-16 12:19:54 -0500772 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500773 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500774 }
775
776 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
777
Brian Salomon85c3d682019-11-04 15:04:54 -0500778 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400779 return false;
780 }
781
Brian Salomon05487ab2020-12-23 20:32:22 -0500782 GrColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500783 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500784 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500785 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400786 return false;
787 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500788 if (colorType != data->pixmap(i).colorType()) {
789 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400790 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400791 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400792 return true;
793}
794
Robert Phillipsb915c942019-12-17 14:44:37 -0500795bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400796 GrMipmapped mipMapped, const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500797
798 if (!data || data->type() == BackendTextureData::Type::kColor) {
799 return true;
800 }
801
802 if (data->type() == BackendTextureData::Type::kPixmaps) {
803 return false;
804 }
805
806 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
807
Robert Phillips99dead92020-01-27 16:11:57 -0500808 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400809 nullptr, mipMapped == GrMipmapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500810
811 return computedSize == data->compressedSize();
812}
813
Brian Salomon85c3d682019-11-04 15:04:54 -0500814GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
815 const GrBackendFormat& format,
816 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400817 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400818 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400819 const GrCaps* caps = this->caps();
820
821 if (!format.isValid()) {
822 return {};
823 }
824
Robert Phillipsd34691b2019-09-24 13:38:43 -0400825 if (caps->isFormatCompressed(format)) {
826 // Compressed formats must go through the createCompressedBackendTexture API
827 return {};
828 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400829
Brian Salomon85c3d682019-11-04 15:04:54 -0500830 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
831 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400832 return {};
833 }
834
Brian Salomon69100f02020-07-21 10:49:25 -0400835 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400836 return {};
837 }
838
Greg Daniel16032b32020-05-06 15:31:10 -0400839 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
840}
841
842bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400843 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400844 const BackendTextureData* data) {
845 SkASSERT(data);
846 const GrCaps* caps = this->caps();
847
Greg Daniel16032b32020-05-06 15:31:10 -0400848 if (!backendTexture.isValid()) {
849 return false;
850 }
851
852 if (data->type() == BackendTextureData::Type::kPixmaps) {
Brian Salomon05487ab2020-12-23 20:32:22 -0500853 auto ct = data->pixmap(0).colorType();
Greg Daniel16032b32020-05-06 15:31:10 -0400854 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
855 return false;
856 }
857 }
858
Brian Salomon69100f02020-07-21 10:49:25 -0400859 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Daniel16032b32020-05-06 15:31:10 -0400860 return false;
861 }
862
Brian Salomon05487ab2020-12-23 20:32:22 -0500863 GrMipmapped mipmapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
864 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipmapped, data)) {
Greg Daniel16032b32020-05-06 15:31:10 -0400865 return false;
866 }
867
Greg Daniel25597782020-06-11 13:15:08 -0400868 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400869}
Robert Phillipsb915c942019-12-17 14:44:37 -0500870
871GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
872 const GrBackendFormat& format,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400873 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400874 GrProtected isProtected) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500875 const GrCaps* caps = this->caps();
876
877 if (!format.isValid()) {
878 return {};
879 }
880
Greg Daniel01f278c2020-06-12 16:58:17 -0400881 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillipsb915c942019-12-17 14:44:37 -0500882 if (compressionType == SkImage::CompressionType::kNone) {
883 // Uncompressed formats must go through the createBackendTexture API
884 return {};
885 }
886
887 if (dimensions.isEmpty() ||
888 dimensions.width() > caps->maxTextureSize() ||
889 dimensions.height() > caps->maxTextureSize()) {
890 return {};
891 }
892
Brian Salomon69100f02020-07-21 10:49:25 -0400893 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500894 return {};
895 }
896
Greg Danielaaf738c2020-07-10 09:30:33 -0400897 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped, isProtected);
898}
899
900bool GrGpu::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
901 sk_sp<GrRefCntedCallback> finishedCallback,
902 const BackendTextureData* data) {
903 SkASSERT(data);
904
905 if (!backendTexture.isValid()) {
906 return false;
Robert Phillipsb915c942019-12-17 14:44:37 -0500907 }
908
Greg Danielaaf738c2020-07-10 09:30:33 -0400909 GrBackendFormat format = backendTexture.getBackendFormat();
910
911 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
912 if (compressionType == SkImage::CompressionType::kNone) {
913 // Uncompressed formats must go through the createBackendTexture API
914 return false;
915 }
916
Brian Salomon69100f02020-07-21 10:49:25 -0400917 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Danielaaf738c2020-07-10 09:30:33 -0400918 return false;
919 }
920
Brian Salomon40a40622020-07-21 10:32:07 -0400921 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -0400922
923 if (!CompressedDataIsCorrect(backendTexture.dimensions(), compressionType, mipMapped, data)) {
924 return false;
925 }
926
927 return this->onUpdateCompressedBackendTexture(backendTexture, std::move(finishedCallback),
928 data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500929}