blob: 8c161fef28be1c0373d4d65b258ff4815602ebd5 [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 Salomon1047a492019-07-02 12:25:21 -0400102static bool validate_levels(int w, int h, const GrMipLevel texels[], int mipLevelCount, int bpp,
Brian Salomona3e29962019-07-16 11:52:08 -0400103 const GrCaps* caps, bool mustHaveDataForAllLevels = false) {
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 Salomona3e29962019-07-16 11:52:08 -0400141 if (levelsWithPixelsCnt == 1 && !mustHaveDataForAllLevels) {
142 return true;
143 }
144 return levelsWithPixelsCnt == mipLevelCount;
Brian Salomon1047a492019-07-02 12:25:21 -0400145}
146
Brian Salomon4eb38b72019-08-05 12:58:39 -0400147sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& origDesc, const GrBackendFormat& format,
148 GrRenderable renderable, int renderTargetSampleCnt,
149 SkBudgeted budgeted, GrProtected isProtected,
150 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomone39526b2019-06-24 16:35:53 -0400151 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400152 if (GrPixelConfigIsCompressed(origDesc.fConfig)) {
153 // Call GrGpu::createCompressedTexture.
154 return nullptr;
155 }
cblume55f2d2d2016-02-26 13:20:48 -0800156 GrSurfaceDesc desc = origDesc;
157
Brian Salomonbdecacf2018-02-02 20:32:49 -0500158 GrMipMapped mipMapped = mipLevelCount > 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);
egdanielb0e1be22015-04-22 13:27:39 -0700170
Brian Salomona3e29962019-07-16 11:52:08 -0400171 bool mustHaveDataForAllLevels = this->caps()->createTextureMustSpecifyAllLevels();
Brian Salomon1047a492019-07-02 12:25:21 -0400172 if (mipLevelCount) {
Brian Salomon1047a492019-07-02 12:25:21 -0400173 int bpp = GrBytesPerPixel(desc.fConfig);
Brian Salomona3e29962019-07-16 11:52:08 -0400174 if (!validate_levels(desc.fWidth, desc.fHeight, texels, mipLevelCount, bpp, this->caps(),
175 mustHaveDataForAllLevels)) {
Brian Salomon1047a492019-07-02 12:25:21 -0400176 return nullptr;
177 }
Brian Salomona3e29962019-07-16 11:52:08 -0400178 } else if (mustHaveDataForAllLevels) {
179 return nullptr;
Brian Salomond17b4a62017-05-23 16:53:47 -0400180 }
181
Robert Phillips92de6312017-05-23 07:43:48 -0400182 this->handleDirtyContext();
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400183 sk_sp<GrTexture> tex = this->onCreateTexture(desc, renderable, renderTargetSampleCnt, budgeted,
184 isProtected, texels, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800185 if (tex) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400186 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
cblume55f2d2d2016-02-26 13:20:48 -0800187 tex->resourcePriv().removeScratchKey();
188 }
bsalomonb12ea412015-02-02 21:19:50 -0800189 fStats.incTextureCreates();
Robert Phillips590533f2017-07-11 14:22:35 -0400190 if (mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -0800191 if (texels[0].fPixels) {
192 fStats.incTextureUploads();
193 }
bsalomonb12ea412015-02-02 21:19:50 -0800194 }
Brian Salomon4eb38b72019-08-05 12:58:39 -0400195 // TODO: Assert this once format is passed to onCreateTexture().
196 // SkASSERT(tex->backendFormat() == format);
bsalomonb12ea412015-02-02 21:19:50 -0800197 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000198 return tex;
199}
200
Brian Salomon4eb38b72019-08-05 12:58:39 -0400201sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, const GrBackendFormat& format,
202 GrRenderable renderable, int renderTargetSampleCnt,
203 SkBudgeted budgeted, GrProtected isProtected) {
204 return this->createTexture(desc, format, renderable, renderTargetSampleCnt, budgeted,
205 isProtected, nullptr, 0);
Brian Salomonc7dced52019-07-18 15:02:01 +0000206}
207
Brian Salomonbb8dde82019-06-27 10:52:13 -0400208sk_sp<GrTexture> GrGpu::createCompressedTexture(int width, int height,
209 SkImage::CompressionType compressionType,
210 SkBudgeted budgeted, const void* data,
211 size_t dataSize) {
212 this->handleDirtyContext();
213 if (width < 1 || width > this->caps()->maxTextureSize() ||
214 height < 1 || height > this->caps()->maxTextureSize()) {
215 return nullptr;
216 }
Brian Salomona3e29962019-07-16 11:52:08 -0400217 // Note if we relax the requirement that data must be provided then we must check
218 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400219 if (!data) {
220 return nullptr;
221 }
222 if (!this->caps()->isConfigTexturable(GrCompressionTypePixelConfig(compressionType))) {
223 return nullptr;
224 }
225 if (dataSize < GrCompressedDataSize(compressionType, width, height)) {
226 return nullptr;
227 }
228 return this->onCreateCompressedTexture(width, height, compressionType, budgeted, data);
229}
230
Greg Daniel7ef28f32017-04-20 16:41:55 +0000231sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400232 GrColorType colorType,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500233 GrWrapOwnership ownership, GrWrapCacheable cacheable,
234 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500235 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000236 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400237
238 const GrCaps* caps = this->caps();
239 SkASSERT(caps);
240
241 if (!caps->isFormatTexturable(colorType, backendTex.getBackendFormat())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800242 return nullptr;
243 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400244 if (backendTex.width() > caps->maxTextureSize() ||
245 backendTex.height() > caps->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800246 return nullptr;
247 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400248
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400249 return this->onWrapBackendTexture(backendTex, colorType, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400250}
Eric Karl5c779752017-05-08 12:02:07 -0700251
Brian Salomond17f6582017-07-19 18:28:58 -0400252sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips0902c982019-07-16 07:47:56 -0400253 int sampleCnt, GrColorType colorType,
254 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500255 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400256 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500257 if (sampleCnt < 1) {
258 return nullptr;
259 }
Robert Phillips0902c982019-07-16 07:47:56 -0400260
261 const GrCaps* caps = this->caps();
262
Robert Phillips0902c982019-07-16 07:47:56 -0400263 if (!caps->isFormatTexturable(colorType, backendTex.getBackendFormat()) ||
Greg Daniel6fa62e22019-08-07 15:52:37 -0400264 !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400265 return nullptr;
266 }
267
Robert Phillips0902c982019-07-16 07:47:56 -0400268 if (backendTex.width() > caps->maxRenderTargetSize() ||
269 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400270 return nullptr;
271 }
Robert Phillips0902c982019-07-16 07:47:56 -0400272 sk_sp<GrTexture> tex = this->onWrapRenderableBackendTexture(backendTex, sampleCnt, colorType,
273 ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400274 SkASSERT(!tex || tex->asRenderTarget());
bungeman6bd52842016-10-27 09:30:08 -0700275 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000276}
277
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400278sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
279 GrColorType colorType) {
280 this->handleDirtyContext();
281
282 const GrCaps* caps = this->caps();
283
Greg Daniel6fa62e22019-08-07 15:52:37 -0400284 if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800285 return nullptr;
286 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400287
288 return this->onWrapBackendRenderTarget(backendRT, colorType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000289}
290
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400291sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex,
292 int sampleCnt,
293 GrColorType colorType) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500294 this->handleDirtyContext();
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400295
296 const GrCaps* caps = this->caps();
297
298 int maxSize = caps->maxTextureSize();
299 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
300 return nullptr;
301 }
302
Greg Daniel6fa62e22019-08-07 15:52:37 -0400303 if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) {
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400304 return nullptr;
305 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400306
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400307 return this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt, colorType);
ericrkf7b8b8a2016-02-24 14:49:51 -0800308}
309
Greg Danielb46add82019-01-02 14:51:29 -0500310sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
311 const GrVkDrawableInfo& vkInfo) {
312 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
313}
314
315sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
316 const GrVkDrawableInfo& vkInfo) {
317 // This is only supported on Vulkan so we default to returning nullptr here
318 return nullptr;
319}
320
Brian Salomondbf70722019-02-07 11:31:24 -0500321sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
322 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400323 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000324 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500325 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700326 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700327 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700328 }
cdalton397536c2016-03-25 12:15:03 -0700329 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800330}
331
Greg Daniel46cfbc62019-06-07 11:43:30 -0400332bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
333 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) {
Brian Salomone39526b2019-06-24 16:35:53 -0400334 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700335 SkASSERT(dst && src);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500336
337 if (dst->readOnly()) {
338 return false;
339 }
340
joshualitt1cbdcde2015-08-21 11:53:29 -0700341 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500342
Greg Daniel46cfbc62019-06-07 11:43:30 -0400343 return this->onCopySurface(dst, src, srcRect, dstPoint, canDiscardOutsideDstRect);
joshualitt1cbdcde2015-08-21 11:53:29 -0700344}
345
Brian Salomona6948702018-06-01 15:33:20 -0400346bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400347 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
348 size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400349 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500350 SkASSERT(surface);
Brian Salomonf77c1462019-08-01 15:19:29 -0400351 SkASSERT(this->caps()->isFormatTexturable(surfaceColorType, surface->backendFormat()));
Brian Salomonbf7b6202016-11-11 16:08:03 -0500352
Brian Salomon1d435302019-07-01 13:05:28 -0400353 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
354 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
355 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700356 return false;
357 }
358
Brian Salomon1047a492019-07-02 12:25:21 -0400359 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
360 if (!this->caps()->readPixelsRowBytesSupport()) {
361 if (rowBytes != minRowBytes) {
362 return false;
363 }
364 } else {
365 if (rowBytes < minRowBytes) {
366 return false;
367 }
368 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
369 return false;
370 }
371 }
372
Jim Van Verth1676cb92019-01-15 13:24:45 -0500373 if (GrPixelConfigIsCompressed(surface->config())) {
374 return false;
375 }
376
Brian Salomonbf7b6202016-11-11 16:08:03 -0500377 this->handleDirtyContext();
378
Brian Salomonf77c1462019-08-01 15:19:29 -0400379 return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType,
380 buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000381}
382
Brian Salomona9b04b92018-06-01 15:04:28 -0400383bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400384 GrColorType surfaceColorType, GrColorType srcColorType,
385 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomone39526b2019-06-24 16:35:53 -0400386 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500387 SkASSERT(surface);
Brian Salomonf77c1462019-08-01 15:19:29 -0400388 SkASSERT(this->caps()->isFormatTexturable(surfaceColorType, surface->backendFormat()));
Brian Salomonc67c31c2018-12-06 10:00:03 -0500389
390 if (surface->readOnly()) {
391 return false;
392 }
393
Brian Salomon1047a492019-07-02 12:25:21 -0400394 if (mipLevelCount == 0) {
395 return false;
396 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400397 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400398 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
399 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400400 if (!bounds.contains(subRect)) {
401 return false;
402 }
403 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
404 // We require that if the texels are mipped, than the write region is the entire surface
405 return false;
406 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400407
Brian Salomonb28cb682019-07-26 12:48:47 -0400408 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400409 if (!validate_levels(width, height, texels, mipLevelCount, bpp, this->caps())) {
410 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800411 }
jvanverth2dc29942015-09-01 07:16:46 -0700412
bsalomon@google.com6f379512011-11-16 20:36:03 +0000413 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400414 if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType,
415 texels, mipLevelCount)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700416 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400417 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800418 fStats.incTextureUploads();
419 return true;
420 }
421 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000422}
423
Brian Salomone05ba5a2019-04-08 11:59:07 -0400424bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400425 GrColorType textureColorType, GrColorType bufferColorType,
426 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400427 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500428 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700429 SkASSERT(transferBuffer);
Brian Salomonf77c1462019-08-01 15:19:29 -0400430 SkASSERT(this->caps()->isFormatTexturable(textureColorType, texture->backendFormat()));
jvanverth17aa0472016-01-05 10:41:27 -0800431
Brian Salomonc67c31c2018-12-06 10:00:03 -0500432 if (texture->readOnly()) {
433 return false;
434 }
435
Greg Daniel660cc992017-06-26 14:55:05 -0400436 // We require that the write region is contained in the texture
437 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
438 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
439 if (!bounds.contains(subRect)) {
440 return false;
441 }
442
Brian Salomonb28cb682019-07-26 12:48:47 -0400443 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Brian Salomon1047a492019-07-02 12:25:21 -0400444 if (this->caps()->writePixelsRowBytesSupport()) {
445 if (rowBytes < SkToSizeT(bpp * width)) {
446 return false;
447 }
448 if (rowBytes % bpp) {
449 return false;
450 }
451 } else {
452 if (rowBytes != SkToSizeT(bpp * width)) {
453 return false;
454 }
455 }
456
jvanverth17aa0472016-01-05 10:41:27 -0800457 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400458 if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType,
459 bufferColorType, transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700460 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500461 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800462 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700463
jvanverth17aa0472016-01-05 10:41:27 -0800464 return true;
465 }
466 return false;
467}
468
Brian Salomon26de56e2019-04-10 12:14:26 -0400469bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400470 GrColorType surfaceColorType, GrColorType bufferColorType,
471 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400472 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400473 SkASSERT(surface);
474 SkASSERT(transferBuffer);
Brian Salomonf77c1462019-08-01 15:19:29 -0400475 SkASSERT(this->caps()->isFormatTexturable(surfaceColorType, surface->backendFormat()));
476
Greg Danielba88ab62019-07-26 09:14:01 -0400477#ifdef SK_DEBUG
478 GrColorType surfCT = GrPixelConfigToColorType(surface->config());
479 auto supportedRead = this->caps()->supportedReadPixelsColorType(surfCT,
480 surface->backendFormat(),
481 bufferColorType);
482 SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer);
483 SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0);
484#endif
Brian Salomone05ba5a2019-04-08 11:59:07 -0400485
486 // 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(surface->width(), surface->height());
489 if (!bounds.contains(subRect)) {
490 return false;
491 }
492
493 this->handleDirtyContext();
Brian Salomonf77c1462019-08-01 15:19:29 -0400494 if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
495 bufferColorType, transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400496 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400497 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400498 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400499 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400500}
501
Brian Salomon930f9392018-06-20 16:25:26 -0400502bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400503 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400504 SkASSERT(texture);
505 SkASSERT(this->caps()->mipMapSupport());
506 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
507 SkASSERT(texture->texturePriv().mipMapsAreDirty());
508 SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500509 if (texture->readOnly()) {
510 return false;
511 }
Brian Salomon930f9392018-06-20 16:25:26 -0400512 if (this->onRegenerateMipMapLevels(texture)) {
513 texture->texturePriv().markMipMapsClean();
514 return true;
515 }
516 return false;
517}
518
Brian Salomon1f05d452019-02-08 12:33:08 -0500519void GrGpu::resetTextureBindings() {
520 this->handleDirtyContext();
521 this->onResetTextureBindings();
522}
523
Brian Salomon1fabd512018-02-09 09:54:25 -0500524void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000525 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000526 this->handleDirtyContext();
Brian Salomon1fabd512018-02-09 09:54:25 -0500527 this->onResolveRenderTarget(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000528}
529
Brian Salomon1fabd512018-02-09 09:54:25 -0500530void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
531 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700532 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500533 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700534 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
535 if (nullptr == bounds || !bounds->isEmpty()) {
536 if (GrRenderTarget* target = surface->asRenderTarget()) {
Brian Salomon1fabd512018-02-09 09:54:25 -0500537 SkIRect flippedBounds;
538 if (kBottomLeft_GrSurfaceOrigin == origin && bounds) {
539 flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom,
540 bounds->fRight, surface->height() - bounds->fTop};
541 bounds = &flippedBounds;
542 }
jvanverth900bd4a2016-04-29 13:53:12 -0700543 target->flagAsNeedingResolve(bounds);
544 }
545 GrTexture* texture = surface->asTexture();
546 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400547 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700548 }
549 }
550}
551
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600552int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700553 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600554 SkASSERT(renderTarget->numSamples() > 1 ||
555 (renderTarget->renderTargetPriv().getStencilAttachment() &&
556 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700557
558 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600559 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700560 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
561}
562
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400563GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
564 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500565 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400566 const GrFlushInfo& info,
567 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400568 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500569 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500570 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500571
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400572 if (this->caps()->semaphoreSupport()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400573 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniel51316782017-08-02 15:10:09 +0000574 sk_sp<GrSemaphore> semaphore;
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400575 if (info.fSignalSemaphores[i].isInitialized()) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500576 semaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400577 info.fSignalSemaphores[i],
578 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500579 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000580 } else {
Robert Phillips6be756b2018-01-16 15:07:54 -0500581 semaphore = resourceProvider->makeSemaphore(false);
Greg Daniel51316782017-08-02 15:10:09 +0000582 }
Greg Daniel858e12c2018-12-06 11:11:37 -0500583 this->insertSemaphore(semaphore);
Greg Daniel51316782017-08-02 15:10:09 +0000584
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400585 if (!info.fSignalSemaphores[i].isInitialized()) {
586 info.fSignalSemaphores[i] = semaphore->backendSemaphore();
Greg Daniel51316782017-08-02 15:10:09 +0000587 }
588 }
589 }
Greg Daniel797efca2019-05-09 14:04:20 -0400590 this->onFinishFlush(proxies, n, access, info, externalRequests);
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400591 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000592 : GrSemaphoresSubmitted::kNo;
593}
Brian Osman71a18892017-08-10 10:23:25 -0400594
Kevin Lubickf4def342018-10-04 12:52:50 -0400595#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400596void GrGpu::dumpJSON(SkJSONWriter* writer) const {
597 writer->beginObject();
598
599 // TODO: Is there anything useful in the base class to dump here?
600
601 this->onDumpJSON(writer);
602
603 writer->endObject();
604}
Kevin Lubickf4def342018-10-04 12:52:50 -0400605#else
606void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
607#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400608
Robert Phillipsf0ced622019-05-16 09:06:25 -0400609#if GR_TEST_UTILS
610
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500611#if GR_GPU_STATS
612void GrGpu::Stats::dump(SkString* out) {
613 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
614 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
615 out->appendf("Textures Created: %d\n", fTextureCreates);
616 out->appendf("Texture Uploads: %d\n", fTextureUploads);
617 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400618 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500619 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
620 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400621 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500622}
623
624void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
625 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
626 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500627}
628
629#endif
630
Robert Phillips646f6372018-09-25 09:31:10 -0400631#endif