blob: dd7df47d1b5eea1db05af439c6510f62784295d3 [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 Danielc0d69152020-10-08 14:59:00 -040017#include "src/gpu/GrAttachment.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrAuditTrail.h"
Greg Daniel01f278c2020-06-12 16:58:17 -040019#include "src/gpu/GrBackendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrCaps.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040021#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040022#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060024#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrPathRendering.h"
26#include "src/gpu/GrPipeline.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040027#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrResourceCache.h"
29#include "src/gpu/GrResourceProvider.h"
Jim Van Verth7e829b22020-07-30 17:04:35 -040030#include "src/gpu/GrRingBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrSemaphore.h"
Greg Daniela58db7f2020-07-15 09:17:59 -040032#include "src/gpu/GrStagingBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrStencilSettings.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrTextureProxyPriv.h"
35#include "src/gpu/GrTracing.h"
36#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070037
bsalomon@google.comd302f142011-03-03 13:54:13 +000038////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000039
Adlai Holler3d0359a2020-07-09 15:35:55 -040040GrGpu::GrGpu(GrDirectContext* direct) : fResetBits(kAll_GrBackendState), fContext(direct) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Stephen Whitef3d5d442020-04-08 10:35:58 -040042GrGpu::~GrGpu() {
Greg Daniel55822f12020-05-26 11:26:45 -040043 this->callSubmittedProcs(false);
Stephen Whitef3d5d442020-04-08 10:35:58 -040044}
bsalomon1d89ddc2014-08-19 14:20:58 -070045
Greg Daniela58db7f2020-07-15 09:17:59 -040046void GrGpu::disconnect(DisconnectType type) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000047
bsalomon@google.comd302f142011-03-03 13:54:13 +000048////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000049
Brian Salomona56a7462020-02-07 14:17:25 -050050static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040051 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040052 SkASSERT(mipLevelCount > 0);
53 bool hasBasePixels = texels[0].fPixels;
54 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040055 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050056 int w = dimensions.fWidth;
57 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040058 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
59 if (texels[currentMipLevel].fPixels) {
60 const size_t minRowBytes = w * bpp;
61 if (caps->writePixelsRowBytesSupport()) {
62 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
63 return false;
64 }
65 if (texels[currentMipLevel].fRowBytes % bpp) {
66 return false;
67 }
68 } else {
69 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
70 return false;
71 }
72 }
73 ++levelsWithPixelsCnt;
74 }
75 if (w == 1 && h == 1) {
76 if (currentMipLevel != mipLevelCount - 1) {
77 return false;
78 }
79 } else {
80 w = std::max(w / 2, 1);
81 h = std::max(h / 2, 1);
82 }
83 }
84 // Either just a base layer or a full stack is required.
85 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
86 return false;
87 }
88 // Can specify just the base, all levels, or no levels.
89 if (!hasBasePixels) {
90 return levelsWithPixelsCnt == 0;
91 }
Brian Salomond2a8ae22019-09-10 16:03:59 -040092 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -040093}
94
Brian Salomona56a7462020-02-07 14:17:25 -050095sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -040096 const GrBackendFormat& format,
97 GrRenderable renderable,
98 int renderTargetSampleCnt,
99 SkBudgeted budgeted,
100 GrProtected isProtected,
101 int mipLevelCount,
102 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400103 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400104 // Call GrGpu::createCompressedTexture.
105 return nullptr;
106 }
cblume55f2d2d2016-02-26 13:20:48 -0800107
Brian Salomon7e67dca2020-07-21 09:27:25 -0400108 GrMipmapped mipMapped = mipLevelCount > 1 ? GrMipmapped::kYes : GrMipmapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500109 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
110 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700112 }
113
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400114 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400115 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400116 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500117 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400118 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400119 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400120 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500121 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400122 format,
123 renderable,
124 renderTargetSampleCnt,
125 budgeted,
126 isProtected,
127 mipLevelCount,
128 levelClearMask);
129 if (tex) {
130 SkASSERT(tex->backendFormat() == format);
131 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
132 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
133 tex->resourcePriv().removeScratchKey();
134 }
135 fStats.incTextureCreates();
136 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
137 SkASSERT(GrRenderable::kYes == renderable);
138 tex->asRenderTarget()->setRequiresManualMSAAResolve();
139 }
140 }
141 return tex;
142}
143
Brian Salomona56a7462020-02-07 14:17:25 -0500144sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400145 const GrBackendFormat& format,
146 GrRenderable renderable,
147 int renderTargetSampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400148 GrMipmapped mipMapped,
Brian Salomona90382f2019-09-17 09:01:56 -0400149 SkBudgeted budgeted,
150 GrProtected isProtected) {
151 int mipLevelCount = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400152 if (mipMapped == GrMipmapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500153 mipLevelCount =
154 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400155 }
156 uint32_t levelClearMask =
157 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500158 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
159 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400160 if (tex && mipMapped == GrMipmapped::kYes && levelClearMask) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400161 tex->markMipmapsClean();
Brian Salomona90382f2019-09-17 09:01:56 -0400162 }
163 return tex;
164}
165
Brian Salomona56a7462020-02-07 14:17:25 -0500166sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400167 const GrBackendFormat& format,
168 GrRenderable renderable,
169 int renderTargetSampleCnt,
170 SkBudgeted budgeted,
171 GrProtected isProtected,
172 GrColorType textureColorType,
173 GrColorType srcColorType,
174 const GrMipLevel texels[],
175 int texelLevelCount) {
176 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400177 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500178 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400179 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400180 return nullptr;
181 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400182 }
183
Brian Osman788b9162020-02-07 10:36:46 -0500184 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400185 uint32_t levelClearMask = 0;
186 if (this->caps()->shouldInitializeTextures()) {
187 if (texelLevelCount) {
188 for (int i = 0; i < mipLevelCount; ++i) {
189 if (!texels->fPixels) {
190 levelClearMask |= static_cast<uint32_t>(1 << i);
191 }
192 }
193 } else {
194 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
195 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400196 }
197
Brian Salomona56a7462020-02-07 14:17:25 -0500198 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
199 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800200 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400201 bool markMipLevelsClean = false;
202 // Currently if level 0 does not have pixels then no other level may, as enforced by
203 // validate_texel_levels.
204 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500205 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
206 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400207 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800208 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400209 // Currently if level[1] of mip map has pixel data then so must all other levels.
210 // as enforced by validate_texel_levels.
211 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
212 fStats.incTextureUploads();
213 } else if (levelClearMask && mipLevelCount > 1) {
214 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800215 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400216 if (markMipLevelsClean) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400217 tex->markMipmapsClean();
Brian Salomond2a8ae22019-09-10 16:03:59 -0400218 }
bsalomonb12ea412015-02-02 21:19:50 -0800219 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000220 return tex;
221}
222
Robert Phillips9f744f72019-12-19 19:14:33 -0500223sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400224 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500225 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400226 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500227 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500228 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400229 size_t dataSize) {
230 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500231 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
232 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400233 return nullptr;
234 }
Brian Salomona3e29962019-07-16 11:52:08 -0400235 // Note if we relax the requirement that data must be provided then we must check
236 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400237 if (!data) {
238 return nullptr;
239 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400240 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400241 return nullptr;
242 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500243
244 // TODO: expand CompressedDataIsCorrect to work here too
Greg Daniel01f278c2020-06-12 16:58:17 -0400245 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillips9f744f72019-12-19 19:14:33 -0500246
Robert Phillips99dead92020-01-27 16:11:57 -0500247 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400248 mipMapped == GrMipmapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400249 return nullptr;
250 }
Robert Phillips3a833922020-01-21 15:25:58 -0500251 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
252 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400253}
254
Greg Daniel7ef28f32017-04-20 16:41:55 +0000255sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400256 GrWrapOwnership ownership,
257 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500258 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500259 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000260 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400261
262 const GrCaps* caps = this->caps();
263 SkASSERT(caps);
264
Greg Daniel7bfc9132019-08-14 14:23:53 -0400265 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800266 return nullptr;
267 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400268 if (backendTex.width() > caps->maxTextureSize() ||
269 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800270 return nullptr;
271 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400272
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400273 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400274}
Eric Karl5c779752017-05-08 12:02:07 -0700275
Robert Phillipsb915c942019-12-17 14:44:37 -0500276sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
277 GrWrapOwnership ownership,
278 GrWrapCacheable cacheable) {
279 this->handleDirtyContext();
280
281 const GrCaps* caps = this->caps();
282 SkASSERT(caps);
283
284 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
285 return nullptr;
286 }
287 if (backendTex.width() > caps->maxTextureSize() ||
288 backendTex.height() > caps->maxTextureSize()) {
289 return nullptr;
290 }
291
292 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
293}
294
Brian Salomond17f6582017-07-19 18:28:58 -0400295sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400296 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400297 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500298 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400299 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500300 if (sampleCnt < 1) {
301 return nullptr;
302 }
Robert Phillips0902c982019-07-16 07:47:56 -0400303
304 const GrCaps* caps = this->caps();
305
Greg Daniel7bfc9132019-08-14 14:23:53 -0400306 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400307 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400308 return nullptr;
309 }
310
Robert Phillips0902c982019-07-16 07:47:56 -0400311 if (backendTex.width() > caps->maxRenderTargetSize() ||
312 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400313 return nullptr;
314 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400315 sk_sp<GrTexture> tex =
316 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400317 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600318 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
319 tex->asRenderTarget()->setRequiresManualMSAAResolve();
320 }
bungeman6bd52842016-10-27 09:30:08 -0700321 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000322}
323
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400324sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400325 this->handleDirtyContext();
326
327 const GrCaps* caps = this->caps();
328
Greg Daniel6fa62e22019-08-07 15:52:37 -0400329 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800330 return nullptr;
331 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400332
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400333 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500334 if (backendRT.isFramebufferOnly()) {
335 rt->setFramebufferOnly();
336 }
337 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000338}
339
Greg Danielb46add82019-01-02 14:51:29 -0500340sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
341 const GrVkDrawableInfo& vkInfo) {
342 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
343}
344
345sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
346 const GrVkDrawableInfo& vkInfo) {
347 // This is only supported on Vulkan so we default to returning nullptr here
348 return nullptr;
349}
350
Brian Salomondbf70722019-02-07 11:31:24 -0500351sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
352 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400353 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000354 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500355 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700356 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700357 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700358 }
cdalton397536c2016-03-25 12:15:03 -0700359 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800360}
361
Greg Daniel46cfbc62019-06-07 11:43:30 -0400362bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400363 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400364 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700365 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500366 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500367
368 if (dst->readOnly()) {
369 return false;
370 }
371
joshualitt1cbdcde2015-08-21 11:53:29 -0700372 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500373
Greg Daniele227fe42019-08-21 13:52:24 -0400374 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700375}
376
Brian Salomona6948702018-06-01 15:33:20 -0400377bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400378 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
379 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400380 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500381 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500382 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400383 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500384
Brian Salomon1d435302019-07-01 13:05:28 -0400385 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
386 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
387 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700388 return false;
389 }
390
Brian Salomon1047a492019-07-02 12:25:21 -0400391 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
392 if (!this->caps()->readPixelsRowBytesSupport()) {
393 if (rowBytes != minRowBytes) {
394 return false;
395 }
396 } else {
397 if (rowBytes < minRowBytes) {
398 return false;
399 }
400 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
401 return false;
402 }
403 }
404
Brian Salomonbf7b6202016-11-11 16:08:03 -0500405 this->handleDirtyContext();
406
Brian Salomonf77c1462019-08-01 15:19:29 -0400407 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
408 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000409}
410
Brian Salomona9b04b92018-06-01 15:04:28 -0400411bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400412 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400413 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400414 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Leon Scroggins III569f07c2020-07-24 16:10:58 -0400415 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Upload %ix%i Texture", width, height);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500416 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500417 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500418
419 if (surface->readOnly()) {
420 return false;
421 }
422
Brian Salomon1047a492019-07-02 12:25:21 -0400423 if (mipLevelCount == 0) {
424 return false;
425 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400426 // We require that if we are not mipped, then the write region is contained in the surface
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());
Greg Daniel660cc992017-06-26 14:55:05 -0400429 if (!bounds.contains(subRect)) {
430 return false;
431 }
432 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
433 // We require that if the texels are mipped, than the write region is the entire surface
434 return false;
435 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400436
Brian Salomona56a7462020-02-07 14:17:25 -0500437 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
438 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400439 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800440 }
jvanverth2dc29942015-09-01 07:16:46 -0700441
bsalomon@google.com6f379512011-11-16 20:36:03 +0000442 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400443 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400444 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700445 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400446 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800447 fStats.incTextureUploads();
448 return true;
449 }
450 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000451}
452
Brian Salomone05ba5a2019-04-08 11:59:07 -0400453bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400454 GrColorType textureColorType, GrColorType bufferColorType,
455 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400456 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500457 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700458 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800459
Brian Salomonc67c31c2018-12-06 10:00:03 -0500460 if (texture->readOnly()) {
461 return false;
462 }
463
Greg Daniel660cc992017-06-26 14:55:05 -0400464 // We require that the write region is contained in the texture
465 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
466 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
467 if (!bounds.contains(subRect)) {
468 return false;
469 }
470
Brian Salomonb28cb682019-07-26 12:48:47 -0400471 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400472 if (this->caps()->writePixelsRowBytesSupport()) {
473 if (rowBytes < SkToSizeT(bpp * width)) {
474 return false;
475 }
476 if (rowBytes % bpp) {
477 return false;
478 }
479 } else {
480 if (rowBytes != SkToSizeT(bpp * width)) {
481 return false;
482 }
483 }
484
jvanverth17aa0472016-01-05 10:41:27 -0800485 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400486 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
487 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700488 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500489 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800490 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700491
jvanverth17aa0472016-01-05 10:41:27 -0800492 return true;
493 }
494 return false;
495}
496
Brian Salomon26de56e2019-04-10 12:14:26 -0400497bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400498 GrColorType surfaceColorType, GrColorType bufferColorType,
499 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400500 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400501 SkASSERT(surface);
502 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400503 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400504
Greg Danielba88ab62019-07-26 09:14:01 -0400505#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400506 auto supportedRead = this->caps()->supportedReadPixelsColorType(
507 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400508 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
509 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
510#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400511
512 // We require that the write region is contained in the texture
513 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
514 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
515 if (!bounds.contains(subRect)) {
516 return false;
517 }
518
519 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400520 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
521 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400522 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400523 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400524 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400525 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400526}
527
Brian Salomon930f9392018-06-20 16:25:26 -0400528bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400529 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400530 SkASSERT(texture);
Brian Salomon69100f02020-07-21 10:49:25 -0400531 SkASSERT(this->caps()->mipmapSupport());
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400532 SkASSERT(texture->mipmapped() == GrMipmapped::kYes);
533 if (!texture->mipmapsAreDirty()) {
Chris Dalton16a33c62019-09-24 22:19:17 -0600534 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
535 // actual target. This may be caused by things that the drawingManager could not predict,
536 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
537 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
538 return true;
539 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500540 if (texture->readOnly()) {
541 return false;
542 }
Brian Salomon930f9392018-06-20 16:25:26 -0400543 if (this->onRegenerateMipMapLevels(texture)) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400544 texture->markMipmapsClean();
Brian Salomon930f9392018-06-20 16:25:26 -0400545 return true;
546 }
547 return false;
548}
549
Brian Salomon1f05d452019-02-08 12:33:08 -0500550void GrGpu::resetTextureBindings() {
551 this->handleDirtyContext();
552 this->onResetTextureBindings();
553}
554
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400555void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000556 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000557 this->handleDirtyContext();
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400558 this->onResolveRenderTarget(target, resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000559}
560
Brian Salomon1fabd512018-02-09 09:54:25 -0500561void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
562 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700563 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500564 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700565 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
566 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700567 GrTexture* texture = surface->asTexture();
568 if (texture && 1 == mipLevels) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400569 texture->markMipmapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700570 }
571 }
572}
573
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600574int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700575 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600576 SkASSERT(renderTarget->numSamples() > 1 ||
Brian Salomonf7f54332020-07-28 09:23:35 -0400577 (renderTarget->getStencilAttachment() &&
578 renderTarget->getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700579
580 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600581 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700582 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
583}
584
Greg Danielfe159622020-04-10 17:43:51 +0000585void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[],
586 int numProxies,
587 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400588 const GrFlushInfo& info,
589 const GrBackendSurfaceMutableState* newState) {
Brian Salomone39526b2019-06-24 16:35:53 -0400590 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000591
Robert Phillips9da87e02019-02-04 13:26:26 -0500592 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500593
Greg Daniel8561fc22020-04-08 12:52:51 -0400594 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
595 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500596 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400597 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400598 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400599 semaphores[i] = resourceProvider->wrapBackendSemaphore(
600 info.fSignalSemaphores[i],
601 GrResourceProvider::SemaphoreWrapType::kWillSignal,
602 kBorrow_GrWrapOwnership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400603 // If we failed to wrap the semaphore it means the client didn't give us a valid
604 // semaphore to begin with. Therefore, it is fine to not signal it.
605 if (semaphores[i]) {
606 this->insertSemaphore(semaphores[i].get());
607 }
Greg Daniel51316782017-08-02 15:10:09 +0000608 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400609 semaphores[i] = resourceProvider->makeSemaphore(false);
610 if (semaphores[i]) {
611 this->insertSemaphore(semaphores[i].get());
612 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
613 }
Greg Daniel51316782017-08-02 15:10:09 +0000614 }
615 }
616 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500617
Greg Danielfe159622020-04-10 17:43:51 +0000618 if (info.fFinishedProc) {
619 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
620 }
Greg Daniel55822f12020-05-26 11:26:45 -0400621
622 if (info.fSubmittedProc) {
623 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
624 }
625
Greg Daniel9efe3862020-06-11 11:51:06 -0400626 // We currently don't support passing in new surface state for multiple proxies here. The only
627 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
628 // updates anyways.
629 SkASSERT(!newState || numProxies == 1);
630 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
631 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, numProxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000632}
633
634bool GrGpu::submitToGpu(bool syncCpu) {
635 this->stats()->incNumSubmitToGpus();
636
Greg Daniela58db7f2020-07-15 09:17:59 -0400637 if (auto manager = this->stagingBufferManager()) {
638 manager->detachBuffers();
639 }
Stephen Whitef3d5d442020-04-08 10:35:58 -0400640
Jim Van Verth7e829b22020-07-30 17:04:35 -0400641 if (auto uniformsBuffer = this->uniformsRingBuffer()) {
642 uniformsBuffer->startSubmit(this);
643 }
644
Greg Danielfe159622020-04-10 17:43:51 +0000645 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400646
Greg Daniel55822f12020-05-26 11:26:45 -0400647 this->callSubmittedProcs(submitted);
648
Greg Danielfe159622020-04-10 17:43:51 +0000649 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000650}
Brian Osman71a18892017-08-10 10:23:25 -0400651
Brian Salomon24069eb2020-06-24 10:19:52 -0400652bool GrGpu::checkAndResetOOMed() {
653 if (fOOMed) {
654 fOOMed = false;
655 return true;
656 }
657 return false;
658}
659
Greg Daniel55822f12020-05-26 11:26:45 -0400660void GrGpu::callSubmittedProcs(bool success) {
661 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
662 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
663 }
664 fSubmittedProcs.reset();
665}
666
Kevin Lubickf4def342018-10-04 12:52:50 -0400667#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400668void GrGpu::dumpJSON(SkJSONWriter* writer) const {
669 writer->beginObject();
670
671 // TODO: Is there anything useful in the base class to dump here?
672
673 this->onDumpJSON(writer);
674
675 writer->endObject();
676}
Kevin Lubickf4def342018-10-04 12:52:50 -0400677#else
678void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
679#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400680
Robert Phillipsf0ced622019-05-16 09:06:25 -0400681#if GR_TEST_UTILS
682
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500683#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500684static const char* cache_result_to_str(int i) {
685 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
686 "hits",
687 "misses",
688 "partials"
689 };
690 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
691 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
692 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
693 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
694 return kCacheResultStrings[i];
695}
696
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500697void GrGpu::Stats::dump(SkString* out) {
698 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
699 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
700 out->appendf("Textures Created: %d\n", fTextureCreates);
701 out->appendf("Texture Uploads: %d\n", fTextureUploads);
702 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400703 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500704 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
Greg Daniel5d0330e2020-10-12 16:05:21 -0400705 out->appendf("MSAA Attachment Creates: %d\n", fMSAAAttachmentCreates);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500706 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400707 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Greg Daniel5d0330e2020-10-12 16:05:21 -0400708 out->appendf("Number of Scratch MSAA Attachments reused %d\n",
709 fNumScratchMSAAAttachmentsReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500710
711 SkASSERT(fNumInlineCompilationFailures == 0);
712 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
713 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
714 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
715 fInlineProgramCacheStats[i]);
716 }
717
718 SkASSERT(fNumPreCompilationFailures == 0);
719 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
720 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
721 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
722 fPreProgramCacheStats[i]);
723 }
724
725 SkASSERT(fNumCompilationFailures == 0);
726 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
727 out->appendf("Total number of partial compilation successes %d\n",
728 fNumPartialCompilationSuccesses);
729 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500730
731 // enable this block to output CSV-style stats for program pre-compilation
732#if 0
733 SkASSERT(fNumInlineCompilationFailures == 0);
734 SkASSERT(fNumPreCompilationFailures == 0);
735 SkASSERT(fNumCompilationFailures == 0);
736 SkASSERT(fNumPartialCompilationSuccesses == 0);
737
738 SkDebugf("%d, %d, %d, %d, %d\n",
739 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
740 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
741 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
742 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
743 fNumCompilationSuccesses);
744#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500745}
746
747void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
748 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
749 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500750}
751
Robert Phillips57ef6802019-09-23 10:12:47 -0400752#endif // GR_GPU_STATS
753#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500754
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500755bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400756 GrMipmapped mipMapped,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500757 const BackendTextureData* data) {
758 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400759 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400760 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500761 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400762
Robert Phillips42716d42019-12-16 12:19:54 -0500763 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400764 return true;
765 }
766
Robert Phillips42716d42019-12-16 12:19:54 -0500767 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500768 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500769 }
770
771 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
772
Brian Salomon85c3d682019-11-04 15:04:54 -0500773 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400774 return false;
775 }
776
Brian Salomon85c3d682019-11-04 15:04:54 -0500777 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500778 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500779 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500780 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400781 return false;
782 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500783 if (colorType != data->pixmap(i).colorType()) {
784 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400785 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400786 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400787 return true;
788}
789
Robert Phillipsb915c942019-12-17 14:44:37 -0500790bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400791 GrMipmapped mipMapped, const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500792
793 if (!data || data->type() == BackendTextureData::Type::kColor) {
794 return true;
795 }
796
797 if (data->type() == BackendTextureData::Type::kPixmaps) {
798 return false;
799 }
800
801 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
802
Robert Phillips99dead92020-01-27 16:11:57 -0500803 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400804 nullptr, mipMapped == GrMipmapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500805
806 return computedSize == data->compressedSize();
807}
808
Brian Salomon85c3d682019-11-04 15:04:54 -0500809GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
810 const GrBackendFormat& format,
811 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400812 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400813 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400814 const GrCaps* caps = this->caps();
815
816 if (!format.isValid()) {
817 return {};
818 }
819
Robert Phillipsd34691b2019-09-24 13:38:43 -0400820 if (caps->isFormatCompressed(format)) {
821 // Compressed formats must go through the createCompressedBackendTexture API
822 return {};
823 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400824
Brian Salomon85c3d682019-11-04 15:04:54 -0500825 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
826 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400827 return {};
828 }
829
Brian Salomon69100f02020-07-21 10:49:25 -0400830 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400831 return {};
832 }
833
Greg Daniel16032b32020-05-06 15:31:10 -0400834 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
835}
836
837bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400838 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400839 const BackendTextureData* data) {
840 SkASSERT(data);
841 const GrCaps* caps = this->caps();
842
Greg Daniel16032b32020-05-06 15:31:10 -0400843 if (!backendTexture.isValid()) {
844 return false;
845 }
846
847 if (data->type() == BackendTextureData::Type::kPixmaps) {
848 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
849 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
850 return false;
851 }
852 }
853
Brian Salomon69100f02020-07-21 10:49:25 -0400854 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Daniel16032b32020-05-06 15:31:10 -0400855 return false;
856 }
857
Brian Salomon40a40622020-07-21 10:32:07 -0400858 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Daniel16032b32020-05-06 15:31:10 -0400859 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) {
860 return false;
861 }
862
Greg Daniel25597782020-06-11 13:15:08 -0400863 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400864}
Robert Phillipsb915c942019-12-17 14:44:37 -0500865
866GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
867 const GrBackendFormat& format,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400868 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400869 GrProtected isProtected) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500870 const GrCaps* caps = this->caps();
871
872 if (!format.isValid()) {
873 return {};
874 }
875
Greg Daniel01f278c2020-06-12 16:58:17 -0400876 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillipsb915c942019-12-17 14:44:37 -0500877 if (compressionType == SkImage::CompressionType::kNone) {
878 // Uncompressed formats must go through the createBackendTexture API
879 return {};
880 }
881
882 if (dimensions.isEmpty() ||
883 dimensions.width() > caps->maxTextureSize() ||
884 dimensions.height() > caps->maxTextureSize()) {
885 return {};
886 }
887
Brian Salomon69100f02020-07-21 10:49:25 -0400888 if (mipMapped == GrMipmapped::kYes && !this->caps()->mipmapSupport()) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500889 return {};
890 }
891
Greg Danielaaf738c2020-07-10 09:30:33 -0400892 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped, isProtected);
893}
894
895bool GrGpu::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
896 sk_sp<GrRefCntedCallback> finishedCallback,
897 const BackendTextureData* data) {
898 SkASSERT(data);
899
900 if (!backendTexture.isValid()) {
901 return false;
Robert Phillipsb915c942019-12-17 14:44:37 -0500902 }
903
Greg Danielaaf738c2020-07-10 09:30:33 -0400904 GrBackendFormat format = backendTexture.getBackendFormat();
905
906 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
907 if (compressionType == SkImage::CompressionType::kNone) {
908 // Uncompressed formats must go through the createBackendTexture API
909 return false;
910 }
911
Brian Salomon69100f02020-07-21 10:49:25 -0400912 if (backendTexture.hasMipmaps() && !this->caps()->mipmapSupport()) {
Greg Danielaaf738c2020-07-10 09:30:33 -0400913 return false;
914 }
915
Brian Salomon40a40622020-07-21 10:32:07 -0400916 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -0400917
918 if (!CompressedDataIsCorrect(backendTexture.dimensions(), compressionType, mipMapped, data)) {
919 return false;
920 }
921
922 return this->onUpdateCompressedBackendTexture(backendTexture, std::move(finishedCallback),
923 data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500924}