blob: c3388f8189f3b744cf0bc63ce797f98ee99f93c9 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrContext.h"
Robert Phillips99dead92020-01-27 16:11:57 -050014#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkMathPriv.h"
Robert Phillips57ef6802019-09-23 10:12:47 -040016#include "src/core/SkMipMap.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrCaps.h"
19#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040020#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrGpuResourcePriv.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060022#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrPathRendering.h"
24#include "src/gpu/GrPipeline.h"
25#include "src/gpu/GrRenderTargetPriv.h"
26#include "src/gpu/GrResourceCache.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrSemaphore.h"
Stephen Whitef3d5d442020-04-08 10:35:58 -040029#include "src/gpu/GrStagingBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrStencilAttachment.h"
31#include "src/gpu/GrStencilSettings.h"
32#include "src/gpu/GrSurfacePriv.h"
33#include "src/gpu/GrTexturePriv.h"
34#include "src/gpu/GrTextureProxyPriv.h"
35#include "src/gpu/GrTracing.h"
36#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070037
Stephen Whitef3d5d442020-04-08 10:35:58 -040038static const size_t kMinStagingBufferSize = 32 * 1024;
39
bsalomon@google.comd302f142011-03-03 13:54:13 +000040////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Brian Salomone2826ab2019-06-04 15:58:31 -040042GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000043
Stephen Whitef3d5d442020-04-08 10:35:58 -040044GrGpu::~GrGpu() {
45 SkASSERT(fBusyStagingBuffers.isEmpty());
46}
bsalomon1d89ddc2014-08-19 14:20:58 -070047
bsalomon6e2aad42016-04-01 11:54:31 -070048void GrGpu::disconnect(DisconnectType) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000049
bsalomon@google.comd302f142011-03-03 13:54:13 +000050////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000051
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000052bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
Brian Salomonc8d092a2020-02-24 10:14:21 -050053 GrSamplerState::Filter filter) {
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000054 SkASSERT(texProxy);
Brian Salomonc8d092a2020-02-24 10:14:21 -050055 if (filter != GrSamplerState::Filter::kMipMap || texProxy->mipMapped() == GrMipMapped::kYes ||
56 !caps->mipMapSupport()) {
57 return false;
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000058 }
Brian Salomonc8d092a2020-02-24 10:14:21 -050059 return SkMipMap::ComputeLevelCount(texProxy->width(), texProxy->height()) > 0;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040060}
61
Brian Salomona56a7462020-02-07 14:17:25 -050062static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -040063 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -040064 SkASSERT(mipLevelCount > 0);
65 bool hasBasePixels = texels[0].fPixels;
66 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -040067 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -050068 int w = dimensions.fWidth;
69 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -040070 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
71 if (texels[currentMipLevel].fPixels) {
72 const size_t minRowBytes = w * bpp;
73 if (caps->writePixelsRowBytesSupport()) {
74 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
75 return false;
76 }
77 if (texels[currentMipLevel].fRowBytes % bpp) {
78 return false;
79 }
80 } else {
81 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
82 return false;
83 }
84 }
85 ++levelsWithPixelsCnt;
86 }
87 if (w == 1 && h == 1) {
88 if (currentMipLevel != mipLevelCount - 1) {
89 return false;
90 }
91 } else {
92 w = std::max(w / 2, 1);
93 h = std::max(h / 2, 1);
94 }
95 }
96 // Either just a base layer or a full stack is required.
97 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
98 return false;
99 }
100 // Can specify just the base, all levels, or no levels.
101 if (!hasBasePixels) {
102 return levelsWithPixelsCnt == 0;
103 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400104 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400105}
106
Brian Salomona56a7462020-02-07 14:17:25 -0500107sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400108 const GrBackendFormat& format,
109 GrRenderable renderable,
110 int renderTargetSampleCnt,
111 SkBudgeted budgeted,
112 GrProtected isProtected,
113 int mipLevelCount,
114 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400115 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400116 // Call GrGpu::createCompressedTexture.
117 return nullptr;
118 }
cblume55f2d2d2016-02-26 13:20:48 -0800119
Brian Salomona90382f2019-09-17 09:01:56 -0400120 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500121 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
122 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700123 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700124 }
125
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400126 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400127 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400128 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500129 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400130 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400131 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400132 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500133 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400134 format,
135 renderable,
136 renderTargetSampleCnt,
137 budgeted,
138 isProtected,
139 mipLevelCount,
140 levelClearMask);
141 if (tex) {
142 SkASSERT(tex->backendFormat() == format);
143 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
144 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
145 tex->resourcePriv().removeScratchKey();
146 }
147 fStats.incTextureCreates();
148 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
149 SkASSERT(GrRenderable::kYes == renderable);
150 tex->asRenderTarget()->setRequiresManualMSAAResolve();
151 }
152 }
153 return tex;
154}
155
Brian Salomona56a7462020-02-07 14:17:25 -0500156sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400157 const GrBackendFormat& format,
158 GrRenderable renderable,
159 int renderTargetSampleCnt,
160 GrMipMapped mipMapped,
161 SkBudgeted budgeted,
162 GrProtected isProtected) {
163 int mipLevelCount = 1;
164 if (mipMapped == GrMipMapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500165 mipLevelCount =
166 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400167 }
168 uint32_t levelClearMask =
169 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500170 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
171 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomona90382f2019-09-17 09:01:56 -0400172 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
173 tex->texturePriv().markMipMapsClean();
174 }
175 return tex;
176}
177
Brian Salomona56a7462020-02-07 14:17:25 -0500178sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400179 const GrBackendFormat& format,
180 GrRenderable renderable,
181 int renderTargetSampleCnt,
182 SkBudgeted budgeted,
183 GrProtected isProtected,
184 GrColorType textureColorType,
185 GrColorType srcColorType,
186 const GrMipLevel texels[],
187 int texelLevelCount) {
188 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400189 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500190 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400191 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400192 return nullptr;
193 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400194 }
195
Brian Osman788b9162020-02-07 10:36:46 -0500196 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400197 uint32_t levelClearMask = 0;
198 if (this->caps()->shouldInitializeTextures()) {
199 if (texelLevelCount) {
200 for (int i = 0; i < mipLevelCount; ++i) {
201 if (!texels->fPixels) {
202 levelClearMask |= static_cast<uint32_t>(1 << i);
203 }
204 }
205 } else {
206 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
207 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400208 }
209
Brian Salomona56a7462020-02-07 14:17:25 -0500210 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
211 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800212 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400213 bool markMipLevelsClean = false;
214 // Currently if level 0 does not have pixels then no other level may, as enforced by
215 // validate_texel_levels.
216 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500217 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
218 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400219 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800220 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400221 // Currently if level[1] of mip map has pixel data then so must all other levels.
222 // as enforced by validate_texel_levels.
223 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
224 fStats.incTextureUploads();
225 } else if (levelClearMask && mipLevelCount > 1) {
226 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800227 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400228 if (markMipLevelsClean) {
229 tex->texturePriv().markMipMapsClean();
230 }
bsalomonb12ea412015-02-02 21:19:50 -0800231 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000232 return tex;
233}
234
Robert Phillips9f744f72019-12-19 19:14:33 -0500235sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400236 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500237 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500238 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500239 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500240 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400241 size_t dataSize) {
242 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500243 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
244 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400245 return nullptr;
246 }
Brian Salomona3e29962019-07-16 11:52:08 -0400247 // Note if we relax the requirement that data must be provided then we must check
248 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400249 if (!data) {
250 return nullptr;
251 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400252 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400253 return nullptr;
254 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500255
256 // TODO: expand CompressedDataIsCorrect to work here too
257 SkImage::CompressionType compressionType = this->caps()->compressionType(format);
258
Robert Phillips99dead92020-01-27 16:11:57 -0500259 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
260 mipMapped == GrMipMapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400261 return nullptr;
262 }
Robert Phillips3a833922020-01-21 15:25:58 -0500263 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
264 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400265}
266
Greg Daniel7ef28f32017-04-20 16:41:55 +0000267sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400268 GrWrapOwnership ownership,
269 GrWrapCacheable cacheable,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500270 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500271 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000272 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400273
274 const GrCaps* caps = this->caps();
275 SkASSERT(caps);
276
Greg Daniel7bfc9132019-08-14 14:23:53 -0400277 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800278 return nullptr;
279 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400280 if (backendTex.width() > caps->maxTextureSize() ||
281 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800282 return nullptr;
283 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400284
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400285 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400286}
Eric Karl5c779752017-05-08 12:02:07 -0700287
Robert Phillipsb915c942019-12-17 14:44:37 -0500288sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
289 GrWrapOwnership ownership,
290 GrWrapCacheable cacheable) {
291 this->handleDirtyContext();
292
293 const GrCaps* caps = this->caps();
294 SkASSERT(caps);
295
296 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
297 return nullptr;
298 }
299 if (backendTex.width() > caps->maxTextureSize() ||
300 backendTex.height() > caps->maxTextureSize()) {
301 return nullptr;
302 }
303
304 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
305}
306
Brian Salomond17f6582017-07-19 18:28:58 -0400307sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400308 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400309 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500310 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400311 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500312 if (sampleCnt < 1) {
313 return nullptr;
314 }
Robert Phillips0902c982019-07-16 07:47:56 -0400315
316 const GrCaps* caps = this->caps();
317
Greg Daniel7bfc9132019-08-14 14:23:53 -0400318 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400319 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400320 return nullptr;
321 }
322
Robert Phillips0902c982019-07-16 07:47:56 -0400323 if (backendTex.width() > caps->maxRenderTargetSize() ||
324 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400325 return nullptr;
326 }
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400327 sk_sp<GrTexture> tex =
328 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400329 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600330 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
331 tex->asRenderTarget()->setRequiresManualMSAAResolve();
332 }
bungeman6bd52842016-10-27 09:30:08 -0700333 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000334}
335
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400336sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400337 this->handleDirtyContext();
338
339 const GrCaps* caps = this->caps();
340
Greg Daniel6fa62e22019-08-07 15:52:37 -0400341 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800342 return nullptr;
343 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400344
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400345 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT);
Stephen White3c0a50f2020-01-16 18:19:54 -0500346 if (backendRT.isFramebufferOnly()) {
347 rt->setFramebufferOnly();
348 }
349 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000350}
351
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400352sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400353 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500354 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400355
356 const GrCaps* caps = this->caps();
357
358 int maxSize = caps->maxTextureSize();
359 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
360 return nullptr;
361 }
362
Greg Daniel6fa62e22019-08-07 15:52:37 -0400363 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400364 return nullptr;
365 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400366
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400367 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
Chris Dalton3f7932e2019-08-19 00:39:13 -0600368 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
369 rt->setRequiresManualMSAAResolve();
370 }
371 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800372}
373
Greg Danielb46add82019-01-02 14:51:29 -0500374sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
375 const GrVkDrawableInfo& vkInfo) {
376 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
377}
378
379sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
380 const GrVkDrawableInfo& vkInfo) {
381 // This is only supported on Vulkan so we default to returning nullptr here
382 return nullptr;
383}
384
Brian Salomondbf70722019-02-07 11:31:24 -0500385sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
386 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400387 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000388 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500389 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700390 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700391 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700392 }
cdalton397536c2016-03-25 12:15:03 -0700393 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800394}
395
Greg Daniel46cfbc62019-06-07 11:43:30 -0400396bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400397 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400398 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700399 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500400 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500401
402 if (dst->readOnly()) {
403 return false;
404 }
405
joshualitt1cbdcde2015-08-21 11:53:29 -0700406 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500407
Greg Daniele227fe42019-08-21 13:52:24 -0400408 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700409}
410
Brian Salomona6948702018-06-01 15:33:20 -0400411bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400412 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
413 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400414 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500415 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500416 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400417 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500418
Brian Salomon1d435302019-07-01 13:05:28 -0400419 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
420 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
421 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700422 return false;
423 }
424
Brian Salomon1047a492019-07-02 12:25:21 -0400425 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
426 if (!this->caps()->readPixelsRowBytesSupport()) {
427 if (rowBytes != minRowBytes) {
428 return false;
429 }
430 } else {
431 if (rowBytes < minRowBytes) {
432 return false;
433 }
434 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
435 return false;
436 }
437 }
438
Brian Salomonbf7b6202016-11-11 16:08:03 -0500439 this->handleDirtyContext();
440
Brian Salomonf77c1462019-08-01 15:19:29 -0400441 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
442 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000443}
444
Brian Salomona9b04b92018-06-01 15:04:28 -0400445bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400446 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400447 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400448 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stan Iliev1db4d142020-04-10 00:58:52 -0400449 ATRACE_ANDROID_FRAMEWORK_ALWAYS("texture_upload");
Brian Salomonbf7b6202016-11-11 16:08:03 -0500450 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500451 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500452
453 if (surface->readOnly()) {
454 return false;
455 }
456
Brian Salomon1047a492019-07-02 12:25:21 -0400457 if (mipLevelCount == 0) {
458 return false;
459 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400460 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400461 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
462 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400463 if (!bounds.contains(subRect)) {
464 return false;
465 }
466 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
467 // We require that if the texels are mipped, than the write region is the entire surface
468 return false;
469 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400470
Brian Salomona56a7462020-02-07 14:17:25 -0500471 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
472 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400473 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800474 }
jvanverth2dc29942015-09-01 07:16:46 -0700475
bsalomon@google.com6f379512011-11-16 20:36:03 +0000476 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400477 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400478 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700479 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400480 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800481 fStats.incTextureUploads();
482 return true;
483 }
484 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000485}
486
Brian Salomone05ba5a2019-04-08 11:59:07 -0400487bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400488 GrColorType textureColorType, GrColorType bufferColorType,
489 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400490 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500491 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700492 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800493
Brian Salomonc67c31c2018-12-06 10:00:03 -0500494 if (texture->readOnly()) {
495 return false;
496 }
497
Greg Daniel660cc992017-06-26 14:55:05 -0400498 // We require that the write region is contained in the texture
499 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
500 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
501 if (!bounds.contains(subRect)) {
502 return false;
503 }
504
Brian Salomonb28cb682019-07-26 12:48:47 -0400505 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400506 if (this->caps()->writePixelsRowBytesSupport()) {
507 if (rowBytes < SkToSizeT(bpp * width)) {
508 return false;
509 }
510 if (rowBytes % bpp) {
511 return false;
512 }
513 } else {
514 if (rowBytes != SkToSizeT(bpp * width)) {
515 return false;
516 }
517 }
518
jvanverth17aa0472016-01-05 10:41:27 -0800519 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400520 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
521 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700522 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500523 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800524 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700525
jvanverth17aa0472016-01-05 10:41:27 -0800526 return true;
527 }
528 return false;
529}
530
Brian Salomon26de56e2019-04-10 12:14:26 -0400531bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400532 GrColorType surfaceColorType, GrColorType bufferColorType,
533 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400534 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400535 SkASSERT(surface);
536 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400537 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400538
Greg Danielba88ab62019-07-26 09:14:01 -0400539#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400540 auto supportedRead = this->caps()->supportedReadPixelsColorType(
541 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400542 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
543 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
544#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400545
546 // We require that the write region is contained in the texture
547 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
548 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
549 if (!bounds.contains(subRect)) {
550 return false;
551 }
552
553 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400554 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
555 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400556 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400557 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400558 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400559 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400560}
561
Brian Salomon930f9392018-06-20 16:25:26 -0400562bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400563 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400564 SkASSERT(texture);
565 SkASSERT(this->caps()->mipMapSupport());
566 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600567 if (!texture->texturePriv().mipMapsAreDirty()) {
568 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
569 // actual target. This may be caused by things that the drawingManager could not predict,
570 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
571 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
572 return true;
573 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500574 if (texture->readOnly()) {
575 return false;
576 }
Brian Salomon930f9392018-06-20 16:25:26 -0400577 if (this->onRegenerateMipMapLevels(texture)) {
578 texture->texturePriv().markMipMapsClean();
579 return true;
580 }
581 return false;
582}
583
Brian Salomon1f05d452019-02-08 12:33:08 -0500584void GrGpu::resetTextureBindings() {
585 this->handleDirtyContext();
586 this->onResetTextureBindings();
587}
588
Chris Dalton16a33c62019-09-24 22:19:17 -0600589void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -0500590 ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000591 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000592 this->handleDirtyContext();
Greg Daniel242536f2020-02-13 14:12:46 -0500593 this->onResolveRenderTarget(target, resolveRect, forExternalIO);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000594}
595
Brian Salomon1fabd512018-02-09 09:54:25 -0500596void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
597 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700598 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500599 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700600 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
601 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700602 GrTexture* texture = surface->asTexture();
603 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400604 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700605 }
606 }
607}
608
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600609int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700610 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600611 SkASSERT(renderTarget->numSamples() > 1 ||
612 (renderTarget->renderTargetPriv().getStencilAttachment() &&
613 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700614
615 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600616 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700617 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
618}
619
Stephen Whitef3d5d442020-04-08 10:35:58 -0400620#ifdef SK_DEBUG
621bool GrGpu::inStagingBuffers(GrStagingBuffer* b) const {
622 for (const auto& i : fStagingBuffers) {
623 if (b == i.get()) {
624 return true;
625 }
626 }
627 return false;
628}
629
630void GrGpu::validateStagingBuffers() const {
631 for (const auto& i : fStagingBuffers) {
632 GrStagingBuffer* buffer = i.get();
633 SkASSERT(fAvailableStagingBuffers.isInList(buffer) ||
634 fActiveStagingBuffers.isInList(buffer) ||
635 fBusyStagingBuffers.isInList(buffer));
636 }
637 for (auto b : fAvailableStagingBuffers) {
638 SkASSERT(this->inStagingBuffers(b));
639 }
640 for (auto b : fActiveStagingBuffers) {
641 SkASSERT(this->inStagingBuffers(b));
642 }
643 for (auto b : fBusyStagingBuffers) {
644 SkASSERT(this->inStagingBuffers(b));
645 }
646}
647#endif
648
Greg Danielfe159622020-04-10 17:43:51 +0000649void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[],
650 int numProxies,
651 SkSurface::BackendSurfaceAccess access,
652 const GrFlushInfo& info,
653 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400654 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danielfe159622020-04-10 17:43:51 +0000655
Robert Phillips9da87e02019-02-04 13:26:26 -0500656 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500657
Greg Daniel8561fc22020-04-08 12:52:51 -0400658 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
659 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500660 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400661 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400662 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400663 semaphores[i] = resourceProvider->wrapBackendSemaphore(
664 info.fSignalSemaphores[i],
665 GrResourceProvider::SemaphoreWrapType::kWillSignal,
666 kBorrow_GrWrapOwnership);
667 this->insertSemaphore(semaphores[i].get());
Greg Daniel51316782017-08-02 15:10:09 +0000668 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400669 semaphores[i] = resourceProvider->makeSemaphore(false);
670 if (semaphores[i]) {
671 this->insertSemaphore(semaphores[i].get());
672 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
673 }
Greg Daniel51316782017-08-02 15:10:09 +0000674 }
675 }
676 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500677
Greg Danielfe159622020-04-10 17:43:51 +0000678 if (info.fFinishedProc) {
679 this->addFinishedProc(info.fFinishedProc, info.fFinishedContext);
680 }
681 this->prepareSurfacesForBackendAccessAndExternalIO(proxies, numProxies, access,
682 externalRequests);
683}
684
685bool GrGpu::submitToGpu(bool syncCpu) {
686 this->stats()->incNumSubmitToGpus();
687
688#ifdef SK_DEBUG
689 this->validateStagingBuffers();
690#endif
Stephen Whitef3d5d442020-04-08 10:35:58 -0400691 this->unmapStagingBuffers();
692
Greg Danielfe159622020-04-10 17:43:51 +0000693 bool submitted = this->onSubmitToGpu(syncCpu);
Greg Daniel8561fc22020-04-08 12:52:51 -0400694
Stephen Whitef3d5d442020-04-08 10:35:58 -0400695 // Move all active staging buffers to the busy list.
Greg Danielfe159622020-04-10 17:43:51 +0000696 // TODO: this should probably be handled inside of the onSubmitToGpu by the backends.
Stephen Whitef3d5d442020-04-08 10:35:58 -0400697 while (GrStagingBuffer* buffer = fActiveStagingBuffers.head()) {
698 fActiveStagingBuffers.remove(buffer);
699 fBusyStagingBuffers.addToTail(buffer);
700 }
Greg Danielfe159622020-04-10 17:43:51 +0000701 return submitted;
Greg Daniel51316782017-08-02 15:10:09 +0000702}
Brian Osman71a18892017-08-10 10:23:25 -0400703
Kevin Lubickf4def342018-10-04 12:52:50 -0400704#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400705void GrGpu::dumpJSON(SkJSONWriter* writer) const {
706 writer->beginObject();
707
708 // TODO: Is there anything useful in the base class to dump here?
709
710 this->onDumpJSON(writer);
711
712 writer->endObject();
713}
Kevin Lubickf4def342018-10-04 12:52:50 -0400714#else
715void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
716#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400717
Robert Phillipsf0ced622019-05-16 09:06:25 -0400718#if GR_TEST_UTILS
719
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500720#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500721static const char* cache_result_to_str(int i) {
722 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
723 "hits",
724 "misses",
725 "partials"
726 };
727 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
728 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
729 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
730 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
731 return kCacheResultStrings[i];
732}
733
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500734void GrGpu::Stats::dump(SkString* out) {
735 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
736 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
737 out->appendf("Textures Created: %d\n", fTextureCreates);
738 out->appendf("Texture Uploads: %d\n", fTextureUploads);
739 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400740 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500741 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
742 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400743 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500744
745 SkASSERT(fNumInlineCompilationFailures == 0);
746 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
747 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
748 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
749 fInlineProgramCacheStats[i]);
750 }
751
752 SkASSERT(fNumPreCompilationFailures == 0);
753 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
754 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
755 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
756 fPreProgramCacheStats[i]);
757 }
758
759 SkASSERT(fNumCompilationFailures == 0);
760 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
761 out->appendf("Total number of partial compilation successes %d\n",
762 fNumPartialCompilationSuccesses);
763 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500764
765 // enable this block to output CSV-style stats for program pre-compilation
766#if 0
767 SkASSERT(fNumInlineCompilationFailures == 0);
768 SkASSERT(fNumPreCompilationFailures == 0);
769 SkASSERT(fNumCompilationFailures == 0);
770 SkASSERT(fNumPartialCompilationSuccesses == 0);
771
772 SkDebugf("%d, %d, %d, %d, %d\n",
773 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
774 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
775 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
776 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
777 fNumCompilationSuccesses);
778#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500779}
780
781void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
782 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
783 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500784}
785
Robert Phillips57ef6802019-09-23 10:12:47 -0400786#endif // GR_GPU_STATS
787#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500788
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500789bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
790 GrMipMapped mipMapped,
791 const BackendTextureData* data) {
792 int numMipLevels = 1;
793 if (mipMapped == GrMipMapped::kYes) {
794 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500795 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400796
Robert Phillips42716d42019-12-16 12:19:54 -0500797 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400798 return true;
799 }
800
Robert Phillips42716d42019-12-16 12:19:54 -0500801 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500802 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500803 }
804
805 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
806
Brian Salomon85c3d682019-11-04 15:04:54 -0500807 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400808 return false;
809 }
810
Brian Salomon85c3d682019-11-04 15:04:54 -0500811 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500812 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500813 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500814 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400815 return false;
816 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500817 if (colorType != data->pixmap(i).colorType()) {
818 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400819 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400820 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400821 return true;
822}
823
Robert Phillipsb915c942019-12-17 14:44:37 -0500824bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
825 GrMipMapped mipMapped, const BackendTextureData* data) {
826
827 if (!data || data->type() == BackendTextureData::Type::kColor) {
828 return true;
829 }
830
831 if (data->type() == BackendTextureData::Type::kPixmaps) {
832 return false;
833 }
834
835 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
836
Robert Phillips99dead92020-01-27 16:11:57 -0500837 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
838 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500839
840 return computedSize == data->compressedSize();
841}
842
Brian Salomon85c3d682019-11-04 15:04:54 -0500843GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
844 const GrBackendFormat& format,
845 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500846 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500847 GrProtected isProtected,
848 const BackendTextureData* data) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400849 const GrCaps* caps = this->caps();
850
851 if (!format.isValid()) {
852 return {};
853 }
854
Robert Phillipsd34691b2019-09-24 13:38:43 -0400855 if (caps->isFormatCompressed(format)) {
856 // Compressed formats must go through the createCompressedBackendTexture API
857 return {};
858 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400859
Brian Salomon85c3d682019-11-04 15:04:54 -0500860 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
861 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
862 if (!caps->areColorTypeAndFormatCompatible(ct, format)) {
863 return {};
864 }
865 }
866
867 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
868 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400869 return {};
870 }
871
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500872 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400873 return {};
874 }
875
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500876 if (!MipMapsAreCorrect(dimensions, mipMapped, data)) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400877 return {};
878 }
879
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500880 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500881 isProtected, data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400882}
Robert Phillipsb915c942019-12-17 14:44:37 -0500883
884GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
885 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500886 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500887 GrProtected isProtected,
888 const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500889 const GrCaps* caps = this->caps();
890
891 if (!format.isValid()) {
892 return {};
893 }
894
895 SkImage::CompressionType compressionType = caps->compressionType(format);
896 if (compressionType == SkImage::CompressionType::kNone) {
897 // Uncompressed formats must go through the createBackendTexture API
898 return {};
899 }
900
901 if (dimensions.isEmpty() ||
902 dimensions.width() > caps->maxTextureSize() ||
903 dimensions.height() > caps->maxTextureSize()) {
904 return {};
905 }
906
907 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
908 return {};
909 }
910
911 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
912 return {};
913 }
914
Robert Phillips4277f012020-01-21 14:28:34 -0500915 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
916 isProtected, data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500917}
Stephen Whitef3d5d442020-04-08 10:35:58 -0400918
919GrStagingBuffer* GrGpu::findStagingBuffer(size_t size) {
920#ifdef SK_DEBUG
921 this->validateStagingBuffers();
922#endif
923 for (auto b : fActiveStagingBuffers) {
924 if (b->remaining() >= size) {
925 return b;
926 }
927 }
928 for (auto b : fAvailableStagingBuffers) {
929 if (b->remaining() >= size) {
930 fAvailableStagingBuffers.remove(b);
931 fActiveStagingBuffers.addToTail(b);
932 return b;
933 }
934 }
935 size = SkNextPow2(size);
936 size = std::max(size, kMinStagingBufferSize);
937 std::unique_ptr<GrStagingBuffer> b = this->createStagingBuffer(size);
938 GrStagingBuffer* stagingBuffer = b.get();
939 fStagingBuffers.push_back(std::move(b));
940 fActiveStagingBuffers.addToTail(stagingBuffer);
941 return stagingBuffer;
942}
943
944GrStagingBuffer::Slice GrGpu::allocateStagingBufferSlice(size_t size) {
945#ifdef SK_DEBUG
946 this->validateStagingBuffers();
947#endif
948 GrStagingBuffer* stagingBuffer = this->findStagingBuffer(size);
949 return stagingBuffer->allocate(size);
950}
951
952void GrGpu::unmapStagingBuffers() {
953#ifdef SK_DEBUG
954 this->validateStagingBuffers();
955#endif
956 // Unmap all active buffers.
957 for (auto buffer : fActiveStagingBuffers) {
958 buffer->unmap();
959 }
960}
961
962void GrGpu::markStagingBufferAvailable(GrStagingBuffer* buffer) {
963#ifdef SK_DEBUG
964 this->validateStagingBuffers();
965#endif
966 fBusyStagingBuffers.remove(buffer);
967 fAvailableStagingBuffers.addToTail(buffer);
968}