blob: b4fa68f0aff4778022b1371e17d0754502d9fdcc [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 Salomona90382f2019-09-17 09:01:56 -0400105static bool validate_texel_levels(int w, int h, GrColorType texelColorType,
106 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 Salomon1047a492019-07-02 12:25:21 -0400111 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
112 if (texels[currentMipLevel].fPixels) {
113 const size_t minRowBytes = w * bpp;
114 if (caps->writePixelsRowBytesSupport()) {
115 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
116 return false;
117 }
118 if (texels[currentMipLevel].fRowBytes % bpp) {
119 return false;
120 }
121 } else {
122 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
123 return false;
124 }
125 }
126 ++levelsWithPixelsCnt;
127 }
128 if (w == 1 && h == 1) {
129 if (currentMipLevel != mipLevelCount - 1) {
130 return false;
131 }
132 } else {
133 w = std::max(w / 2, 1);
134 h = std::max(h / 2, 1);
135 }
136 }
137 // Either just a base layer or a full stack is required.
138 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
139 return false;
140 }
141 // Can specify just the base, all levels, or no levels.
142 if (!hasBasePixels) {
143 return levelsWithPixelsCnt == 0;
144 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400145 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400146}
147
Brian Salomona90382f2019-09-17 09:01:56 -0400148sk_sp<GrTexture> GrGpu::createTextureCommon(const GrSurfaceDesc& desc,
149 const GrBackendFormat& format,
150 GrRenderable renderable,
151 int renderTargetSampleCnt,
152 SkBudgeted budgeted,
153 GrProtected isProtected,
154 int mipLevelCount,
155 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400156 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400157 // Call GrGpu::createCompressedTexture.
158 return nullptr;
159 }
cblume55f2d2d2016-02-26 13:20:48 -0800160
Brian Salomona90382f2019-09-17 09:01:56 -0400161 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel9a48beb2020-01-16 16:57:16 -0500162 if (!this->caps()->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, renderable,
163 renderTargetSampleCnt, mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700164 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700165 }
166
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400167 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400168 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400169 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500170 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400171 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400172 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400173 this->handleDirtyContext();
174 auto tex = this->onCreateTexture(desc,
175 format,
176 renderable,
177 renderTargetSampleCnt,
178 budgeted,
179 isProtected,
180 mipLevelCount,
181 levelClearMask);
182 if (tex) {
183 SkASSERT(tex->backendFormat() == format);
184 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
185 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
186 tex->resourcePriv().removeScratchKey();
187 }
188 fStats.incTextureCreates();
189 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
190 SkASSERT(GrRenderable::kYes == renderable);
191 tex->asRenderTarget()->setRequiresManualMSAAResolve();
192 }
193 }
194 return tex;
195}
196
197sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc,
198 const GrBackendFormat& format,
199 GrRenderable renderable,
200 int renderTargetSampleCnt,
201 GrMipMapped mipMapped,
202 SkBudgeted budgeted,
203 GrProtected isProtected) {
204 int mipLevelCount = 1;
205 if (mipMapped == GrMipMapped::kYes) {
Brian Osman788b9162020-02-07 10:36:46 -0500206 mipLevelCount = 32 - SkCLZ(static_cast<uint32_t>(std::max(desc.fWidth, desc.fHeight)));
Brian Salomona90382f2019-09-17 09:01:56 -0400207 }
208 uint32_t levelClearMask =
209 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
210 auto tex = this->createTextureCommon(desc, format, renderable, renderTargetSampleCnt, budgeted,
211 isProtected, mipLevelCount, levelClearMask);
212 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
213 tex->texturePriv().markMipMapsClean();
214 }
215 return tex;
216}
217
218sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc,
219 const GrBackendFormat& format,
220 GrRenderable renderable,
221 int renderTargetSampleCnt,
222 SkBudgeted budgeted,
223 GrProtected isProtected,
224 GrColorType textureColorType,
225 GrColorType srcColorType,
226 const GrMipLevel texels[],
227 int texelLevelCount) {
228 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400229 if (texelLevelCount) {
Brian Salomona90382f2019-09-17 09:01:56 -0400230 if (!validate_texel_levels(desc.fWidth, desc.fHeight, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400231 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400232 return nullptr;
233 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400234 }
235
Brian Osman788b9162020-02-07 10:36:46 -0500236 int mipLevelCount = std::max(1, texelLevelCount);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400237 uint32_t levelClearMask = 0;
238 if (this->caps()->shouldInitializeTextures()) {
239 if (texelLevelCount) {
240 for (int i = 0; i < mipLevelCount; ++i) {
241 if (!texels->fPixels) {
242 levelClearMask |= static_cast<uint32_t>(1 << i);
243 }
244 }
245 } else {
246 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
247 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400248 }
249
Brian Salomona90382f2019-09-17 09:01:56 -0400250 auto tex = this->createTextureCommon(desc, format, renderable, renderTargetSampleCnt, budgeted,
251 isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800252 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400253 bool markMipLevelsClean = false;
254 // Currently if level 0 does not have pixels then no other level may, as enforced by
255 // validate_texel_levels.
256 if (texelLevelCount && texels[0].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400257 if (!this->writePixels(tex.get(), 0, 0, desc.fWidth, desc.fHeight, textureColorType,
Brian Salomona90382f2019-09-17 09:01:56 -0400258 srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400259 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800260 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400261 // Currently if level[1] of mip map has pixel data then so must all other levels.
262 // as enforced by validate_texel_levels.
263 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
264 fStats.incTextureUploads();
265 } else if (levelClearMask && mipLevelCount > 1) {
266 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800267 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400268 if (markMipLevelsClean) {
269 tex->texturePriv().markMipMapsClean();
270 }
bsalomonb12ea412015-02-02 21:19:50 -0800271 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000272 return tex;
273}
274
Robert Phillips9f744f72019-12-19 19:14:33 -0500275sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400276 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500277 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500278 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500279 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500280 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400281 size_t dataSize) {
282 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500283 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
284 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400285 return nullptr;
286 }
Brian Salomona3e29962019-07-16 11:52:08 -0400287 // Note if we relax the requirement that data must be provided then we must check
288 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400289 if (!data) {
290 return nullptr;
291 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400292 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400293 return nullptr;
294 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500295
296 // TODO: expand CompressedDataIsCorrect to work here too
297 SkImage::CompressionType compressionType = this->caps()->compressionType(format);
298
Robert Phillips99dead92020-01-27 16:11:57 -0500299 if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr,
300 mipMapped == GrMipMapped::kYes)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400301 return nullptr;
302 }
Robert Phillips3a833922020-01-21 15:25:58 -0500303 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
304 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400305}
306
Greg Daniel7ef28f32017-04-20 16:41:55 +0000307sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400308 GrColorType colorType,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500309 GrWrapOwnership ownership, GrWrapCacheable cacheable,
310 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500311 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000312 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400313
314 const GrCaps* caps = this->caps();
315 SkASSERT(caps);
316
Greg Daniel7bfc9132019-08-14 14:23:53 -0400317 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800318 return nullptr;
319 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400320 if (backendTex.width() > caps->maxTextureSize() ||
321 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800322 return nullptr;
323 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400324
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400325 return this->onWrapBackendTexture(backendTex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400326}
Eric Karl5c779752017-05-08 12:02:07 -0700327
Robert Phillipsb915c942019-12-17 14:44:37 -0500328sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
329 GrWrapOwnership ownership,
330 GrWrapCacheable cacheable) {
331 this->handleDirtyContext();
332
333 const GrCaps* caps = this->caps();
334 SkASSERT(caps);
335
336 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
337 return nullptr;
338 }
339 if (backendTex.width() > caps->maxTextureSize() ||
340 backendTex.height() > caps->maxTextureSize()) {
341 return nullptr;
342 }
343
344 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
345}
346
347
Brian Salomond17f6582017-07-19 18:28:58 -0400348sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips0902c982019-07-16 07:47:56 -0400349 int sampleCnt, GrColorType colorType,
350 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500351 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400352 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500353 if (sampleCnt < 1) {
354 return nullptr;
355 }
Robert Phillips0902c982019-07-16 07:47:56 -0400356
357 const GrCaps* caps = this->caps();
358
Greg Daniel7bfc9132019-08-14 14:23:53 -0400359 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400360 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400361 return nullptr;
362 }
363
Robert Phillips0902c982019-07-16 07:47:56 -0400364 if (backendTex.width() > caps->maxRenderTargetSize() ||
365 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400366 return nullptr;
367 }
Robert Phillips0902c982019-07-16 07:47:56 -0400368 sk_sp<GrTexture> tex = this->onWrapRenderableBackendTexture(backendTex, sampleCnt, colorType,
369 ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400370 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600371 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
372 tex->asRenderTarget()->setRequiresManualMSAAResolve();
373 }
bungeman6bd52842016-10-27 09:30:08 -0700374 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000375}
376
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400377sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
378 GrColorType colorType) {
379 this->handleDirtyContext();
380
381 const GrCaps* caps = this->caps();
382
Greg Daniel6fa62e22019-08-07 15:52:37 -0400383 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800384 return nullptr;
385 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400386
Stephen White3c0a50f2020-01-16 18:19:54 -0500387 sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT, colorType);
388 if (backendRT.isFramebufferOnly()) {
389 rt->setFramebufferOnly();
390 }
391 return rt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000392}
393
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400394sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
395 int sampleCnt,
396 GrColorType colorType) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500397 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400398
399 const GrCaps* caps = this->caps();
400
401 int maxSize = caps->maxTextureSize();
402 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
403 return nullptr;
404 }
405
Greg Daniel6fa62e22019-08-07 15:52:37 -0400406 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400407 return nullptr;
408 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400409
Chris Dalton3f7932e2019-08-19 00:39:13 -0600410 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt, colorType);
411 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
412 rt->setRequiresManualMSAAResolve();
413 }
414 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800415}
416
Greg Danielb46add82019-01-02 14:51:29 -0500417sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
418 const GrVkDrawableInfo& vkInfo) {
419 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
420}
421
422sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
423 const GrVkDrawableInfo& vkInfo) {
424 // This is only supported on Vulkan so we default to returning nullptr here
425 return nullptr;
426}
427
Brian Salomondbf70722019-02-07 11:31:24 -0500428sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
429 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400430 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000431 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500432 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700433 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700434 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700435 }
cdalton397536c2016-03-25 12:15:03 -0700436 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800437}
438
Greg Daniel46cfbc62019-06-07 11:43:30 -0400439bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400440 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400441 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700442 SkASSERT(dst && src);
Stephen White3c0a50f2020-01-16 18:19:54 -0500443 SkASSERT(!src->framebufferOnly());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500444
445 if (dst->readOnly()) {
446 return false;
447 }
448
joshualitt1cbdcde2015-08-21 11:53:29 -0700449 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500450
Greg Daniele227fe42019-08-21 13:52:24 -0400451 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700452}
453
Brian Salomona6948702018-06-01 15:33:20 -0400454bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400455 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
456 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400457 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500458 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500459 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400460 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500461
Brian Salomon1d435302019-07-01 13:05:28 -0400462 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
463 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
464 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700465 return false;
466 }
467
Brian Salomon1047a492019-07-02 12:25:21 -0400468 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
469 if (!this->caps()->readPixelsRowBytesSupport()) {
470 if (rowBytes != minRowBytes) {
471 return false;
472 }
473 } else {
474 if (rowBytes < minRowBytes) {
475 return false;
476 }
477 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
478 return false;
479 }
480 }
481
Brian Salomonbf7b6202016-11-11 16:08:03 -0500482 this->handleDirtyContext();
483
Brian Salomonf77c1462019-08-01 15:19:29 -0400484 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
485 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000486}
487
Brian Salomona9b04b92018-06-01 15:04:28 -0400488bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400489 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400490 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400491 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500492 SkASSERT(surface);
Stephen White3c0a50f2020-01-16 18:19:54 -0500493 SkASSERT(!surface->framebufferOnly());
Greg Daniel7bfc9132019-08-14 14:23:53 -0400494 SkASSERT(this->caps()->isFormatTexturableAndUploadable(surfaceColorType,
495 surface->backendFormat()));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500496
497 if (surface->readOnly()) {
498 return false;
499 }
500
Brian Salomon1047a492019-07-02 12:25:21 -0400501 if (mipLevelCount == 0) {
502 return false;
503 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400504 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400505 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
506 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400507 if (!bounds.contains(subRect)) {
508 return false;
509 }
510 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
511 // We require that if the texels are mipped, than the write region is the entire surface
512 return false;
513 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400514
Brian Salomona90382f2019-09-17 09:01:56 -0400515 if (!validate_texel_levels(width, height, srcColorType, texels, mipLevelCount, this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400516 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800517 }
jvanverth2dc29942015-09-01 07:16:46 -0700518
bsalomon@google.com6f379512011-11-16 20:36:03 +0000519 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400520 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400521 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700522 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400523 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800524 fStats.incTextureUploads();
525 return true;
526 }
527 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000528}
529
Brian Salomone05ba5a2019-04-08 11:59:07 -0400530bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400531 GrColorType textureColorType, GrColorType bufferColorType,
532 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400533 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500534 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700535 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400536 SkASSERT(this->caps()->isFormatTexturableAndUploadable(textureColorType,
537 texture->backendFormat()));
jvanverth17aa0472016-01-05 10:41:27 -0800538
Brian Salomonc67c31c2018-12-06 10:00:03 -0500539 if (texture->readOnly()) {
540 return false;
541 }
542
Greg Daniel660cc992017-06-26 14:55:05 -0400543 // We require that the write region is contained in the texture
544 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
545 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
546 if (!bounds.contains(subRect)) {
547 return false;
548 }
549
Brian Salomonb28cb682019-07-26 12:48:47 -0400550 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400551 if (this->caps()->writePixelsRowBytesSupport()) {
552 if (rowBytes < SkToSizeT(bpp * width)) {
553 return false;
554 }
555 if (rowBytes % bpp) {
556 return false;
557 }
558 } else {
559 if (rowBytes != SkToSizeT(bpp * width)) {
560 return false;
561 }
562 }
563
jvanverth17aa0472016-01-05 10:41:27 -0800564 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400565 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
566 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700567 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500568 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800569 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700570
jvanverth17aa0472016-01-05 10:41:27 -0800571 return true;
572 }
573 return false;
574}
575
Brian Salomon26de56e2019-04-10 12:14:26 -0400576bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400577 GrColorType surfaceColorType, GrColorType bufferColorType,
578 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400579 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400580 SkASSERT(surface);
581 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400582 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400583
Greg Danielba88ab62019-07-26 09:14:01 -0400584#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400585 auto supportedRead = this->caps()->supportedReadPixelsColorType(
586 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400587 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
588 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
589#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400590
591 // We require that the write region is contained in the texture
592 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
593 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
594 if (!bounds.contains(subRect)) {
595 return false;
596 }
597
598 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400599 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
600 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400601 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400602 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400603 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400604 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400605}
606
Brian Salomon930f9392018-06-20 16:25:26 -0400607bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400608 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400609 SkASSERT(texture);
610 SkASSERT(this->caps()->mipMapSupport());
611 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600612 if (!texture->texturePriv().mipMapsAreDirty()) {
613 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
614 // actual target. This may be caused by things that the drawingManager could not predict,
615 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
616 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
617 return true;
618 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500619 if (texture->readOnly()) {
620 return false;
621 }
Brian Salomon930f9392018-06-20 16:25:26 -0400622 if (this->onRegenerateMipMapLevels(texture)) {
623 texture->texturePriv().markMipMapsClean();
624 return true;
625 }
626 return false;
627}
628
Brian Salomon1f05d452019-02-08 12:33:08 -0500629void GrGpu::resetTextureBindings() {
630 this->handleDirtyContext();
631 this->onResetTextureBindings();
632}
633
Chris Dalton16a33c62019-09-24 22:19:17 -0600634void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
635 GrSurfaceOrigin origin, ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000636 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000637 this->handleDirtyContext();
Chris Dalton16a33c62019-09-24 22:19:17 -0600638 this->onResolveRenderTarget(target, resolveRect, origin, forExternalIO);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000639}
640
Brian Salomon1fabd512018-02-09 09:54:25 -0500641void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
642 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700643 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500644 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700645 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
646 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700647 GrTexture* texture = surface->asTexture();
648 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400649 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700650 }
651 }
652}
653
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600654int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700655 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600656 SkASSERT(renderTarget->numSamples() > 1 ||
657 (renderTarget->renderTargetPriv().getStencilAttachment() &&
658 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700659
660 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600661 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700662 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
663}
664
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400665GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
666 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500667 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400668 const GrFlushInfo& info,
669 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400670 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500671 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500672 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500673
Greg Daniel30a35e82019-11-19 14:12:25 -0500674 struct SemaphoreInfo {
675 std::unique_ptr<GrSemaphore> fSemaphore;
676 bool fDidCreate = false;
677 };
678
679 bool failedSemaphoreCreation = false;
680 std::unique_ptr<SemaphoreInfo[]> semaphoreInfos(new SemaphoreInfo[info.fNumSemaphores]);
681 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
682 for (int i = 0; i < info.fNumSemaphores && !failedSemaphoreCreation; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400683 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel30a35e82019-11-19 14:12:25 -0500684 semaphoreInfos[i].fSemaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400685 info.fSignalSemaphores[i],
686 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500687 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000688 } else {
Greg Daniel30a35e82019-11-19 14:12:25 -0500689 semaphoreInfos[i].fSemaphore = resourceProvider->makeSemaphore(false);
690 semaphoreInfos[i].fDidCreate = true;
Greg Daniel51316782017-08-02 15:10:09 +0000691 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500692 if (!semaphoreInfos[i].fSemaphore) {
693 semaphoreInfos[i].fDidCreate = false;
694 failedSemaphoreCreation = true;
695 }
696 }
697 if (!failedSemaphoreCreation) {
698 for (int i = 0; i < info.fNumSemaphores && !failedSemaphoreCreation; ++i) {
699 this->insertSemaphore(semaphoreInfos[i].fSemaphore.get());
Greg Daniel51316782017-08-02 15:10:09 +0000700 }
701 }
702 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500703
704 // We always want to try flushing, so do that before checking if we failed semaphore creation.
705 if (!this->onFinishFlush(proxies, n, access, info, externalRequests) ||
706 failedSemaphoreCreation) {
707 // If we didn't do the flush or failed semaphore creations then none of the semaphores were
708 // submitted. Therefore the client can't wait on any of the semaphores. Additionally any
709 // semaphores we created here the client is not responsible for deleting so we must make
710 // sure they get deleted. We do this by changing the ownership from borrowed to owned.
711 for (int i = 0; i < info.fNumSemaphores; ++i) {
712 if (semaphoreInfos[i].fDidCreate) {
713 SkASSERT(semaphoreInfos[i].fSemaphore);
714 semaphoreInfos[i].fSemaphore->setIsOwned();
715 }
716 }
717 return GrSemaphoresSubmitted::kNo;
718 }
719
720 for (int i = 0; i < info.fNumSemaphores; ++i) {
721 if (!info.fSignalSemaphores[i].isInitialized()) {
722 SkASSERT(semaphoreInfos[i].fSemaphore);
723 info.fSignalSemaphores[i] = semaphoreInfos[i].fSemaphore->backendSemaphore();
724 }
725 }
726
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400727 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000728 : GrSemaphoresSubmitted::kNo;
729}
Brian Osman71a18892017-08-10 10:23:25 -0400730
Kevin Lubickf4def342018-10-04 12:52:50 -0400731#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400732void GrGpu::dumpJSON(SkJSONWriter* writer) const {
733 writer->beginObject();
734
735 // TODO: Is there anything useful in the base class to dump here?
736
737 this->onDumpJSON(writer);
738
739 writer->endObject();
740}
Kevin Lubickf4def342018-10-04 12:52:50 -0400741#else
742void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
743#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400744
Robert Phillipsf0ced622019-05-16 09:06:25 -0400745#if GR_TEST_UTILS
746
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500747#if GR_GPU_STATS
748void GrGpu::Stats::dump(SkString* out) {
749 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
750 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
751 out->appendf("Textures Created: %d\n", fTextureCreates);
752 out->appendf("Texture Uploads: %d\n", fTextureUploads);
753 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400754 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500755 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
756 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400757 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500758}
759
760void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
761 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
762 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500763}
764
Robert Phillips57ef6802019-09-23 10:12:47 -0400765#endif // GR_GPU_STATS
766#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500767
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500768bool GrGpu::MipMapsAreCorrect(SkISize dimensions,
769 GrMipMapped mipMapped,
770 const BackendTextureData* data) {
771 int numMipLevels = 1;
772 if (mipMapped == GrMipMapped::kYes) {
773 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Brian Salomon85c3d682019-11-04 15:04:54 -0500774 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400775
Robert Phillips42716d42019-12-16 12:19:54 -0500776 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400777 return true;
778 }
779
Robert Phillips42716d42019-12-16 12:19:54 -0500780 if (data->type() == BackendTextureData::Type::kCompressed) {
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500781 return false; // This should be going through CompressedDataIsCorrect
Robert Phillips42716d42019-12-16 12:19:54 -0500782 }
783
784 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
785
Brian Salomon85c3d682019-11-04 15:04:54 -0500786 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400787 return false;
788 }
789
Brian Salomon85c3d682019-11-04 15:04:54 -0500790 SkColorType colorType = data->pixmap(0).colorType();
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500791 for (int i = 1; i < numMipLevels; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500792 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -0500793 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400794 return false;
795 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500796 if (colorType != data->pixmap(i).colorType()) {
797 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400798 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400799 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400800 return true;
801}
802
Robert Phillipsb915c942019-12-17 14:44:37 -0500803bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
804 GrMipMapped mipMapped, const BackendTextureData* data) {
805
806 if (!data || data->type() == BackendTextureData::Type::kColor) {
807 return true;
808 }
809
810 if (data->type() == BackendTextureData::Type::kPixmaps) {
811 return false;
812 }
813
814 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
815
Robert Phillips99dead92020-01-27 16:11:57 -0500816 size_t computedSize = SkCompressedDataSize(compressionType, dimensions,
817 nullptr, mipMapped == GrMipMapped::kYes);
Robert Phillipsb915c942019-12-17 14:44:37 -0500818
819 return computedSize == data->compressedSize();
820}
821
Brian Salomon85c3d682019-11-04 15:04:54 -0500822GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
823 const GrBackendFormat& format,
824 GrRenderable renderable,
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500825 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500826 GrProtected isProtected,
827 const BackendTextureData* data) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400828 const GrCaps* caps = this->caps();
829
830 if (!format.isValid()) {
831 return {};
832 }
833
Robert Phillipsd34691b2019-09-24 13:38:43 -0400834 if (caps->isFormatCompressed(format)) {
835 // Compressed formats must go through the createCompressedBackendTexture API
836 return {};
837 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400838
Brian Salomon85c3d682019-11-04 15:04:54 -0500839 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
840 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
841 if (!caps->areColorTypeAndFormatCompatible(ct, format)) {
842 return {};
843 }
844 }
845
846 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
847 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400848 return {};
849 }
850
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500851 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400852 return {};
853 }
854
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500855 if (!MipMapsAreCorrect(dimensions, mipMapped, data)) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400856 return {};
857 }
858
Robert Phillipsba5c7ad2020-01-24 11:03:33 -0500859 return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500860 isProtected, data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400861}
Robert Phillipsb915c942019-12-17 14:44:37 -0500862
863GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
864 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500865 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500866 GrProtected isProtected,
867 const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500868 const GrCaps* caps = this->caps();
869
870 if (!format.isValid()) {
871 return {};
872 }
873
874 SkImage::CompressionType compressionType = caps->compressionType(format);
875 if (compressionType == SkImage::CompressionType::kNone) {
876 // Uncompressed formats must go through the createBackendTexture API
877 return {};
878 }
879
880 if (dimensions.isEmpty() ||
881 dimensions.width() > caps->maxTextureSize() ||
882 dimensions.height() > caps->maxTextureSize()) {
883 return {};
884 }
885
886 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
887 return {};
888 }
889
890 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
891 return {};
892 }
893
Robert Phillips4277f012020-01-21 14:28:34 -0500894 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
895 isProtected, data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500896}