blob: 035665112b48bd5093fab1dfebff57278aa5bdb7 [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
Brian Salomon1a217eb2019-10-24 10:50:36 -040047bool GrGpu::IsACopyNeededForRepeatWrapMode(const GrCaps* caps,
48 GrTextureProxy* texProxy,
49 SkISize dimensions,
Greg Daniel8f5bbda2018-06-08 17:22:23 -040050 GrSamplerState::Filter filter,
51 GrTextureProducer::CopyParams* copyParams,
52 SkScalar scaleAdjust[2]) {
53 if (!caps->npotTextureTileSupport() &&
Brian Salomon1a217eb2019-10-24 10:50:36 -040054 (!SkIsPow2(dimensions.width()) || !SkIsPow2(dimensions.height()))) {
Greg Daniel09c94002018-06-08 22:11:51 +000055 SkASSERT(scaleAdjust);
Brian Salomon1a217eb2019-10-24 10:50:36 -040056 copyParams->fDimensions = {SkNextPow2(dimensions.width()), SkNextPow2(dimensions.height())};
Greg Daniel09c94002018-06-08 22:11:51 +000057 SkASSERT(scaleAdjust);
Brian Salomon1a217eb2019-10-24 10:50:36 -040058 scaleAdjust[0] = ((SkScalar)copyParams->fDimensions.width()) / dimensions.width();
59 scaleAdjust[1] = ((SkScalar)copyParams->fDimensions.height()) / dimensions.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;
Brian Salomon1a217eb2019-10-24 10:50:36 -040078 copyParams->fDimensions = texProxy->dimensions();
Robert Phillipsabf7b762018-03-21 12:13:37 -040079 return true;
80 }
81 }
82
bsalomon100b8f82015-10-28 08:37:44 -070083 return false;
bsalomon045802d2015-10-20 07:58:01 -070084}
85
Greg Daniel8f5bbda2018-06-08 17:22:23 -040086bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
87 GrSamplerState::Filter filter,
88 GrTextureProducer::CopyParams* copyParams) {
89 SkASSERT(texProxy);
90 bool willNeedMips = GrSamplerState::Filter::kMipMap == filter && caps->mipMapSupport();
91 // If the texture format itself doesn't support mipmapping (and those capabilities are required)
92 // force a copy.
93 if (willNeedMips && texProxy->mipMapped() == GrMipMapped::kNo) {
94 copyParams->fFilter = GrSamplerState::Filter::kNearest;
Brian Salomon1a217eb2019-10-24 10:50:36 -040095 copyParams->fDimensions = texProxy->dimensions();
Greg Daniel8f5bbda2018-06-08 17:22:23 -040096 return true;
97 }
98
99 return false;
100}
101
Brian Salomona90382f2019-09-17 09:01:56 -0400102static bool validate_texel_levels(int w, int h, GrColorType texelColorType,
103 const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -0400104 SkASSERT(mipLevelCount > 0);
105 bool hasBasePixels = texels[0].fPixels;
106 int levelsWithPixelsCnt = 0;
Brian Salomona90382f2019-09-17 09:01:56 -0400107 auto bpp = GrColorTypeBytesPerPixel(texelColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400108 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
109 if (texels[currentMipLevel].fPixels) {
110 const size_t minRowBytes = w * bpp;
111 if (caps->writePixelsRowBytesSupport()) {
112 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
113 return false;
114 }
115 if (texels[currentMipLevel].fRowBytes % bpp) {
116 return false;
117 }
118 } else {
119 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
120 return false;
121 }
122 }
123 ++levelsWithPixelsCnt;
124 }
125 if (w == 1 && h == 1) {
126 if (currentMipLevel != mipLevelCount - 1) {
127 return false;
128 }
129 } else {
130 w = std::max(w / 2, 1);
131 h = std::max(h / 2, 1);
132 }
133 }
134 // Either just a base layer or a full stack is required.
135 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
136 return false;
137 }
138 // Can specify just the base, all levels, or no levels.
139 if (!hasBasePixels) {
140 return levelsWithPixelsCnt == 0;
141 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400142 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400143}
144
Brian Salomona90382f2019-09-17 09:01:56 -0400145sk_sp<GrTexture> GrGpu::createTextureCommon(const GrSurfaceDesc& desc,
146 const GrBackendFormat& format,
147 GrRenderable renderable,
148 int renderTargetSampleCnt,
149 SkBudgeted budgeted,
150 GrProtected isProtected,
151 int mipLevelCount,
152 uint32_t levelClearMask) {
Brian Salomon81536f22019-08-08 16:30:49 -0400153 if (this->caps()->isFormatCompressed(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400154 // Call GrGpu::createCompressedTexture.
155 return nullptr;
156 }
cblume55f2d2d2016-02-26 13:20:48 -0800157
Brian Salomona90382f2019-09-17 09:01:56 -0400158 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel9a48beb2020-01-16 16:57:16 -0500159 if (!this->caps()->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, renderable,
160 renderTargetSampleCnt, mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700161 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700162 }
163
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400164 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400165 renderTargetSampleCnt =
Greg Daniel6fa62e22019-08-07 15:52:37 -0400166 this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500167 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400168 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400169 SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64);
Brian Salomona90382f2019-09-17 09:01:56 -0400170 this->handleDirtyContext();
171 auto tex = this->onCreateTexture(desc,
172 format,
173 renderable,
174 renderTargetSampleCnt,
175 budgeted,
176 isProtected,
177 mipLevelCount,
178 levelClearMask);
179 if (tex) {
180 SkASSERT(tex->backendFormat() == format);
181 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
182 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
183 tex->resourcePriv().removeScratchKey();
184 }
185 fStats.incTextureCreates();
186 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
187 SkASSERT(GrRenderable::kYes == renderable);
188 tex->asRenderTarget()->setRequiresManualMSAAResolve();
189 }
190 }
191 return tex;
192}
193
194sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc,
195 const GrBackendFormat& format,
196 GrRenderable renderable,
197 int renderTargetSampleCnt,
198 GrMipMapped mipMapped,
199 SkBudgeted budgeted,
200 GrProtected isProtected) {
201 int mipLevelCount = 1;
202 if (mipMapped == GrMipMapped::kYes) {
203 mipLevelCount = 32 - SkCLZ(static_cast<uint32_t>(SkTMax(desc.fWidth, desc.fHeight)));
204 }
205 uint32_t levelClearMask =
206 this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0;
207 auto tex = this->createTextureCommon(desc, format, renderable, renderTargetSampleCnt, budgeted,
208 isProtected, mipLevelCount, levelClearMask);
209 if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) {
210 tex->texturePriv().markMipMapsClean();
211 }
212 return tex;
213}
214
215sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc,
216 const GrBackendFormat& format,
217 GrRenderable renderable,
218 int renderTargetSampleCnt,
219 SkBudgeted budgeted,
220 GrProtected isProtected,
221 GrColorType textureColorType,
222 GrColorType srcColorType,
223 const GrMipLevel texels[],
224 int texelLevelCount) {
225 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400226 if (texelLevelCount) {
Brian Salomona90382f2019-09-17 09:01:56 -0400227 if (!validate_texel_levels(desc.fWidth, desc.fHeight, srcColorType, texels, texelLevelCount,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400228 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400229 return nullptr;
230 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400231 }
232
233 int mipLevelCount = SkTMax(1, texelLevelCount);
234 uint32_t levelClearMask = 0;
235 if (this->caps()->shouldInitializeTextures()) {
236 if (texelLevelCount) {
237 for (int i = 0; i < mipLevelCount; ++i) {
238 if (!texels->fPixels) {
239 levelClearMask |= static_cast<uint32_t>(1 << i);
240 }
241 }
242 } else {
243 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
244 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400245 }
246
Brian Salomona90382f2019-09-17 09:01:56 -0400247 auto tex = this->createTextureCommon(desc, format, renderable, renderTargetSampleCnt, budgeted,
248 isProtected, texelLevelCount, levelClearMask);
bsalomonb12ea412015-02-02 21:19:50 -0800249 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400250 bool markMipLevelsClean = false;
251 // Currently if level 0 does not have pixels then no other level may, as enforced by
252 // validate_texel_levels.
253 if (texelLevelCount && texels[0].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400254 if (!this->writePixels(tex.get(), 0, 0, desc.fWidth, desc.fHeight, textureColorType,
Brian Salomona90382f2019-09-17 09:01:56 -0400255 srcColorType, texels, texelLevelCount)) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400256 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800257 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400258 // Currently if level[1] of mip map has pixel data then so must all other levels.
259 // as enforced by validate_texel_levels.
260 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
261 fStats.incTextureUploads();
262 } else if (levelClearMask && mipLevelCount > 1) {
263 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800264 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400265 if (markMipLevelsClean) {
266 tex->texturePriv().markMipMapsClean();
267 }
bsalomonb12ea412015-02-02 21:19:50 -0800268 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000269 return tex;
270}
271
Robert Phillips9f744f72019-12-19 19:14:33 -0500272sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400273 const GrBackendFormat& format,
Robert Phillips42716d42019-12-16 12:19:54 -0500274 SkBudgeted budgeted,
Robert Phillipse4720c62020-01-14 14:33:24 -0500275 GrMipMapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -0500276 GrProtected isProtected,
Robert Phillips42716d42019-12-16 12:19:54 -0500277 const void* data,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400278 size_t dataSize) {
279 this->handleDirtyContext();
Robert Phillips9f744f72019-12-19 19:14:33 -0500280 if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() ||
281 dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400282 return nullptr;
283 }
Brian Salomona3e29962019-07-16 11:52:08 -0400284 // Note if we relax the requirement that data must be provided then we must check
285 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400286 if (!data) {
287 return nullptr;
288 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400289 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400290 return nullptr;
291 }
Robert Phillips9f744f72019-12-19 19:14:33 -0500292
293 // TODO: expand CompressedDataIsCorrect to work here too
294 SkImage::CompressionType compressionType = this->caps()->compressionType(format);
295
Robert Phillipse4720c62020-01-14 14:33:24 -0500296 if (dataSize < GrCompressedDataSize(compressionType, dimensions, nullptr, mipMapped)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400297 return nullptr;
298 }
Robert Phillips3a833922020-01-21 15:25:58 -0500299 return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected,
300 data, dataSize);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400301}
302
Greg Daniel7ef28f32017-04-20 16:41:55 +0000303sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400304 GrColorType colorType,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500305 GrWrapOwnership ownership, GrWrapCacheable cacheable,
306 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500307 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000308 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400309
310 const GrCaps* caps = this->caps();
311 SkASSERT(caps);
312
Greg Daniel7bfc9132019-08-14 14:23:53 -0400313 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800314 return nullptr;
315 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400316 if (backendTex.width() > caps->maxTextureSize() ||
317 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800318 return nullptr;
319 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400320
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400321 return this->onWrapBackendTexture(backendTex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400322}
Eric Karl5c779752017-05-08 12:02:07 -0700323
Robert Phillipsb915c942019-12-17 14:44:37 -0500324sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex,
325 GrWrapOwnership ownership,
326 GrWrapCacheable cacheable) {
327 this->handleDirtyContext();
328
329 const GrCaps* caps = this->caps();
330 SkASSERT(caps);
331
332 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
333 return nullptr;
334 }
335 if (backendTex.width() > caps->maxTextureSize() ||
336 backendTex.height() > caps->maxTextureSize()) {
337 return nullptr;
338 }
339
340 return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable);
341}
342
343
Brian Salomond17f6582017-07-19 18:28:58 -0400344sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips0902c982019-07-16 07:47:56 -0400345 int sampleCnt, GrColorType colorType,
346 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500347 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400348 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500349 if (sampleCnt < 1) {
350 return nullptr;
351 }
Robert Phillips0902c982019-07-16 07:47:56 -0400352
353 const GrCaps* caps = this->caps();
354
Greg Daniel7bfc9132019-08-14 14:23:53 -0400355 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400356 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400357 return nullptr;
358 }
359
Robert Phillips0902c982019-07-16 07:47:56 -0400360 if (backendTex.width() > caps->maxRenderTargetSize() ||
361 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400362 return nullptr;
363 }
Robert Phillips0902c982019-07-16 07:47:56 -0400364 sk_sp<GrTexture> tex = this->onWrapRenderableBackendTexture(backendTex, sampleCnt, colorType,
365 ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400366 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600367 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
368 tex->asRenderTarget()->setRequiresManualMSAAResolve();
369 }
bungeman6bd52842016-10-27 09:30:08 -0700370 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000371}
372
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400373sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
374 GrColorType colorType) {
375 this->handleDirtyContext();
376
377 const GrCaps* caps = this->caps();
378
Greg Daniel6fa62e22019-08-07 15:52:37 -0400379 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800380 return nullptr;
381 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400382
383 return this->onWrapBackendRenderTarget(backendRT, colorType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000384}
385
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400386sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
387 int sampleCnt,
388 GrColorType colorType) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500389 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400390
391 const GrCaps* caps = this->caps();
392
393 int maxSize = caps->maxTextureSize();
394 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
395 return nullptr;
396 }
397
Greg Daniel6fa62e22019-08-07 15:52:37 -0400398 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400399 return nullptr;
400 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400401
Chris Dalton3f7932e2019-08-19 00:39:13 -0600402 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt, colorType);
403 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
404 rt->setRequiresManualMSAAResolve();
405 }
406 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800407}
408
Greg Danielb46add82019-01-02 14:51:29 -0500409sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
410 const GrVkDrawableInfo& vkInfo) {
411 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
412}
413
414sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
415 const GrVkDrawableInfo& vkInfo) {
416 // This is only supported on Vulkan so we default to returning nullptr here
417 return nullptr;
418}
419
Brian Salomondbf70722019-02-07 11:31:24 -0500420sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
421 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400422 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000423 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500424 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700425 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700426 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700427 }
cdalton397536c2016-03-25 12:15:03 -0700428 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800429}
430
Greg Daniel46cfbc62019-06-07 11:43:30 -0400431bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400432 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400433 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700434 SkASSERT(dst && src);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500435
436 if (dst->readOnly()) {
437 return false;
438 }
439
joshualitt1cbdcde2015-08-21 11:53:29 -0700440 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500441
Greg Daniele227fe42019-08-21 13:52:24 -0400442 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700443}
444
Brian Salomona6948702018-06-01 15:33:20 -0400445bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400446 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
447 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400448 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500449 SkASSERT(surface);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400450 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500451
Brian Salomon1d435302019-07-01 13:05:28 -0400452 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
453 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
454 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700455 return false;
456 }
457
Brian Salomon1047a492019-07-02 12:25:21 -0400458 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
459 if (!this->caps()->readPixelsRowBytesSupport()) {
460 if (rowBytes != minRowBytes) {
461 return false;
462 }
463 } else {
464 if (rowBytes < minRowBytes) {
465 return false;
466 }
467 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
468 return false;
469 }
470 }
471
Brian Salomonbf7b6202016-11-11 16:08:03 -0500472 this->handleDirtyContext();
473
Brian Salomonf77c1462019-08-01 15:19:29 -0400474 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
475 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000476}
477
Brian Salomona9b04b92018-06-01 15:04:28 -0400478bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400479 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400480 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400481 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500482 SkASSERT(surface);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400483 SkASSERT(this->caps()->isFormatTexturableAndUploadable(surfaceColorType,
484 surface->backendFormat()));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500485
486 if (surface->readOnly()) {
487 return false;
488 }
489
Brian Salomon1047a492019-07-02 12:25:21 -0400490 if (mipLevelCount == 0) {
491 return false;
492 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400493 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400494 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
495 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400496 if (!bounds.contains(subRect)) {
497 return false;
498 }
499 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
500 // We require that if the texels are mipped, than the write region is the entire surface
501 return false;
502 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400503
Brian Salomona90382f2019-09-17 09:01:56 -0400504 if (!validate_texel_levels(width, height, srcColorType, texels, mipLevelCount, this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400505 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800506 }
jvanverth2dc29942015-09-01 07:16:46 -0700507
bsalomon@google.com6f379512011-11-16 20:36:03 +0000508 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400509 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400510 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700511 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400512 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800513 fStats.incTextureUploads();
514 return true;
515 }
516 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000517}
518
Brian Salomone05ba5a2019-04-08 11:59:07 -0400519bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400520 GrColorType textureColorType, GrColorType bufferColorType,
521 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400522 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500523 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700524 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400525 SkASSERT(this->caps()->isFormatTexturableAndUploadable(textureColorType,
526 texture->backendFormat()));
jvanverth17aa0472016-01-05 10:41:27 -0800527
Brian Salomonc67c31c2018-12-06 10:00:03 -0500528 if (texture->readOnly()) {
529 return false;
530 }
531
Greg Daniel660cc992017-06-26 14:55:05 -0400532 // We require that the write region is contained in the texture
533 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
534 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
535 if (!bounds.contains(subRect)) {
536 return false;
537 }
538
Brian Salomonb28cb682019-07-26 12:48:47 -0400539 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400540 if (this->caps()->writePixelsRowBytesSupport()) {
541 if (rowBytes < SkToSizeT(bpp * width)) {
542 return false;
543 }
544 if (rowBytes % bpp) {
545 return false;
546 }
547 } else {
548 if (rowBytes != SkToSizeT(bpp * width)) {
549 return false;
550 }
551 }
552
jvanverth17aa0472016-01-05 10:41:27 -0800553 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400554 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
555 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700556 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500557 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800558 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700559
jvanverth17aa0472016-01-05 10:41:27 -0800560 return true;
561 }
562 return false;
563}
564
Brian Salomon26de56e2019-04-10 12:14:26 -0400565bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400566 GrColorType surfaceColorType, GrColorType bufferColorType,
567 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400568 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400569 SkASSERT(surface);
570 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400571 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400572
Greg Danielba88ab62019-07-26 09:14:01 -0400573#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400574 auto supportedRead = this->caps()->supportedReadPixelsColorType(
575 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400576 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
577 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
578#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400579
580 // We require that the write region is contained in the texture
581 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
582 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
583 if (!bounds.contains(subRect)) {
584 return false;
585 }
586
587 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400588 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
589 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400590 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400591 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400592 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400593 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400594}
595
Brian Salomon930f9392018-06-20 16:25:26 -0400596bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400597 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400598 SkASSERT(texture);
599 SkASSERT(this->caps()->mipMapSupport());
600 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
Chris Dalton16a33c62019-09-24 22:19:17 -0600601 if (!texture->texturePriv().mipMapsAreDirty()) {
602 // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the
603 // actual target. This may be caused by things that the drawingManager could not predict,
604 // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc.
605 // NOTE: This goes away once we quit tracking mipmap state on the actual texture.
606 return true;
607 }
Brian Salomonc67c31c2018-12-06 10:00:03 -0500608 if (texture->readOnly()) {
609 return false;
610 }
Brian Salomon930f9392018-06-20 16:25:26 -0400611 if (this->onRegenerateMipMapLevels(texture)) {
612 texture->texturePriv().markMipMapsClean();
613 return true;
614 }
615 return false;
616}
617
Brian Salomon1f05d452019-02-08 12:33:08 -0500618void GrGpu::resetTextureBindings() {
619 this->handleDirtyContext();
620 this->onResetTextureBindings();
621}
622
Chris Dalton16a33c62019-09-24 22:19:17 -0600623void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
624 GrSurfaceOrigin origin, ForExternalIO forExternalIO) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000625 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000626 this->handleDirtyContext();
Chris Dalton16a33c62019-09-24 22:19:17 -0600627 this->onResolveRenderTarget(target, resolveRect, origin, forExternalIO);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000628}
629
Brian Salomon1fabd512018-02-09 09:54:25 -0500630void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
631 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700632 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500633 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700634 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
635 if (nullptr == bounds || !bounds->isEmpty()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700636 GrTexture* texture = surface->asTexture();
637 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400638 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700639 }
640 }
641}
642
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600643int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700644 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600645 SkASSERT(renderTarget->numSamples() > 1 ||
646 (renderTarget->renderTargetPriv().getStencilAttachment() &&
647 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700648
649 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600650 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700651 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
652}
653
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400654GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
655 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500656 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400657 const GrFlushInfo& info,
658 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400659 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500660 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500661 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500662
Greg Daniel30a35e82019-11-19 14:12:25 -0500663 struct SemaphoreInfo {
664 std::unique_ptr<GrSemaphore> fSemaphore;
665 bool fDidCreate = false;
666 };
667
668 bool failedSemaphoreCreation = false;
669 std::unique_ptr<SemaphoreInfo[]> semaphoreInfos(new SemaphoreInfo[info.fNumSemaphores]);
670 if (this->caps()->semaphoreSupport() && info.fNumSemaphores) {
671 for (int i = 0; i < info.fNumSemaphores && !failedSemaphoreCreation; ++i) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400672 if (info.fSignalSemaphores[i].isInitialized()) {
Greg Daniel30a35e82019-11-19 14:12:25 -0500673 semaphoreInfos[i].fSemaphore = 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 {
Greg Daniel30a35e82019-11-19 14:12:25 -0500678 semaphoreInfos[i].fSemaphore = resourceProvider->makeSemaphore(false);
679 semaphoreInfos[i].fDidCreate = true;
Greg Daniel51316782017-08-02 15:10:09 +0000680 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500681 if (!semaphoreInfos[i].fSemaphore) {
682 semaphoreInfos[i].fDidCreate = false;
683 failedSemaphoreCreation = true;
684 }
685 }
686 if (!failedSemaphoreCreation) {
687 for (int i = 0; i < info.fNumSemaphores && !failedSemaphoreCreation; ++i) {
688 this->insertSemaphore(semaphoreInfos[i].fSemaphore.get());
Greg Daniel51316782017-08-02 15:10:09 +0000689 }
690 }
691 }
Greg Daniel30a35e82019-11-19 14:12:25 -0500692
693 // We always want to try flushing, so do that before checking if we failed semaphore creation.
694 if (!this->onFinishFlush(proxies, n, access, info, externalRequests) ||
695 failedSemaphoreCreation) {
696 // If we didn't do the flush or failed semaphore creations then none of the semaphores were
697 // submitted. Therefore the client can't wait on any of the semaphores. Additionally any
698 // semaphores we created here the client is not responsible for deleting so we must make
699 // sure they get deleted. We do this by changing the ownership from borrowed to owned.
700 for (int i = 0; i < info.fNumSemaphores; ++i) {
701 if (semaphoreInfos[i].fDidCreate) {
702 SkASSERT(semaphoreInfos[i].fSemaphore);
703 semaphoreInfos[i].fSemaphore->setIsOwned();
704 }
705 }
706 return GrSemaphoresSubmitted::kNo;
707 }
708
709 for (int i = 0; i < info.fNumSemaphores; ++i) {
710 if (!info.fSignalSemaphores[i].isInitialized()) {
711 SkASSERT(semaphoreInfos[i].fSemaphore);
712 info.fSignalSemaphores[i] = semaphoreInfos[i].fSemaphore->backendSemaphore();
713 }
714 }
715
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400716 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000717 : GrSemaphoresSubmitted::kNo;
718}
Brian Osman71a18892017-08-10 10:23:25 -0400719
Kevin Lubickf4def342018-10-04 12:52:50 -0400720#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400721void GrGpu::dumpJSON(SkJSONWriter* writer) const {
722 writer->beginObject();
723
724 // TODO: Is there anything useful in the base class to dump here?
725
726 this->onDumpJSON(writer);
727
728 writer->endObject();
729}
Kevin Lubickf4def342018-10-04 12:52:50 -0400730#else
731void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
732#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400733
Robert Phillipsf0ced622019-05-16 09:06:25 -0400734#if GR_TEST_UTILS
735
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500736#if GR_GPU_STATS
737void GrGpu::Stats::dump(SkString* out) {
738 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
739 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
740 out->appendf("Textures Created: %d\n", fTextureCreates);
741 out->appendf("Texture Uploads: %d\n", fTextureUploads);
742 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400743 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500744 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
745 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400746 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500747}
748
749void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
750 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
751 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500752}
753
Robert Phillips57ef6802019-09-23 10:12:47 -0400754#endif // GR_GPU_STATS
755#endif // GR_TEST_UTILS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500756
Brian Salomon85c3d682019-11-04 15:04:54 -0500757bool GrGpu::MipMapsAreCorrect(SkISize dimensions, const BackendTextureData* data, int numLevels) {
758 if (numLevels != 1 &&
759 numLevels != SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1) {
760 return false;
761 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400762
Robert Phillips42716d42019-12-16 12:19:54 -0500763 if (!data || data->type() == BackendTextureData::Type::kColor) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400764 return true;
765 }
766
Robert Phillips42716d42019-12-16 12:19:54 -0500767 if (data->type() == BackendTextureData::Type::kCompressed) {
768 return false;
769 }
770
771 SkASSERT(data->type() == BackendTextureData::Type::kPixmaps);
772
Brian Salomon85c3d682019-11-04 15:04:54 -0500773 if (data->pixmap(0).dimensions() != dimensions) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400774 return false;
775 }
776
Brian Salomon85c3d682019-11-04 15:04:54 -0500777 SkColorType colorType = data->pixmap(0).colorType();
778 for (int i = 1; i < numLevels; ++i) {
779 dimensions = {SkTMax(1, dimensions.width() /2),
780 SkTMax(1, dimensions.height()/2)};
781 if (dimensions != data->pixmap(i).dimensions()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400782 return false;
783 }
Brian Salomon85c3d682019-11-04 15:04:54 -0500784 if (colorType != data->pixmap(i).colorType()) {
785 return false;
Robert Phillips57ef6802019-09-23 10:12:47 -0400786 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400787 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400788 return true;
789}
790
Robert Phillipsb915c942019-12-17 14:44:37 -0500791bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType,
792 GrMipMapped mipMapped, const BackendTextureData* data) {
793
794 if (!data || data->type() == BackendTextureData::Type::kColor) {
795 return true;
796 }
797
798 if (data->type() == BackendTextureData::Type::kPixmaps) {
799 return false;
800 }
801
802 SkASSERT(data->type() == BackendTextureData::Type::kCompressed);
803
Robert Phillips0d7e2f12019-12-18 13:01:04 -0500804 size_t computedSize = GrCompressedDataSize(compressionType, dimensions,
805 nullptr, mipMapped);
Robert Phillipsb915c942019-12-17 14:44:37 -0500806
807 return computedSize == data->compressedSize();
808}
809
Brian Salomon85c3d682019-11-04 15:04:54 -0500810GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions,
811 const GrBackendFormat& format,
812 GrRenderable renderable,
Brian Salomon85c3d682019-11-04 15:04:54 -0500813 int numMipLevels,
Robert Phillips4277f012020-01-21 14:28:34 -0500814 GrProtected isProtected,
815 const BackendTextureData* data) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400816 const GrCaps* caps = this->caps();
817
818 if (!format.isValid()) {
819 return {};
820 }
821
Robert Phillipsd34691b2019-09-24 13:38:43 -0400822 if (caps->isFormatCompressed(format)) {
823 // Compressed formats must go through the createCompressedBackendTexture API
824 return {};
825 }
Robert Phillips57ef6802019-09-23 10:12:47 -0400826
Brian Salomon85c3d682019-11-04 15:04:54 -0500827 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
828 auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType());
829 if (!caps->areColorTypeAndFormatCompatible(ct, format)) {
830 return {};
831 }
832 }
833
834 if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() ||
835 dimensions.height() > caps->maxTextureSize()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400836 return {};
837 }
838
Brian Salomon85c3d682019-11-04 15:04:54 -0500839 if (numMipLevels > 1 && !this->caps()->mipMapSupport()) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400840 return {};
841 }
842
Brian Salomon85c3d682019-11-04 15:04:54 -0500843 if (!MipMapsAreCorrect(dimensions, data, numMipLevels)) {
Robert Phillips57ef6802019-09-23 10:12:47 -0400844 return {};
845 }
846
Robert Phillips4277f012020-01-21 14:28:34 -0500847 return this->onCreateBackendTexture(dimensions, format, renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -0500848 numMipLevels > 1 ? GrMipMapped::kYes : GrMipMapped::kNo,
Robert Phillips4277f012020-01-21 14:28:34 -0500849 isProtected, data);
Robert Phillips57ef6802019-09-23 10:12:47 -0400850}
Robert Phillipsb915c942019-12-17 14:44:37 -0500851
852GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions,
853 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -0500854 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -0500855 GrProtected isProtected,
856 const BackendTextureData* data) {
Robert Phillipsb915c942019-12-17 14:44:37 -0500857 const GrCaps* caps = this->caps();
858
859 if (!format.isValid()) {
860 return {};
861 }
862
863 SkImage::CompressionType compressionType = caps->compressionType(format);
864 if (compressionType == SkImage::CompressionType::kNone) {
865 // Uncompressed formats must go through the createBackendTexture API
866 return {};
867 }
868
869 if (dimensions.isEmpty() ||
870 dimensions.width() > caps->maxTextureSize() ||
871 dimensions.height() > caps->maxTextureSize()) {
872 return {};
873 }
874
875 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
876 return {};
877 }
878
879 if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) {
880 return {};
881 }
882
Robert Phillips4277f012020-01-21 14:28:34 -0500883 return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped,
884 isProtected, data);
Robert Phillipsb915c942019-12-17 14:44:37 -0500885}