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. |
| 25 | // TODO: Once we start using texture attachments this check really should be !texture. However, |
| 26 | // until then in GrVkTextureRenderTarget we make a wrapped attachment to use for the render |
| 27 | // target which duplicates the GrTexture. These will be merged once we use texture attachments. |
| 28 | if ((fSupportedUsages & UsageFlags::kStencilAttachment) || |
| 29 | ((fSupportedUsages & UsageFlags::kColorAttachment) && fSampleCnt > 1)) { |
| 30 | GrBackendFormat format = this->backendFormat(); |
| 31 | SkImage::CompressionType compression = GrBackendFormatToCompressionType(format); |
Greg Daniel | c89a7ee | 2020-10-12 16:50:18 -0400 | [diff] [blame] | 32 | |
Greg Daniel | 00d6cf4 | 2021-03-05 22:50:08 +0000 | [diff] [blame^] | 33 | uint64_t size = GrNumBlocks(compression, this->dimensions()); |
| 34 | size *= GrBackendFormatBytesPerBlock(this->backendFormat()); |
| 35 | size *= this->numSamples(); |
| 36 | return size; |
| 37 | } |
| 38 | return 0; |
Greg Daniel | c89a7ee | 2020-10-12 16:50:18 -0400 | [diff] [blame] | 39 | } |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 40 | |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 41 | static void build_key(GrResourceKey::Builder* builder, |
| 42 | const GrCaps& caps, |
| 43 | const GrBackendFormat& format, |
| 44 | SkISize dimensions, |
| 45 | GrAttachment::UsageFlags requiredUsage, |
| 46 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 47 | GrMipmapped mipmapped, |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 48 | GrProtected isProtected) { |
| 49 | SkASSERT(!dimensions.isEmpty()); |
| 50 | |
| 51 | SkASSERT(static_cast<uint32_t>(isProtected) <= 1); |
| 52 | SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8)); |
| 53 | SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 9))); |
| 54 | |
| 55 | uint64_t formatKey = caps.computeFormatKey(format); |
| 56 | (*builder)[0] = dimensions.width(); |
| 57 | (*builder)[1] = dimensions.height(); |
| 58 | (*builder)[2] = formatKey & 0xFFFFFFFF; |
| 59 | (*builder)[3] = (formatKey >> 32) & 0xFFFFFFFF; |
| 60 | (*builder)[4] = (static_cast<uint32_t>(isProtected) << 0) | |
| 61 | (static_cast<uint32_t>(requiredUsage) << 1) | |
| 62 | (static_cast<uint32_t>(sampleCnt) << 9); |
| 63 | } |
| 64 | |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 65 | void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps, |
| 66 | const GrBackendFormat& format, |
| 67 | SkISize dimensions, |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 68 | UsageFlags requiredUsage, |
| 69 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 70 | GrMipmapped mipmapped, |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 71 | GrProtected isProtected, |
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 | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 76 | build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected); |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void GrAttachment::ComputeScratchKey(const GrCaps& caps, |
| 80 | const GrBackendFormat& format, |
| 81 | SkISize dimensions, |
| 82 | UsageFlags requiredUsage, |
| 83 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 84 | GrMipmapped mipmapped, |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 85 | GrProtected isProtected, |
| 86 | GrScratchKey* key) { |
| 87 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
| 88 | |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 89 | GrScratchKey::Builder builder(key, kType, 5); |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 90 | build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected); |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 91 | } |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 92 | |
| 93 | void GrAttachment::computeScratchKey(GrScratchKey* key) const { |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 94 | if (!SkToBool(fSupportedUsages & UsageFlags::kStencilAttachment)) { |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 95 | auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo; |
| 96 | ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), this->dimensions(), |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame] | 97 | fSupportedUsages, this->numSamples(), this->mipmapped(), isProtected, |
| 98 | key); |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 99 | } |
| 100 | } |