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 "src/gpu/GrBackendUtils.h"
11 #include "src/gpu/GrCaps.h"
12 #include "src/gpu/GrDataUtils.h"
13 #include "src/gpu/GrGpu.h"
14
onGpuMemorySize() const15 size_t GrAttachment::onGpuMemorySize() const {
16 // The GrTexture[RenderTarget] is built up by a bunch of attachments each of which are their
17 // own GrGpuResource. Ideally the GrRenderTarget would not be a GrGpuResource and the GrTexture
18 // would just merge with the new GrSurface/Attachment world. Then we could just depend on each
19 // attachment to give its own size since we don't have GrGpuResources owning other
20 // GrGpuResources. Until we get to that point we need to live in some hybrid world. We will let
21 // the msaa and stencil attachments track their own size because they do get cached separately.
22 // For all GrTexture* based things we will continue to to use the GrTexture* to report size and
23 // the owned attachments will have no size and be uncached.
24 if (!(fSupportedUsages & UsageFlags::kTexture) && fMemoryless == GrMemoryless::kNo) {
25 GrBackendFormat format = this->backendFormat();
26 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
27
28 uint64_t size = GrNumBlocks(compression, this->dimensions());
29 size *= GrBackendFormatBytesPerBlock(this->backendFormat());
30 size *= this->numSamples();
31 return size;
32 }
33 return 0;
34 }
35
build_key(skgpu::ResourceKey::Builder * builder,const GrCaps & caps,const GrBackendFormat & format,SkISize dimensions,GrAttachment::UsageFlags requiredUsage,int sampleCnt,GrMipmapped mipmapped,GrProtected isProtected,GrMemoryless memoryless)36 static void build_key(skgpu::ResourceKey::Builder* builder,
37 const GrCaps& caps,
38 const GrBackendFormat& format,
39 SkISize dimensions,
40 GrAttachment::UsageFlags requiredUsage,
41 int sampleCnt,
42 GrMipmapped mipmapped,
43 GrProtected isProtected,
44 GrMemoryless memoryless) {
45 SkASSERT(!dimensions.isEmpty());
46
47 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
48 SkASSERT(static_cast<uint32_t>(memoryless) <= 1);
49 SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8));
50 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 10)));
51
52 uint64_t formatKey = caps.computeFormatKey(format);
53 (*builder)[0] = dimensions.width();
54 (*builder)[1] = dimensions.height();
55 (*builder)[2] = formatKey & 0xFFFFFFFF;
56 (*builder)[3] = (formatKey >> 32) & 0xFFFFFFFF;
57 (*builder)[4] = (static_cast<uint32_t>(isProtected) << 0) |
58 (static_cast<uint32_t>(memoryless) << 1) |
59 (static_cast<uint32_t>(requiredUsage) << 2) |
60 (static_cast<uint32_t>(sampleCnt) << 10);
61 }
62
ComputeSharedAttachmentUniqueKey(const GrCaps & caps,const GrBackendFormat & format,SkISize dimensions,UsageFlags requiredUsage,int sampleCnt,GrMipmapped mipmapped,GrProtected isProtected,GrMemoryless memoryless,skgpu::UniqueKey * key)63 void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps,
64 const GrBackendFormat& format,
65 SkISize dimensions,
66 UsageFlags requiredUsage,
67 int sampleCnt,
68 GrMipmapped mipmapped,
69 GrProtected isProtected,
70 GrMemoryless memoryless,
71 skgpu::UniqueKey* key) {
72 static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain();
73
74 skgpu::UniqueKey::Builder builder(key, kDomain, 5);
75 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected,
76 memoryless);
77 }
78
ComputeScratchKey(const GrCaps & caps,const GrBackendFormat & format,SkISize dimensions,UsageFlags requiredUsage,int sampleCnt,GrMipmapped mipmapped,GrProtected isProtected,GrMemoryless memoryless,skgpu::ScratchKey * key)79 void GrAttachment::ComputeScratchKey(const GrCaps& caps,
80 const GrBackendFormat& format,
81 SkISize dimensions,
82 UsageFlags requiredUsage,
83 int sampleCnt,
84 GrMipmapped mipmapped,
85 GrProtected isProtected,
86 GrMemoryless memoryless,
87 skgpu::ScratchKey* key) {
88 static const skgpu::ScratchKey::ResourceType kType = skgpu::ScratchKey::GenerateResourceType();
89
90 skgpu::ScratchKey::Builder builder(key, kType, 5);
91 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected,
92 memoryless);
93 }
94
computeScratchKey(skgpu::ScratchKey * key) const95 void GrAttachment::computeScratchKey(skgpu::ScratchKey* key) const {
96 if (!SkToBool(fSupportedUsages & UsageFlags::kStencilAttachment)) {
97 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
98 ComputeScratchKey(*this->getGpu()->caps(),
99 this->backendFormat(),
100 this->dimensions(),
101 fSupportedUsages,
102 this->numSamples(),
103 this->mipmapped(),
104 isProtected,
105 fMemoryless,
106 key);
107 }
108 }
109