blob: 1f8f5a237e23cdd99c561a5756867a4ab7bf9b9e [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);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500449 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500450 SkASSERT(!surface->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500451
452 if (surface->readOnly()) {
453 return false;
454 }
455
Brian Salomon1047a492019-07-02 12:25:21 -0400456 if (mipLevelCount == 0) {
457 return false;
458 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400459 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400460 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
461 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400462 if (!bounds.contains(subRect)) {
463 return false;
464 }
465 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
466 // We require that if the texels are mipped, than the write region is the entire surface
467 return false;
468 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400469
Brian Salomona56a7462020-02-07 14:17:25 -0500470 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
471 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400472 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800473 }
jvanverth2dc29942015-09-01 07:16:46 -0700474
bsalomon@google.com6f379512011-11-16 20:36:03 +0000475 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400476 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400477 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700478 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400479 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800480 fStats.incTextureUploads();
481 return true;
482 }
483 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000484}
485
Brian Salomone05ba5a2019-04-08 11:59:07 -0400486bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400487 GrColorType textureColorType, GrColorType bufferColorType,
488 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400489 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500490 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700491 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800492
Brian Salomonc67c31c2018-12-06 10:00:03 -0500493 if (texture->readOnly()) {
494 return false;
495 }
496
Greg Daniel660cc992017-06-26 14:55:05 -0400497 // We require that the write region is contained in the texture
498 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
499 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
500 if (!bounds.contains(subRect)) {
501 return false;
502 }
503
Brian Salomonb28cb682019-07-26 12:48:47 -0400504 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400505 if (this->caps()->writePixelsRowBytesSupport()) {
506 if (rowBytes < SkToSizeT(bpp * width)) {
507 return false;
508 }
509 if (rowBytes % bpp) {
510 return false;
511 }
512 } else {
513 if (rowBytes != SkToSizeT(bpp * width)) {
514 return false;
515 }
516 }
517
jvanverth17aa0472016-01-05 10:41:27 -0800518 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400519 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
520 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700521 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500522 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800523 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700524
jvanverth17aa0472016-01-05 10:41:27 -0800525 return true;
526 }
527 return false;
528}
529
Brian Salomon26de56e2019-04-10 12:14:26 -0400530bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400531 GrColorType surfaceColorType, GrColorType bufferColorType,
532 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400533 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400534 SkASSERT(surface);
535 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400536 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400537
Greg Danielba88ab62019-07-26 09:14:01 -0400538#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400539 auto supportedRead = this->caps()->supportedReadPixelsColorType(
540 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400541 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
542 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
543#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400544
545 // We require that the write region is contained in the texture
546 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
547 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
548 if (!bounds.contains(subRect)) {
549 return false;
550 }
551
552 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400553 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
554 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400555 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400556 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400557 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400558 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400559}
560
Brian Salomon930f9392018-06-20 16:25:26 -0400561bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400562 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400563 SkASSERT(texture);
564 SkASSERT(this->caps()->mipMapSupport());
565 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600566 if (!texture->texturePriv().mipMapsAreDirty()) {
567 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
568 // actual target. This may be caused by things that the drawingManager could not predict,
569 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
570 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
571 return true;
572 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500573 if (texture->readOnly()) {
574 return false;
575 }
Brian Salomon930f9392018-06-20 16:25:26 -0400576 if (this->onRegenerateMipMapLevels(texture)) {
577 texture->texturePriv().markMipMapsClean();
578 return true;
579 }
580 return false;
581}
582
Brian Salomon1f05d452019-02-08 12:33:08 -0500583void GrGpu::resetTextureBindings() {
584 this->handleDirtyContext();
585 this->onResetTextureBindings();
586}
587
Chris Dalton16a33c62019-09-24 22:19:17 -0600588void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -0500589 ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000590 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000591 this->handleDirtyContext();
Greg Daniel242536f2020-02-13 14:12:46 -0500592 this->onResolveRenderTarget(target, resolveRect, forExternalIO);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000593}
594
Brian Salomon1fabd512018-02-09 09:54:25 -0500595void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
596 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700597 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500598 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700599 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
600 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700601 GrTexture* texture = surface->asTexture();
602 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400603 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700604 }
605 }
606}
607
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600608int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700609 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600610 SkASSERT(renderTarget->numSamples() > 1 ||
611 (renderTarget->renderTargetPriv().getStencilAttachment() &&
612 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700613
614 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600615 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700616 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
617}
618
Stephen Whitef3d5d442020-04-08 10:35:58 -0400619#ifdef SK_DEBUG
620bool GrGpu::inStagingBuffers(GrStagingBuffer* b) const {
621 for (const auto& i : fStagingBuffers) {
622 if (b == i.get()) {
623 return true;
624 }
625 }
626 return false;
627}
628
629void GrGpu::validateStagingBuffers() const {
630 for (const auto& i : fStagingBuffers) {
631 GrStagingBuffer* buffer = i.get();
632 SkASSERT(fAvailableStagingBuffers.isInList(buffer) ||
633 fActiveStagingBuffers.isInList(buffer) ||
634 fBusyStagingBuffers.isInList(buffer));
635 }
636 for (auto b : fAvailableStagingBuffers) {
637 SkASSERT(this->inStagingBuffers(b));
638 }
639 for (auto b : fActiveStagingBuffers) {
640 SkASSERT(this->inStagingBuffers(b));
641 }
642 for (auto b : fBusyStagingBuffers) {
643 SkASSERT(this->inStagingBuffers(b));
644 }
645}
646#endif
647
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400648GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
649 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500650 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400651 const GrFlushInfo& info,
652 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400653 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Stephen Whitef3d5d442020-04-08 10:35:58 -0400654#ifdef SK_DEBUG
655 this->validateStagingBuffers();
656#endif
Greg Danield2073452018-12-07 11:20:33 -0500657 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500658 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500659
Greg Daniel8561fc22020-04-08 12:52:51 -0400660 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores(
661 new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]);
Greg Daniel30a35e82019-11-19 14:12:25 -0500662 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400663 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400664 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel8561fc22020-04-08 12:52:51 -0400665 semaphores[i] = resourceProvider->wrapBackendSemaphore(
666 info.fSignalSemaphores[i],
667 GrResourceProvider::SemaphoreWrapType::kWillSignal,
668 kBorrow_GrWrapOwnership);
669 this->insertSemaphore(semaphores[i].get());
Greg Daniel51316782017-08-02 15:10:09 +0000670 } else {
Greg Daniel8561fc22020-04-08 12:52:51 -0400671 semaphores[i] = resourceProvider->makeSemaphore(false);
672 if (semaphores[i]) {
673 this->insertSemaphore(semaphores[i].get());
674 info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore();
675 }
Greg Daniel51316782017-08-02 15:10:09 +0000676 }
677 }
678 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500679
Stephen Whitef3d5d442020-04-08 10:35:58 -0400680 this->unmapStagingBuffers();
681
Greg Daniel8561fc22020-04-08 12:52:51 -0400682 if (!this->onFinishFlush(proxies, n, access, info, externalRequests)) {
Greg Daniel30a35e82019-11-19 14:12:25 -0500683 return GrSemaphoresSubmitted::kNo;
684 }
685
Greg Daniel8561fc22020-04-08 12:52:51 -0400686
Stephen Whitef3d5d442020-04-08 10:35:58 -0400687 // Move all active staging buffers to the busy list.
688 while (GrStagingBuffer* buffer = fActiveStagingBuffers.head()) {
689 fActiveStagingBuffers.remove(buffer);
690 fBusyStagingBuffers.addToTail(buffer);
691 }
692
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400693 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000694 : GrSemaphoresSubmitted::kNo;
695}
Brian Osman71a18892017-08-10 10:23:25 -0400696
Kevin Lubickf4def342018-10-04 12:52:50 -0400697#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400698void GrGpu::dumpJSON(SkJSONWriter* writer) const {
699 writer->beginObject();
700
701 // TODO: Is there anything useful in the base class to dump here?
702
703 this->onDumpJSON(writer);
704
705 writer->endObject();
706}
Kevin Lubickf4def342018-10-04 12:52:50 -0400707#else
708void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
709#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400710
Robert Phillipsf0ced622019-05-16 09:06:25 -0400711#if GR_TEST_UTILS
712
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500713#if GR_GPU_STATS
Robert Phillips19f466d2020-02-26 10:27:07 -0500714static const char* cache_result_to_str(int i) {
715 const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = {
716 "hits",
717 "misses",
718 "partials"
719 };
720 static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit);
721 static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss);
722 static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial);
723 static_assert(GrGpu::Stats::kNumProgramCacheResults == 3);
724 return kCacheResultStrings[i];
725}
726
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500727void GrGpu::Stats::dump(SkString* out) {
728 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
729 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
730 out->appendf("Textures Created: %d\n", fTextureCreates);
731 out->appendf("Texture Uploads: %d\n", fTextureUploads);
732 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400733 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500734 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
735 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400736 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillips19f466d2020-02-26 10:27:07 -0500737
738 SkASSERT(fNumInlineCompilationFailures == 0);
739 out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures);
740 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
741 out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i),
742 fInlineProgramCacheStats[i]);
743 }
744
745 SkASSERT(fNumPreCompilationFailures == 0);
746 out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures);
747 for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) {
748 out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i),
749 fPreProgramCacheStats[i]);
750 }
751
752 SkASSERT(fNumCompilationFailures == 0);
753 out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures);
754 out->appendf("Total number of partial compilation successes %d\n",
755 fNumPartialCompilationSuccesses);
756 out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses);
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500757
758 // enable this block to output CSV-style stats for program pre-compilation
759#if 0
760 SkASSERT(fNumInlineCompilationFailures == 0);
761 SkASSERT(fNumPreCompilationFailures == 0);
762 SkASSERT(fNumCompilationFailures == 0);
763 SkASSERT(fNumPartialCompilationSuccesses == 0);
764
765 SkDebugf("%d, %d, %d, %d, %d\n",
766 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
767 fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
768 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit],
769 fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss],
770 fNumCompilationSuccesses);
771#endif
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500772}
773
774void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
775 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
776 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500777}
778
Robert Phillips57ef6802019-09-23 10:12:47 -0400779#endif // GR_GPU_STATS
780#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500781
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500782bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
783 GrMipMapped mipMapped,
784 const BackendTextureData* data) {
785 int numMipLevels = 1;
786 if (mipMapped == GrMipMapped::kYes) {
787 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500788 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400789
Robert Phillips42716d42019-12-16 12:19:54 -0500790 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400791 return true;
792 }
793
Robert Phillips42716d42019-12-16 12:19:54 -0500794 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500795 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500796 }
797
798 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
799
Brian Salomon85c3d682019-11-04 15:04:54 -0500800 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400801 return false;
802 }
803
Brian Salomon85c3d682019-11-04 15:04:54 -0500804 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500805 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500806 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500807 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400808 return false;
809 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500810 if (colorType != data->pixmap(i).colorType()) {
811 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400812 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400813 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400814 return true;
815}
816
Robert Phillipsb915c942019-12-17 14:44:37 -0500817bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
818 GrMipMapped mipMapped, const BackendTextureData* data) {
819
820 if (!data || data->type() == BackendTextureData::Type::kColor) {
821 return true;
822 }
823
824 if (data->type() == BackendTextureData::Type::kPixmaps) {
825 return false;
826 }
827
828 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
829
Robert Phillips99dead92020-01-27 16:11:57 -0500830 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
831 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500832
833 return computedSize == data->compressedSize();
834}
835
Brian Salomon85c3d682019-11-04 15:04:54 -0500836GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
837 const GrBackendFormat& format,
838 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500839 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500840 GrProtected isProtected,
841 const BackendTextureData* data) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400842 const GrCaps* caps = this->caps();
843
844 if (!format.isValid()) {
845 return {};
846 }
847
Robert Phillipsd34691b2019-09-24 13:38:43 -0400848 if (caps->isFormatCompressed(format)) {
849 // Compressed formats must go through the createCompressedBackendTexture API
850 return {};
851 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400852
Brian Salomon85c3d682019-11-04 15:04:54 -0500853 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
854 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
855 if (!caps->areColorTypeAndFormatCompatible(ct, format)) {
856 return {};
857 }
858 }
859
860 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
861 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400862 return {};
863 }
864
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500865 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400866 return {};
867 }
868
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500869 if (!MipMapsAreCorrect(dimensions, mipMapped, data)) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400870 return {};
871 }
872
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500873 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500874 isProtected, data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400875}
Robert Phillipsb915c942019-12-17 14:44:37 -0500876
877GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
878 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500879 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500880 GrProtected isProtected,
881 const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500882 const GrCaps* caps = this->caps();
883
884 if (!format.isValid()) {
885 return {};
886 }
887
888 SkImage::CompressionType compressionType = caps->compressionType(format);
889 if (compressionType == SkImage::CompressionType::kNone) {
890 // Uncompressed formats must go through the createBackendTexture API
891 return {};
892 }
893
894 if (dimensions.isEmpty() ||
895 dimensions.width() > caps->maxTextureSize() ||
896 dimensions.height() > caps->maxTextureSize()) {
897 return {};
898 }
899
900 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
901 return {};
902 }
903
904 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
905 return {};
906 }
907
Robert Phillips4277f012020-01-21 14:28:34 -0500908 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
909 isProtected, data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500910}
Stephen Whitef3d5d442020-04-08 10:35:58 -0400911
912GrStagingBuffer* GrGpu::findStagingBuffer(size_t size) {
913#ifdef SK_DEBUG
914 this->validateStagingBuffers();
915#endif
916 for (auto b : fActiveStagingBuffers) {
917 if (b->remaining() >= size) {
918 return b;
919 }
920 }
921 for (auto b : fAvailableStagingBuffers) {
922 if (b->remaining() >= size) {
923 fAvailableStagingBuffers.remove(b);
924 fActiveStagingBuffers.addToTail(b);
925 return b;
926 }
927 }
928 size = SkNextPow2(size);
929 size = std::max(size, kMinStagingBufferSize);
930 std::unique_ptr<GrStagingBuffer> b = this->createStagingBuffer(size);
931 GrStagingBuffer* stagingBuffer = b.get();
932 fStagingBuffers.push_back(std::move(b));
933 fActiveStagingBuffers.addToTail(stagingBuffer);
934 return stagingBuffer;
935}
936
937GrStagingBuffer::Slice GrGpu::allocateStagingBufferSlice(size_t size) {
938#ifdef SK_DEBUG
939 this->validateStagingBuffers();
940#endif
941 GrStagingBuffer* stagingBuffer = this->findStagingBuffer(size);
942 return stagingBuffer->allocate(size);
943}
944
945void GrGpu::unmapStagingBuffers() {
946#ifdef SK_DEBUG
947 this->validateStagingBuffers();
948#endif
949 // Unmap all active buffers.
950 for (auto buffer : fActiveStagingBuffers) {
951 buffer->unmap();
952 }
953}
954
955void GrGpu::markStagingBufferAvailable(GrStagingBuffer* buffer) {
956#ifdef SK_DEBUG
957 this->validateStagingBuffers();
958#endif
959 fBusyStagingBuffers.remove(buffer);
960 fAvailableStagingBuffers.addToTail(buffer);
961}