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" |
| 14 | #include "src/core/SkMathPriv.h" |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 15 | #include "src/core/SkMipMap.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrCaps.h" |
| 18 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrGpuResourcePriv.h" |
| 21 | #include "src/gpu/GrMesh.h" |
| 22 | #include "src/gpu/GrPathRendering.h" |
| 23 | #include "src/gpu/GrPipeline.h" |
| 24 | #include "src/gpu/GrRenderTargetPriv.h" |
| 25 | #include "src/gpu/GrResourceCache.h" |
| 26 | #include "src/gpu/GrResourceProvider.h" |
| 27 | #include "src/gpu/GrSemaphore.h" |
| 28 | #include "src/gpu/GrStencilAttachment.h" |
| 29 | #include "src/gpu/GrStencilSettings.h" |
| 30 | #include "src/gpu/GrSurfacePriv.h" |
| 31 | #include "src/gpu/GrTexturePriv.h" |
| 32 | #include "src/gpu/GrTextureProxyPriv.h" |
| 33 | #include "src/gpu/GrTracing.h" |
| 34 | #include "src/utils/SkJSONWriter.h" |
bsalomon | cb8979d | 2015-05-05 09:51:38 -0700 | [diff] [blame] | 35 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 36 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 38 | GrGpu::GrGpu(GrContext* context) : fResetBits(kAll_GrBackendState), fContext(context) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 40 | GrGpu::~GrGpu() {} |
bsalomon | 1d89ddc | 2014-08-19 14:20:58 -0700 | [diff] [blame] | 41 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 42 | void GrGpu::disconnect(DisconnectType) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 44 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 46 | bool GrGpu::IsACopyNeededForRepeatWrapMode(const GrCaps* caps, GrTextureProxy* texProxy, |
| 47 | int width, int height, |
| 48 | GrSamplerState::Filter filter, |
| 49 | GrTextureProducer::CopyParams* copyParams, |
| 50 | SkScalar scaleAdjust[2]) { |
| 51 | if (!caps->npotTextureTileSupport() && |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 52 | (!SkIsPow2(width) || !SkIsPow2(height))) { |
| 53 | SkASSERT(scaleAdjust); |
| 54 | copyParams->fWidth = GrNextPow2(width); |
| 55 | copyParams->fHeight = GrNextPow2(height); |
| 56 | SkASSERT(scaleAdjust); |
| 57 | scaleAdjust[0] = ((SkScalar)copyParams->fWidth) / width; |
| 58 | scaleAdjust[1] = ((SkScalar)copyParams->fHeight) / height; |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 59 | switch (filter) { |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 60 | case GrSamplerState::Filter::kNearest: |
| 61 | copyParams->fFilter = GrSamplerState::Filter::kNearest; |
| 62 | break; |
| 63 | case GrSamplerState::Filter::kBilerp: |
| 64 | case GrSamplerState::Filter::kMipMap: |
| 65 | // We are only ever scaling up so no reason to ever indicate kMipMap. |
| 66 | copyParams->fFilter = GrSamplerState::Filter::kBilerp; |
| 67 | break; |
| 68 | } |
| 69 | return true; |
| 70 | } |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 71 | |
| 72 | if (texProxy) { |
| 73 | // If the texture format itself doesn't support repeat wrap mode or mipmapping (and |
| 74 | // those capabilities are required) force a copy. |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 75 | if (texProxy->hasRestrictedSampling()) { |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 76 | copyParams->fFilter = GrSamplerState::Filter::kNearest; |
| 77 | copyParams->fWidth = texProxy->width(); |
| 78 | copyParams->fHeight = texProxy->height(); |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 83 | return false; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 86 | bool GrGpu::IsACopyNeededForMips(const GrCaps* caps, const GrTextureProxy* texProxy, |
| 87 | GrSamplerState::Filter filter, |
| 88 | GrTextureProducer::CopyParams* copyParams) { |
| 89 | SkASSERT(texProxy); |
| 90 | bool willNeedMips = GrSamplerState::Filter::kMipMap == filter && caps->mipMapSupport(); |
| 91 | // If the texture format itself doesn't support mipmapping (and those capabilities are required) |
| 92 | // force a copy. |
| 93 | if (willNeedMips && texProxy->mipMapped() == GrMipMapped::kNo) { |
| 94 | copyParams->fFilter = GrSamplerState::Filter::kNearest; |
| 95 | copyParams->fWidth = texProxy->width(); |
| 96 | copyParams->fHeight = texProxy->height(); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 103 | static bool validate_texel_levels(int w, int h, GrColorType texelColorType, |
| 104 | const GrMipLevel* texels, int mipLevelCount, const GrCaps* caps) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 105 | SkASSERT(mipLevelCount > 0); |
| 106 | bool hasBasePixels = texels[0].fPixels; |
| 107 | int levelsWithPixelsCnt = 0; |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 108 | auto bpp = GrColorTypeBytesPerPixel(texelColorType); |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 109 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) { |
| 110 | if (texels[currentMipLevel].fPixels) { |
| 111 | const size_t minRowBytes = w * bpp; |
| 112 | if (caps->writePixelsRowBytesSupport()) { |
| 113 | if (texels[currentMipLevel].fRowBytes < minRowBytes) { |
| 114 | return false; |
| 115 | } |
| 116 | if (texels[currentMipLevel].fRowBytes % bpp) { |
| 117 | return false; |
| 118 | } |
| 119 | } else { |
| 120 | if (texels[currentMipLevel].fRowBytes != minRowBytes) { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | ++levelsWithPixelsCnt; |
| 125 | } |
| 126 | if (w == 1 && h == 1) { |
| 127 | if (currentMipLevel != mipLevelCount - 1) { |
| 128 | return false; |
| 129 | } |
| 130 | } else { |
| 131 | w = std::max(w / 2, 1); |
| 132 | h = std::max(h / 2, 1); |
| 133 | } |
| 134 | } |
| 135 | // Either just a base layer or a full stack is required. |
| 136 | if (mipLevelCount != 1 && (w != 1 || h != 1)) { |
| 137 | return false; |
| 138 | } |
| 139 | // Can specify just the base, all levels, or no levels. |
| 140 | if (!hasBasePixels) { |
| 141 | return levelsWithPixelsCnt == 0; |
| 142 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 143 | return levelsWithPixelsCnt == 1 || levelsWithPixelsCnt == mipLevelCount; |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 146 | sk_sp<GrTexture> GrGpu::createTextureCommon(const GrSurfaceDesc& desc, |
| 147 | const GrBackendFormat& format, |
| 148 | GrRenderable renderable, |
| 149 | int renderTargetSampleCnt, |
| 150 | SkBudgeted budgeted, |
| 151 | GrProtected isProtected, |
| 152 | int mipLevelCount, |
| 153 | uint32_t levelClearMask) { |
Brian Salomon | 81536f2 | 2019-08-08 16:30:49 -0400 | [diff] [blame] | 154 | if (this->caps()->isFormatCompressed(format)) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 155 | // Call GrGpu::createCompressedTexture. |
| 156 | return nullptr; |
| 157 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 158 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 159 | GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo; |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 160 | if (!this->caps()->validateSurfaceParams({desc.fWidth, desc.fHeight}, format, desc.fConfig, |
| 161 | renderable, renderTargetSampleCnt, mipMapped)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 162 | return nullptr; |
egdaniel | 8c9b6f1 | 2015-05-12 13:36:30 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 165 | if (renderable == GrRenderable::kYes) { |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 166 | renderTargetSampleCnt = |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 167 | this->caps()->getRenderTargetSampleCount(renderTargetSampleCnt, format); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 168 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 169 | // Attempt to catch un- or wrongly initialized sample counts. |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 170 | SkASSERT(renderTargetSampleCnt > 0 && renderTargetSampleCnt <= 64); |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 171 | this->handleDirtyContext(); |
| 172 | auto tex = this->onCreateTexture(desc, |
| 173 | format, |
| 174 | renderable, |
| 175 | renderTargetSampleCnt, |
| 176 | budgeted, |
| 177 | isProtected, |
| 178 | mipLevelCount, |
| 179 | levelClearMask); |
| 180 | if (tex) { |
| 181 | SkASSERT(tex->backendFormat() == format); |
| 182 | SkASSERT(GrRenderable::kNo == renderable || tex->asRenderTarget()); |
| 183 | if (!this->caps()->reuseScratchTextures() && renderable == GrRenderable::kNo) { |
| 184 | tex->resourcePriv().removeScratchKey(); |
| 185 | } |
| 186 | fStats.incTextureCreates(); |
| 187 | if (renderTargetSampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) { |
| 188 | SkASSERT(GrRenderable::kYes == renderable); |
| 189 | tex->asRenderTarget()->setRequiresManualMSAAResolve(); |
| 190 | } |
| 191 | } |
| 192 | return tex; |
| 193 | } |
| 194 | |
| 195 | sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, |
| 196 | const GrBackendFormat& format, |
| 197 | GrRenderable renderable, |
| 198 | int renderTargetSampleCnt, |
| 199 | GrMipMapped mipMapped, |
| 200 | SkBudgeted budgeted, |
| 201 | GrProtected isProtected) { |
| 202 | int mipLevelCount = 1; |
| 203 | if (mipMapped == GrMipMapped::kYes) { |
| 204 | mipLevelCount = 32 - SkCLZ(static_cast<uint32_t>(SkTMax(desc.fWidth, desc.fHeight))); |
| 205 | } |
| 206 | uint32_t levelClearMask = |
| 207 | this->caps()->shouldInitializeTextures() ? (1 << mipLevelCount) - 1 : 0; |
| 208 | auto tex = this->createTextureCommon(desc, format, renderable, renderTargetSampleCnt, budgeted, |
| 209 | isProtected, mipLevelCount, levelClearMask); |
| 210 | if (tex && mipMapped == GrMipMapped::kYes && levelClearMask) { |
| 211 | tex->texturePriv().markMipMapsClean(); |
| 212 | } |
| 213 | return tex; |
| 214 | } |
| 215 | |
| 216 | sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, |
| 217 | const GrBackendFormat& format, |
| 218 | GrRenderable renderable, |
| 219 | int renderTargetSampleCnt, |
| 220 | SkBudgeted budgeted, |
| 221 | GrProtected isProtected, |
| 222 | GrColorType textureColorType, |
| 223 | GrColorType srcColorType, |
| 224 | const GrMipLevel texels[], |
| 225 | int texelLevelCount) { |
| 226 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 227 | if (texelLevelCount) { |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 228 | if (!validate_texel_levels(desc.fWidth, desc.fHeight, srcColorType, texels, texelLevelCount, |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 229 | this->caps())) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 230 | return nullptr; |
| 231 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | int mipLevelCount = SkTMax(1, texelLevelCount); |
| 235 | uint32_t levelClearMask = 0; |
| 236 | if (this->caps()->shouldInitializeTextures()) { |
| 237 | if (texelLevelCount) { |
| 238 | for (int i = 0; i < mipLevelCount; ++i) { |
| 239 | if (!texels->fPixels) { |
| 240 | levelClearMask |= static_cast<uint32_t>(1 << i); |
| 241 | } |
| 242 | } |
| 243 | } else { |
| 244 | levelClearMask = static_cast<uint32_t>((1 << mipLevelCount) - 1); |
| 245 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 246 | } |
| 247 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 248 | auto tex = this->createTextureCommon(desc, format, renderable, renderTargetSampleCnt, budgeted, |
| 249 | isProtected, texelLevelCount, levelClearMask); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 250 | if (tex) { |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 251 | bool markMipLevelsClean = false; |
| 252 | // Currently if level 0 does not have pixels then no other level may, as enforced by |
| 253 | // validate_texel_levels. |
| 254 | if (texelLevelCount && texels[0].fPixels) { |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 255 | if (!this->writePixels(tex.get(), 0, 0, desc.fWidth, desc.fHeight, textureColorType, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 256 | srcColorType, texels, texelLevelCount)) { |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 257 | return nullptr; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 258 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 259 | // Currently if level[1] of mip map has pixel data then so must all other levels. |
| 260 | // as enforced by validate_texel_levels. |
| 261 | markMipLevelsClean = (texelLevelCount > 1 && !levelClearMask && texels[1].fPixels); |
| 262 | fStats.incTextureUploads(); |
| 263 | } else if (levelClearMask && mipLevelCount > 1) { |
| 264 | markMipLevelsClean = true; |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 265 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 266 | if (markMipLevelsClean) { |
| 267 | tex->texturePriv().markMipMapsClean(); |
| 268 | } |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 269 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 270 | return tex; |
| 271 | } |
| 272 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 273 | sk_sp<GrTexture> GrGpu::createCompressedTexture(int width, int height, |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 274 | const GrBackendFormat& format, |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 275 | SkImage::CompressionType compressionType, |
| 276 | SkBudgeted budgeted, const void* data, |
| 277 | size_t dataSize) { |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 278 | // If we ever add a new CompressionType, we should add a check here to make sure the |
| 279 | // GrBackendFormat and CompressionType are compatible with eachother. |
| 280 | SkASSERT(compressionType == SkImage::kETC1_CompressionType); |
| 281 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 282 | this->handleDirtyContext(); |
| 283 | if (width < 1 || width > this->caps()->maxTextureSize() || |
| 284 | height < 1 || height > this->caps()->maxTextureSize()) { |
| 285 | return nullptr; |
| 286 | } |
Brian Salomon | a3e2996 | 2019-07-16 11:52:08 -0400 | [diff] [blame] | 287 | // Note if we relax the requirement that data must be provided then we must check |
| 288 | // caps()->shouldInitializeTextures() here. |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 289 | if (!data) { |
| 290 | return nullptr; |
| 291 | } |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 292 | if (!this->caps()->isFormatTexturable(format)) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 293 | return nullptr; |
| 294 | } |
| 295 | if (dataSize < GrCompressedDataSize(compressionType, width, height)) { |
| 296 | return nullptr; |
| 297 | } |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 298 | return this->onCreateCompressedTexture(width, height, format, compressionType, budgeted, data); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 301 | sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex, |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 302 | GrColorType colorType, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 303 | GrWrapOwnership ownership, GrWrapCacheable cacheable, |
| 304 | GrIOType ioType) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 305 | SkASSERT(ioType != kWrite_GrIOType); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 306 | this->handleDirtyContext(); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 307 | |
| 308 | const GrCaps* caps = this->caps(); |
| 309 | SkASSERT(caps); |
| 310 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 311 | if (!caps->isFormatTexturable(backendTex.getBackendFormat())) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 312 | return nullptr; |
| 313 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 314 | if (backendTex.width() > caps->maxTextureSize() || |
| 315 | backendTex.height() > caps->maxTextureSize()) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 316 | return nullptr; |
| 317 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 318 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 319 | return this->onWrapBackendTexture(backendTex, colorType, ownership, cacheable, ioType); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 320 | } |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 321 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 322 | sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 323 | int sampleCnt, GrColorType colorType, |
| 324 | GrWrapOwnership ownership, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 325 | GrWrapCacheable cacheable) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 326 | this->handleDirtyContext(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 327 | if (sampleCnt < 1) { |
| 328 | return nullptr; |
| 329 | } |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 330 | |
| 331 | const GrCaps* caps = this->caps(); |
| 332 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 333 | if (!caps->isFormatTexturable(backendTex.getBackendFormat()) || |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 334 | !caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 335 | return nullptr; |
| 336 | } |
| 337 | |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 338 | if (backendTex.width() > caps->maxRenderTargetSize() || |
| 339 | backendTex.height() > caps->maxRenderTargetSize()) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 340 | return nullptr; |
| 341 | } |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 342 | sk_sp<GrTexture> tex = this->onWrapRenderableBackendTexture(backendTex, sampleCnt, colorType, |
| 343 | ownership, cacheable); |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 344 | SkASSERT(!tex || tex->asRenderTarget()); |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 345 | if (tex && sampleCnt > 1 && !caps->msaaResolvesAutomatically()) { |
| 346 | tex->asRenderTarget()->setRequiresManualMSAAResolve(); |
| 347 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 348 | return tex; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 351 | sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT, |
| 352 | GrColorType colorType) { |
| 353 | this->handleDirtyContext(); |
| 354 | |
| 355 | const GrCaps* caps = this->caps(); |
| 356 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 357 | if (!caps->isFormatRenderable(backendRT.getBackendFormat(), backendRT.sampleCnt())) { |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 358 | return nullptr; |
| 359 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 360 | |
| 361 | return this->onWrapBackendRenderTarget(backendRT, colorType); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 364 | sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& backendTex, |
| 365 | int sampleCnt, |
| 366 | GrColorType colorType) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 367 | this->handleDirtyContext(); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 368 | |
| 369 | const GrCaps* caps = this->caps(); |
| 370 | |
| 371 | int maxSize = caps->maxTextureSize(); |
| 372 | if (backendTex.width() > maxSize || backendTex.height() > maxSize) { |
| 373 | return nullptr; |
| 374 | } |
| 375 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 376 | if (!caps->isFormatRenderable(backendTex.getBackendFormat(), sampleCnt)) { |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 377 | return nullptr; |
| 378 | } |
Robert Phillips | 1cd1ed8 | 2019-07-23 13:21:01 -0400 | [diff] [blame] | 379 | |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 380 | auto rt = this->onWrapBackendTextureAsRenderTarget(backendTex, sampleCnt, colorType); |
| 381 | if (rt && sampleCnt > 1 && !this->caps()->msaaResolvesAutomatically()) { |
| 382 | rt->setRequiresManualMSAAResolve(); |
| 383 | } |
| 384 | return rt; |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 387 | sk_sp<GrRenderTarget> GrGpu::wrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo, |
| 388 | const GrVkDrawableInfo& vkInfo) { |
| 389 | return this->onWrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo); |
| 390 | } |
| 391 | |
| 392 | sk_sp<GrRenderTarget> GrGpu::onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo& imageInfo, |
| 393 | const GrVkDrawableInfo& vkInfo) { |
| 394 | // This is only supported on Vulkan so we default to returning nullptr here |
| 395 | return nullptr; |
| 396 | } |
| 397 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 398 | sk_sp<GrGpuBuffer> GrGpu::createBuffer(size_t size, GrGpuBufferType intendedType, |
| 399 | GrAccessPattern accessPattern, const void* data) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 400 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 401 | this->handleDirtyContext(); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 402 | sk_sp<GrGpuBuffer> buffer = this->onCreateBuffer(size, intendedType, accessPattern, data); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 403 | if (!this->caps()->reuseScratchBuffers()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 404 | buffer->resourcePriv().removeScratchKey(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 405 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 406 | return buffer; |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 409 | bool GrGpu::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
Greg Daniel | e227fe4 | 2019-08-21 13:52:24 -0400 | [diff] [blame] | 410 | const SkIPoint& dstPoint) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 411 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 412 | SkASSERT(dst && src); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 413 | |
| 414 | if (dst->readOnly()) { |
| 415 | return false; |
| 416 | } |
| 417 | |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 418 | this->handleDirtyContext(); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 419 | |
Greg Daniel | e227fe4 | 2019-08-21 13:52:24 -0400 | [diff] [blame] | 420 | return this->onCopySurface(dst, src, srcRect, dstPoint); |
joshualitt | 1cbdcde | 2015-08-21 11:53:29 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 423 | 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] | 424 | GrColorType surfaceColorType, GrColorType dstColorType, void* buffer, |
| 425 | size_t rowBytes) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 426 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 427 | SkASSERT(surface); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 428 | SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat())); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 429 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 430 | auto subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 431 | auto bounds = SkIRect::MakeWH(surface->width(), surface->height()); |
| 432 | if (!bounds.contains(subRect)) { |
egdaniel | 6d901da | 2015-07-30 12:02:15 -0700 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 436 | size_t minRowBytes = SkToSizeT(GrColorTypeBytesPerPixel(dstColorType) * width); |
| 437 | if (!this->caps()->readPixelsRowBytesSupport()) { |
| 438 | if (rowBytes != minRowBytes) { |
| 439 | return false; |
| 440 | } |
| 441 | } else { |
| 442 | if (rowBytes < minRowBytes) { |
| 443 | return false; |
| 444 | } |
| 445 | if (rowBytes % GrColorTypeBytesPerPixel(dstColorType)) { |
| 446 | return false; |
| 447 | } |
| 448 | } |
| 449 | |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 450 | if (this->caps()->isFormatCompressed(surface->backendFormat())) { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 451 | return false; |
| 452 | } |
| 453 | |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 454 | this->handleDirtyContext(); |
| 455 | |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 456 | return this->onReadPixels(surface, left, top, width, height, surfaceColorType, dstColorType, |
| 457 | buffer, rowBytes); |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 460 | 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] | 461 | GrColorType surfaceColorType, GrColorType srcColorType, |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 462 | const GrMipLevel texels[], int mipLevelCount, bool prepForTexSampling) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 463 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 464 | SkASSERT(surface); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 465 | SkASSERT(this->caps()->isFormatTexturableAndUploadable(surfaceColorType, |
| 466 | surface->backendFormat())); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 467 | |
| 468 | if (surface->readOnly()) { |
| 469 | return false; |
| 470 | } |
| 471 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 472 | if (mipLevelCount == 0) { |
| 473 | return false; |
| 474 | } else if (mipLevelCount == 1) { |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 475 | // 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] | 476 | auto subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 477 | auto bounds = SkIRect::MakeWH(surface->width(), surface->height()); |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 478 | if (!bounds.contains(subRect)) { |
| 479 | return false; |
| 480 | } |
| 481 | } else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) { |
| 482 | // We require that if the texels are mipped, than the write region is the entire surface |
| 483 | return false; |
| 484 | } |
Robert Phillips | b7b7e5f | 2017-05-22 13:23:19 -0400 | [diff] [blame] | 485 | |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 486 | if (!validate_texel_levels(width, height, srcColorType, texels, mipLevelCount, this->caps())) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 487 | return false; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 488 | } |
jvanverth | 2dc2994 | 2015-09-01 07:16:46 -0700 | [diff] [blame] | 489 | |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 490 | this->handleDirtyContext(); |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 491 | if (this->onWritePixels(surface, left, top, width, height, surfaceColorType, srcColorType, |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 492 | texels, mipLevelCount, prepForTexSampling)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 493 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 494 | this->didWriteToSurface(surface, kTopLeft_GrSurfaceOrigin, &rect, mipLevelCount); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 495 | fStats.incTextureUploads(); |
| 496 | return true; |
| 497 | } |
| 498 | return false; |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 501 | 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] | 502 | GrColorType textureColorType, GrColorType bufferColorType, |
| 503 | GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 504 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 505 | SkASSERT(texture); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 506 | SkASSERT(transferBuffer); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 507 | SkASSERT(this->caps()->isFormatTexturableAndUploadable(textureColorType, |
| 508 | texture->backendFormat())); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 509 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 510 | if (texture->readOnly()) { |
| 511 | return false; |
| 512 | } |
| 513 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 514 | // We require that the write region is contained in the texture |
| 515 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 516 | SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height()); |
| 517 | if (!bounds.contains(subRect)) { |
| 518 | return false; |
| 519 | } |
| 520 | |
Brian Salomon | b28cb68 | 2019-07-26 12:48:47 -0400 | [diff] [blame] | 521 | size_t bpp = GrColorTypeBytesPerPixel(bufferColorType); |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 522 | if (this->caps()->writePixelsRowBytesSupport()) { |
| 523 | if (rowBytes < SkToSizeT(bpp * width)) { |
| 524 | return false; |
| 525 | } |
| 526 | if (rowBytes % bpp) { |
| 527 | return false; |
| 528 | } |
| 529 | } else { |
| 530 | if (rowBytes != SkToSizeT(bpp * width)) { |
| 531 | return false; |
| 532 | } |
| 533 | } |
| 534 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 535 | this->handleDirtyContext(); |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 536 | if (this->onTransferPixelsTo(texture, left, top, width, height, textureColorType, |
| 537 | bufferColorType, transferBuffer, offset, rowBytes)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 538 | SkIRect rect = SkIRect::MakeXYWH(left, top, width, height); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 539 | this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 540 | fStats.incTransfersToTexture(); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 541 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | return false; |
| 545 | } |
| 546 | |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 547 | 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] | 548 | GrColorType surfaceColorType, GrColorType bufferColorType, |
| 549 | GrGpuBuffer* transferBuffer, size_t offset) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 550 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 551 | SkASSERT(surface); |
| 552 | SkASSERT(transferBuffer); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 553 | SkASSERT(this->caps()->isFormatTexturable(surface->backendFormat())); |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 554 | |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 555 | #ifdef SK_DEBUG |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 556 | auto supportedRead = this->caps()->supportedReadPixelsColorType( |
| 557 | surfaceColorType, surface->backendFormat(), bufferColorType); |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 558 | SkASSERT(supportedRead.fOffsetAlignmentForTransferBuffer); |
| 559 | SkASSERT(offset % supportedRead.fOffsetAlignmentForTransferBuffer == 0); |
| 560 | #endif |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 561 | |
| 562 | // We require that the write region is contained in the texture |
| 563 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 564 | SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height()); |
| 565 | if (!bounds.contains(subRect)) { |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | this->handleDirtyContext(); |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 570 | if (this->onTransferPixelsFrom(surface, left, top, width, height, surfaceColorType, |
| 571 | bufferColorType, transferBuffer, offset)) { |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 572 | fStats.incTransfersFromSurface(); |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 573 | return true; |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 574 | } |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 575 | return false; |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 578 | bool GrGpu::regenerateMipMapLevels(GrTexture* texture) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 579 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 580 | SkASSERT(texture); |
| 581 | SkASSERT(this->caps()->mipMapSupport()); |
| 582 | SkASSERT(texture->texturePriv().mipMapped() == GrMipMapped::kYes); |
| 583 | SkASSERT(texture->texturePriv().mipMapsAreDirty()); |
| 584 | SkASSERT(!texture->asRenderTarget() || !texture->asRenderTarget()->needsResolve()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 585 | if (texture->readOnly()) { |
| 586 | return false; |
| 587 | } |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 588 | if (this->onRegenerateMipMapLevels(texture)) { |
| 589 | texture->texturePriv().markMipMapsClean(); |
| 590 | return true; |
| 591 | } |
| 592 | return false; |
| 593 | } |
| 594 | |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 595 | void GrGpu::resetTextureBindings() { |
| 596 | this->handleDirtyContext(); |
| 597 | this->onResetTextureBindings(); |
| 598 | } |
| 599 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 600 | void GrGpu::resolveRenderTarget(GrRenderTarget* target) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 601 | SkASSERT(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 602 | this->handleDirtyContext(); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 603 | this->onResolveRenderTarget(target); |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 606 | void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds, |
| 607 | uint32_t mipLevels) const { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 608 | SkASSERT(surface); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 609 | SkASSERT(!surface->readOnly()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 610 | // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds. |
| 611 | if (nullptr == bounds || !bounds->isEmpty()) { |
| 612 | if (GrRenderTarget* target = surface->asRenderTarget()) { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 613 | SkIRect flippedBounds; |
| 614 | if (kBottomLeft_GrSurfaceOrigin == origin && bounds) { |
| 615 | flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom, |
| 616 | bounds->fRight, surface->height() - bounds->fTop}; |
| 617 | bounds = &flippedBounds; |
| 618 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 619 | target->flagAsNeedingResolve(bounds); |
| 620 | } |
| 621 | GrTexture* texture = surface->asTexture(); |
| 622 | if (texture && 1 == mipLevels) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 623 | texture->texturePriv().markMipMapsDirty(); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 628 | int GrGpu::findOrAssignSamplePatternKey(GrRenderTarget* renderTarget) { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 629 | SkASSERT(this->caps()->sampleLocationsSupport()); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 630 | SkASSERT(renderTarget->numSamples() > 1 || |
| 631 | (renderTarget->renderTargetPriv().getStencilAttachment() && |
| 632 | renderTarget->renderTargetPriv().getStencilAttachment()->numSamples() > 1)); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 633 | |
| 634 | SkSTArray<16, SkPoint> sampleLocations; |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 635 | this->querySampleLocations(renderTarget, &sampleLocations); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 636 | return fSamplePatternDictionary.findOrAssignSamplePatternKey(sampleLocations); |
| 637 | } |
| 638 | |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 639 | GrSemaphoresSubmitted GrGpu::finishFlush(GrSurfaceProxy* proxies[], |
| 640 | int n, |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 641 | SkSurface::BackendSurfaceAccess access, |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 642 | const GrFlushInfo& info, |
| 643 | const GrPrepareForExternalIORequests& externalRequests) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 644 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Greg Daniel | d207345 | 2018-12-07 11:20:33 -0500 | [diff] [blame] | 645 | this->stats()->incNumFinishFlushes(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 646 | GrResourceProvider* resourceProvider = fContext->priv().resourceProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 647 | |
Brian Salomon | 9ff5acb | 2019-05-08 09:04:47 -0400 | [diff] [blame] | 648 | if (this->caps()->semaphoreSupport()) { |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 649 | for (int i = 0; i < info.fNumSemaphores; ++i) { |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 650 | sk_sp<GrSemaphore> semaphore; |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 651 | if (info.fSignalSemaphores[i].isInitialized()) { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 652 | semaphore = resourceProvider->wrapBackendSemaphore( |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 653 | info.fSignalSemaphores[i], |
| 654 | GrResourceProvider::SemaphoreWrapType::kWillSignal, |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 655 | kBorrow_GrWrapOwnership); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 656 | } else { |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 657 | semaphore = resourceProvider->makeSemaphore(false); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 658 | } |
Greg Daniel | 858e12c | 2018-12-06 11:11:37 -0500 | [diff] [blame] | 659 | this->insertSemaphore(semaphore); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 660 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 661 | if (!info.fSignalSemaphores[i].isInitialized()) { |
| 662 | info.fSignalSemaphores[i] = semaphore->backendSemaphore(); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | } |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 666 | this->onFinishFlush(proxies, n, access, info, externalRequests); |
Brian Salomon | 9ff5acb | 2019-05-08 09:04:47 -0400 | [diff] [blame] | 667 | return this->caps()->semaphoreSupport() ? GrSemaphoresSubmitted::kYes |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 668 | : GrSemaphoresSubmitted::kNo; |
| 669 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 670 | |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 671 | #ifdef SK_ENABLE_DUMP_GPU |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 672 | void GrGpu::dumpJSON(SkJSONWriter* writer) const { |
| 673 | writer->beginObject(); |
| 674 | |
| 675 | // TODO: Is there anything useful in the base class to dump here? |
| 676 | |
| 677 | this->onDumpJSON(writer); |
| 678 | |
| 679 | writer->endObject(); |
| 680 | } |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 681 | #else |
| 682 | void GrGpu::dumpJSON(SkJSONWriter* writer) const { } |
| 683 | #endif |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 684 | |
Robert Phillips | f0ced62 | 2019-05-16 09:06:25 -0400 | [diff] [blame] | 685 | #if GR_TEST_UTILS |
| 686 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 687 | #if GR_GPU_STATS |
| 688 | void GrGpu::Stats::dump(SkString* out) { |
| 689 | out->appendf("Render Target Binds: %d\n", fRenderTargetBinds); |
| 690 | out->appendf("Shader Compilations: %d\n", fShaderCompilations); |
| 691 | out->appendf("Textures Created: %d\n", fTextureCreates); |
| 692 | out->appendf("Texture Uploads: %d\n", fTextureUploads); |
| 693 | out->appendf("Transfers to Texture: %d\n", fTransfersToTexture); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 694 | out->appendf("Transfers from Surface: %d\n", fTransfersFromSurface); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 695 | out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates); |
| 696 | out->appendf("Number of draws: %d\n", fNumDraws); |
Robert Phillips | f9fcf7f | 2019-07-11 09:03:27 -0400 | [diff] [blame] | 697 | out->appendf("Number of Scratch Textures reused %d\n", fNumScratchTexturesReused); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) { |
| 701 | keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds); |
| 702 | keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 703 | } |
| 704 | |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 705 | #endif // GR_GPU_STATS |
| 706 | #endif // GR_TEST_UTILS |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 707 | |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 708 | |
| 709 | bool GrGpu::MipMapsAreCorrect(int baseWidth, int baseHeight, GrMipMapped mipMapped, |
| 710 | const SkPixmap srcData[], int numMipLevels) { |
| 711 | if (!srcData) { |
| 712 | return true; |
| 713 | } |
| 714 | |
| 715 | if (baseWidth != srcData[0].width() || baseHeight != srcData[0].height()) { |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | if (mipMapped == GrMipMapped::kYes) { |
| 720 | if (numMipLevels != SkMipMap::ComputeLevelCount(baseWidth, baseHeight) + 1) { |
| 721 | return false; |
| 722 | } |
| 723 | |
| 724 | SkColorType colorType = srcData[0].colorType(); |
| 725 | |
| 726 | int currentWidth = baseWidth; |
| 727 | int currentHeight = baseHeight; |
| 728 | for (int i = 1; i < numMipLevels; ++i) { |
| 729 | currentWidth = SkTMax(1, currentWidth / 2); |
| 730 | currentHeight = SkTMax(1, currentHeight / 2); |
| 731 | |
| 732 | if (srcData[i].colorType() != colorType) { // all levels must have same colorType |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | if (srcData[i].width() != currentWidth || srcData[i].height() != currentHeight) { |
| 737 | return false; |
| 738 | } |
| 739 | } |
| 740 | } else if (numMipLevels != 1) { |
| 741 | return false; |
| 742 | } |
| 743 | |
| 744 | return true; |
| 745 | } |
| 746 | |
| 747 | GrBackendTexture GrGpu::createBackendTexture(int w, int h, const GrBackendFormat& format, |
| 748 | GrMipMapped mipMapped, GrRenderable renderable, |
| 749 | const SkPixmap srcData[], int numMipLevels, |
| 750 | const SkColor4f* color, GrProtected isProtected) { |
| 751 | const GrCaps* caps = this->caps(); |
| 752 | |
| 753 | if (!format.isValid()) { |
| 754 | return {}; |
| 755 | } |
| 756 | |
Robert Phillips | d34691b | 2019-09-24 13:38:43 -0400 | [diff] [blame^] | 757 | if (caps->isFormatCompressed(format)) { |
| 758 | // Compressed formats must go through the createCompressedBackendTexture API |
| 759 | return {}; |
| 760 | } |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 761 | |
| 762 | if (w < 1 || w > caps->maxTextureSize() || h < 1 || h > caps->maxTextureSize()) { |
| 763 | return {}; |
| 764 | } |
| 765 | |
| 766 | // TODO: maybe just ignore the mipMapped parameter in this case |
| 767 | if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) { |
| 768 | return {}; |
| 769 | } |
| 770 | |
| 771 | if (!MipMapsAreCorrect(w, h, mipMapped, srcData, numMipLevels)) { |
| 772 | return {}; |
| 773 | } |
| 774 | |
| 775 | return this->onCreateBackendTexture(w, h, format, mipMapped, renderable, |
| 776 | srcData, numMipLevels, color, isProtected); |
| 777 | } |