blob: c862d2702428f8fb035292022b3d954d6ff4915c [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
Adlai Holler3d0359a2020-07-09 15:35:55 -040013#include "include/gpu/GrDirectContext.h"
Robert Phillips99dead92020-01-27 16:11:57 -050014#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkMathPriv.h"
Mike Reed13711eb2020-07-14 17:16:32 -040016#include "src/core/SkMipmap.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrAuditTrail.h"
Greg Daniel01f278c2020-06-12 16:58:17 -040018#include "src/gpu/GrBackendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrCaps.h"
20#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040021#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060023#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrPathRendering.h"
25#include "src/gpu/GrPipeline.h"
26#include "src/gpu/GrRenderTargetPriv.h"
27#include "src/gpu/GrResourceCache.h"
28#include "src/gpu/GrResourceProvider.h"
29#include "src/gpu/GrSemaphore.h"
Greg Daniela58db7f2020-07-15 09:17:59 -040030#include "src/gpu/GrStagingBufferManager.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrStencilAttachment.h"
32#include "src/gpu/GrStencilSettings.h"
33#include "src/gpu/GrSurfacePriv.h"
34#include "src/gpu/GrTexturePriv.h"
35#include "src/gpu/GrTextureProxyPriv.h"
36#include "src/gpu/GrTracing.h"
37#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070038
bsalomon@google.comd302f142011-03-03 13:54:13 +000039////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000040
Adlai Holler3d0359a2020-07-09 15:35:55 -040041GrGpu::GrGpu(GrDirectContext* direct) : fResetBits(kAll_GrBackendState), fContext(direct) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000042
Stephen Whitef3d5d442020-04-08 10:35:58 -040043GrGpu::~GrGpu() {
Greg Daniel55822f12020-05-26 11:26:45 -040044 this->callSubmittedProcs(false);
Stephen Whitef3d5d442020-04-08 10:35:58 -040045}
bsalomon1d89ddc2014-08-19 14:20:58 -070046
Greg Daniela58db7f2020-07-15 09:17:59 -040047void GrGpu::disconnect(DisconnectType type) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000048
bsalomon@google.comd302f142011-03-03 13:54:13 +000049////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000050
Brian Salomona56a7462020-02-07 14:17:25 -050051static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040052 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040053 SkASSERT(mipLevelCount > 0);
54 bool hasBasePixels = texels[0].fPixels;
55 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040056 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050057 int w = dimensions.fWidth;
58 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040059 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
60 if (texels[currentMipLevel].fPixels) {
61 const size_t minRowBytes = w * bpp;
62 if (caps->writePixelsRowBytesSupport()) {
63 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
64 return false;
65 }
66 if (texels[currentMipLevel].fRowBytes % bpp) {
67 return false;
68 }
69 } else {
70 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
71 return false;
72 }
73 }
74 ++levelsWithPixelsCnt;
75 }
76 if (w == 1 && h == 1) {
77 if (currentMipLevel != mipLevelCount - 1) {
78 return false;
79 }
80 } else {
81 w = std::max(w / 2, 1);
82 h = std::max(h / 2, 1);
83 }
84 }
85 // Either just a base layer or a full stack is required.
86 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
87 return false;
88 }
89 // Can specify just the base, all levels, or no levels.
90 if (!hasBasePixels) {
91 return levelsWithPixelsCnt == 0;
92 }
Brian Salomond2a8ae22019-09-10 16:03:59 -040093 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -040094}
95
Brian Salomona56a7462020-02-07 14:17:25 -050096sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -040097 const GrBackendFormat& format,
98 GrRenderable renderable,
99 int renderTargetSampleCnt,
100 SkBudgeted budgeted,
101 GrProtected isProtected,
102 int mipLevelCount,
103 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400104 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400105 // Call GrGpu::createCompressedTexture.
106 return nullptr;
107 }
cblume55f2d2d2016-02-26 13:20:48 -0800108
Brian Salomona90382f2019-09-17 09:01:56 -0400109 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500110 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
111 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700113 }
114
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400115 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400116 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400117 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500118 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400119 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400120 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400121 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500122 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400123 format,
124 renderable,
125 renderTargetSampleCnt,
126 budgeted,
127 isProtected,
128 mipLevelCount,
129 levelClearMask);
130 if (tex) {
131 SkASSERT(tex->backendFormat() == format);
132 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
133 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
134 tex->resourcePriv().removeScratchKey();
135 }
136 fStats.incTextureCreates();
137 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
138 SkASSERT(GrRenderable::kYes == renderable);
139 tex->asRenderTarget()->setRequiresManualMSAAResolve();
140 }
141 }
142 return tex;
143}
144
Brian Salomona56a7462020-02-07 14:17:25 -0500145sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400146 const GrBackendFormat& format,
147 GrRenderable renderable,
148 int renderTargetSampleCnt,
149 GrMipMapped mipMapped,
150 SkBudgeted budgeted,
151 GrProtected isProtected) {
152 int mipLevelCount = 1;
153 if (mipMapped == GrMipMapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500154 mipLevelCount =
155 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400156 }
157 uint32_t levelClearMask =
158 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500159 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
160 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomona90382f2019-09-17 09:01:56 -0400161 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
162 tex->texturePriv().markMipMapsClean();
163 }
164 return tex;
165}
166
Brian Salomona56a7462020-02-07 14:17:25 -0500167sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400168 const GrBackendFormat& format,
169 GrRenderable renderable,
170 int renderTargetSampleCnt,
171 SkBudgeted budgeted,
172 GrProtected isProtected,
173 GrColorType textureColorType,
174 GrColorType srcColorType,
175 const GrMipLevel texels[],
176 int texelLevelCount) {
177 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400178 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500179 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400180 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400181 return nullptr;
182 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400183 }
184
Brian Osman788b9162020-02-07 10:36:46 -0500185 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400186 uint32_t levelClearMask = 0;
187 if (this->caps()->shouldInitializeTextures()) {
188 if (texelLevelCount) {
189 for (int i = 0; i < mipLevelCount; ++i) {
190 if (!texels->fPixels) {
191 levelClearMask |= static_cast<uint32_t>(1 << i);
192 }
193 }
194 } else {
195 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
196 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400197 }
198
Brian Salomona56a7462020-02-07 14:17:25 -0500199 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
200 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800201 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400202 bool markMipLevelsClean = false;
203 // Currently if level 0 does not have pixels then no other level may, as enforced by
204 // validate_texel_levels.
205 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500206 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
207 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400208 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800209 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400210 // Currently if level[1] of mip map has pixel data then so must all other levels.
211 // as enforced by validate_texel_levels.
212 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
213 fStats.incTextureUploads();
214 } else if (levelClearMask && mipLevelCount > 1) {
215 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800216 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400217 if (markMipLevelsClean) {
218 tex->texturePriv().markMipMapsClean();
219 }
bsalomonb12ea412015-02-02 21:19:50 -0800220 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000221 return tex;
222}
223
Robert Phillips9f744f72019-12-19 19:14:33 -0500224sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400225 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500226 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500227 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500228 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500229 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400230 size_t dataSize) {
231 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500232 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
233 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400234 return nullptr;
235 }
Brian Salomona3e29962019-07-16 11:52:08 -0400236 // Note if we relax the requirement that data must be provided then we must check
237 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400238 if (!data) {
239 return nullptr;
240 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400241 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400242 return nullptr;
243 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500244
245 // TODO: expand CompressedDataIsCorrect to work here too
Greg Daniel01f278c2020-06-12 16:58:17 -0400246 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillips9f744f72019-12-19 19:14:33 -0500247
Robert Phillips99dead92020-01-27 16:11:57 -0500248 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
249 mipMapped == GrMipMapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400250 return nullptr;
251 }
Robert Phillips3a833922020-01-21 15:25:58 -0500252 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
253 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400254}
255
Greg Daniel7ef28f32017-04-20 16:41:55 +0000256sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400257 GrWrapOwnership ownership,
258 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500259 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500260 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000261 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400262
263 const GrCaps* caps = this->caps();
264 SkASSERT(caps);
265
Greg Daniel7bfc9132019-08-14 14:23:53 -0400266 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800267 return nullptr;
268 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400269 if (backendTex.width() > caps->maxTextureSize() ||
270 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800271 return nullptr;
272 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400273
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400274 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400275}
Eric Karl5c779752017-05-08 12:02:07 -0700276
Robert Phillipsb915c942019-12-17 14:44:37 -0500277sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
278 GrWrapOwnership ownership,
279 GrWrapCacheable cacheable) {
280 this->handleDirtyContext();
281
282 const GrCaps* caps = this->caps();
283 SkASSERT(caps);
284
285 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
286 return nullptr;
287 }
288 if (backendTex.width() > caps->maxTextureSize() ||
289 backendTex.height() > caps->maxTextureSize()) {
290 return nullptr;
291 }
292
293 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
294}
295
Brian Salomond17f6582017-07-19 18:28:58 -0400296sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400297 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400298 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500299 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400300 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500301 if (sampleCnt < 1) {
302 return nullptr;
303 }
Robert Phillips0902c982019-07-16 07:47:56 -0400304
305 const GrCaps* caps = this->caps();
306
Greg Daniel7bfc9132019-08-14 14:23:53 -0400307 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400308 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400309 return nullptr;
310 }
311
Robert Phillips0902c982019-07-16 07:47:56 -0400312 if (backendTex.width() > caps->maxRenderTargetSize() ||
313 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400314 return nullptr;
315 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400316 sk_sp<GrTexture> tex =
317 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400318 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600319 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
320 tex->asRenderTarget()->setRequiresManualMSAAResolve();
321 }
bungeman6bd52842016-10-27 09:30:08 -0700322 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000323}
324
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400325sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400326 this->handleDirtyContext();
327
328 const GrCaps* caps = this->caps();
329
Greg Daniel6fa62e22019-08-07 15:52:37 -0400330 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800331 return nullptr;
332 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400333
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400334 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500335 if (backendRT.isFramebufferOnly()) {
336 rt->setFramebufferOnly();
337 }
338 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000339}
340
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400341sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400342 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500343 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400344
345 const GrCaps* caps = this->caps();
346
347 int maxSize = caps->maxTextureSize();
348 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
349 return nullptr;
350 }
351
Greg Daniel6fa62e22019-08-07 15:52:37 -0400352 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400353 return nullptr;
354 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400355
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400356 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Chris Dalton3f7932e2019-08-19 00:39:13 -0600357 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
358 rt->setRequiresManualMSAAResolve();
359 }
360 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800361}
362
Greg Danielb46add82019-01-02 14:51:29 -0500363sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
364 const GrVkDrawableInfo& vkInfo) {
365 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
366}
367
368sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
369 const GrVkDrawableInfo& vkInfo) {
370 // This is only supported on Vulkan so we default to returning nullptr here
371 return nullptr;
372}
373
Brian Salomondbf70722019-02-07 11:31:24 -0500374sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
375 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400376 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000377 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500378 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700379 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700380 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700381 }
cdalton397536c2016-03-25 12:15:03 -0700382 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800383}
384
Greg Daniel46cfbc62019-06-07 11:43:30 -0400385bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400386 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400387 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700388 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500389 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500390
391 if (dst->readOnly()) {
392 return false;
393 }
394
joshualitt1cbdcde2015-08-21 11:53:29 -0700395 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500396
Greg Daniele227fe42019-08-21 13:52:24 -0400397 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700398}
399
Brian Salomona6948702018-06-01 15:33:20 -0400400bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400401 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
402 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400403 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500404 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500405 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400406 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500407
Brian Salomon1d435302019-07-01 13:05:28 -0400408 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
409 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
410 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700411 return false;
412 }
413
Brian Salomon1047a492019-07-02 12:25:21 -0400414 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
415 if (!this->caps()->readPixelsRowBytesSupport()) {
416 if (rowBytes != minRowBytes) {
417 return false;
418 }
419 } else {
420 if (rowBytes < minRowBytes) {
421 return false;
422 }
423 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
424 return false;
425 }
426 }
427
Brian Salomonbf7b6202016-11-11 16:08:03 -0500428 this->handleDirtyContext();
429
Brian Salomonf77c1462019-08-01 15:19:29 -0400430 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
431 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000432}
433
Brian Salomona9b04b92018-06-01 15:04:28 -0400434bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400435 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400436 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400437 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev1db4d142020-04-10 00:58:52 -0400438 ATRACE_ANDROID_FRAMEWORK_ALWAYS("texture_upload");
Brian Salomonbf7b6202016-11-11 16:08:03 -0500439 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500440 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500441
442 if (surface->readOnly()) {
443 return false;
444 }
445
Brian Salomon1047a492019-07-02 12:25:21 -0400446 if (mipLevelCount == 0) {
447 return false;
448 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400449 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400450 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
451 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400452 if (!bounds.contains(subRect)) {
453 return false;
454 }
455 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
456 // We require that if the texels are mipped, than the write region is the entire surface
457 return false;
458 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400459
Brian Salomona56a7462020-02-07 14:17:25 -0500460 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
461 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400462 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800463 }
jvanverth2dc29942015-09-01 07:16:46 -0700464
bsalomon@google.com6f379512011-11-16 20:36:03 +0000465 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400466 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400467 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700468 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400469 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800470 fStats.incTextureUploads();
471 return true;
472 }
473 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000474}
475
Brian Salomone05ba5a2019-04-08 11:59:07 -0400476bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400477 GrColorType textureColorType, GrColorType bufferColorType,
478 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400479 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500480 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700481 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800482
Brian Salomonc67c31c2018-12-06 10:00:03 -0500483 if (texture->readOnly()) {
484 return false;
485 }
486
Greg Daniel660cc992017-06-26 14:55:05 -0400487 // We require that the write region is contained in the texture
488 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
489 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
490 if (!bounds.contains(subRect)) {
491 return false;
492 }
493
Brian Salomonb28cb682019-07-26 12:48:47 -0400494 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400495 if (this->caps()->writePixelsRowBytesSupport()) {
496 if (rowBytes < SkToSizeT(bpp * width)) {
497 return false;
498 }
499 if (rowBytes % bpp) {
500 return false;
501 }
502 } else {
503 if (rowBytes != SkToSizeT(bpp * width)) {
504 return false;
505 }
506 }
507
jvanverth17aa0472016-01-05 10:41:27 -0800508 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400509 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
510 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700511 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500512 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800513 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700514
jvanverth17aa0472016-01-05 10:41:27 -0800515 return true;
516 }
517 return false;
518}
519
Brian Salomon26de56e2019-04-10 12:14:26 -0400520bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400521 GrColorType surfaceColorType, GrColorType bufferColorType,
522 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400523 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400524 SkASSERT(surface);
525 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400526 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400527
Greg Danielba88ab62019-07-26 09:14:01 -0400528#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400529 auto supportedRead = this->caps()->supportedReadPixelsColorType(
530 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400531 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
532 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
533#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400534
535 // We require that the write region is contained in the texture
536 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
537 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
538 if (!bounds.contains(subRect)) {
539 return false;
540 }
541
542 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400543 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
544 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400545 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400546 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400547 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400548 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400549}
550
Brian Salomon930f9392018-06-20 16:25:26 -0400551bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400552 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400553 SkASSERT(texture);
554 SkASSERT(this->caps()->mipMapSupport());
555 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600556 if (!texture->texturePriv().mipMapsAreDirty()) {
557 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
558 // actual target. This may be caused by things that the drawingManager could not predict,
559 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
560 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
561 return true;
562 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500563 if (texture->readOnly()) {
564 return false;
565 }
Brian Salomon930f9392018-06-20 16:25:26 -0400566 if (this->onRegenerateMipMapLevels(texture)) {
567 texture->texturePriv().markMipMapsClean();
568 return true;
569 }
570 return false;
571}
572
Brian Salomon1f05d452019-02-08 12:33:08 -0500573void GrGpu::resetTextureBindings() {
574 this->handleDirtyContext();
575 this->onResetTextureBindings();
576}
577
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400578void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000579 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000580 this->handleDirtyContext();
Jim Van Verthbb61fe32020-07-07 16:39:04 -0400581 this->onResolveRenderTarget(target, resolveRect);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000582}
583
Brian Salomon1fabd512018-02-09 09:54:25 -0500584void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
585 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700586 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500587 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700588 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
589 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700590 GrTexture* texture = surface->asTexture();
591 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400592 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700593 }
594 }
595}
596
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600597int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700598 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600599 SkASSERT(renderTarget->numSamples() > 1 ||
600 (renderTarget->renderTargetPriv().getStencilAttachment() &&
601 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700602
603 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600604 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700605 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
606}
607
Greg Danielfe159622020-04-10 17:43:51 +0000608void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[],
609 int numProxies,
610 SkSurface::BackendSurfaceAccess access,
Greg Daniel9efe3862020-06-11 11:51:06 -0400611 const GrFlushInfo& info,
612 const GrBackendSurfaceMutableState* newState) {
Brian Salomone39526b2019-06-24 16:35:53 -0400613 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000614
Robert Phillips9da87e02019-02-04 13:26:26 -0500615 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500616
Greg Daniel8561fc22020-04-08 12:52:51 -0400617 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
618 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500619 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400620 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400621 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400622 semaphores[i] = resourceProvider->wrapBackendSemaphore(
623 info.fSignalSemaphores[i],
624 GrResourceProvider::SemaphoreWrapType::kWillSignal,
625 kBorrow_GrWrapOwnership);
Greg Daniel0106fcc2020-07-01 17:40:12 -0400626 // If we failed to wrap the semaphore it means the client didn't give us a valid
627 // semaphore to begin with. Therefore, it is fine to not signal it.
628 if (semaphores[i]) {
629 this->insertSemaphore(semaphores[i].get());
630 }
Greg Daniel51316782017-08-02 15:10:09 +0000631 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400632 semaphores[i] = resourceProvider->makeSemaphore(false);
633 if (semaphores[i]) {
634 this->insertSemaphore(semaphores[i].get());
635 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
636 }
Greg Daniel51316782017-08-02 15:10:09 +0000637 }
638 }
639 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500640
Greg Danielfe159622020-04-10 17:43:51 +0000641 if (info.fFinishedProc) {
642 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
643 }
Greg Daniel55822f12020-05-26 11:26:45 -0400644
645 if (info.fSubmittedProc) {
646 fSubmittedProcs.emplace_back(info.fSubmittedProc, info.fSubmittedContext);
647 }
648
Greg Daniel9efe3862020-06-11 11:51:06 -0400649 // We currently don't support passing in new surface state for multiple proxies here. The only
650 // time we have multiple proxies is if we are flushing a yuv SkImage which won't have state
651 // updates anyways.
652 SkASSERT(!newState || numProxies == 1);
653 SkASSERT(!newState || access == SkSurface::BackendSurfaceAccess::kNoAccess);
654 this->prepareSurfacesForBackendAccessAndStateUpdates(proxies, numProxies, access, newState);
Greg Danielfe159622020-04-10 17:43:51 +0000655}
656
657bool GrGpu::submitToGpu(bool syncCpu) {
658 this->stats()->incNumSubmitToGpus();
659
Greg Daniela58db7f2020-07-15 09:17:59 -0400660 if (auto manager = this->stagingBufferManager()) {
661 manager->detachBuffers();
662 }
Stephen Whitef3d5d442020-04-08 10:35:58 -0400663
Greg Danielfe159622020-04-10 17:43:51 +0000664 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400665
Greg Daniel55822f12020-05-26 11:26:45 -0400666 this->callSubmittedProcs(submitted);
667
Greg Danielfe159622020-04-10 17:43:51 +0000668 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000669}
Brian Osman71a18892017-08-10 10:23:25 -0400670
Brian Salomon24069eb2020-06-24 10:19:52 -0400671bool GrGpu::checkAndResetOOMed() {
672 if (fOOMed) {
673 fOOMed = false;
674 return true;
675 }
676 return false;
677}
678
Greg Daniel55822f12020-05-26 11:26:45 -0400679void GrGpu::callSubmittedProcs(bool success) {
680 for (int i = 0; i < fSubmittedProcs.count(); ++i) {
681 fSubmittedProcs[i].fProc(fSubmittedProcs[i].fContext, success);
682 }
683 fSubmittedProcs.reset();
684}
685
Kevin Lubickf4def342018-10-04 12:52:50 -0400686#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400687void GrGpu::dumpJSON(SkJSONWriter* writer) const {
688 writer->beginObject();
689
690 // TODO: Is there anything useful in the base class to dump here?
691
692 this->onDumpJSON(writer);
693
694 writer->endObject();
695}
Kevin Lubickf4def342018-10-04 12:52:50 -0400696#else
697void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
698#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400699
Robert Phillipsf0ced622019-05-16 09:06:25 -0400700#if GR_TEST_UTILS
701
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500702#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500703static const char* cache_result_to_str(int i) {
704 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
705 "hits",
706 "misses",
707 "partials"
708 };
709 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
710 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
711 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
712 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
713 return kCacheResultStrings[i];
714}
715
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500716void GrGpu::Stats::dump(SkString* out) {
717 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
718 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
719 out->appendf("Textures Created: %d\n", fTextureCreates);
720 out->appendf("Texture Uploads: %d\n", fTextureUploads);
721 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400722 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500723 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
724 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400725 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500726
727 SkASSERT(fNumInlineCompilationFailures == 0);
728 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
729 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
730 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
731 fInlineProgramCacheStats[i]);
732 }
733
734 SkASSERT(fNumPreCompilationFailures == 0);
735 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
736 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
737 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
738 fPreProgramCacheStats[i]);
739 }
740
741 SkASSERT(fNumCompilationFailures == 0);
742 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
743 out->appendf("Total number of partial compilation successes %d\n",
744 fNumPartialCompilationSuccesses);
745 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500746
747 // enable this block to output CSV-style stats for program pre-compilation
748#if 0
749 SkASSERT(fNumInlineCompilationFailures == 0);
750 SkASSERT(fNumPreCompilationFailures == 0);
751 SkASSERT(fNumCompilationFailures == 0);
752 SkASSERT(fNumPartialCompilationSuccesses == 0);
753
754 SkDebugf("%d, %d, %d, %d, %d\n",
755 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
756 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
757 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
758 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
759 fNumCompilationSuccesses);
760#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500761}
762
763void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
764 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
765 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500766}
767
Robert Phillips57ef6802019-09-23 10:12:47 -0400768#endif // GR_GPU_STATS
769#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500770
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500771bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
772 GrMipMapped mipMapped,
773 const BackendTextureData* data) {
774 int numMipLevels = 1;
775 if (mipMapped == GrMipMapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400776 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500777 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400778
Robert Phillips42716d42019-12-16 12:19:54 -0500779 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400780 return true;
781 }
782
Robert Phillips42716d42019-12-16 12:19:54 -0500783 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500784 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500785 }
786
787 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
788
Brian Salomon85c3d682019-11-04 15:04:54 -0500789 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400790 return false;
791 }
792
Brian Salomon85c3d682019-11-04 15:04:54 -0500793 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500794 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500795 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500796 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400797 return false;
798 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500799 if (colorType != data->pixmap(i).colorType()) {
800 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400801 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400802 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400803 return true;
804}
805
Robert Phillipsb915c942019-12-17 14:44:37 -0500806bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
807 GrMipMapped mipMapped, const BackendTextureData* data) {
808
809 if (!data || data->type() == BackendTextureData::Type::kColor) {
810 return true;
811 }
812
813 if (data->type() == BackendTextureData::Type::kPixmaps) {
814 return false;
815 }
816
817 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
818
Robert Phillips99dead92020-01-27 16:11:57 -0500819 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
820 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500821
822 return computedSize == data->compressedSize();
823}
824
Brian Salomon85c3d682019-11-04 15:04:54 -0500825GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
826 const GrBackendFormat& format,
827 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500828 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -0400829 GrProtected isProtected) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400830 const GrCaps* caps = this->caps();
831
832 if (!format.isValid()) {
833 return {};
834 }
835
Robert Phillipsd34691b2019-09-24 13:38:43 -0400836 if (caps->isFormatCompressed(format)) {
837 // Compressed formats must go through the createCompressedBackendTexture API
838 return {};
839 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400840
Brian Salomon85c3d682019-11-04 15:04:54 -0500841 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
842 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400843 return {};
844 }
845
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500846 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400847 return {};
848 }
849
Greg Daniel16032b32020-05-06 15:31:10 -0400850 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected);
851}
852
853bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture,
Greg Daniel25597782020-06-11 13:15:08 -0400854 sk_sp<GrRefCntedCallback> finishedCallback,
Greg Daniel16032b32020-05-06 15:31:10 -0400855 const BackendTextureData* data) {
856 SkASSERT(data);
857 const GrCaps* caps = this->caps();
858
Greg Daniel16032b32020-05-06 15:31:10 -0400859 if (!backendTexture.isValid()) {
860 return false;
861 }
862
863 if (data->type() == BackendTextureData::Type::kPixmaps) {
864 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
865 if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) {
866 return false;
867 }
868 }
869
870 if (backendTexture.hasMipMaps() && !this->caps()->mipMapSupport()) {
871 return false;
872 }
873
874 GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
875 if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) {
876 return false;
877 }
878
Greg Daniel25597782020-06-11 13:15:08 -0400879 return this->onUpdateBackendTexture(backendTexture, std::move(finishedCallback), data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400880}
Robert Phillipsb915c942019-12-17 14:44:37 -0500881
882GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
883 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500884 GrMipMapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -0400885 GrProtected isProtected) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500886 const GrCaps* caps = this->caps();
887
888 if (!format.isValid()) {
889 return {};
890 }
891
Greg Daniel01f278c2020-06-12 16:58:17 -0400892 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
Robert Phillipsb915c942019-12-17 14:44:37 -0500893 if (compressionType == SkImage::CompressionType::kNone) {
894 // Uncompressed formats must go through the createBackendTexture API
895 return {};
896 }
897
898 if (dimensions.isEmpty() ||
899 dimensions.width() > caps->maxTextureSize() ||
900 dimensions.height() > caps->maxTextureSize()) {
901 return {};
902 }
903
904 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
905 return {};
906 }
907
Greg Danielaaf738c2020-07-10 09:30:33 -0400908 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped, isProtected);
909}
910
911bool GrGpu::updateCompressedBackendTexture(const GrBackendTexture& backendTexture,
912 sk_sp<GrRefCntedCallback> finishedCallback,
913 const BackendTextureData* data) {
914 SkASSERT(data);
915
916 if (!backendTexture.isValid()) {
917 return false;
Robert Phillipsb915c942019-12-17 14:44:37 -0500918 }
919
Greg Danielaaf738c2020-07-10 09:30:33 -0400920 GrBackendFormat format = backendTexture.getBackendFormat();
921
922 SkImage::CompressionType compressionType = GrBackendFormatToCompressionType(format);
923 if (compressionType == SkImage::CompressionType::kNone) {
924 // Uncompressed formats must go through the createBackendTexture API
925 return false;
926 }
927
928 if (backendTexture.hasMipMaps() && !this->caps()->mipMapSupport()) {
929 return false;
930 }
931
932 GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
933
934 if (!CompressedDataIsCorrect(backendTexture.dimensions(), compressionType, mipMapped, data)) {
935 return false;
936 }
937
938 return this->onUpdateCompressedBackendTexture(backendTexture, std::move(finishedCallback),
939 data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500940}