blob: a1a0ea1216f0f7712f64d51eb4dac5de262c30a7 [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
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
Greg Daniel51316782017-08-02 15:10:09 +000011#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrBackendSurface.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070013#include "GrCaps.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000014#include "GrContext.h"
Robert Phillips6be756b2018-01-16 15:07:54 -050015#include "GrContextPriv.h"
bsalomon3582d3e2015-02-13 14:20:05 -080016#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070017#include "GrMesh.h"
kkinnunencabe20c2015-06-01 01:37:26 -070018#include "GrPathRendering.h"
bsalomoncb02b382015-08-12 11:14:50 -070019#include "GrPipeline.h"
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040020#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080021#include "GrResourceCache.h"
egdanielec00d942015-09-14 12:56:10 -070022#include "GrResourceProvider.h"
Greg Daniel51316782017-08-02 15:10:09 +000023#include "GrSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070024#include "GrStencilAttachment.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070025#include "GrStencilSettings.h"
egdaniel6d901da2015-07-30 12:02:15 -070026#include "GrSurfacePriv.h"
jvanverth900bd4a2016-04-29 13:53:12 -070027#include "GrTexturePriv.h"
Robert Phillipsabf7b762018-03-21 12:13:37 -040028#include "GrTextureProxyPriv.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040029#include "GrTracing.h"
Brian Osman71a18892017-08-10 10:23:25 -040030#include "SkJSONWriter.h"
halcanary4dbbd042016-06-07 17:21:10 -070031#include "SkMathPriv.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
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000035GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080036 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000037 , fResetBits(kAll_GrBackendState)
joshualitt3322fa42014-11-07 08:48:51 -080038 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000039}
40
bsalomoned0bcad2015-05-04 10:36:42 -070041GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070042
bsalomon6e2aad42016-04-01 11:54:31 -070043void GrGpu::disconnect(DisconnectType) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000044
bsalomon@google.comd302f142011-03-03 13:54:13 +000045////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000046
Greg Daniel8f5bbda2018-06-08 17:22:23 -040047bool GrGpu::IsACopyNeededForRepeatWrapMode(const GrCaps* caps, GrTextureProxy* texProxy,
48 int width, int height,
49 GrSamplerState::Filter filter,
50 GrTextureProducer::CopyParams* copyParams,
51 SkScalar scaleAdjust[2]) {
52 if (!caps->npotTextureTileSupport() &&
Greg Daniel09c94002018-06-08 22:11:51 +000053 (!SkIsPow2(width) || !SkIsPow2(height))) {
54 SkASSERT(scaleAdjust);
55 copyParams->fWidth = GrNextPow2(width);
56 copyParams->fHeight = GrNextPow2(height);
57 SkASSERT(scaleAdjust);
58 scaleAdjust[0] = ((SkScalar)copyParams->fWidth) / width;
59 scaleAdjust[1] = ((SkScalar)copyParams->fHeight) / height;
Greg Daniel8f5bbda2018-06-08 17:22:23 -040060 switch (filter) {
Greg Daniel09c94002018-06-08 22:11:51 +000061 case GrSamplerState::Filter::kNearest:
62 copyParams->fFilter = GrSamplerState::Filter::kNearest;
63 break;
64 case GrSamplerState::Filter::kBilerp:
65 case GrSamplerState::Filter::kMipMap:
66 // We are only ever scaling up so no reason to ever indicate kMipMap.
67 copyParams->fFilter = GrSamplerState::Filter::kBilerp;
68 break;
69 }
70 return true;
71 }
Robert Phillipsabf7b762018-03-21 12:13:37 -040072
73 if (texProxy) {
74 // If the texture format itself doesn't support repeat wrap mode or mipmapping (and
75 // those capabilities are required) force a copy.
Brian Salomonfd98c2c2018-07-31 17:25:29 -040076 if (texProxy->hasRestrictedSampling()) {
Robert Phillipsabf7b762018-03-21 12:13:37 -040077 copyParams->fFilter = GrSamplerState::Filter::kNearest;
78 copyParams->fWidth = texProxy->width();
79 copyParams->fHeight = texProxy->height();
80 return true;
81 }
82 }
83
bsalomon100b8f82015-10-28 08:37:44 -070084 return false;
bsalomon045802d2015-10-20 07:58:01 -070085}
86
Greg Daniel8f5bbda2018-06-08 17:22:23 -040087bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy,
88 GrSamplerState::Filter filter,
89 GrTextureProducer::CopyParams* copyParams) {
90 SkASSERT(texProxy);
91 bool willNeedMips = GrSamplerState::Filter::kMipMap == filter && caps->mipMapSupport();
92 // If the texture format itself doesn't support mipmapping (and those capabilities are required)
93 // force a copy.
94 if (willNeedMips && texProxy->mipMapped() == GrMipMapped::kNo) {
95 copyParams->fFilter = GrSamplerState::Filter::kNearest;
96 copyParams->fWidth = texProxy->width();
97 copyParams->fHeight = texProxy->height();
98 return true;
99 }
100
101 return false;
102}
103
Robert Phillips67d52cf2017-06-05 13:38:13 -0400104sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500105 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomondcbb9d92017-07-19 10:53:20 -0400106 GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "createTexture", fContext);
cblume55f2d2d2016-02-26 13:20:48 -0800107 GrSurfaceDesc desc = origDesc;
108
Brian Salomonbdecacf2018-02-02 20:32:49 -0500109 GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
110 if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700112 }
113
Brian Salomonbdecacf2018-02-02 20:32:49 -0500114 bool isRT = desc.fFlags & kRenderTarget_GrSurfaceFlag;
115 if (isRT) {
116 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
117 }
Brian Salomond17b4a62017-05-23 16:53:47 -0400118 // Attempt to catch un- or wrongly initialized sample counts.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500119 SkASSERT(desc.fSampleCnt > 0 && desc.fSampleCnt <= 64);
egdanielb0e1be22015-04-22 13:27:39 -0700120
Robert Phillips590533f2017-07-11 14:22:35 -0400121 if (mipLevelCount && (desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400122 return nullptr;
123 }
124
Jim Van Verth1676cb92019-01-15 13:24:45 -0500125 // We shouldn't be rendering into compressed textures
126 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || !isRT);
127 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || 1 == desc.fSampleCnt);
128
Robert Phillips92de6312017-05-23 07:43:48 -0400129 this->handleDirtyContext();
Brian Salomon58389b92018-03-07 13:01:25 -0500130 sk_sp<GrTexture> tex = this->onCreateTexture(desc, budgeted, texels, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800131 if (tex) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500132 if (!this->caps()->reuseScratchTextures() && !isRT) {
cblume55f2d2d2016-02-26 13:20:48 -0800133 tex->resourcePriv().removeScratchKey();
134 }
bsalomonb12ea412015-02-02 21:19:50 -0800135 fStats.incTextureCreates();
Robert Phillips590533f2017-07-11 14:22:35 -0400136 if (mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -0800137 if (texels[0].fPixels) {
138 fStats.incTextureUploads();
139 }
bsalomonb12ea412015-02-02 21:19:50 -0800140 }
141 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000142 return tex;
143}
144
Robert Phillips646e4292017-06-13 12:44:56 -0400145sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
Brian Salomon58389b92018-03-07 13:01:25 -0500146 return this->createTexture(desc, budgeted, nullptr, 0);
Robert Phillips646e4292017-06-13 12:44:56 -0400147}
148
Greg Daniel7ef28f32017-04-20 16:41:55 +0000149sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500150 GrWrapOwnership ownership, GrWrapCacheable cacheable,
151 GrIOType ioType) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500152 SkASSERT(ioType != kWrite_GrIOType);
bsalomon@google.come269f212011-11-07 13:29:52 +0000153 this->handleDirtyContext();
Jim Van Verth1676cb92019-01-15 13:24:45 -0500154 SkASSERT(this->caps());
Greg Daniel7ef28f32017-04-20 16:41:55 +0000155 if (!this->caps()->isConfigTexturable(backendTex.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800156 return nullptr;
157 }
Brian Salomond17f6582017-07-19 18:28:58 -0400158 if (backendTex.width() > this->caps()->maxTextureSize() ||
159 backendTex.height() > this->caps()->maxTextureSize()) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800160 return nullptr;
161 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500162 return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType);
Brian Salomond17f6582017-07-19 18:28:58 -0400163}
Eric Karl5c779752017-05-08 12:02:07 -0700164
Brian Salomond17f6582017-07-19 18:28:58 -0400165sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500166 int sampleCnt, GrWrapOwnership ownership,
167 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400168 this->handleDirtyContext();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500169 if (sampleCnt < 1) {
170 return nullptr;
171 }
Brian Salomond17f6582017-07-19 18:28:58 -0400172 if (!this->caps()->isConfigTexturable(backendTex.config()) ||
Brian Salomonbdecacf2018-02-02 20:32:49 -0500173 !this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400174 return nullptr;
175 }
176
177 if (backendTex.width() > this->caps()->maxRenderTargetSize() ||
178 backendTex.height() > this->caps()->maxRenderTargetSize()) {
179 return nullptr;
180 }
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500181 sk_sp<GrTexture> tex =
182 this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable);
Greg Daniele3204862018-04-16 11:24:10 -0400183 SkASSERT(!tex || tex->asRenderTarget());
bungeman6bd52842016-10-27 09:30:08 -0700184 return tex;
bsalomon@google.come269f212011-11-07 13:29:52 +0000185}
186
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400187sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500188 if (0 == this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config())) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800189 return nullptr;
190 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000191 this->handleDirtyContext();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400192 return this->onWrapBackendRenderTarget(backendRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000193}
194
Greg Daniel7ef28f32017-04-20 16:41:55 +0000195sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000196 int sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500197 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config())) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800198 return nullptr;
199 }
200 int maxSize = this->caps()->maxTextureSize();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000201 if (tex.width() > maxSize || tex.height() > maxSize) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800202 return nullptr;
203 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500204 this->handleDirtyContext();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400205 return this->onWrapBackendTextureAsRenderTarget(tex, sampleCnt);
ericrkf7b8b8a2016-02-24 14:49:51 -0800206}
207
Greg Danielb46add82019-01-02 14:51:29 -0500208sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
209 const GrVkDrawableInfo& vkInfo) {
210 return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo);
211}
212
213sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo,
214 const GrVkDrawableInfo& vkInfo) {
215 // This is only supported on Vulkan so we default to returning nullptr here
216 return nullptr;
217}
218
Brian Salomondbf70722019-02-07 11:31:24 -0500219sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType,
220 GrAccessPattern accessPattern, const void* data) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000221 this->handleDirtyContext();
Brian Salomondbf70722019-02-07 11:31:24 -0500222 sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700223 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700224 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700225 }
cdalton397536c2016-03-25 12:15:03 -0700226 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800227}
228
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400229bool GrGpu::copySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
230 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -0400231 const SkIRect& srcRect, const SkIPoint& dstPoint,
232 bool canDiscardOutsideDstRect) {
Brian Salomondcbb9d92017-07-19 10:53:20 -0400233 GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "copySurface", fContext);
joshualitt1cbdcde2015-08-21 11:53:29 -0700234 SkASSERT(dst && src);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500235
236 if (dst->readOnly()) {
237 return false;
238 }
239
joshualitt1cbdcde2015-08-21 11:53:29 -0700240 this->handleDirtyContext();
Brian Salomonc67c31c2018-12-06 10:00:03 -0500241
Greg Daniel55fa6472018-03-16 16:13:10 -0400242 return this->onCopySurface(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint,
243 canDiscardOutsideDstRect);
joshualitt1cbdcde2015-08-21 11:53:29 -0700244}
245
Brian Salomona6948702018-06-01 15:33:20 -0400246bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height,
247 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonbf7b6202016-11-11 16:08:03 -0500248 SkASSERT(surface);
249
Brian Salomonc320b152018-02-20 14:05:36 -0500250 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6d901da2015-07-30 12:02:15 -0700251 if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
252 &left, &top, &width, &height,
253 &buffer,
254 &rowBytes)) {
255 return false;
256 }
257
Jim Van Verth1676cb92019-01-15 13:24:45 -0500258 if (GrPixelConfigIsCompressed(surface->config())) {
259 return false;
260 }
261
Brian Salomonbf7b6202016-11-11 16:08:03 -0500262 this->handleDirtyContext();
263
Brian Salomona6948702018-06-01 15:33:20 -0400264 return this->onReadPixels(surface, left, top, width, height, dstColorType, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000265}
266
Brian Salomona9b04b92018-06-01 15:04:28 -0400267bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height,
268 GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount) {
Brian Salomonbf7b6202016-11-11 16:08:03 -0500269 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500270
271 if (surface->readOnly()) {
272 return false;
273 }
274
Robert Phillips590533f2017-07-11 14:22:35 -0400275 if (1 == mipLevelCount) {
Greg Daniel660cc992017-06-26 14:55:05 -0400276 // We require that if we are not mipped, then the write region is contained in the surface
277 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
278 SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
279 if (!bounds.contains(subRect)) {
280 return false;
281 }
282 } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
283 // We require that if the texels are mipped, than the write region is the entire surface
284 return false;
285 }
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400286
Robert Phillips590533f2017-07-11 14:22:35 -0400287 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800288 if (!texels[currentMipLevel].fPixels ) {
289 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800290 }
291 }
jvanverth2dc29942015-09-01 07:16:46 -0700292
bsalomon@google.com6f379512011-11-16 20:36:03 +0000293 this->handleDirtyContext();
Brian Salomona9b04b92018-06-01 15:04:28 -0400294 if (this->onWritePixels(surface, left, top, width, height, srcColorType, texels,
Brian Salomonc320b152018-02-20 14:05:36 -0500295 mipLevelCount)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700296 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomona9b04b92018-06-01 15:04:28 -0400297 this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount);
bsalomonb12ea412015-02-02 21:19:50 -0800298 fStats.incTextureUploads();
299 return true;
300 }
301 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000302}
303
Brian Salomonc320b152018-02-20 14:05:36 -0500304bool GrGpu::transferPixels(GrTexture* texture, int left, int top, int width, int height,
Brian Salomondbf70722019-02-07 11:31:24 -0500305 GrColorType bufferColorType, GrGpuBuffer* transferBuffer, size_t offset,
Brian Salomonc320b152018-02-20 14:05:36 -0500306 size_t rowBytes) {
Brian Salomonc67c31c2018-12-06 10:00:03 -0500307 SkASSERT(texture);
cdalton397536c2016-03-25 12:15:03 -0700308 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800309
Brian Salomonc67c31c2018-12-06 10:00:03 -0500310 if (texture->readOnly()) {
311 return false;
312 }
313
Greg Daniel660cc992017-06-26 14:55:05 -0400314 // We require that the write region is contained in the texture
315 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
316 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
317 if (!bounds.contains(subRect)) {
318 return false;
319 }
320
jvanverth17aa0472016-01-05 10:41:27 -0800321 this->handleDirtyContext();
Brian Salomonc320b152018-02-20 14:05:36 -0500322 if (this->onTransferPixels(texture, left, top, width, height, bufferColorType, transferBuffer,
323 offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700324 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
Brian Salomon1fabd512018-02-09 09:54:25 -0500325 this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800326 fStats.incTransfersToTexture();
jvanverth84741b32016-09-30 08:39:02 -0700327
jvanverth17aa0472016-01-05 10:41:27 -0800328 return true;
329 }
330 return false;
331}
332
Brian Salomon930f9392018-06-20 16:25:26 -0400333bool GrGpu::regenerateMipMapLevels(GrTexture* texture) {
334 SkASSERT(texture);
335 SkASSERT(this->caps()->mipMapSupport());
336 SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes);
337 SkASSERT(texture->texturePriv().mipMapsAreDirty());
338 SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500339 if (texture->readOnly()) {
340 return false;
341 }
Brian Salomon930f9392018-06-20 16:25:26 -0400342 if (this->onRegenerateMipMapLevels(texture)) {
343 texture->texturePriv().markMipMapsClean();
344 return true;
345 }
346 return false;
347}
348
Brian Salomon1f05d452019-02-08 12:33:08 -0500349void GrGpu::resetTextureBindings() {
350 this->handleDirtyContext();
351 this->onResetTextureBindings();
352}
353
Brian Salomon1fabd512018-02-09 09:54:25 -0500354void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000355 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000356 this->handleDirtyContext();
Brian Salomon1fabd512018-02-09 09:54:25 -0500357 this->onResolveRenderTarget(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000358}
359
Brian Salomon1fabd512018-02-09 09:54:25 -0500360void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
361 uint32_t mipLevels) const {
jvanverth900bd4a2016-04-29 13:53:12 -0700362 SkASSERT(surface);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500363 SkASSERT(!surface->readOnly());
jvanverth900bd4a2016-04-29 13:53:12 -0700364 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
365 if (nullptr == bounds || !bounds->isEmpty()) {
366 if (GrRenderTarget* target = surface->asRenderTarget()) {
Brian Salomon1fabd512018-02-09 09:54:25 -0500367 SkIRect flippedBounds;
368 if (kBottomLeft_GrSurfaceOrigin == origin && bounds) {
369 flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom,
370 bounds->fRight, surface->height() - bounds->fTop};
371 bounds = &flippedBounds;
372 }
jvanverth900bd4a2016-04-29 13:53:12 -0700373 target->flagAsNeedingResolve(bounds);
374 }
375 GrTexture* texture = surface->asTexture();
376 if (texture && 1 == mipLevels) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400377 texture->texturePriv().markMipMapsDirty();
jvanverth900bd4a2016-04-29 13:53:12 -0700378 }
379 }
380}
381
Greg Daniel51316782017-08-02 15:10:09 +0000382GrSemaphoresSubmitted GrGpu::finishFlush(int numSemaphores,
383 GrBackendSemaphore backendSemaphores[]) {
Greg Danield2073452018-12-07 11:20:33 -0500384 this->stats()->incNumFinishFlushes();
Robert Phillips9da87e02019-02-04 13:26:26 -0500385 GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500386
Greg Daniel51316782017-08-02 15:10:09 +0000387 if (this->caps()->fenceSyncSupport()) {
388 for (int i = 0; i < numSemaphores; ++i) {
389 sk_sp<GrSemaphore> semaphore;
390 if (backendSemaphores[i].isInitialized()) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500391 semaphore = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -0500392 backendSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillSignal,
393 kBorrow_GrWrapOwnership);
Greg Daniel51316782017-08-02 15:10:09 +0000394 } else {
Robert Phillips6be756b2018-01-16 15:07:54 -0500395 semaphore = resourceProvider->makeSemaphore(false);
Greg Daniel51316782017-08-02 15:10:09 +0000396 }
Greg Daniel858e12c2018-12-06 11:11:37 -0500397 this->insertSemaphore(semaphore);
Greg Daniel51316782017-08-02 15:10:09 +0000398
399 if (!backendSemaphores[i].isInitialized()) {
Brian Salomon1e576e72018-08-30 10:20:38 -0400400 backendSemaphores[i] = semaphore->backendSemaphore();
Greg Daniel51316782017-08-02 15:10:09 +0000401 }
402 }
403 }
404 this->onFinishFlush((numSemaphores > 0 && this->caps()->fenceSyncSupport()));
405 return this->caps()->fenceSyncSupport() ? GrSemaphoresSubmitted::kYes
406 : GrSemaphoresSubmitted::kNo;
407}
Brian Osman71a18892017-08-10 10:23:25 -0400408
Kevin Lubickf4def342018-10-04 12:52:50 -0400409#ifdef SK_ENABLE_DUMP_GPU
Brian Osman71a18892017-08-10 10:23:25 -0400410void GrGpu::dumpJSON(SkJSONWriter* writer) const {
411 writer->beginObject();
412
413 // TODO: Is there anything useful in the base class to dump here?
414
415 this->onDumpJSON(writer);
416
417 writer->endObject();
418}
Kevin Lubickf4def342018-10-04 12:52:50 -0400419#else
420void GrGpu::dumpJSON(SkJSONWriter* writer) const { }
421#endif
Robert Phillips646f6372018-09-25 09:31:10 -0400422
423#if GR_TEST_UTILS
424GrBackendTexture GrGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h,
425 SkColorType colorType, bool isRenderTarget,
426 GrMipMapped isMipped, size_t rowBytes) {
427 GrColorType grCT = SkColorTypeToGrColorType(colorType);
428
429 return this->createTestingOnlyBackendTexture(pixels, w, h, grCT, isRenderTarget, isMipped,
430 rowBytes);
431}
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500432
433#if GR_GPU_STATS
434void GrGpu::Stats::dump(SkString* out) {
435 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
436 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
437 out->appendf("Textures Created: %d\n", fTextureCreates);
438 out->appendf("Texture Uploads: %d\n", fTextureUploads);
439 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
440 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
441 out->appendf("Number of draws: %d\n", fNumDraws);
442}
443
444void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
445 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
446 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
447 keys->push_back(SkString("texture_uploads")); values->push_back(fTextureUploads);
448 keys->push_back(SkString("number_of_draws")); values->push_back(fNumDraws);
449 keys->push_back(SkString("number_of_failed_draws")); values->push_back(fNumFailedDraws);
450}
451
452#endif
453
Robert Phillips646f6372018-09-25 09:31:10 -0400454#endif