blob: 52adb00cf83ce4d3edda24a59170f4344e0249e5 [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"
18#include "src/gpu/GrGpuResourcePriv.h"
19#include "src/gpu/GrMesh.h"
20#include "src/gpu/GrPathRendering.h"
21#include "src/gpu/GrPipeline.h"
22#include "src/gpu/GrRenderTargetPriv.h"
23#include "src/gpu/GrResourceCache.h"
24#include "src/gpu/GrResourceProvider.h"
25#include "src/gpu/GrSemaphore.h"
26#include "src/gpu/GrStencilAttachment.h"
27#include "src/gpu/GrStencilSettings.h"
28#include "src/gpu/GrSurfacePriv.h"
29#include "src/gpu/GrTexturePriv.h"
30#include "src/gpu/GrTextureProxyPriv.h"
31#include "src/gpu/GrTracing.h"
32#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070033
bsalomon@google.comd302f142011-03-03 13:54:13 +000034////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000035
Brian Salomone2826ab2019-06-04 15:58:31 -040036GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000037
bsalomoned0bcad2015-05-04 10:36:42 -070038GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070039
bsalomon6e2aad42016-04-01 11:54:31 -070040void GrGpu::disconnect(DisconnectType) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000041
bsalomon@google.comd302f142011-03-03 13:54:13 +000042////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000043
Greg Daniel8f5bbda2018-06-08 17:22:23 -040044bool GrGpu::IsACopyNeededForRepeatWrapMode(const GrCaps* caps, GrTextureProxy* texProxy,
45 int width, int height,
46 GrSamplerState::Filter filter,
47 GrTextureProducer::CopyParams* copyParams,
48 SkScalar scaleAdjust[2]) {
49 if (!caps->npotTextureTileSupport() &&
Greg Daniel09c94002018-06-08 22:11:51 +000050 (!SkIsPow2(width) || !SkIsPow2(height))) {
51 SkASSERT(scaleAdjust);
52 copyParams->fWidth = GrNextPow2(width);
53 copyParams->fHeight = GrNextPow2(height);
54 SkASSERT(scaleAdjust);
55 scaleAdjust[0] = ((SkScalar)copyParams->fWidth) / width;
56 scaleAdjust[1] = ((SkScalar)copyParams->fHeight) / height;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040057 switch (filter) {
Greg Daniel09c94002018-06-08 22:11:51 +000058 case GrSamplerState::Filter::kNearest:
59 copyParams->fFilter = GrSamplerState::Filter::kNearest;
60 break;
61 case GrSamplerState::Filter::kBilerp:
62 case GrSamplerState::Filter::kMipMap:
63 // We are only ever scaling up so no reason to ever indicate kMipMap.
64 copyParams->fFilter = GrSamplerState::Filter::kBilerp;
65 break;
66 }
67 return true;
68 }
Robert Phillipsabf7b762018-03-21 12:13:37 -040069
70 if (texProxy) {
71 // If the texture format itself doesn't support repeat wrap mode or mipmapping (and
72 // those capabilities are required) force a copy.
Brian Salomonfd98c2c2018-07-31 17:25:29 -040073 if (texProxy->hasRestrictedSampling()) {
Robert Phillipsabf7b762018-03-21 12:13:37 -040074 copyParams->fFilter = GrSamplerState::Filter::kNearest;
75 copyParams->fWidth = texProxy->width();
76 copyParams->fHeight = texProxy->height();
77 return true;
78 }
79 }
80
bsalomon100b8f82015-10-28 08:37:44 -070081 return false;
bsalomon045802d2015-10-20 07:58:01 -070082}
83
Greg Daniel8f5bbda2018-06-08 17:22:23 -040084bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
85 GrSamplerState::Filter filter,
86 GrTextureProducer::CopyParams* copyParams) {
87 SkASSERT(texProxy);
88 bool willNeedMips = GrSamplerState::Filter::kMipMap == filter && caps->mipMapSupport();
89 // If the texture format itself doesn't support mipmapping (and those capabilities are required)
90 // force a copy.
91 if (willNeedMips && texProxy->mipMapped() == GrMipMapped::kNo) {
92 copyParams->fFilter = GrSamplerState::Filter::kNearest;
93 copyParams->fWidth = texProxy->width();
94 copyParams->fHeight = texProxy->height();
95 return true;
96 }
97
98 return false;
99}
100
Robert Phillips67d52cf2017-06-05 13:38:13 -0400101sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500102 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomone39526b2019-06-24 16:35:53 -0400103 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
cblume55f2d2d2016-02-26 13:20:48 -0800104 GrSurfaceDesc desc = origDesc;
105
Brian Salomonbdecacf2018-02-02 20:32:49 -0500106 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
107 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700108 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700109 }
110
Brian Salomonbdecacf2018-02-02 20:32:49 -0500111 bool isRT = desc.fFlags & kRenderTarget_GrSurfaceFlag;
112 if (isRT) {
113 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
114 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400115 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500116 SkASSERT(desc.fSampleCnt > 0 && desc.fSampleCnt <= 64);
egdanielb0e1be22015-04-22 13:27:39 -0700117
Robert Phillips590533f2017-07-11 14:22:35 -0400118 if (mipLevelCount && (desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400119 return nullptr;
120 }
121
Jim Van Verth1676cb92019-01-15 13:24:45 -0500122 // We shouldn't be rendering into compressed textures
123 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || !isRT);
124 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || 1 == desc.fSampleCnt);
125
Robert Phillips92de6312017-05-23 07:43:48 -0400126 this->handleDirtyContext();
Brian Salomon58389b92018-03-07 13:01:25 -0500127 sk_sp<GrTexture> tex = this->onCreateTexture(desc, budgeted, texels, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800128 if (tex) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500129 if (!this->caps()->reuseScratchTextures() && !isRT) {
cblume55f2d2d2016-02-26 13:20:48 -0800130 tex->resourcePriv().removeScratchKey();
131 }
bsalomonb12ea412015-02-02 21:19:50 -0800132 fStats.incTextureCreates();
Robert Phillips590533f2017-07-11 14:22:35 -0400133 if (mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -0800134 if (texels[0].fPixels) {
135 fStats.incTextureUploads();
136 }
bsalomonb12ea412015-02-02 21:19:50 -0800137 }
138 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000139 return tex;
140}
141
Robert Phillips646e4292017-06-13 12:44:56 -0400142sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
Brian Salomon58389b92018-03-07 13:01:25 -0500143 return this->createTexture(desc, budgeted, nullptr, 0);
Robert Phillips646e4292017-06-13 12:44:56 -0400144}
145
Greg Daniel7ef28f32017-04-20 16:41:55 +0000146sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500147 GrWrapOwnership ownership, GrWrapCacheable cacheable,
148 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500149 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000150 this->handleDirtyContext();
Jim Van Verth1676cb92019-01-15 13:24:45 -0500151 SkASSERT(this->caps());
Greg Daniel7ef28f32017-04-20 16:41:55 +0000152 if (!this->caps()->isConfigTexturable(backendTex.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800153 return nullptr;
154 }
Brian Salomond17f6582017-07-19 18:28:58 -0400155 if (backendTex.width() > this->caps()->maxTextureSize() ||
156 backendTex.height() > this->caps()->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800157 return nullptr;
158 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500159 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400160}
Eric Karl5c779752017-05-08 12:02:07 -0700161
Brian Salomond17f6582017-07-19 18:28:58 -0400162sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500163 int sampleCnt, GrWrapOwnership ownership,
164 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400165 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500166 if (sampleCnt < 1) {
167 return nullptr;
168 }
Brian Salomond17f6582017-07-19 18:28:58 -0400169 if (!this->caps()->isConfigTexturable(backendTex.config()) ||
Brian Salomonbdecacf2018-02-02 20:32:49 -0500170 !this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400171 return nullptr;
172 }
173
174 if (backendTex.width() > this->caps()->maxRenderTargetSize() ||
175 backendTex.height() > this->caps()->maxRenderTargetSize()) {
176 return nullptr;
177 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500178 sk_sp<GrTexture> tex =
179 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400180 SkASSERT(!tex || tex->asRenderTarget());
bungeman6bd52842016-10-27 09:30:08 -0700181 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000182}
183
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400184sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500185 if (0 == this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800186 return nullptr;
187 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000188 this->handleDirtyContext();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400189 return this->onWrapBackendRenderTarget(backendRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000190}
191
Greg Daniel7ef28f32017-04-20 16:41:55 +0000192sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000193 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500194 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config())) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800195 return nullptr;
196 }
197 int maxSize = this->caps()->maxTextureSize();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000198 if (tex.width() > maxSize || tex.height() > maxSize) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800199 return nullptr;
200 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500201 this->handleDirtyContext();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400202 return this->onWrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800203}
204
Greg Danielb46add82019-01-02 14:51:29 -0500205sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
206 const GrVkDrawableInfo& vkInfo) {
207 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
208}
209
210sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
211 const GrVkDrawableInfo& vkInfo) {
212 // This is only supported on Vulkan so we default to returning nullptr here
213 return nullptr;
214}
215
Brian Salomondbf70722019-02-07 11:31:24 -0500216sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
217 GrAccessPattern accessPattern, const void* data) {
Brian Salomone39526b2019-06-24 16:35:53 -0400218 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000219 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500220 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700221 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700222 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700223 }
cdalton397536c2016-03-25 12:15:03 -0700224 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800225}
226
Greg Daniel46cfbc62019-06-07 11:43:30 -0400227bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
228 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) {
Brian Salomone39526b2019-06-24 16:35:53 -0400229 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
joshualitt1cbdcde2015-08-21 11:53:29 -0700230 SkASSERT(dst && src);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500231
232 if (dst->readOnly()) {
233 return false;
234 }
235
joshualitt1cbdcde2015-08-21 11:53:29 -0700236 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500237
Greg Daniel46cfbc62019-06-07 11:43:30 -0400238 return this->onCopySurface(dst, src, srcRect, dstPoint, canDiscardOutsideDstRect);
joshualitt1cbdcde2015-08-21 11:53:29 -0700239}
240
Brian Salomona6948702018-06-01 15:33:20 -0400241bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
242 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400243 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500244 SkASSERT(surface);
245
Brian Salomonc320b152018-02-20 14:05:36 -0500246 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6d901da2015-07-30 12:02:15 -0700247 if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
248 &left, &top, &width, &height,
249 &buffer,
250 &rowBytes)) {
251 return false;
252 }
253
Jim Van Verth1676cb92019-01-15 13:24:45 -0500254 if (GrPixelConfigIsCompressed(surface->config())) {
255 return false;
256 }
257
Brian Salomonbf7b6202016-11-11 16:08:03 -0500258 this->handleDirtyContext();
259
Brian Salomona6948702018-06-01 15:33:20 -0400260 return this->onReadPixels(surface, left, top, width, height, dstColorType, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000261}
262
Brian Salomona9b04b92018-06-01 15:04:28 -0400263bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
264 GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount) {
Brian Salomone39526b2019-06-24 16:35:53 -0400265 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500266 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500267
268 if (surface->readOnly()) {
269 return false;
270 }
271
Robert Phillips590533f2017-07-11 14:22:35 -0400272 if (1 == mipLevelCount) {
Greg Daniel660cc992017-06-26 14:55:05 -0400273 // We require that if we are not mipped, then the write region is contained in the surface
274 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
275 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
276 if (!bounds.contains(subRect)) {
277 return false;
278 }
279 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
280 // We require that if the texels are mipped, than the write region is the entire surface
281 return false;
282 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400283
Robert Phillips590533f2017-07-11 14:22:35 -0400284 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800285 if (!texels[currentMipLevel].fPixels ) {
286 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800287 }
288 }
jvanverth2dc29942015-09-01 07:16:46 -0700289
bsalomon@google.com6f379512011-11-16 20:36:03 +0000290 this->handleDirtyContext();
Brian Salomona9b04b92018-06-01 15:04:28 -0400291 if (this->onWritePixels(surface, left, top, width, height, srcColorType, texels,
Brian Salomonc320b152018-02-20 14:05:36 -0500292 mipLevelCount)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700293 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400294 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800295 fStats.incTextureUploads();
296 return true;
297 }
298 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000299}
300
Brian Salomone05ba5a2019-04-08 11:59:07 -0400301bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
302 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
303 size_t offset, size_t rowBytes) {
Brian Salomone39526b2019-06-24 16:35:53 -0400304 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500305 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700306 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800307
Brian Salomonc67c31c2018-12-06 10:00:03 -0500308 if (texture->readOnly()) {
309 return false;
310 }
311
Greg Daniel660cc992017-06-26 14:55:05 -0400312 // We require that the write region is contained in the texture
313 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
314 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
315 if (!bounds.contains(subRect)) {
316 return false;
317 }
318
jvanverth17aa0472016-01-05 10:41:27 -0800319 this->handleDirtyContext();
Brian Salomone05ba5a2019-04-08 11:59:07 -0400320 if (this->onTransferPixelsTo(texture, left, top, width, height, bufferColorType, transferBuffer,
321 offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700322 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500323 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800324 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700325
jvanverth17aa0472016-01-05 10:41:27 -0800326 return true;
327 }
328 return false;
329}
330
Brian Salomon26de56e2019-04-10 12:14:26 -0400331bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
332 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
333 size_t offset) {
Brian Salomone39526b2019-06-24 16:35:53 -0400334 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400335 SkASSERT(surface);
336 SkASSERT(transferBuffer);
Brian Salomon26de56e2019-04-10 12:14:26 -0400337 SkASSERT(this->caps()->transferFromOffsetAlignment(bufferColorType));
338 SkASSERT(offset % this->caps()->transferFromOffsetAlignment(bufferColorType) == 0);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400339
340 // We require that the write region is contained in the texture
341 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
342 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
343 if (!bounds.contains(subRect)) {
344 return false;
345 }
346
347 this->handleDirtyContext();
Brian Salomon26de56e2019-04-10 12:14:26 -0400348 if (this->onTransferPixelsFrom(surface, left, top, width, height, bufferColorType,
349 transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400350 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400351 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400352 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400353 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400354}
355
Brian Salomon930f9392018-06-20 16:25:26 -0400356bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
Brian Salomone39526b2019-06-24 16:35:53 -0400357 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Brian Salomon930f9392018-06-20 16:25:26 -0400358 SkASSERT(texture);
359 SkASSERT(this->caps()->mipMapSupport());
360 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
361 SkASSERT(texture->texturePriv().mipMapsAreDirty());
362 SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500363 if (texture->readOnly()) {
364 return false;
365 }
Brian Salomon930f9392018-06-20 16:25:26 -0400366 if (this->onRegenerateMipMapLevels(texture)) {
367 texture->texturePriv().markMipMapsClean();
368 return true;
369 }
370 return false;
371}
372
Brian Salomon1f05d452019-02-08 12:33:08 -0500373void GrGpu::resetTextureBindings() {
374 this->handleDirtyContext();
375 this->onResetTextureBindings();
376}
377
Brian Salomon1fabd512018-02-09 09:54:25 -0500378void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000379 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000380 this->handleDirtyContext();
Brian Salomon1fabd512018-02-09 09:54:25 -0500381 this->onResolveRenderTarget(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000382}
383
Brian Salomon1fabd512018-02-09 09:54:25 -0500384void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
385 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700386 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500387 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700388 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
389 if (nullptr == bounds || !bounds->isEmpty()) {
390 if (GrRenderTarget* target = surface->asRenderTarget()) {
Brian Salomon1fabd512018-02-09 09:54:25 -0500391 SkIRect flippedBounds;
392 if (kBottomLeft_GrSurfaceOrigin == origin && bounds) {
393 flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom,
394 bounds->fRight, surface->height() - bounds->fTop};
395 bounds = &flippedBounds;
396 }
jvanverth900bd4a2016-04-29 13:53:12 -0700397 target->flagAsNeedingResolve(bounds);
398 }
399 GrTexture* texture = surface->asTexture();
400 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400401 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700402 }
403 }
404}
405
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600406int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700407 SkASSERT(this->caps()->sampleLocationsSupport());
Chris Dalton6ce447a2019-06-23 18:07:38 -0600408 SkASSERT(renderTarget->numSamples() > 1);
Chris Daltond7291ba2019-03-07 14:17:03 -0700409
410 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600411 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700412 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
413}
414
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400415GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
416 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500417 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400418 const GrFlushInfo& info,
419 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomone39526b2019-06-24 16:35:53 -0400420 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Danield2073452018-12-07 11:20:33 -0500421 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500422 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500423
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400424 if (this->caps()->semaphoreSupport()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400425 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniel51316782017-08-02 15:10:09 +0000426 sk_sp<GrSemaphore> semaphore;
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400427 if (info.fSignalSemaphores[i].isInitialized()) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500428 semaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400429 info.fSignalSemaphores[i],
430 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500431 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000432 } else {
Robert Phillips6be756b2018-01-16 15:07:54 -0500433 semaphore = resourceProvider->makeSemaphore(false);
Greg Daniel51316782017-08-02 15:10:09 +0000434 }
Greg Daniel858e12c2018-12-06 11:11:37 -0500435 this->insertSemaphore(semaphore);
Greg Daniel51316782017-08-02 15:10:09 +0000436
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400437 if (!info.fSignalSemaphores[i].isInitialized()) {
438 info.fSignalSemaphores[i] = semaphore->backendSemaphore();
Greg Daniel51316782017-08-02 15:10:09 +0000439 }
440 }
441 }
Greg Daniel797efca2019-05-09 14:04:20 -0400442 this->onFinishFlush(proxies, n, access, info, externalRequests);
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400443 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000444 : GrSemaphoresSubmitted::kNo;
445}
Brian Osman71a18892017-08-10 10:23:25 -0400446
Kevin Lubickf4def342018-10-04 12:52:50 -0400447#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400448void GrGpu::dumpJSON(SkJSONWriter* writer) const {
449 writer->beginObject();
450
451 // TODO: Is there anything useful in the base class to dump here?
452
453 this->onDumpJSON(writer);
454
455 writer->endObject();
456}
Kevin Lubickf4def342018-10-04 12:52:50 -0400457#else
458void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
459#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400460
Robert Phillipsf0ced622019-05-16 09:06:25 -0400461#if GR_TEST_UTILS
462
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500463#if GR_GPU_STATS
464void GrGpu::Stats::dump(SkString* out) {
465 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
466 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
467 out->appendf("Textures Created: %d\n", fTextureCreates);
468 out->appendf("Texture Uploads: %d\n", fTextureUploads);
469 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400470 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500471 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
472 out->appendf("Number of draws: %d\n", fNumDraws);
473}
474
475void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
476 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
477 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500478}
479
480#endif
481
Robert Phillips646f6372018-09-25 09:31:10 -0400482#endif