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