blob: 5bc99f0035486e7dc5bf70c78ea9cf11da986b82 [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"
13#include "include/gpu/GrContext.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"
Robert Phillips57ef6802019-09-23 10:12:47 -040016#include "src/core/SkMipMap.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrCaps.h"
19#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040020#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060022#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrPathRendering.h"
24#include "src/gpu/GrPipeline.h"
25#include "src/gpu/GrRenderTargetPriv.h"
26#include "src/gpu/GrResourceCache.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrSemaphore.h"
Stephen Whitef3d5d442020-04-08 10:35:58 -040029#include "src/gpu/GrStagingBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrStencilAttachment.h"
31#include "src/gpu/GrStencilSettings.h"
32#include "src/gpu/GrSurfacePriv.h"
33#include "src/gpu/GrTexturePriv.h"
34#include "src/gpu/GrTextureProxyPriv.h"
35#include "src/gpu/GrTracing.h"
36#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070037
Stephen Whitef3d5d442020-04-08 10:35:58 -040038static const size_t kMinStagingBufferSize = 32 * 1024;
39
bsalomon@google.comd302f142011-03-03 13:54:13 +000040////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Brian Salomone2826ab2019-06-04 15:58:31 -040042GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000043
Stephen Whitef3d5d442020-04-08 10:35:58 -040044GrGpu::~GrGpu() {
45 SkASSERT(fBusyStagingBuffers.isEmpty());
46}
bsalomon1d89ddc2014-08-19 14:20:58 -070047
Stephen White7a026142020-05-13 17:16:33 -040048void GrGpu::disconnect(DisconnectType type) {
49 if (DisconnectType::kAbandon == type) {
50 fAvailableStagingBuffers.reset();
51 fActiveStagingBuffers.reset();
52 fBusyStagingBuffers.reset();
53 }
54 fStagingBuffers.clear();
55}
reed@google.comac10a2d2010-12-22 21:39:39 +000056
bsalomon@google.comd302f142011-03-03 13:54:13 +000057////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000058
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000059bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
Brian Salomonc8d092a2020-02-24 10:14:21 -050060 GrSamplerState::Filter filter) {
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000061 SkASSERT(texProxy);
Brian Salomonc8d092a2020-02-24 10:14:21 -050062 if (filter != GrSamplerState::Filter::kMipMap || texProxy->mipMapped() == GrMipMapped::kYes ||
63 !caps->mipMapSupport()) {
64 return false;
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000065 }
Brian Salomonc8d092a2020-02-24 10:14:21 -050066 return SkMipMap::ComputeLevelCount(texProxy->width(), texProxy->height()) > 0;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040067}
68
Brian Salomona56a7462020-02-07 14:17:25 -050069static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040070 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040071 SkASSERT(mipLevelCount > 0);
72 bool hasBasePixels = texels[0].fPixels;
73 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040074 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050075 int w = dimensions.fWidth;
76 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040077 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
78 if (texels[currentMipLevel].fPixels) {
79 const size_t minRowBytes = w * bpp;
80 if (caps->writePixelsRowBytesSupport()) {
81 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
82 return false;
83 }
84 if (texels[currentMipLevel].fRowBytes % bpp) {
85 return false;
86 }
87 } else {
88 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
89 return false;
90 }
91 }
92 ++levelsWithPixelsCnt;
93 }
94 if (w == 1 && h == 1) {
95 if (currentMipLevel != mipLevelCount - 1) {
96 return false;
97 }
98 } else {
99 w = std::max(w / 2, 1);
100 h = std::max(h / 2, 1);
101 }
102 }
103 // Either just a base layer or a full stack is required.
104 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
105 return false;
106 }
107 // Can specify just the base, all levels, or no levels.
108 if (!hasBasePixels) {
109 return levelsWithPixelsCnt == 0;
110 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400111 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400112}
113
Brian Salomona56a7462020-02-07 14:17:25 -0500114sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400115 const GrBackendFormat& format,
116 GrRenderable renderable,
117 int renderTargetSampleCnt,
118 SkBudgeted budgeted,
119 GrProtected isProtected,
120 int mipLevelCount,
121 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400122 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400123 // Call GrGpu::createCompressedTexture.
124 return nullptr;
125 }
cblume55f2d2d2016-02-26 13:20:48 -0800126
Brian Salomona90382f2019-09-17 09:01:56 -0400127 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500128 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
129 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700130 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700131 }
132
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400133 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400134 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400135 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500136 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400137 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400138 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400139 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500140 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400141 format,
142 renderable,
143 renderTargetSampleCnt,
144 budgeted,
145 isProtected,
146 mipLevelCount,
147 levelClearMask);
148 if (tex) {
149 SkASSERT(tex->backendFormat() == format);
150 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
151 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
152 tex->resourcePriv().removeScratchKey();
153 }
154 fStats.incTextureCreates();
155 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
156 SkASSERT(GrRenderable::kYes == renderable);
157 tex->asRenderTarget()->setRequiresManualMSAAResolve();
158 }
159 }
160 return tex;
161}
162
Brian Salomona56a7462020-02-07 14:17:25 -0500163sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400164 const GrBackendFormat& format,
165 GrRenderable renderable,
166 int renderTargetSampleCnt,
167 GrMipMapped mipMapped,
168 SkBudgeted budgeted,
169 GrProtected isProtected) {
170 int mipLevelCount = 1;
171 if (mipMapped == GrMipMapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500172 mipLevelCount =
173 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400174 }
175 uint32_t levelClearMask =
176 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500177 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
178 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomona90382f2019-09-17 09:01:56 -0400179 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
180 tex->texturePriv().markMipMapsClean();
181 }
182 return tex;
183}
184
Brian Salomona56a7462020-02-07 14:17:25 -0500185sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400186 const GrBackendFormat& format,
187 GrRenderable renderable,
188 int renderTargetSampleCnt,
189 SkBudgeted budgeted,
190 GrProtected isProtected,
191 GrColorType textureColorType,
192 GrColorType srcColorType,
193 const GrMipLevel texels[],
194 int texelLevelCount) {
195 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400196 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500197 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400198 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400199 return nullptr;
200 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400201 }
202
Brian Osman788b9162020-02-07 10:36:46 -0500203 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400204 uint32_t levelClearMask = 0;
205 if (this->caps()->shouldInitializeTextures()) {
206 if (texelLevelCount) {
207 for (int i = 0; i < mipLevelCount; ++i) {
208 if (!texels->fPixels) {
209 levelClearMask |= static_cast<uint32_t>(1 << i);
210 }
211 }
212 } else {
213 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
214 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400215 }
216
Brian Salomona56a7462020-02-07 14:17:25 -0500217 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
218 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800219 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400220 bool markMipLevelsClean = false;
221 // Currently if level 0 does not have pixels then no other level may, as enforced by
222 // validate_texel_levels.
223 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500224 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
225 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400226 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800227 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400228 // Currently if level[1] of mip map has pixel data then so must all other levels.
229 // as enforced by validate_texel_levels.
230 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
231 fStats.incTextureUploads();
232 } else if (levelClearMask && mipLevelCount > 1) {
233 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800234 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400235 if (markMipLevelsClean) {
236 tex->texturePriv().markMipMapsClean();
237 }
bsalomonb12ea412015-02-02 21:19:50 -0800238 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000239 return tex;
240}
241
Robert Phillips9f744f72019-12-19 19:14:33 -0500242sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400243 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500244 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500245 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500246 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500247 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400248 size_t dataSize) {
249 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500250 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
251 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400252 return nullptr;
253 }
Brian Salomona3e29962019-07-16 11:52:08 -0400254 // Note if we relax the requirement that data must be provided then we must check
255 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400256 if (!data) {
257 return nullptr;
258 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400259 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400260 return nullptr;
261 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500262
263 // TODO: expand CompressedDataIsCorrect to work here too
264 SkImage::CompressionType compressionType = this->caps()->compressionType(format);
265
Robert Phillips99dead92020-01-27 16:11:57 -0500266 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
267 mipMapped == GrMipMapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400268 return nullptr;
269 }
Robert Phillips3a833922020-01-21 15:25:58 -0500270 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
271 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400272}
273
Greg Daniel7ef28f32017-04-20 16:41:55 +0000274sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400275 GrWrapOwnership ownership,
276 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500277 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500278 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000279 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400280
281 const GrCaps* caps = this->caps();
282 SkASSERT(caps);
283
Greg Daniel7bfc9132019-08-14 14:23:53 -0400284 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800285 return nullptr;
286 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400287 if (backendTex.width() > caps->maxTextureSize() ||
288 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800289 return nullptr;
290 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400291
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400292 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400293}
Eric Karl5c779752017-05-08 12:02:07 -0700294
Robert Phillipsb915c942019-12-17 14:44:37 -0500295sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
296 GrWrapOwnership ownership,
297 GrWrapCacheable cacheable) {
298 this->handleDirtyContext();
299
300 const GrCaps* caps = this->caps();
301 SkASSERT(caps);
302
303 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
304 return nullptr;
305 }
306 if (backendTex.width() > caps->maxTextureSize() ||
307 backendTex.height() > caps->maxTextureSize()) {
308 return nullptr;
309 }
310
311 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
312}
313
Brian Salomond17f6582017-07-19 18:28:58 -0400314sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400315 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400316 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500317 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400318 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500319 if (sampleCnt < 1) {
320 return nullptr;
321 }
Robert Phillips0902c982019-07-16 07:47:56 -0400322
323 const GrCaps* caps = this->caps();
324
Greg Daniel7bfc9132019-08-14 14:23:53 -0400325 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400326 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400327 return nullptr;
328 }
329
Robert Phillips0902c982019-07-16 07:47:56 -0400330 if (backendTex.width() > caps->maxRenderTargetSize() ||
331 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400332 return nullptr;
333 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400334 sk_sp<GrTexture> tex =
335 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400336 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600337 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
338 tex->asRenderTarget()->setRequiresManualMSAAResolve();
339 }
bungeman6bd52842016-10-27 09:30:08 -0700340 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000341}
342
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400343sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400344 this->handleDirtyContext();
345
346 const GrCaps* caps = this->caps();
347
Greg Daniel6fa62e22019-08-07 15:52:37 -0400348 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800349 return nullptr;
350 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400351
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400352 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500353 if (backendRT.isFramebufferOnly()) {
354 rt->setFramebufferOnly();
355 }
356 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000357}
358
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400359sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400360 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500361 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400362
363 const GrCaps* caps = this->caps();
364
365 int maxSize = caps->maxTextureSize();
366 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
367 return nullptr;
368 }
369
Greg Daniel6fa62e22019-08-07 15:52:37 -0400370 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400371 return nullptr;
372 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400373
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400374 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Chris Dalton3f7932e2019-08-19 00:39:13 -0600375 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
376 rt->setRequiresManualMSAAResolve();
377 }
378 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800379}
380
Greg Danielb46add82019-01-02 14:51:29 -0500381sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
382 const GrVkDrawableInfo& vkInfo) {
383 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
384}
385
386sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
387 const GrVkDrawableInfo& vkInfo) {
388 // This is only supported on Vulkan so we default to returning nullptr here
389 return nullptr;
390}
391
Brian Salomondbf70722019-02-07 11:31:24 -0500392sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
393 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400394 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000395 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500396 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700397 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700398 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700399 }
cdalton397536c2016-03-25 12:15:03 -0700400 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800401}
402
Greg Daniel46cfbc62019-06-07 11:43:30 -0400403bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400404 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400405 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700406 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500407 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500408
409 if (dst->readOnly()) {
410 return false;
411 }
412
joshualitt1cbdcde2015-08-21 11:53:29 -0700413 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500414
Greg Daniele227fe42019-08-21 13:52:24 -0400415 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700416}
417
Brian Salomona6948702018-06-01 15:33:20 -0400418bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400419 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
420 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400421 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500422 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500423 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400424 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500425
Brian Salomon1d435302019-07-01 13:05:28 -0400426 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
427 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
428 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700429 return false;
430 }
431
Brian Salomon1047a492019-07-02 12:25:21 -0400432 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
433 if (!this->caps()->readPixelsRowBytesSupport()) {
434 if (rowBytes != minRowBytes) {
435 return false;
436 }
437 } else {
438 if (rowBytes < minRowBytes) {
439 return false;
440 }
441 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
442 return false;
443 }
444 }
445
Brian Salomonbf7b6202016-11-11 16:08:03 -0500446 this->handleDirtyContext();
447
Brian Salomonf77c1462019-08-01 15:19:29 -0400448 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
449 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000450}
451
Brian Salomona9b04b92018-06-01 15:04:28 -0400452bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400453 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400454 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400455 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev1db4d142020-04-10 00:58:52 -0400456 ATRACE_ANDROID_FRAMEWORK_ALWAYS("texture_upload");
Brian Salomonbf7b6202016-11-11 16:08:03 -0500457 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500458 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500459
460 if (surface->readOnly()) {
461 return false;
462 }
463
Brian Salomon1047a492019-07-02 12:25:21 -0400464 if (mipLevelCount == 0) {
465 return false;
466 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400467 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400468 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
469 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400470 if (!bounds.contains(subRect)) {
471 return false;
472 }
473 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
474 // We require that if the texels are mipped, than the write region is the entire surface
475 return false;
476 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400477
Brian Salomona56a7462020-02-07 14:17:25 -0500478 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
479 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400480 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800481 }
jvanverth2dc29942015-09-01 07:16:46 -0700482
bsalomon@google.com6f379512011-11-16 20:36:03 +0000483 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400484 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400485 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700486 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400487 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800488 fStats.incTextureUploads();
489 return true;
490 }
491 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000492}
493
Brian Salomone05ba5a2019-04-08 11:59:07 -0400494bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400495 GrColorType textureColorType, GrColorType bufferColorType,
496 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400497 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500498 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700499 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800500
Brian Salomonc67c31c2018-12-06 10:00:03 -0500501 if (texture->readOnly()) {
502 return false;
503 }
504
Greg Daniel660cc992017-06-26 14:55:05 -0400505 // We require that the write region is contained in the texture
506 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
507 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
508 if (!bounds.contains(subRect)) {
509 return false;
510 }
511
Brian Salomonb28cb682019-07-26 12:48:47 -0400512 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400513 if (this->caps()->writePixelsRowBytesSupport()) {
514 if (rowBytes < SkToSizeT(bpp * width)) {
515 return false;
516 }
517 if (rowBytes % bpp) {
518 return false;
519 }
520 } else {
521 if (rowBytes != SkToSizeT(bpp * width)) {
522 return false;
523 }
524 }
525
jvanverth17aa0472016-01-05 10:41:27 -0800526 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400527 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
528 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700529 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500530 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800531 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700532
jvanverth17aa0472016-01-05 10:41:27 -0800533 return true;
534 }
535 return false;
536}
537
Brian Salomon26de56e2019-04-10 12:14:26 -0400538bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400539 GrColorType surfaceColorType, GrColorType bufferColorType,
540 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400541 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400542 SkASSERT(surface);
543 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400544 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400545
Greg Danielba88ab62019-07-26 09:14:01 -0400546#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400547 auto supportedRead = this->caps()->supportedReadPixelsColorType(
548 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400549 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
550 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
551#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400552
553 // We require that the write region is contained in the texture
554 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
555 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
556 if (!bounds.contains(subRect)) {
557 return false;
558 }
559
560 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400561 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
562 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400563 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400564 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400565 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400566 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400567}
568
Brian Salomon930f9392018-06-20 16:25:26 -0400569bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400570 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400571 SkASSERT(texture);
572 SkASSERT(this->caps()->mipMapSupport());
573 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600574 if (!texture->texturePriv().mipMapsAreDirty()) {
575 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
576 // actual target. This may be caused by things that the drawingManager could not predict,
577 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
578 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
579 return true;
580 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500581 if (texture->readOnly()) {
582 return false;
583 }
Brian Salomon930f9392018-06-20 16:25:26 -0400584 if (this->onRegenerateMipMapLevels(texture)) {
585 texture->texturePriv().markMipMapsClean();
586 return true;
587 }
588 return false;
589}
590
Brian Salomon1f05d452019-02-08 12:33:08 -0500591void GrGpu::resetTextureBindings() {
592 this->handleDirtyContext();
593 this->onResetTextureBindings();
594}
595
Chris Dalton16a33c62019-09-24 22:19:17 -0600596void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -0500597 ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000598 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000599 this->handleDirtyContext();
Greg Daniel242536f2020-02-13 14:12:46 -0500600 this->onResolveRenderTarget(target, resolveRect, forExternalIO);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000601}
602
Brian Salomon1fabd512018-02-09 09:54:25 -0500603void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
604 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700605 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500606 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700607 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
608 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700609 GrTexture* texture = surface->asTexture();
610 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400611 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700612 }
613 }
614}
615
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600616int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700617 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600618 SkASSERT(renderTarget->numSamples() > 1 ||
619 (renderTarget->renderTargetPriv().getStencilAttachment() &&
620 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700621
622 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600623 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700624 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
625}
626
Stephen Whitef3d5d442020-04-08 10:35:58 -0400627#ifdef SK_DEBUG
628bool GrGpu::inStagingBuffers(GrStagingBuffer* b) const {
629 for (const auto& i : fStagingBuffers) {
630 if (b == i.get()) {
631 return true;
632 }
633 }
634 return false;
635}
636
637void GrGpu::validateStagingBuffers() const {
638 for (const auto& i : fStagingBuffers) {
639 GrStagingBuffer* buffer = i.get();
640 SkASSERT(fAvailableStagingBuffers.isInList(buffer) ||
641 fActiveStagingBuffers.isInList(buffer) ||
642 fBusyStagingBuffers.isInList(buffer));
643 }
644 for (auto b : fAvailableStagingBuffers) {
645 SkASSERT(this->inStagingBuffers(b));
646 }
647 for (auto b : fActiveStagingBuffers) {
648 SkASSERT(this->inStagingBuffers(b));
649 }
650 for (auto b : fBusyStagingBuffers) {
651 SkASSERT(this->inStagingBuffers(b));
652 }
653}
654#endif
655
Greg Danielfe159622020-04-10 17:43:51 +0000656void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[],
657 int numProxies,
658 SkSurface::BackendSurfaceAccess access,
659 const GrFlushInfo& info,
660 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400661 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000662
Robert Phillips9da87e02019-02-04 13:26:26 -0500663 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500664
Greg Daniel8561fc22020-04-08 12:52:51 -0400665 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
666 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500667 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400668 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400669 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400670 semaphores[i] = resourceProvider->wrapBackendSemaphore(
671 info.fSignalSemaphores[i],
672 GrResourceProvider::SemaphoreWrapType::kWillSignal,
673 kBorrow_GrWrapOwnership);
674 this->insertSemaphore(semaphores[i].get());
Greg Daniel51316782017-08-02 15:10:09 +0000675 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400676 semaphores[i] = resourceProvider->makeSemaphore(false);
677 if (semaphores[i]) {
678 this->insertSemaphore(semaphores[i].get());
679 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
680 }
Greg Daniel51316782017-08-02 15:10:09 +0000681 }
682 }
683 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500684
Greg Danielfe159622020-04-10 17:43:51 +0000685 if (info.fFinishedProc) {
686 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
687 }
688 this->prepareSurfacesForBackendAccessAndExternalIO(proxies, numProxies, access,
689 externalRequests);
690}
691
692bool GrGpu::submitToGpu(bool syncCpu) {
693 this->stats()->incNumSubmitToGpus();
694
695#ifdef SK_DEBUG
696 this->validateStagingBuffers();
697#endif
Stephen Whitef3d5d442020-04-08 10:35:58 -0400698 this->unmapStagingBuffers();
699
Greg Danielfe159622020-04-10 17:43:51 +0000700 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400701
Greg Danielfe159622020-04-10 17:43:51 +0000702 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000703}
Brian Osman71a18892017-08-10 10:23:25 -0400704
Kevin Lubickf4def342018-10-04 12:52:50 -0400705#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400706void GrGpu::dumpJSON(SkJSONWriter* writer) const {
707 writer->beginObject();
708
709 // TODO: Is there anything useful in the base class to dump here?
710
711 this->onDumpJSON(writer);
712
713 writer->endObject();
714}
Kevin Lubickf4def342018-10-04 12:52:50 -0400715#else
716void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
717#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400718
Robert Phillipsf0ced622019-05-16 09:06:25 -0400719#if GR_TEST_UTILS
720
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500721#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500722static const char* cache_result_to_str(int i) {
723 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
724 "hits",
725 "misses",
726 "partials"
727 };
728 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
729 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
730 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
731 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
732 return kCacheResultStrings[i];
733}
734
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500735void GrGpu::Stats::dump(SkString* out) {
736 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
737 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
738 out->appendf("Textures Created: %d\n", fTextureCreates);
739 out->appendf("Texture Uploads: %d\n", fTextureUploads);
740 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400741 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500742 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
743 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400744 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500745
746 SkASSERT(fNumInlineCompilationFailures == 0);
747 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
748 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
749 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
750 fInlineProgramCacheStats[i]);
751 }
752
753 SkASSERT(fNumPreCompilationFailures == 0);
754 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
755 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
756 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
757 fPreProgramCacheStats[i]);
758 }
759
760 SkASSERT(fNumCompilationFailures == 0);
761 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
762 out->appendf("Total number of partial compilation successes %d\n",
763 fNumPartialCompilationSuccesses);
764 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500765
766 // enable this block to output CSV-style stats for program pre-compilation
767#if 0
768 SkASSERT(fNumInlineCompilationFailures == 0);
769 SkASSERT(fNumPreCompilationFailures == 0);
770 SkASSERT(fNumCompilationFailures == 0);
771 SkASSERT(fNumPartialCompilationSuccesses == 0);
772
773 SkDebugf("%d, %d, %d, %d, %d\n",
774 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
775 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
776 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
777 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
778 fNumCompilationSuccesses);
779#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500780}
781
782void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
783 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
784 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500785}
786
Robert Phillips57ef6802019-09-23 10:12:47 -0400787#endif // GR_GPU_STATS
788#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500789
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500790bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
791 GrMipMapped mipMapped,
792 const BackendTextureData* data) {
793 int numMipLevels = 1;
794 if (mipMapped == GrMipMapped::kYes) {
795 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500796 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400797
Robert Phillips42716d42019-12-16 12:19:54 -0500798 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400799 return true;
800 }
801
Robert Phillips42716d42019-12-16 12:19:54 -0500802 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500803 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500804 }
805
806 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
807
Brian Salomon85c3d682019-11-04 15:04:54 -0500808 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400809 return false;
810 }
811
Brian Salomon85c3d682019-11-04 15:04:54 -0500812 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500813 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500814 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500815 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400816 return false;
817 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500818 if (colorType != data->pixmap(i).colorType()) {
819 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400820 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400821 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400822 return true;
823}
824
Robert Phillipsb915c942019-12-17 14:44:37 -0500825bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
826 GrMipMapped mipMapped, const BackendTextureData* data) {
827
828 if (!data || data->type() == BackendTextureData::Type::kColor) {
829 return true;
830 }
831
832 if (data->type() == BackendTextureData::Type::kPixmaps) {
833 return false;
834 }
835
836 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
837
Robert Phillips99dead92020-01-27 16:11:57 -0500838 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
839 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500840
841 return computedSize == data->compressedSize();
842}
843
Brian Salomon85c3d682019-11-04 15:04:54 -0500844GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
845 const GrBackendFormat& format,
846 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500847 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400848 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400849 const GrCaps* caps = this->caps();
850
851 if (!format.isValid()) {
852 return {};
853 }
854
Robert Phillipsd34691b2019-09-24 13:38:43 -0400855 if (caps->isFormatCompressed(format)) {
856 // Compressed formats must go through the createCompressedBackendTexture API
857 return {};
858 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400859
Brian Salomon85c3d682019-11-04 15:04:54 -0500860 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
861 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400862 return {};
863 }
864
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500865 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400866 return {};
867 }
868
Greg Daniel16032b32020-05-06 15:31:10 -0400869 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
870}
871
872bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
873 GrGpuFinishedProc finishedProc,
874 GrGpuFinishedContext finishedContext,
875 const BackendTextureData* data) {
876 SkASSERT(data);
877 const GrCaps* caps = this->caps();
878
879 sk_sp<GrRefCntedCallback> callback;
880 if (finishedProc) {
881 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
Robert Phillips57ef6802019-09-23 10:12:47 -0400882 }
883
Greg Daniel16032b32020-05-06 15:31:10 -0400884 if (!backendTexture.isValid()) {
885 return false;
886 }
887
888 if (data->type() == BackendTextureData::Type::kPixmaps) {
889 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
890 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
891 return false;
892 }
893 }
894
895 if (backendTexture.hasMipMaps() && !this->caps()->mipMapSupport()) {
896 return false;
897 }
898
899 GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
900 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) {
901 return false;
902 }
903
904 return this->onUpdateBackendTexture(backendTexture, std::move(callback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400905}
Robert Phillipsb915c942019-12-17 14:44:37 -0500906
907GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
908 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500909 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500910 GrProtected isProtected,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400911 GrGpuFinishedProc finishedProc,
912 GrGpuFinishedContext finishedContext,
Robert Phillips4277f012020-01-21 14:28:34 -0500913 const BackendTextureData* data) {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400914 sk_sp<GrRefCntedCallback> callback;
915 if (finishedProc) {
916 callback.reset(new GrRefCntedCallback(finishedProc, finishedContext));
917 }
918
Robert Phillipsb915c942019-12-17 14:44:37 -0500919 const GrCaps* caps = this->caps();
920
921 if (!format.isValid()) {
922 return {};
923 }
924
925 SkImage::CompressionType compressionType = caps->compressionType(format);
926 if (compressionType == SkImage::CompressionType::kNone) {
927 // Uncompressed formats must go through the createBackendTexture API
928 return {};
929 }
930
931 if (dimensions.isEmpty() ||
932 dimensions.width() > caps->maxTextureSize() ||
933 dimensions.height() > caps->maxTextureSize()) {
934 return {};
935 }
936
937 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
938 return {};
939 }
940
941 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
942 return {};
943 }
944
Robert Phillips4277f012020-01-21 14:28:34 -0500945 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400946 isProtected, std::move(callback), data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500947}
Stephen Whitef3d5d442020-04-08 10:35:58 -0400948
949GrStagingBuffer* GrGpu::findStagingBuffer(size_t size) {
950#ifdef SK_DEBUG
951 this->validateStagingBuffers();
952#endif
953 for (auto b : fActiveStagingBuffers) {
954 if (b->remaining() >= size) {
955 return b;
956 }
957 }
958 for (auto b : fAvailableStagingBuffers) {
959 if (b->remaining() >= size) {
960 fAvailableStagingBuffers.remove(b);
961 fActiveStagingBuffers.addToTail(b);
962 return b;
963 }
964 }
965 size = SkNextPow2(size);
966 size = std::max(size, kMinStagingBufferSize);
967 std::unique_ptr<GrStagingBuffer> b = this->createStagingBuffer(size);
968 GrStagingBuffer* stagingBuffer = b.get();
969 fStagingBuffers.push_back(std::move(b));
970 fActiveStagingBuffers.addToTail(stagingBuffer);
971 return stagingBuffer;
972}
973
974GrStagingBuffer::Slice GrGpu::allocateStagingBufferSlice(size_t size) {
975#ifdef SK_DEBUG
976 this->validateStagingBuffers();
977#endif
978 GrStagingBuffer* stagingBuffer = this->findStagingBuffer(size);
979 return stagingBuffer->allocate(size);
980}
981
982void GrGpu::unmapStagingBuffers() {
983#ifdef SK_DEBUG
984 this->validateStagingBuffers();
985#endif
986 // Unmap all active buffers.
987 for (auto buffer : fActiveStagingBuffers) {
988 buffer->unmap();
989 }
990}
991
Stephen White7a026142020-05-13 17:16:33 -0400992void GrGpu::moveStagingBufferFromBusyToAvailable(GrStagingBuffer* buffer) {
Stephen Whitef3d5d442020-04-08 10:35:58 -0400993#ifdef SK_DEBUG
994 this->validateStagingBuffers();
995#endif
996 fBusyStagingBuffers.remove(buffer);
997 fAvailableStagingBuffers.addToTail(buffer);
998}
Stephen White7a026142020-05-13 17:16:33 -0400999
1000void GrGpu::moveStagingBufferFromActiveToBusy(GrStagingBuffer* buffer) {
1001#ifdef SK_DEBUG
1002 this->validateStagingBuffers();
1003#endif
1004 fActiveStagingBuffers.remove(buffer);
1005 fBusyStagingBuffers.addToTail(buffer);
1006}