blob: a7a3bb1c170b0fc5a8aee4506806ca3e7337d58c [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrCaps.h"
17#include "src/gpu/GrContextPriv.h"
Brian Salomonbb8dde82019-06-27 10:52:13 -040018#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrGpuResourcePriv.h"
20#include "src/gpu/GrMesh.h"
21#include "src/gpu/GrPathRendering.h"
22#include "src/gpu/GrPipeline.h"
23#include "src/gpu/GrRenderTargetPriv.h"
24#include "src/gpu/GrResourceCache.h"
25#include "src/gpu/GrResourceProvider.h"
26#include "src/gpu/GrSemaphore.h"
27#include "src/gpu/GrStencilAttachment.h"
28#include "src/gpu/GrStencilSettings.h"
29#include "src/gpu/GrSurfacePriv.h"
30#include "src/gpu/GrTexturePriv.h"
31#include "src/gpu/GrTextureProxyPriv.h"
32#include "src/gpu/GrTracing.h"
33#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070034
bsalomon@google.comd302f142011-03-03 13:54:13 +000035////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000036
Brian Salomone2826ab2019-06-04 15:58:31 -040037GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000038
bsalomoned0bcad2015-05-04 10:36:42 -070039GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070040
bsalomon6e2aad42016-04-01 11:54:31 -070041void GrGpu::disconnect(DisconnectType) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000042
bsalomon@google.comd302f142011-03-03 13:54:13 +000043////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000044
Greg Daniel8f5bbda2018-06-08 17:22:23 -040045bool GrGpu::IsACopyNeededForRepeatWrapMode(const GrCaps* caps, GrTextureProxy* texProxy,
46 int width, int height,
47 GrSamplerState::Filter filter,
48 GrTextureProducer::CopyParams* copyParams,
49 SkScalar scaleAdjust[2]) {
50 if (!caps->npotTextureTileSupport() &&
Greg Daniel09c94002018-06-08 22:11:51 +000051 (!SkIsPow2(width) || !SkIsPow2(height))) {
52 SkASSERT(scaleAdjust);
53 copyParams->fWidth = GrNextPow2(width);
54 copyParams->fHeight = GrNextPow2(height);
55 SkASSERT(scaleAdjust);
56 scaleAdjust[0] = ((SkScalar)copyParams->fWidth) / width;
57 scaleAdjust[1] = ((SkScalar)copyParams->fHeight) / height;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040058 switch (filter) {
Greg Daniel09c94002018-06-08 22:11:51 +000059 case GrSamplerState::Filter::kNearest:
60 copyParams->fFilter = GrSamplerState::Filter::kNearest;
61 break;
62 case GrSamplerState::Filter::kBilerp:
63 case GrSamplerState::Filter::kMipMap:
64 // We are only ever scaling up so no reason to ever indicate kMipMap.
65 copyParams->fFilter = GrSamplerState::Filter::kBilerp;
66 break;
67 }
68 return true;
69 }
Robert Phillipsabf7b762018-03-21 12:13:37 -040070
71 if (texProxy) {
72 // If the texture format itself doesn't support repeat wrap mode or mipmapping (and
73 // those capabilities are required) force a copy.
Brian Salomonfd98c2c2018-07-31 17:25:29 -040074 if (texProxy->hasRestrictedSampling()) {
Robert Phillipsabf7b762018-03-21 12:13:37 -040075 copyParams->fFilter = GrSamplerState::Filter::kNearest;
76 copyParams->fWidth = texProxy->width();
77 copyParams->fHeight = texProxy->height();
78 return true;
79 }
80 }
81
bsalomon100b8f82015-10-28 08:37:44 -070082 return false;
bsalomon045802d2015-10-20 07:58:01 -070083}
84
Greg Daniel8f5bbda2018-06-08 17:22:23 -040085bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
86 GrSamplerState::Filter filter,
87 GrTextureProducer::CopyParams* copyParams) {
88 SkASSERT(texProxy);
89 bool willNeedMips = GrSamplerState::Filter::kMipMap == filter && caps->mipMapSupport();
90 // If the texture format itself doesn't support mipmapping (and those capabilities are required)
91 // force a copy.
92 if (willNeedMips && texProxy->mipMapped() == GrMipMapped::kNo) {
93 copyParams->fFilter = GrSamplerState::Filter::kNearest;
94 copyParams->fWidth = texProxy->width();
95 copyParams->fHeight = texProxy->height();
96 return true;
97 }
98
99 return false;
100}
101
Brian Salomond2a8ae22019-09-10 16:03:59 -0400102static bool validate_texel_levels(int w, int h, const GrMipLevel* texels, int mipLevelCount,
103 int bpp, const GrCaps* caps) {
Brian Salomon1047a492019-07-02 12:25:21 -0400104 SkASSERT(mipLevelCount > 0);
105 bool hasBasePixels = texels[0].fPixels;
106 int levelsWithPixelsCnt = 0;
107 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
108 if (texels[currentMipLevel].fPixels) {
109 const size_t minRowBytes = w * bpp;
110 if (caps->writePixelsRowBytesSupport()) {
111 if (texels[currentMipLevel].fRowBytes < minRowBytes) {
112 return false;
113 }
114 if (texels[currentMipLevel].fRowBytes % bpp) {
115 return false;
116 }
117 } else {
118 if (texels[currentMipLevel].fRowBytes != minRowBytes) {
119 return false;
120 }
121 }
122 ++levelsWithPixelsCnt;
123 }
124 if (w == 1 && h == 1) {
125 if (currentMipLevel != mipLevelCount - 1) {
126 return false;
127 }
128 } else {
129 w = std::max(w / 2, 1);
130 h = std::max(h / 2, 1);
131 }
132 }
133 // Either just a base layer or a full stack is required.
134 if (mipLevelCount != 1 && (w != 1 || h != 1)) {
135 return false;
136 }
137 // Can specify just the base, all levels, or no levels.
138 if (!hasBasePixels) {
139 return levelsWithPixelsCnt == 0;
140 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400141 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400142}
143
Brian Salomon81536f22019-08-08 16:30:49 -0400144sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc,
145 const GrBackendFormat& format,
146 GrRenderable renderable,
147 int renderTargetSampleCnt,
148 SkBudgeted budgeted,
149 GrProtected isProtected,
150 const GrMipLevel texels[],
Brian Salomond2a8ae22019-09-10 16:03:59 -0400151 int texelLevelCount) {
Brian Salomone39526b2019-06-24 16:35:53 -0400152 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
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 Salomond2a8ae22019-09-10 16:03:59 -0400158 GrMipMapped mipMapped = texelLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel6fa62e22019-08-07 15:52:37 -0400159 if (!this->caps()->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig,
160 renderable, 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 Salomond2a8ae22019-09-10 16:03:59 -0400170 if (texelLevelCount) {
Brian Salomon1047a492019-07-02 12:25:21 -0400171 int bpp = GrBytesPerPixel(desc.fConfig);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400172 if (!validate_texel_levels(desc.fWidth, desc.fHeight, texels, texelLevelCount, bpp,
173 this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400174 return nullptr;
175 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400176 }
177
178 int mipLevelCount = SkTMax(1, texelLevelCount);
179 uint32_t levelClearMask = 0;
180 if (this->caps()->shouldInitializeTextures()) {
181 if (texelLevelCount) {
182 for (int i = 0; i < mipLevelCount; ++i) {
183 if (!texels->fPixels) {
184 levelClearMask |= static_cast<uint32_t>(1 << i);
185 }
186 }
187 } else {
188 levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1);
189 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400190 }
191
Robert Phillips92de6312017-05-23 07:43:48 -0400192 this->handleDirtyContext();
Brian Salomon81536f22019-08-08 16:30:49 -0400193 sk_sp<GrTexture> tex = this->onCreateTexture(desc,
194 format,
195 renderable,
196 renderTargetSampleCnt,
197 budgeted,
198 isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -0400199 mipLevelCount,
200 levelClearMask);
201
bsalomonb12ea412015-02-02 21:19:50 -0800202 if (tex) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400203 SkASSERT(tex->backendFormat() == format);
204 SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400205 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
cblume55f2d2d2016-02-26 13:20:48 -0800206 tex->resourcePriv().removeScratchKey();
207 }
bsalomonb12ea412015-02-02 21:19:50 -0800208 fStats.incTextureCreates();
Brian Salomond2a8ae22019-09-10 16:03:59 -0400209 bool markMipLevelsClean = false;
210 // Currently if level 0 does not have pixels then no other level may, as enforced by
211 // validate_texel_levels.
212 if (texelLevelCount && texels[0].fPixels) {
213 auto textureColorType = GrPixelConfigToColorType(desc.fConfig);
214 auto dataColorType = textureColorType;
215 if (!this->writePixels(tex.get(), 0, 0, desc.fWidth, desc.fHeight, textureColorType,
216 dataColorType, texels, texelLevelCount)) {
217 return nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800218 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400219 // Currently if level[1] of mip map has pixel data then so must all other levels.
220 // as enforced by validate_texel_levels.
221 markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels);
222 fStats.incTextureUploads();
223 } else if (levelClearMask && mipLevelCount > 1) {
224 markMipLevelsClean = true;
bsalomonb12ea412015-02-02 21:19:50 -0800225 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400226 if (markMipLevelsClean) {
227 tex->texturePriv().markMipMapsClean();
228 }
Chris Dalton3f7932e2019-08-19 00:39:13 -0600229 if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
230 SkASSERT(GrRenderable::kYes == renderable);
231 tex->asRenderTarget()->setRequiresManualMSAAResolve();
232 }
bsalomonb12ea412015-02-02 21:19:50 -0800233 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000234 return tex;
235}
236
Brian Salomon4eb38b72019-08-05 12:58:39 -0400237sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, const GrBackendFormat& format,
238 GrRenderable renderable, int renderTargetSampleCnt,
239 SkBudgeted budgeted, GrProtected isProtected) {
240 return this->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
241 isProtected, nullptr, 0);
Brian Salomonc7dced52019-07-18 15:02:01 +0000242}
243
Brian Salomonbb8dde82019-06-27 10:52:13 -0400244sk_sp<GrTexture> GrGpu::createCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -0400245 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400246 SkImage::CompressionType compressionType,
247 SkBudgeted budgeted, const void* data,
248 size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400249 // If we ever add a new CompressionType, we should add a check here to make sure the
250 // GrBackendFormat and CompressionType are compatible with eachother.
251 SkASSERT(compressionType == SkImage::kETC1_CompressionType);
252
Brian Salomonbb8dde82019-06-27 10:52:13 -0400253 this->handleDirtyContext();
254 if (width < 1 || width > this->caps()->maxTextureSize() ||
255 height < 1 || height > this->caps()->maxTextureSize()) {
256 return nullptr;
257 }
Brian Salomona3e29962019-07-16 11:52:08 -0400258 // Note if we relax the requirement that data must be provided then we must check
259 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400260 if (!data) {
261 return nullptr;
262 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400263 if (!this->caps()->isFormatTexturable(format)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -0400264 return nullptr;
265 }
266 if (dataSize < GrCompressedDataSize(compressionType, width, height)) {
267 return nullptr;
268 }
Greg Daniel7bfc9132019-08-14 14:23:53 -0400269 return this->onCreateCompressedTexture(width, height, format, compressionType, budgeted, data);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400270}
271
Greg Daniel7ef28f32017-04-20 16:41:55 +0000272sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400273 GrColorType colorType,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500274 GrWrapOwnership ownership, GrWrapCacheable cacheable,
275 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500276 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000277 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400278
279 const GrCaps* caps = this->caps();
280 SkASSERT(caps);
281
Greg Daniel7bfc9132019-08-14 14:23:53 -0400282 if (!caps->isFormatTexturable(backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800283 return nullptr;
284 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400285 if (backendTex.width() > caps->maxTextureSize() ||
286 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800287 return nullptr;
288 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400289
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400290 return this->onWrapBackendTexture(backendTex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400291}
Eric Karl5c779752017-05-08 12:02:07 -0700292
Brian Salomond17f6582017-07-19 18:28:58 -0400293sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips0902c982019-07-16 07:47:56 -0400294 int sampleCnt, GrColorType colorType,
295 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500296 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400297 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500298 if (sampleCnt < 1) {
299 return nullptr;
300 }
Robert Phillips0902c982019-07-16 07:47:56 -0400301
302 const GrCaps* caps = this->caps();
303
Greg Daniel7bfc9132019-08-14 14:23:53 -0400304 if (!caps->isFormatTexturable(backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400305 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400306 return nullptr;
307 }
308
Robert Phillips0902c982019-07-16 07:47:56 -0400309 if (backendTex.width() > caps->maxRenderTargetSize() ||
310 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400311 return nullptr;
312 }
Robert Phillips0902c982019-07-16 07:47:56 -0400313 sk_sp<GrTexture> tex = this->onWrapRenderableBackendTexture(backendTex, sampleCnt, colorType,
314 ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400315 SkASSERT(!tex || tex->asRenderTarget());
Chris Dalton3f7932e2019-08-19 00:39:13 -0600316 if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) {
317 tex->asRenderTarget()->setRequiresManualMSAAResolve();
318 }
bungeman6bd52842016-10-27 09:30:08 -0700319 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000320}
321
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400322sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
323 GrColorType colorType) {
324 this->handleDirtyContext();
325
326 const GrCaps* caps = this->caps();
327
Greg Daniel6fa62e22019-08-07 15:52:37 -0400328 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800329 return nullptr;
330 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400331
332 return this->onWrapBackendRenderTarget(backendRT, colorType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000333}
334
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400335sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
336 int sampleCnt,
337 GrColorType colorType) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500338 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400339
340 const GrCaps* caps = this->caps();
341
342 int maxSize = caps->maxTextureSize();
343 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
344 return nullptr;
345 }
346
Greg Daniel6fa62e22019-08-07 15:52:37 -0400347 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400348 return nullptr;
349 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400350
Chris Dalton3f7932e2019-08-19 00:39:13 -0600351 auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt, colorType);
352 if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) {
353 rt->setRequiresManualMSAAResolve();
354 }
355 return rt;
ericrkf7b8b8a2016-02-24 14:49:51 -0800356}
357
Greg Danielb46add82019-01-02 14:51:29 -0500358sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
359 const GrVkDrawableInfo& vkInfo) {
360 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
361}
362
363sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
364 const GrVkDrawableInfo& vkInfo) {
365 // This is only supported on Vulkan so we default to returning nullptr here
366 return nullptr;
367}
368
Brian Salomondbf70722019-02-07 11:31:24 -0500369sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
370 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400371 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000372 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500373 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700374 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700375 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700376 }
cdalton397536c2016-03-25 12:15:03 -0700377 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800378}
379
Greg Daniel46cfbc62019-06-07 11:43:30 -0400380bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -0400381 const SkIPoint& dstPoint) {
Brian Salomone39526b2019-06-24 16:35:53 -0400382 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700383 SkASSERT(dst && src);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500384
385 if (dst->readOnly()) {
386 return false;
387 }
388
joshualitt1cbdcde2015-08-21 11:53:29 -0700389 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500390
Greg Daniele227fe42019-08-21 13:52:24 -0400391 return this->onCopySurface(dst, src, srcRect, dstPoint);
joshualitt1cbdcde2015-08-21 11:53:29 -0700392}
393
Brian Salomona6948702018-06-01 15:33:20 -0400394bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400395 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
396 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400397 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500398 SkASSERT(surface);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400399 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500400
Brian Salomon1d435302019-07-01 13:05:28 -0400401 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
402 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
403 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700404 return false;
405 }
406
Brian Salomon1047a492019-07-02 12:25:21 -0400407 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
408 if (!this->caps()->readPixelsRowBytesSupport()) {
409 if (rowBytes != minRowBytes) {
410 return false;
411 }
412 } else {
413 if (rowBytes < minRowBytes) {
414 return false;
415 }
416 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
417 return false;
418 }
419 }
420
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400421 if (this->caps()->isFormatCompressed(surface->backendFormat())) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500422 return false;
423 }
424
Brian Salomonbf7b6202016-11-11 16:08:03 -0500425 this->handleDirtyContext();
426
Brian Salomonf77c1462019-08-01 15:19:29 -0400427 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
428 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000429}
430
Brian Salomona9b04b92018-06-01 15:04:28 -0400431bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400432 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400433 const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) {
Brian Salomone39526b2019-06-24 16:35:53 -0400434 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500435 SkASSERT(surface);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400436 SkASSERT(this->caps()->isFormatTexturableAndUploadable(surfaceColorType,
437 surface->backendFormat()));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500438
439 if (surface->readOnly()) {
440 return false;
441 }
442
Brian Salomon1047a492019-07-02 12:25:21 -0400443 if (mipLevelCount == 0) {
444 return false;
445 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400446 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400447 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
448 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400449 if (!bounds.contains(subRect)) {
450 return false;
451 }
452 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
453 // We require that if the texels are mipped, than the write region is the entire surface
454 return false;
455 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400456
Brian Salomonb28cb682019-07-26 12:48:47 -0400457 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -0400458 if (!validate_texel_levels(width, height, texels, mipLevelCount, bpp, this->caps())) {
Brian Salomon1047a492019-07-02 12:25:21 -0400459 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800460 }
jvanverth2dc29942015-09-01 07:16:46 -0700461
bsalomon@google.com6f379512011-11-16 20:36:03 +0000462 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400463 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400464 texels, mipLevelCount, prepForTexSampling)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700465 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400466 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800467 fStats.incTextureUploads();
468 return true;
469 }
470 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000471}
472
Brian Salomone05ba5a2019-04-08 11:59:07 -0400473bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400474 GrColorType textureColorType, GrColorType bufferColorType,
475 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400476 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500477 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700478 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400479 SkASSERT(this->caps()->isFormatTexturableAndUploadable(textureColorType,
480 texture->backendFormat()));
jvanverth17aa0472016-01-05 10:41:27 -0800481
Brian Salomonc67c31c2018-12-06 10:00:03 -0500482 if (texture->readOnly()) {
483 return false;
484 }
485
Greg Daniel660cc992017-06-26 14:55:05 -0400486 // We require that the write region is contained in the texture
487 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
488 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
489 if (!bounds.contains(subRect)) {
490 return false;
491 }
492
Brian Salomonb28cb682019-07-26 12:48:47 -0400493 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400494 if (this->caps()->writePixelsRowBytesSupport()) {
495 if (rowBytes < SkToSizeT(bpp * width)) {
496 return false;
497 }
498 if (rowBytes % bpp) {
499 return false;
500 }
501 } else {
502 if (rowBytes != SkToSizeT(bpp * width)) {
503 return false;
504 }
505 }
506
jvanverth17aa0472016-01-05 10:41:27 -0800507 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400508 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
509 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700510 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500511 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800512 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700513
jvanverth17aa0472016-01-05 10:41:27 -0800514 return true;
515 }
516 return false;
517}
518
Brian Salomon26de56e2019-04-10 12:14:26 -0400519bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400520 GrColorType surfaceColorType, GrColorType bufferColorType,
521 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400522 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400523 SkASSERT(surface);
524 SkASSERT(transferBuffer);
Greg Daniel7bfc9132019-08-14 14:23:53 -0400525 SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat()));
Brian Salomonf77c1462019-08-01 15:19:29 -0400526
Greg Danielba88ab62019-07-26 09:14:01 -0400527#ifdef SK_DEBUG
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400528 auto supportedRead = this->caps()->supportedReadPixelsColorType(
529 surfaceColorType, surface->backendFormat(), bufferColorType);
Greg Danielba88ab62019-07-26 09:14:01 -0400530 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
531 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
532#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400533
534 // We require that the write region is contained in the texture
535 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
536 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
537 if (!bounds.contains(subRect)) {
538 return false;
539 }
540
541 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400542 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
543 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400544 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400545 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400546 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400547 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400548}
549
Brian Salomon930f9392018-06-20 16:25:26 -0400550bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400551 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400552 SkASSERT(texture);
553 SkASSERT(this->caps()->mipMapSupport());
554 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
555 SkASSERT(texture->texturePriv().mipMapsAreDirty());
556 SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500557 if (texture->readOnly()) {
558 return false;
559 }
Brian Salomon930f9392018-06-20 16:25:26 -0400560 if (this->onRegenerateMipMapLevels(texture)) {
561 texture->texturePriv().markMipMapsClean();
562 return true;
563 }
564 return false;
565}
566
Brian Salomon1f05d452019-02-08 12:33:08 -0500567void GrGpu::resetTextureBindings() {
568 this->handleDirtyContext();
569 this->onResetTextureBindings();
570}
571
Brian Salomon1fabd512018-02-09 09:54:25 -0500572void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000573 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000574 this->handleDirtyContext();
Brian Salomon1fabd512018-02-09 09:54:25 -0500575 this->onResolveRenderTarget(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000576}
577
Brian Salomon1fabd512018-02-09 09:54:25 -0500578void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
579 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700580 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500581 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700582 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
583 if (nullptr == bounds || !bounds->isEmpty()) {
584 if (GrRenderTarget* target = surface->asRenderTarget()) {
Brian Salomon1fabd512018-02-09 09:54:25 -0500585 SkIRect flippedBounds;
586 if (kBottomLeft_GrSurfaceOrigin == origin && bounds) {
587 flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom,
588 bounds->fRight, surface->height() - bounds->fTop};
589 bounds = &flippedBounds;
590 }
jvanverth900bd4a2016-04-29 13:53:12 -0700591 target->flagAsNeedingResolve(bounds);
592 }
593 GrTexture* texture = surface->asTexture();
594 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400595 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700596 }
597 }
598}
599
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600600int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700601 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600602 SkASSERT(renderTarget->numSamples() > 1 ||
603 (renderTarget->renderTargetPriv().getStencilAttachment() &&
604 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700605
606 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600607 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700608 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
609}
610
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400611GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
612 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500613 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400614 const GrFlushInfo& info,
615 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400616 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500617 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500618 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500619
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400620 if (this->caps()->semaphoreSupport()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400621 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniel51316782017-08-02 15:10:09 +0000622 sk_sp<GrSemaphore> semaphore;
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400623 if (info.fSignalSemaphores[i].isInitialized()) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500624 semaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400625 info.fSignalSemaphores[i],
626 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500627 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000628 } else {
Robert Phillips6be756b2018-01-16 15:07:54 -0500629 semaphore = resourceProvider->makeSemaphore(false);
Greg Daniel51316782017-08-02 15:10:09 +0000630 }
Greg Daniel858e12c2018-12-06 11:11:37 -0500631 this->insertSemaphore(semaphore);
Greg Daniel51316782017-08-02 15:10:09 +0000632
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400633 if (!info.fSignalSemaphores[i].isInitialized()) {
634 info.fSignalSemaphores[i] = semaphore->backendSemaphore();
Greg Daniel51316782017-08-02 15:10:09 +0000635 }
636 }
637 }
Greg Daniel797efca2019-05-09 14:04:20 -0400638 this->onFinishFlush(proxies, n, access, info, externalRequests);
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400639 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000640 : GrSemaphoresSubmitted::kNo;
641}
Brian Osman71a18892017-08-10 10:23:25 -0400642
Kevin Lubickf4def342018-10-04 12:52:50 -0400643#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400644void GrGpu::dumpJSON(SkJSONWriter* writer) const {
645 writer->beginObject();
646
647 // TODO: Is there anything useful in the base class to dump here?
648
649 this->onDumpJSON(writer);
650
651 writer->endObject();
652}
Kevin Lubickf4def342018-10-04 12:52:50 -0400653#else
654void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
655#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400656
Robert Phillipsf0ced622019-05-16 09:06:25 -0400657#if GR_TEST_UTILS
658
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500659#if GR_GPU_STATS
660void GrGpu::Stats::dump(SkString* out) {
661 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
662 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
663 out->appendf("Textures Created: %d\n", fTextureCreates);
664 out->appendf("Texture Uploads: %d\n", fTextureUploads);
665 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400666 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500667 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
668 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400669 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500670}
671
672void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
673 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
674 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500675}
676
677#endif
678
Robert Phillips646f6372018-09-25 09:31:10 -0400679#endif