blob: 6b8e47edb90672e4b54234da284e3da855cd9d5d [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrContext.h"
Robert Phillips99dead92020-01-27 16:11:57 -050014#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkMathPriv.h"
Robert Phillips57ef6802019-09-23 10:12:47 -040016#include "src/core/SkMipMap.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrCaps.h"
19#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040020#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060022#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrPathRendering.h"
24#include "src/gpu/GrPipeline.h"
25#include "src/gpu/GrRenderTargetPriv.h"
26#include "src/gpu/GrResourceCache.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrSemaphore.h"
29#include "src/gpu/GrStencilAttachment.h"
30#include "src/gpu/GrStencilSettings.h"
31#include "src/gpu/GrSurfacePriv.h"
32#include "src/gpu/GrTexturePriv.h"
33#include "src/gpu/GrTextureProxyPriv.h"
34#include "src/gpu/GrTracing.h"
35#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070036
bsalomon@google.comd302f142011-03-03 13:54:13 +000037////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000038
Brian Salomone2826ab2019-06-04 15:58:31 -040039GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomoned0bcad2015-05-04 10:36:42 -070041GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070042
bsalomon6e2aad42016-04-01 11:54:31 -070043void GrGpu::disconnect(DisconnectType) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000044
bsalomon@google.comd302f142011-03-03 13:54:13 +000045////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000046
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000047bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
Brian Salomonc8d092a2020-02-24 10:14:21 -050048 GrSamplerState::Filter filter) {
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000049 SkASSERT(texProxy);
Brian Salomonc8d092a2020-02-24 10:14:21 -050050 if (filter != GrSamplerState::Filter::kMipMap || texProxy->mipMapped() == GrMipMapped::kYes ||
51 !caps->mipMapSupport()) {
52 return false;
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000053 }
Brian Salomonc8d092a2020-02-24 10:14:21 -050054 return SkMipMap::ComputeLevelCount(texProxy->width(), texProxy->height()) > 0;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040055}
56
Brian Salomona56a7462020-02-07 14:17:25 -050057static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040058 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040059 SkASSERT(mipLevelCount > 0);
60 bool hasBasePixels = texels[0].fPixels;
61 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040062 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050063 int w = dimensions.fWidth;
64 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040065 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
66 if (texels[currentMipLevel].fPixels) {
67 const size_t minRowBytes = w * bpp;
68 if (caps->writePixelsRowBytesSupport()) {
69 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
70 return false;
71 }
72 if (texels[currentMipLevel].fRowBytes % bpp) {
73 return false;
74 }
75 } else {
76 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
77 return false;
78 }
79 }
80 ++levelsWithPixelsCnt;
81 }
82 if (w == 1 && h == 1) {
83 if (currentMipLevel != mipLevelCount - 1) {
84 return false;
85 }
86 } else {
87 w = std::max(w / 2, 1);
88 h = std::max(h / 2, 1);
89 }
90 }
91 // Either just a base layer or a full stack is required.
92 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
93 return false;
94 }
95 // Can specify just the base, all levels, or no levels.
96 if (!hasBasePixels) {
97 return levelsWithPixelsCnt == 0;
98 }
Brian Salomond2a8ae22019-09-10 16:03:59 -040099 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400100}
101
Brian Salomona56a7462020-02-07 14:17:25 -0500102sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400103 const GrBackendFormat& format,
104 GrRenderable renderable,
105 int renderTargetSampleCnt,
106 SkBudgeted budgeted,
107 GrProtected isProtected,
108 int mipLevelCount,
109 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400110 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400111 // Call GrGpu::createCompressedTexture.
112 return nullptr;
113 }
cblume55f2d2d2016-02-26 13:20:48 -0800114
Brian Salomona90382f2019-09-17 09:01:56 -0400115 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500116 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
117 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700118 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700119 }
120
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400121 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400122 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400123 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500124 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400125 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400126 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400127 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500128 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400129 format,
130 renderable,
131 renderTargetSampleCnt,
132 budgeted,
133 isProtected,
134 mipLevelCount,
135 levelClearMask);
136 if (tex) {
137 SkASSERT(tex->backendFormat() == format);
138 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
139 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
140 tex->resourcePriv().removeScratchKey();
141 }
142 fStats.incTextureCreates();
143 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
144 SkASSERT(GrRenderable::kYes == renderable);
145 tex->asRenderTarget()->setRequiresManualMSAAResolve();
146 }
147 }
148 return tex;
149}
150
Brian Salomona56a7462020-02-07 14:17:25 -0500151sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400152 const GrBackendFormat& format,
153 GrRenderable renderable,
154 int renderTargetSampleCnt,
155 GrMipMapped mipMapped,
156 SkBudgeted budgeted,
157 GrProtected isProtected) {
158 int mipLevelCount = 1;
159 if (mipMapped == GrMipMapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500160 mipLevelCount =
161 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400162 }
163 uint32_t levelClearMask =
164 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500165 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
166 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomona90382f2019-09-17 09:01:56 -0400167 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
168 tex->texturePriv().markMipMapsClean();
169 }
170 return tex;
171}
172
Brian Salomona56a7462020-02-07 14:17:25 -0500173sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400174 const GrBackendFormat& format,
175 GrRenderable renderable,
176 int renderTargetSampleCnt,
177 SkBudgeted budgeted,
178 GrProtected isProtected,
179 GrColorType textureColorType,
180 GrColorType srcColorType,
181 const GrMipLevel texels[],
182 int texelLevelCount) {
183 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400184 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500185 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400186 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400187 return nullptr;
188 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400189 }
190
Brian Osman788b9162020-02-07 10:36:46 -0500191 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400192 uint32_t levelClearMask = 0;
193 if (this->caps()->shouldInitializeTextures()) {
194 if (texelLevelCount) {
195 for (int i = 0; i < mipLevelCount; ++i) {
196 if (!texels->fPixels) {
197 levelClearMask |= static_cast<uint32_t>(1 << i);
198 }
199 }
200 } else {
201 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
202 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400203 }
204
Brian Salomona56a7462020-02-07 14:17:25 -0500205 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
206 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800207 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400208 bool markMipLevelsClean = false;
209 // Currently if level 0 does not have pixels then no other level may, as enforced by
210 // validate_texel_levels.
211 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500212 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
213 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400214 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800215 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400216 // Currently if level[1] of mip map has pixel data then so must all other levels.
217 // as enforced by validate_texel_levels.
218 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
219 fStats.incTextureUploads();
220 } else if (levelClearMask && mipLevelCount > 1) {
221 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800222 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400223 if (markMipLevelsClean) {
224 tex->texturePriv().markMipMapsClean();
225 }
bsalomonb12ea412015-02-02 21:19:50 -0800226 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000227 return tex;
228}
229
Robert Phillips9f744f72019-12-19 19:14:33 -0500230sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400231 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500232 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500233 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500234 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500235 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400236 size_t dataSize) {
237 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500238 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
239 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400240 return nullptr;
241 }
Brian Salomona3e29962019-07-16 11:52:08 -0400242 // Note if we relax the requirement that data must be provided then we must check
243 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400244 if (!data) {
245 return nullptr;
246 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400247 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400248 return nullptr;
249 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500250
251 // TODO: expand CompressedDataIsCorrect to work here too
252 SkImage::CompressionType compressionType = this->caps()->compressionType(format);
253
Robert Phillips99dead92020-01-27 16:11:57 -0500254 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
255 mipMapped == GrMipMapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400256 return nullptr;
257 }
Robert Phillips3a833922020-01-21 15:25:58 -0500258 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
259 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400260}
261
Greg Daniel7ef28f32017-04-20 16:41:55 +0000262sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400263 GrWrapOwnership ownership,
264 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500265 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500266 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000267 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400268
269 const GrCaps* caps = this->caps();
270 SkASSERT(caps);
271
Greg Daniel7bfc9132019-08-14 14:23:53 -0400272 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800273 return nullptr;
274 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400275 if (backendTex.width() > caps->maxTextureSize() ||
276 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800277 return nullptr;
278 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400279
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400280 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400281}
Eric Karl5c779752017-05-08 12:02:07 -0700282
Robert Phillipsb915c942019-12-17 14:44:37 -0500283sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
284 GrWrapOwnership ownership,
285 GrWrapCacheable cacheable) {
286 this->handleDirtyContext();
287
288 const GrCaps* caps = this->caps();
289 SkASSERT(caps);
290
291 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
292 return nullptr;
293 }
294 if (backendTex.width() > caps->maxTextureSize() ||
295 backendTex.height() > caps->maxTextureSize()) {
296 return nullptr;
297 }
298
299 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
300}
301
Brian Salomond17f6582017-07-19 18:28:58 -0400302sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400303 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400304 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500305 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400306 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500307 if (sampleCnt < 1) {
308 return nullptr;
309 }
Robert Phillips0902c982019-07-16 07:47:56 -0400310
311 const GrCaps* caps = this->caps();
312
Greg Daniel7bfc9132019-08-14 14:23:53 -0400313 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400314 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400315 return nullptr;
316 }
317
Robert Phillips0902c982019-07-16 07:47:56 -0400318 if (backendTex.width() > caps->maxRenderTargetSize() ||
319 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400320 return nullptr;
321 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400322 sk_sp<GrTexture> tex =
323 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400324 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600325 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
326 tex->asRenderTarget()->setRequiresManualMSAAResolve();
327 }
bungeman6bd52842016-10-27 09:30:08 -0700328 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000329}
330
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400331sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400332 this->handleDirtyContext();
333
334 const GrCaps* caps = this->caps();
335
Greg Daniel6fa62e22019-08-07 15:52:37 -0400336 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800337 return nullptr;
338 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400339
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400340 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500341 if (backendRT.isFramebufferOnly()) {
342 rt->setFramebufferOnly();
343 }
344 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000345}
346
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400347sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400348 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500349 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400350
351 const GrCaps* caps = this->caps();
352
353 int maxSize = caps->maxTextureSize();
354 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
355 return nullptr;
356 }
357
Greg Daniel6fa62e22019-08-07 15:52:37 -0400358 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400359 return nullptr;
360 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400361
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400362 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Chris Dalton3f7932e2019-08-19 00:39:13 -0600363 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
364 rt->setRequiresManualMSAAResolve();
365 }
366 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800367}
368
Greg Danielb46add82019-01-02 14:51:29 -0500369sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
370 const GrVkDrawableInfo& vkInfo) {
371 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
372}
373
374sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
375 const GrVkDrawableInfo& vkInfo) {
376 // This is only supported on Vulkan so we default to returning nullptr here
377 return nullptr;
378}
379
Brian Salomondbf70722019-02-07 11:31:24 -0500380sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
381 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400382 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000383 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500384 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700385 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700386 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700387 }
cdalton397536c2016-03-25 12:15:03 -0700388 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800389}
390
Greg Daniel46cfbc62019-06-07 11:43:30 -0400391bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400392 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400393 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700394 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500395 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500396
397 if (dst->readOnly()) {
398 return false;
399 }
400
joshualitt1cbdcde2015-08-21 11:53:29 -0700401 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500402
Greg Daniele227fe42019-08-21 13:52:24 -0400403 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700404}
405
Brian Salomona6948702018-06-01 15:33:20 -0400406bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400407 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
408 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400409 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500410 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500411 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400412 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500413
Brian Salomon1d435302019-07-01 13:05:28 -0400414 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
415 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
416 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700417 return false;
418 }
419
Brian Salomon1047a492019-07-02 12:25:21 -0400420 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
421 if (!this->caps()->readPixelsRowBytesSupport()) {
422 if (rowBytes != minRowBytes) {
423 return false;
424 }
425 } else {
426 if (rowBytes < minRowBytes) {
427 return false;
428 }
429 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
430 return false;
431 }
432 }
433
Brian Salomonbf7b6202016-11-11 16:08:03 -0500434 this->handleDirtyContext();
435
Brian Salomonf77c1462019-08-01 15:19:29 -0400436 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
437 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000438}
439
Brian Salomona9b04b92018-06-01 15:04:28 -0400440bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400441 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400442 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400443 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500444 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500445 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500446
447 if (surface->readOnly()) {
448 return false;
449 }
450
Brian Salomon1047a492019-07-02 12:25:21 -0400451 if (mipLevelCount == 0) {
452 return false;
453 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400454 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400455 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
456 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400457 if (!bounds.contains(subRect)) {
458 return false;
459 }
460 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
461 // We require that if the texels are mipped, than the write region is the entire surface
462 return false;
463 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400464
Brian Salomona56a7462020-02-07 14:17:25 -0500465 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
466 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400467 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800468 }
jvanverth2dc29942015-09-01 07:16:46 -0700469
bsalomon@google.com6f379512011-11-16 20:36:03 +0000470 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400471 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400472 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700473 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400474 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800475 fStats.incTextureUploads();
476 return true;
477 }
478 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000479}
480
Brian Salomone05ba5a2019-04-08 11:59:07 -0400481bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400482 GrColorType textureColorType, GrColorType bufferColorType,
483 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400484 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500485 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700486 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800487
Brian Salomonc67c31c2018-12-06 10:00:03 -0500488 if (texture->readOnly()) {
489 return false;
490 }
491
Greg Daniel660cc992017-06-26 14:55:05 -0400492 // We require that the write region is contained in the texture
493 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
494 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
495 if (!bounds.contains(subRect)) {
496 return false;
497 }
498
Brian Salomonb28cb682019-07-26 12:48:47 -0400499 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400500 if (this->caps()->writePixelsRowBytesSupport()) {
501 if (rowBytes < SkToSizeT(bpp * width)) {
502 return false;
503 }
504 if (rowBytes % bpp) {
505 return false;
506 }
507 } else {
508 if (rowBytes != SkToSizeT(bpp * width)) {
509 return false;
510 }
511 }
512
jvanverth17aa0472016-01-05 10:41:27 -0800513 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400514 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
515 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700516 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500517 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800518 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700519
jvanverth17aa0472016-01-05 10:41:27 -0800520 return true;
521 }
522 return false;
523}
524
Brian Salomon26de56e2019-04-10 12:14:26 -0400525bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400526 GrColorType surfaceColorType, GrColorType bufferColorType,
527 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400528 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400529 SkASSERT(surface);
530 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400531 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400532
Greg Danielba88ab62019-07-26 09:14:01 -0400533#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400534 auto supportedRead = this->caps()->supportedReadPixelsColorType(
535 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400536 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
537 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
538#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400539
540 // We require that the write region is contained in the texture
541 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
542 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
543 if (!bounds.contains(subRect)) {
544 return false;
545 }
546
547 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400548 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
549 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400550 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400551 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400552 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400553 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400554}
555
Brian Salomon930f9392018-06-20 16:25:26 -0400556bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400557 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400558 SkASSERT(texture);
559 SkASSERT(this->caps()->mipMapSupport());
560 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600561 if (!texture->texturePriv().mipMapsAreDirty()) {
562 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
563 // actual target. This may be caused by things that the drawingManager could not predict,
564 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
565 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
566 return true;
567 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500568 if (texture->readOnly()) {
569 return false;
570 }
Brian Salomon930f9392018-06-20 16:25:26 -0400571 if (this->onRegenerateMipMapLevels(texture)) {
572 texture->texturePriv().markMipMapsClean();
573 return true;
574 }
575 return false;
576}
577
Brian Salomon1f05d452019-02-08 12:33:08 -0500578void GrGpu::resetTextureBindings() {
579 this->handleDirtyContext();
580 this->onResetTextureBindings();
581}
582
Chris Dalton16a33c62019-09-24 22:19:17 -0600583void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -0500584 ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000585 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000586 this->handleDirtyContext();
Greg Daniel242536f2020-02-13 14:12:46 -0500587 this->onResolveRenderTarget(target, resolveRect, forExternalIO);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000588}
589
Brian Salomon1fabd512018-02-09 09:54:25 -0500590void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
591 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700592 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500593 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700594 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
595 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700596 GrTexture* texture = surface->asTexture();
597 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400598 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700599 }
600 }
601}
602
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600603int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700604 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600605 SkASSERT(renderTarget->numSamples() > 1 ||
606 (renderTarget->renderTargetPriv().getStencilAttachment() &&
607 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700608
609 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600610 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700611 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
612}
613
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400614GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
615 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500616 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400617 const GrFlushInfo& info,
618 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400619 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500620 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500621 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500622
Greg Daniel30a35e82019-11-19 14:12:25 -0500623 struct SemaphoreInfo {
624 std::unique_ptr<GrSemaphore> fSemaphore;
625 bool fDidCreate = false;
626 };
627
628 bool failedSemaphoreCreation = false;
629 std::unique_ptr<SemaphoreInfo[]> semaphoreInfos(new SemaphoreInfo[info.fNumSemaphores]);
630 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
631 for (int i = 0; i < info.fNumSemaphores && !failedSemaphoreCreation; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400632 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel30a35e82019-11-19 14:12:25 -0500633 semaphoreInfos[i].fSemaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400634 info.fSignalSemaphores[i],
635 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500636 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000637 } else {
Greg Daniel30a35e82019-11-19 14:12:25 -0500638 semaphoreInfos[i].fSemaphore = resourceProvider->makeSemaphore(false);
639 semaphoreInfos[i].fDidCreate = true;
Greg Daniel51316782017-08-02 15:10:09 +0000640 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500641 if (!semaphoreInfos[i].fSemaphore) {
642 semaphoreInfos[i].fDidCreate = false;
643 failedSemaphoreCreation = true;
644 }
645 }
646 if (!failedSemaphoreCreation) {
647 for (int i = 0; i < info.fNumSemaphores && !failedSemaphoreCreation; ++i) {
648 this->insertSemaphore(semaphoreInfos[i].fSemaphore.get());
Greg Daniel51316782017-08-02 15:10:09 +0000649 }
650 }
651 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500652
653 // We always want to try flushing, so do that before checking if we failed semaphore creation.
654 if (!this->onFinishFlush(proxies, n, access, info, externalRequests) ||
655 failedSemaphoreCreation) {
656 // If we didn't do the flush or failed semaphore creations then none of the semaphores were
657 // submitted. Therefore the client can't wait on any of the semaphores. Additionally any
658 // semaphores we created here the client is not responsible for deleting so we must make
659 // sure they get deleted. We do this by changing the ownership from borrowed to owned.
660 for (int i = 0; i < info.fNumSemaphores; ++i) {
661 if (semaphoreInfos[i].fDidCreate) {
662 SkASSERT(semaphoreInfos[i].fSemaphore);
663 semaphoreInfos[i].fSemaphore->setIsOwned();
664 }
665 }
666 return GrSemaphoresSubmitted::kNo;
667 }
668
669 for (int i = 0; i < info.fNumSemaphores; ++i) {
670 if (!info.fSignalSemaphores[i].isInitialized()) {
671 SkASSERT(semaphoreInfos[i].fSemaphore);
672 info.fSignalSemaphores[i] = semaphoreInfos[i].fSemaphore->backendSemaphore();
673 }
674 }
675
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400676 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000677 : GrSemaphoresSubmitted::kNo;
678}
Brian Osman71a18892017-08-10 10:23:25 -0400679
Kevin Lubickf4def342018-10-04 12:52:50 -0400680#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400681void GrGpu::dumpJSON(SkJSONWriter* writer) const {
682 writer->beginObject();
683
684 // TODO: Is there anything useful in the base class to dump here?
685
686 this->onDumpJSON(writer);
687
688 writer->endObject();
689}
Kevin Lubickf4def342018-10-04 12:52:50 -0400690#else
691void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
692#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400693
Robert Phillipsf0ced622019-05-16 09:06:25 -0400694#if GR_TEST_UTILS
695
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500696#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500697static const char* cache_result_to_str(int i) {
698 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
699 "hits",
700 "misses",
701 "partials"
702 };
703 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
704 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
705 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
706 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
707 return kCacheResultStrings[i];
708}
709
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500710void GrGpu::Stats::dump(SkString* out) {
711 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
712 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
713 out->appendf("Textures Created: %d\n", fTextureCreates);
714 out->appendf("Texture Uploads: %d\n", fTextureUploads);
715 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400716 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500717 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
718 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400719 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500720
721 SkASSERT(fNumInlineCompilationFailures == 0);
722 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
723 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
724 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
725 fInlineProgramCacheStats[i]);
726 }
727
728 SkASSERT(fNumPreCompilationFailures == 0);
729 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
730 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
731 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
732 fPreProgramCacheStats[i]);
733 }
734
735 SkASSERT(fNumCompilationFailures == 0);
736 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
737 out->appendf("Total number of partial compilation successes %d\n",
738 fNumPartialCompilationSuccesses);
739 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500740
741 // enable this block to output CSV-style stats for program pre-compilation
742#if 0
743 SkASSERT(fNumInlineCompilationFailures == 0);
744 SkASSERT(fNumPreCompilationFailures == 0);
745 SkASSERT(fNumCompilationFailures == 0);
746 SkASSERT(fNumPartialCompilationSuccesses == 0);
747
748 SkDebugf("%d, %d, %d, %d, %d\n",
749 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
750 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
751 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
752 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
753 fNumCompilationSuccesses);
754#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500755}
756
757void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
758 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
759 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500760}
761
Robert Phillips57ef6802019-09-23 10:12:47 -0400762#endif // GR_GPU_STATS
763#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500764
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500765bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
766 GrMipMapped mipMapped,
767 const BackendTextureData* data) {
768 int numMipLevels = 1;
769 if (mipMapped == GrMipMapped::kYes) {
770 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500771 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400772
Robert Phillips42716d42019-12-16 12:19:54 -0500773 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400774 return true;
775 }
776
Robert Phillips42716d42019-12-16 12:19:54 -0500777 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500778 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500779 }
780
781 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
782
Brian Salomon85c3d682019-11-04 15:04:54 -0500783 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400784 return false;
785 }
786
Brian Salomon85c3d682019-11-04 15:04:54 -0500787 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500788 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500789 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500790 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400791 return false;
792 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500793 if (colorType != data->pixmap(i).colorType()) {
794 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400795 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400796 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400797 return true;
798}
799
Robert Phillipsb915c942019-12-17 14:44:37 -0500800bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
801 GrMipMapped mipMapped, const BackendTextureData* data) {
802
803 if (!data || data->type() == BackendTextureData::Type::kColor) {
804 return true;
805 }
806
807 if (data->type() == BackendTextureData::Type::kPixmaps) {
808 return false;
809 }
810
811 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
812
Robert Phillips99dead92020-01-27 16:11:57 -0500813 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
814 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500815
816 return computedSize == data->compressedSize();
817}
818
Brian Salomon85c3d682019-11-04 15:04:54 -0500819GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
820 const GrBackendFormat& format,
821 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500822 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500823 GrProtected isProtected,
824 const BackendTextureData* data) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400825 const GrCaps* caps = this->caps();
826
827 if (!format.isValid()) {
828 return {};
829 }
830
Robert Phillipsd34691b2019-09-24 13:38:43 -0400831 if (caps->isFormatCompressed(format)) {
832 // Compressed formats must go through the createCompressedBackendTexture API
833 return {};
834 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400835
Brian Salomon85c3d682019-11-04 15:04:54 -0500836 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
837 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
838 if (!caps->areColorTypeAndFormatCompatible(ct, format)) {
839 return {};
840 }
841 }
842
843 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
844 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400845 return {};
846 }
847
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500848 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400849 return {};
850 }
851
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500852 if (!MipMapsAreCorrect(dimensions, mipMapped, data)) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400853 return {};
854 }
855
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500856 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500857 isProtected, data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400858}
Robert Phillipsb915c942019-12-17 14:44:37 -0500859
860GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
861 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500862 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500863 GrProtected isProtected,
864 const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500865 const GrCaps* caps = this->caps();
866
867 if (!format.isValid()) {
868 return {};
869 }
870
871 SkImage::CompressionType compressionType = caps->compressionType(format);
872 if (compressionType == SkImage::CompressionType::kNone) {
873 // Uncompressed formats must go through the createBackendTexture API
874 return {};
875 }
876
877 if (dimensions.isEmpty() ||
878 dimensions.width() > caps->maxTextureSize() ||
879 dimensions.height() > caps->maxTextureSize()) {
880 return {};
881 }
882
883 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
884 return {};
885 }
886
887 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
888 return {};
889 }
890
Robert Phillips4277f012020-01-21 14:28:34 -0500891 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
892 isProtected, data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500893}