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