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 | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 25 | if (!(fSupportedUsages & UsageFlags::kTexture) && fMemoryless == GrMemoryless::kNo) { |
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 | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 44 | GrProtected isProtected, |
| 45 | GrMemoryless memoryless) { |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 46 | SkASSERT(!dimensions.isEmpty()); |
| 47 | |
| 48 | SkASSERT(static_cast<uint32_t>(isProtected) <= 1); |
Greg Daniel | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 49 | SkASSERT(static_cast<uint32_t>(memoryless) <= 1); |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 50 | SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8)); |
Greg Daniel | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 51 | SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 10))); |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 52 | |
| 53 | uint64_t formatKey = caps.computeFormatKey(format); |
| 54 | (*builder)[0] = dimensions.width(); |
| 55 | (*builder)[1] = dimensions.height(); |
| 56 | (*builder)[2] = formatKey & 0xFFFFFFFF; |
| 57 | (*builder)[3] = (formatKey >> 32) & 0xFFFFFFFF; |
| 58 | (*builder)[4] = (static_cast<uint32_t>(isProtected) << 0) | |
Greg Daniel | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 59 | (static_cast<uint32_t>(memoryless) << 1) | |
| 60 | (static_cast<uint32_t>(requiredUsage) << 2) | |
| 61 | (static_cast<uint32_t>(sampleCnt) << 10); |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 64 | void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps, |
| 65 | const GrBackendFormat& format, |
| 66 | SkISize dimensions, |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 67 | UsageFlags requiredUsage, |
| 68 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 69 | GrMipmapped mipmapped, |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 70 | GrProtected isProtected, |
Greg Daniel | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 71 | GrMemoryless memoryless, |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 72 | GrUniqueKey* key) { |
| 73 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 74 | |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 75 | GrUniqueKey::Builder builder(key, kDomain, 5); |
Greg Daniel | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 76 | build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected, |
| 77 | memoryless); |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void GrAttachment::ComputeScratchKey(const GrCaps& caps, |
| 81 | const GrBackendFormat& format, |
| 82 | SkISize dimensions, |
| 83 | UsageFlags requiredUsage, |
| 84 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 85 | GrMipmapped mipmapped, |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 86 | GrProtected isProtected, |
Greg Daniel | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 87 | GrMemoryless memoryless, |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 88 | GrScratchKey* key) { |
| 89 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
| 90 | |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 91 | GrScratchKey::Builder builder(key, kType, 5); |
Greg Daniel | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 92 | build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected, |
| 93 | memoryless); |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 94 | } |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 95 | |
| 96 | void GrAttachment::computeScratchKey(GrScratchKey* key) const { |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 97 | if (!SkToBool(fSupportedUsages & UsageFlags::kStencilAttachment)) { |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 98 | auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo; |
Greg Daniel | 7743559 | 2021-09-22 13:55:44 -0400 | [diff] [blame] | 99 | ComputeScratchKey(*this->getGpu()->caps(), |
| 100 | this->backendFormat(), |
| 101 | this->dimensions(), |
| 102 | fSupportedUsages, |
| 103 | this->numSamples(), |
| 104 | this->mipmapped(), |
| 105 | isProtected, |
| 106 | fMemoryless, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 107 | key); |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 108 | } |
| 109 | } |