blob: fccb22574d180d31dc85dd670e8852573a516e54 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrContext.h"
Robert Phillips99dead92020-01-27 16:11:57 -050014#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkMathPriv.h"
Robert Phillips57ef6802019-09-23 10:12:47 -040016#include "src/core/SkMipMap.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrCaps.h"
19#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040020#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060022#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrPathRendering.h"
24#include "src/gpu/GrPipeline.h"
25#include "src/gpu/GrRenderTargetPriv.h"
26#include "src/gpu/GrResourceCache.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrSemaphore.h"
Stephen Whitef3d5d442020-04-08 10:35:58 -040029#include "src/gpu/GrStagingBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrStencilAttachment.h"
31#include "src/gpu/GrStencilSettings.h"
32#include "src/gpu/GrSurfacePriv.h"
33#include "src/gpu/GrTexturePriv.h"
34#include "src/gpu/GrTextureProxyPriv.h"
35#include "src/gpu/GrTracing.h"
36#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070037
Stephen Whitef3d5d442020-04-08 10:35:58 -040038static const size_t kMinStagingBufferSize = 32 * 1024;
39
bsalomon@google.comd302f142011-03-03 13:54:13 +000040////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Brian Salomone2826ab2019-06-04 15:58:31 -040042GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000043
Stephen Whitef3d5d442020-04-08 10:35:58 -040044GrGpu::~GrGpu() {
Greg Daniel55822f12020-05-26 11:26:45 -040045 this->callSubmittedProcs(false);
Stephen Whitef3d5d442020-04-08 10:35:58 -040046 SkASSERT(fBusyStagingBuffers.isEmpty());
47}
bsalomon1d89ddc2014-08-19 14:20:58 -070048
Stephen White7a026142020-05-13 17:16:33 -040049void GrGpu::disconnect(DisconnectType type) {
50 if (DisconnectType::kAbandon == type) {
51 fAvailableStagingBuffers.reset();
52 fActiveStagingBuffers.reset();
53 fBusyStagingBuffers.reset();
54 }
55 fStagingBuffers.clear();
56}
reed@google.comac10a2d2010-12-22 21:39:39 +000057
bsalomon@google.comd302f142011-03-03 13:54:13 +000058////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000059
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000060bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
Brian Salomonc8d092a2020-02-24 10:14:21 -050061 GrSamplerState::Filter filter) {
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000062 SkASSERT(texProxy);
Brian Salomonc8d092a2020-02-24 10:14:21 -050063 if (filter != GrSamplerState::Filter::kMipMap || texProxy->mipMapped() == GrMipMapped::kYes ||
64 !caps->mipMapSupport()) {
65 return false;
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000066 }
Brian Salomonc8d092a2020-02-24 10:14:21 -050067 return SkMipMap::ComputeLevelCount(texProxy->width(), texProxy->height()) > 0;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040068}
69
Brian Salomona56a7462020-02-07 14:17:25 -050070static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040071 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040072 SkASSERT(mipLevelCount > 0);
73 bool hasBasePixels = texels[0].fPixels;
74 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040075 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050076 int w = dimensions.fWidth;
77 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040078 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
79 if (texels[currentMipLevel].fPixels) {
80 const size_t minRowBytes = w * bpp;
81 if (caps->writePixelsRowBytesSupport()) {
82 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
83 return false;
84 }
85 if (texels[currentMipLevel].fRowBytes % bpp) {
86 return false;
87 }
88 } else {
89 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
90 return false;
91 }
92 }
93 ++levelsWithPixelsCnt;
94 }
95 if (w == 1 && h == 1) {
96 if (currentMipLevel != mipLevelCount - 1) {
97 return false;
98 }
99 } else {
100 w = std::max(w / 2, 1);
101 h = std::max(h / 2, 1);
102 }
103 }
104 // Either just a base layer or a full stack is required.
105 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
106 return false;
107 }
108 // Can specify just the base, all levels, or no levels.
109 if (!hasBasePixels) {
110 return levelsWithPixelsCnt == 0;
111 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400112 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400113}
114
Brian Salomona56a7462020-02-07 14:17:25 -0500115sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400116 const GrBackendFormat& format,
117 GrRenderable renderable,
118 int renderTargetSampleCnt,
119 SkBudgeted budgeted,
120 GrProtected isProtected,
121 int mipLevelCount,
122 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400123 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400124 // Call GrGpu::createCompressedTexture.
125 return nullptr;
126 }
cblume55f2d2d2016-02-26 13:20:48 -0800127
Brian Salomona90382f2019-09-17 09:01:56 -0400128 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500129 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
130 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700131 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700132 }
133
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400134 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400135 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400136 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500137 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400138 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400139 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400140 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500141 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400142 format,
143 renderable,
144 renderTargetSampleCnt,
145 budgeted,
146 isProtected,
147 mipLevelCount,
148 levelClearMask);
149 if (tex) {
150 SkASSERT(tex->backendFormat() == format);
151 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
152 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
153 tex->resourcePriv().removeScratchKey();
154 }
155 fStats.incTextureCreates();
156 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
157 SkASSERT(GrRenderable::kYes == renderable);
158 tex->asRenderTarget()->setRequiresManualMSAAResolve();
159 }
160 }
161 return tex;
162}
163
Brian Salomona56a7462020-02-07 14:17:25 -0500164sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400165 const GrBackendFormat& format,
166 GrRenderable renderable,
167 int renderTargetSampleCnt,
168 GrMipMapped mipMapped,
169 SkBudgeted budgeted,
170 GrProtected isProtected) {
171 int mipLevelCount = 1;
172 if (mipMapped == GrMipMapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500173 mipLevelCount =
174 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400175 }
176 uint32_t levelClearMask =
177 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500178 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
179 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomona90382f2019-09-17 09:01:56 -0400180 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
181 tex->texturePriv().markMipMapsClean();
182 }
183 return tex;
184}
185
Brian Salomona56a7462020-02-07 14:17:25 -0500186sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400187 const GrBackendFormat& format,
188 GrRenderable renderable,
189 int renderTargetSampleCnt,
190 SkBudgeted budgeted,
191 GrProtected isProtected,
192 GrColorType textureColorType,
193 GrColorType srcColorType,
194 const GrMipLevel texels[],
195 int texelLevelCount) {
196 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400197 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500198 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400199 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400200 return nullptr;
201 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400202 }
203
Brian Osman788b9162020-02-07 10:36:46 -0500204 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400205 uint32_t levelClearMask = 0;
206 if (this->caps()->shouldInitializeTextures()) {
207 if (texelLevelCount) {
208 for (int i = 0; i < mipLevelCount; ++i) {
209 if (!texels->fPixels) {
210 levelClearMask |= static_cast<uint32_t>(1 << i);
211 }
212 }
213 } else {
214 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
215 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400216 }
217
Brian Salomona56a7462020-02-07 14:17:25 -0500218 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
219 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800220 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400221 bool markMipLevelsClean = false;
222 // Currently if level 0 does not have pixels then no other level may, as enforced by
223 // validate_texel_levels.
224 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500225 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
226 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400227 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800228 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400229 // Currently if level[1] of mip map has pixel data then so must all other levels.
230 // as enforced by validate_texel_levels.
231 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
232 fStats.incTextureUploads();
233 } else if (levelClearMask && mipLevelCount > 1) {
234 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800235 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400236 if (markMipLevelsClean) {
237 tex->texturePriv().markMipMapsClean();
238 }
bsalomonb12ea412015-02-02 21:19:50 -0800239 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000240 return tex;
241}
242
Robert Phillips9f744f72019-12-19 19:14:33 -0500243sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400244 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500245 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500246 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500247 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500248 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400249 size_t dataSize) {
250 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500251 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
252 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400253 return nullptr;
254 }
Brian Salomona3e29962019-07-16 11:52:08 -0400255 // Note if we relax the requirement that data must be provided then we must check
256 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400257 if (!data) {
258 return nullptr;
259 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400260 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400261 return nullptr;
262 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500263
264 // TODO: expand CompressedDataIsCorrect to work here too
265 SkImage::CompressionType compressionType = this->caps()->compressionType(format);
266
Robert Phillips99dead92020-01-27 16:11:57 -0500267 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
268 mipMapped == GrMipMapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400269 return nullptr;
270 }
Robert Phillips3a833922020-01-21 15:25:58 -0500271 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
272 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400273}
274
Greg Daniel7ef28f32017-04-20 16:41:55 +0000275sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400276 GrWrapOwnership ownership,
277 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500278 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500279 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000280 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400281
282 const GrCaps* caps = this->caps();
283 SkASSERT(caps);
284
Greg Daniel7bfc9132019-08-14 14:23:53 -0400285 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800286 return nullptr;
287 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400288 if (backendTex.width() > caps->maxTextureSize() ||
289 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800290 return nullptr;
291 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400292
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400293 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400294}
Eric Karl5c779752017-05-08 12:02:07 -0700295
Robert Phillipsb915c942019-12-17 14:44:37 -0500296sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
297 GrWrapOwnership ownership,
298 GrWrapCacheable cacheable) {
299 this->handleDirtyContext();
300
301 const GrCaps* caps = this->caps();
302 SkASSERT(caps);
303
304 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
305 return nullptr;
306 }
307 if (backendTex.width() > caps->maxTextureSize() ||
308 backendTex.height() > caps->maxTextureSize()) {
309 return nullptr;
310 }
311
312 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
313}
314
Brian Salomond17f6582017-07-19 18:28:58 -0400315sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400316 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400317 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500318 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400319 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500320 if (sampleCnt < 1) {
321 return nullptr;
322 }
Robert Phillips0902c982019-07-16 07:47:56 -0400323
324 const GrCaps* caps = this->caps();
325
Greg Daniel7bfc9132019-08-14 14:23:53 -0400326 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400327 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400328 return nullptr;
329 }
330
Robert Phillips0902c982019-07-16 07:47:56 -0400331 if (backendTex.width() > caps->maxRenderTargetSize() ||
332 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400333 return nullptr;
334 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400335 sk_sp<GrTexture> tex =
336 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400337 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600338 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
339 tex->asRenderTarget()->setRequiresManualMSAAResolve();
340 }
bungeman6bd52842016-10-27 09:30:08 -0700341 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000342}
343
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400344sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400345 this->handleDirtyContext();
346
347 const GrCaps* caps = this->caps();
348
Greg Daniel6fa62e22019-08-07 15:52:37 -0400349 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800350 return nullptr;
351 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400352
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400353 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500354 if (backendRT.isFramebufferOnly()) {
355 rt->setFramebufferOnly();
356 }
357 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000358}
359
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400360sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400361 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500362 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400363
364 const GrCaps* caps = this->caps();
365
366 int maxSize = caps->maxTextureSize();
367 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
368 return nullptr;
369 }
370
Greg Daniel6fa62e22019-08-07 15:52:37 -0400371 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400372 return nullptr;
373 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400374
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400375 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Chris Dalton3f7932e2019-08-19 00:39:13 -0600376 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
377 rt->setRequiresManualMSAAResolve();
378 }
379 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800380}
381
Greg Danielb46add82019-01-02 14:51:29 -0500382sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
383 const GrVkDrawableInfo& vkInfo) {
384 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
385}
386
387sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
388 const GrVkDrawableInfo& vkInfo) {
389 // This is only supported on Vulkan so we default to returning nullptr here
390 return nullptr;
391}
392
Brian Salomondbf70722019-02-07 11:31:24 -0500393sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
394 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400395 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000396 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500397 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700398 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700399 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700400 }
cdalton397536c2016-03-25 12:15:03 -0700401 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800402}
403
Greg Daniel46cfbc62019-06-07 11:43:30 -0400404bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400405 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400406 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700407 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500408 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500409
410 if (dst->readOnly()) {
411 return false;
412 }
413
joshualitt1cbdcde2015-08-21 11:53:29 -0700414 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500415
Greg Daniele227fe42019-08-21 13:52:24 -0400416 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700417}
418
Brian Salomona6948702018-06-01 15:33:20 -0400419bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400420 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
421 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400422 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500423 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500424 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400425 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500426
Brian Salomon1d435302019-07-01 13:05:28 -0400427 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
428 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
429 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700430 return false;
431 }
432
Brian Salomon1047a492019-07-02 12:25:21 -0400433 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
434 if (!this->caps()->readPixelsRowBytesSupport()) {
435 if (rowBytes != minRowBytes) {
436 return false;
437 }
438 } else {
439 if (rowBytes < minRowBytes) {
440 return false;
441 }
442 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
443 return false;
444 }
445 }
446
Brian Salomonbf7b6202016-11-11 16:08:03 -0500447 this->handleDirtyContext();
448
Brian Salomonf77c1462019-08-01 15:19:29 -0400449 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
450 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000451}
452
Brian Salomona9b04b92018-06-01 15:04:28 -0400453bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400454 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400455 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400456 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev1db4d142020-04-10 00:58:52 -0400457 ATRACE_ANDROID_FRAMEWORK_ALWAYS("texture_upload");
Brian Salomonbf7b6202016-11-11 16:08:03 -0500458 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500459 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500460
461 if (surface->readOnly()) {
462 return false;
463 }
464
Brian Salomon1047a492019-07-02 12:25:21 -0400465 if (mipLevelCount == 0) {
466 return false;
467 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400468 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400469 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
470 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400471 if (!bounds.contains(subRect)) {
472 return false;
473 }
474 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
475 // We require that if the texels are mipped, than the write region is the entire surface
476 return false;
477 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400478
Brian Salomona56a7462020-02-07 14:17:25 -0500479 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
480 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400481 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800482 }
jvanverth2dc29942015-09-01 07:16:46 -0700483
bsalomon@google.com6f379512011-11-16 20:36:03 +0000484 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400485 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400486 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700487 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400488 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800489 fStats.incTextureUploads();
490 return true;
491 }
492 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000493}
494
Brian Salomone05ba5a2019-04-08 11:59:07 -0400495bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400496 GrColorType textureColorType, GrColorType bufferColorType,
497 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400498 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500499 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700500 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800501
Brian Salomonc67c31c2018-12-06 10:00:03 -0500502 if (texture->readOnly()) {
503 return false;
504 }
505
Greg Daniel660cc992017-06-26 14:55:05 -0400506 // We require that the write region is contained in the texture
507 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
508 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
509 if (!bounds.contains(subRect)) {
510 return false;
511 }
512
Brian Salomonb28cb682019-07-26 12:48:47 -0400513 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400514 if (this->caps()->writePixelsRowBytesSupport()) {
515 if (rowBytes < SkToSizeT(bpp * width)) {
516 return false;
517 }
518 if (rowBytes % bpp) {
519 return false;
520 }
521 } else {
522 if (rowBytes != SkToSizeT(bpp * width)) {
523 return false;
524 }
525 }
526
jvanverth17aa0472016-01-05 10:41:27 -0800527 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400528 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
529 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700530 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500531 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800532 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700533
jvanverth17aa0472016-01-05 10:41:27 -0800534 return true;
535 }
536 return false;
537}
538
Brian Salomon26de56e2019-04-10 12:14:26 -0400539bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400540 GrColorType surfaceColorType, GrColorType bufferColorType,
541 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400542 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400543 SkASSERT(surface);
544 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400545 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400546
Greg Danielba88ab62019-07-26 09:14:01 -0400547#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400548 auto supportedRead = this->caps()->supportedReadPixelsColorType(
549 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400550 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
551 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
552#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400553
554 // We require that the write region is contained in the texture
555 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
556 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
557 if (!bounds.contains(subRect)) {
558 return false;
559 }
560
561 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400562 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
563 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400564 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400565 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400566 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400567 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400568}
569
Brian Salomon930f9392018-06-20 16:25:26 -0400570bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400571 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400572 SkASSERT(texture);
573 SkASSERT(this->caps()->mipMapSupport());
574 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600575 if (!texture->texturePriv().mipMapsAreDirty()) {
576 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
577 // actual target. This may be caused by things that the drawingManager could not predict,
578 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
579 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
580 return true;
581 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500582 if (texture->readOnly()) {
583 return false;
584 }
Brian Salomon930f9392018-06-20 16:25:26 -0400585 if (this->onRegenerateMipMapLevels(texture)) {
586 texture->texturePriv().markMipMapsClean();
587 return true;
588 }
589 return false;
590}
591
Brian Salomon1f05d452019-02-08 12:33:08 -0500592void GrGpu::resetTextureBindings() {
593 this->handleDirtyContext();
594 this->onResetTextureBindings();
595}
596
Chris Dalton16a33c62019-09-24 22:19:17 -0600597void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -0500598 ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000599 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000600 this->handleDirtyContext();
Greg Daniel242536f2020-02-13 14:12:46 -0500601 this->onResolveRenderTarget(target, resolveRect, forExternalIO);
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);
675 this->insertSemaphore(semaphores[i].get());
Greg Daniel51316782017-08-02 15:10:09 +0000676 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400677 semaphores[i] = resourceProvider->makeSemaphore(false);
678 if (semaphores[i]) {
679 this->insertSemaphore(semaphores[i].get());
680 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
681 }
Greg Daniel51316782017-08-02 15:10:09 +0000682 }
683 }
684 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500685
Greg Danielfe159622020-04-10 17:43:51 +0000686 if (info.fFinishedProc) {
687 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
688 }
Greg Daniel55822f12020-05-26 11:26:45 -0400689
690 if (info.fSubmittedProc) {
691 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
692 }
693
Greg Daniel9efe3862020-06-11 11:51:06 -0400694 // We currently don't support passing in new surface state for multiple proxies here. The only
695 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
696 // updates anyways.
697 SkASSERT(!newState || numProxies == 1);
698 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
699 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, numProxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000700}
701
702bool GrGpu::submitToGpu(bool syncCpu) {
703 this->stats()->incNumSubmitToGpus();
704
705#ifdef SK_DEBUG
706 this->validateStagingBuffers();
707#endif
Stephen Whitef3d5d442020-04-08 10:35:58 -0400708 this->unmapStagingBuffers();
709
Greg Danielfe159622020-04-10 17:43:51 +0000710 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400711
Greg Daniel55822f12020-05-26 11:26:45 -0400712 this->callSubmittedProcs(submitted);
713
Greg Danielfe159622020-04-10 17:43:51 +0000714 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000715}
Brian Osman71a18892017-08-10 10:23:25 -0400716
Greg Daniel55822f12020-05-26 11:26:45 -0400717void GrGpu::callSubmittedProcs(bool success) {
718 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
719 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
720 }
721 fSubmittedProcs.reset();
722}
723
Kevin Lubickf4def342018-10-04 12:52:50 -0400724#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400725void GrGpu::dumpJSON(SkJSONWriter* writer) const {
726 writer->beginObject();
727
728 // TODO: Is there anything useful in the base class to dump here?
729
730 this->onDumpJSON(writer);
731
732 writer->endObject();
733}
Kevin Lubickf4def342018-10-04 12:52:50 -0400734#else
735void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
736#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400737
Robert Phillipsf0ced622019-05-16 09:06:25 -0400738#if GR_TEST_UTILS
739
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500740#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500741static const char* cache_result_to_str(int i) {
742 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
743 "hits",
744 "misses",
745 "partials"
746 };
747 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
748 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
749 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
750 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
751 return kCacheResultStrings[i];
752}
753
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500754void GrGpu::Stats::dump(SkString* out) {
755 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
756 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
757 out->appendf("Textures Created: %d\n", fTextureCreates);
758 out->appendf("Texture Uploads: %d\n", fTextureUploads);
759 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400760 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500761 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
762 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400763 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500764
765 SkASSERT(fNumInlineCompilationFailures == 0);
766 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
767 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
768 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
769 fInlineProgramCacheStats[i]);
770 }
771
772 SkASSERT(fNumPreCompilationFailures == 0);
773 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
774 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
775 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
776 fPreProgramCacheStats[i]);
777 }
778
779 SkASSERT(fNumCompilationFailures == 0);
780 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
781 out->appendf("Total number of partial compilation successes %d\n",
782 fNumPartialCompilationSuccesses);
783 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500784
785 // enable this block to output CSV-style stats for program pre-compilation
786#if 0
787 SkASSERT(fNumInlineCompilationFailures == 0);
788 SkASSERT(fNumPreCompilationFailures == 0);
789 SkASSERT(fNumCompilationFailures == 0);
790 SkASSERT(fNumPartialCompilationSuccesses == 0);
791
792 SkDebugf("%d, %d, %d, %d, %d\n",
793 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
794 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
795 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
796 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
797 fNumCompilationSuccesses);
798#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500799}
800
801void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
802 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
803 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500804}
805
Robert Phillips57ef6802019-09-23 10:12:47 -0400806#endif // GR_GPU_STATS
807#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500808
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500809bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
810 GrMipMapped mipMapped,
811 const BackendTextureData* data) {
812 int numMipLevels = 1;
813 if (mipMapped == GrMipMapped::kYes) {
814 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500815 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400816
Robert Phillips42716d42019-12-16 12:19:54 -0500817 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400818 return true;
819 }
820
Robert Phillips42716d42019-12-16 12:19:54 -0500821 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500822 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500823 }
824
825 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
826
Brian Salomon85c3d682019-11-04 15:04:54 -0500827 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400828 return false;
829 }
830
Brian Salomon85c3d682019-11-04 15:04:54 -0500831 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500832 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500833 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500834 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400835 return false;
836 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500837 if (colorType != data->pixmap(i).colorType()) {
838 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400839 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400840 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400841 return true;
842}
843
Robert Phillipsb915c942019-12-17 14:44:37 -0500844bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
845 GrMipMapped mipMapped, const BackendTextureData* data) {
846
847 if (!data || data->type() == BackendTextureData::Type::kColor) {
848 return true;
849 }
850
851 if (data->type() == BackendTextureData::Type::kPixmaps) {
852 return false;
853 }
854
855 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
856
Robert Phillips99dead92020-01-27 16:11:57 -0500857 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
858 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500859
860 return computedSize == data->compressedSize();
861}
862
Brian Salomon85c3d682019-11-04 15:04:54 -0500863GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
864 const GrBackendFormat& format,
865 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500866 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400867 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400868 const GrCaps* caps = this->caps();
869
870 if (!format.isValid()) {
871 return {};
872 }
873
Robert Phillipsd34691b2019-09-24 13:38:43 -0400874 if (caps->isFormatCompressed(format)) {
875 // Compressed formats must go through the createCompressedBackendTexture API
876 return {};
877 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400878
Brian Salomon85c3d682019-11-04 15:04:54 -0500879 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
880 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400881 return {};
882 }
883
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500884 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400885 return {};
886 }
887
Greg Daniel16032b32020-05-06 15:31:10 -0400888 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
889}
890
891bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400892 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400893 const BackendTextureData* data) {
894 SkASSERT(data);
895 const GrCaps* caps = this->caps();
896
Greg Daniel16032b32020-05-06 15:31:10 -0400897 if (!backendTexture.isValid()) {
898 return false;
899 }
900
901 if (data->type() == BackendTextureData::Type::kPixmaps) {
902 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
903 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
904 return false;
905 }
906 }
907
908 if (backendTexture.hasMipMaps() && !this->caps()->mipMapSupport()) {
909 return false;
910 }
911
912 GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
913 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) {
914 return false;
915 }
916
Greg Daniel25597782020-06-11 13:15:08 -0400917 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400918}
Robert Phillipsb915c942019-12-17 14:44:37 -0500919
920GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
921 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500922 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500923 GrProtected isProtected,
Greg Daniel25597782020-06-11 13:15:08 -0400924 sk_sp<GrRefCntedCallback> finishedCallback,
Robert Phillips4277f012020-01-21 14:28:34 -0500925 const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500926 const GrCaps* caps = this->caps();
927
928 if (!format.isValid()) {
929 return {};
930 }
931
932 SkImage::CompressionType compressionType = caps->compressionType(format);
933 if (compressionType == SkImage::CompressionType::kNone) {
934 // Uncompressed formats must go through the createBackendTexture API
935 return {};
936 }
937
938 if (dimensions.isEmpty() ||
939 dimensions.width() > caps->maxTextureSize() ||
940 dimensions.height() > caps->maxTextureSize()) {
941 return {};
942 }
943
944 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
945 return {};
946 }
947
948 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
949 return {};
950 }
951
Robert Phillips4277f012020-01-21 14:28:34 -0500952 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
Greg Daniel25597782020-06-11 13:15:08 -0400953 isProtected, std::move(finishedCallback), data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500954}
Stephen Whitef3d5d442020-04-08 10:35:58 -0400955
956GrStagingBuffer* GrGpu::findStagingBuffer(size_t size) {
957#ifdef SK_DEBUG
958 this->validateStagingBuffers();
959#endif
960 for (auto b : fActiveStagingBuffers) {
961 if (b->remaining() >= size) {
962 return b;
963 }
964 }
965 for (auto b : fAvailableStagingBuffers) {
966 if (b->remaining() >= size) {
967 fAvailableStagingBuffers.remove(b);
968 fActiveStagingBuffers.addToTail(b);
969 return b;
970 }
971 }
972 size = SkNextPow2(size);
973 size = std::max(size, kMinStagingBufferSize);
974 std::unique_ptr<GrStagingBuffer> b = this->createStagingBuffer(size);
975 GrStagingBuffer* stagingBuffer = b.get();
976 fStagingBuffers.push_back(std::move(b));
977 fActiveStagingBuffers.addToTail(stagingBuffer);
978 return stagingBuffer;
979}
980
981GrStagingBuffer::Slice GrGpu::allocateStagingBufferSlice(size_t size) {
982#ifdef SK_DEBUG
983 this->validateStagingBuffers();
984#endif
985 GrStagingBuffer* stagingBuffer = this->findStagingBuffer(size);
986 return stagingBuffer->allocate(size);
987}
988
989void GrGpu::unmapStagingBuffers() {
990#ifdef SK_DEBUG
991 this->validateStagingBuffers();
992#endif
993 // Unmap all active buffers.
994 for (auto buffer : fActiveStagingBuffers) {
995 buffer->unmap();
996 }
997}
998
Stephen White7a026142020-05-13 17:16:33 -0400999void GrGpu::moveStagingBufferFromBusyToAvailable(GrStagingBuffer* buffer) {
Stephen Whitef3d5d442020-04-08 10:35:58 -04001000#ifdef SK_DEBUG
1001 this->validateStagingBuffers();
1002#endif
1003 fBusyStagingBuffers.remove(buffer);
1004 fAvailableStagingBuffers.addToTail(buffer);
1005}
Stephen White7a026142020-05-13 17:16:33 -04001006
1007void GrGpu::moveStagingBufferFromActiveToBusy(GrStagingBuffer* buffer) {
1008#ifdef SK_DEBUG
1009 this->validateStagingBuffers();
1010#endif
1011 fActiveStagingBuffers.remove(buffer);
1012 fBusyStagingBuffers.addToTail(buffer);
1013}