blob: 310078a352924ec18bf04ac35cf4133754dd0b66 [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,
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 Danielb8949bd2020-10-12 15:21:02 -040049void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps,
50 const GrBackendFormat& format,
51 SkISize dimensions,
Greg Danielc0d69152020-10-08 14:59:00 -040052 UsageFlags requiredUsage,
53 int sampleCnt,
Greg Danielb8949bd2020-10-12 15:21:02 -040054 GrProtected isProtected,
Greg Danielc0d69152020-10-08 14:59:00 -040055 GrUniqueKey* key) {
56 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
57
Greg Danielb8949bd2020-10-12 15:21:02 -040058 GrUniqueKey::Builder builder(key, kDomain, 5);
Greg Daniel5d0330e2020-10-12 16:05:21 -040059 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, isProtected);
60}
61
62void 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 Danielc0d69152020-10-08 14:59:00 -040075}
Greg Danielf5323d92020-10-13 16:42:08 -040076
77void 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}