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"
11 #include "src/gpu/GrBackendUtils.h"
12 #include "src/gpu/GrCaps.h"
13 #include "src/gpu/GrDataUtils.h"
14 #include "src/gpu/GrGpu.h"
15
onGpuMemorySize() const16 size_t GrAttachment::onGpuMemorySize() const {
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 if (!(fSupportedUsages & UsageFlags::kTexture) && fMemoryless == GrMemoryless::kNo) {
26 GrBackendFormat format = this->backendFormat();
27 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
28
29 uint64_t size = GrNumBlocks(compression, this->dimensions());
30 size *= GrBackendFormatBytesPerBlock(this->backendFormat());
31 size *= this->numSamples();
32 return size;
33 }
34 return 0;
35 }
36
build_key(GrResourceKey::Builder * builder,const GrCaps & caps,const GrBackendFormat & format,SkISize dimensions,GrAttachment::UsageFlags requiredUsage,int sampleCnt,GrMipmapped mipmapped,GrProtected isProtected,GrMemoryless memoryless)37 static void build_key(GrResourceKey::Builder* builder,
38 const GrCaps& caps,
39 const GrBackendFormat& format,
40 SkISize dimensions,
41 GrAttachment::UsageFlags requiredUsage,
42 int sampleCnt,
43 GrMipmapped mipmapped,
44 GrProtected isProtected,
45 GrMemoryless memoryless) {
46 SkASSERT(!dimensions.isEmpty());
47
48 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
49 SkASSERT(static_cast<uint32_t>(memoryless) <= 1);
50 SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8));
51 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 10)));
52
53 uint64_t formatKey = caps.computeFormatKey(format);
54 (*builder)[0] = dimensions.width();
55 (*builder)[1] = dimensions.height();
56 (*builder)[2] = formatKey & 0xFFFFFFFF;
57 (*builder)[3] = (formatKey >> 32) & 0xFFFFFFFF;
58 (*builder)[4] = (static_cast<uint32_t>(isProtected) << 0) |
59 (static_cast<uint32_t>(memoryless) << 1) |
60 (static_cast<uint32_t>(requiredUsage) << 2) |
61 (static_cast<uint32_t>(sampleCnt) << 10);
62 }
63
ComputeSharedAttachmentUniqueKey(const GrCaps & caps,const GrBackendFormat & format,SkISize dimensions,UsageFlags requiredUsage,int sampleCnt,GrMipmapped mipmapped,GrProtected isProtected,GrMemoryless memoryless,GrUniqueKey * key)64 void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps,
65 const GrBackendFormat& format,
66 SkISize dimensions,
67 UsageFlags requiredUsage,
68 int sampleCnt,
69 GrMipmapped mipmapped,
70 GrProtected isProtected,
71 GrMemoryless memoryless,
72 GrUniqueKey* key) {
73 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
74
75 GrUniqueKey::Builder builder(key, kDomain, 5);
76 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected,
77 memoryless);
78 }
79
ComputeScratchKey(const GrCaps & caps,const GrBackendFormat & format,SkISize dimensions,UsageFlags requiredUsage,int sampleCnt,GrMipmapped mipmapped,GrProtected isProtected,GrMemoryless memoryless,GrScratchKey * key)80 void GrAttachment::ComputeScratchKey(const GrCaps& caps,
81 const GrBackendFormat& format,
82 SkISize dimensions,
83 UsageFlags requiredUsage,
84 int sampleCnt,
85 GrMipmapped mipmapped,
86 GrProtected isProtected,
87 GrMemoryless memoryless,
88 GrScratchKey* key) {
89 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
90
91 GrScratchKey::Builder builder(key, kType, 5);
92 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected,
93 memoryless);
94 }
95
computeScratchKey(GrScratchKey * key) const96 void GrAttachment::computeScratchKey(GrScratchKey* key) const {
97 if (!SkToBool(fSupportedUsages & UsageFlags::kStencilAttachment)) {
98 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
99 ComputeScratchKey(*this->getGpu()->caps(),
100 this->backendFormat(),
101 this->dimensions(),
102 fSupportedUsages,
103 this->numSamples(),
104 this->mipmapped(),
105 isProtected,
106 fMemoryless,
107 key);
108 }
109 }
110