blob: 380288888e96c67d7b1ab61684480edb420ff27b [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 Salomonf2c2ba92019-07-17 09:59:59 -0400147sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& origDesc, GrRenderable renderable,
148 SkBudgeted budgeted, const GrMipLevel texels[],
149 int mipLevelCount) {
Brian Salomone39526b2019-06-24 16:35:53 -0400150 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400151 if (GrPixelConfigIsCompressed(origDesc.fConfig)) {
152 // Call GrGpu::createCompressedTexture.
153 return nullptr;
154 }
cblume55f2d2d2016-02-26 13:20:48 -0800155 GrSurfaceDesc desc = origDesc;
156
Brian Salomonbdecacf2018-02-02 20:32:49 -0500157 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400158 if (!this->caps()->validateSurfaceDesc(desc, renderable, mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700159 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700160 }
161
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400162 if (renderable == GrRenderable::kYes) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500163 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
164 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400165 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500166 SkASSERT(desc.fSampleCnt > 0 && desc.fSampleCnt <= 64);
egdanielb0e1be22015-04-22 13:27:39 -0700167
Brian Salomona3e29962019-07-16 11:52:08 -0400168 bool mustHaveDataForAllLevels = this->caps()->createTextureMustSpecifyAllLevels();
Brian Salomon1047a492019-07-02 12:25:21 -0400169 if (mipLevelCount) {
Brian Salomon1047a492019-07-02 12:25:21 -0400170 int bpp = GrBytesPerPixel(desc.fConfig);
Brian Salomona3e29962019-07-16 11:52:08 -0400171 if (!validate_levels(desc.fWidth, desc.fHeight, texels, mipLevelCount, bpp, this->caps(),
172 mustHaveDataForAllLevels)) {
Brian Salomon1047a492019-07-02 12:25:21 -0400173 return nullptr;
174 }
Brian Salomona3e29962019-07-16 11:52:08 -0400175 } else if (mustHaveDataForAllLevels) {
176 return nullptr;
Brian Salomond17b4a62017-05-23 16:53:47 -0400177 }
178
Robert Phillips92de6312017-05-23 07:43:48 -0400179 this->handleDirtyContext();
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400180 sk_sp<GrTexture> tex = this->onCreateTexture(desc, renderable, budgeted, texels, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800181 if (tex) {
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400182 if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) {
cblume55f2d2d2016-02-26 13:20:48 -0800183 tex->resourcePriv().removeScratchKey();
184 }
bsalomonb12ea412015-02-02 21:19:50 -0800185 fStats.incTextureCreates();
Robert Phillips590533f2017-07-11 14:22:35 -0400186 if (mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -0800187 if (texels[0].fPixels) {
188 fStats.incTextureUploads();
189 }
bsalomonb12ea412015-02-02 21:19:50 -0800190 }
191 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000192 return tex;
193}
194
Brian Salomonbb8dde82019-06-27 10:52:13 -0400195sk_sp<GrTexture> GrGpu::createCompressedTexture(int width, int height,
196 SkImage::CompressionType compressionType,
197 SkBudgeted budgeted, const void* data,
198 size_t dataSize) {
199 this->handleDirtyContext();
200 if (width < 1 || width > this->caps()->maxTextureSize() ||
201 height < 1 || height > this->caps()->maxTextureSize()) {
202 return nullptr;
203 }
Brian Salomona3e29962019-07-16 11:52:08 -0400204 // Note if we relax the requirement that data must be provided then we must check
205 // caps()->shouldInitializeTextures() here.
Brian Salomonbb8dde82019-06-27 10:52:13 -0400206 if (!data) {
207 return nullptr;
208 }
209 if (!this->caps()->isConfigTexturable(GrCompressionTypePixelConfig(compressionType))) {
210 return nullptr;
211 }
212 if (dataSize < GrCompressedDataSize(compressionType, width, height)) {
213 return nullptr;
214 }
215 return this->onCreateCompressedTexture(width, height, compressionType, budgeted, data);
216}
217
Greg Daniel7ef28f32017-04-20 16:41:55 +0000218sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500219 GrWrapOwnership ownership, GrWrapCacheable cacheable,
220 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500221 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000222 this->handleDirtyContext();
Robert Phillipsdd399802019-07-18 12:28:00 +0000223 SkASSERT(this->caps());
224 if (!this->caps()->isConfigTexturable(backendTex.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800225 return nullptr;
226 }
Robert Phillipsdd399802019-07-18 12:28:00 +0000227 if (backendTex.width() > this->caps()->maxTextureSize() ||
228 backendTex.height() > this->caps()->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800229 return nullptr;
230 }
Robert Phillipsdd399802019-07-18 12:28:00 +0000231 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400232}
Eric Karl5c779752017-05-08 12:02:07 -0700233
Brian Salomond17f6582017-07-19 18:28:58 -0400234sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips0902c982019-07-16 07:47:56 -0400235 int sampleCnt, GrColorType colorType,
236 GrWrapOwnership ownership,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500237 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400238 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500239 if (sampleCnt < 1) {
240 return nullptr;
241 }
Robert Phillips0902c982019-07-16 07:47:56 -0400242
243 const GrCaps* caps = this->caps();
244
245 SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
246 caps->getConfigFromBackendFormat(
247 backendTex.getBackendFormat(),
248 colorType)));
249
250 if (!caps->isFormatTexturable(colorType, backendTex.getBackendFormat()) ||
251 !caps->getRenderTargetSampleCount(sampleCnt, colorType, backendTex.getBackendFormat())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400252 return nullptr;
253 }
254
Robert Phillips0902c982019-07-16 07:47:56 -0400255 if (backendTex.width() > caps->maxRenderTargetSize() ||
256 backendTex.height() > caps->maxRenderTargetSize()) {
Brian Salomond17f6582017-07-19 18:28:58 -0400257 return nullptr;
258 }
Robert Phillips0902c982019-07-16 07:47:56 -0400259 sk_sp<GrTexture> tex = this->onWrapRenderableBackendTexture(backendTex, sampleCnt, colorType,
260 ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400261 SkASSERT(!tex || tex->asRenderTarget());
bungeman6bd52842016-10-27 09:30:08 -0700262 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000263}
264
Robert Phillipsdd399802019-07-18 12:28:00 +0000265sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
266 if (0 == this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800267 return nullptr;
268 }
Robert Phillipsdd399802019-07-18 12:28:00 +0000269 this->handleDirtyContext();
270 return this->onWrapBackendRenderTarget(backendRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000271}
272
Robert Phillipsdd399802019-07-18 12:28:00 +0000273sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
274 int sampleCnt) {
275 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config())) {
276 return nullptr;
277 }
278 int maxSize = this->caps()->maxTextureSize();
279 if (tex.width() > maxSize || tex.height() > maxSize) {
280 return nullptr;
281 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500282 this->handleDirtyContext();
Robert Phillipsdd399802019-07-18 12:28:00 +0000283 return this->onWrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800284}
285
Greg Danielb46add82019-01-02 14:51:29 -0500286sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
287 const GrVkDrawableInfo& vkInfo) {
288 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
289}
290
291sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
292 const GrVkDrawableInfo& vkInfo) {
293 // This is only supported on Vulkan so we default to returning nullptr here
294 return nullptr;
295}
296
Brian Salomondbf70722019-02-07 11:31:24 -0500297sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
298 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400299 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000300 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500301 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700302 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700303 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700304 }
cdalton397536c2016-03-25 12:15:03 -0700305 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800306}
307
Greg Daniel46cfbc62019-06-07 11:43:30 -0400308bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
309 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) {
Brian Salomone39526b2019-06-24 16:35:53 -0400310 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700311 SkASSERT(dst && src);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500312
313 if (dst->readOnly()) {
314 return false;
315 }
316
joshualitt1cbdcde2015-08-21 11:53:29 -0700317 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500318
Greg Daniel46cfbc62019-06-07 11:43:30 -0400319 return this->onCopySurface(dst, src, srcRect, dstPoint, canDiscardOutsideDstRect);
joshualitt1cbdcde2015-08-21 11:53:29 -0700320}
321
Brian Salomona6948702018-06-01 15:33:20 -0400322bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
323 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400324 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500325 SkASSERT(surface);
326
Brian Salomon1d435302019-07-01 13:05:28 -0400327 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
328 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
329 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700330 return false;
331 }
332
Brian Salomon1047a492019-07-02 12:25:21 -0400333 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
334 if (!this->caps()->readPixelsRowBytesSupport()) {
335 if (rowBytes != minRowBytes) {
336 return false;
337 }
338 } else {
339 if (rowBytes < minRowBytes) {
340 return false;
341 }
342 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
343 return false;
344 }
345 }
346
Jim Van Verth1676cb92019-01-15 13:24:45 -0500347 if (GrPixelConfigIsCompressed(surface->config())) {
348 return false;
349 }
350
Brian Salomonbf7b6202016-11-11 16:08:03 -0500351 this->handleDirtyContext();
352
Brian Salomona6948702018-06-01 15:33:20 -0400353 return this->onReadPixels(surface, left, top, width, height, dstColorType, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000354}
355
Brian Salomona9b04b92018-06-01 15:04:28 -0400356bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
357 GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount) {
Brian Salomone39526b2019-06-24 16:35:53 -0400358 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500359 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500360
361 if (surface->readOnly()) {
362 return false;
363 }
364
Brian Salomon1047a492019-07-02 12:25:21 -0400365 if (mipLevelCount == 0) {
366 return false;
367 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400368 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400369 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
370 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400371 if (!bounds.contains(subRect)) {
372 return false;
373 }
374 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
375 // We require that if the texels are mipped, than the write region is the entire surface
376 return false;
377 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400378
Brian Salomon1047a492019-07-02 12:25:21 -0400379 int bpp = GrColorTypeBytesPerPixel(srcColorType);
380 if (!validate_levels(width, height, texels, mipLevelCount, bpp, this->caps())) {
381 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800382 }
jvanverth2dc29942015-09-01 07:16:46 -0700383
bsalomon@google.com6f379512011-11-16 20:36:03 +0000384 this->handleDirtyContext();
Brian Salomona9b04b92018-06-01 15:04:28 -0400385 if (this->onWritePixels(surface, left, top, width, height, srcColorType, texels,
Brian Salomonc320b152018-02-20 14:05:36 -0500386 mipLevelCount)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700387 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400388 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800389 fStats.incTextureUploads();
390 return true;
391 }
392 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000393}
394
Brian Salomone05ba5a2019-04-08 11:59:07 -0400395bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
396 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
397 size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400398 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500399 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700400 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800401
Brian Salomonc67c31c2018-12-06 10:00:03 -0500402 if (texture->readOnly()) {
403 return false;
404 }
405
Greg Daniel660cc992017-06-26 14:55:05 -0400406 // We require that the write region is contained in the texture
407 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
408 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
409 if (!bounds.contains(subRect)) {
410 return false;
411 }
412
Brian Salomon1047a492019-07-02 12:25:21 -0400413 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
414 if (this->caps()->writePixelsRowBytesSupport()) {
415 if (rowBytes < SkToSizeT(bpp * width)) {
416 return false;
417 }
418 if (rowBytes % bpp) {
419 return false;
420 }
421 } else {
422 if (rowBytes != SkToSizeT(bpp * width)) {
423 return false;
424 }
425 }
426
jvanverth17aa0472016-01-05 10:41:27 -0800427 this->handleDirtyContext();
Brian Salomone05ba5a2019-04-08 11:59:07 -0400428 if (this->onTransferPixelsTo(texture, left, top, width, height, bufferColorType, transferBuffer,
429 offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700430 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500431 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800432 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700433
jvanverth17aa0472016-01-05 10:41:27 -0800434 return true;
435 }
436 return false;
437}
438
Brian Salomon26de56e2019-04-10 12:14:26 -0400439bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
440 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
441 size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400442 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400443 SkASSERT(surface);
444 SkASSERT(transferBuffer);
Brian Salomon26de56e2019-04-10 12:14:26 -0400445 SkASSERT(this->caps()->transferFromOffsetAlignment(bufferColorType));
446 SkASSERT(offset % this->caps()->transferFromOffsetAlignment(bufferColorType) == 0);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400447
448 // We require that the write region is contained in the texture
449 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
450 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
451 if (!bounds.contains(subRect)) {
452 return false;
453 }
454
455 this->handleDirtyContext();
Brian Salomon26de56e2019-04-10 12:14:26 -0400456 if (this->onTransferPixelsFrom(surface, left, top, width, height, bufferColorType,
457 transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400458 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400459 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400460 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400461 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400462}
463
Brian Salomon930f9392018-06-20 16:25:26 -0400464bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400465 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400466 SkASSERT(texture);
467 SkASSERT(this->caps()->mipMapSupport());
468 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
469 SkASSERT(texture->texturePriv().mipMapsAreDirty());
470 SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500471 if (texture->readOnly()) {
472 return false;
473 }
Brian Salomon930f9392018-06-20 16:25:26 -0400474 if (this->onRegenerateMipMapLevels(texture)) {
475 texture->texturePriv().markMipMapsClean();
476 return true;
477 }
478 return false;
479}
480
Brian Salomon1f05d452019-02-08 12:33:08 -0500481void GrGpu::resetTextureBindings() {
482 this->handleDirtyContext();
483 this->onResetTextureBindings();
484}
485
Brian Salomon1fabd512018-02-09 09:54:25 -0500486void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000487 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000488 this->handleDirtyContext();
Brian Salomon1fabd512018-02-09 09:54:25 -0500489 this->onResolveRenderTarget(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000490}
491
Brian Salomon1fabd512018-02-09 09:54:25 -0500492void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
493 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700494 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500495 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700496 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
497 if (nullptr == bounds || !bounds->isEmpty()) {
498 if (GrRenderTarget* target = surface->asRenderTarget()) {
Brian Salomon1fabd512018-02-09 09:54:25 -0500499 SkIRect flippedBounds;
500 if (kBottomLeft_GrSurfaceOrigin == origin && bounds) {
501 flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom,
502 bounds->fRight, surface->height() - bounds->fTop};
503 bounds = &flippedBounds;
504 }
jvanverth900bd4a2016-04-29 13:53:12 -0700505 target->flagAsNeedingResolve(bounds);
506 }
507 GrTexture* texture = surface->asTexture();
508 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400509 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700510 }
511 }
512}
513
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600514int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700515 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600516 SkASSERT(renderTarget->numSamples() > 1 ||
517 (renderTarget->renderTargetPriv().getStencilAttachment() &&
518 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700519
520 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600521 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700522 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
523}
524
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400525GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
526 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500527 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400528 const GrFlushInfo& info,
529 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400530 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500531 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500532 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500533
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400534 if (this->caps()->semaphoreSupport()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400535 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniel51316782017-08-02 15:10:09 +0000536 sk_sp<GrSemaphore> semaphore;
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400537 if (info.fSignalSemaphores[i].isInitialized()) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500538 semaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400539 info.fSignalSemaphores[i],
540 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500541 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000542 } else {
Robert Phillips6be756b2018-01-16 15:07:54 -0500543 semaphore = resourceProvider->makeSemaphore(false);
Greg Daniel51316782017-08-02 15:10:09 +0000544 }
Greg Daniel858e12c2018-12-06 11:11:37 -0500545 this->insertSemaphore(semaphore);
Greg Daniel51316782017-08-02 15:10:09 +0000546
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400547 if (!info.fSignalSemaphores[i].isInitialized()) {
548 info.fSignalSemaphores[i] = semaphore->backendSemaphore();
Greg Daniel51316782017-08-02 15:10:09 +0000549 }
550 }
551 }
Greg Daniel797efca2019-05-09 14:04:20 -0400552 this->onFinishFlush(proxies, n, access, info, externalRequests);
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400553 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000554 : GrSemaphoresSubmitted::kNo;
555}
Brian Osman71a18892017-08-10 10:23:25 -0400556
Kevin Lubickf4def342018-10-04 12:52:50 -0400557#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400558void GrGpu::dumpJSON(SkJSONWriter* writer) const {
559 writer->beginObject();
560
561 // TODO: Is there anything useful in the base class to dump here?
562
563 this->onDumpJSON(writer);
564
565 writer->endObject();
566}
Kevin Lubickf4def342018-10-04 12:52:50 -0400567#else
568void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
569#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400570
Robert Phillipsf0ced622019-05-16 09:06:25 -0400571#if GR_TEST_UTILS
572
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500573#if GR_GPU_STATS
574void GrGpu::Stats::dump(SkString* out) {
575 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
576 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
577 out->appendf("Textures Created: %d\n", fTextureCreates);
578 out->appendf("Texture Uploads: %d\n", fTextureUploads);
579 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400580 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500581 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
582 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400583 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500584}
585
586void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
587 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
588 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500589}
590
591#endif
592
Robert Phillips646f6372018-09-25 09:31:10 -0400593#endif