blob: caa17d34d77af709e5507ea6b9b4969c0ef42694 [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"
15#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrGpuResourcePriv.h"
18#include "src/gpu/GrMesh.h"
19#include "src/gpu/GrPathRendering.h"
20#include "src/gpu/GrPipeline.h"
21#include "src/gpu/GrRenderTargetPriv.h"
22#include "src/gpu/GrResourceCache.h"
23#include "src/gpu/GrResourceProvider.h"
24#include "src/gpu/GrSemaphore.h"
25#include "src/gpu/GrStencilAttachment.h"
26#include "src/gpu/GrStencilSettings.h"
27#include "src/gpu/GrSurfacePriv.h"
28#include "src/gpu/GrTexturePriv.h"
29#include "src/gpu/GrTextureProxyPriv.h"
30#include "src/gpu/GrTracing.h"
31#include "src/utils/SkJSONWriter.h"
bsalomoncb8979d2015-05-05 09:51:38 -070032
bsalomon@google.comd302f142011-03-03 13:54:13 +000033////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000034
Brian Salomone2826ab2019-06-04 15:58:31 -040035GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000036
bsalomoned0bcad2015-05-04 10:36:42 -070037GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070038
bsalomon6e2aad42016-04-01 11:54:31 -070039void GrGpu::disconnect(DisconnectType) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomon@google.comd302f142011-03-03 13:54:13 +000041////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000042
Greg Daniel8f5bbda2018-06-08 17:22:23 -040043bool GrGpu::IsACopyNeededForRepeatWrapMode(const GrCaps* caps, GrTextureProxy* texProxy,
44 int width, int height,
45 GrSamplerState::Filter filter,
46 GrTextureProducer::CopyParams* copyParams,
47 SkScalar scaleAdjust[2]) {
48 if (!caps->npotTextureTileSupport() &&
Greg Daniel09c94002018-06-08 22:11:51 +000049 (!SkIsPow2(width) || !SkIsPow2(height))) {
50 SkASSERT(scaleAdjust);
51 copyParams->fWidth = GrNextPow2(width);
52 copyParams->fHeight = GrNextPow2(height);
53 SkASSERT(scaleAdjust);
54 scaleAdjust[0] = ((SkScalar)copyParams->fWidth) / width;
55 scaleAdjust[1] = ((SkScalar)copyParams->fHeight) / height;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040056 switch (filter) {
Greg Daniel09c94002018-06-08 22:11:51 +000057 case GrSamplerState::Filter::kNearest:
58 copyParams->fFilter = GrSamplerState::Filter::kNearest;
59 break;
60 case GrSamplerState::Filter::kBilerp:
61 case GrSamplerState::Filter::kMipMap:
62 // We are only ever scaling up so no reason to ever indicate kMipMap.
63 copyParams->fFilter = GrSamplerState::Filter::kBilerp;
64 break;
65 }
66 return true;
67 }
Robert Phillipsabf7b762018-03-21 12:13:37 -040068
69 if (texProxy) {
70 // If the texture format itself doesn't support repeat wrap mode or mipmapping (and
71 // those capabilities are required) force a copy.
Brian Salomonfd98c2c2018-07-31 17:25:29 -040072 if (texProxy->hasRestrictedSampling()) {
Robert Phillipsabf7b762018-03-21 12:13:37 -040073 copyParams->fFilter = GrSamplerState::Filter::kNearest;
74 copyParams->fWidth = texProxy->width();
75 copyParams->fHeight = texProxy->height();
76 return true;
77 }
78 }
79
bsalomon100b8f82015-10-28 08:37:44 -070080 return false;
bsalomon045802d2015-10-20 07:58:01 -070081}
82
Greg Daniel8f5bbda2018-06-08 17:22:23 -040083bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
84 GrSamplerState::Filter filter,
85 GrTextureProducer::CopyParams* copyParams) {
86 SkASSERT(texProxy);
87 bool willNeedMips = GrSamplerState::Filter::kMipMap == filter && caps->mipMapSupport();
88 // If the texture format itself doesn't support mipmapping (and those capabilities are required)
89 // force a copy.
90 if (willNeedMips && texProxy->mipMapped() == GrMipMapped::kNo) {
91 copyParams->fFilter = GrSamplerState::Filter::kNearest;
92 copyParams->fWidth = texProxy->width();
93 copyParams->fHeight = texProxy->height();
94 return true;
95 }
96
97 return false;
98}
99
Robert Phillips67d52cf2017-06-05 13:38:13 -0400100sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500101 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomondcbb9d92017-07-19 10:53:20 -0400102 GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "createTexture", fContext);
cblume55f2d2d2016-02-26 13:20:48 -0800103 GrSurfaceDesc desc = origDesc;
104
Brian Salomonbdecacf2018-02-02 20:32:49 -0500105 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
106 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700107 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700108 }
109
Brian Salomonbdecacf2018-02-02 20:32:49 -0500110 bool isRT = desc.fFlags & kRenderTarget_GrSurfaceFlag;
111 if (isRT) {
112 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
113 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400114 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500115 SkASSERT(desc.fSampleCnt > 0 && desc.fSampleCnt <= 64);
egdanielb0e1be22015-04-22 13:27:39 -0700116
Robert Phillips590533f2017-07-11 14:22:35 -0400117 if (mipLevelCount && (desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400118 return nullptr;
119 }
120
Jim Van Verth1676cb92019-01-15 13:24:45 -0500121 // We shouldn't be rendering into compressed textures
122 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || !isRT);
123 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || 1 == desc.fSampleCnt);
124
Robert Phillips92de6312017-05-23 07:43:48 -0400125 this->handleDirtyContext();
Brian Salomon58389b92018-03-07 13:01:25 -0500126 sk_sp<GrTexture> tex = this->onCreateTexture(desc, budgeted, texels, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800127 if (tex) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500128 if (!this->caps()->reuseScratchTextures() && !isRT) {
cblume55f2d2d2016-02-26 13:20:48 -0800129 tex->resourcePriv().removeScratchKey();
130 }
bsalomonb12ea412015-02-02 21:19:50 -0800131 fStats.incTextureCreates();
Robert Phillips590533f2017-07-11 14:22:35 -0400132 if (mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -0800133 if (texels[0].fPixels) {
134 fStats.incTextureUploads();
135 }
bsalomonb12ea412015-02-02 21:19:50 -0800136 }
137 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000138 return tex;
139}
140
Robert Phillips646e4292017-06-13 12:44:56 -0400141sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
Brian Salomon58389b92018-03-07 13:01:25 -0500142 return this->createTexture(desc, budgeted, nullptr, 0);
Robert Phillips646e4292017-06-13 12:44:56 -0400143}
144
Greg Daniel7ef28f32017-04-20 16:41:55 +0000145sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500146 GrWrapOwnership ownership, GrWrapCacheable cacheable,
147 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500148 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000149 this->handleDirtyContext();
Jim Van Verth1676cb92019-01-15 13:24:45 -0500150 SkASSERT(this->caps());
Greg Daniel7ef28f32017-04-20 16:41:55 +0000151 if (!this->caps()->isConfigTexturable(backendTex.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800152 return nullptr;
153 }
Brian Salomond17f6582017-07-19 18:28:58 -0400154 if (backendTex.width() > this->caps()->maxTextureSize() ||
155 backendTex.height() > this->caps()->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800156 return nullptr;
157 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500158 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400159}
Eric Karl5c779752017-05-08 12:02:07 -0700160
Brian Salomond17f6582017-07-19 18:28:58 -0400161sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500162 int sampleCnt, GrWrapOwnership ownership,
163 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400164 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500165 if (sampleCnt < 1) {
166 return nullptr;
167 }
Brian Salomond17f6582017-07-19 18:28:58 -0400168 if (!this->caps()->isConfigTexturable(backendTex.config()) ||
Brian Salomonbdecacf2018-02-02 20:32:49 -0500169 !this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400170 return nullptr;
171 }
172
173 if (backendTex.width() > this->caps()->maxRenderTargetSize() ||
174 backendTex.height() > this->caps()->maxRenderTargetSize()) {
175 return nullptr;
176 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500177 sk_sp<GrTexture> tex =
178 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400179 SkASSERT(!tex || tex->asRenderTarget());
bungeman6bd52842016-10-27 09:30:08 -0700180 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000181}
182
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400183sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500184 if (0 == this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800185 return nullptr;
186 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000187 this->handleDirtyContext();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400188 return this->onWrapBackendRenderTarget(backendRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000189}
190
Greg Daniel7ef28f32017-04-20 16:41:55 +0000191sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000192 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500193 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config())) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800194 return nullptr;
195 }
196 int maxSize = this->caps()->maxTextureSize();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000197 if (tex.width() > maxSize || tex.height() > maxSize) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800198 return nullptr;
199 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500200 this->handleDirtyContext();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400201 return this->onWrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800202}
203
Greg Danielb46add82019-01-02 14:51:29 -0500204sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
205 const GrVkDrawableInfo& vkInfo) {
206 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
207}
208
209sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
210 const GrVkDrawableInfo& vkInfo) {
211 // This is only supported on Vulkan so we default to returning nullptr here
212 return nullptr;
213}
214
Brian Salomondbf70722019-02-07 11:31:24 -0500215sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
216 GrAccessPattern accessPattern, const void* data) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000217 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500218 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700219 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700220 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700221 }
cdalton397536c2016-03-25 12:15:03 -0700222 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800223}
224
Greg Daniel46cfbc62019-06-07 11:43:30 -0400225bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
226 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) {
Brian Salomondcbb9d92017-07-19 10:53:20 -0400227 GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "copySurface", fContext);
joshualitt1cbdcde2015-08-21 11:53:29 -0700228 SkASSERT(dst && src);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500229
230 if (dst->readOnly()) {
231 return false;
232 }
233
joshualitt1cbdcde2015-08-21 11:53:29 -0700234 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500235
Greg Daniel46cfbc62019-06-07 11:43:30 -0400236 return this->onCopySurface(dst, src, srcRect, dstPoint, canDiscardOutsideDstRect);
joshualitt1cbdcde2015-08-21 11:53:29 -0700237}
238
Brian Salomona6948702018-06-01 15:33:20 -0400239bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
240 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonbf7b6202016-11-11 16:08:03 -0500241 SkASSERT(surface);
242
Brian Salomonc320b152018-02-20 14:05:36 -0500243 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6d901da2015-07-30 12:02:15 -0700244 if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
245 &left, &top, &width, &height,
246 &buffer,
247 &rowBytes)) {
248 return false;
249 }
250
Jim Van Verth1676cb92019-01-15 13:24:45 -0500251 if (GrPixelConfigIsCompressed(surface->config())) {
252 return false;
253 }
254
Brian Salomonbf7b6202016-11-11 16:08:03 -0500255 this->handleDirtyContext();
256
Brian Salomona6948702018-06-01 15:33:20 -0400257 return this->onReadPixels(surface, left, top, width, height, dstColorType, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000258}
259
Brian Salomona9b04b92018-06-01 15:04:28 -0400260bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
261 GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount) {
Brian Salomonbf7b6202016-11-11 16:08:03 -0500262 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500263
264 if (surface->readOnly()) {
265 return false;
266 }
267
Robert Phillips590533f2017-07-11 14:22:35 -0400268 if (1 == mipLevelCount) {
Greg Daniel660cc992017-06-26 14:55:05 -0400269 // We require that if we are not mipped, then the write region is contained in the surface
270 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
271 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
272 if (!bounds.contains(subRect)) {
273 return false;
274 }
275 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
276 // We require that if the texels are mipped, than the write region is the entire surface
277 return false;
278 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400279
Robert Phillips590533f2017-07-11 14:22:35 -0400280 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800281 if (!texels[currentMipLevel].fPixels ) {
282 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800283 }
284 }
jvanverth2dc29942015-09-01 07:16:46 -0700285
bsalomon@google.com6f379512011-11-16 20:36:03 +0000286 this->handleDirtyContext();
Brian Salomona9b04b92018-06-01 15:04:28 -0400287 if (this->onWritePixels(surface, left, top, width, height, srcColorType, texels,
Brian Salomonc320b152018-02-20 14:05:36 -0500288 mipLevelCount)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700289 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400290 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800291 fStats.incTextureUploads();
292 return true;
293 }
294 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000295}
296
Brian Salomone05ba5a2019-04-08 11:59:07 -0400297bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
298 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
299 size_t offset, size_t rowBytes) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500300 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700301 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800302
Brian Salomonc67c31c2018-12-06 10:00:03 -0500303 if (texture->readOnly()) {
304 return false;
305 }
306
Greg Daniel660cc992017-06-26 14:55:05 -0400307 // We require that the write region is contained in the texture
308 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
309 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
310 if (!bounds.contains(subRect)) {
311 return false;
312 }
313
jvanverth17aa0472016-01-05 10:41:27 -0800314 this->handleDirtyContext();
Brian Salomone05ba5a2019-04-08 11:59:07 -0400315 if (this->onTransferPixelsTo(texture, left, top, width, height, bufferColorType, transferBuffer,
316 offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700317 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500318 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800319 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700320
jvanverth17aa0472016-01-05 10:41:27 -0800321 return true;
322 }
323 return false;
324}
325
Brian Salomon26de56e2019-04-10 12:14:26 -0400326bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
327 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
328 size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400329 SkASSERT(surface);
330 SkASSERT(transferBuffer);
Brian Salomon26de56e2019-04-10 12:14:26 -0400331 SkASSERT(this->caps()->transferFromOffsetAlignment(bufferColorType));
332 SkASSERT(offset % this->caps()->transferFromOffsetAlignment(bufferColorType) == 0);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400333
334 // We require that the write region is contained in the texture
335 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
336 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
337 if (!bounds.contains(subRect)) {
338 return false;
339 }
340
341 this->handleDirtyContext();
Brian Salomon26de56e2019-04-10 12:14:26 -0400342 if (this->onTransferPixelsFrom(surface, left, top, width, height, bufferColorType,
343 transferBuffer, offset)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400344 fStats.incTransfersFromSurface();
Brian Salomon26de56e2019-04-10 12:14:26 -0400345 return true;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400346 }
Brian Salomon26de56e2019-04-10 12:14:26 -0400347 return false;
Brian Salomone05ba5a2019-04-08 11:59:07 -0400348}
349
Brian Salomon930f9392018-06-20 16:25:26 -0400350bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
351 SkASSERT(texture);
352 SkASSERT(this->caps()->mipMapSupport());
353 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
354 SkASSERT(texture->texturePriv().mipMapsAreDirty());
355 SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500356 if (texture->readOnly()) {
357 return false;
358 }
Brian Salomon930f9392018-06-20 16:25:26 -0400359 if (this->onRegenerateMipMapLevels(texture)) {
360 texture->texturePriv().markMipMapsClean();
361 return true;
362 }
363 return false;
364}
365
Brian Salomon1f05d452019-02-08 12:33:08 -0500366void GrGpu::resetTextureBindings() {
367 this->handleDirtyContext();
368 this->onResetTextureBindings();
369}
370
Brian Salomon1fabd512018-02-09 09:54:25 -0500371void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000372 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000373 this->handleDirtyContext();
Brian Salomon1fabd512018-02-09 09:54:25 -0500374 this->onResolveRenderTarget(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000375}
376
Brian Salomon1fabd512018-02-09 09:54:25 -0500377void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
378 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700379 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500380 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700381 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
382 if (nullptr == bounds || !bounds->isEmpty()) {
383 if (GrRenderTarget* target = surface->asRenderTarget()) {
Brian Salomon1fabd512018-02-09 09:54:25 -0500384 SkIRect flippedBounds;
385 if (kBottomLeft_GrSurfaceOrigin == origin && bounds) {
386 flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom,
387 bounds->fRight, surface->height() - bounds->fTop};
388 bounds = &flippedBounds;
389 }
jvanverth900bd4a2016-04-29 13:53:12 -0700390 target->flagAsNeedingResolve(bounds);
391 }
392 GrTexture* texture = surface->asTexture();
393 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400394 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700395 }
396 }
397}
398
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600399int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700400 SkASSERT(this->caps()->sampleLocationsSupport());
401 SkASSERT(renderTarget->numStencilSamples() > 1);
Chris Daltond7291ba2019-03-07 14:17:03 -0700402
403 SkSTArray<16, SkPoint> sampleLocations;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600404 this->querySampleLocations(renderTarget, &sampleLocations);
Chris Daltond7291ba2019-03-07 14:17:03 -0700405 return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations);
406}
407
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400408GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[],
409 int n,
Greg Danielbae71212019-03-01 15:24:35 -0500410 SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -0400411 const GrFlushInfo& info,
412 const GrPrepareForExternalIORequests& externalRequests) {
Greg Danield2073452018-12-07 11:20:33 -0500413 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500414 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500415
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400416 if (this->caps()->semaphoreSupport()) {
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400417 for (int i = 0; i < info.fNumSemaphores; ++i) {
Greg Daniel51316782017-08-02 15:10:09 +0000418 sk_sp<GrSemaphore> semaphore;
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400419 if (info.fSignalSemaphores[i].isInitialized()) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500420 semaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400421 info.fSignalSemaphores[i],
422 GrResourceProvider::SemaphoreWrapType::kWillSignal,
Greg Daniel17b7c052018-01-09 13:55:33 -0500423 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000424 } else {
Robert Phillips6be756b2018-01-16 15:07:54 -0500425 semaphore = resourceProvider->makeSemaphore(false);
Greg Daniel51316782017-08-02 15:10:09 +0000426 }
Greg Daniel858e12c2018-12-06 11:11:37 -0500427 this->insertSemaphore(semaphore);
Greg Daniel51316782017-08-02 15:10:09 +0000428
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400429 if (!info.fSignalSemaphores[i].isInitialized()) {
430 info.fSignalSemaphores[i] = semaphore->backendSemaphore();
Greg Daniel51316782017-08-02 15:10:09 +0000431 }
432 }
433 }
Greg Daniel797efca2019-05-09 14:04:20 -0400434 this->onFinishFlush(proxies, n, access, info, externalRequests);
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400435 return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes
Greg Daniel51316782017-08-02 15:10:09 +0000436 : GrSemaphoresSubmitted::kNo;
437}
Brian Osman71a18892017-08-10 10:23:25 -0400438
Kevin Lubickf4def342018-10-04 12:52:50 -0400439#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400440void GrGpu::dumpJSON(SkJSONWriter* writer) const {
441 writer->beginObject();
442
443 // TODO: Is there anything useful in the base class to dump here?
444
445 this->onDumpJSON(writer);
446
447 writer->endObject();
448}
Kevin Lubickf4def342018-10-04 12:52:50 -0400449#else
450void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
451#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400452
Robert Phillipsf0ced622019-05-16 09:06:25 -0400453#if GR_TEST_UTILS
454
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500455#if GR_GPU_STATS
456void GrGpu::Stats::dump(SkString* out) {
457 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
458 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
459 out->appendf("Textures Created: %d\n", fTextureCreates);
460 out->appendf("Texture Uploads: %d\n", fTextureUploads);
461 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400462 out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500463 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
464 out->appendf("Number of draws: %d\n", fNumDraws);
465}
466
467void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
468 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
469 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500470}
471
472#endif
473
Robert Phillips646f6372018-09-25 09:31:10 -0400474#endif