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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/gpu/GrGpu.h" |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #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" |
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 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame^] | 35 | GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 37 | GrGpu::~GrGpu() {} |
bsalomon | 1d89ddc | 2014-08-19 14:20:58 -0700 | [diff] [blame] | 38 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 39 | void GrGpu::disconnect(DisconnectType) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 41 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 43 | bool 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 Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 49 | (!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 Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 56 | switch (filter) { |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 57 | 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 Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 68 | |
| 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 Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 72 | if (texProxy->hasRestrictedSampling()) { |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 73 | copyParams->fFilter = GrSamplerState::Filter::kNearest; |
| 74 | copyParams->fWidth = texProxy->width(); |
| 75 | copyParams->fHeight = texProxy->height(); |
| 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 80 | return false; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 83 | bool 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 Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 100 | sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 101 | const GrMipLevel texels[], int mipLevelCount) { |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 102 | GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "createTexture", fContext); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 103 | GrSurfaceDesc desc = origDesc; |
| 104 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 105 | GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 106 | if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 107 | return nullptr; |
egdaniel | 8c9b6f1 | 2015-05-12 13:36:30 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 110 | bool isRT = desc.fFlags & kRenderTarget_GrSurfaceFlag; |
| 111 | if (isRT) { |
| 112 | desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig); |
| 113 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 114 | // Attempt to catch un- or wrongly initialized sample counts. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 115 | SkASSERT(desc.fSampleCnt > 0 && desc.fSampleCnt <= 64); |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 116 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 117 | if (mipLevelCount && (desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 118 | return nullptr; |
| 119 | } |
| 120 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 121 | // We shouldn't be rendering into compressed textures |
| 122 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || !isRT); |
| 123 | SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig) || 1 == desc.fSampleCnt); |
| 124 | |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 125 | this->handleDirtyContext(); |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 126 | sk_sp<GrTexture> tex = this->onCreateTexture(desc, budgeted, texels, mipLevelCount); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 127 | if (tex) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 128 | if (!this->caps()->reuseScratchTextures() && !isRT) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 129 | tex->resourcePriv().removeScratchKey(); |
| 130 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 131 | fStats.incTextureCreates(); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 132 | if (mipLevelCount) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 133 | if (texels[0].fPixels) { |
| 134 | fStats.incTextureUploads(); |
| 135 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 138 | return tex; |
| 139 | } |
| 140 | |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 141 | sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 142 | return this->createTexture(desc, budgeted, nullptr, 0); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 145 | sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 146 | GrWrapOwnership ownership, GrWrapCacheable cacheable, |
| 147 | GrIOType ioType) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 148 | SkASSERT(ioType != kWrite_GrIOType); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 149 | this->handleDirtyContext(); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 150 | SkASSERT(this->caps()); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 151 | if (!this->caps()->isConfigTexturable(backendTex.config())) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 152 | return nullptr; |
| 153 | } |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 154 | if (backendTex.width() > this->caps()->maxTextureSize() || |
| 155 | backendTex.height() > this->caps()->maxTextureSize()) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 156 | return nullptr; |
| 157 | } |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 158 | return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 159 | } |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 160 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 161 | sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 162 | int sampleCnt, GrWrapOwnership ownership, |
| 163 | GrWrapCacheable cacheable) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 164 | this->handleDirtyContext(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 165 | if (sampleCnt < 1) { |
| 166 | return nullptr; |
| 167 | } |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 168 | if (!this->caps()->isConfigTexturable(backendTex.config()) || |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 169 | !this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config())) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 170 | return nullptr; |
| 171 | } |
| 172 | |
| 173 | if (backendTex.width() > this->caps()->maxRenderTargetSize() || |
| 174 | backendTex.height() > this->caps()->maxRenderTargetSize()) { |
| 175 | return nullptr; |
| 176 | } |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 177 | sk_sp<GrTexture> tex = |
| 178 | this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable); |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 179 | SkASSERT(!tex || tex->asRenderTarget()); |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 180 | return tex; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 183 | sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 184 | if (0 == this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config())) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 185 | return nullptr; |
| 186 | } |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 187 | this->handleDirtyContext(); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 188 | return this->onWrapBackendRenderTarget(backendRT); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 191 | sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 192 | int sampleCnt) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 193 | if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config())) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 194 | return nullptr; |
| 195 | } |
| 196 | int maxSize = this->caps()->maxTextureSize(); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 197 | if (tex.width() > maxSize || tex.height() > maxSize) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 198 | return nullptr; |
| 199 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 200 | this->handleDirtyContext(); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 201 | return this->onWrapBackendTextureAsRenderTarget(tex, sampleCnt); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 204 | sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo, |
| 205 | const GrVkDrawableInfo& vkInfo) { |
| 206 | return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo); |
| 207 | } |
| 208 | |
| 209 | sk_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 Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 215 | sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType, |
| 216 | GrAccessPattern accessPattern, const void* data) { |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 217 | this->handleDirtyContext(); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 218 | sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 219 | if (!this->caps()->reuseScratchBuffers()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 220 | buffer->resourcePriv().removeScratchKey(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 221 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 222 | return buffer; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 225 | bool GrGpu::copySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 226 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 227 | const SkIRect& srcRect, const SkIPoint& dstPoint, |
| 228 | bool canDiscardOutsideDstRect) { |
Brian Salomon | dcbb9d9 | 2017-07-19 10:53:20 -0400 | [diff] [blame] | 229 | GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "copySurface", fContext); |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 230 | SkASSERT(dst && src); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 231 | |
| 232 | if (dst->readOnly()) { |
| 233 | return false; |
| 234 | } |
| 235 | |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 236 | this->handleDirtyContext(); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 237 | |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 238 | return this->onCopySurface(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint, |
| 239 | canDiscardOutsideDstRect); |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 242 | bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height, |
| 243 | GrColorType dstColorType, void* buffer, size_t rowBytes) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 244 | SkASSERT(surface); |
| 245 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 246 | int bpp = GrColorTypeBytesPerPixel(dstColorType); |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 247 | if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp, |
| 248 | &left, &top, &width, &height, |
| 249 | &buffer, |
| 250 | &rowBytes)) { |
| 251 | return false; |
| 252 | } |
| 253 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 254 | if (GrPixelConfigIsCompressed(surface->config())) { |
| 255 | return false; |
| 256 | } |
| 257 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 258 | this->handleDirtyContext(); |
| 259 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 260 | return this->onReadPixels(surface, left, top, width, height, dstColorType, buffer, rowBytes); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 263 | bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height, |
| 264 | GrColorType srcColorType, const GrMipLevel texels[], int mipLevelCount) { |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 265 | SkASSERT(surface); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 266 | |
| 267 | if (surface->readOnly()) { |
| 268 | return false; |
| 269 | } |
| 270 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 271 | if (1 == mipLevelCount) { |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 272 | // We require that if we are not mipped, then the write region is contained in the surface |
| 273 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 274 | SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height()); |
| 275 | if (!bounds.contains(subRect)) { |
| 276 | return false; |
| 277 | } |
| 278 | } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) { |
| 279 | // We require that if the texels are mipped, than the write region is the entire surface |
| 280 | return false; |
| 281 | } |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 282 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 283 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 284 | if (!texels[currentMipLevel].fPixels ) { |
| 285 | return false; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
jvanverth | 2dc2994 | 2015-09-01 07:16:46 -0700 | [diff] [blame] | 288 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 289 | this->handleDirtyContext(); |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 290 | if (this->onWritePixels(surface, left, top, width, height, srcColorType, texels, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 291 | mipLevelCount)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 292 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 293 | this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 294 | fStats.incTextureUploads(); |
| 295 | return true; |
| 296 | } |
| 297 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 300 | bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height, |
| 301 | GrColorType bufferColorType, GrGpuBuffer* transferBuffer, |
| 302 | size_t offset, size_t rowBytes) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 303 | SkASSERT(texture); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 304 | SkASSERT(transferBuffer); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 305 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 306 | if (texture->readOnly()) { |
| 307 | return false; |
| 308 | } |
| 309 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 310 | // We require that the write region is contained in the texture |
| 311 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 312 | SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height()); |
| 313 | if (!bounds.contains(subRect)) { |
| 314 | return false; |
| 315 | } |
| 316 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 317 | this->handleDirtyContext(); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 318 | if (this->onTransferPixelsTo(texture, left, top, width, height, bufferColorType, transferBuffer, |
| 319 | offset, rowBytes)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 320 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 321 | this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 322 | fStats.incTransfersToTexture(); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 323 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 324 | return true; |
| 325 | } |
| 326 | return false; |
| 327 | } |
| 328 | |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 329 | bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height, |
| 330 | GrColorType bufferColorType, GrGpuBuffer* transferBuffer, |
| 331 | size_t offset) { |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 332 | SkASSERT(surface); |
| 333 | SkASSERT(transferBuffer); |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 334 | SkASSERT(this->caps()->transferFromOffsetAlignment(bufferColorType)); |
| 335 | SkASSERT(offset % this->caps()->transferFromOffsetAlignment(bufferColorType) == 0); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 336 | |
| 337 | // We require that the write region is contained in the texture |
| 338 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 339 | SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height()); |
| 340 | if (!bounds.contains(subRect)) { |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | this->handleDirtyContext(); |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 345 | if (this->onTransferPixelsFrom(surface, left, top, width, height, bufferColorType, |
| 346 | transferBuffer, offset)) { |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 347 | fStats.incTransfersFromSurface(); |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 348 | return true; |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 349 | } |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 350 | return false; |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 353 | bool GrGpu::regenerateMipMapLevels(GrTexture* texture) { |
| 354 | SkASSERT(texture); |
| 355 | SkASSERT(this->caps()->mipMapSupport()); |
| 356 | SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes); |
| 357 | SkASSERT(texture->texturePriv().mipMapsAreDirty()); |
| 358 | SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 359 | if (texture->readOnly()) { |
| 360 | return false; |
| 361 | } |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 362 | if (this->onRegenerateMipMapLevels(texture)) { |
| 363 | texture->texturePriv().markMipMapsClean(); |
| 364 | return true; |
| 365 | } |
| 366 | return false; |
| 367 | } |
| 368 | |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 369 | void GrGpu::resetTextureBindings() { |
| 370 | this->handleDirtyContext(); |
| 371 | this->onResetTextureBindings(); |
| 372 | } |
| 373 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 374 | void GrGpu::resolveRenderTarget(GrRenderTarget* target) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 375 | SkASSERT(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 376 | this->handleDirtyContext(); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 377 | this->onResolveRenderTarget(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 380 | void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds, |
| 381 | uint32_t mipLevels) const { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 382 | SkASSERT(surface); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 383 | SkASSERT(!surface->readOnly()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 384 | // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds. |
| 385 | if (nullptr == bounds || !bounds->isEmpty()) { |
| 386 | if (GrRenderTarget* target = surface->asRenderTarget()) { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 387 | SkIRect flippedBounds; |
| 388 | if (kBottomLeft_GrSurfaceOrigin == origin && bounds) { |
| 389 | flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom, |
| 390 | bounds->fRight, surface->height() - bounds->fTop}; |
| 391 | bounds = &flippedBounds; |
| 392 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 393 | target->flagAsNeedingResolve(bounds); |
| 394 | } |
| 395 | GrTexture* texture = surface->asTexture(); |
| 396 | if (texture && 1 == mipLevels) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 397 | texture->texturePriv().markMipMapsDirty(); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 402 | int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 403 | SkASSERT(this->caps()->sampleLocationsSupport()); |
| 404 | SkASSERT(renderTarget->numStencilSamples() > 1); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 405 | |
| 406 | SkSTArray<16, SkPoint> sampleLocations; |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 407 | this->querySampleLocations(renderTarget, &sampleLocations); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 408 | return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations); |
| 409 | } |
| 410 | |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 411 | GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[], |
| 412 | int n, |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 413 | SkSurface::BackendSurfaceAccess access, |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 414 | const GrFlushInfo& info, |
| 415 | const GrPrepareForExternalIORequests& externalRequests) { |
Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 416 | this->stats()->incNumFinishFlushes(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 417 | GrResourceProvider* resourceProvider = fContext->priv().resourceProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 418 | |
Brian Salomon | 9ff5acb | 2019-05-08 09:04:47 -0400 | [diff] [blame] | 419 | if (this->caps()->semaphoreSupport()) { |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 420 | for (int i = 0; i < info.fNumSemaphores; ++i) { |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 421 | sk_sp<GrSemaphore> semaphore; |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 422 | if (info.fSignalSemaphores[i].isInitialized()) { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 423 | semaphore = resourceProvider->wrapBackendSemaphore( |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 424 | info.fSignalSemaphores[i], |
| 425 | GrResourceProvider::SemaphoreWrapType::kWillSignal, |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 426 | kBorrow_GrWrapOwnership); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 427 | } else { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 428 | semaphore = resourceProvider->makeSemaphore(false); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 429 | } |
Greg Daniel | 858e12c | 2018-12-06 11:11:37 -0500 | [diff] [blame] | 430 | this->insertSemaphore(semaphore); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 431 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 432 | if (!info.fSignalSemaphores[i].isInitialized()) { |
| 433 | info.fSignalSemaphores[i] = semaphore->backendSemaphore(); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 437 | this->onFinishFlush(proxies, n, access, info, externalRequests); |
Brian Salomon | 9ff5acb | 2019-05-08 09:04:47 -0400 | [diff] [blame] | 438 | return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 439 | : GrSemaphoresSubmitted::kNo; |
| 440 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 441 | |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 442 | #ifdef SK_ENABLE_DUMP_GPU |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 443 | void GrGpu::dumpJSON(SkJSONWriter* writer) const { |
| 444 | writer->beginObject(); |
| 445 | |
| 446 | // TODO: Is there anything useful in the base class to dump here? |
| 447 | |
| 448 | this->onDumpJSON(writer); |
| 449 | |
| 450 | writer->endObject(); |
| 451 | } |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 452 | #else |
| 453 | void GrGpu::dumpJSON(SkJSONWriter* writer) const { } |
| 454 | #endif |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 455 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 456 | GrBackendTexture GrGpu::createTestingOnlyBackendTexture(int w, int h, SkColorType colorType, |
| 457 | GrMipMapped mipMapped, |
| 458 | GrRenderable renderable, |
| 459 | const void* pixels, size_t rowBytes) { |
| 460 | GrBackendFormat format = this->caps()->getBackendFormatFromColorType(colorType); |
| 461 | if (!format.isValid()) { |
| 462 | return GrBackendTexture(); |
| 463 | } |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 464 | |
Robert Phillips | f0313ee | 2019-05-21 13:51:11 -0400 | [diff] [blame] | 465 | return this->createBackendTexture(w, h, format, mipMapped, renderable, |
| 466 | pixels, rowBytes); |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 467 | } |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 468 | |
Robert Phillips | f0ced62 | 2019-05-16 09:06:25 -0400 | [diff] [blame] | 469 | #if GR_TEST_UTILS |
| 470 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 471 | #if GR_GPU_STATS |
| 472 | void GrGpu::Stats::dump(SkString* out) { |
| 473 | out->appendf("Render Target Binds: %d\n", fRenderTargetBinds); |
| 474 | out->appendf("Shader Compilations: %d\n", fShaderCompilations); |
| 475 | out->appendf("Textures Created: %d\n", fTextureCreates); |
| 476 | out->appendf("Texture Uploads: %d\n", fTextureUploads); |
| 477 | out->appendf("Transfers to Texture: %d\n", fTransfersToTexture); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 478 | out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 479 | out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates); |
| 480 | out->appendf("Number of draws: %d\n", fNumDraws); |
| 481 | } |
| 482 | |
| 483 | void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) { |
| 484 | keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds); |
| 485 | keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | #endif |
| 489 | |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 490 | #endif |