blob: a38a51a3462b873df1c0880d4c8adddc06553f3f [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
Adlai Holler3d0359a2020-07-09 15:35:55 -040013#include "include/gpu/GrDirectContext.h"
Robert Phillips99dead92020-01-27 16:11:57 -050014#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkMathPriv.h"
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
Adlai Holler3d0359a2020-07-09 15:35:55 -040043GrGpu::GrGpu(GrDirectContext* direct) : fResetBits(kAll_GrBackendState), fContext(direct) {}
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
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400598void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000599 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000600 this->handleDirtyContext();
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400601 this->onResolveRenderTarget(target, resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000602}
603
Brian Salomon1fabd512018-02-09 09:54:25 -0500604void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
605 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700606 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500607 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700608 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
609 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700610 GrTexture* texture = surface->asTexture();
611 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400612 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700613 }
614 }
615}
616
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600617int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700618 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600619 SkASSERT(renderTarget->numSamples() > 1 ||
620 (renderTarget->renderTargetPriv().getStencilAttachment() &&
621 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700622
623 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600624 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700625 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
626}
627
Stephen Whitef3d5d442020-04-08 10:35:58 -0400628#ifdef SK_DEBUG
629bool GrGpu::inStagingBuffers(GrStagingBuffer* b) const {
630 for (const auto& i : fStagingBuffers) {
631 if (b == i.get()) {
632 return true;
633 }
634 }
635 return false;
636}
637
638void GrGpu::validateStagingBuffers() const {
639 for (const auto& i : fStagingBuffers) {
640 GrStagingBuffer* buffer = i.get();
641 SkASSERT(fAvailableStagingBuffers.isInList(buffer) ||
642 fActiveStagingBuffers.isInList(buffer) ||
643 fBusyStagingBuffers.isInList(buffer));
644 }
645 for (auto b : fAvailableStagingBuffers) {
646 SkASSERT(this->inStagingBuffers(b));
647 }
648 for (auto b : fActiveStagingBuffers) {
649 SkASSERT(this->inStagingBuffers(b));
650 }
651 for (auto b : fBusyStagingBuffers) {
652 SkASSERT(this->inStagingBuffers(b));
653 }
654}
655#endif
656
Greg Danielfe159622020-04-10 17:43:51 +0000657void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[],
658 int numProxies,
659 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400660 const GrFlushInfo& info,
661 const GrBackendSurfaceMutableState* newState) {
Brian Salomone39526b2019-06-24 16:35:53 -0400662 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000663
Robert Phillips9da87e02019-02-04 13:26:26 -0500664 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500665
Greg Daniel8561fc22020-04-08 12:52:51 -0400666 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
667 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500668 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400669 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400670 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400671 semaphores[i] = resourceProvider->wrapBackendSemaphore(
672 info.fSignalSemaphores[i],
673 GrResourceProvider::SemaphoreWrapType::kWillSignal,
674 kBorrow_GrWrapOwnership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400675 // If we failed to wrap the semaphore it means the client didn't give us a valid
676 // semaphore to begin with. Therefore, it is fine to not signal it.
677 if (semaphores[i]) {
678 this->insertSemaphore(semaphores[i].get());
679 }
Greg Daniel51316782017-08-02 15:10:09 +0000680 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400681 semaphores[i] = resourceProvider->makeSemaphore(false);
682 if (semaphores[i]) {
683 this->insertSemaphore(semaphores[i].get());
684 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
685 }
Greg Daniel51316782017-08-02 15:10:09 +0000686 }
687 }
688 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500689
Greg Danielfe159622020-04-10 17:43:51 +0000690 if (info.fFinishedProc) {
691 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
692 }
Greg Daniel55822f12020-05-26 11:26:45 -0400693
694 if (info.fSubmittedProc) {
695 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
696 }
697
Greg Daniel9efe3862020-06-11 11:51:06 -0400698 // We currently don't support passing in new surface state for multiple proxies here. The only
699 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
700 // updates anyways.
701 SkASSERT(!newState || numProxies == 1);
702 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
703 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, numProxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000704}
705
706bool GrGpu::submitToGpu(bool syncCpu) {
707 this->stats()->incNumSubmitToGpus();
708
709#ifdef SK_DEBUG
710 this->validateStagingBuffers();
711#endif
Stephen Whitef3d5d442020-04-08 10:35:58 -0400712 this->unmapStagingBuffers();
713
Greg Danielfe159622020-04-10 17:43:51 +0000714 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400715
Greg Daniel55822f12020-05-26 11:26:45 -0400716 this->callSubmittedProcs(submitted);
717
Greg Danielfe159622020-04-10 17:43:51 +0000718 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000719}
Brian Osman71a18892017-08-10 10:23:25 -0400720
Brian Salomon24069eb2020-06-24 10:19:52 -0400721bool GrGpu::checkAndResetOOMed() {
722 if (fOOMed) {
723 fOOMed = false;
724 return true;
725 }
726 return false;
727}
728
Greg Daniel55822f12020-05-26 11:26:45 -0400729void GrGpu::callSubmittedProcs(bool success) {
730 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
731 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
732 }
733 fSubmittedProcs.reset();
734}
735
Kevin Lubickf4def342018-10-04 12:52:50 -0400736#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400737void GrGpu::dumpJSON(SkJSONWriter* writer) const {
738 writer->beginObject();
739
740 // TODO: Is there anything useful in the base class to dump here?
741
742 this->onDumpJSON(writer);
743
744 writer->endObject();
745}
Kevin Lubickf4def342018-10-04 12:52:50 -0400746#else
747void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
748#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400749
Robert Phillipsf0ced622019-05-16 09:06:25 -0400750#if GR_TEST_UTILS
751
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500752#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500753static const char* cache_result_to_str(int i) {
754 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
755 "hits",
756 "misses",
757 "partials"
758 };
759 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
760 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
761 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
762 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
763 return kCacheResultStrings[i];
764}
765
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500766void GrGpu::Stats::dump(SkString* out) {
767 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
768 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
769 out->appendf("Textures Created: %d\n", fTextureCreates);
770 out->appendf("Texture Uploads: %d\n", fTextureUploads);
771 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400772 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500773 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
774 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400775 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500776
777 SkASSERT(fNumInlineCompilationFailures == 0);
778 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
779 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
780 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
781 fInlineProgramCacheStats[i]);
782 }
783
784 SkASSERT(fNumPreCompilationFailures == 0);
785 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
786 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
787 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
788 fPreProgramCacheStats[i]);
789 }
790
791 SkASSERT(fNumCompilationFailures == 0);
792 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
793 out->appendf("Total number of partial compilation successes %d\n",
794 fNumPartialCompilationSuccesses);
795 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500796
797 // enable this block to output CSV-style stats for program pre-compilation
798#if 0
799 SkASSERT(fNumInlineCompilationFailures == 0);
800 SkASSERT(fNumPreCompilationFailures == 0);
801 SkASSERT(fNumCompilationFailures == 0);
802 SkASSERT(fNumPartialCompilationSuccesses == 0);
803
804 SkDebugf("%d, %d, %d, %d, %d\n",
805 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
806 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
807 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
808 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
809 fNumCompilationSuccesses);
810#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500811}
812
813void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
814 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
815 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500816}
817
Robert Phillips57ef6802019-09-23 10:12:47 -0400818#endif // GR_GPU_STATS
819#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500820
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500821bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
822 GrMipMapped mipMapped,
823 const BackendTextureData* data) {
824 int numMipLevels = 1;
825 if (mipMapped == GrMipMapped::kYes) {
826 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500827 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400828
Robert Phillips42716d42019-12-16 12:19:54 -0500829 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400830 return true;
831 }
832
Robert Phillips42716d42019-12-16 12:19:54 -0500833 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500834 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500835 }
836
837 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
838
Brian Salomon85c3d682019-11-04 15:04:54 -0500839 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400840 return false;
841 }
842
Brian Salomon85c3d682019-11-04 15:04:54 -0500843 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500844 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500845 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500846 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400847 return false;
848 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500849 if (colorType != data->pixmap(i).colorType()) {
850 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400851 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400852 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400853 return true;
854}
855
Robert Phillipsb915c942019-12-17 14:44:37 -0500856bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
857 GrMipMapped mipMapped, const BackendTextureData* data) {
858
859 if (!data || data->type() == BackendTextureData::Type::kColor) {
860 return true;
861 }
862
863 if (data->type() == BackendTextureData::Type::kPixmaps) {
864 return false;
865 }
866
867 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
868
Robert Phillips99dead92020-01-27 16:11:57 -0500869 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
870 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500871
872 return computedSize == data->compressedSize();
873}
874
Brian Salomon85c3d682019-11-04 15:04:54 -0500875GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
876 const GrBackendFormat& format,
877 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500878 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400879 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400880 const GrCaps* caps = this->caps();
881
882 if (!format.isValid()) {
883 return {};
884 }
885
Robert Phillipsd34691b2019-09-24 13:38:43 -0400886 if (caps->isFormatCompressed(format)) {
887 // Compressed formats must go through the createCompressedBackendTexture API
888 return {};
889 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400890
Brian Salomon85c3d682019-11-04 15:04:54 -0500891 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
892 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400893 return {};
894 }
895
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500896 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400897 return {};
898 }
899
Greg Daniel16032b32020-05-06 15:31:10 -0400900 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
901}
902
903bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400904 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400905 const BackendTextureData* data) {
906 SkASSERT(data);
907 const GrCaps* caps = this->caps();
908
Greg Daniel16032b32020-05-06 15:31:10 -0400909 if (!backendTexture.isValid()) {
910 return false;
911 }
912
913 if (data->type() == BackendTextureData::Type::kPixmaps) {
914 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
915 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
916 return false;
917 }
918 }
919
920 if (backendTexture.hasMipMaps() && !this->caps()->mipMapSupport()) {
921 return false;
922 }
923
924 GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
925 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) {
926 return false;
927 }
928
Greg Daniel25597782020-06-11 13:15:08 -0400929 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400930}
Robert Phillipsb915c942019-12-17 14:44:37 -0500931
932GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
933 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500934 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500935 GrProtected isProtected,
Greg Daniel25597782020-06-11 13:15:08 -0400936 sk_sp<GrRefCntedCallback> finishedCallback,
Robert Phillips4277f012020-01-21 14:28:34 -0500937 const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500938 const GrCaps* caps = this->caps();
939
940 if (!format.isValid()) {
941 return {};
942 }
943
Greg Daniel01f278c2020-06-12 16:58:17 -0400944 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillipsb915c942019-12-17 14:44:37 -0500945 if (compressionType == SkImage::CompressionType::kNone) {
946 // Uncompressed formats must go through the createBackendTexture API
947 return {};
948 }
949
950 if (dimensions.isEmpty() ||
951 dimensions.width() > caps->maxTextureSize() ||
952 dimensions.height() > caps->maxTextureSize()) {
953 return {};
954 }
955
956 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
957 return {};
958 }
959
960 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
961 return {};
962 }
963
Robert Phillips4277f012020-01-21 14:28:34 -0500964 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400965 isProtected, std::move(finishedCallback), data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500966}
Stephen Whitef3d5d442020-04-08 10:35:58 -0400967
968GrStagingBuffer* GrGpu::findStagingBuffer(size_t size) {
969#ifdef SK_DEBUG
970 this->validateStagingBuffers();
971#endif
972 for (auto b : fActiveStagingBuffers) {
973 if (b->remaining() >= size) {
974 return b;
975 }
976 }
977 for (auto b : fAvailableStagingBuffers) {
978 if (b->remaining() >= size) {
979 fAvailableStagingBuffers.remove(b);
980 fActiveStagingBuffers.addToTail(b);
981 return b;
982 }
983 }
984 size = SkNextPow2(size);
985 size = std::max(size, kMinStagingBufferSize);
986 std::unique_ptr<GrStagingBuffer> b = this->createStagingBuffer(size);
987 GrStagingBuffer* stagingBuffer = b.get();
988 fStagingBuffers.push_back(std::move(b));
989 fActiveStagingBuffers.addToTail(stagingBuffer);
990 return stagingBuffer;
991}
992
993GrStagingBuffer::Slice GrGpu::allocateStagingBufferSlice(size_t size) {
994#ifdef SK_DEBUG
995 this->validateStagingBuffers();
996#endif
997 GrStagingBuffer* stagingBuffer = this->findStagingBuffer(size);
998 return stagingBuffer->allocate(size);
999}
1000
1001void GrGpu::unmapStagingBuffers() {
1002#ifdef SK_DEBUG
1003 this->validateStagingBuffers();
1004#endif
1005 // Unmap all active buffers.
1006 for (auto buffer : fActiveStagingBuffers) {
1007 buffer->unmap();
1008 }
1009}
1010
Stephen White7a026142020-05-13 17:16:33 -04001011void GrGpu::moveStagingBufferFromBusyToAvailable(GrStagingBuffer* buffer) {
Stephen Whitef3d5d442020-04-08 10:35:58 -04001012#ifdef SK_DEBUG
1013 this->validateStagingBuffers();
1014#endif
1015 fBusyStagingBuffers.remove(buffer);
1016 fAvailableStagingBuffers.addToTail(buffer);
1017}
Stephen White7a026142020-05-13 17:16:33 -04001018
1019void GrGpu::moveStagingBufferFromActiveToBusy(GrStagingBuffer* buffer) {
1020#ifdef SK_DEBUG
1021 this->validateStagingBuffers();
1022#endif
1023 fActiveStagingBuffers.remove(buffer);
1024 fBusyStagingBuffers.addToTail(buffer);
1025}