blob: 6dc2d8f45d16f874deba7a033e8c271d8190e102 [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"
21#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040022#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060024#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrPathRendering.h"
26#include "src/gpu/GrPipeline.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040027#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrResourceCache.h"
29#include "src/gpu/GrResourceProvider.h"
Jim Van Verth7e829b22020-07-30 17:04:35 -040030#include "src/gpu/GrRingBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrSemaphore.h"
Greg Daniela58db7f2020-07-15 09:17:59 -040032#include "src/gpu/GrStagingBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrStencilSettings.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrTextureProxyPriv.h"
35#include "src/gpu/GrTracing.h"
36#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070037
bsalomon@google.comd302f142011-03-03 13:54:13 +000038////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000039
Adlai Holler3d0359a2020-07-09 15:35:55 -040040GrGpu::GrGpu(GrDirectContext* direct) : fResetBits(kAll_GrBackendState), fContext(direct) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Stephen Whitef3d5d442020-04-08 10:35:58 -040042GrGpu::~GrGpu() {
Greg Daniel55822f12020-05-26 11:26:45 -040043 this->callSubmittedProcs(false);
Stephen Whitef3d5d442020-04-08 10:35:58 -040044}
bsalomon1d89ddc2014-08-19 14:20:58 -070045
Greg Daniela58db7f2020-07-15 09:17:59 -040046void GrGpu::disconnect(DisconnectType type) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000047
bsalomon@google.comd302f142011-03-03 13:54:13 +000048////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000049
Brian Salomona56a7462020-02-07 14:17:25 -050050static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040051 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040052 SkASSERT(mipLevelCount > 0);
53 bool hasBasePixels = texels[0].fPixels;
54 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040055 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050056 int w = dimensions.fWidth;
57 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040058 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
59 if (texels[currentMipLevel].fPixels) {
60 const size_t minRowBytes = w * bpp;
61 if (caps->writePixelsRowBytesSupport()) {
62 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
63 return false;
64 }
65 if (texels[currentMipLevel].fRowBytes % bpp) {
66 return false;
67 }
68 } else {
69 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
70 return false;
71 }
72 }
73 ++levelsWithPixelsCnt;
74 }
75 if (w == 1 && h == 1) {
76 if (currentMipLevel != mipLevelCount - 1) {
77 return false;
78 }
79 } else {
80 w = std::max(w / 2, 1);
81 h = std::max(h / 2, 1);
82 }
83 }
84 // Either just a base layer or a full stack is required.
85 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
86 return false;
87 }
88 // Can specify just the base, all levels, or no levels.
89 if (!hasBasePixels) {
90 return levelsWithPixelsCnt == 0;
91 }
Brian Salomond2a8ae22019-09-10 16:03:59 -040092 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -040093}
94
Brian Salomona56a7462020-02-07 14:17:25 -050095sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -040096 const GrBackendFormat& format,
97 GrRenderable renderable,
98 int renderTargetSampleCnt,
99 SkBudgeted budgeted,
100 GrProtected isProtected,
101 int mipLevelCount,
102 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400103 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400104 // Call GrGpu::createCompressedTexture.
105 return nullptr;
106 }
cblume55f2d2d2016-02-26 13:20:48 -0800107
Brian Salomon7e67dca2020-07-21 09:27:25 -0400108 GrMipmapped mipMapped = mipLevelCount > 1 ? GrMipmapped::kYes : GrMipmapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500109 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
110 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700112 }
113
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400114 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400115 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400116 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500117 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400118 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400119 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400120 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500121 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400122 format,
123 renderable,
124 renderTargetSampleCnt,
125 budgeted,
126 isProtected,
127 mipLevelCount,
128 levelClearMask);
129 if (tex) {
130 SkASSERT(tex->backendFormat() == format);
131 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
132 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
133 tex->resourcePriv().removeScratchKey();
134 }
135 fStats.incTextureCreates();
136 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
137 SkASSERT(GrRenderable::kYes == renderable);
138 tex->asRenderTarget()->setRequiresManualMSAAResolve();
139 }
140 }
141 return tex;
142}
143
Brian Salomona56a7462020-02-07 14:17:25 -0500144sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400145 const GrBackendFormat& format,
146 GrRenderable renderable,
147 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400148 GrMipmapped mipMapped,
Brian Salomona90382f2019-09-17 09:01:56 -0400149 SkBudgeted budgeted,
150 GrProtected isProtected) {
151 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400152 if (mipMapped == GrMipmapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500153 mipLevelCount =
154 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400155 }
156 uint32_t levelClearMask =
157 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500158 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
159 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400160 if (tex && mipMapped == GrMipmapped::kYes && levelClearMask) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400161 tex->markMipmapsClean();
Brian Salomona90382f2019-09-17 09:01:56 -0400162 }
163 return tex;
164}
165
Brian Salomona56a7462020-02-07 14:17:25 -0500166sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400167 const GrBackendFormat& format,
168 GrRenderable renderable,
169 int renderTargetSampleCnt,
170 SkBudgeted budgeted,
171 GrProtected isProtected,
172 GrColorType textureColorType,
173 GrColorType srcColorType,
174 const GrMipLevel texels[],
175 int texelLevelCount) {
176 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400177 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500178 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400179 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400180 return nullptr;
181 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400182 }
183
Brian Osman788b9162020-02-07 10:36:46 -0500184 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400185 uint32_t levelClearMask = 0;
186 if (this->caps()->shouldInitializeTextures()) {
187 if (texelLevelCount) {
188 for (int i = 0; i < mipLevelCount; ++i) {
189 if (!texels->fPixels) {
190 levelClearMask |= static_cast<uint32_t>(1 << i);
191 }
192 }
193 } else {
194 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
195 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400196 }
197
Brian Salomona56a7462020-02-07 14:17:25 -0500198 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
199 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800200 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400201 bool markMipLevelsClean = false;
202 // Currently if level 0 does not have pixels then no other level may, as enforced by
203 // validate_texel_levels.
204 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500205 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
206 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400207 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800208 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400209 // Currently if level[1] of mip map has pixel data then so must all other levels.
210 // as enforced by validate_texel_levels.
211 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
212 fStats.incTextureUploads();
213 } else if (levelClearMask && mipLevelCount > 1) {
214 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800215 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400216 if (markMipLevelsClean) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400217 tex->markMipmapsClean();
Brian Salomond2a8ae22019-09-10 16:03:59 -0400218 }
bsalomonb12ea412015-02-02 21:19:50 -0800219 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000220 return tex;
221}
222
Robert Phillips9f744f72019-12-19 19:14:33 -0500223sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400224 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500225 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400226 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500227 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500228 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400229 size_t dataSize) {
230 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500231 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
232 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400233 return nullptr;
234 }
Brian Salomona3e29962019-07-16 11:52:08 -0400235 // Note if we relax the requirement that data must be provided then we must check
236 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400237 if (!data) {
238 return nullptr;
239 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400240 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400241 return nullptr;
242 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500243
244 // TODO: expand CompressedDataIsCorrect to work here too
Greg Daniel01f278c2020-06-12 16:58:17 -0400245 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillips9f744f72019-12-19 19:14:33 -0500246
Robert Phillips99dead92020-01-27 16:11:57 -0500247 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400248 mipMapped == GrMipmapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400249 return nullptr;
250 }
Robert Phillips3a833922020-01-21 15:25:58 -0500251 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
252 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400253}
254
Greg Daniel7ef28f32017-04-20 16:41:55 +0000255sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400256 GrWrapOwnership ownership,
257 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500258 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500259 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000260 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400261
262 const GrCaps* caps = this->caps();
263 SkASSERT(caps);
264
Greg Daniel7bfc9132019-08-14 14:23:53 -0400265 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800266 return nullptr;
267 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400268 if (backendTex.width() > caps->maxTextureSize() ||
269 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800270 return nullptr;
271 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400272
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400273 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400274}
Eric Karl5c779752017-05-08 12:02:07 -0700275
Robert Phillipsb915c942019-12-17 14:44:37 -0500276sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
277 GrWrapOwnership ownership,
278 GrWrapCacheable cacheable) {
279 this->handleDirtyContext();
280
281 const GrCaps* caps = this->caps();
282 SkASSERT(caps);
283
284 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
285 return nullptr;
286 }
287 if (backendTex.width() > caps->maxTextureSize() ||
288 backendTex.height() > caps->maxTextureSize()) {
289 return nullptr;
290 }
291
292 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
293}
294
Brian Salomond17f6582017-07-19 18:28:58 -0400295sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400296 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400297 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500298 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400299 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500300 if (sampleCnt < 1) {
301 return nullptr;
302 }
Robert Phillips0902c982019-07-16 07:47:56 -0400303
304 const GrCaps* caps = this->caps();
305
Greg Daniel7bfc9132019-08-14 14:23:53 -0400306 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400307 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400308 return nullptr;
309 }
310
Robert Phillips0902c982019-07-16 07:47:56 -0400311 if (backendTex.width() > caps->maxRenderTargetSize() ||
312 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400313 return nullptr;
314 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400315 sk_sp<GrTexture> tex =
316 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400317 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600318 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
319 tex->asRenderTarget()->setRequiresManualMSAAResolve();
320 }
bungeman6bd52842016-10-27 09:30:08 -0700321 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000322}
323
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400324sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400325 this->handleDirtyContext();
326
327 const GrCaps* caps = this->caps();
328
Greg Daniel6fa62e22019-08-07 15:52:37 -0400329 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800330 return nullptr;
331 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400332
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400333 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500334 if (backendRT.isFramebufferOnly()) {
335 rt->setFramebufferOnly();
336 }
337 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000338}
339
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400340sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400341 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500342 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400343
344 const GrCaps* caps = this->caps();
345
346 int maxSize = caps->maxTextureSize();
347 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
348 return nullptr;
349 }
350
Greg Daniel6fa62e22019-08-07 15:52:37 -0400351 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400352 return nullptr;
353 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400354
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400355 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Chris Dalton3f7932e2019-08-19 00:39:13 -0600356 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
357 rt->setRequiresManualMSAAResolve();
358 }
359 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800360}
361
Greg Danielb46add82019-01-02 14:51:29 -0500362sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
363 const GrVkDrawableInfo& vkInfo) {
364 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
365}
366
367sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
368 const GrVkDrawableInfo& vkInfo) {
369 // This is only supported on Vulkan so we default to returning nullptr here
370 return nullptr;
371}
372
Brian Salomondbf70722019-02-07 11:31:24 -0500373sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
374 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400375 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000376 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500377 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700378 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700379 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700380 }
cdalton397536c2016-03-25 12:15:03 -0700381 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800382}
383
Greg Daniel46cfbc62019-06-07 11:43:30 -0400384bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400385 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400386 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700387 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500388 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500389
390 if (dst->readOnly()) {
391 return false;
392 }
393
joshualitt1cbdcde2015-08-21 11:53:29 -0700394 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500395
Greg Daniele227fe42019-08-21 13:52:24 -0400396 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700397}
398
Brian Salomona6948702018-06-01 15:33:20 -0400399bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400400 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
401 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400402 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500403 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500404 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400405 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500406
Brian Salomon1d435302019-07-01 13:05:28 -0400407 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
408 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
409 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700410 return false;
411 }
412
Brian Salomon1047a492019-07-02 12:25:21 -0400413 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
414 if (!this->caps()->readPixelsRowBytesSupport()) {
415 if (rowBytes != minRowBytes) {
416 return false;
417 }
418 } else {
419 if (rowBytes < minRowBytes) {
420 return false;
421 }
422 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
423 return false;
424 }
425 }
426
Brian Salomonbf7b6202016-11-11 16:08:03 -0500427 this->handleDirtyContext();
428
Brian Salomonf77c1462019-08-01 15:19:29 -0400429 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
430 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000431}
432
Brian Salomona9b04b92018-06-01 15:04:28 -0400433bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400434 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400435 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400436 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Leon Scroggins III569f07c2020-07-24 16:10:58 -0400437 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Upload %ix%i Texture", width, height);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500438 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500439 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500440
441 if (surface->readOnly()) {
442 return false;
443 }
444
Brian Salomon1047a492019-07-02 12:25:21 -0400445 if (mipLevelCount == 0) {
446 return false;
447 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400448 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400449 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
450 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400451 if (!bounds.contains(subRect)) {
452 return false;
453 }
454 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
455 // We require that if the texels are mipped, than the write region is the entire surface
456 return false;
457 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400458
Brian Salomona56a7462020-02-07 14:17:25 -0500459 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
460 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400461 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800462 }
jvanverth2dc29942015-09-01 07:16:46 -0700463
bsalomon@google.com6f379512011-11-16 20:36:03 +0000464 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400465 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400466 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700467 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400468 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800469 fStats.incTextureUploads();
470 return true;
471 }
472 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000473}
474
Brian Salomone05ba5a2019-04-08 11:59:07 -0400475bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400476 GrColorType textureColorType, GrColorType bufferColorType,
477 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400478 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500479 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700480 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800481
Brian Salomonc67c31c2018-12-06 10:00:03 -0500482 if (texture->readOnly()) {
483 return false;
484 }
485
Greg Daniel660cc992017-06-26 14:55:05 -0400486 // We require that the write region is contained in the texture
487 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
488 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
489 if (!bounds.contains(subRect)) {
490 return false;
491 }
492
Brian Salomonb28cb682019-07-26 12:48:47 -0400493 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400494 if (this->caps()->writePixelsRowBytesSupport()) {
495 if (rowBytes < SkToSizeT(bpp * width)) {
496 return false;
497 }
498 if (rowBytes % bpp) {
499 return false;
500 }
501 } else {
502 if (rowBytes != SkToSizeT(bpp * width)) {
503 return false;
504 }
505 }
506
jvanverth17aa0472016-01-05 10:41:27 -0800507 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400508 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
509 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700510 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500511 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800512 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700513
jvanverth17aa0472016-01-05 10:41:27 -0800514 return true;
515 }
516 return false;
517}
518
Brian Salomon26de56e2019-04-10 12:14:26 -0400519bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400520 GrColorType surfaceColorType, GrColorType bufferColorType,
521 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400522 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400523 SkASSERT(surface);
524 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400525 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400526
Greg Danielba88ab62019-07-26 09:14:01 -0400527#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400528 auto supportedRead = this->caps()->supportedReadPixelsColorType(
529 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400530 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
531 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
532#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400533
534 // We require that the write region is contained in the texture
535 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
536 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
537 if (!bounds.contains(subRect)) {
538 return false;
539 }
540
541 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400542 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
543 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400544 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400545 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400546 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400547 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400548}
549
Brian Salomon930f9392018-06-20 16:25:26 -0400550bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400551 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400552 SkASSERT(texture);
Brian Salomon69100f02020-07-21 10:49:25 -0400553 SkASSERT(this->caps()->mipmapSupport());
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400554 SkASSERT(texture->mipmapped() == GrMipmapped::kYes);
555 if (!texture->mipmapsAreDirty()) {
Chris Dalton16a33c62019-09-24 22:19:17 -0600556 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
557 // actual target. This may be caused by things that the drawingManager could not predict,
558 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
559 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
560 return true;
561 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500562 if (texture->readOnly()) {
563 return false;
564 }
Brian Salomon930f9392018-06-20 16:25:26 -0400565 if (this->onRegenerateMipMapLevels(texture)) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400566 texture->markMipmapsClean();
Brian Salomon930f9392018-06-20 16:25:26 -0400567 return true;
568 }
569 return false;
570}
571
Brian Salomon1f05d452019-02-08 12:33:08 -0500572void GrGpu::resetTextureBindings() {
573 this->handleDirtyContext();
574 this->onResetTextureBindings();
575}
576
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400577void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000578 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000579 this->handleDirtyContext();
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400580 this->onResolveRenderTarget(target, resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000581}
582
Brian Salomon1fabd512018-02-09 09:54:25 -0500583void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
584 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700585 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500586 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700587 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
588 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700589 GrTexture* texture = surface->asTexture();
590 if (texture && 1 == mipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400591 texture->markMipmapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700592 }
593 }
594}
595
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600596int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700597 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600598 SkASSERT(renderTarget->numSamples() > 1 ||
Brian Salomonf7f54332020-07-28 09:23:35 -0400599 (renderTarget->getStencilAttachment() &&
600 renderTarget->getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700601
602 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600603 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700604 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
605}
606
Greg Danielfe159622020-04-10 17:43:51 +0000607void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[],
608 int numProxies,
609 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400610 const GrFlushInfo& info,
611 const GrBackendSurfaceMutableState* newState) {
Brian Salomone39526b2019-06-24 16:35:53 -0400612 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000613
Robert Phillips9da87e02019-02-04 13:26:26 -0500614 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500615
Greg Daniel8561fc22020-04-08 12:52:51 -0400616 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
617 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500618 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400619 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400620 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400621 semaphores[i] = resourceProvider->wrapBackendSemaphore(
622 info.fSignalSemaphores[i],
623 GrResourceProvider::SemaphoreWrapType::kWillSignal,
624 kBorrow_GrWrapOwnership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400625 // If we failed to wrap the semaphore it means the client didn't give us a valid
626 // semaphore to begin with. Therefore, it is fine to not signal it.
627 if (semaphores[i]) {
628 this->insertSemaphore(semaphores[i].get());
629 }
Greg Daniel51316782017-08-02 15:10:09 +0000630 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400631 semaphores[i] = resourceProvider->makeSemaphore(false);
632 if (semaphores[i]) {
633 this->insertSemaphore(semaphores[i].get());
634 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
635 }
Greg Daniel51316782017-08-02 15:10:09 +0000636 }
637 }
638 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500639
Greg Danielfe159622020-04-10 17:43:51 +0000640 if (info.fFinishedProc) {
641 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
642 }
Greg Daniel55822f12020-05-26 11:26:45 -0400643
644 if (info.fSubmittedProc) {
645 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
646 }
647
Greg Daniel9efe3862020-06-11 11:51:06 -0400648 // We currently don't support passing in new surface state for multiple proxies here. The only
649 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
650 // updates anyways.
651 SkASSERT(!newState || numProxies == 1);
652 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
653 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, numProxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000654}
655
656bool 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 Danielfe159622020-04-10 17:43:51 +0000671 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000672}
Brian Osman71a18892017-08-10 10:23:25 -0400673
Brian Salomon24069eb2020-06-24 10:19:52 -0400674bool GrGpu::checkAndResetOOMed() {
675 if (fOOMed) {
676 fOOMed = false;
677 return true;
678 }
679 return false;
680}
681
Greg Daniel55822f12020-05-26 11:26:45 -0400682void GrGpu::callSubmittedProcs(bool success) {
683 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
684 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
685 }
686 fSubmittedProcs.reset();
687}
688
Kevin Lubickf4def342018-10-04 12:52:50 -0400689#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400690void GrGpu::dumpJSON(SkJSONWriter* writer) const {
691 writer->beginObject();
692
693 // TODO: Is there anything useful in the base class to dump here?
694
695 this->onDumpJSON(writer);
696
697 writer->endObject();
698}
Kevin Lubickf4def342018-10-04 12:52:50 -0400699#else
700void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
701#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400702
Robert Phillipsf0ced622019-05-16 09:06:25 -0400703#if GR_TEST_UTILS
704
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500705#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500706static const char* cache_result_to_str(int i) {
707 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
708 "hits",
709 "misses",
710 "partials"
711 };
712 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
713 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
714 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
715 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
716 return kCacheResultStrings[i];
717}
718
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500719void GrGpu::Stats::dump(SkString* out) {
720 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
721 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
722 out->appendf("Textures Created: %d\n", fTextureCreates);
723 out->appendf("Texture Uploads: %d\n", fTextureUploads);
724 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400725 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500726 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
727 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400728 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500729
730 SkASSERT(fNumInlineCompilationFailures == 0);
731 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
732 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
733 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
734 fInlineProgramCacheStats[i]);
735 }
736
737 SkASSERT(fNumPreCompilationFailures == 0);
738 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
739 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
740 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
741 fPreProgramCacheStats[i]);
742 }
743
744 SkASSERT(fNumCompilationFailures == 0);
745 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
746 out->appendf("Total number of partial compilation successes %d\n",
747 fNumPartialCompilationSuccesses);
748 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500749
750 // enable this block to output CSV-style stats for program pre-compilation
751#if 0
752 SkASSERT(fNumInlineCompilationFailures == 0);
753 SkASSERT(fNumPreCompilationFailures == 0);
754 SkASSERT(fNumCompilationFailures == 0);
755 SkASSERT(fNumPartialCompilationSuccesses == 0);
756
757 SkDebugf("%d, %d, %d, %d, %d\n",
758 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
759 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
760 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
761 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
762 fNumCompilationSuccesses);
763#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500764}
765
766void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
767 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
768 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500769}
770
Robert Phillips57ef6802019-09-23 10:12:47 -0400771#endif // GR_GPU_STATS
772#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500773
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500774bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400775 GrMipmapped mipMapped,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500776 const BackendTextureData* data) {
777 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400778 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400779 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500780 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400781
Robert Phillips42716d42019-12-16 12:19:54 -0500782 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400783 return true;
784 }
785
Robert Phillips42716d42019-12-16 12:19:54 -0500786 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500787 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500788 }
789
790 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
791
Brian Salomon85c3d682019-11-04 15:04:54 -0500792 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400793 return false;
794 }
795
Brian Salomon85c3d682019-11-04 15:04:54 -0500796 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500797 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500798 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500799 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400800 return false;
801 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500802 if (colorType != data->pixmap(i).colorType()) {
803 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400804 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400805 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400806 return true;
807}
808
Robert Phillipsb915c942019-12-17 14:44:37 -0500809bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400810 GrMipmapped mipMapped, const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500811
812 if (!data || data->type() == BackendTextureData::Type::kColor) {
813 return true;
814 }
815
816 if (data->type() == BackendTextureData::Type::kPixmaps) {
817 return false;
818 }
819
820 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
821
Robert Phillips99dead92020-01-27 16:11:57 -0500822 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400823 nullptr, mipMapped == GrMipmapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500824
825 return computedSize == data->compressedSize();
826}
827
Brian Salomon85c3d682019-11-04 15:04:54 -0500828GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
829 const GrBackendFormat& format,
830 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400831 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400832 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400833 const GrCaps* caps = this->caps();
834
835 if (!format.isValid()) {
836 return {};
837 }
838
Robert Phillipsd34691b2019-09-24 13:38:43 -0400839 if (caps->isFormatCompressed(format)) {
840 // Compressed formats must go through the createCompressedBackendTexture API
841 return {};
842 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400843
Brian Salomon85c3d682019-11-04 15:04:54 -0500844 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
845 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400846 return {};
847 }
848
Brian Salomon69100f02020-07-21 10:49:25 -0400849 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400850 return {};
851 }
852
Greg Daniel16032b32020-05-06 15:31:10 -0400853 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
854}
855
856bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400857 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400858 const BackendTextureData* data) {
859 SkASSERT(data);
860 const GrCaps* caps = this->caps();
861
Greg Daniel16032b32020-05-06 15:31:10 -0400862 if (!backendTexture.isValid()) {
863 return false;
864 }
865
866 if (data->type() == BackendTextureData::Type::kPixmaps) {
867 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
868 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
869 return false;
870 }
871 }
872
Brian Salomon69100f02020-07-21 10:49:25 -0400873 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Daniel16032b32020-05-06 15:31:10 -0400874 return false;
875 }
876
Brian Salomon40a40622020-07-21 10:32:07 -0400877 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel16032b32020-05-06 15:31:10 -0400878 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) {
879 return false;
880 }
881
Greg Daniel25597782020-06-11 13:15:08 -0400882 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400883}
Robert Phillipsb915c942019-12-17 14:44:37 -0500884
885GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
886 const GrBackendFormat& format,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400887 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400888 GrProtected isProtected) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500889 const GrCaps* caps = this->caps();
890
891 if (!format.isValid()) {
892 return {};
893 }
894
Greg Daniel01f278c2020-06-12 16:58:17 -0400895 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillipsb915c942019-12-17 14:44:37 -0500896 if (compressionType == SkImage::CompressionType::kNone) {
897 // Uncompressed formats must go through the createBackendTexture API
898 return {};
899 }
900
901 if (dimensions.isEmpty() ||
902 dimensions.width() > caps->maxTextureSize() ||
903 dimensions.height() > caps->maxTextureSize()) {
904 return {};
905 }
906
Brian Salomon69100f02020-07-21 10:49:25 -0400907 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500908 return {};
909 }
910
Greg Danielaaf738c2020-07-10 09:30:33 -0400911 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped, isProtected);
912}
913
914bool GrGpu::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
915 sk_sp<GrRefCntedCallback> finishedCallback,
916 const BackendTextureData* data) {
917 SkASSERT(data);
918
919 if (!backendTexture.isValid()) {
920 return false;
Robert Phillipsb915c942019-12-17 14:44:37 -0500921 }
922
Greg Danielaaf738c2020-07-10 09:30:33 -0400923 GrBackendFormat format = backendTexture.getBackendFormat();
924
925 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
926 if (compressionType == SkImage::CompressionType::kNone) {
927 // Uncompressed formats must go through the createBackendTexture API
928 return false;
929 }
930
Brian Salomon69100f02020-07-21 10:49:25 -0400931 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Danielaaf738c2020-07-10 09:30:33 -0400932 return false;
933 }
934
Brian Salomon40a40622020-07-21 10:32:07 -0400935 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -0400936
937 if (!CompressedDataIsCorrect(backendTexture.dimensions(), compressionType, mipMapped, data)) {
938 return false;
939 }
940
941 return this->onUpdateCompressedBackendTexture(backendTexture, std::move(finishedCallback),
942 data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500943}