blob: 6db517d909af798fb6afdae5eadaaf8a29c8f21c [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 {
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 Danielc0d69152020-10-08 14:59:00 -040025
Greg Daniel5d0330e2020-10-12 16:05:21 -040026static void build_key(GrResourceKey::Builder* builder,
27 const GrCaps& caps,
28 const GrBackendFormat& format,
29 SkISize dimensions,
30 GrAttachment::UsageFlags requiredUsage,
31 int sampleCnt,
Greg Daniele0baf602021-03-02 16:12:55 -050032 GrMipmapped mipmapped,
Greg Daniel5d0330e2020-10-12 16:05:21 -040033 GrProtected isProtected) {
34 SkASSERT(!dimensions.isEmpty());
35
36 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
37 SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8));
38 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 9)));
39
40 uint64_t formatKey = caps.computeFormatKey(format);
41 (*builder)[0] = dimensions.width();
42 (*builder)[1] = dimensions.height();
43 (*builder)[2] = formatKey & 0xFFFFFFFF;
44 (*builder)[3] = (formatKey >> 32) & 0xFFFFFFFF;
45 (*builder)[4] = (static_cast<uint32_t>(isProtected) << 0) |
46 (static_cast<uint32_t>(requiredUsage) << 1) |
47 (static_cast<uint32_t>(sampleCnt) << 9);
48}
49
Greg Danielb8949bd2020-10-12 15:21:02 -040050void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps,
51 const GrBackendFormat& format,
52 SkISize dimensions,
Greg Danielc0d69152020-10-08 14:59:00 -040053 UsageFlags requiredUsage,
54 int sampleCnt,
Greg Daniele0baf602021-03-02 16:12:55 -050055 GrMipmapped mipmapped,
Greg Danielb8949bd2020-10-12 15:21:02 -040056 GrProtected isProtected,
Greg Danielc0d69152020-10-08 14:59:00 -040057 GrUniqueKey* key) {
58 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
59
Greg Danielb8949bd2020-10-12 15:21:02 -040060 GrUniqueKey::Builder builder(key, kDomain, 5);
Greg Daniele0baf602021-03-02 16:12:55 -050061 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected);
Greg Daniel5d0330e2020-10-12 16:05:21 -040062}
63
64void GrAttachment::ComputeScratchKey(const GrCaps& caps,
65 const GrBackendFormat& format,
66 SkISize dimensions,
67 UsageFlags requiredUsage,
68 int sampleCnt,
Greg Daniele0baf602021-03-02 16:12:55 -050069 GrMipmapped mipmapped,
Greg Daniel5d0330e2020-10-12 16:05:21 -040070 GrProtected isProtected,
71 GrScratchKey* key) {
72 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
73
Greg Daniel5d0330e2020-10-12 16:05:21 -040074 GrScratchKey::Builder builder(key, kType, 5);
Greg Daniele0baf602021-03-02 16:12:55 -050075 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected);
Greg Danielc0d69152020-10-08 14:59:00 -040076}
Greg Danielf5323d92020-10-13 16:42:08 -040077
78void GrAttachment::computeScratchKey(GrScratchKey* key) const {
Greg Daniele0baf602021-03-02 16:12:55 -050079 if (!SkToBool(fSupportedUsages & UsageFlags::kStencilAttachment)) {
Greg Danielf5323d92020-10-13 16:42:08 -040080 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
81 ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), this->dimensions(),
Greg Daniele0baf602021-03-02 16:12:55 -050082 fSupportedUsages, this->numSamples(), this->mipmapped(), isProtected,
83 key);
Greg Danielf5323d92020-10-13 16:42:08 -040084 }
85}