blob: 2bd5ce83bd0ecfb4c7a74fa64d92258b8630cc2d [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 Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrAuditTrail.h"
Greg Daniel01f278c2020-06-12 16:58:17 -040018#include "src/gpu/GrBackendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrCaps.h"
20#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040021#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060023#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrPathRendering.h"
25#include "src/gpu/GrPipeline.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040026#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrResourceCache.h"
28#include "src/gpu/GrResourceProvider.h"
29#include "src/gpu/GrSemaphore.h"
Greg Daniela58db7f2020-07-15 09:17:59 -040030#include "src/gpu/GrStagingBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrStencilAttachment.h"
32#include "src/gpu/GrStencilSettings.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrTextureProxyPriv.h"
34#include "src/gpu/GrTracing.h"
35#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070036
bsalomon@google.comd302f142011-03-03 13:54:13 +000037////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000038
Adlai Holler3d0359a2020-07-09 15:35:55 -040039GrGpu::GrGpu(GrDirectContext* direct) : fResetBits(kAll_GrBackendState), fContext(direct) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000040
Stephen Whitef3d5d442020-04-08 10:35:58 -040041GrGpu::~GrGpu() {
Greg Daniel55822f12020-05-26 11:26:45 -040042 this->callSubmittedProcs(false);
Stephen Whitef3d5d442020-04-08 10:35:58 -040043}
bsalomon1d89ddc2014-08-19 14:20:58 -070044
Greg Daniela58db7f2020-07-15 09:17:59 -040045void GrGpu::disconnect(DisconnectType type) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000046
bsalomon@google.comd302f142011-03-03 13:54:13 +000047////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000048
Brian Salomona56a7462020-02-07 14:17:25 -050049static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040050 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040051 SkASSERT(mipLevelCount > 0);
52 bool hasBasePixels = texels[0].fPixels;
53 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040054 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050055 int w = dimensions.fWidth;
56 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040057 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
58 if (texels[currentMipLevel].fPixels) {
59 const size_t minRowBytes = w * bpp;
60 if (caps->writePixelsRowBytesSupport()) {
61 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
62 return false;
63 }
64 if (texels[currentMipLevel].fRowBytes % bpp) {
65 return false;
66 }
67 } else {
68 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
69 return false;
70 }
71 }
72 ++levelsWithPixelsCnt;
73 }
74 if (w == 1 && h == 1) {
75 if (currentMipLevel != mipLevelCount - 1) {
76 return false;
77 }
78 } else {
79 w = std::max(w / 2, 1);
80 h = std::max(h / 2, 1);
81 }
82 }
83 // Either just a base layer or a full stack is required.
84 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
85 return false;
86 }
87 // Can specify just the base, all levels, or no levels.
88 if (!hasBasePixels) {
89 return levelsWithPixelsCnt == 0;
90 }
Brian Salomond2a8ae22019-09-10 16:03:59 -040091 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -040092}
93
Brian Salomona56a7462020-02-07 14:17:25 -050094sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -040095 const GrBackendFormat& format,
96 GrRenderable renderable,
97 int renderTargetSampleCnt,
98 SkBudgeted budgeted,
99 GrProtected isProtected,
100 int mipLevelCount,
101 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400102 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400103 // Call GrGpu::createCompressedTexture.
104 return nullptr;
105 }
cblume55f2d2d2016-02-26 13:20:48 -0800106
Brian Salomon7e67dca2020-07-21 09:27:25 -0400107 GrMipmapped mipMapped = mipLevelCount > 1 ? GrMipmapped::kYes : GrMipmapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500108 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
109 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700110 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700111 }
112
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400113 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400114 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400115 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500116 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400117 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400118 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400119 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500120 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400121 format,
122 renderable,
123 renderTargetSampleCnt,
124 budgeted,
125 isProtected,
126 mipLevelCount,
127 levelClearMask);
128 if (tex) {
129 SkASSERT(tex->backendFormat() == format);
130 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
131 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
132 tex->resourcePriv().removeScratchKey();
133 }
134 fStats.incTextureCreates();
135 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
136 SkASSERT(GrRenderable::kYes == renderable);
137 tex->asRenderTarget()->setRequiresManualMSAAResolve();
138 }
139 }
140 return tex;
141}
142
Brian Salomona56a7462020-02-07 14:17:25 -0500143sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400144 const GrBackendFormat& format,
145 GrRenderable renderable,
146 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400147 GrMipmapped mipMapped,
Brian Salomona90382f2019-09-17 09:01:56 -0400148 SkBudgeted budgeted,
149 GrProtected isProtected) {
150 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400151 if (mipMapped == GrMipmapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500152 mipLevelCount =
153 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400154 }
155 uint32_t levelClearMask =
156 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500157 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
158 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400159 if (tex && mipMapped == GrMipmapped::kYes && levelClearMask) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400160 tex->markMipmapsClean();
Brian Salomona90382f2019-09-17 09:01:56 -0400161 }
162 return tex;
163}
164
Brian Salomona56a7462020-02-07 14:17:25 -0500165sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400166 const GrBackendFormat& format,
167 GrRenderable renderable,
168 int renderTargetSampleCnt,
169 SkBudgeted budgeted,
170 GrProtected isProtected,
171 GrColorType textureColorType,
172 GrColorType srcColorType,
173 const GrMipLevel texels[],
174 int texelLevelCount) {
175 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400176 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500177 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400178 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400179 return nullptr;
180 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400181 }
182
Brian Osman788b9162020-02-07 10:36:46 -0500183 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400184 uint32_t levelClearMask = 0;
185 if (this->caps()->shouldInitializeTextures()) {
186 if (texelLevelCount) {
187 for (int i = 0; i < mipLevelCount; ++i) {
188 if (!texels->fPixels) {
189 levelClearMask |= static_cast<uint32_t>(1 << i);
190 }
191 }
192 } else {
193 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
194 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400195 }
196
Brian Salomona56a7462020-02-07 14:17:25 -0500197 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
198 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800199 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400200 bool markMipLevelsClean = false;
201 // Currently if level 0 does not have pixels then no other level may, as enforced by
202 // validate_texel_levels.
203 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500204 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
205 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400206 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800207 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400208 // Currently if level[1] of mip map has pixel data then so must all other levels.
209 // as enforced by validate_texel_levels.
210 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
211 fStats.incTextureUploads();
212 } else if (levelClearMask && mipLevelCount > 1) {
213 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800214 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400215 if (markMipLevelsClean) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400216 tex->markMipmapsClean();
Brian Salomond2a8ae22019-09-10 16:03:59 -0400217 }
bsalomonb12ea412015-02-02 21:19:50 -0800218 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000219 return tex;
220}
221
Robert Phillips9f744f72019-12-19 19:14:33 -0500222sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400223 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500224 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400225 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500226 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500227 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400228 size_t dataSize) {
229 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500230 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
231 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400232 return nullptr;
233 }
Brian Salomona3e29962019-07-16 11:52:08 -0400234 // Note if we relax the requirement that data must be provided then we must check
235 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400236 if (!data) {
237 return nullptr;
238 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400239 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400240 return nullptr;
241 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500242
243 // TODO: expand CompressedDataIsCorrect to work here too
Greg Daniel01f278c2020-06-12 16:58:17 -0400244 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillips9f744f72019-12-19 19:14:33 -0500245
Robert Phillips99dead92020-01-27 16:11:57 -0500246 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400247 mipMapped == GrMipmapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400248 return nullptr;
249 }
Robert Phillips3a833922020-01-21 15:25:58 -0500250 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
251 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400252}
253
Greg Daniel7ef28f32017-04-20 16:41:55 +0000254sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400255 GrWrapOwnership ownership,
256 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500257 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500258 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000259 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400260
261 const GrCaps* caps = this->caps();
262 SkASSERT(caps);
263
Greg Daniel7bfc9132019-08-14 14:23:53 -0400264 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800265 return nullptr;
266 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400267 if (backendTex.width() > caps->maxTextureSize() ||
268 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800269 return nullptr;
270 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400271
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400272 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400273}
Eric Karl5c779752017-05-08 12:02:07 -0700274
Robert Phillipsb915c942019-12-17 14:44:37 -0500275sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
276 GrWrapOwnership ownership,
277 GrWrapCacheable cacheable) {
278 this->handleDirtyContext();
279
280 const GrCaps* caps = this->caps();
281 SkASSERT(caps);
282
283 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
284 return nullptr;
285 }
286 if (backendTex.width() > caps->maxTextureSize() ||
287 backendTex.height() > caps->maxTextureSize()) {
288 return nullptr;
289 }
290
291 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
292}
293
Brian Salomond17f6582017-07-19 18:28:58 -0400294sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400295 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400296 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500297 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400298 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500299 if (sampleCnt < 1) {
300 return nullptr;
301 }
Robert Phillips0902c982019-07-16 07:47:56 -0400302
303 const GrCaps* caps = this->caps();
304
Greg Daniel7bfc9132019-08-14 14:23:53 -0400305 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400306 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400307 return nullptr;
308 }
309
Robert Phillips0902c982019-07-16 07:47:56 -0400310 if (backendTex.width() > caps->maxRenderTargetSize() ||
311 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400312 return nullptr;
313 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400314 sk_sp<GrTexture> tex =
315 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400316 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600317 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
318 tex->asRenderTarget()->setRequiresManualMSAAResolve();
319 }
bungeman6bd52842016-10-27 09:30:08 -0700320 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000321}
322
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400323sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400324 this->handleDirtyContext();
325
326 const GrCaps* caps = this->caps();
327
Greg Daniel6fa62e22019-08-07 15:52:37 -0400328 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800329 return nullptr;
330 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400331
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400332 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500333 if (backendRT.isFramebufferOnly()) {
334 rt->setFramebufferOnly();
335 }
336 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000337}
338
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400339sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400340 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500341 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400342
343 const GrCaps* caps = this->caps();
344
345 int maxSize = caps->maxTextureSize();
346 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
347 return nullptr;
348 }
349
Greg Daniel6fa62e22019-08-07 15:52:37 -0400350 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400351 return nullptr;
352 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400353
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400354 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Chris Dalton3f7932e2019-08-19 00:39:13 -0600355 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
356 rt->setRequiresManualMSAAResolve();
357 }
358 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800359}
360
Greg Danielb46add82019-01-02 14:51:29 -0500361sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
362 const GrVkDrawableInfo& vkInfo) {
363 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
364}
365
366sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
367 const GrVkDrawableInfo& vkInfo) {
368 // This is only supported on Vulkan so we default to returning nullptr here
369 return nullptr;
370}
371
Brian Salomondbf70722019-02-07 11:31:24 -0500372sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
373 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400374 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000375 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500376 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700377 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700378 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700379 }
cdalton397536c2016-03-25 12:15:03 -0700380 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800381}
382
Greg Daniel46cfbc62019-06-07 11:43:30 -0400383bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400384 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400385 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700386 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500387 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500388
389 if (dst->readOnly()) {
390 return false;
391 }
392
joshualitt1cbdcde2015-08-21 11:53:29 -0700393 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500394
Greg Daniele227fe42019-08-21 13:52:24 -0400395 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700396}
397
Brian Salomona6948702018-06-01 15:33:20 -0400398bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400399 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
400 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400401 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500402 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500403 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400404 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500405
Brian Salomon1d435302019-07-01 13:05:28 -0400406 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
407 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
408 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700409 return false;
410 }
411
Brian Salomon1047a492019-07-02 12:25:21 -0400412 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
413 if (!this->caps()->readPixelsRowBytesSupport()) {
414 if (rowBytes != minRowBytes) {
415 return false;
416 }
417 } else {
418 if (rowBytes < minRowBytes) {
419 return false;
420 }
421 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
422 return false;
423 }
424 }
425
Brian Salomonbf7b6202016-11-11 16:08:03 -0500426 this->handleDirtyContext();
427
Brian Salomonf77c1462019-08-01 15:19:29 -0400428 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
429 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000430}
431
Brian Salomona9b04b92018-06-01 15:04:28 -0400432bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400433 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400434 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400435 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Leon Scroggins III569f07c2020-07-24 16:10:58 -0400436 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Upload %ix%i Texture", width, height);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500437 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500438 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500439
440 if (surface->readOnly()) {
441 return false;
442 }
443
Brian Salomon1047a492019-07-02 12:25:21 -0400444 if (mipLevelCount == 0) {
445 return false;
446 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400447 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400448 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
449 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400450 if (!bounds.contains(subRect)) {
451 return false;
452 }
453 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
454 // We require that if the texels are mipped, than the write region is the entire surface
455 return false;
456 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400457
Brian Salomona56a7462020-02-07 14:17:25 -0500458 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
459 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400460 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800461 }
jvanverth2dc29942015-09-01 07:16:46 -0700462
bsalomon@google.com6f379512011-11-16 20:36:03 +0000463 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400464 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400465 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700466 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400467 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800468 fStats.incTextureUploads();
469 return true;
470 }
471 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000472}
473
Brian Salomone05ba5a2019-04-08 11:59:07 -0400474bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400475 GrColorType textureColorType, GrColorType bufferColorType,
476 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400477 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500478 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700479 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800480
Brian Salomonc67c31c2018-12-06 10:00:03 -0500481 if (texture->readOnly()) {
482 return false;
483 }
484
Greg Daniel660cc992017-06-26 14:55:05 -0400485 // We require that the write region is contained in the texture
486 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
487 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
488 if (!bounds.contains(subRect)) {
489 return false;
490 }
491
Brian Salomonb28cb682019-07-26 12:48:47 -0400492 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400493 if (this->caps()->writePixelsRowBytesSupport()) {
494 if (rowBytes < SkToSizeT(bpp * width)) {
495 return false;
496 }
497 if (rowBytes % bpp) {
498 return false;
499 }
500 } else {
501 if (rowBytes != SkToSizeT(bpp * width)) {
502 return false;
503 }
504 }
505
jvanverth17aa0472016-01-05 10:41:27 -0800506 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400507 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
508 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700509 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500510 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800511 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700512
jvanverth17aa0472016-01-05 10:41:27 -0800513 return true;
514 }
515 return false;
516}
517
Brian Salomon26de56e2019-04-10 12:14:26 -0400518bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400519 GrColorType surfaceColorType, GrColorType bufferColorType,
520 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400521 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400522 SkASSERT(surface);
523 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400524 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400525
Greg Danielba88ab62019-07-26 09:14:01 -0400526#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400527 auto supportedRead = this->caps()->supportedReadPixelsColorType(
528 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400529 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
530 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
531#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400532
533 // We require that the write region is contained in the texture
534 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
535 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
536 if (!bounds.contains(subRect)) {
537 return false;
538 }
539
540 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400541 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
542 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400543 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400544 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400545 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400546 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400547}
548
Brian Salomon930f9392018-06-20 16:25:26 -0400549bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400550 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400551 SkASSERT(texture);
Brian Salomon69100f02020-07-21 10:49:25 -0400552 SkASSERT(this->caps()->mipmapSupport());
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400553 SkASSERT(texture->mipmapped() == GrMipmapped::kYes);
554 if (!texture->mipmapsAreDirty()) {
Chris Dalton16a33c62019-09-24 22:19:17 -0600555 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
556 // actual target. This may be caused by things that the drawingManager could not predict,
557 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
558 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
559 return true;
560 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500561 if (texture->readOnly()) {
562 return false;
563 }
Brian Salomon930f9392018-06-20 16:25:26 -0400564 if (this->onRegenerateMipMapLevels(texture)) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400565 texture->markMipmapsClean();
Brian Salomon930f9392018-06-20 16:25:26 -0400566 return true;
567 }
568 return false;
569}
570
Brian Salomon1f05d452019-02-08 12:33:08 -0500571void GrGpu::resetTextureBindings() {
572 this->handleDirtyContext();
573 this->onResetTextureBindings();
574}
575
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400576void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000577 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000578 this->handleDirtyContext();
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400579 this->onResolveRenderTarget(target, resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000580}
581
Brian Salomon1fabd512018-02-09 09:54:25 -0500582void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
583 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700584 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500585 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700586 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
587 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700588 GrTexture* texture = surface->asTexture();
589 if (texture && 1 == mipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400590 texture->markMipmapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700591 }
592 }
593}
594
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600595int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700596 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600597 SkASSERT(renderTarget->numSamples() > 1 ||
Brian Salomonf7f54332020-07-28 09:23:35 -0400598 (renderTarget->getStencilAttachment() &&
599 renderTarget->getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700600
601 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600602 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700603 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
604}
605
Greg Danielfe159622020-04-10 17:43:51 +0000606void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[],
607 int numProxies,
608 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400609 const GrFlushInfo& info,
610 const GrBackendSurfaceMutableState* newState) {
Brian Salomone39526b2019-06-24 16:35:53 -0400611 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000612
Robert Phillips9da87e02019-02-04 13:26:26 -0500613 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500614
Greg Daniel8561fc22020-04-08 12:52:51 -0400615 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
616 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500617 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400618 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400619 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400620 semaphores[i] = resourceProvider->wrapBackendSemaphore(
621 info.fSignalSemaphores[i],
622 GrResourceProvider::SemaphoreWrapType::kWillSignal,
623 kBorrow_GrWrapOwnership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400624 // If we failed to wrap the semaphore it means the client didn't give us a valid
625 // semaphore to begin with. Therefore, it is fine to not signal it.
626 if (semaphores[i]) {
627 this->insertSemaphore(semaphores[i].get());
628 }
Greg Daniel51316782017-08-02 15:10:09 +0000629 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400630 semaphores[i] = resourceProvider->makeSemaphore(false);
631 if (semaphores[i]) {
632 this->insertSemaphore(semaphores[i].get());
633 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
634 }
Greg Daniel51316782017-08-02 15:10:09 +0000635 }
636 }
637 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500638
Greg Danielfe159622020-04-10 17:43:51 +0000639 if (info.fFinishedProc) {
640 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
641 }
Greg Daniel55822f12020-05-26 11:26:45 -0400642
643 if (info.fSubmittedProc) {
644 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
645 }
646
Greg Daniel9efe3862020-06-11 11:51:06 -0400647 // We currently don't support passing in new surface state for multiple proxies here. The only
648 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
649 // updates anyways.
650 SkASSERT(!newState || numProxies == 1);
651 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
652 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, numProxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000653}
654
655bool 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
Greg Danielfe159622020-04-10 17:43:51 +0000662 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400663
Greg Daniel55822f12020-05-26 11:26:45 -0400664 this->callSubmittedProcs(submitted);
665
Greg Danielfe159622020-04-10 17:43:51 +0000666 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000667}
Brian Osman71a18892017-08-10 10:23:25 -0400668
Brian Salomon24069eb2020-06-24 10:19:52 -0400669bool GrGpu::checkAndResetOOMed() {
670 if (fOOMed) {
671 fOOMed = false;
672 return true;
673 }
674 return false;
675}
676
Greg Daniel55822f12020-05-26 11:26:45 -0400677void GrGpu::callSubmittedProcs(bool success) {
678 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
679 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
680 }
681 fSubmittedProcs.reset();
682}
683
Kevin Lubickf4def342018-10-04 12:52:50 -0400684#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400685void GrGpu::dumpJSON(SkJSONWriter* writer) const {
686 writer->beginObject();
687
688 // TODO: Is there anything useful in the base class to dump here?
689
690 this->onDumpJSON(writer);
691
692 writer->endObject();
693}
Kevin Lubickf4def342018-10-04 12:52:50 -0400694#else
695void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
696#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400697
Robert Phillipsf0ced622019-05-16 09:06:25 -0400698#if GR_TEST_UTILS
699
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500700#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500701static const char* cache_result_to_str(int i) {
702 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
703 "hits",
704 "misses",
705 "partials"
706 };
707 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
708 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
709 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
710 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
711 return kCacheResultStrings[i];
712}
713
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500714void GrGpu::Stats::dump(SkString* out) {
715 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
716 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
717 out->appendf("Textures Created: %d\n", fTextureCreates);
718 out->appendf("Texture Uploads: %d\n", fTextureUploads);
719 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400720 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500721 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
722 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400723 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500724
725 SkASSERT(fNumInlineCompilationFailures == 0);
726 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
727 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
728 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
729 fInlineProgramCacheStats[i]);
730 }
731
732 SkASSERT(fNumPreCompilationFailures == 0);
733 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
734 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
735 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
736 fPreProgramCacheStats[i]);
737 }
738
739 SkASSERT(fNumCompilationFailures == 0);
740 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
741 out->appendf("Total number of partial compilation successes %d\n",
742 fNumPartialCompilationSuccesses);
743 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500744
745 // enable this block to output CSV-style stats for program pre-compilation
746#if 0
747 SkASSERT(fNumInlineCompilationFailures == 0);
748 SkASSERT(fNumPreCompilationFailures == 0);
749 SkASSERT(fNumCompilationFailures == 0);
750 SkASSERT(fNumPartialCompilationSuccesses == 0);
751
752 SkDebugf("%d, %d, %d, %d, %d\n",
753 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
754 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
755 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
756 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
757 fNumCompilationSuccesses);
758#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500759}
760
761void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
762 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
763 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500764}
765
Robert Phillips57ef6802019-09-23 10:12:47 -0400766#endif // GR_GPU_STATS
767#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500768
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500769bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400770 GrMipmapped mipMapped,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500771 const BackendTextureData* data) {
772 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400773 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400774 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500775 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400776
Robert Phillips42716d42019-12-16 12:19:54 -0500777 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400778 return true;
779 }
780
Robert Phillips42716d42019-12-16 12:19:54 -0500781 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500782 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500783 }
784
785 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
786
Brian Salomon85c3d682019-11-04 15:04:54 -0500787 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400788 return false;
789 }
790
Brian Salomon85c3d682019-11-04 15:04:54 -0500791 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500792 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500793 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500794 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400795 return false;
796 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500797 if (colorType != data->pixmap(i).colorType()) {
798 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400799 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400800 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400801 return true;
802}
803
Robert Phillipsb915c942019-12-17 14:44:37 -0500804bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400805 GrMipmapped mipMapped, const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500806
807 if (!data || data->type() == BackendTextureData::Type::kColor) {
808 return true;
809 }
810
811 if (data->type() == BackendTextureData::Type::kPixmaps) {
812 return false;
813 }
814
815 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
816
Robert Phillips99dead92020-01-27 16:11:57 -0500817 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400818 nullptr, mipMapped == GrMipmapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500819
820 return computedSize == data->compressedSize();
821}
822
Brian Salomon85c3d682019-11-04 15:04:54 -0500823GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
824 const GrBackendFormat& format,
825 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400826 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400827 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400828 const GrCaps* caps = this->caps();
829
830 if (!format.isValid()) {
831 return {};
832 }
833
Robert Phillipsd34691b2019-09-24 13:38:43 -0400834 if (caps->isFormatCompressed(format)) {
835 // Compressed formats must go through the createCompressedBackendTexture API
836 return {};
837 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400838
Brian Salomon85c3d682019-11-04 15:04:54 -0500839 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
840 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400841 return {};
842 }
843
Brian Salomon69100f02020-07-21 10:49:25 -0400844 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400845 return {};
846 }
847
Greg Daniel16032b32020-05-06 15:31:10 -0400848 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
849}
850
851bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400852 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400853 const BackendTextureData* data) {
854 SkASSERT(data);
855 const GrCaps* caps = this->caps();
856
Greg Daniel16032b32020-05-06 15:31:10 -0400857 if (!backendTexture.isValid()) {
858 return false;
859 }
860
861 if (data->type() == BackendTextureData::Type::kPixmaps) {
862 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
863 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
864 return false;
865 }
866 }
867
Brian Salomon69100f02020-07-21 10:49:25 -0400868 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Daniel16032b32020-05-06 15:31:10 -0400869 return false;
870 }
871
Brian Salomon40a40622020-07-21 10:32:07 -0400872 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel16032b32020-05-06 15:31:10 -0400873 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) {
874 return false;
875 }
876
Greg Daniel25597782020-06-11 13:15:08 -0400877 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400878}
Robert Phillipsb915c942019-12-17 14:44:37 -0500879
880GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
881 const GrBackendFormat& format,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400882 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400883 GrProtected isProtected) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500884 const GrCaps* caps = this->caps();
885
886 if (!format.isValid()) {
887 return {};
888 }
889
Greg Daniel01f278c2020-06-12 16:58:17 -0400890 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillipsb915c942019-12-17 14:44:37 -0500891 if (compressionType == SkImage::CompressionType::kNone) {
892 // Uncompressed formats must go through the createBackendTexture API
893 return {};
894 }
895
896 if (dimensions.isEmpty() ||
897 dimensions.width() > caps->maxTextureSize() ||
898 dimensions.height() > caps->maxTextureSize()) {
899 return {};
900 }
901
Brian Salomon69100f02020-07-21 10:49:25 -0400902 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500903 return {};
904 }
905
Greg Danielaaf738c2020-07-10 09:30:33 -0400906 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped, isProtected);
907}
908
909bool GrGpu::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
910 sk_sp<GrRefCntedCallback> finishedCallback,
911 const BackendTextureData* data) {
912 SkASSERT(data);
913
914 if (!backendTexture.isValid()) {
915 return false;
Robert Phillipsb915c942019-12-17 14:44:37 -0500916 }
917
Greg Danielaaf738c2020-07-10 09:30:33 -0400918 GrBackendFormat format = backendTexture.getBackendFormat();
919
920 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
921 if (compressionType == SkImage::CompressionType::kNone) {
922 // Uncompressed formats must go through the createBackendTexture API
923 return false;
924 }
925
Brian Salomon69100f02020-07-21 10:49:25 -0400926 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Danielaaf738c2020-07-10 09:30:33 -0400927 return false;
928 }
929
Brian Salomon40a40622020-07-21 10:32:07 -0400930 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -0400931
932 if (!CompressedDataIsCorrect(backendTexture.dimensions(), compressionType, mipMapped, data)) {
933 return false;
934 }
935
936 return this->onUpdateCompressedBackendTexture(backendTexture, std::move(finishedCallback),
937 data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500938}