blob: 562526a5f01134750211577f2b34b25348b9c3a8 [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"
14
15size_t GrAttachment::onGpuMemorySize() const {
16 GrBackendFormat format = this->backendFormat();
17 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
18
19 uint64_t size = GrNumBlocks(compression, this->dimensions());
20 size *= GrBackendFormatBytesPerBlock(this->backendFormat());
21 size *= this->numSamples();
22 return size;
23}
Greg Danielc0d69152020-10-08 14:59:00 -040024
Greg Daniel5d0330e2020-10-12 16:05:21 -040025static void build_key(GrResourceKey::Builder* builder,
26 const GrCaps& caps,
27 const GrBackendFormat& format,
28 SkISize dimensions,
29 GrAttachment::UsageFlags requiredUsage,
30 int sampleCnt,
31 GrProtected isProtected) {
32 SkASSERT(!dimensions.isEmpty());
33
34 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
35 SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8));
36 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 9)));
37
38 uint64_t formatKey = caps.computeFormatKey(format);
39 (*builder)[0] = dimensions.width();
40 (*builder)[1] = dimensions.height();
41 (*builder)[2] = formatKey & 0xFFFFFFFF;
42 (*builder)[3] = (formatKey >> 32) & 0xFFFFFFFF;
43 (*builder)[4] = (static_cast<uint32_t>(isProtected) << 0) |
44 (static_cast<uint32_t>(requiredUsage) << 1) |
45 (static_cast<uint32_t>(sampleCnt) << 9);
46}
47
Greg Danielb8949bd2020-10-12 15:21:02 -040048void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps,
49 const GrBackendFormat& format,
50 SkISize dimensions,
Greg Danielc0d69152020-10-08 14:59:00 -040051 UsageFlags requiredUsage,
52 int sampleCnt,
Greg Danielb8949bd2020-10-12 15:21:02 -040053 GrProtected isProtected,
Greg Danielc0d69152020-10-08 14:59:00 -040054 GrUniqueKey* key) {
55 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
56
Greg Danielb8949bd2020-10-12 15:21:02 -040057 GrUniqueKey::Builder builder(key, kDomain, 5);
Greg Daniel5d0330e2020-10-12 16:05:21 -040058 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, isProtected);
59}
60
61void GrAttachment::ComputeScratchKey(const GrCaps& caps,
62 const GrBackendFormat& format,
63 SkISize dimensions,
64 UsageFlags requiredUsage,
65 int sampleCnt,
66 GrProtected isProtected,
67 GrScratchKey* key) {
68 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
69
70 SkASSERT(sampleCnt > 1);
71
72 GrScratchKey::Builder builder(key, kType, 5);
73 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, isProtected);
Greg Danielc0d69152020-10-08 14:59:00 -040074}