reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 9 | #include "GrGpu.h" |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 10 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 11 | #include "GrBackendSemaphore.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 12 | #include "GrBackendSurface.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 13 | #include "GrCaps.h" |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 14 | #include "GrContext.h" |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 15 | #include "GrContextPriv.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 16 | #include "GrGpuResourcePriv.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 17 | #include "GrMesh.h" |
kkinnunen | cabe20c | 2015-06-01 01:37:26 -0700 | [diff] [blame] | 18 | #include "GrPathRendering.h" |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 19 | #include "GrPipeline.h" |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 20 | #include "GrRenderTargetPriv.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 21 | #include "GrResourceCache.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 22 | #include "GrResourceProvider.h" |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 23 | #include "GrSemaphore.h" |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 24 | #include "GrStencilAttachment.h" |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 25 | #include "GrStencilSettings.h" |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 26 | #include "GrSurfacePriv.h" |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 27 | #include "GrTexturePriv.h" |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 28 | #include "GrTextureProxyPriv.h" |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 29 | #include "GrTracing.h" |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 30 | #include "SkJSONWriter.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 31 | #include "SkMathPriv.h" |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 32 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 33 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 35 | GrGpu::GrGpu(GrContext* context) |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 36 | : fResetTimestamp(kExpiredTimestamp+1) |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 37 | , fResetBits(kAll_GrBackendState) |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 38 | , fContext(context) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | } |
| 40 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 41 | GrGpu::~GrGpu() {} |
bsalomon | 1d89ddc | 2014-08-19 14:20:58 -0700 | [diff] [blame] | 42 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 43 | void GrGpu::disconnect(DisconnectType) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 45 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 47 | bool 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 Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 53 | (!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 Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 60 | switch (filter) { |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 61 | 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 Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 72 | |
| 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 Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 76 | if (texProxy->hasRestrictedSampling()) { |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 77 | copyParams->fFilter = GrSamplerState::Filter::kNearest; |
| 78 | copyParams->fWidth = texProxy->width(); |
| 79 | copyParams->fHeight = texProxy->height(); |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 84 | return false; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 87 | bool 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 Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 104 | sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 105 | const GrMipLevel texels[], int mipLevelCount) { |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 106 | GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "createTexture", fContext); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 107 | GrSurfaceDesc desc = origDesc; |
| 108 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 109 | GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 110 | if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 111 | return nullptr; |
egdaniel | 8c9b6f1 | 2015-05-12 13:36:30 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 114 | bool isRT = desc.fFlags & kRenderTarget_GrSurfaceFlag; |
| 115 | if (isRT) { |
| 116 | desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig); |
| 117 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 118 | // Attempt to catch un- or wrongly initialized sample counts. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 119 | SkASSERT(desc.fSampleCnt > 0 && desc.fSampleCnt <= 64); |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 120 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 121 | if (mipLevelCount && (desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 122 | return nullptr; |
| 123 | } |
| 124 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 125 | // We shouldn't be rendering into compressed textures |
| 126 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || !isRT); |
| 127 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || 1 == desc.fSampleCnt); |
| 128 | |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 129 | this->handleDirtyContext(); |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 130 | sk_sp<GrTexture> tex = this->onCreateTexture(desc, budgeted, texels, mipLevelCount); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 131 | if (tex) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 132 | if (!this->caps()->reuseScratchTextures() && !isRT) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 133 | tex->resourcePriv().removeScratchKey(); |
| 134 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 135 | fStats.incTextureCreates(); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 136 | if (mipLevelCount) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 137 | if (texels[0].fPixels) { |
| 138 | fStats.incTextureUploads(); |
| 139 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 142 | return tex; |
| 143 | } |
| 144 | |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 145 | sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 146 | return this->createTexture(desc, budgeted, nullptr, 0); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 149 | sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 150 | GrWrapOwnership ownership, GrWrapCacheable cacheable, |
| 151 | GrIOType ioType) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 152 | SkASSERT(ioType != kWrite_GrIOType); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 153 | this->handleDirtyContext(); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 154 | SkASSERT(this->caps()); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 155 | if (!this->caps()->isConfigTexturable(backendTex.config())) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 156 | return nullptr; |
| 157 | } |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 158 | if (backendTex.width() > this->caps()->maxTextureSize() || |
| 159 | backendTex.height() > this->caps()->maxTextureSize()) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 160 | return nullptr; |
| 161 | } |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 162 | return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 163 | } |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 164 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 165 | sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 166 | int sampleCnt, GrWrapOwnership ownership, |
| 167 | GrWrapCacheable cacheable) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 168 | this->handleDirtyContext(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 169 | if (sampleCnt < 1) { |
| 170 | return nullptr; |
| 171 | } |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 172 | if (!this->caps()->isConfigTexturable(backendTex.config()) || |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 173 | !this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config())) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 174 | return nullptr; |
| 175 | } |
| 176 | |
| 177 | if (backendTex.width() > this->caps()->maxRenderTargetSize() || |
| 178 | backendTex.height() > this->caps()->maxRenderTargetSize()) { |
| 179 | return nullptr; |
| 180 | } |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 181 | sk_sp<GrTexture> tex = |
| 182 | this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable); |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 183 | SkASSERT(!tex || tex->asRenderTarget()); |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 184 | return tex; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 187 | sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 188 | if (0 == this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config())) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 189 | return nullptr; |
| 190 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 191 | this->handleDirtyContext(); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 192 | return this->onWrapBackendRenderTarget(backendRT); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 195 | sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 196 | int sampleCnt) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 197 | if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config())) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 198 | return nullptr; |
| 199 | } |
| 200 | int maxSize = this->caps()->maxTextureSize(); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 201 | if (tex.width() > maxSize || tex.height() > maxSize) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 202 | return nullptr; |
| 203 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 204 | this->handleDirtyContext(); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 205 | return this->onWrapBackendTextureAsRenderTarget(tex, sampleCnt); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 208 | sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo, |
| 209 | const GrVkDrawableInfo& vkInfo) { |
| 210 | return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo); |
| 211 | } |
| 212 | |
| 213 | sk_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 Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 219 | sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType, |
| 220 | GrAccessPattern accessPattern, const void* data) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 221 | this->handleDirtyContext(); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 222 | sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 223 | if (!this->caps()->reuseScratchBuffers()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 224 | buffer->resourcePriv().removeScratchKey(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 225 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 226 | return buffer; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 229 | bool GrGpu::copySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 230 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 231 | const SkIRect& srcRect, const SkIPoint& dstPoint, |
| 232 | bool canDiscardOutsideDstRect) { |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 233 | GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "copySurface", fContext); |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 234 | SkASSERT(dst && src); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 235 | |
| 236 | if (dst->readOnly()) { |
| 237 | return false; |
| 238 | } |
| 239 | |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 240 | this->handleDirtyContext(); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 241 | |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 242 | return this->onCopySurface(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint, |
| 243 | canDiscardOutsideDstRect); |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 246 | bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height, |
| 247 | GrColorType dstColorType, void* buffer, size_t rowBytes) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 248 | SkASSERT(surface); |
| 249 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 250 | int bpp = GrColorTypeBytesPerPixel(dstColorType); |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 251 | if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp, |
| 252 | &left, &top, &width, &height, |
| 253 | &buffer, |
| 254 | &rowBytes)) { |
| 255 | return false; |
| 256 | } |
| 257 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 258 | if (GrPixelConfigIsCompressed(surface->config())) { |
| 259 | return false; |
| 260 | } |
| 261 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 262 | this->handleDirtyContext(); |
| 263 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 264 | return this->onReadPixels(surface, left, top, width, height, dstColorType, buffer, rowBytes); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 267 | bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height, |
| 268 | GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 269 | SkASSERT(surface); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 270 | |
| 271 | if (surface->readOnly()) { |
| 272 | return false; |
| 273 | } |
| 274 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 275 | if (1 == mipLevelCount) { |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 276 | // 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 Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 286 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 287 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 288 | if (!texels[currentMipLevel].fPixels ) { |
| 289 | return false; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
jvanverth | 2dc2994 | 2015-09-01 07:16:46 -0700 | [diff] [blame] | 292 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 293 | this->handleDirtyContext(); |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 294 | if (this->onWritePixels(surface, left, top, width, height, srcColorType, texels, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 295 | mipLevelCount)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 296 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 297 | this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 298 | fStats.incTextureUploads(); |
| 299 | return true; |
| 300 | } |
| 301 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 304 | bool GrGpu::transferPixels(GrTexture* texture, int left, int top, int width, int height, |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 305 | GrColorType bufferColorType, GrGpuBuffer* transferBuffer, size_t offset, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 306 | size_t rowBytes) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 307 | SkASSERT(texture); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 308 | SkASSERT(transferBuffer); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 309 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 310 | if (texture->readOnly()) { |
| 311 | return false; |
| 312 | } |
| 313 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 314 | // 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 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 321 | this->handleDirtyContext(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 322 | if (this->onTransferPixels(texture, left, top, width, height, bufferColorType, transferBuffer, |
| 323 | offset, rowBytes)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 324 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 325 | this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 326 | fStats.incTransfersToTexture(); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 327 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | return false; |
| 331 | } |
| 332 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 333 | bool 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 Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 339 | if (texture->readOnly()) { |
| 340 | return false; |
| 341 | } |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 342 | if (this->onRegenerateMipMapLevels(texture)) { |
| 343 | texture->texturePriv().markMipMapsClean(); |
| 344 | return true; |
| 345 | } |
| 346 | return false; |
| 347 | } |
| 348 | |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 349 | void GrGpu::resetTextureBindings() { |
| 350 | this->handleDirtyContext(); |
| 351 | this->onResetTextureBindings(); |
| 352 | } |
| 353 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 354 | void GrGpu::resolveRenderTarget(GrRenderTarget* target) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 355 | SkASSERT(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 356 | this->handleDirtyContext(); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 357 | this->onResolveRenderTarget(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 360 | void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds, |
| 361 | uint32_t mipLevels) const { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 362 | SkASSERT(surface); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 363 | SkASSERT(!surface->readOnly()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 364 | // 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 Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 367 | 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 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 373 | target->flagAsNeedingResolve(bounds); |
| 374 | } |
| 375 | GrTexture* texture = surface->asTexture(); |
| 376 | if (texture && 1 == mipLevels) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 377 | texture->texturePriv().markMipMapsDirty(); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 382 | GrSemaphoresSubmitted GrGpu::finishFlush(int numSemaphores, |
| 383 | GrBackendSemaphore backendSemaphores[]) { |
Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 384 | this->stats()->incNumFinishFlushes(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 385 | GrResourceProvider* resourceProvider = fContext->priv().resourceProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 386 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 387 | if (this->caps()->fenceSyncSupport()) { |
| 388 | for (int i = 0; i < numSemaphores; ++i) { |
| 389 | sk_sp<GrSemaphore> semaphore; |
| 390 | if (backendSemaphores[i].isInitialized()) { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 391 | semaphore = resourceProvider->wrapBackendSemaphore( |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 392 | backendSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillSignal, |
| 393 | kBorrow_GrWrapOwnership); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 394 | } else { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 395 | semaphore = resourceProvider->makeSemaphore(false); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 396 | } |
Greg Daniel | 858e12c | 2018-12-06 11:11:37 -0500 | [diff] [blame] | 397 | this->insertSemaphore(semaphore); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 398 | |
| 399 | if (!backendSemaphores[i].isInitialized()) { |
Brian Salomon | 1e576e7 | 2018-08-30 10:20:38 -0400 | [diff] [blame] | 400 | backendSemaphores[i] = semaphore->backendSemaphore(); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | this->onFinishFlush((numSemaphores > 0 && this->caps()->fenceSyncSupport())); |
| 405 | return this->caps()->fenceSyncSupport() ? GrSemaphoresSubmitted::kYes |
| 406 | : GrSemaphoresSubmitted::kNo; |
| 407 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 408 | |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 409 | #ifdef SK_ENABLE_DUMP_GPU |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 410 | void 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 Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 419 | #else |
| 420 | void GrGpu::dumpJSON(SkJSONWriter* writer) const { } |
| 421 | #endif |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 422 | |
| 423 | #if GR_TEST_UTILS |
| 424 | GrBackendTexture 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 Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 432 | |
| 433 | #if GR_GPU_STATS |
| 434 | void 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 | |
| 444 | void 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 Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 454 | #endif |