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 { |
| 17 | GrBackendFormat format = this->backendFormat(); |
| 18 | SkImage::CompressionType compression = GrBackendFormatToCompressionType(format); |
| 19 | |
| 20 | uint64_t size = GrNumBlocks(compression, this->dimensions()); |
| 21 | size *= GrBackendFormatBytesPerBlock(this->backendFormat()); |
| 22 | size *= this->numSamples(); |
| 23 | return size; |
| 24 | } |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 25 | |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 26 | static void build_key(GrResourceKey::Builder* builder, |
| 27 | const GrCaps& caps, |
| 28 | const GrBackendFormat& format, |
| 29 | SkISize dimensions, |
| 30 | GrAttachment::UsageFlags requiredUsage, |
| 31 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame^] | 32 | GrMipmapped mipmapped, |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 33 | GrProtected isProtected) { |
| 34 | SkASSERT(!dimensions.isEmpty()); |
| 35 | |
| 36 | SkASSERT(static_cast<uint32_t>(isProtected) <= 1); |
| 37 | SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8)); |
| 38 | SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 9))); |
| 39 | |
| 40 | uint64_t formatKey = caps.computeFormatKey(format); |
| 41 | (*builder)[0] = dimensions.width(); |
| 42 | (*builder)[1] = dimensions.height(); |
| 43 | (*builder)[2] = formatKey & 0xFFFFFFFF; |
| 44 | (*builder)[3] = (formatKey >> 32) & 0xFFFFFFFF; |
| 45 | (*builder)[4] = (static_cast<uint32_t>(isProtected) << 0) | |
| 46 | (static_cast<uint32_t>(requiredUsage) << 1) | |
| 47 | (static_cast<uint32_t>(sampleCnt) << 9); |
| 48 | } |
| 49 | |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 50 | void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps, |
| 51 | const GrBackendFormat& format, |
| 52 | SkISize dimensions, |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 53 | UsageFlags requiredUsage, |
| 54 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame^] | 55 | GrMipmapped mipmapped, |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 56 | GrProtected isProtected, |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 57 | GrUniqueKey* key) { |
| 58 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 59 | |
Greg Daniel | b8949bd | 2020-10-12 15:21:02 -0400 | [diff] [blame] | 60 | GrUniqueKey::Builder builder(key, kDomain, 5); |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame^] | 61 | build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected); |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void GrAttachment::ComputeScratchKey(const GrCaps& caps, |
| 65 | const GrBackendFormat& format, |
| 66 | SkISize dimensions, |
| 67 | UsageFlags requiredUsage, |
| 68 | int sampleCnt, |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame^] | 69 | GrMipmapped mipmapped, |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 70 | GrProtected isProtected, |
| 71 | GrScratchKey* key) { |
| 72 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
| 73 | |
Greg Daniel | 5d0330e | 2020-10-12 16:05:21 -0400 | [diff] [blame] | 74 | GrScratchKey::Builder builder(key, kType, 5); |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame^] | 75 | build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected); |
Greg Daniel | c0d6915 | 2020-10-08 14:59:00 -0400 | [diff] [blame] | 76 | } |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 77 | |
| 78 | void GrAttachment::computeScratchKey(GrScratchKey* key) const { |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame^] | 79 | if (!SkToBool(fSupportedUsages & UsageFlags::kStencilAttachment)) { |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 80 | auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo; |
| 81 | ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), this->dimensions(), |
Greg Daniel | e0baf60 | 2021-03-02 16:12:55 -0500 | [diff] [blame^] | 82 | fSupportedUsages, this->numSamples(), this->mipmapped(), isProtected, |
| 83 | key); |
Greg Daniel | f5323d9 | 2020-10-13 16:42:08 -0400 | [diff] [blame] | 84 | } |
| 85 | } |