blob: 2966058991d5a5afe14c1b79363bb8c4d7c5b212 [file] [log] [blame]
Greg Danielc0d69152020-10-08 14:59:00 -04001/*
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 Danielc89a7ee2020-10-12 16:50:18 -040011#include "src/gpu/GrBackendUtils.h"
Greg Danielb8949bd2020-10-12 15:21:02 -040012#include "src/gpu/GrCaps.h"
Greg Danielc89a7ee2020-10-12 16:50:18 -040013#include "src/gpu/GrDataUtils.h"
Greg Danielf5323d92020-10-13 16:42:08 -040014#include "src/gpu/GrGpu.h"
Greg Danielc89a7ee2020-10-12 16:50:18 -040015
16size_t GrAttachment::onGpuMemorySize() const {
Greg Daniel00d6cf42021-03-05 22:50:08 +000017 // 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.
Greg Daniel77435592021-09-22 13:55:44 -040025 if (!(fSupportedUsages & UsageFlags::kTexture) && fMemoryless == GrMemoryless::kNo) {
Greg Daniel00d6cf42021-03-05 22:50:08 +000026 GrBackendFormat format = this->backendFormat();
27 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
Greg Danielc89a7ee2020-10-12 16:50:18 -040028
Greg Daniel00d6cf42021-03-05 22:50:08 +000029 uint64_t size = GrNumBlocks(compression, this->dimensions());
30 size *= GrBackendFormatBytesPerBlock(this->backendFormat());
31 size *= this->numSamples();
32 return size;
33 }
34 return 0;
Greg Danielc89a7ee2020-10-12 16:50:18 -040035}
Greg Danielc0d69152020-10-08 14:59:00 -040036
Greg Daniel5d0330e2020-10-12 16:05:21 -040037static void build_key(GrResourceKey::Builder* builder,
38 const GrCaps& caps,
39 const GrBackendFormat& format,
40 SkISize dimensions,
41 GrAttachment::UsageFlags requiredUsage,
42 int sampleCnt,
Greg Daniele0baf602021-03-02 16:12:55 -050043 GrMipmapped mipmapped,
Greg Daniel77435592021-09-22 13:55:44 -040044 GrProtected isProtected,
45 GrMemoryless memoryless) {
Greg Daniel5d0330e2020-10-12 16:05:21 -040046 SkASSERT(!dimensions.isEmpty());
47
48 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
Greg Daniel77435592021-09-22 13:55:44 -040049 SkASSERT(static_cast<uint32_t>(memoryless) <= 1);
Greg Daniel5d0330e2020-10-12 16:05:21 -040050 SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8));
Greg Daniel77435592021-09-22 13:55:44 -040051 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 10)));
Greg Daniel5d0330e2020-10-12 16:05:21 -040052
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) |
Greg Daniel77435592021-09-22 13:55:44 -040059 (static_cast<uint32_t>(memoryless) << 1) |
60 (static_cast<uint32_t>(requiredUsage) << 2) |
61 (static_cast<uint32_t>(sampleCnt) << 10);
Greg Daniel5d0330e2020-10-12 16:05:21 -040062}
63
Greg Danielb8949bd2020-10-12 15:21:02 -040064void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps,
65 const GrBackendFormat& format,
66 SkISize dimensions,
Greg Danielc0d69152020-10-08 14:59:00 -040067 UsageFlags requiredUsage,
68 int sampleCnt,
Greg Daniele0baf602021-03-02 16:12:55 -050069 GrMipmapped mipmapped,
Greg Danielb8949bd2020-10-12 15:21:02 -040070 GrProtected isProtected,
Greg Daniel77435592021-09-22 13:55:44 -040071 GrMemoryless memoryless,
Greg Danielc0d69152020-10-08 14:59:00 -040072 GrUniqueKey* key) {
73 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
74
Greg Danielb8949bd2020-10-12 15:21:02 -040075 GrUniqueKey::Builder builder(key, kDomain, 5);
Greg Daniel77435592021-09-22 13:55:44 -040076 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected,
77 memoryless);
Greg Daniel5d0330e2020-10-12 16:05:21 -040078}
79
80void GrAttachment::ComputeScratchKey(const GrCaps& caps,
81 const GrBackendFormat& format,
82 SkISize dimensions,
83 UsageFlags requiredUsage,
84 int sampleCnt,
Greg Daniele0baf602021-03-02 16:12:55 -050085 GrMipmapped mipmapped,
Greg Daniel5d0330e2020-10-12 16:05:21 -040086 GrProtected isProtected,
Greg Daniel77435592021-09-22 13:55:44 -040087 GrMemoryless memoryless,
Greg Daniel5d0330e2020-10-12 16:05:21 -040088 GrScratchKey* key) {
89 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
90
Greg Daniel5d0330e2020-10-12 16:05:21 -040091 GrScratchKey::Builder builder(key, kType, 5);
Greg Daniel77435592021-09-22 13:55:44 -040092 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected,
93 memoryless);
Greg Danielc0d69152020-10-08 14:59:00 -040094}
Greg Danielf5323d92020-10-13 16:42:08 -040095
96void GrAttachment::computeScratchKey(GrScratchKey* key) const {
Greg Daniele0baf602021-03-02 16:12:55 -050097 if (!SkToBool(fSupportedUsages & UsageFlags::kStencilAttachment)) {
Greg Danielf5323d92020-10-13 16:42:08 -040098 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
Greg Daniel77435592021-09-22 13:55:44 -040099 ComputeScratchKey(*this->getGpu()->caps(),
100 this->backendFormat(),
101 this->dimensions(),
102 fSupportedUsages,
103 this->numSamples(),
104 this->mipmapped(),
105 isProtected,
106 fMemoryless,
Greg Daniele0baf602021-03-02 16:12:55 -0500107 key);
Greg Danielf5323d92020-10-13 16:42:08 -0400108 }
109}