blob: 79cfbf48a7bbe8940956ee874b8d917efb73c8fa [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,
103 const GrCaps* caps) {
104 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 }
141 return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount;
142}
143
Robert Phillips67d52cf2017-06-05 13:38:13 -0400144sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500145 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomone39526b2019-06-24 16:35:53 -0400146 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbb8dde82019-06-27 10:52:13 -0400147 if (GrPixelConfigIsCompressed(origDesc.fConfig)) {
148 // Call GrGpu::createCompressedTexture.
149 return nullptr;
150 }
cblume55f2d2d2016-02-26 13:20:48 -0800151 GrSurfaceDesc desc = origDesc;
152
Brian Salomonbdecacf2018-02-02 20:32:49 -0500153 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
154 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700156 }
157
Brian Salomonbdecacf2018-02-02 20:32:49 -0500158 bool isRT = desc.fFlags & kRenderTarget_GrSurfaceFlag;
159 if (isRT) {
160 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
161 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400162 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500163 SkASSERT(desc.fSampleCnt > 0 && desc.fSampleCnt <= 64);
egdanielb0e1be22015-04-22 13:27:39 -0700164
Brian Salomon1047a492019-07-02 12:25:21 -0400165 if (mipLevelCount) {
166 if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) {
167 return nullptr;
168 }
169 int bpp = GrBytesPerPixel(desc.fConfig);
170 if (!validate_levels(desc.fWidth, desc.fHeight, texels, mipLevelCount, bpp, this->caps())) {
171 return nullptr;
172 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400173 }
174
Robert Phillips92de6312017-05-23 07:43:48 -0400175 this->handleDirtyContext();
Brian Salomon58389b92018-03-07 13:01:25 -0500176 sk_sp<GrTexture> tex = this->onCreateTexture(desc, budgeted, texels, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800177 if (tex) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500178 if (!this->caps()->reuseScratchTextures() && !isRT) {
cblume55f2d2d2016-02-26 13:20:48 -0800179 tex->resourcePriv().removeScratchKey();
180 }
bsalomonb12ea412015-02-02 21:19:50 -0800181 fStats.incTextureCreates();
Robert Phillips590533f2017-07-11 14:22:35 -0400182 if (mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -0800183 if (texels[0].fPixels) {
184 fStats.incTextureUploads();
185 }
bsalomonb12ea412015-02-02 21:19:50 -0800186 }
187 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000188 return tex;
189}
190
Robert Phillips646e4292017-06-13 12:44:56 -0400191sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
Brian Salomon58389b92018-03-07 13:01:25 -0500192 return this->createTexture(desc, budgeted, nullptr, 0);
Robert Phillips646e4292017-06-13 12:44:56 -0400193}
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 }
204 if (!data) {
205 return nullptr;
206 }
207 if (!this->caps()->isConfigTexturable(GrCompressionTypePixelConfig(compressionType))) {
208 return nullptr;
209 }
210 if (dataSize < GrCompressedDataSize(compressionType, width, height)) {
211 return nullptr;
212 }
213 return this->onCreateCompressedTexture(width, height, compressionType, budgeted, data);
214}
215
Greg Daniel7ef28f32017-04-20 16:41:55 +0000216sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500217 GrWrapOwnership ownership, GrWrapCacheable cacheable,
218 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500219 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000220 this->handleDirtyContext();
Jim Van Verth1676cb92019-01-15 13:24:45 -0500221 SkASSERT(this->caps());
Greg Daniel7ef28f32017-04-20 16:41:55 +0000222 if (!this->caps()->isConfigTexturable(backendTex.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800223 return nullptr;
224 }
Brian Salomond17f6582017-07-19 18:28:58 -0400225 if (backendTex.width() > this->caps()->maxTextureSize() ||
226 backendTex.height() > this->caps()->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800227 return nullptr;
228 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500229 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400230}
Eric Karl5c779752017-05-08 12:02:07 -0700231
Brian Salomond17f6582017-07-19 18:28:58 -0400232sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500233 int sampleCnt, GrWrapOwnership ownership,
234 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400235 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500236 if (sampleCnt < 1) {
237 return nullptr;
238 }
Brian Salomond17f6582017-07-19 18:28:58 -0400239 if (!this->caps()->isConfigTexturable(backendTex.config()) ||
Brian Salomonbdecacf2018-02-02 20:32:49 -0500240 !this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400241 return nullptr;
242 }
243
244 if (backendTex.width() > this->caps()->maxRenderTargetSize() ||
245 backendTex.height() > this->caps()->maxRenderTargetSize()) {
246 return nullptr;
247 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500248 sk_sp<GrTexture> tex =
249 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400250 SkASSERT(!tex || tex->asRenderTarget());
bungeman6bd52842016-10-27 09:30:08 -0700251 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000252}
253
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400254sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500255 if (0 == this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800256 return nullptr;
257 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000258 this->handleDirtyContext();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400259 return this->onWrapBackendRenderTarget(backendRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000260}
261
Greg Daniel7ef28f32017-04-20 16:41:55 +0000262sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000263 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500264 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config())) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800265 return nullptr;
266 }
267 int maxSize = this->caps()->maxTextureSize();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000268 if (tex.width() > maxSize || tex.height() > maxSize) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800269 return nullptr;
270 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500271 this->handleDirtyContext();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400272 return this->onWrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800273}
274
Greg Danielb46add82019-01-02 14:51:29 -0500275sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
276 const GrVkDrawableInfo& vkInfo) {
277 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
278}
279
280sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
281 const GrVkDrawableInfo& vkInfo) {
282 // This is only supported on Vulkan so we default to returning nullptr here
283 return nullptr;
284}
285
Brian Salomondbf70722019-02-07 11:31:24 -0500286sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
287 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400288 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000289 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500290 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700291 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700292 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700293 }
cdalton397536c2016-03-25 12:15:03 -0700294 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800295}
296
Greg Daniel46cfbc62019-06-07 11:43:30 -0400297bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
298 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) {
Brian Salomone39526b2019-06-24 16:35:53 -0400299 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700300 SkASSERT(dst && src);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500301
302 if (dst->readOnly()) {
303 return false;
304 }
305
joshualitt1cbdcde2015-08-21 11:53:29 -0700306 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500307
Greg Daniel46cfbc62019-06-07 11:43:30 -0400308 return this->onCopySurface(dst, src, srcRect, dstPoint, canDiscardOutsideDstRect);
joshualitt1cbdcde2015-08-21 11:53:29 -0700309}
310
Brian Salomona6948702018-06-01 15:33:20 -0400311bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
312 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400313 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500314 SkASSERT(surface);
315
Brian Salomon1d435302019-07-01 13:05:28 -0400316 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
317 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
318 if (!bounds.contains(subRect)) {
egdaniel6d901da2015-07-30 12:02:15 -0700319 return false;
320 }
321
Brian Salomon1047a492019-07-02 12:25:21 -0400322 size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width);
323 if (!this->caps()->readPixelsRowBytesSupport()) {
324 if (rowBytes != minRowBytes) {
325 return false;
326 }
327 } else {
328 if (rowBytes < minRowBytes) {
329 return false;
330 }
331 if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) {
332 return false;
333 }
334 }
335
Jim Van Verth1676cb92019-01-15 13:24:45 -0500336 if (GrPixelConfigIsCompressed(surface->config())) {
337 return false;
338 }
339
Brian Salomonbf7b6202016-11-11 16:08:03 -0500340 this->handleDirtyContext();
341
Brian Salomona6948702018-06-01 15:33:20 -0400342 return this->onReadPixels(surface, left, top, width, height, dstColorType, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000343}
344
Brian Salomona9b04b92018-06-01 15:04:28 -0400345bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
346 GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount) {
Brian Salomone39526b2019-06-24 16:35:53 -0400347 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500348 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500349
350 if (surface->readOnly()) {
351 return false;
352 }
353
Brian Salomon1047a492019-07-02 12:25:21 -0400354 if (mipLevelCount == 0) {
355 return false;
356 } else if (mipLevelCount == 1) {
Greg Daniel660cc992017-06-26 14:55:05 -0400357 // We require that if we are not mipped, then the write region is contained in the surface
Brian Salomon1d435302019-07-01 13:05:28 -0400358 auto subRect = SkIRect::MakeXYWH(left, top, width, height);
359 auto bounds = SkIRect::MakeWH(surface->width(), surface->height());
Greg Daniel660cc992017-06-26 14:55:05 -0400360 if (!bounds.contains(subRect)) {
361 return false;
362 }
363 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
364 // We require that if the texels are mipped, than the write region is the entire surface
365 return false;
366 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400367
Brian Salomon1047a492019-07-02 12:25:21 -0400368 int bpp = GrColorTypeBytesPerPixel(srcColorType);
369 if (!validate_levels(width, height, texels, mipLevelCount, bpp, this->caps())) {
370 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800371 }
jvanverth2dc29942015-09-01 07:16:46 -0700372
bsalomon@google.com6f379512011-11-16 20:36:03 +0000373 this->handleDirtyContext();
Brian Salomona9b04b92018-06-01 15:04:28 -0400374 if (this->onWritePixels(surface, left, top, width, height, srcColorType, texels,
Brian Salomonc320b152018-02-20 14:05:36 -0500375 mipLevelCount)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700376 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400377 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800378 fStats.incTextureUploads();
379 return true;
380 }
381 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000382}
383
Brian Salomone05ba5a2019-04-08 11:59:07 -0400384bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
385 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
386 size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400387 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500388 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700389 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800390
Brian Salomonc67c31c2018-12-06 10:00:03 -0500391 if (texture->readOnly()) {
392 return false;
393 }
394
Greg Daniel660cc992017-06-26 14:55:05 -0400395 // We require that the write region is contained in the texture
396 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
397 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
398 if (!bounds.contains(subRect)) {
399 return false;
400 }
401
Brian Salomon1047a492019-07-02 12:25:21 -0400402 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
403 if (this->caps()->writePixelsRowBytesSupport()) {
404 if (rowBytes < SkToSizeT(bpp * width)) {
405 return false;
406 }
407 if (rowBytes % bpp) {
408 return false;
409 }
410 } else {
411 if (rowBytes != SkToSizeT(bpp * width)) {
412 return false;
413 }
414 }
415
jvanverth17aa0472016-01-05 10:41:27 -0800416 this->handleDirtyContext();
Brian Salomone05ba5a2019-04-08 11:59:07 -0400417 if (this->onTransferPixelsTo(texture, left, top, width, height, bufferColorType, transferBuffer,
418 offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700419 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500420 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800421 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700422
jvanverth17aa0472016-01-05 10:41:27 -0800423 return true;
424 }
425 return false;
426}
427
Brian Salomon26de56e2019-04-10 12:14:26 -0400428bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
429 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
430 size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400431 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400432 SkASSERT(surface);
433 SkASSERT(transferBuffer);
Brian Salomon26de56e2019-04-10 12:14:26 -0400434 SkASSERT(this->caps()->transferFromOffsetAlignment(bufferColorType));
435 SkASSERT(offset % this->caps()->transferFromOffsetAlignment(bufferColorType) == 0);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400436
437 // We require that the write region is contained in the texture
438 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
439 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
440 if (!bounds.contains(subRect)) {
441 return false;
442 }
443
444 this->handleDirtyContext();
Brian Salomon26de56e2019-04-10 12:14:26 -0400445 if (this->onTransferPixelsFrom(surface, left, top, width, height, bufferColorType,
446 transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400447 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400448 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400449 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400450 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400451}
452
Brian Salomon930f9392018-06-20 16:25:26 -0400453bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400454 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400455 SkASSERT(texture);
456 SkASSERT(this->caps()->mipMapSupport());
457 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
458 SkASSERT(texture->texturePriv().mipMapsAreDirty());
459 SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500460 if (texture->readOnly()) {
461 return false;
462 }
Brian Salomon930f9392018-06-20 16:25:26 -0400463 if (this->onRegenerateMipMapLevels(texture)) {
464 texture->texturePriv().markMipMapsClean();
465 return true;
466 }
467 return false;
468}
469
Brian Salomon1f05d452019-02-08 12:33:08 -0500470void GrGpu::resetTextureBindings() {
471 this->handleDirtyContext();
472 this->onResetTextureBindings();
473}
474
Brian Salomon1fabd512018-02-09 09:54:25 -0500475void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000476 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000477 this->handleDirtyContext();
Brian Salomon1fabd512018-02-09 09:54:25 -0500478 this->onResolveRenderTarget(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000479}
480
Brian Salomon1fabd512018-02-09 09:54:25 -0500481void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
482 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700483 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500484 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700485 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
486 if (nullptr == bounds || !bounds->isEmpty()) {
487 if (GrRenderTarget* target = surface->asRenderTarget()) {
Brian Salomon1fabd512018-02-09 09:54:25 -0500488 SkIRect flippedBounds;
489 if (kBottomLeft_GrSurfaceOrigin == origin && bounds) {
490 flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom,
491 bounds->fRight, surface->height() - bounds->fTop};
492 bounds = &flippedBounds;
493 }
jvanverth900bd4a2016-04-29 13:53:12 -0700494 target->flagAsNeedingResolve(bounds);
495 }
496 GrTexture* texture = surface->asTexture();
497 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400498 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700499 }
500 }
501}
502
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600503int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700504 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Daltoneffee202019-07-01 22:28:03 -0600505 SkASSERT(renderTarget->numSamples() > 1 ||
506 (renderTarget->renderTargetPriv().getStencilAttachment() &&
507 renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1));
Chris Daltond7291ba2019-03-07 14:17:03 -0700508
509 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600510 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700511 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
512}
513
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400514GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
515 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500516 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400517 const GrFlushInfo& info,
518 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400519 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500520 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500521 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500522
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400523 if (this->caps()->semaphoreSupport()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400524 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniel51316782017-08-02 15:10:09 +0000525 sk_sp<GrSemaphore> semaphore;
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400526 if (info.fSignalSemaphores[i].isInitialized()) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500527 semaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400528 info.fSignalSemaphores[i],
529 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500530 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000531 } else {
Robert Phillips6be756b2018-01-16 15:07:54 -0500532 semaphore = resourceProvider->makeSemaphore(false);
Greg Daniel51316782017-08-02 15:10:09 +0000533 }
Greg Daniel858e12c2018-12-06 11:11:37 -0500534 this->insertSemaphore(semaphore);
Greg Daniel51316782017-08-02 15:10:09 +0000535
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400536 if (!info.fSignalSemaphores[i].isInitialized()) {
537 info.fSignalSemaphores[i] = semaphore->backendSemaphore();
Greg Daniel51316782017-08-02 15:10:09 +0000538 }
539 }
540 }
Greg Daniel797efca2019-05-09 14:04:20 -0400541 this->onFinishFlush(proxies, n, access, info, externalRequests);
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400542 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000543 : GrSemaphoresSubmitted::kNo;
544}
Brian Osman71a18892017-08-10 10:23:25 -0400545
Kevin Lubickf4def342018-10-04 12:52:50 -0400546#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400547void GrGpu::dumpJSON(SkJSONWriter* writer) const {
548 writer->beginObject();
549
550 // TODO: Is there anything useful in the base class to dump here?
551
552 this->onDumpJSON(writer);
553
554 writer->endObject();
555}
Kevin Lubickf4def342018-10-04 12:52:50 -0400556#else
557void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
558#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400559
Robert Phillipsf0ced622019-05-16 09:06:25 -0400560#if GR_TEST_UTILS
561
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500562#if GR_GPU_STATS
563void GrGpu::Stats::dump(SkString* out) {
564 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
565 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
566 out->appendf("Textures Created: %d\n", fTextureCreates);
567 out->appendf("Texture Uploads: %d\n", fTextureUploads);
568 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400569 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500570 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
571 out->appendf("Number of draws: %d\n", fNumDraws);
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -0400572 out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500573}
574
575void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
576 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
577 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500578}
579
580#endif
581
Robert Phillips646f6372018-09-25 09:31:10 -0400582#endif