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