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