blob: e46052b921926d1ffeb8e1ad38a376e01253abd5 [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"
22#include "src/gpu/GrMesh.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060023#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrPathRendering.h"
25#include "src/gpu/GrPipeline.h"
26#include "src/gpu/GrRenderTargetPriv.h"
27#include "src/gpu/GrResourceCache.h"
28#include "src/gpu/GrResourceProvider.h"
29#include "src/gpu/GrSemaphore.h"
30#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
bsalomon@google.comd302f142011-03-03 13:54:13 +000038////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000039
Brian Salomone2826ab2019-06-04 15:58:31 -040040GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000041
bsalomoned0bcad2015-05-04 10:36:42 -070042GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070043
bsalomon6e2aad42016-04-01 11:54:31 -070044void GrGpu::disconnect(DisconnectType) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000045
bsalomon@google.comd302f142011-03-03 13:54:13 +000046////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000047
Brian Salomon1a217eb2019-10-24 10:50:36 -040048bool GrGpu::IsACopyNeededForRepeatWrapMode(const GrCaps* caps,
49 GrTextureProxy* texProxy,
50 SkISize dimensions,
Greg Daniel8f5bbda2018-06-08 17:22:23 -040051 GrSamplerState::Filter filter,
52 GrTextureProducer::CopyParams* copyParams,
53 SkScalar scaleAdjust[2]) {
54 if (!caps->npotTextureTileSupport() &&
Brian Salomon1a217eb2019-10-24 10:50:36 -040055 (!SkIsPow2(dimensions.width()) || !SkIsPow2(dimensions.height()))) {
Greg Daniel09c94002018-06-08 22:11:51 +000056 SkASSERT(scaleAdjust);
Brian Salomon1a217eb2019-10-24 10:50:36 -040057 copyParams->fDimensions = {SkNextPow2(dimensions.width()), SkNextPow2(dimensions.height())};
Greg Daniel09c94002018-06-08 22:11:51 +000058 SkASSERT(scaleAdjust);
Brian Salomon1a217eb2019-10-24 10:50:36 -040059 scaleAdjust[0] = ((SkScalar)copyParams->fDimensions.width()) / dimensions.width();
60 scaleAdjust[1] = ((SkScalar)copyParams->fDimensions.height()) / dimensions.height();
Greg Daniel8f5bbda2018-06-08 17:22:23 -040061 switch (filter) {
Greg Daniel09c94002018-06-08 22:11:51 +000062 case GrSamplerState::Filter::kNearest:
63 copyParams->fFilter = GrSamplerState::Filter::kNearest;
64 break;
65 case GrSamplerState::Filter::kBilerp:
66 case GrSamplerState::Filter::kMipMap:
67 // We are only ever scaling up so no reason to ever indicate kMipMap.
68 copyParams->fFilter = GrSamplerState::Filter::kBilerp;
69 break;
70 }
71 return true;
72 }
Robert Phillipsabf7b762018-03-21 12:13:37 -040073
74 if (texProxy) {
75 // If the texture format itself doesn't support repeat wrap mode or mipmapping (and
76 // those capabilities are required) force a copy.
Brian Salomonfd98c2c2018-07-31 17:25:29 -040077 if (texProxy->hasRestrictedSampling()) {
Robert Phillipsabf7b762018-03-21 12:13:37 -040078 copyParams->fFilter = GrSamplerState::Filter::kNearest;
Brian Salomon1a217eb2019-10-24 10:50:36 -040079 copyParams->fDimensions = texProxy->dimensions();
Robert Phillipsabf7b762018-03-21 12:13:37 -040080 return true;
81 }
82 }
83
bsalomon100b8f82015-10-28 08:37:44 -070084 return false;
bsalomon045802d2015-10-20 07:58:01 -070085}
86
Greg Daniel8f5bbda2018-06-08 17:22:23 -040087bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
88 GrSamplerState::Filter filter,
89 GrTextureProducer::CopyParams* copyParams) {
90 SkASSERT(texProxy);
Greg Danielcc104db2020-02-03 14:17:08 -050091 int mipCount = SkMipMap::ComputeLevelCount(texProxy->width(), texProxy->height());
92 bool willNeedMips = GrSamplerState::Filter::kMipMap == filter && caps->mipMapSupport() &&
93 mipCount;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040094 // If the texture format itself doesn't support mipmapping (and those capabilities are required)
95 // force a copy.
96 if (willNeedMips && texProxy->mipMapped() == GrMipMapped::kNo) {
97 copyParams->fFilter = GrSamplerState::Filter::kNearest;
Brian Salomon1a217eb2019-10-24 10:50:36 -040098 copyParams->fDimensions = texProxy->dimensions();
Greg Daniel8f5bbda2018-06-08 17:22:23 -040099 return true;
100 }
101
102 return false;
103}
104
Brian Salomona56a7462020-02-07 14:17:25 -0500105static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType,
Brian Salomona90382f2019-09-17 09:01:56 -0400106 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -0400107 SkASSERT(mipLevelCount > 0);
108 bool hasBasePixels = texels[0].fPixels;
109 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -0400110 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomona56a7462020-02-07 14:17:25 -0500111 int w = dimensions.fWidth;
112 int h = dimensions.fHeight;
Brian Salomon1047a492019-07-02 12:25:21 -0400113 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
114 if (texels[currentMipLevel].fPixels) {
115 const size_t minRowBytes = w * bpp;
116 if (caps->writePixelsRowBytesSupport()) {
117 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
118 return false;
119 }
120 if (texels[currentMipLevel].fRowBytes % bpp) {
121 return false;
122 }
123 } else {
124 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
125 return false;
126 }
127 }
128 ++levelsWithPixelsCnt;
129 }
130 if (w == 1 && h == 1) {
131 if (currentMipLevel != mipLevelCount - 1) {
132 return false;
133 }
134 } else {
135 w = std::max(w / 2, 1);
136 h = std::max(h / 2, 1);
137 }
138 }
139 // Either just a base layer or a full stack is required.
140 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
141 return false;
142 }
143 // Can specify just the base, all levels, or no levels.
144 if (!hasBasePixels) {
145 return levelsWithPixelsCnt == 0;
146 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400147 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400148}
149
Brian Salomona56a7462020-02-07 14:17:25 -0500150sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400151 const GrBackendFormat& format,
152 GrRenderable renderable,
153 int renderTargetSampleCnt,
154 SkBudgeted budgeted,
155 GrProtected isProtected,
156 int mipLevelCount,
157 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400158 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400159 // Call GrGpu::createCompressedTexture.
160 return nullptr;
161 }
cblume55f2d2d2016-02-26 13:20:48 -0800162
Brian Salomona90382f2019-09-17 09:01:56 -0400163 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomona56a7462020-02-07 14:17:25 -0500164 if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt,
165 mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700166 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700167 }
168
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400169 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400170 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400171 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500172 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400173 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400174 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400175 this->handleDirtyContext();
Brian Salomona56a7462020-02-07 14:17:25 -0500176 auto tex = this->onCreateTexture(dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400177 format,
178 renderable,
179 renderTargetSampleCnt,
180 budgeted,
181 isProtected,
182 mipLevelCount,
183 levelClearMask);
184 if (tex) {
185 SkASSERT(tex->backendFormat() == format);
186 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
187 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
188 tex->resourcePriv().removeScratchKey();
189 }
190 fStats.incTextureCreates();
191 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
192 SkASSERT(GrRenderable::kYes == renderable);
193 tex->asRenderTarget()->setRequiresManualMSAAResolve();
194 }
195 }
196 return tex;
197}
198
Brian Salomona56a7462020-02-07 14:17:25 -0500199sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400200 const GrBackendFormat& format,
201 GrRenderable renderable,
202 int renderTargetSampleCnt,
203 GrMipMapped mipMapped,
204 SkBudgeted budgeted,
205 GrProtected isProtected) {
206 int mipLevelCount = 1;
207 if (mipMapped == GrMipMapped::kYes) {
Brian Salomona56a7462020-02-07 14:17:25 -0500208 mipLevelCount =
209 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400210 }
211 uint32_t levelClearMask =
212 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
Brian Salomona56a7462020-02-07 14:17:25 -0500213 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
214 budgeted, isProtected, mipLevelCount, levelClearMask);
Brian Salomona90382f2019-09-17 09:01:56 -0400215 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
216 tex->texturePriv().markMipMapsClean();
217 }
218 return tex;
219}
220
Brian Salomona56a7462020-02-07 14:17:25 -0500221sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions,
Brian Salomona90382f2019-09-17 09:01:56 -0400222 const GrBackendFormat& format,
223 GrRenderable renderable,
224 int renderTargetSampleCnt,
225 SkBudgeted budgeted,
226 GrProtected isProtected,
227 GrColorType textureColorType,
228 GrColorType srcColorType,
229 const GrMipLevel texels[],
230 int texelLevelCount) {
231 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400232 if (texelLevelCount) {
Brian Salomona56a7462020-02-07 14:17:25 -0500233 if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400234 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400235 return nullptr;
236 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400237 }
238
Brian Osman788b9162020-02-07 10:36:46 -0500239 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400240 uint32_t levelClearMask = 0;
241 if (this->caps()->shouldInitializeTextures()) {
242 if (texelLevelCount) {
243 for (int i = 0; i < mipLevelCount; ++i) {
244 if (!texels->fPixels) {
245 levelClearMask |= static_cast<uint32_t>(1 << i);
246 }
247 }
248 } else {
249 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
250 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400251 }
252
Brian Salomona56a7462020-02-07 14:17:25 -0500253 auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt,
254 budgeted, isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800255 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400256 bool markMipLevelsClean = false;
257 // Currently if level 0 does not have pixels then no other level may, as enforced by
258 // validate_texel_levels.
259 if (texelLevelCount && texels[0].fPixels) {
Brian Salomona56a7462020-02-07 14:17:25 -0500260 if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight,
261 textureColorType, srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400262 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800263 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400264 // Currently if level[1] of mip map has pixel data then so must all other levels.
265 // as enforced by validate_texel_levels.
266 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
267 fStats.incTextureUploads();
268 } else if (levelClearMask && mipLevelCount > 1) {
269 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800270 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400271 if (markMipLevelsClean) {
272 tex->texturePriv().markMipMapsClean();
273 }
bsalomonb12ea412015-02-02 21:19:50 -0800274 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000275 return tex;
276}
277
Robert Phillips9f744f72019-12-19 19:14:33 -0500278sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400279 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500280 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500281 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500282 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500283 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400284 size_t dataSize) {
285 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500286 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
287 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400288 return nullptr;
289 }
Brian Salomona3e29962019-07-16 11:52:08 -0400290 // Note if we relax the requirement that data must be provided then we must check
291 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400292 if (!data) {
293 return nullptr;
294 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400295 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400296 return nullptr;
297 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500298
299 // TODO: expand CompressedDataIsCorrect to work here too
300 SkImage::CompressionType compressionType = this->caps()->compressionType(format);
301
Robert Phillips99dead92020-01-27 16:11:57 -0500302 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
303 mipMapped == GrMipMapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400304 return nullptr;
305 }
Robert Phillips3a833922020-01-21 15:25:58 -0500306 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
307 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400308}
309
Greg Daniel7ef28f32017-04-20 16:41:55 +0000310sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400311 GrColorType colorType,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500312 GrWrapOwnership ownership, GrWrapCacheable cacheable,
313 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500314 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000315 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400316
317 const GrCaps* caps = this->caps();
318 SkASSERT(caps);
319
Greg Daniel7bfc9132019-08-14 14:23:53 -0400320 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800321 return nullptr;
322 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400323 if (backendTex.width() > caps->maxTextureSize() ||
324 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800325 return nullptr;
326 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400327
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400328 return this->onWrapBackendTexture(backendTex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400329}
Eric Karl5c779752017-05-08 12:02:07 -0700330
Robert Phillipsb915c942019-12-17 14:44:37 -0500331sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
332 GrWrapOwnership ownership,
333 GrWrapCacheable cacheable) {
334 this->handleDirtyContext();
335
336 const GrCaps* caps = this->caps();
337 SkASSERT(caps);
338
339 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
340 return nullptr;
341 }
342 if (backendTex.width() > caps->maxTextureSize() ||
343 backendTex.height() > caps->maxTextureSize()) {
344 return nullptr;
345 }
346
347 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
348}
349
350
Brian Salomond17f6582017-07-19 18:28:58 -0400351sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips0902c982019-07-16 07:47:56 -0400352 int sampleCnt, GrColorType colorType,
353 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500354 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400355 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500356 if (sampleCnt < 1) {
357 return nullptr;
358 }
Robert Phillips0902c982019-07-16 07:47:56 -0400359
360 const GrCaps* caps = this->caps();
361
Greg Daniel7bfc9132019-08-14 14:23:53 -0400362 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400363 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400364 return nullptr;
365 }
366
Robert Phillips0902c982019-07-16 07:47:56 -0400367 if (backendTex.width() > caps->maxRenderTargetSize() ||
368 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400369 return nullptr;
370 }
Robert Phillips0902c982019-07-16 07:47:56 -0400371 sk_sp<GrTexture> tex = this->onWrapRenderableBackendTexture(backendTex, sampleCnt, colorType,
372 ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400373 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600374 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
375 tex->asRenderTarget()->setRequiresManualMSAAResolve();
376 }
bungeman6bd52842016-10-27 09:30:08 -0700377 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000378}
379
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400380sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
381 GrColorType colorType) {
382 this->handleDirtyContext();
383
384 const GrCaps* caps = this->caps();
385
Greg Daniel6fa62e22019-08-07 15:52:37 -0400386 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800387 return nullptr;
388 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400389
Stephen White3c0a50f2020-01-16 18:19:54 -0500390 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT, colorType);
391 if (backendRT.isFramebufferOnly()) {
392 rt->setFramebufferOnly();
393 }
394 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000395}
396
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400397sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
398 int sampleCnt,
399 GrColorType colorType) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500400 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400401
402 const GrCaps* caps = this->caps();
403
404 int maxSize = caps->maxTextureSize();
405 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
406 return nullptr;
407 }
408
Greg Daniel6fa62e22019-08-07 15:52:37 -0400409 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400410 return nullptr;
411 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400412
Chris Dalton3f7932e2019-08-19 00:39:13 -0600413 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt, colorType);
414 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
415 rt->setRequiresManualMSAAResolve();
416 }
417 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800418}
419
Greg Danielb46add82019-01-02 14:51:29 -0500420sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
421 const GrVkDrawableInfo& vkInfo) {
422 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
423}
424
425sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
426 const GrVkDrawableInfo& vkInfo) {
427 // This is only supported on Vulkan so we default to returning nullptr here
428 return nullptr;
429}
430
Brian Salomondbf70722019-02-07 11:31:24 -0500431sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
432 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400433 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000434 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500435 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700436 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700437 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700438 }
cdalton397536c2016-03-25 12:15:03 -0700439 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800440}
441
Greg Daniel46cfbc62019-06-07 11:43:30 -0400442bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400443 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400444 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700445 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500446 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500447
448 if (dst->readOnly()) {
449 return false;
450 }
451
joshualitt1cbdcde2015-08-21 11:53:29 -0700452 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500453
Greg Daniele227fe42019-08-21 13:52:24 -0400454 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700455}
456
Brian Salomona6948702018-06-01 15:33:20 -0400457bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400458 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
459 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400460 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500461 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500462 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400463 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500464
Brian Salomon1d435302019-07-01 13:05:28 -0400465 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
466 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
467 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700468 return false;
469 }
470
Brian Salomon1047a492019-07-02 12:25:21 -0400471 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
472 if (!this->caps()->readPixelsRowBytesSupport()) {
473 if (rowBytes != minRowBytes) {
474 return false;
475 }
476 } else {
477 if (rowBytes < minRowBytes) {
478 return false;
479 }
480 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
481 return false;
482 }
483 }
484
Brian Salomonbf7b6202016-11-11 16:08:03 -0500485 this->handleDirtyContext();
486
Brian Salomonf77c1462019-08-01 15:19:29 -0400487 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
488 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000489}
490
Brian Salomona9b04b92018-06-01 15:04:28 -0400491bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400492 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400493 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400494 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500495 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500496 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400497 SkASSERT(this->caps()->isFormatTexturableAndUploadable(surfaceColorType,
498 surface->backendFormat()));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500499
500 if (surface->readOnly()) {
501 return false;
502 }
503
Brian Salomon1047a492019-07-02 12:25:21 -0400504 if (mipLevelCount == 0) {
505 return false;
506 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400507 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400508 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
509 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400510 if (!bounds.contains(subRect)) {
511 return false;
512 }
513 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
514 // We require that if the texels are mipped, than the write region is the entire surface
515 return false;
516 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400517
Brian Salomona56a7462020-02-07 14:17:25 -0500518 if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount,
519 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400520 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800521 }
jvanverth2dc29942015-09-01 07:16:46 -0700522
bsalomon@google.com6f379512011-11-16 20:36:03 +0000523 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400524 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400525 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700526 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400527 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800528 fStats.incTextureUploads();
529 return true;
530 }
531 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000532}
533
Brian Salomone05ba5a2019-04-08 11:59:07 -0400534bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400535 GrColorType textureColorType, GrColorType bufferColorType,
536 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400537 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500538 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700539 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400540 SkASSERT(this->caps()->isFormatTexturableAndUploadable(textureColorType,
541 texture->backendFormat()));
jvanverth17aa0472016-01-05 10:41:27 -0800542
Brian Salomonc67c31c2018-12-06 10:00:03 -0500543 if (texture->readOnly()) {
544 return false;
545 }
546
Greg Daniel660cc992017-06-26 14:55:05 -0400547 // We require that the write region is contained in the texture
548 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
549 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
550 if (!bounds.contains(subRect)) {
551 return false;
552 }
553
Brian Salomonb28cb682019-07-26 12:48:47 -0400554 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400555 if (this->caps()->writePixelsRowBytesSupport()) {
556 if (rowBytes < SkToSizeT(bpp * width)) {
557 return false;
558 }
559 if (rowBytes % bpp) {
560 return false;
561 }
562 } else {
563 if (rowBytes != SkToSizeT(bpp * width)) {
564 return false;
565 }
566 }
567
jvanverth17aa0472016-01-05 10:41:27 -0800568 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400569 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
570 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700571 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500572 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800573 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700574
jvanverth17aa0472016-01-05 10:41:27 -0800575 return true;
576 }
577 return false;
578}
579
Brian Salomon26de56e2019-04-10 12:14:26 -0400580bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400581 GrColorType surfaceColorType, GrColorType bufferColorType,
582 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400583 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400584 SkASSERT(surface);
585 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400586 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400587
Greg Danielba88ab62019-07-26 09:14:01 -0400588#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400589 auto supportedRead = this->caps()->supportedReadPixelsColorType(
590 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400591 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
592 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
593#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400594
595 // We require that the write region is contained in the texture
596 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
597 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
598 if (!bounds.contains(subRect)) {
599 return false;
600 }
601
602 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400603 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
604 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400605 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400606 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400607 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400608 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400609}
610
Brian Salomon930f9392018-06-20 16:25:26 -0400611bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400612 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400613 SkASSERT(texture);
614 SkASSERT(this->caps()->mipMapSupport());
615 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600616 if (!texture->texturePriv().mipMapsAreDirty()) {
617 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
618 // actual target. This may be caused by things that the drawingManager could not predict,
619 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
620 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
621 return true;
622 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500623 if (texture->readOnly()) {
624 return false;
625 }
Brian Salomon930f9392018-06-20 16:25:26 -0400626 if (this->onRegenerateMipMapLevels(texture)) {
627 texture->texturePriv().markMipMapsClean();
628 return true;
629 }
630 return false;
631}
632
Brian Salomon1f05d452019-02-08 12:33:08 -0500633void GrGpu::resetTextureBindings() {
634 this->handleDirtyContext();
635 this->onResetTextureBindings();
636}
637
Chris Dalton16a33c62019-09-24 22:19:17 -0600638void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
639 GrSurfaceOrigin origin, ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000640 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000641 this->handleDirtyContext();
Chris Dalton16a33c62019-09-24 22:19:17 -0600642 this->onResolveRenderTarget(target, resolveRect, origin, forExternalIO);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000643}
644
Brian Salomon1fabd512018-02-09 09:54:25 -0500645void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
646 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700647 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500648 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700649 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
650 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700651 GrTexture* texture = surface->asTexture();
652 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400653 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700654 }
655 }
656}
657
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600658int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700659 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600660 SkASSERT(renderTarget->numSamples() > 1 ||
661 (renderTarget->renderTargetPriv().getStencilAttachment() &&
662 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700663
664 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600665 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700666 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
667}
668
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400669GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
670 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500671 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400672 const GrFlushInfo& info,
673 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400674 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500675 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500676 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500677
Greg Daniel30a35e82019-11-19 14:12:25 -0500678 struct SemaphoreInfo {
679 std::unique_ptr<GrSemaphore> fSemaphore;
680 bool fDidCreate = false;
681 };
682
683 bool failedSemaphoreCreation = false;
684 std::unique_ptr<SemaphoreInfo[]> semaphoreInfos(new SemaphoreInfo[info.fNumSemaphores]);
685 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
686 for (int i = 0; i < info.fNumSemaphores && !failedSemaphoreCreation; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400687 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel30a35e82019-11-19 14:12:25 -0500688 semaphoreInfos[i].fSemaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400689 info.fSignalSemaphores[i],
690 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500691 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000692 } else {
Greg Daniel30a35e82019-11-19 14:12:25 -0500693 semaphoreInfos[i].fSemaphore = resourceProvider->makeSemaphore(false);
694 semaphoreInfos[i].fDidCreate = true;
Greg Daniel51316782017-08-02 15:10:09 +0000695 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500696 if (!semaphoreInfos[i].fSemaphore) {
697 semaphoreInfos[i].fDidCreate = false;
698 failedSemaphoreCreation = true;
699 }
700 }
701 if (!failedSemaphoreCreation) {
702 for (int i = 0; i < info.fNumSemaphores && !failedSemaphoreCreation; ++i) {
703 this->insertSemaphore(semaphoreInfos[i].fSemaphore.get());
Greg Daniel51316782017-08-02 15:10:09 +0000704 }
705 }
706 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500707
708 // We always want to try flushing, so do that before checking if we failed semaphore creation.
709 if (!this->onFinishFlush(proxies, n, access, info, externalRequests) ||
710 failedSemaphoreCreation) {
711 // If we didn't do the flush or failed semaphore creations then none of the semaphores were
712 // submitted. Therefore the client can't wait on any of the semaphores. Additionally any
713 // semaphores we created here the client is not responsible for deleting so we must make
714 // sure they get deleted. We do this by changing the ownership from borrowed to owned.
715 for (int i = 0; i < info.fNumSemaphores; ++i) {
716 if (semaphoreInfos[i].fDidCreate) {
717 SkASSERT(semaphoreInfos[i].fSemaphore);
718 semaphoreInfos[i].fSemaphore->setIsOwned();
719 }
720 }
721 return GrSemaphoresSubmitted::kNo;
722 }
723
724 for (int i = 0; i < info.fNumSemaphores; ++i) {
725 if (!info.fSignalSemaphores[i].isInitialized()) {
726 SkASSERT(semaphoreInfos[i].fSemaphore);
727 info.fSignalSemaphores[i] = semaphoreInfos[i].fSemaphore->backendSemaphore();
728 }
729 }
730
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400731 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000732 : GrSemaphoresSubmitted::kNo;
733}
Brian Osman71a18892017-08-10 10:23:25 -0400734
Kevin Lubickf4def342018-10-04 12:52:50 -0400735#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400736void GrGpu::dumpJSON(SkJSONWriter* writer) const {
737 writer->beginObject();
738
739 // TODO: Is there anything useful in the base class to dump here?
740
741 this->onDumpJSON(writer);
742
743 writer->endObject();
744}
Kevin Lubickf4def342018-10-04 12:52:50 -0400745#else
746void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
747#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400748
Robert Phillipsf0ced622019-05-16 09:06:25 -0400749#if GR_TEST_UTILS
750
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500751#if GR_GPU_STATS
752void GrGpu::Stats::dump(SkString* out) {
753 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
754 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
755 out->appendf("Textures Created: %d\n", fTextureCreates);
756 out->appendf("Texture Uploads: %d\n", fTextureUploads);
757 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400758 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500759 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
760 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400761 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500762}
763
764void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
765 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
766 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500767}
768
Robert Phillips57ef6802019-09-23 10:12:47 -0400769#endif // GR_GPU_STATS
770#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500771
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500772bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
773 GrMipMapped mipMapped,
774 const BackendTextureData* data) {
775 int numMipLevels = 1;
776 if (mipMapped == GrMipMapped::kYes) {
777 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500778 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400779
Robert Phillips42716d42019-12-16 12:19:54 -0500780 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400781 return true;
782 }
783
Robert Phillips42716d42019-12-16 12:19:54 -0500784 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500785 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500786 }
787
788 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
789
Brian Salomon85c3d682019-11-04 15:04:54 -0500790 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400791 return false;
792 }
793
Brian Salomon85c3d682019-11-04 15:04:54 -0500794 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500795 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500796 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500797 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400798 return false;
799 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500800 if (colorType != data->pixmap(i).colorType()) {
801 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400802 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400803 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400804 return true;
805}
806
Robert Phillipsb915c942019-12-17 14:44:37 -0500807bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
808 GrMipMapped mipMapped, const BackendTextureData* data) {
809
810 if (!data || data->type() == BackendTextureData::Type::kColor) {
811 return true;
812 }
813
814 if (data->type() == BackendTextureData::Type::kPixmaps) {
815 return false;
816 }
817
818 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
819
Robert Phillips99dead92020-01-27 16:11:57 -0500820 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
821 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500822
823 return computedSize == data->compressedSize();
824}
825
Brian Salomon85c3d682019-11-04 15:04:54 -0500826GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
827 const GrBackendFormat& format,
828 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500829 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500830 GrProtected isProtected,
831 const BackendTextureData* data) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400832 const GrCaps* caps = this->caps();
833
834 if (!format.isValid()) {
835 return {};
836 }
837
Robert Phillipsd34691b2019-09-24 13:38:43 -0400838 if (caps->isFormatCompressed(format)) {
839 // Compressed formats must go through the createCompressedBackendTexture API
840 return {};
841 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400842
Brian Salomon85c3d682019-11-04 15:04:54 -0500843 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
844 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
845 if (!caps->areColorTypeAndFormatCompatible(ct, format)) {
846 return {};
847 }
848 }
849
850 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
851 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400852 return {};
853 }
854
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500855 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400856 return {};
857 }
858
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500859 if (!MipMapsAreCorrect(dimensions, mipMapped, data)) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400860 return {};
861 }
862
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500863 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500864 isProtected, data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400865}
Robert Phillipsb915c942019-12-17 14:44:37 -0500866
867GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
868 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500869 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500870 GrProtected isProtected,
871 const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500872 const GrCaps* caps = this->caps();
873
874 if (!format.isValid()) {
875 return {};
876 }
877
878 SkImage::CompressionType compressionType = caps->compressionType(format);
879 if (compressionType == SkImage::CompressionType::kNone) {
880 // Uncompressed formats must go through the createBackendTexture API
881 return {};
882 }
883
884 if (dimensions.isEmpty() ||
885 dimensions.width() > caps->maxTextureSize() ||
886 dimensions.height() > caps->maxTextureSize()) {
887 return {};
888 }
889
890 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
891 return {};
892 }
893
894 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
895 return {};
896 }
897
Robert Phillips4277f012020-01-21 14:28:34 -0500898 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
899 isProtected, data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500900}