Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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. |
| 6 | */ |
| 7 | |
| 8 | #include "src/gpu/GrAttachment.h" |
| 9 | |
| 10 | #include "include/private/GrResourceKey.h" |
Greg Daniel | c89a7ee | 2020-10-12 16:50:18 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrBackendUtils.h" |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrCaps.h" |
Greg Daniel | c89a7ee | 2020-10-12 16:50:18 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrDataUtils.h" |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrGpu.h" |
Greg Daniel | c89a7ee | 2020-10-12 16:50:18 -0400 | [diff] [blame] | 15 | |
| 16 | size_t GrAttachment::onGpuMemorySize() const { |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 17 | // The GrTexture[RenderTarget] is built up by a bunch of attachments each of which are their |
| 18 | // own GrGpuResource. Ideally the GrRenderTarget would not be a GrGpuResource and the GrTexture |
| 19 | // would just merge with the new GrSurface/Attachment world. Then we could just depend on each |
| 20 | // attachment to give its own size since we don't have GrGpuResources owning other |
| 21 | // GrGpuResources. Until we get to that point we need to live in some hybrid world. We will let |
| 22 | // the msaa and stencil attachments track their own size because they do get cached separately. |
| 23 | // For all GrTexture* based things we will continue to to use the GrTexture* to report size and |
| 24 | // the owned attachments will have no size and be uncached. |
Greg Daniel | e895ab2 | 2021-03-12 16:29:40 -0500 | [diff] [blame] | 25 | if (!(fSupportedUsages & UsageFlags::kTexture)) { |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 26 | GrBackendFormat format = this->backendFormat(); |
| 27 | SkImage::CompressionType compression = GrBackendFormatToCompressionType(format); |
Greg Daniel | c89a7ee | 2020-10-12 16:50:18 -0400 | [diff] [blame] | 28 | |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame] | 29 | uint64_t size = GrNumBlocks(compression, this->dimensions()); |
| 30 | size *= GrBackendFormatBytesPerBlock(this->backendFormat()); |
| 31 | size *= this->numSamples(); |
| 32 | return size; |
| 33 | } |
| 34 | return 0; |
Greg Daniel | c89a7ee | 2020-10-12 16:50:18 -0400 | [diff] [blame] | 35 | } |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 36 | |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 37 | static void build_key(GrResourceKey::Builder* builder, |
| 38 | const GrCaps& caps, |
| 39 | const GrBackendFormat& format, |
| 40 | SkISize dimensions, |
| 41 | GrAttachment::UsageFlags requiredUsage, |
| 42 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 43 | GrMipmapped mipmapped, |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 44 | GrProtected isProtected) { |
| 45 | SkASSERT(!dimensions.isEmpty()); |
| 46 | |
| 47 | SkASSERT(static_cast<uint32_t>(isProtected) <= 1); |
| 48 | SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8)); |
| 49 | SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 9))); |
| 50 | |
| 51 | uint64_t formatKey = caps.computeFormatKey(format); |
| 52 | (*builder)[0] = dimensions.width(); |
| 53 | (*builder)[1] = dimensions.height(); |
| 54 | (*builder)[2] = formatKey & 0xFFFFFFFF; |
| 55 | (*builder)[3] = (formatKey >> 32) & 0xFFFFFFFF; |
| 56 | (*builder)[4] = (static_cast<uint32_t>(isProtected) << 0) | |
| 57 | (static_cast<uint32_t>(requiredUsage) << 1) | |
| 58 | (static_cast<uint32_t>(sampleCnt) << 9); |
| 59 | } |
| 60 | |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 61 | void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps, |
| 62 | const GrBackendFormat& format, |
| 63 | SkISize dimensions, |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 64 | UsageFlags requiredUsage, |
| 65 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 66 | GrMipmapped mipmapped, |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 67 | GrProtected isProtected, |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 68 | GrUniqueKey* key) { |
| 69 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 70 | |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 71 | GrUniqueKey::Builder builder(key, kDomain, 5); |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 72 | build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected); |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void GrAttachment::ComputeScratchKey(const GrCaps& caps, |
| 76 | const GrBackendFormat& format, |
| 77 | SkISize dimensions, |
| 78 | UsageFlags requiredUsage, |
| 79 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 80 | GrMipmapped mipmapped, |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 81 | GrProtected isProtected, |
| 82 | GrScratchKey* key) { |
| 83 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
| 84 | |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 85 | GrScratchKey::Builder builder(key, kType, 5); |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 86 | build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected); |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 87 | } |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 88 | |
| 89 | void GrAttachment::computeScratchKey(GrScratchKey* key) const { |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 90 | if (!SkToBool(fSupportedUsages & UsageFlags::kStencilAttachment)) { |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 91 | auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo; |
| 92 | ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), this->dimensions(), |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 93 | fSupportedUsages, this->numSamples(), this->mipmapped(), isProtected, |
| 94 | key); |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 95 | } |
| 96 | } |