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