blob: 6f65b1a25fd1d2981451fab860fa00893b514eaa [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
Adlai Holler3d0359a2020-07-09 15:35:55 -040013#include "include/gpu/GrDirectContext.h"
Robert Phillips99dead92020-01-27 16:11:57 -050014#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkMathPriv.h"
Mike Reed13711eb2020-07-14 17:16:32 -040016#include "src/core/SkMipmap.h"
Greg 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
Brian Salomona56a7462020-02-07 14:17:25 -050061static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040062 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040063 SkASSERT(mipLevelCount > 0);
64 bool hasBasePixels = texels[0].fPixels;
65 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040066 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050067 int w = dimensions.fWidth;
68 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040069 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
70 if (texels[currentMipLevel].fPixels) {
71 const size_t minRowBytes = w * bpp;
72 if (caps->writePixelsRowBytesSupport()) {
73 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
74 return false;
75 }
76 if (texels[currentMipLevel].fRowBytes % bpp) {
77 return false;
78 }
79 } else {
80 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
81 return false;
82 }
83 }
84 ++levelsWithPixelsCnt;
85 }
86 if (w == 1 && h == 1) {
87 if (currentMipLevel != mipLevelCount - 1) {
88 return false;
89 }
90 } else {
91 w = std::max(w / 2, 1);
92 h = std::max(h / 2, 1);
93 }
94 }
95 // Either just a base layer or a full stack is required.
96 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
97 return false;
98 }
99 // Can specify just the base, all levels, or no levels.
100 if (!hasBasePixels) {
101 return levelsWithPixelsCnt == 0;
102 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400103 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400104}
105
Brian Salomona56a7462020-02-07 14:17:25 -0500106sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400107 const GrBackendFormat& format,
108 GrRenderable renderable,
109 int renderTargetSampleCnt,
110 SkBudgeted budgeted,
111 GrProtected isProtected,
112 int mipLevelCount,
113 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400114 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400115 // Call GrGpu::createCompressedTexture.
116 return nullptr;
117 }
cblume55f2d2d2016-02-26 13:20:48 -0800118
Brian Salomona90382f2019-09-17 09:01:56 -0400119 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500120 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
121 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700122 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700123 }
124
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400125 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400126 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400127 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500128 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400129 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400130 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400131 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500132 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400133 format,
134 renderable,
135 renderTargetSampleCnt,
136 budgeted,
137 isProtected,
138 mipLevelCount,
139 levelClearMask);
140 if (tex) {
141 SkASSERT(tex->backendFormat() == format);
142 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
143 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
144 tex->resourcePriv().removeScratchKey();
145 }
146 fStats.incTextureCreates();
147 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
148 SkASSERT(GrRenderable::kYes == renderable);
149 tex->asRenderTarget()->setRequiresManualMSAAResolve();
150 }
151 }
152 return tex;
153}
154
Brian Salomona56a7462020-02-07 14:17:25 -0500155sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400156 const GrBackendFormat& format,
157 GrRenderable renderable,
158 int renderTargetSampleCnt,
159 GrMipMapped mipMapped,
160 SkBudgeted budgeted,
161 GrProtected isProtected) {
162 int mipLevelCount = 1;
163 if (mipMapped == GrMipMapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500164 mipLevelCount =
165 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400166 }
167 uint32_t levelClearMask =
168 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500169 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
170 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomona90382f2019-09-17 09:01:56 -0400171 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
172 tex->texturePriv().markMipMapsClean();
173 }
174 return tex;
175}
176
Brian Salomona56a7462020-02-07 14:17:25 -0500177sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400178 const GrBackendFormat& format,
179 GrRenderable renderable,
180 int renderTargetSampleCnt,
181 SkBudgeted budgeted,
182 GrProtected isProtected,
183 GrColorType textureColorType,
184 GrColorType srcColorType,
185 const GrMipLevel texels[],
186 int texelLevelCount) {
187 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400188 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500189 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400190 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400191 return nullptr;
192 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400193 }
194
Brian Osman788b9162020-02-07 10:36:46 -0500195 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400196 uint32_t levelClearMask = 0;
197 if (this->caps()->shouldInitializeTextures()) {
198 if (texelLevelCount) {
199 for (int i = 0; i < mipLevelCount; ++i) {
200 if (!texels->fPixels) {
201 levelClearMask |= static_cast<uint32_t>(1 << i);
202 }
203 }
204 } else {
205 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
206 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400207 }
208
Brian Salomona56a7462020-02-07 14:17:25 -0500209 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
210 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800211 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400212 bool markMipLevelsClean = false;
213 // Currently if level 0 does not have pixels then no other level may, as enforced by
214 // validate_texel_levels.
215 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500216 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
217 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400218 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800219 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400220 // Currently if level[1] of mip map has pixel data then so must all other levels.
221 // as enforced by validate_texel_levels.
222 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
223 fStats.incTextureUploads();
224 } else if (levelClearMask && mipLevelCount > 1) {
225 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800226 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400227 if (markMipLevelsClean) {
228 tex->texturePriv().markMipMapsClean();
229 }
bsalomonb12ea412015-02-02 21:19:50 -0800230 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000231 return tex;
232}
233
Robert Phillips9f744f72019-12-19 19:14:33 -0500234sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400235 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500236 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500237 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500238 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500239 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400240 size_t dataSize) {
241 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500242 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
243 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400244 return nullptr;
245 }
Brian Salomona3e29962019-07-16 11:52:08 -0400246 // Note if we relax the requirement that data must be provided then we must check
247 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400248 if (!data) {
249 return nullptr;
250 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400251 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400252 return nullptr;
253 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500254
255 // TODO: expand CompressedDataIsCorrect to work here too
Greg Daniel01f278c2020-06-12 16:58:17 -0400256 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillips9f744f72019-12-19 19:14:33 -0500257
Robert Phillips99dead92020-01-27 16:11:57 -0500258 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
259 mipMapped == GrMipMapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400260 return nullptr;
261 }
Robert Phillips3a833922020-01-21 15:25:58 -0500262 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
263 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400264}
265
Greg Daniel7ef28f32017-04-20 16:41:55 +0000266sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400267 GrWrapOwnership ownership,
268 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500269 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500270 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000271 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400272
273 const GrCaps* caps = this->caps();
274 SkASSERT(caps);
275
Greg Daniel7bfc9132019-08-14 14:23:53 -0400276 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800277 return nullptr;
278 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400279 if (backendTex.width() > caps->maxTextureSize() ||
280 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800281 return nullptr;
282 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400283
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400284 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400285}
Eric Karl5c779752017-05-08 12:02:07 -0700286
Robert Phillipsb915c942019-12-17 14:44:37 -0500287sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
288 GrWrapOwnership ownership,
289 GrWrapCacheable cacheable) {
290 this->handleDirtyContext();
291
292 const GrCaps* caps = this->caps();
293 SkASSERT(caps);
294
295 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
296 return nullptr;
297 }
298 if (backendTex.width() > caps->maxTextureSize() ||
299 backendTex.height() > caps->maxTextureSize()) {
300 return nullptr;
301 }
302
303 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
304}
305
Brian Salomond17f6582017-07-19 18:28:58 -0400306sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400307 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400308 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500309 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400310 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500311 if (sampleCnt < 1) {
312 return nullptr;
313 }
Robert Phillips0902c982019-07-16 07:47:56 -0400314
315 const GrCaps* caps = this->caps();
316
Greg Daniel7bfc9132019-08-14 14:23:53 -0400317 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400318 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400319 return nullptr;
320 }
321
Robert Phillips0902c982019-07-16 07:47:56 -0400322 if (backendTex.width() > caps->maxRenderTargetSize() ||
323 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400324 return nullptr;
325 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400326 sk_sp<GrTexture> tex =
327 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400328 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600329 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
330 tex->asRenderTarget()->setRequiresManualMSAAResolve();
331 }
bungeman6bd52842016-10-27 09:30:08 -0700332 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000333}
334
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400335sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400336 this->handleDirtyContext();
337
338 const GrCaps* caps = this->caps();
339
Greg Daniel6fa62e22019-08-07 15:52:37 -0400340 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800341 return nullptr;
342 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400343
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400344 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500345 if (backendRT.isFramebufferOnly()) {
346 rt->setFramebufferOnly();
347 }
348 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000349}
350
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400351sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400352 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500353 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400354
355 const GrCaps* caps = this->caps();
356
357 int maxSize = caps->maxTextureSize();
358 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
359 return nullptr;
360 }
361
Greg Daniel6fa62e22019-08-07 15:52:37 -0400362 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400363 return nullptr;
364 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400365
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400366 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Chris Dalton3f7932e2019-08-19 00:39:13 -0600367 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
368 rt->setRequiresManualMSAAResolve();
369 }
370 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800371}
372
Greg Danielb46add82019-01-02 14:51:29 -0500373sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
374 const GrVkDrawableInfo& vkInfo) {
375 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
376}
377
378sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
379 const GrVkDrawableInfo& vkInfo) {
380 // This is only supported on Vulkan so we default to returning nullptr here
381 return nullptr;
382}
383
Brian Salomondbf70722019-02-07 11:31:24 -0500384sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
385 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400386 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000387 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500388 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700389 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700390 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700391 }
cdalton397536c2016-03-25 12:15:03 -0700392 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800393}
394
Greg Daniel46cfbc62019-06-07 11:43:30 -0400395bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400396 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400397 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700398 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500399 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500400
401 if (dst->readOnly()) {
402 return false;
403 }
404
joshualitt1cbdcde2015-08-21 11:53:29 -0700405 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500406
Greg Daniele227fe42019-08-21 13:52:24 -0400407 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700408}
409
Brian Salomona6948702018-06-01 15:33:20 -0400410bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400411 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
412 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400413 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500414 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500415 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400416 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500417
Brian Salomon1d435302019-07-01 13:05:28 -0400418 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
419 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
420 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700421 return false;
422 }
423
Brian Salomon1047a492019-07-02 12:25:21 -0400424 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
425 if (!this->caps()->readPixelsRowBytesSupport()) {
426 if (rowBytes != minRowBytes) {
427 return false;
428 }
429 } else {
430 if (rowBytes < minRowBytes) {
431 return false;
432 }
433 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
434 return false;
435 }
436 }
437
Brian Salomonbf7b6202016-11-11 16:08:03 -0500438 this->handleDirtyContext();
439
Brian Salomonf77c1462019-08-01 15:19:29 -0400440 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
441 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000442}
443
Brian Salomona9b04b92018-06-01 15:04:28 -0400444bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400445 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400446 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400447 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev1db4d142020-04-10 00:58:52 -0400448 ATRACE_ANDROID_FRAMEWORK_ALWAYS("texture_upload");
Brian Salomonbf7b6202016-11-11 16:08:03 -0500449 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500450 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500451
452 if (surface->readOnly()) {
453 return false;
454 }
455
Brian Salomon1047a492019-07-02 12:25:21 -0400456 if (mipLevelCount == 0) {
457 return false;
458 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400459 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400460 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
461 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400462 if (!bounds.contains(subRect)) {
463 return false;
464 }
465 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
466 // We require that if the texels are mipped, than the write region is the entire surface
467 return false;
468 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400469
Brian Salomona56a7462020-02-07 14:17:25 -0500470 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
471 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400472 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800473 }
jvanverth2dc29942015-09-01 07:16:46 -0700474
bsalomon@google.com6f379512011-11-16 20:36:03 +0000475 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400476 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400477 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700478 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400479 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800480 fStats.incTextureUploads();
481 return true;
482 }
483 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000484}
485
Brian Salomone05ba5a2019-04-08 11:59:07 -0400486bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400487 GrColorType textureColorType, GrColorType bufferColorType,
488 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400489 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500490 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700491 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800492
Brian Salomonc67c31c2018-12-06 10:00:03 -0500493 if (texture->readOnly()) {
494 return false;
495 }
496
Greg Daniel660cc992017-06-26 14:55:05 -0400497 // We require that the write region is contained in the texture
498 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
499 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
500 if (!bounds.contains(subRect)) {
501 return false;
502 }
503
Brian Salomonb28cb682019-07-26 12:48:47 -0400504 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400505 if (this->caps()->writePixelsRowBytesSupport()) {
506 if (rowBytes < SkToSizeT(bpp * width)) {
507 return false;
508 }
509 if (rowBytes % bpp) {
510 return false;
511 }
512 } else {
513 if (rowBytes != SkToSizeT(bpp * width)) {
514 return false;
515 }
516 }
517
jvanverth17aa0472016-01-05 10:41:27 -0800518 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400519 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
520 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700521 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500522 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800523 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700524
jvanverth17aa0472016-01-05 10:41:27 -0800525 return true;
526 }
527 return false;
528}
529
Brian Salomon26de56e2019-04-10 12:14:26 -0400530bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400531 GrColorType surfaceColorType, GrColorType bufferColorType,
532 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400533 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400534 SkASSERT(surface);
535 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400536 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400537
Greg Danielba88ab62019-07-26 09:14:01 -0400538#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400539 auto supportedRead = this->caps()->supportedReadPixelsColorType(
540 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400541 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
542 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
543#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400544
545 // We require that the write region is contained in the texture
546 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
547 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
548 if (!bounds.contains(subRect)) {
549 return false;
550 }
551
552 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400553 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
554 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400555 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400556 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400557 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400558 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400559}
560
Brian Salomon930f9392018-06-20 16:25:26 -0400561bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400562 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400563 SkASSERT(texture);
564 SkASSERT(this->caps()->mipMapSupport());
565 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600566 if (!texture->texturePriv().mipMapsAreDirty()) {
567 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
568 // actual target. This may be caused by things that the drawingManager could not predict,
569 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
570 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
571 return true;
572 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500573 if (texture->readOnly()) {
574 return false;
575 }
Brian Salomon930f9392018-06-20 16:25:26 -0400576 if (this->onRegenerateMipMapLevels(texture)) {
577 texture->texturePriv().markMipMapsClean();
578 return true;
579 }
580 return false;
581}
582
Brian Salomon1f05d452019-02-08 12:33:08 -0500583void GrGpu::resetTextureBindings() {
584 this->handleDirtyContext();
585 this->onResetTextureBindings();
586}
587
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400588void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000589 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000590 this->handleDirtyContext();
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400591 this->onResolveRenderTarget(target, resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000592}
593
Brian Salomon1fabd512018-02-09 09:54:25 -0500594void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
595 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700596 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500597 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700598 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
599 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700600 GrTexture* texture = surface->asTexture();
601 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400602 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700603 }
604 }
605}
606
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600607int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700608 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600609 SkASSERT(renderTarget->numSamples() > 1 ||
610 (renderTarget->renderTargetPriv().getStencilAttachment() &&
611 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700612
613 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600614 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700615 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
616}
617
Stephen Whitef3d5d442020-04-08 10:35:58 -0400618#ifdef SK_DEBUG
619bool GrGpu::inStagingBuffers(GrStagingBuffer* b) const {
620 for (const auto& i : fStagingBuffers) {
621 if (b == i.get()) {
622 return true;
623 }
624 }
625 return false;
626}
627
628void GrGpu::validateStagingBuffers() const {
629 for (const auto& i : fStagingBuffers) {
630 GrStagingBuffer* buffer = i.get();
631 SkASSERT(fAvailableStagingBuffers.isInList(buffer) ||
632 fActiveStagingBuffers.isInList(buffer) ||
633 fBusyStagingBuffers.isInList(buffer));
634 }
635 for (auto b : fAvailableStagingBuffers) {
636 SkASSERT(this->inStagingBuffers(b));
637 }
638 for (auto b : fActiveStagingBuffers) {
639 SkASSERT(this->inStagingBuffers(b));
640 }
641 for (auto b : fBusyStagingBuffers) {
642 SkASSERT(this->inStagingBuffers(b));
643 }
644}
645#endif
646
Greg Danielfe159622020-04-10 17:43:51 +0000647void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[],
648 int numProxies,
649 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400650 const GrFlushInfo& info,
651 const GrBackendSurfaceMutableState* newState) {
Brian Salomone39526b2019-06-24 16:35:53 -0400652 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000653
Robert Phillips9da87e02019-02-04 13:26:26 -0500654 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500655
Greg Daniel8561fc22020-04-08 12:52:51 -0400656 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
657 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500658 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400659 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400660 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400661 semaphores[i] = resourceProvider->wrapBackendSemaphore(
662 info.fSignalSemaphores[i],
663 GrResourceProvider::SemaphoreWrapType::kWillSignal,
664 kBorrow_GrWrapOwnership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400665 // If we failed to wrap the semaphore it means the client didn't give us a valid
666 // semaphore to begin with. Therefore, it is fine to not signal it.
667 if (semaphores[i]) {
668 this->insertSemaphore(semaphores[i].get());
669 }
Greg Daniel51316782017-08-02 15:10:09 +0000670 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400671 semaphores[i] = resourceProvider->makeSemaphore(false);
672 if (semaphores[i]) {
673 this->insertSemaphore(semaphores[i].get());
674 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
675 }
Greg Daniel51316782017-08-02 15:10:09 +0000676 }
677 }
678 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500679
Greg Danielfe159622020-04-10 17:43:51 +0000680 if (info.fFinishedProc) {
681 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
682 }
Greg Daniel55822f12020-05-26 11:26:45 -0400683
684 if (info.fSubmittedProc) {
685 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
686 }
687
Greg Daniel9efe3862020-06-11 11:51:06 -0400688 // We currently don't support passing in new surface state for multiple proxies here. The only
689 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
690 // updates anyways.
691 SkASSERT(!newState || numProxies == 1);
692 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
693 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, numProxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000694}
695
696bool GrGpu::submitToGpu(bool syncCpu) {
697 this->stats()->incNumSubmitToGpus();
698
699#ifdef SK_DEBUG
700 this->validateStagingBuffers();
701#endif
Stephen Whitef3d5d442020-04-08 10:35:58 -0400702 this->unmapStagingBuffers();
703
Greg Danielfe159622020-04-10 17:43:51 +0000704 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400705
Greg Daniel55822f12020-05-26 11:26:45 -0400706 this->callSubmittedProcs(submitted);
707
Greg Danielfe159622020-04-10 17:43:51 +0000708 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000709}
Brian Osman71a18892017-08-10 10:23:25 -0400710
Brian Salomon24069eb2020-06-24 10:19:52 -0400711bool GrGpu::checkAndResetOOMed() {
712 if (fOOMed) {
713 fOOMed = false;
714 return true;
715 }
716 return false;
717}
718
Greg Daniel55822f12020-05-26 11:26:45 -0400719void GrGpu::callSubmittedProcs(bool success) {
720 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
721 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
722 }
723 fSubmittedProcs.reset();
724}
725
Kevin Lubickf4def342018-10-04 12:52:50 -0400726#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400727void GrGpu::dumpJSON(SkJSONWriter* writer) const {
728 writer->beginObject();
729
730 // TODO: Is there anything useful in the base class to dump here?
731
732 this->onDumpJSON(writer);
733
734 writer->endObject();
735}
Kevin Lubickf4def342018-10-04 12:52:50 -0400736#else
737void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
738#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400739
Robert Phillipsf0ced622019-05-16 09:06:25 -0400740#if GR_TEST_UTILS
741
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500742#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500743static const char* cache_result_to_str(int i) {
744 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
745 "hits",
746 "misses",
747 "partials"
748 };
749 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
750 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
751 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
752 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
753 return kCacheResultStrings[i];
754}
755
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500756void GrGpu::Stats::dump(SkString* out) {
757 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
758 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
759 out->appendf("Textures Created: %d\n", fTextureCreates);
760 out->appendf("Texture Uploads: %d\n", fTextureUploads);
761 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400762 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500763 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
764 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400765 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500766
767 SkASSERT(fNumInlineCompilationFailures == 0);
768 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
769 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
770 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
771 fInlineProgramCacheStats[i]);
772 }
773
774 SkASSERT(fNumPreCompilationFailures == 0);
775 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
776 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
777 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
778 fPreProgramCacheStats[i]);
779 }
780
781 SkASSERT(fNumCompilationFailures == 0);
782 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
783 out->appendf("Total number of partial compilation successes %d\n",
784 fNumPartialCompilationSuccesses);
785 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500786
787 // enable this block to output CSV-style stats for program pre-compilation
788#if 0
789 SkASSERT(fNumInlineCompilationFailures == 0);
790 SkASSERT(fNumPreCompilationFailures == 0);
791 SkASSERT(fNumCompilationFailures == 0);
792 SkASSERT(fNumPartialCompilationSuccesses == 0);
793
794 SkDebugf("%d, %d, %d, %d, %d\n",
795 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
796 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
797 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
798 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
799 fNumCompilationSuccesses);
800#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500801}
802
803void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
804 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
805 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500806}
807
Robert Phillips57ef6802019-09-23 10:12:47 -0400808#endif // GR_GPU_STATS
809#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500810
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500811bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
812 GrMipMapped mipMapped,
813 const BackendTextureData* data) {
814 int numMipLevels = 1;
815 if (mipMapped == GrMipMapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400816 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500817 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400818
Robert Phillips42716d42019-12-16 12:19:54 -0500819 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400820 return true;
821 }
822
Robert Phillips42716d42019-12-16 12:19:54 -0500823 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500824 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500825 }
826
827 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
828
Brian Salomon85c3d682019-11-04 15:04:54 -0500829 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400830 return false;
831 }
832
Brian Salomon85c3d682019-11-04 15:04:54 -0500833 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500834 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500835 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500836 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400837 return false;
838 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500839 if (colorType != data->pixmap(i).colorType()) {
840 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400841 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400842 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400843 return true;
844}
845
Robert Phillipsb915c942019-12-17 14:44:37 -0500846bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
847 GrMipMapped mipMapped, const BackendTextureData* data) {
848
849 if (!data || data->type() == BackendTextureData::Type::kColor) {
850 return true;
851 }
852
853 if (data->type() == BackendTextureData::Type::kPixmaps) {
854 return false;
855 }
856
857 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
858
Robert Phillips99dead92020-01-27 16:11:57 -0500859 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
860 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500861
862 return computedSize == data->compressedSize();
863}
864
Brian Salomon85c3d682019-11-04 15:04:54 -0500865GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
866 const GrBackendFormat& format,
867 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500868 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400869 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400870 const GrCaps* caps = this->caps();
871
872 if (!format.isValid()) {
873 return {};
874 }
875
Robert Phillipsd34691b2019-09-24 13:38:43 -0400876 if (caps->isFormatCompressed(format)) {
877 // Compressed formats must go through the createCompressedBackendTexture API
878 return {};
879 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400880
Brian Salomon85c3d682019-11-04 15:04:54 -0500881 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
882 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400883 return {};
884 }
885
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500886 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400887 return {};
888 }
889
Greg Daniel16032b32020-05-06 15:31:10 -0400890 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
891}
892
893bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400894 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400895 const BackendTextureData* data) {
896 SkASSERT(data);
897 const GrCaps* caps = this->caps();
898
Greg Daniel16032b32020-05-06 15:31:10 -0400899 if (!backendTexture.isValid()) {
900 return false;
901 }
902
903 if (data->type() == BackendTextureData::Type::kPixmaps) {
904 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
905 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
906 return false;
907 }
908 }
909
910 if (backendTexture.hasMipMaps() && !this->caps()->mipMapSupport()) {
911 return false;
912 }
913
914 GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
915 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) {
916 return false;
917 }
918
Greg Daniel25597782020-06-11 13:15:08 -0400919 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400920}
Robert Phillipsb915c942019-12-17 14:44:37 -0500921
922GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
923 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500924 GrMipMapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400925 GrProtected isProtected) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500926 const GrCaps* caps = this->caps();
927
928 if (!format.isValid()) {
929 return {};
930 }
931
Greg Daniel01f278c2020-06-12 16:58:17 -0400932 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillipsb915c942019-12-17 14:44:37 -0500933 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
Greg Danielaaf738c2020-07-10 09:30:33 -0400948 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped, isProtected);
949}
950
951bool GrGpu::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
952 sk_sp<GrRefCntedCallback> finishedCallback,
953 const BackendTextureData* data) {
954 SkASSERT(data);
955
956 if (!backendTexture.isValid()) {
957 return false;
Robert Phillipsb915c942019-12-17 14:44:37 -0500958 }
959
Greg Danielaaf738c2020-07-10 09:30:33 -0400960 GrBackendFormat format = backendTexture.getBackendFormat();
961
962 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
963 if (compressionType == SkImage::CompressionType::kNone) {
964 // Uncompressed formats must go through the createBackendTexture API
965 return false;
966 }
967
968 if (backendTexture.hasMipMaps() && !this->caps()->mipMapSupport()) {
969 return false;
970 }
971
972 GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
973
974 if (!CompressedDataIsCorrect(backendTexture.dimensions(), compressionType, mipMapped, data)) {
975 return false;
976 }
977
978 return this->onUpdateCompressedBackendTexture(backendTexture, std::move(finishedCallback),
979 data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500980}
Stephen Whitef3d5d442020-04-08 10:35:58 -0400981
982GrStagingBuffer* GrGpu::findStagingBuffer(size_t size) {
983#ifdef SK_DEBUG
984 this->validateStagingBuffers();
985#endif
986 for (auto b : fActiveStagingBuffers) {
987 if (b->remaining() >= size) {
988 return b;
989 }
990 }
991 for (auto b : fAvailableStagingBuffers) {
992 if (b->remaining() >= size) {
993 fAvailableStagingBuffers.remove(b);
994 fActiveStagingBuffers.addToTail(b);
995 return b;
996 }
997 }
998 size = SkNextPow2(size);
999 size = std::max(size, kMinStagingBufferSize);
1000 std::unique_ptr<GrStagingBuffer> b = this->createStagingBuffer(size);
1001 GrStagingBuffer* stagingBuffer = b.get();
1002 fStagingBuffers.push_back(std::move(b));
1003 fActiveStagingBuffers.addToTail(stagingBuffer);
1004 return stagingBuffer;
1005}
1006
1007GrStagingBuffer::Slice GrGpu::allocateStagingBufferSlice(size_t size) {
1008#ifdef SK_DEBUG
1009 this->validateStagingBuffers();
1010#endif
1011 GrStagingBuffer* stagingBuffer = this->findStagingBuffer(size);
1012 return stagingBuffer->allocate(size);
1013}
1014
1015void GrGpu::unmapStagingBuffers() {
1016#ifdef SK_DEBUG
1017 this->validateStagingBuffers();
1018#endif
1019 // Unmap all active buffers.
1020 for (auto buffer : fActiveStagingBuffers) {
1021 buffer->unmap();
1022 }
1023}
1024
Stephen White7a026142020-05-13 17:16:33 -04001025void GrGpu::moveStagingBufferFromBusyToAvailable(GrStagingBuffer* buffer) {
Stephen Whitef3d5d442020-04-08 10:35:58 -04001026#ifdef SK_DEBUG
1027 this->validateStagingBuffers();
1028#endif
1029 fBusyStagingBuffers.remove(buffer);
1030 fAvailableStagingBuffers.addToTail(buffer);
1031}
Stephen White7a026142020-05-13 17:16:33 -04001032
1033void GrGpu::moveStagingBufferFromActiveToBusy(GrStagingBuffer* buffer) {
1034#ifdef SK_DEBUG
1035 this->validateStagingBuffers();
1036#endif
1037 fActiveStagingBuffers.remove(buffer);
1038 fBusyStagingBuffers.addToTail(buffer);
1039}