blob: cb80efa9b78734c76b71f07b1931db29dcb111c5 [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"
14#include "src/core/SkMathPriv.h"
Robert Phillips57ef6802019-09-23 10:12:47 -040015#include "src/core/SkMipMap.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrCaps.h"
18#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040019#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrGpuResourcePriv.h"
21#include "src/gpu/GrMesh.h"
Chris Dalton16a33c62019-09-24 22:19:17 -060022#include "src/gpu/GrNativeRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrPathRendering.h"
24#include "src/gpu/GrPipeline.h"
25#include "src/gpu/GrRenderTargetPriv.h"
26#include "src/gpu/GrResourceCache.h"
27#include "src/gpu/GrResourceProvider.h"
28#include "src/gpu/GrSemaphore.h"
29#include "src/gpu/GrStencilAttachment.h"
30#include "src/gpu/GrStencilSettings.h"
31#include "src/gpu/GrSurfacePriv.h"
32#include "src/gpu/GrTexturePriv.h"
33#include "src/gpu/GrTextureProxyPriv.h"
34#include "src/gpu/GrTracing.h"
35#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070036
bsalomon@google.comd302f142011-03-03 13:54:13 +000037////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000038
Brian Salomone2826ab2019-06-04 15:58:31 -040039GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomoned0bcad2015-05-04 10:36:42 -070041GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070042
bsalomon6e2aad42016-04-01 11:54:31 -070043void GrGpu::disconnect(DisconnectType) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000044
bsalomon@google.comd302f142011-03-03 13:54:13 +000045////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000046
Greg Daniel8f5bbda2018-06-08 17:22:23 -040047bool GrGpu::IsACopyNeededForRepeatWrapMode(const GrCaps* caps, GrTextureProxy* texProxy,
48 int width, int height,
49 GrSamplerState::Filter filter,
50 GrTextureProducer::CopyParams* copyParams,
51 SkScalar scaleAdjust[2]) {
52 if (!caps->npotTextureTileSupport() &&
Greg Daniel09c94002018-06-08 22:11:51 +000053 (!SkIsPow2(width) || !SkIsPow2(height))) {
54 SkASSERT(scaleAdjust);
55 copyParams->fWidth = GrNextPow2(width);
56 copyParams->fHeight = GrNextPow2(height);
57 SkASSERT(scaleAdjust);
58 scaleAdjust[0] = ((SkScalar)copyParams->fWidth) / width;
59 scaleAdjust[1] = ((SkScalar)copyParams->fHeight) / height;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040060 switch (filter) {
Greg Daniel09c94002018-06-08 22:11:51 +000061 case GrSamplerState::Filter::kNearest:
62 copyParams->fFilter = GrSamplerState::Filter::kNearest;
63 break;
64 case GrSamplerState::Filter::kBilerp:
65 case GrSamplerState::Filter::kMipMap:
66 // We are only ever scaling up so no reason to ever indicate kMipMap.
67 copyParams->fFilter = GrSamplerState::Filter::kBilerp;
68 break;
69 }
70 return true;
71 }
Robert Phillipsabf7b762018-03-21 12:13:37 -040072
73 if (texProxy) {
74 // If the texture format itself doesn't support repeat wrap mode or mipmapping (and
75 // those capabilities are required) force a copy.
Brian Salomonfd98c2c2018-07-31 17:25:29 -040076 if (texProxy->hasRestrictedSampling()) {
Robert Phillipsabf7b762018-03-21 12:13:37 -040077 copyParams->fFilter = GrSamplerState::Filter::kNearest;
78 copyParams->fWidth = texProxy->width();
79 copyParams->fHeight = texProxy->height();
80 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);
91 bool willNeedMips = GrSamplerState::Filter::kMipMap == filter && caps->mipMapSupport();
92 // If the texture format itself doesn't support mipmapping (and those capabilities are required)
93 // force a copy.
94 if (willNeedMips && texProxy->mipMapped() == GrMipMapped::kNo) {
95 copyParams->fFilter = GrSamplerState::Filter::kNearest;
96 copyParams->fWidth = texProxy->width();
97 copyParams->fHeight = texProxy->height();
98 return true;
99 }
100
101 return false;
102}
103
Brian Salomona90382f2019-09-17 09:01:56 -0400104static bool validate_texel_levels(int w, int h, GrColorType texelColorType,
105 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -0400106 SkASSERT(mipLevelCount > 0);
107 bool hasBasePixels = texels[0].fPixels;
108 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -0400109 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400110 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
111 if (texels[currentMipLevel].fPixels) {
112 const size_t minRowBytes = w * bpp;
113 if (caps->writePixelsRowBytesSupport()) {
114 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
115 return false;
116 }
117 if (texels[currentMipLevel].fRowBytes % bpp) {
118 return false;
119 }
120 } else {
121 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
122 return false;
123 }
124 }
125 ++levelsWithPixelsCnt;
126 }
127 if (w == 1 && h == 1) {
128 if (currentMipLevel != mipLevelCount - 1) {
129 return false;
130 }
131 } else {
132 w = std::max(w / 2, 1);
133 h = std::max(h / 2, 1);
134 }
135 }
136 // Either just a base layer or a full stack is required.
137 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
138 return false;
139 }
140 // Can specify just the base, all levels, or no levels.
141 if (!hasBasePixels) {
142 return levelsWithPixelsCnt == 0;
143 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400144 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400145}
146
Brian Salomona90382f2019-09-17 09:01:56 -0400147sk_sp<GrTexture> GrGpu::createTextureCommon(const GrSurfaceDesc& desc,
148 const GrBackendFormat& format,
149 GrRenderable renderable,
150 int renderTargetSampleCnt,
151 SkBudgeted budgeted,
152 GrProtected isProtected,
153 int mipLevelCount,
154 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400155 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400156 // Call GrGpu::createCompressedTexture.
157 return nullptr;
158 }
cblume55f2d2d2016-02-26 13:20:48 -0800159
Brian Salomona90382f2019-09-17 09:01:56 -0400160 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel6fa62e22019-08-07 15:52:37 -0400161 if (!this->caps()->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig,
162 renderable, renderTargetSampleCnt, mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700163 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700164 }
165
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400166 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400167 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400168 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500169 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400170 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400171 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400172 this->handleDirtyContext();
173 auto tex = this->onCreateTexture(desc,
174 format,
175 renderable,
176 renderTargetSampleCnt,
177 budgeted,
178 isProtected,
179 mipLevelCount,
180 levelClearMask);
181 if (tex) {
182 SkASSERT(tex->backendFormat() == format);
183 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
184 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
185 tex->resourcePriv().removeScratchKey();
186 }
187 fStats.incTextureCreates();
188 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
189 SkASSERT(GrRenderable::kYes == renderable);
190 tex->asRenderTarget()->setRequiresManualMSAAResolve();
191 }
192 }
193 return tex;
194}
195
196sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc,
197 const GrBackendFormat& format,
198 GrRenderable renderable,
199 int renderTargetSampleCnt,
200 GrMipMapped mipMapped,
201 SkBudgeted budgeted,
202 GrProtected isProtected) {
203 int mipLevelCount = 1;
204 if (mipMapped == GrMipMapped::kYes) {
205 mipLevelCount = 32 - SkCLZ(static_cast<uint32_t>(SkTMax(desc.fWidth, desc.fHeight)));
206 }
207 uint32_t levelClearMask =
208 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
209 auto tex = this->createTextureCommon(desc, format, renderable, renderTargetSampleCnt, budgeted,
210 isProtected, mipLevelCount, levelClearMask);
211 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
212 tex->texturePriv().markMipMapsClean();
213 }
214 return tex;
215}
216
217sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc,
218 const GrBackendFormat& format,
219 GrRenderable renderable,
220 int renderTargetSampleCnt,
221 SkBudgeted budgeted,
222 GrProtected isProtected,
223 GrColorType textureColorType,
224 GrColorType srcColorType,
225 const GrMipLevel texels[],
226 int texelLevelCount) {
227 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400228 if (texelLevelCount) {
Brian Salomona90382f2019-09-17 09:01:56 -0400229 if (!validate_texel_levels(desc.fWidth, desc.fHeight, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400230 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400231 return nullptr;
232 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400233 }
234
235 int mipLevelCount = SkTMax(1, texelLevelCount);
236 uint32_t levelClearMask = 0;
237 if (this->caps()->shouldInitializeTextures()) {
238 if (texelLevelCount) {
239 for (int i = 0; i < mipLevelCount; ++i) {
240 if (!texels->fPixels) {
241 levelClearMask |= static_cast<uint32_t>(1 << i);
242 }
243 }
244 } else {
245 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
246 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400247 }
248
Brian Salomona90382f2019-09-17 09:01:56 -0400249 auto tex = this->createTextureCommon(desc, format, renderable, renderTargetSampleCnt, budgeted,
250 isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800251 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400252 bool markMipLevelsClean = false;
253 // Currently if level 0 does not have pixels then no other level may, as enforced by
254 // validate_texel_levels.
255 if (texelLevelCount && texels[0].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400256 if (!this->writePixels(tex.get(), 0, 0, desc.fWidth, desc.fHeight, textureColorType,
Brian Salomona90382f2019-09-17 09:01:56 -0400257 srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400258 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800259 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400260 // Currently if level[1] of mip map has pixel data then so must all other levels.
261 // as enforced by validate_texel_levels.
262 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
263 fStats.incTextureUploads();
264 } else if (levelClearMask && mipLevelCount > 1) {
265 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800266 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400267 if (markMipLevelsClean) {
268 tex->texturePriv().markMipMapsClean();
269 }
bsalomonb12ea412015-02-02 21:19:50 -0800270 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000271 return tex;
272}
273
Brian Salomonbb8dde82019-06-27 10:52:13 -0400274sk_sp<GrTexture> GrGpu::createCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400275 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400276 SkImage::CompressionType compressionType,
277 SkBudgeted budgeted, const void* data,
278 size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400279 // If we ever add a new CompressionType, we should add a check here to make sure the
280 // GrBackendFormat and CompressionType are compatible with eachother.
281 SkASSERT(compressionType == SkImage::kETC1_CompressionType);
282
Brian Salomonbb8dde82019-06-27 10:52:13 -0400283 this->handleDirtyContext();
284 if (width < 1 || width > this->caps()->maxTextureSize() ||
285 height < 1 || height > this->caps()->maxTextureSize()) {
286 return nullptr;
287 }
Brian Salomona3e29962019-07-16 11:52:08 -0400288 // Note if we relax the requirement that data must be provided then we must check
289 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400290 if (!data) {
291 return nullptr;
292 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400293 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400294 return nullptr;
295 }
296 if (dataSize < GrCompressedDataSize(compressionType, width, height)) {
297 return nullptr;
298 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400299 return this->onCreateCompressedTexture(width, height, format, compressionType, budgeted, data);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400300}
301
Greg Daniel7ef28f32017-04-20 16:41:55 +0000302sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400303 GrColorType colorType,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500304 GrWrapOwnership ownership, GrWrapCacheable cacheable,
305 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500306 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000307 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400308
309 const GrCaps* caps = this->caps();
310 SkASSERT(caps);
311
Greg Daniel7bfc9132019-08-14 14:23:53 -0400312 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800313 return nullptr;
314 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400315 if (backendTex.width() > caps->maxTextureSize() ||
316 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800317 return nullptr;
318 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400319
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400320 return this->onWrapBackendTexture(backendTex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400321}
Eric Karl5c779752017-05-08 12:02:07 -0700322
Brian Salomond17f6582017-07-19 18:28:58 -0400323sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips0902c982019-07-16 07:47:56 -0400324 int sampleCnt, GrColorType colorType,
325 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500326 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400327 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500328 if (sampleCnt < 1) {
329 return nullptr;
330 }
Robert Phillips0902c982019-07-16 07:47:56 -0400331
332 const GrCaps* caps = this->caps();
333
Greg Daniel7bfc9132019-08-14 14:23:53 -0400334 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400335 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400336 return nullptr;
337 }
338
Robert Phillips0902c982019-07-16 07:47:56 -0400339 if (backendTex.width() > caps->maxRenderTargetSize() ||
340 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400341 return nullptr;
342 }
Robert Phillips0902c982019-07-16 07:47:56 -0400343 sk_sp<GrTexture> tex = this->onWrapRenderableBackendTexture(backendTex, sampleCnt, colorType,
344 ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400345 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600346 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
347 tex->asRenderTarget()->setRequiresManualMSAAResolve();
348 }
bungeman6bd52842016-10-27 09:30:08 -0700349 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000350}
351
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400352sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
353 GrColorType colorType) {
354 this->handleDirtyContext();
355
356 const GrCaps* caps = this->caps();
357
Greg Daniel6fa62e22019-08-07 15:52:37 -0400358 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800359 return nullptr;
360 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400361
362 return this->onWrapBackendRenderTarget(backendRT, colorType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000363}
364
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400365sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
366 int sampleCnt,
367 GrColorType colorType) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500368 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400369
370 const GrCaps* caps = this->caps();
371
372 int maxSize = caps->maxTextureSize();
373 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
374 return nullptr;
375 }
376
Greg Daniel6fa62e22019-08-07 15:52:37 -0400377 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400378 return nullptr;
379 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400380
Chris Dalton3f7932e2019-08-19 00:39:13 -0600381 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt, colorType);
382 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
383 rt->setRequiresManualMSAAResolve();
384 }
385 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800386}
387
Greg Danielb46add82019-01-02 14:51:29 -0500388sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
389 const GrVkDrawableInfo& vkInfo) {
390 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
391}
392
393sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
394 const GrVkDrawableInfo& vkInfo) {
395 // This is only supported on Vulkan so we default to returning nullptr here
396 return nullptr;
397}
398
Brian Salomondbf70722019-02-07 11:31:24 -0500399sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
400 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400401 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000402 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500403 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700404 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700405 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700406 }
cdalton397536c2016-03-25 12:15:03 -0700407 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800408}
409
Greg Daniel46cfbc62019-06-07 11:43:30 -0400410bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400411 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400412 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700413 SkASSERT(dst && src);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500414
415 if (dst->readOnly()) {
416 return false;
417 }
418
joshualitt1cbdcde2015-08-21 11:53:29 -0700419 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500420
Greg Daniele227fe42019-08-21 13:52:24 -0400421 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700422}
423
Brian Salomona6948702018-06-01 15:33:20 -0400424bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400425 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
426 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400427 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500428 SkASSERT(surface);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400429 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500430
Brian Salomon1d435302019-07-01 13:05:28 -0400431 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
432 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
433 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700434 return false;
435 }
436
Brian Salomon1047a492019-07-02 12:25:21 -0400437 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
438 if (!this->caps()->readPixelsRowBytesSupport()) {
439 if (rowBytes != minRowBytes) {
440 return false;
441 }
442 } else {
443 if (rowBytes < minRowBytes) {
444 return false;
445 }
446 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
447 return false;
448 }
449 }
450
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400451 if (this->caps()->isFormatCompressed(surface->backendFormat())) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500452 return false;
453 }
454
Brian Salomonbf7b6202016-11-11 16:08:03 -0500455 this->handleDirtyContext();
456
Brian Salomonf77c1462019-08-01 15:19:29 -0400457 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
458 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000459}
460
Brian Salomona9b04b92018-06-01 15:04:28 -0400461bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400462 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400463 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400464 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500465 SkASSERT(surface);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400466 SkASSERT(this->caps()->isFormatTexturableAndUploadable(surfaceColorType,
467 surface->backendFormat()));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500468
469 if (surface->readOnly()) {
470 return false;
471 }
472
Brian Salomon1047a492019-07-02 12:25:21 -0400473 if (mipLevelCount == 0) {
474 return false;
475 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400476 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400477 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
478 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400479 if (!bounds.contains(subRect)) {
480 return false;
481 }
482 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
483 // We require that if the texels are mipped, than the write region is the entire surface
484 return false;
485 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400486
Brian Salomona90382f2019-09-17 09:01:56 -0400487 if (!validate_texel_levels(width, height, srcColorType, texels, mipLevelCount, this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400488 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800489 }
jvanverth2dc29942015-09-01 07:16:46 -0700490
bsalomon@google.com6f379512011-11-16 20:36:03 +0000491 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400492 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400493 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700494 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400495 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800496 fStats.incTextureUploads();
497 return true;
498 }
499 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000500}
501
Brian Salomone05ba5a2019-04-08 11:59:07 -0400502bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400503 GrColorType textureColorType, GrColorType bufferColorType,
504 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400505 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500506 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700507 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400508 SkASSERT(this->caps()->isFormatTexturableAndUploadable(textureColorType,
509 texture->backendFormat()));
jvanverth17aa0472016-01-05 10:41:27 -0800510
Brian Salomonc67c31c2018-12-06 10:00:03 -0500511 if (texture->readOnly()) {
512 return false;
513 }
514
Greg Daniel660cc992017-06-26 14:55:05 -0400515 // We require that the write region is contained in the texture
516 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
517 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
518 if (!bounds.contains(subRect)) {
519 return false;
520 }
521
Brian Salomonb28cb682019-07-26 12:48:47 -0400522 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400523 if (this->caps()->writePixelsRowBytesSupport()) {
524 if (rowBytes < SkToSizeT(bpp * width)) {
525 return false;
526 }
527 if (rowBytes % bpp) {
528 return false;
529 }
530 } else {
531 if (rowBytes != SkToSizeT(bpp * width)) {
532 return false;
533 }
534 }
535
jvanverth17aa0472016-01-05 10:41:27 -0800536 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400537 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
538 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700539 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500540 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800541 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700542
jvanverth17aa0472016-01-05 10:41:27 -0800543 return true;
544 }
545 return false;
546}
547
Brian Salomon26de56e2019-04-10 12:14:26 -0400548bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400549 GrColorType surfaceColorType, GrColorType bufferColorType,
550 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400551 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400552 SkASSERT(surface);
553 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400554 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400555
Greg Danielba88ab62019-07-26 09:14:01 -0400556#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400557 auto supportedRead = this->caps()->supportedReadPixelsColorType(
558 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400559 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
560 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
561#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400562
563 // We require that the write region is contained in the texture
564 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
565 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
566 if (!bounds.contains(subRect)) {
567 return false;
568 }
569
570 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400571 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
572 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400573 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400574 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400575 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400576 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400577}
578
Brian Salomon930f9392018-06-20 16:25:26 -0400579bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400580 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400581 SkASSERT(texture);
582 SkASSERT(this->caps()->mipMapSupport());
583 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Brian Salomon930f9392018-06-20 16:25:26 -0400584 SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve());
Chris Dalton16a33c62019-09-24 22:19:17 -0600585 if (!texture->texturePriv().mipMapsAreDirty()) {
586 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
587 // actual target. This may be caused by things that the drawingManager could not predict,
588 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
589 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
590 return true;
591 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500592 if (texture->readOnly()) {
593 return false;
594 }
Brian Salomon930f9392018-06-20 16:25:26 -0400595 if (this->onRegenerateMipMapLevels(texture)) {
596 texture->texturePriv().markMipMapsClean();
597 return true;
598 }
599 return false;
600}
601
Brian Salomon1f05d452019-02-08 12:33:08 -0500602void GrGpu::resetTextureBindings() {
603 this->handleDirtyContext();
604 this->onResetTextureBindings();
605}
606
Chris Dalton16a33c62019-09-24 22:19:17 -0600607void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
608 GrSurfaceOrigin origin, ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000609 SkASSERT(target);
Chris Dalton16a33c62019-09-24 22:19:17 -0600610 if (!target->needsResolve()) {
611 // This can happen when the proxy expects MSAA to be dirty, but it is not dirty on the
612 // actual target. This may be caused by things that the drawingManager could not predict,
613 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
614 // NOTE: This goes away once we quit tracking dirty state on the actual render target.
615 return;
616 }
617#ifdef SK_DEBUG
618 auto nativeResolveRect = GrNativeRect::MakeRelativeTo(origin, target->height(), resolveRect);
619 // The proxy will often track a tighter resolve rect than GrRenderTarget, but it should never be
620 // the other way around.
621 SkASSERT(target->getResolveRect().contains(nativeResolveRect.asSkIRect()));
622#endif
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000623 this->handleDirtyContext();
Chris Dalton16a33c62019-09-24 22:19:17 -0600624 this->onResolveRenderTarget(target, resolveRect, origin, forExternalIO);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000625}
626
Brian Salomon1fabd512018-02-09 09:54:25 -0500627void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
628 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700629 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500630 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700631 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
632 if (nullptr == bounds || !bounds->isEmpty()) {
633 if (GrRenderTarget* target = surface->asRenderTarget()) {
Brian Salomon1fabd512018-02-09 09:54:25 -0500634 SkIRect flippedBounds;
635 if (kBottomLeft_GrSurfaceOrigin == origin && bounds) {
636 flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom,
637 bounds->fRight, surface->height() - bounds->fTop};
638 bounds = &flippedBounds;
639 }
jvanverth900bd4a2016-04-29 13:53:12 -0700640 target->flagAsNeedingResolve(bounds);
641 }
642 GrTexture* texture = surface->asTexture();
643 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400644 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700645 }
646 }
647}
648
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600649int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700650 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600651 SkASSERT(renderTarget->numSamples() > 1 ||
652 (renderTarget->renderTargetPriv().getStencilAttachment() &&
653 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700654
655 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600656 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700657 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
658}
659
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400660GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
661 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500662 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400663 const GrFlushInfo& info,
664 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400665 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500666 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500667 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500668
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400669 if (this->caps()->semaphoreSupport()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400670 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniel51316782017-08-02 15:10:09 +0000671 sk_sp<GrSemaphore> semaphore;
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400672 if (info.fSignalSemaphores[i].isInitialized()) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500673 semaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400674 info.fSignalSemaphores[i],
675 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500676 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000677 } else {
Robert Phillips6be756b2018-01-16 15:07:54 -0500678 semaphore = resourceProvider->makeSemaphore(false);
Greg Daniel51316782017-08-02 15:10:09 +0000679 }
Greg Daniel858e12c2018-12-06 11:11:37 -0500680 this->insertSemaphore(semaphore);
Greg Daniel51316782017-08-02 15:10:09 +0000681
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400682 if (!info.fSignalSemaphores[i].isInitialized()) {
683 info.fSignalSemaphores[i] = semaphore->backendSemaphore();
Greg Daniel51316782017-08-02 15:10:09 +0000684 }
685 }
686 }
Greg Daniel797efca2019-05-09 14:04:20 -0400687 this->onFinishFlush(proxies, n, access, info, externalRequests);
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400688 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000689 : GrSemaphoresSubmitted::kNo;
690}
Brian Osman71a18892017-08-10 10:23:25 -0400691
Kevin Lubickf4def342018-10-04 12:52:50 -0400692#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400693void GrGpu::dumpJSON(SkJSONWriter* writer) const {
694 writer->beginObject();
695
696 // TODO: Is there anything useful in the base class to dump here?
697
698 this->onDumpJSON(writer);
699
700 writer->endObject();
701}
Kevin Lubickf4def342018-10-04 12:52:50 -0400702#else
703void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
704#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400705
Robert Phillipsf0ced622019-05-16 09:06:25 -0400706#if GR_TEST_UTILS
707
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500708#if GR_GPU_STATS
709void GrGpu::Stats::dump(SkString* out) {
710 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
711 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
712 out->appendf("Textures Created: %d\n", fTextureCreates);
713 out->appendf("Texture Uploads: %d\n", fTextureUploads);
714 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400715 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500716 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
717 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400718 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500719}
720
721void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
722 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
723 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500724}
725
Robert Phillips57ef6802019-09-23 10:12:47 -0400726#endif // GR_GPU_STATS
727#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500728
Robert Phillips57ef6802019-09-23 10:12:47 -0400729
730bool GrGpu::MipMapsAreCorrect(int baseWidth, int baseHeight, GrMipMapped mipMapped,
731 const SkPixmap srcData[], int numMipLevels) {
732 if (!srcData) {
733 return true;
734 }
735
736 if (baseWidth != srcData[0].width() || baseHeight != srcData[0].height()) {
737 return false;
738 }
739
740 if (mipMapped == GrMipMapped::kYes) {
741 if (numMipLevels != SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1) {
742 return false;
743 }
744
745 SkColorType colorType = srcData[0].colorType();
746
747 int currentWidth = baseWidth;
748 int currentHeight = baseHeight;
749 for (int i = 1; i < numMipLevels; ++i) {
750 currentWidth = SkTMax(1, currentWidth / 2);
751 currentHeight = SkTMax(1, currentHeight / 2);
752
753 if (srcData[i].colorType() != colorType) { // all levels must have same colorType
754 return false;
755 }
756
757 if (srcData[i].width() != currentWidth || srcData[i].height() != currentHeight) {
758 return false;
759 }
760 }
761 } else if (numMipLevels != 1) {
762 return false;
763 }
764
765 return true;
766}
767
768GrBackendTexture GrGpu::createBackendTexture(int w, int h, const GrBackendFormat& format,
769 GrMipMapped mipMapped, GrRenderable renderable,
770 const SkPixmap srcData[], int numMipLevels,
771 const SkColor4f* color, GrProtected isProtected) {
772 const GrCaps* caps = this->caps();
773
774 if (!format.isValid()) {
775 return {};
776 }
777
Robert Phillipsd34691b2019-09-24 13:38:43 -0400778 if (caps->isFormatCompressed(format)) {
779 // Compressed formats must go through the createCompressedBackendTexture API
780 return {};
781 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400782
783 if (w < 1 || w > caps->maxTextureSize() || h < 1 || h > caps->maxTextureSize()) {
784 return {};
785 }
786
787 // TODO: maybe just ignore the mipMapped parameter in this case
788 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
789 return {};
790 }
791
792 if (!MipMapsAreCorrect(w, h, mipMapped, srcData, numMipLevels)) {
793 return {};
794 }
795
796 return this->onCreateBackendTexture(w, h, format, mipMapped, renderable,
797 srcData, numMipLevels, color, isProtected);
798}