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" |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 14 | #include "src/core/SkCompressedDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/core/SkMathPriv.h" |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 16 | #include "src/core/SkMipMap.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrCaps.h" |
| 19 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrGpuResourcePriv.h" |
Chris Dalton | 16a33c6 | 2019-09-24 22:19:17 -0600 | [diff] [blame] | 22 | #include "src/gpu/GrNativeRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrPathRendering.h" |
| 24 | #include "src/gpu/GrPipeline.h" |
| 25 | #include "src/gpu/GrRenderTargetPriv.h" |
| 26 | #include "src/gpu/GrResourceCache.h" |
| 27 | #include "src/gpu/GrResourceProvider.h" |
| 28 | #include "src/gpu/GrSemaphore.h" |
Stephen White | f3d5d44 | 2020-04-08 10:35:58 -0400 | [diff] [blame] | 29 | #include "src/gpu/GrStagingBuffer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/gpu/GrStencilAttachment.h" |
| 31 | #include "src/gpu/GrStencilSettings.h" |
| 32 | #include "src/gpu/GrSurfacePriv.h" |
| 33 | #include "src/gpu/GrTexturePriv.h" |
| 34 | #include "src/gpu/GrTextureProxyPriv.h" |
| 35 | #include "src/gpu/GrTracing.h" |
| 36 | #include "src/utils/SkJSONWriter.h" |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 37 | |
Stephen White | f3d5d44 | 2020-04-08 10:35:58 -0400 | [diff] [blame] | 38 | static const size_t kMinStagingBufferSize = 32 * 1024; |
| 39 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 40 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 42 | GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | |
Stephen White | f3d5d44 | 2020-04-08 10:35:58 -0400 | [diff] [blame] | 44 | GrGpu::~GrGpu() { |
| 45 | SkASSERT(fBusyStagingBuffers.isEmpty()); |
| 46 | } |
bsalomon | 1d89ddc | 2014-08-19 14:20:58 -0700 | [diff] [blame] | 47 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 48 | void GrGpu::disconnect(DisconnectType) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 49 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 50 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | |
Robert Phillips | bf5cb0f | 2020-02-21 13:46:38 +0000 | [diff] [blame] | 52 | bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy, |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 53 | GrSamplerState::Filter filter) { |
Robert Phillips | bf5cb0f | 2020-02-21 13:46:38 +0000 | [diff] [blame] | 54 | SkASSERT(texProxy); |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 55 | if (filter != GrSamplerState::Filter::kMipMap || texProxy->mipMapped() == GrMipMapped::kYes || |
| 56 | !caps->mipMapSupport()) { |
| 57 | return false; |
Robert Phillips | bf5cb0f | 2020-02-21 13:46:38 +0000 | [diff] [blame] | 58 | } |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 59 | return SkMipMap::ComputeLevelCount(texProxy->width(), texProxy->height()) > 0; |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 62 | static bool validate_texel_levels(SkISize dimensions, GrColorType texelColorType, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 63 | const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 64 | SkASSERT(mipLevelCount > 0); |
| 65 | bool hasBasePixels = texels[0].fPixels; |
| 66 | int levelsWithPixelsCnt = 0; |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 67 | auto bpp = GrColorTypeBytesPerPixel(texelColorType); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 68 | int w = dimensions.fWidth; |
| 69 | int h = dimensions.fHeight; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 70 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) { |
| 71 | if (texels[currentMipLevel].fPixels) { |
| 72 | const size_t minRowBytes = w * bpp; |
| 73 | if (caps->writePixelsRowBytesSupport()) { |
| 74 | if (texels[currentMipLevel].fRowBytes < minRowBytes) { |
| 75 | return false; |
| 76 | } |
| 77 | if (texels[currentMipLevel].fRowBytes % bpp) { |
| 78 | return false; |
| 79 | } |
| 80 | } else { |
| 81 | if (texels[currentMipLevel].fRowBytes != minRowBytes) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | ++levelsWithPixelsCnt; |
| 86 | } |
| 87 | if (w == 1 && h == 1) { |
| 88 | if (currentMipLevel != mipLevelCount - 1) { |
| 89 | return false; |
| 90 | } |
| 91 | } else { |
| 92 | w = std::max(w / 2, 1); |
| 93 | h = std::max(h / 2, 1); |
| 94 | } |
| 95 | } |
| 96 | // Either just a base layer or a full stack is required. |
| 97 | if (mipLevelCount != 1 && (w != 1 || h != 1)) { |
| 98 | return false; |
| 99 | } |
| 100 | // Can specify just the base, all levels, or no levels. |
| 101 | if (!hasBasePixels) { |
| 102 | return levelsWithPixelsCnt == 0; |
| 103 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 104 | return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 107 | sk_sp<GrTexture> GrGpu::createTextureCommon(SkISize dimensions, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 108 | const GrBackendFormat& format, |
| 109 | GrRenderable renderable, |
| 110 | int renderTargetSampleCnt, |
| 111 | SkBudgeted budgeted, |
| 112 | GrProtected isProtected, |
| 113 | int mipLevelCount, |
| 114 | uint32_t levelClearMask) { |
Brian Salomon | 81536f2 | 2019-08-08 16:30:49 -0400 | [diff] [blame] | 115 | if (this->caps()->isFormatCompressed(format)) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 116 | // Call GrGpu::createCompressedTexture. |
| 117 | return nullptr; |
| 118 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 119 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 120 | GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo; |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 121 | if (!this->caps()->validateSurfaceParams(dimensions, format, renderable, renderTargetSampleCnt, |
| 122 | mipMapped)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | return nullptr; |
egdaniel | 8c9b6f1 | 2015-05-12 13:36:30 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 126 | if (renderable == GrRenderable::kYes) { |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 127 | renderTargetSampleCnt = |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 128 | this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 129 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 130 | // Attempt to catch un- or wrongly initialized sample counts. |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 131 | SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 132 | this->handleDirtyContext(); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 133 | auto tex = this->onCreateTexture(dimensions, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 134 | format, |
| 135 | renderable, |
| 136 | renderTargetSampleCnt, |
| 137 | budgeted, |
| 138 | isProtected, |
| 139 | mipLevelCount, |
| 140 | levelClearMask); |
| 141 | if (tex) { |
| 142 | SkASSERT(tex->backendFormat() == format); |
| 143 | SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget()); |
| 144 | if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) { |
| 145 | tex->resourcePriv().removeScratchKey(); |
| 146 | } |
| 147 | fStats.incTextureCreates(); |
| 148 | if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) { |
| 149 | SkASSERT(GrRenderable::kYes == renderable); |
| 150 | tex->asRenderTarget()->setRequiresManualMSAAResolve(); |
| 151 | } |
| 152 | } |
| 153 | return tex; |
| 154 | } |
| 155 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 156 | sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 157 | const GrBackendFormat& format, |
| 158 | GrRenderable renderable, |
| 159 | int renderTargetSampleCnt, |
| 160 | GrMipMapped mipMapped, |
| 161 | SkBudgeted budgeted, |
| 162 | GrProtected isProtected) { |
| 163 | int mipLevelCount = 1; |
| 164 | if (mipMapped == GrMipMapped::kYes) { |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 165 | mipLevelCount = |
| 166 | 32 - SkCLZ(static_cast<uint32_t>(std::max(dimensions.fWidth, dimensions.fHeight))); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 167 | } |
| 168 | uint32_t levelClearMask = |
| 169 | this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0; |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 170 | auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt, |
| 171 | budgeted, isProtected, mipLevelCount, levelClearMask); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 172 | if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) { |
| 173 | tex->texturePriv().markMipMapsClean(); |
| 174 | } |
| 175 | return tex; |
| 176 | } |
| 177 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 178 | sk_sp<GrTexture> GrGpu::createTexture(SkISize dimensions, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 179 | const GrBackendFormat& format, |
| 180 | GrRenderable renderable, |
| 181 | int renderTargetSampleCnt, |
| 182 | SkBudgeted budgeted, |
| 183 | GrProtected isProtected, |
| 184 | GrColorType textureColorType, |
| 185 | GrColorType srcColorType, |
| 186 | const GrMipLevel texels[], |
| 187 | int texelLevelCount) { |
| 188 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 189 | if (texelLevelCount) { |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 190 | if (!validate_texel_levels(dimensions, srcColorType, texels, texelLevelCount, |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 191 | this->caps())) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 192 | return nullptr; |
| 193 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 196 | int mipLevelCount = std::max(1, texelLevelCount); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 197 | uint32_t levelClearMask = 0; |
| 198 | if (this->caps()->shouldInitializeTextures()) { |
| 199 | if (texelLevelCount) { |
| 200 | for (int i = 0; i < mipLevelCount; ++i) { |
| 201 | if (!texels->fPixels) { |
| 202 | levelClearMask |= static_cast<uint32_t>(1 << i); |
| 203 | } |
| 204 | } |
| 205 | } else { |
| 206 | levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1); |
| 207 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 210 | auto tex = this->createTextureCommon(dimensions, format, renderable, renderTargetSampleCnt, |
| 211 | budgeted, isProtected, texelLevelCount, levelClearMask); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 212 | if (tex) { |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 213 | bool markMipLevelsClean = false; |
| 214 | // Currently if level 0 does not have pixels then no other level may, as enforced by |
| 215 | // validate_texel_levels. |
| 216 | if (texelLevelCount && texels[0].fPixels) { |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 217 | if (!this->writePixels(tex.get(), 0, 0, dimensions.fWidth, dimensions.fHeight, |
| 218 | textureColorType, srcColorType, texels, texelLevelCount)) { |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 219 | return nullptr; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 220 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 221 | // Currently if level[1] of mip map has pixel data then so must all other levels. |
| 222 | // as enforced by validate_texel_levels. |
| 223 | markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels); |
| 224 | fStats.incTextureUploads(); |
| 225 | } else if (levelClearMask && mipLevelCount > 1) { |
| 226 | markMipLevelsClean = true; |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 227 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 228 | if (markMipLevelsClean) { |
| 229 | tex->texturePriv().markMipMapsClean(); |
| 230 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 231 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 232 | return tex; |
| 233 | } |
| 234 | |
Robert Phillips | 9f744f7 | 2019-12-19 19:14:33 -0500 | [diff] [blame] | 235 | sk_sp<GrTexture> GrGpu::createCompressedTexture(SkISize dimensions, |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 236 | const GrBackendFormat& format, |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 237 | SkBudgeted budgeted, |
Robert Phillips | e4720c6 | 2020-01-14 14:33:24 -0500 | [diff] [blame] | 238 | GrMipMapped mipMapped, |
Robert Phillips | 3a83392 | 2020-01-21 15:25:58 -0500 | [diff] [blame] | 239 | GrProtected isProtected, |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 240 | const void* data, |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 241 | size_t dataSize) { |
| 242 | this->handleDirtyContext(); |
Robert Phillips | 9f744f7 | 2019-12-19 19:14:33 -0500 | [diff] [blame] | 243 | if (dimensions.width() < 1 || dimensions.width() > this->caps()->maxTextureSize() || |
| 244 | dimensions.height() < 1 || dimensions.height() > this->caps()->maxTextureSize()) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 245 | return nullptr; |
| 246 | } |
Brian Salomon | a3e2996 | 2019-07-16 11:52:08 -0400 | [diff] [blame] | 247 | // Note if we relax the requirement that data must be provided then we must check |
| 248 | // caps()->shouldInitializeTextures() here. |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 249 | if (!data) { |
| 250 | return nullptr; |
| 251 | } |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 252 | if (!this->caps()->isFormatTexturable(format)) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 253 | return nullptr; |
| 254 | } |
Robert Phillips | 9f744f7 | 2019-12-19 19:14:33 -0500 | [diff] [blame] | 255 | |
| 256 | // TODO: expand CompressedDataIsCorrect to work here too |
| 257 | SkImage::CompressionType compressionType = this->caps()->compressionType(format); |
| 258 | |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 259 | if (dataSize < SkCompressedDataSize(compressionType, dimensions, nullptr, |
| 260 | mipMapped == GrMipMapped::kYes)) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 261 | return nullptr; |
| 262 | } |
Robert Phillips | 3a83392 | 2020-01-21 15:25:58 -0500 | [diff] [blame] | 263 | return this->onCreateCompressedTexture(dimensions, format, budgeted, mipMapped, isProtected, |
| 264 | data, dataSize); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 267 | sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 268 | GrWrapOwnership ownership, |
| 269 | GrWrapCacheable cacheable, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 270 | GrIOType ioType) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 271 | SkASSERT(ioType != kWrite_GrIOType); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 272 | this->handleDirtyContext(); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 273 | |
| 274 | const GrCaps* caps = this->caps(); |
| 275 | SkASSERT(caps); |
| 276 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 277 | if (!caps->isFormatTexturable(backendTex.getBackendFormat())) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 278 | return nullptr; |
| 279 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 280 | if (backendTex.width() > caps->maxTextureSize() || |
| 281 | backendTex.height() > caps->maxTextureSize()) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 282 | return nullptr; |
| 283 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 284 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 285 | return this->onWrapBackendTexture(backendTex, ownership, cacheable, ioType); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 286 | } |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 287 | |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 288 | sk_sp<GrTexture> GrGpu::wrapCompressedBackendTexture(const GrBackendTexture& backendTex, |
| 289 | GrWrapOwnership ownership, |
| 290 | GrWrapCacheable cacheable) { |
| 291 | this->handleDirtyContext(); |
| 292 | |
| 293 | const GrCaps* caps = this->caps(); |
| 294 | SkASSERT(caps); |
| 295 | |
| 296 | if (!caps->isFormatTexturable(backendTex.getBackendFormat())) { |
| 297 | return nullptr; |
| 298 | } |
| 299 | if (backendTex.width() > caps->maxTextureSize() || |
| 300 | backendTex.height() > caps->maxTextureSize()) { |
| 301 | return nullptr; |
| 302 | } |
| 303 | |
| 304 | return this->onWrapCompressedBackendTexture(backendTex, ownership, cacheable); |
| 305 | } |
| 306 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 307 | sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 308 | int sampleCnt, |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 309 | GrWrapOwnership ownership, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 310 | GrWrapCacheable cacheable) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 311 | this->handleDirtyContext(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 312 | if (sampleCnt < 1) { |
| 313 | return nullptr; |
| 314 | } |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 315 | |
| 316 | const GrCaps* caps = this->caps(); |
| 317 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 318 | if (!caps->isFormatTexturable(backendTex.getBackendFormat()) || |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 319 | !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 320 | return nullptr; |
| 321 | } |
| 322 | |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 323 | if (backendTex.width() > caps->maxRenderTargetSize() || |
| 324 | backendTex.height() > caps->maxRenderTargetSize()) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 325 | return nullptr; |
| 326 | } |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 327 | sk_sp<GrTexture> tex = |
| 328 | this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership, cacheable); |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 329 | SkASSERT(!tex || tex->asRenderTarget()); |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 330 | if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) { |
| 331 | tex->asRenderTarget()->setRequiresManualMSAAResolve(); |
| 332 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 333 | return tex; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 336 | sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) { |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 337 | this->handleDirtyContext(); |
| 338 | |
| 339 | const GrCaps* caps = this->caps(); |
| 340 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 341 | if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 342 | return nullptr; |
| 343 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 344 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 345 | sk_sp<GrRenderTarget> rt = this->onWrapBackendRenderTarget(backendRT); |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 346 | if (backendRT.isFramebufferOnly()) { |
| 347 | rt->setFramebufferOnly(); |
| 348 | } |
| 349 | return rt; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 352 | sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex, |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 353 | int sampleCnt) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 354 | this->handleDirtyContext(); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 355 | |
| 356 | const GrCaps* caps = this->caps(); |
| 357 | |
| 358 | int maxSize = caps->maxTextureSize(); |
| 359 | if (backendTex.width() > maxSize || backendTex.height() > maxSize) { |
| 360 | return nullptr; |
| 361 | } |
| 362 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 363 | if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) { |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 364 | return nullptr; |
| 365 | } |
Robert Phillips | 1cd1ed8 | 2019-07-23 13:21:01 -0400 | [diff] [blame] | 366 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 367 | auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt); |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 368 | if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) { |
| 369 | rt->setRequiresManualMSAAResolve(); |
| 370 | } |
| 371 | return rt; |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 374 | sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo, |
| 375 | const GrVkDrawableInfo& vkInfo) { |
| 376 | return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo); |
| 377 | } |
| 378 | |
| 379 | sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo, |
| 380 | const GrVkDrawableInfo& vkInfo) { |
| 381 | // This is only supported on Vulkan so we default to returning nullptr here |
| 382 | return nullptr; |
| 383 | } |
| 384 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 385 | sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType, |
| 386 | GrAccessPattern accessPattern, const void* data) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 387 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 388 | this->handleDirtyContext(); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 389 | sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 390 | if (!this->caps()->reuseScratchBuffers()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 391 | buffer->resourcePriv().removeScratchKey(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 392 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 393 | return buffer; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 396 | bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
Greg Daniel | e227fe4 | 2019-08-21 13:52:24 -0400 | [diff] [blame] | 397 | const SkIPoint& dstPoint) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 398 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 399 | SkASSERT(dst && src); |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 400 | SkASSERT(!src->framebufferOnly()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 401 | |
| 402 | if (dst->readOnly()) { |
| 403 | return false; |
| 404 | } |
| 405 | |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 406 | this->handleDirtyContext(); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 407 | |
Greg Daniel | e227fe4 | 2019-08-21 13:52:24 -0400 | [diff] [blame] | 408 | return this->onCopySurface(dst, src, srcRect, dstPoint); |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 411 | bool GrGpu::readPixels(GrSurface* surface, int left, int top, int width, int height, |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 412 | GrColorType surfaceColorType, GrColorType dstColorType, void* buffer, |
| 413 | size_t rowBytes) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 414 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 415 | SkASSERT(surface); |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 416 | SkASSERT(!surface->framebufferOnly()); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 417 | SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat())); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 418 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 419 | auto subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 420 | auto bounds = SkIRect::MakeWH(surface->width(), surface->height()); |
| 421 | if (!bounds.contains(subRect)) { |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 422 | return false; |
| 423 | } |
| 424 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 425 | size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width); |
| 426 | if (!this->caps()->readPixelsRowBytesSupport()) { |
| 427 | if (rowBytes != minRowBytes) { |
| 428 | return false; |
| 429 | } |
| 430 | } else { |
| 431 | if (rowBytes < minRowBytes) { |
| 432 | return false; |
| 433 | } |
| 434 | if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) { |
| 435 | return false; |
| 436 | } |
| 437 | } |
| 438 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 439 | this->handleDirtyContext(); |
| 440 | |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 441 | return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType, |
| 442 | buffer, rowBytes); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 445 | bool GrGpu::writePixels(GrSurface* surface, int left, int top, int width, int height, |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 446 | GrColorType surfaceColorType, GrColorType srcColorType, |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 447 | const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 448 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Stan Iliev | 1db4d14 | 2020-04-10 00:58:52 -0400 | [diff] [blame] | 449 | ATRACE_ANDROID_FRAMEWORK_ALWAYS("texture_upload"); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 450 | SkASSERT(surface); |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 451 | SkASSERT(!surface->framebufferOnly()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 452 | |
| 453 | if (surface->readOnly()) { |
| 454 | return false; |
| 455 | } |
| 456 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 457 | if (mipLevelCount == 0) { |
| 458 | return false; |
| 459 | } else if (mipLevelCount == 1) { |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 460 | // We require that if we are not mipped, then the write region is contained in the surface |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 461 | auto subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 462 | auto bounds = SkIRect::MakeWH(surface->width(), surface->height()); |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 463 | if (!bounds.contains(subRect)) { |
| 464 | return false; |
| 465 | } |
| 466 | } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) { |
| 467 | // We require that if the texels are mipped, than the write region is the entire surface |
| 468 | return false; |
| 469 | } |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 470 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 471 | if (!validate_texel_levels({width, height}, srcColorType, texels, mipLevelCount, |
| 472 | this->caps())) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 473 | return false; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 474 | } |
jvanverth | 2dc2994 | 2015-09-01 07:16:46 -0700 | [diff] [blame] | 475 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 476 | this->handleDirtyContext(); |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 477 | if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType, |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 478 | texels, mipLevelCount, prepForTexSampling)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 479 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 480 | this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 481 | fStats.incTextureUploads(); |
| 482 | return true; |
| 483 | } |
| 484 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 487 | bool GrGpu::transferPixelsTo(GrTexture* texture, int left, int top, int width, int height, |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 488 | GrColorType textureColorType, GrColorType bufferColorType, |
| 489 | GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 490 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 491 | SkASSERT(texture); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 492 | SkASSERT(transferBuffer); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 493 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 494 | if (texture->readOnly()) { |
| 495 | return false; |
| 496 | } |
| 497 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 498 | // We require that the write region is contained in the texture |
| 499 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 500 | SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height()); |
| 501 | if (!bounds.contains(subRect)) { |
| 502 | return false; |
| 503 | } |
| 504 | |
Brian Salomon | b28cb68 | 2019-07-26 12:48:47 -0400 | [diff] [blame] | 505 | size_t bpp = GrColorTypeBytesPerPixel(bufferColorType); |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 506 | if (this->caps()->writePixelsRowBytesSupport()) { |
| 507 | if (rowBytes < SkToSizeT(bpp * width)) { |
| 508 | return false; |
| 509 | } |
| 510 | if (rowBytes % bpp) { |
| 511 | return false; |
| 512 | } |
| 513 | } else { |
| 514 | if (rowBytes != SkToSizeT(bpp * width)) { |
| 515 | return false; |
| 516 | } |
| 517 | } |
| 518 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 519 | this->handleDirtyContext(); |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 520 | if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType, |
| 521 | bufferColorType, transferBuffer, offset, rowBytes)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 522 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 523 | this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 524 | fStats.incTransfersToTexture(); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 525 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 526 | return true; |
| 527 | } |
| 528 | return false; |
| 529 | } |
| 530 | |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 531 | bool GrGpu::transferPixelsFrom(GrSurface* surface, int left, int top, int width, int height, |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 532 | GrColorType surfaceColorType, GrColorType bufferColorType, |
| 533 | GrGpuBuffer* transferBuffer, size_t offset) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 534 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 535 | SkASSERT(surface); |
| 536 | SkASSERT(transferBuffer); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 537 | SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat())); |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 538 | |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 539 | #ifdef SK_DEBUG |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 540 | auto supportedRead = this->caps()->supportedReadPixelsColorType( |
| 541 | surfaceColorType, surface->backendFormat(), bufferColorType); |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 542 | SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer); |
| 543 | SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0); |
| 544 | #endif |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 545 | |
| 546 | // We require that the write region is contained in the texture |
| 547 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 548 | SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height()); |
| 549 | if (!bounds.contains(subRect)) { |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | this->handleDirtyContext(); |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 554 | if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType, |
| 555 | bufferColorType, transferBuffer, offset)) { |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 556 | fStats.incTransfersFromSurface(); |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 557 | return true; |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 558 | } |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 559 | return false; |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 562 | bool GrGpu::regenerateMipMapLevels(GrTexture* texture) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 563 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 564 | SkASSERT(texture); |
| 565 | SkASSERT(this->caps()->mipMapSupport()); |
| 566 | SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes); |
Chris Dalton | 16a33c6 | 2019-09-24 22:19:17 -0600 | [diff] [blame] | 567 | if (!texture->texturePriv().mipMapsAreDirty()) { |
| 568 | // This can happen when the proxy expects mipmaps to be dirty, but they are not dirty on the |
| 569 | // actual target. This may be caused by things that the drawingManager could not predict, |
| 570 | // i.e., ops that don't draw anything, aborting a draw for exceptional circumstances, etc. |
| 571 | // NOTE: This goes away once we quit tracking mipmap state on the actual texture. |
| 572 | return true; |
| 573 | } |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 574 | if (texture->readOnly()) { |
| 575 | return false; |
| 576 | } |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 577 | if (this->onRegenerateMipMapLevels(texture)) { |
| 578 | texture->texturePriv().markMipMapsClean(); |
| 579 | return true; |
| 580 | } |
| 581 | return false; |
| 582 | } |
| 583 | |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 584 | void GrGpu::resetTextureBindings() { |
| 585 | this->handleDirtyContext(); |
| 586 | this->onResetTextureBindings(); |
| 587 | } |
| 588 | |
Chris Dalton | 16a33c6 | 2019-09-24 22:19:17 -0600 | [diff] [blame] | 589 | void GrGpu::resolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect, |
Greg Daniel | 242536f | 2020-02-13 14:12:46 -0500 | [diff] [blame] | 590 | ForExternalIO forExternalIO) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 591 | SkASSERT(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 592 | this->handleDirtyContext(); |
Greg Daniel | 242536f | 2020-02-13 14:12:46 -0500 | [diff] [blame] | 593 | this->onResolveRenderTarget(target, resolveRect, forExternalIO); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 596 | void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds, |
| 597 | uint32_t mipLevels) const { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 598 | SkASSERT(surface); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 599 | SkASSERT(!surface->readOnly()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 600 | // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds. |
| 601 | if (nullptr == bounds || !bounds->isEmpty()) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 602 | GrTexture* texture = surface->asTexture(); |
| 603 | if (texture && 1 == mipLevels) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 604 | texture->texturePriv().markMipMapsDirty(); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 609 | int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 610 | SkASSERT(this->caps()->sampleLocationsSupport()); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 611 | SkASSERT(renderTarget->numSamples() > 1 || |
| 612 | (renderTarget->renderTargetPriv().getStencilAttachment() && |
| 613 | renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1)); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 614 | |
| 615 | SkSTArray<16, SkPoint> sampleLocations; |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 616 | this->querySampleLocations(renderTarget, &sampleLocations); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 617 | return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations); |
| 618 | } |
| 619 | |
Stephen White | f3d5d44 | 2020-04-08 10:35:58 -0400 | [diff] [blame] | 620 | #ifdef SK_DEBUG |
| 621 | bool GrGpu::inStagingBuffers(GrStagingBuffer* b) const { |
| 622 | for (const auto& i : fStagingBuffers) { |
| 623 | if (b == i.get()) { |
| 624 | return true; |
| 625 | } |
| 626 | } |
| 627 | return false; |
| 628 | } |
| 629 | |
| 630 | void GrGpu::validateStagingBuffers() const { |
| 631 | for (const auto& i : fStagingBuffers) { |
| 632 | GrStagingBuffer* buffer = i.get(); |
| 633 | SkASSERT(fAvailableStagingBuffers.isInList(buffer) || |
| 634 | fActiveStagingBuffers.isInList(buffer) || |
| 635 | fBusyStagingBuffers.isInList(buffer)); |
| 636 | } |
| 637 | for (auto b : fAvailableStagingBuffers) { |
| 638 | SkASSERT(this->inStagingBuffers(b)); |
| 639 | } |
| 640 | for (auto b : fActiveStagingBuffers) { |
| 641 | SkASSERT(this->inStagingBuffers(b)); |
| 642 | } |
| 643 | for (auto b : fBusyStagingBuffers) { |
| 644 | SkASSERT(this->inStagingBuffers(b)); |
| 645 | } |
| 646 | } |
| 647 | #endif |
| 648 | |
Greg Daniel | fe15962 | 2020-04-10 17:43:51 +0000 | [diff] [blame] | 649 | void GrGpu::executeFlushInfo(GrSurfaceProxy* proxies[], |
| 650 | int numProxies, |
| 651 | SkSurface::BackendSurfaceAccess access, |
| 652 | const GrFlushInfo& info, |
| 653 | const GrPrepareForExternalIORequests& externalRequests) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 654 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Greg Daniel | fe15962 | 2020-04-10 17:43:51 +0000 | [diff] [blame] | 655 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 656 | GrResourceProvider* resourceProvider = fContext->priv().resourceProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 657 | |
Greg Daniel | 8561fc2 | 2020-04-08 12:52:51 -0400 | [diff] [blame] | 658 | std::unique_ptr<std::unique_ptr<GrSemaphore>[]> semaphores( |
| 659 | new std::unique_ptr<GrSemaphore>[info.fNumSemaphores]); |
Greg Daniel | 30a35e8 | 2019-11-19 14:12:25 -0500 | [diff] [blame] | 660 | if (this->caps()->semaphoreSupport() && info.fNumSemaphores) { |
Greg Daniel | 8561fc2 | 2020-04-08 12:52:51 -0400 | [diff] [blame] | 661 | for (int i = 0; i < info.fNumSemaphores; ++i) { |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 662 | if (info.fSignalSemaphores[i].isInitialized()) { |
Greg Daniel | 8561fc2 | 2020-04-08 12:52:51 -0400 | [diff] [blame] | 663 | semaphores[i] = resourceProvider->wrapBackendSemaphore( |
| 664 | info.fSignalSemaphores[i], |
| 665 | GrResourceProvider::SemaphoreWrapType::kWillSignal, |
| 666 | kBorrow_GrWrapOwnership); |
| 667 | this->insertSemaphore(semaphores[i].get()); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 668 | } else { |
Greg Daniel | 8561fc2 | 2020-04-08 12:52:51 -0400 | [diff] [blame] | 669 | semaphores[i] = resourceProvider->makeSemaphore(false); |
| 670 | if (semaphores[i]) { |
| 671 | this->insertSemaphore(semaphores[i].get()); |
| 672 | info.fSignalSemaphores[i] = semaphores[i]->backendSemaphore(); |
| 673 | } |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | } |
Greg Daniel | 30a35e8 | 2019-11-19 14:12:25 -0500 | [diff] [blame] | 677 | |
Greg Daniel | fe15962 | 2020-04-10 17:43:51 +0000 | [diff] [blame] | 678 | if (info.fFinishedProc) { |
| 679 | this->addFinishedProc(info.fFinishedProc, info.fFinishedContext); |
| 680 | } |
| 681 | this->prepareSurfacesForBackendAccessAndExternalIO(proxies, numProxies, access, |
| 682 | externalRequests); |
| 683 | } |
| 684 | |
| 685 | bool GrGpu::submitToGpu(bool syncCpu) { |
| 686 | this->stats()->incNumSubmitToGpus(); |
| 687 | |
| 688 | #ifdef SK_DEBUG |
| 689 | this->validateStagingBuffers(); |
| 690 | #endif |
Stephen White | f3d5d44 | 2020-04-08 10:35:58 -0400 | [diff] [blame] | 691 | this->unmapStagingBuffers(); |
| 692 | |
Greg Daniel | fe15962 | 2020-04-10 17:43:51 +0000 | [diff] [blame] | 693 | bool submitted = this->onSubmitToGpu(syncCpu); |
Greg Daniel | 8561fc2 | 2020-04-08 12:52:51 -0400 | [diff] [blame] | 694 | |
Stephen White | f3d5d44 | 2020-04-08 10:35:58 -0400 | [diff] [blame] | 695 | // Move all active staging buffers to the busy list. |
Greg Daniel | fe15962 | 2020-04-10 17:43:51 +0000 | [diff] [blame] | 696 | // TODO: this should probably be handled inside of the onSubmitToGpu by the backends. |
Stephen White | f3d5d44 | 2020-04-08 10:35:58 -0400 | [diff] [blame] | 697 | while (GrStagingBuffer* buffer = fActiveStagingBuffers.head()) { |
| 698 | fActiveStagingBuffers.remove(buffer); |
| 699 | fBusyStagingBuffers.addToTail(buffer); |
| 700 | } |
Greg Daniel | fe15962 | 2020-04-10 17:43:51 +0000 | [diff] [blame] | 701 | return submitted; |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 702 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 703 | |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 704 | #ifdef SK_ENABLE_DUMP_GPU |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 705 | void GrGpu::dumpJSON(SkJSONWriter* writer) const { |
| 706 | writer->beginObject(); |
| 707 | |
| 708 | // TODO: Is there anything useful in the base class to dump here? |
| 709 | |
| 710 | this->onDumpJSON(writer); |
| 711 | |
| 712 | writer->endObject(); |
| 713 | } |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 714 | #else |
| 715 | void GrGpu::dumpJSON(SkJSONWriter* writer) const { } |
| 716 | #endif |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 717 | |
Robert Phillips | f0ced62 | 2019-05-16 09:06:25 -0400 | [diff] [blame] | 718 | #if GR_TEST_UTILS |
| 719 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 720 | #if GR_GPU_STATS |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 721 | static const char* cache_result_to_str(int i) { |
| 722 | const char* kCacheResultStrings[GrGpu::Stats::kNumProgramCacheResults] = { |
| 723 | "hits", |
| 724 | "misses", |
| 725 | "partials" |
| 726 | }; |
| 727 | static_assert(0 == (int) GrGpu::Stats::ProgramCacheResult::kHit); |
| 728 | static_assert(1 == (int) GrGpu::Stats::ProgramCacheResult::kMiss); |
| 729 | static_assert(2 == (int) GrGpu::Stats::ProgramCacheResult::kPartial); |
| 730 | static_assert(GrGpu::Stats::kNumProgramCacheResults == 3); |
| 731 | return kCacheResultStrings[i]; |
| 732 | } |
| 733 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 734 | void GrGpu::Stats::dump(SkString* out) { |
| 735 | out->appendf("Render Target Binds: %d\n", fRenderTargetBinds); |
| 736 | out->appendf("Shader Compilations: %d\n", fShaderCompilations); |
| 737 | out->appendf("Textures Created: %d\n", fTextureCreates); |
| 738 | out->appendf("Texture Uploads: %d\n", fTextureUploads); |
| 739 | out->appendf("Transfers to Texture: %d\n", fTransfersToTexture); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 740 | out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 741 | out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates); |
| 742 | out->appendf("Number of draws: %d\n", fNumDraws); |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 743 | out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused); |
Robert Phillips | 19f466d | 2020-02-26 10:27:07 -0500 | [diff] [blame] | 744 | |
| 745 | SkASSERT(fNumInlineCompilationFailures == 0); |
| 746 | out->appendf("Number of Inline compile failures %d\n", fNumInlineCompilationFailures); |
| 747 | for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) { |
| 748 | out->appendf("Inline Program Cache %s %d\n", cache_result_to_str(i), |
| 749 | fInlineProgramCacheStats[i]); |
| 750 | } |
| 751 | |
| 752 | SkASSERT(fNumPreCompilationFailures == 0); |
| 753 | out->appendf("Number of precompile failures %d\n", fNumPreCompilationFailures); |
| 754 | for (int i = 0; i < Stats::kNumProgramCacheResults-1; ++i) { |
| 755 | out->appendf("Precompile Program Cache %s %d\n", cache_result_to_str(i), |
| 756 | fPreProgramCacheStats[i]); |
| 757 | } |
| 758 | |
| 759 | SkASSERT(fNumCompilationFailures == 0); |
| 760 | out->appendf("Total number of compilation failures %d\n", fNumCompilationFailures); |
| 761 | out->appendf("Total number of partial compilation successes %d\n", |
| 762 | fNumPartialCompilationSuccesses); |
| 763 | out->appendf("Total number of compilation successes %d\n", fNumCompilationSuccesses); |
Robert Phillips | 6eb5cb9 | 2020-03-05 12:52:45 -0500 | [diff] [blame] | 764 | |
| 765 | // enable this block to output CSV-style stats for program pre-compilation |
| 766 | #if 0 |
| 767 | SkASSERT(fNumInlineCompilationFailures == 0); |
| 768 | SkASSERT(fNumPreCompilationFailures == 0); |
| 769 | SkASSERT(fNumCompilationFailures == 0); |
| 770 | SkASSERT(fNumPartialCompilationSuccesses == 0); |
| 771 | |
| 772 | SkDebugf("%d, %d, %d, %d, %d\n", |
| 773 | fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kHit], |
| 774 | fInlineProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss], |
| 775 | fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kHit], |
| 776 | fPreProgramCacheStats[(int) Stats::ProgramCacheResult::kMiss], |
| 777 | fNumCompilationSuccesses); |
| 778 | #endif |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) { |
| 782 | keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds); |
| 783 | keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 784 | } |
| 785 | |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 786 | #endif // GR_GPU_STATS |
| 787 | #endif // GR_TEST_UTILS |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 788 | |
Robert Phillips | ba5c7ad | 2020-01-24 11:03:33 -0500 | [diff] [blame] | 789 | bool GrGpu::MipMapsAreCorrect(SkISize dimensions, |
| 790 | GrMipMapped mipMapped, |
| 791 | const BackendTextureData* data) { |
| 792 | int numMipLevels = 1; |
| 793 | if (mipMapped == GrMipMapped::kYes) { |
| 794 | numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1; |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 795 | } |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 796 | |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 797 | if (!data || data->type() == BackendTextureData::Type::kColor) { |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 798 | return true; |
| 799 | } |
| 800 | |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 801 | if (data->type() == BackendTextureData::Type::kCompressed) { |
Robert Phillips | ba5c7ad | 2020-01-24 11:03:33 -0500 | [diff] [blame] | 802 | return false; // This should be going through CompressedDataIsCorrect |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | SkASSERT(data->type() == BackendTextureData::Type::kPixmaps); |
| 806 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 807 | if (data->pixmap(0).dimensions() != dimensions) { |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 808 | return false; |
| 809 | } |
| 810 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 811 | SkColorType colorType = data->pixmap(0).colorType(); |
Robert Phillips | ba5c7ad | 2020-01-24 11:03:33 -0500 | [diff] [blame] | 812 | for (int i = 1; i < numMipLevels; ++i) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 813 | dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)}; |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 814 | if (dimensions != data->pixmap(i).dimensions()) { |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 815 | return false; |
| 816 | } |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 817 | if (colorType != data->pixmap(i).colorType()) { |
| 818 | return false; |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 819 | } |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 820 | } |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 821 | return true; |
| 822 | } |
| 823 | |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 824 | bool GrGpu::CompressedDataIsCorrect(SkISize dimensions, SkImage::CompressionType compressionType, |
| 825 | GrMipMapped mipMapped, const BackendTextureData* data) { |
| 826 | |
| 827 | if (!data || data->type() == BackendTextureData::Type::kColor) { |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | if (data->type() == BackendTextureData::Type::kPixmaps) { |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | SkASSERT(data->type() == BackendTextureData::Type::kCompressed); |
| 836 | |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 837 | size_t computedSize = SkCompressedDataSize(compressionType, dimensions, |
| 838 | nullptr, mipMapped == GrMipMapped::kYes); |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 839 | |
| 840 | return computedSize == data->compressedSize(); |
| 841 | } |
| 842 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 843 | GrBackendTexture GrGpu::createBackendTexture(SkISize dimensions, |
| 844 | const GrBackendFormat& format, |
| 845 | GrRenderable renderable, |
Robert Phillips | ba5c7ad | 2020-01-24 11:03:33 -0500 | [diff] [blame] | 846 | GrMipMapped mipMapped, |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame^] | 847 | GrProtected isProtected) { |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 848 | const GrCaps* caps = this->caps(); |
| 849 | |
| 850 | if (!format.isValid()) { |
| 851 | return {}; |
| 852 | } |
| 853 | |
Robert Phillips | d34691b | 2019-09-24 13:38:43 -0400 | [diff] [blame] | 854 | if (caps->isFormatCompressed(format)) { |
| 855 | // Compressed formats must go through the createCompressedBackendTexture API |
| 856 | return {}; |
| 857 | } |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 858 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 859 | if (dimensions.isEmpty() || dimensions.width() > caps->maxTextureSize() || |
| 860 | dimensions.height() > caps->maxTextureSize()) { |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 861 | return {}; |
| 862 | } |
| 863 | |
Robert Phillips | ba5c7ad | 2020-01-24 11:03:33 -0500 | [diff] [blame] | 864 | if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) { |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 865 | return {}; |
| 866 | } |
| 867 | |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame^] | 868 | return this->onCreateBackendTexture(dimensions, format, renderable, mipMapped, isProtected); |
| 869 | } |
| 870 | |
| 871 | bool GrGpu::updateBackendTexture(const GrBackendTexture& backendTexture, |
| 872 | GrGpuFinishedProc finishedProc, |
| 873 | GrGpuFinishedContext finishedContext, |
| 874 | const BackendTextureData* data) { |
| 875 | SkASSERT(data); |
| 876 | const GrCaps* caps = this->caps(); |
| 877 | |
| 878 | sk_sp<GrRefCntedCallback> callback; |
| 879 | if (finishedProc) { |
| 880 | callback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 881 | } |
| 882 | |
Greg Daniel | 16032b3 | 2020-05-06 15:31:10 -0400 | [diff] [blame^] | 883 | if (!backendTexture.isValid()) { |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | if (data->type() == BackendTextureData::Type::kPixmaps) { |
| 888 | auto ct = SkColorTypeToGrColorType(data->pixmap(0).colorType()); |
| 889 | if (!caps->areColorTypeAndFormatCompatible(ct, backendTexture.getBackendFormat())) { |
| 890 | return false; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | if (backendTexture.hasMipMaps() && !this->caps()->mipMapSupport()) { |
| 895 | return false; |
| 896 | } |
| 897 | |
| 898 | GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 899 | if (!MipMapsAreCorrect(backendTexture.dimensions(), mipMapped, data)) { |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | return this->onUpdateBackendTexture(backendTexture, std::move(callback), data); |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 904 | } |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 905 | |
| 906 | GrBackendTexture GrGpu::createCompressedBackendTexture(SkISize dimensions, |
| 907 | const GrBackendFormat& format, |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 908 | GrMipMapped mipMapped, |
Robert Phillips | 4277f01 | 2020-01-21 14:28:34 -0500 | [diff] [blame] | 909 | GrProtected isProtected, |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 910 | GrGpuFinishedProc finishedProc, |
| 911 | GrGpuFinishedContext finishedContext, |
Robert Phillips | 4277f01 | 2020-01-21 14:28:34 -0500 | [diff] [blame] | 912 | const BackendTextureData* data) { |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 913 | sk_sp<GrRefCntedCallback> callback; |
| 914 | if (finishedProc) { |
| 915 | callback.reset(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 916 | } |
| 917 | |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 918 | const GrCaps* caps = this->caps(); |
| 919 | |
| 920 | if (!format.isValid()) { |
| 921 | return {}; |
| 922 | } |
| 923 | |
| 924 | SkImage::CompressionType compressionType = caps->compressionType(format); |
| 925 | if (compressionType == SkImage::CompressionType::kNone) { |
| 926 | // Uncompressed formats must go through the createBackendTexture API |
| 927 | return {}; |
| 928 | } |
| 929 | |
| 930 | if (dimensions.isEmpty() || |
| 931 | dimensions.width() > caps->maxTextureSize() || |
| 932 | dimensions.height() > caps->maxTextureSize()) { |
| 933 | return {}; |
| 934 | } |
| 935 | |
| 936 | if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) { |
| 937 | return {}; |
| 938 | } |
| 939 | |
| 940 | if (!CompressedDataIsCorrect(dimensions, compressionType, mipMapped, data)) { |
| 941 | return {}; |
| 942 | } |
| 943 | |
Robert Phillips | 4277f01 | 2020-01-21 14:28:34 -0500 | [diff] [blame] | 944 | return this->onCreateCompressedBackendTexture(dimensions, format, mipMapped, |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 945 | isProtected, std::move(callback), data); |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 946 | } |
Stephen White | f3d5d44 | 2020-04-08 10:35:58 -0400 | [diff] [blame] | 947 | |
| 948 | GrStagingBuffer* GrGpu::findStagingBuffer(size_t size) { |
| 949 | #ifdef SK_DEBUG |
| 950 | this->validateStagingBuffers(); |
| 951 | #endif |
| 952 | for (auto b : fActiveStagingBuffers) { |
| 953 | if (b->remaining() >= size) { |
| 954 | return b; |
| 955 | } |
| 956 | } |
| 957 | for (auto b : fAvailableStagingBuffers) { |
| 958 | if (b->remaining() >= size) { |
| 959 | fAvailableStagingBuffers.remove(b); |
| 960 | fActiveStagingBuffers.addToTail(b); |
| 961 | return b; |
| 962 | } |
| 963 | } |
| 964 | size = SkNextPow2(size); |
| 965 | size = std::max(size, kMinStagingBufferSize); |
| 966 | std::unique_ptr<GrStagingBuffer> b = this->createStagingBuffer(size); |
| 967 | GrStagingBuffer* stagingBuffer = b.get(); |
| 968 | fStagingBuffers.push_back(std::move(b)); |
| 969 | fActiveStagingBuffers.addToTail(stagingBuffer); |
| 970 | return stagingBuffer; |
| 971 | } |
| 972 | |
| 973 | GrStagingBuffer::Slice GrGpu::allocateStagingBufferSlice(size_t size) { |
| 974 | #ifdef SK_DEBUG |
| 975 | this->validateStagingBuffers(); |
| 976 | #endif |
| 977 | GrStagingBuffer* stagingBuffer = this->findStagingBuffer(size); |
| 978 | return stagingBuffer->allocate(size); |
| 979 | } |
| 980 | |
| 981 | void GrGpu::unmapStagingBuffers() { |
| 982 | #ifdef SK_DEBUG |
| 983 | this->validateStagingBuffers(); |
| 984 | #endif |
| 985 | // Unmap all active buffers. |
| 986 | for (auto buffer : fActiveStagingBuffers) { |
| 987 | buffer->unmap(); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | void GrGpu::markStagingBufferAvailable(GrStagingBuffer* buffer) { |
| 992 | #ifdef SK_DEBUG |
| 993 | this->validateStagingBuffers(); |
| 994 | #endif |
| 995 | fBusyStagingBuffers.remove(buffer); |
| 996 | fAvailableStagingBuffers.addToTail(buffer); |
| 997 | } |