blob: 8c7578816f42674c63826332a094f5bd95ca5329 [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"
Greg Daniel01f278c2020-06-12 16:58:17 -040018#include "src/gpu/GrBackendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrCaps.h"
20#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040021#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060023#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrPathRendering.h"
25#include "src/gpu/GrPipeline.h"
26#include "src/gpu/GrRenderTargetPriv.h"
27#include "src/gpu/GrResourceCache.h"
28#include "src/gpu/GrResourceProvider.h"
29#include "src/gpu/GrSemaphore.h"
Stephen Whitef3d5d442020-04-08 10:35:58 -040030#include "src/gpu/GrStagingBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrStencilAttachment.h"
32#include "src/gpu/GrStencilSettings.h"
33#include "src/gpu/GrSurfacePriv.h"
34#include "src/gpu/GrTexturePriv.h"
35#include "src/gpu/GrTextureProxyPriv.h"
36#include "src/gpu/GrTracing.h"
37#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070038
Stephen Whitef3d5d442020-04-08 10:35:58 -040039static const size_t kMinStagingBufferSize = 32 * 1024;
40
bsalomon@google.comd302f142011-03-03 13:54:13 +000041////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000042
Brian Salomone2826ab2019-06-04 15:58:31 -040043GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000044
Stephen Whitef3d5d442020-04-08 10:35:58 -040045GrGpu::~GrGpu() {
Greg Daniel55822f12020-05-26 11:26:45 -040046 this->callSubmittedProcs(false);
Stephen Whitef3d5d442020-04-08 10:35:58 -040047 SkASSERT(fBusyStagingBuffers.isEmpty());
48}
bsalomon1d89ddc2014-08-19 14:20:58 -070049
Stephen White7a026142020-05-13 17:16:33 -040050void GrGpu::disconnect(DisconnectType type) {
51 if (DisconnectType::kAbandon == type) {
52 fAvailableStagingBuffers.reset();
53 fActiveStagingBuffers.reset();
54 fBusyStagingBuffers.reset();
55 }
56 fStagingBuffers.clear();
57}
reed@google.comac10a2d2010-12-22 21:39:39 +000058
bsalomon@google.comd302f142011-03-03 13:54:13 +000059////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000060
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000061bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
Brian Salomonc8d092a2020-02-24 10:14:21 -050062 GrSamplerState::Filter filter) {
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000063 SkASSERT(texProxy);
Brian Salomonc8d092a2020-02-24 10:14:21 -050064 if (filter != GrSamplerState::Filter::kMipMap || texProxy->mipMapped() == GrMipMapped::kYes ||
65 !caps->mipMapSupport()) {
66 return false;
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000067 }
Brian Salomonc8d092a2020-02-24 10:14:21 -050068 return SkMipMap::ComputeLevelCount(texProxy->width(), texProxy->height()) > 0;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040069}
70
Brian Salomona56a7462020-02-07 14:17:25 -050071static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040072 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040073 SkASSERT(mipLevelCount > 0);
74 bool hasBasePixels = texels[0].fPixels;
75 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040076 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050077 int w = dimensions.fWidth;
78 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040079 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
80 if (texels[currentMipLevel].fPixels) {
81 const size_t minRowBytes = w * bpp;
82 if (caps->writePixelsRowBytesSupport()) {
83 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
84 return false;
85 }
86 if (texels[currentMipLevel].fRowBytes % bpp) {
87 return false;
88 }
89 } else {
90 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
91 return false;
92 }
93 }
94 ++levelsWithPixelsCnt;
95 }
96 if (w == 1 && h == 1) {
97 if (currentMipLevel != mipLevelCount - 1) {
98 return false;
99 }
100 } else {
101 w = std::max(w / 2, 1);
102 h = std::max(h / 2, 1);
103 }
104 }
105 // Either just a base layer or a full stack is required.
106 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
107 return false;
108 }
109 // Can specify just the base, all levels, or no levels.
110 if (!hasBasePixels) {
111 return levelsWithPixelsCnt == 0;
112 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400113 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400114}
115
Brian Salomona56a7462020-02-07 14:17:25 -0500116sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400117 const GrBackendFormat& format,
118 GrRenderable renderable,
119 int renderTargetSampleCnt,
120 SkBudgeted budgeted,
121 GrProtected isProtected,
122 int mipLevelCount,
123 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400124 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400125 // Call GrGpu::createCompressedTexture.
126 return nullptr;
127 }
cblume55f2d2d2016-02-26 13:20:48 -0800128
Brian Salomona90382f2019-09-17 09:01:56 -0400129 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500130 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
131 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700132 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700133 }
134
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400135 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400136 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400137 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500138 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400139 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400140 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400141 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500142 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400143 format,
144 renderable,
145 renderTargetSampleCnt,
146 budgeted,
147 isProtected,
148 mipLevelCount,
149 levelClearMask);
150 if (tex) {
151 SkASSERT(tex->backendFormat() == format);
152 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
153 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
154 tex->resourcePriv().removeScratchKey();
155 }
156 fStats.incTextureCreates();
157 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
158 SkASSERT(GrRenderable::kYes == renderable);
159 tex->asRenderTarget()->setRequiresManualMSAAResolve();
160 }
161 }
162 return tex;
163}
164
Brian Salomona56a7462020-02-07 14:17:25 -0500165sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400166 const GrBackendFormat& format,
167 GrRenderable renderable,
168 int renderTargetSampleCnt,
169 GrMipMapped mipMapped,
170 SkBudgeted budgeted,
171 GrProtected isProtected) {
172 int mipLevelCount = 1;
173 if (mipMapped == GrMipMapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500174 mipLevelCount =
175 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400176 }
177 uint32_t levelClearMask =
178 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500179 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
180 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomona90382f2019-09-17 09:01:56 -0400181 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
182 tex->texturePriv().markMipMapsClean();
183 }
184 return tex;
185}
186
Brian Salomona56a7462020-02-07 14:17:25 -0500187sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400188 const GrBackendFormat& format,
189 GrRenderable renderable,
190 int renderTargetSampleCnt,
191 SkBudgeted budgeted,
192 GrProtected isProtected,
193 GrColorType textureColorType,
194 GrColorType srcColorType,
195 const GrMipLevel texels[],
196 int texelLevelCount) {
197 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400198 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500199 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400200 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400201 return nullptr;
202 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400203 }
204
Brian Osman788b9162020-02-07 10:36:46 -0500205 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400206 uint32_t levelClearMask = 0;
207 if (this->caps()->shouldInitializeTextures()) {
208 if (texelLevelCount) {
209 for (int i = 0; i < mipLevelCount; ++i) {
210 if (!texels->fPixels) {
211 levelClearMask |= static_cast<uint32_t>(1 << i);
212 }
213 }
214 } else {
215 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
216 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400217 }
218
Brian Salomona56a7462020-02-07 14:17:25 -0500219 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
220 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800221 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400222 bool markMipLevelsClean = false;
223 // Currently if level 0 does not have pixels then no other level may, as enforced by
224 // validate_texel_levels.
225 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500226 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
227 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400228 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800229 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400230 // Currently if level[1] of mip map has pixel data then so must all other levels.
231 // as enforced by validate_texel_levels.
232 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
233 fStats.incTextureUploads();
234 } else if (levelClearMask && mipLevelCount > 1) {
235 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800236 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400237 if (markMipLevelsClean) {
238 tex->texturePriv().markMipMapsClean();
239 }
bsalomonb12ea412015-02-02 21:19:50 -0800240 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000241 return tex;
242}
243
Robert Phillips9f744f72019-12-19 19:14:33 -0500244sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400245 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500246 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500247 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500248 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500249 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400250 size_t dataSize) {
251 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500252 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
253 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400254 return nullptr;
255 }
Brian Salomona3e29962019-07-16 11:52:08 -0400256 // Note if we relax the requirement that data must be provided then we must check
257 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400258 if (!data) {
259 return nullptr;
260 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400261 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400262 return nullptr;
263 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500264
265 // TODO: expand CompressedDataIsCorrect to work here too
Greg Daniel01f278c2020-06-12 16:58:17 -0400266 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillips9f744f72019-12-19 19:14:33 -0500267
Robert Phillips99dead92020-01-27 16:11:57 -0500268 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
269 mipMapped == GrMipMapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400270 return nullptr;
271 }
Robert Phillips3a833922020-01-21 15:25:58 -0500272 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
273 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400274}
275
Greg Daniel7ef28f32017-04-20 16:41:55 +0000276sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400277 GrWrapOwnership ownership,
278 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500279 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500280 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000281 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400282
283 const GrCaps* caps = this->caps();
284 SkASSERT(caps);
285
Greg Daniel7bfc9132019-08-14 14:23:53 -0400286 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800287 return nullptr;
288 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400289 if (backendTex.width() > caps->maxTextureSize() ||
290 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800291 return nullptr;
292 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400293
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400294 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400295}
Eric Karl5c779752017-05-08 12:02:07 -0700296
Robert Phillipsb915c942019-12-17 14:44:37 -0500297sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
298 GrWrapOwnership ownership,
299 GrWrapCacheable cacheable) {
300 this->handleDirtyContext();
301
302 const GrCaps* caps = this->caps();
303 SkASSERT(caps);
304
305 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
306 return nullptr;
307 }
308 if (backendTex.width() > caps->maxTextureSize() ||
309 backendTex.height() > caps->maxTextureSize()) {
310 return nullptr;
311 }
312
313 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
314}
315
Brian Salomond17f6582017-07-19 18:28:58 -0400316sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400317 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400318 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500319 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400320 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500321 if (sampleCnt < 1) {
322 return nullptr;
323 }
Robert Phillips0902c982019-07-16 07:47:56 -0400324
325 const GrCaps* caps = this->caps();
326
Greg Daniel7bfc9132019-08-14 14:23:53 -0400327 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400328 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400329 return nullptr;
330 }
331
Robert Phillips0902c982019-07-16 07:47:56 -0400332 if (backendTex.width() > caps->maxRenderTargetSize() ||
333 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400334 return nullptr;
335 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400336 sk_sp<GrTexture> tex =
337 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400338 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600339 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
340 tex->asRenderTarget()->setRequiresManualMSAAResolve();
341 }
bungeman6bd52842016-10-27 09:30:08 -0700342 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000343}
344
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400345sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400346 this->handleDirtyContext();
347
348 const GrCaps* caps = this->caps();
349
Greg Daniel6fa62e22019-08-07 15:52:37 -0400350 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800351 return nullptr;
352 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400353
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400354 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500355 if (backendRT.isFramebufferOnly()) {
356 rt->setFramebufferOnly();
357 }
358 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000359}
360
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400361sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400362 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500363 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400364
365 const GrCaps* caps = this->caps();
366
367 int maxSize = caps->maxTextureSize();
368 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
369 return nullptr;
370 }
371
Greg Daniel6fa62e22019-08-07 15:52:37 -0400372 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400373 return nullptr;
374 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400375
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400376 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Chris Dalton3f7932e2019-08-19 00:39:13 -0600377 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
378 rt->setRequiresManualMSAAResolve();
379 }
380 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800381}
382
Greg Danielb46add82019-01-02 14:51:29 -0500383sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
384 const GrVkDrawableInfo& vkInfo) {
385 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
386}
387
388sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
389 const GrVkDrawableInfo& vkInfo) {
390 // This is only supported on Vulkan so we default to returning nullptr here
391 return nullptr;
392}
393
Brian Salomondbf70722019-02-07 11:31:24 -0500394sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
395 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400396 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000397 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500398 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700399 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700400 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700401 }
cdalton397536c2016-03-25 12:15:03 -0700402 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800403}
404
Greg Daniel46cfbc62019-06-07 11:43:30 -0400405bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400406 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400407 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700408 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500409 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500410
411 if (dst->readOnly()) {
412 return false;
413 }
414
joshualitt1cbdcde2015-08-21 11:53:29 -0700415 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500416
Greg Daniele227fe42019-08-21 13:52:24 -0400417 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700418}
419
Brian Salomona6948702018-06-01 15:33:20 -0400420bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400421 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
422 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400423 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500424 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500425 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400426 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500427
Brian Salomon1d435302019-07-01 13:05:28 -0400428 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
429 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
430 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700431 return false;
432 }
433
Brian Salomon1047a492019-07-02 12:25:21 -0400434 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
435 if (!this->caps()->readPixelsRowBytesSupport()) {
436 if (rowBytes != minRowBytes) {
437 return false;
438 }
439 } else {
440 if (rowBytes < minRowBytes) {
441 return false;
442 }
443 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
444 return false;
445 }
446 }
447
Brian Salomonbf7b6202016-11-11 16:08:03 -0500448 this->handleDirtyContext();
449
Brian Salomonf77c1462019-08-01 15:19:29 -0400450 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
451 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000452}
453
Brian Salomona9b04b92018-06-01 15:04:28 -0400454bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400455 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400456 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400457 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev1db4d142020-04-10 00:58:52 -0400458 ATRACE_ANDROID_FRAMEWORK_ALWAYS("texture_upload");
Brian Salomonbf7b6202016-11-11 16:08:03 -0500459 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500460 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500461
462 if (surface->readOnly()) {
463 return false;
464 }
465
Brian Salomon1047a492019-07-02 12:25:21 -0400466 if (mipLevelCount == 0) {
467 return false;
468 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400469 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400470 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
471 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400472 if (!bounds.contains(subRect)) {
473 return false;
474 }
475 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
476 // We require that if the texels are mipped, than the write region is the entire surface
477 return false;
478 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400479
Brian Salomona56a7462020-02-07 14:17:25 -0500480 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
481 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400482 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800483 }
jvanverth2dc29942015-09-01 07:16:46 -0700484
bsalomon@google.com6f379512011-11-16 20:36:03 +0000485 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400486 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400487 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700488 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400489 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800490 fStats.incTextureUploads();
491 return true;
492 }
493 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000494}
495
Brian Salomone05ba5a2019-04-08 11:59:07 -0400496bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400497 GrColorType textureColorType, GrColorType bufferColorType,
498 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400499 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500500 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700501 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800502
Brian Salomonc67c31c2018-12-06 10:00:03 -0500503 if (texture->readOnly()) {
504 return false;
505 }
506
Greg Daniel660cc992017-06-26 14:55:05 -0400507 // We require that the write region is contained in the texture
508 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
509 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
510 if (!bounds.contains(subRect)) {
511 return false;
512 }
513
Brian Salomonb28cb682019-07-26 12:48:47 -0400514 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400515 if (this->caps()->writePixelsRowBytesSupport()) {
516 if (rowBytes < SkToSizeT(bpp * width)) {
517 return false;
518 }
519 if (rowBytes % bpp) {
520 return false;
521 }
522 } else {
523 if (rowBytes != SkToSizeT(bpp * width)) {
524 return false;
525 }
526 }
527
jvanverth17aa0472016-01-05 10:41:27 -0800528 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400529 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
530 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700531 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500532 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800533 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700534
jvanverth17aa0472016-01-05 10:41:27 -0800535 return true;
536 }
537 return false;
538}
539
Brian Salomon26de56e2019-04-10 12:14:26 -0400540bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400541 GrColorType surfaceColorType, GrColorType bufferColorType,
542 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400543 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400544 SkASSERT(surface);
545 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400546 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400547
Greg Danielba88ab62019-07-26 09:14:01 -0400548#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400549 auto supportedRead = this->caps()->supportedReadPixelsColorType(
550 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400551 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
552 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
553#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400554
555 // We require that the write region is contained in the texture
556 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
557 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
558 if (!bounds.contains(subRect)) {
559 return false;
560 }
561
562 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400563 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
564 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400565 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400566 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400567 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400568 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400569}
570
Brian Salomon930f9392018-06-20 16:25:26 -0400571bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400572 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400573 SkASSERT(texture);
574 SkASSERT(this->caps()->mipMapSupport());
575 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600576 if (!texture->texturePriv().mipMapsAreDirty()) {
577 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
578 // actual target. This may be caused by things that the drawingManager could not predict,
579 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
580 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
581 return true;
582 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500583 if (texture->readOnly()) {
584 return false;
585 }
Brian Salomon930f9392018-06-20 16:25:26 -0400586 if (this->onRegenerateMipMapLevels(texture)) {
587 texture->texturePriv().markMipMapsClean();
588 return true;
589 }
590 return false;
591}
592
Brian Salomon1f05d452019-02-08 12:33:08 -0500593void GrGpu::resetTextureBindings() {
594 this->handleDirtyContext();
595 this->onResetTextureBindings();
596}
597
Chris Dalton16a33c62019-09-24 22:19:17 -0600598void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -0500599 ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000600 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000601 this->handleDirtyContext();
Greg Daniel242536f2020-02-13 14:12:46 -0500602 this->onResolveRenderTarget(target, resolveRect, forExternalIO);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000603}
604
Brian Salomon1fabd512018-02-09 09:54:25 -0500605void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
606 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700607 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500608 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700609 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
610 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700611 GrTexture* texture = surface->asTexture();
612 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400613 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700614 }
615 }
616}
617
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600618int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700619 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600620 SkASSERT(renderTarget->numSamples() > 1 ||
621 (renderTarget->renderTargetPriv().getStencilAttachment() &&
622 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700623
624 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600625 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700626 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
627}
628
Stephen Whitef3d5d442020-04-08 10:35:58 -0400629#ifdef SK_DEBUG
630bool GrGpu::inStagingBuffers(GrStagingBuffer* b) const {
631 for (const auto& i : fStagingBuffers) {
632 if (b == i.get()) {
633 return true;
634 }
635 }
636 return false;
637}
638
639void GrGpu::validateStagingBuffers() const {
640 for (const auto& i : fStagingBuffers) {
641 GrStagingBuffer* buffer = i.get();
642 SkASSERT(fAvailableStagingBuffers.isInList(buffer) ||
643 fActiveStagingBuffers.isInList(buffer) ||
644 fBusyStagingBuffers.isInList(buffer));
645 }
646 for (auto b : fAvailableStagingBuffers) {
647 SkASSERT(this->inStagingBuffers(b));
648 }
649 for (auto b : fActiveStagingBuffers) {
650 SkASSERT(this->inStagingBuffers(b));
651 }
652 for (auto b : fBusyStagingBuffers) {
653 SkASSERT(this->inStagingBuffers(b));
654 }
655}
656#endif
657
Greg Danielfe159622020-04-10 17:43:51 +0000658void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[],
659 int numProxies,
660 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400661 const GrFlushInfo& info,
662 const GrBackendSurfaceMutableState* newState) {
Brian Salomone39526b2019-06-24 16:35:53 -0400663 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000664
Robert Phillips9da87e02019-02-04 13:26:26 -0500665 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500666
Greg Daniel8561fc22020-04-08 12:52:51 -0400667 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
668 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500669 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400670 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400671 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400672 semaphores[i] = resourceProvider->wrapBackendSemaphore(
673 info.fSignalSemaphores[i],
674 GrResourceProvider::SemaphoreWrapType::kWillSignal,
675 kBorrow_GrWrapOwnership);
676 this->insertSemaphore(semaphores[i].get());
Greg Daniel51316782017-08-02 15:10:09 +0000677 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400678 semaphores[i] = resourceProvider->makeSemaphore(false);
679 if (semaphores[i]) {
680 this->insertSemaphore(semaphores[i].get());
681 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
682 }
Greg Daniel51316782017-08-02 15:10:09 +0000683 }
684 }
685 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500686
Greg Danielfe159622020-04-10 17:43:51 +0000687 if (info.fFinishedProc) {
688 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
689 }
Greg Daniel55822f12020-05-26 11:26:45 -0400690
691 if (info.fSubmittedProc) {
692 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
693 }
694
Greg Daniel9efe3862020-06-11 11:51:06 -0400695 // We currently don't support passing in new surface state for multiple proxies here. The only
696 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
697 // updates anyways.
698 SkASSERT(!newState || numProxies == 1);
699 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
700 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, numProxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000701}
702
703bool GrGpu::submitToGpu(bool syncCpu) {
704 this->stats()->incNumSubmitToGpus();
705
706#ifdef SK_DEBUG
707 this->validateStagingBuffers();
708#endif
Stephen Whitef3d5d442020-04-08 10:35:58 -0400709 this->unmapStagingBuffers();
710
Greg Danielfe159622020-04-10 17:43:51 +0000711 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400712
Greg Daniel55822f12020-05-26 11:26:45 -0400713 this->callSubmittedProcs(submitted);
714
Greg Danielfe159622020-04-10 17:43:51 +0000715 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000716}
Brian Osman71a18892017-08-10 10:23:25 -0400717
Brian Salomon24069eb2020-06-24 10:19:52 -0400718bool GrGpu::checkAndResetOOMed() {
719 if (fOOMed) {
720 fOOMed = false;
721 return true;
722 }
723 return false;
724}
725
Greg Daniel55822f12020-05-26 11:26:45 -0400726void GrGpu::callSubmittedProcs(bool success) {
727 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
728 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
729 }
730 fSubmittedProcs.reset();
731}
732
Kevin Lubickf4def342018-10-04 12:52:50 -0400733#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400734void GrGpu::dumpJSON(SkJSONWriter* writer) const {
735 writer->beginObject();
736
737 // TODO: Is there anything useful in the base class to dump here?
738
739 this->onDumpJSON(writer);
740
741 writer->endObject();
742}
Kevin Lubickf4def342018-10-04 12:52:50 -0400743#else
744void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
745#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400746
Robert Phillipsf0ced622019-05-16 09:06:25 -0400747#if GR_TEST_UTILS
748
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500749#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500750static const char* cache_result_to_str(int i) {
751 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
752 "hits",
753 "misses",
754 "partials"
755 };
756 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
757 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
758 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
759 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
760 return kCacheResultStrings[i];
761}
762
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500763void GrGpu::Stats::dump(SkString* out) {
764 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
765 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
766 out->appendf("Textures Created: %d\n", fTextureCreates);
767 out->appendf("Texture Uploads: %d\n", fTextureUploads);
768 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400769 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500770 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
771 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400772 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500773
774 SkASSERT(fNumInlineCompilationFailures == 0);
775 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
776 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
777 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
778 fInlineProgramCacheStats[i]);
779 }
780
781 SkASSERT(fNumPreCompilationFailures == 0);
782 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
783 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
784 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
785 fPreProgramCacheStats[i]);
786 }
787
788 SkASSERT(fNumCompilationFailures == 0);
789 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
790 out->appendf("Total number of partial compilation successes %d\n",
791 fNumPartialCompilationSuccesses);
792 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500793
794 // enable this block to output CSV-style stats for program pre-compilation
795#if 0
796 SkASSERT(fNumInlineCompilationFailures == 0);
797 SkASSERT(fNumPreCompilationFailures == 0);
798 SkASSERT(fNumCompilationFailures == 0);
799 SkASSERT(fNumPartialCompilationSuccesses == 0);
800
801 SkDebugf("%d, %d, %d, %d, %d\n",
802 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
803 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
804 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
805 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
806 fNumCompilationSuccesses);
807#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500808}
809
810void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
811 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
812 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500813}
814
Robert Phillips57ef6802019-09-23 10:12:47 -0400815#endif // GR_GPU_STATS
816#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500817
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500818bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
819 GrMipMapped mipMapped,
820 const BackendTextureData* data) {
821 int numMipLevels = 1;
822 if (mipMapped == GrMipMapped::kYes) {
823 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500824 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400825
Robert Phillips42716d42019-12-16 12:19:54 -0500826 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400827 return true;
828 }
829
Robert Phillips42716d42019-12-16 12:19:54 -0500830 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500831 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500832 }
833
834 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
835
Brian Salomon85c3d682019-11-04 15:04:54 -0500836 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400837 return false;
838 }
839
Brian Salomon85c3d682019-11-04 15:04:54 -0500840 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500841 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500842 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500843 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400844 return false;
845 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500846 if (colorType != data->pixmap(i).colorType()) {
847 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400848 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400849 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400850 return true;
851}
852
Robert Phillipsb915c942019-12-17 14:44:37 -0500853bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
854 GrMipMapped mipMapped, const BackendTextureData* data) {
855
856 if (!data || data->type() == BackendTextureData::Type::kColor) {
857 return true;
858 }
859
860 if (data->type() == BackendTextureData::Type::kPixmaps) {
861 return false;
862 }
863
864 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
865
Robert Phillips99dead92020-01-27 16:11:57 -0500866 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
867 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500868
869 return computedSize == data->compressedSize();
870}
871
Brian Salomon85c3d682019-11-04 15:04:54 -0500872GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
873 const GrBackendFormat& format,
874 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500875 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400876 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400877 const GrCaps* caps = this->caps();
878
879 if (!format.isValid()) {
880 return {};
881 }
882
Robert Phillipsd34691b2019-09-24 13:38:43 -0400883 if (caps->isFormatCompressed(format)) {
884 // Compressed formats must go through the createCompressedBackendTexture API
885 return {};
886 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400887
Brian Salomon85c3d682019-11-04 15:04:54 -0500888 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
889 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400890 return {};
891 }
892
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500893 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400894 return {};
895 }
896
Greg Daniel16032b32020-05-06 15:31:10 -0400897 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
898}
899
900bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400901 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400902 const BackendTextureData* data) {
903 SkASSERT(data);
904 const GrCaps* caps = this->caps();
905
Greg Daniel16032b32020-05-06 15:31:10 -0400906 if (!backendTexture.isValid()) {
907 return false;
908 }
909
910 if (data->type() == BackendTextureData::Type::kPixmaps) {
911 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
912 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
913 return false;
914 }
915 }
916
917 if (backendTexture.hasMipMaps() && !this->caps()->mipMapSupport()) {
918 return false;
919 }
920
921 GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
922 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) {
923 return false;
924 }
925
Greg Daniel25597782020-06-11 13:15:08 -0400926 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400927}
Robert Phillipsb915c942019-12-17 14:44:37 -0500928
929GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
930 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500931 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500932 GrProtected isProtected,
Greg Daniel25597782020-06-11 13:15:08 -0400933 sk_sp<GrRefCntedCallback> finishedCallback,
Robert Phillips4277f012020-01-21 14:28:34 -0500934 const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500935 const GrCaps* caps = this->caps();
936
937 if (!format.isValid()) {
938 return {};
939 }
940
Greg Daniel01f278c2020-06-12 16:58:17 -0400941 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillipsb915c942019-12-17 14:44:37 -0500942 if (compressionType == SkImage::CompressionType::kNone) {
943 // Uncompressed formats must go through the createBackendTexture API
944 return {};
945 }
946
947 if (dimensions.isEmpty() ||
948 dimensions.width() > caps->maxTextureSize() ||
949 dimensions.height() > caps->maxTextureSize()) {
950 return {};
951 }
952
953 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
954 return {};
955 }
956
957 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
958 return {};
959 }
960
Robert Phillips4277f012020-01-21 14:28:34 -0500961 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400962 isProtected, std::move(finishedCallback), data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500963}
Stephen Whitef3d5d442020-04-08 10:35:58 -0400964
965GrStagingBuffer* GrGpu::findStagingBuffer(size_t size) {
966#ifdef SK_DEBUG
967 this->validateStagingBuffers();
968#endif
969 for (auto b : fActiveStagingBuffers) {
970 if (b->remaining() >= size) {
971 return b;
972 }
973 }
974 for (auto b : fAvailableStagingBuffers) {
975 if (b->remaining() >= size) {
976 fAvailableStagingBuffers.remove(b);
977 fActiveStagingBuffers.addToTail(b);
978 return b;
979 }
980 }
981 size = SkNextPow2(size);
982 size = std::max(size, kMinStagingBufferSize);
983 std::unique_ptr<GrStagingBuffer> b = this->createStagingBuffer(size);
984 GrStagingBuffer* stagingBuffer = b.get();
985 fStagingBuffers.push_back(std::move(b));
986 fActiveStagingBuffers.addToTail(stagingBuffer);
987 return stagingBuffer;
988}
989
990GrStagingBuffer::Slice GrGpu::allocateStagingBufferSlice(size_t size) {
991#ifdef SK_DEBUG
992 this->validateStagingBuffers();
993#endif
994 GrStagingBuffer* stagingBuffer = this->findStagingBuffer(size);
995 return stagingBuffer->allocate(size);
996}
997
998void GrGpu::unmapStagingBuffers() {
999#ifdef SK_DEBUG
1000 this->validateStagingBuffers();
1001#endif
1002 // Unmap all active buffers.
1003 for (auto buffer : fActiveStagingBuffers) {
1004 buffer->unmap();
1005 }
1006}
1007
Stephen White7a026142020-05-13 17:16:33 -04001008void GrGpu::moveStagingBufferFromBusyToAvailable(GrStagingBuffer* buffer) {
Stephen Whitef3d5d442020-04-08 10:35:58 -04001009#ifdef SK_DEBUG
1010 this->validateStagingBuffers();
1011#endif
1012 fBusyStagingBuffers.remove(buffer);
1013 fAvailableStagingBuffers.addToTail(buffer);
1014}
Stephen White7a026142020-05-13 17:16:33 -04001015
1016void GrGpu::moveStagingBufferFromActiveToBusy(GrStagingBuffer* buffer) {
1017#ifdef SK_DEBUG
1018 this->validateStagingBuffers();
1019#endif
1020 fActiveStagingBuffers.remove(buffer);
1021 fBusyStagingBuffers.addToTail(buffer);
1022}