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