blob: 04edf57c9d40640395df722d375ebe06c362cfd7 [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.
25 // TODO: Once we start using texture attachments this check really should be !texture. However,
26 // until then in GrVkTextureRenderTarget we make a wrapped attachment to use for the render
27 // target which duplicates the GrTexture. These will be merged once we use texture attachments.
28 if ((fSupportedUsages & UsageFlags::kStencilAttachment) ||
29 ((fSupportedUsages & UsageFlags::kColorAttachment) && fSampleCnt > 1)) {
30 GrBackendFormat format = this->backendFormat();
31 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
Greg Danielc89a7ee2020-10-12 16:50:18 -040032
Greg Daniel00d6cf42021-03-05 22:50:08 +000033 uint64_t size = GrNumBlocks(compression, this->dimensions());
34 size *= GrBackendFormatBytesPerBlock(this->backendFormat());
35 size *= this->numSamples();
36 return size;
37 }
38 return 0;
Greg Danielc89a7ee2020-10-12 16:50:18 -040039}
Greg Danielc0d69152020-10-08 14:59:00 -040040
Greg Daniel5d0330e2020-10-12 16:05:21 -040041static void build_key(GrResourceKey::Builder* builder,
42 const GrCaps& caps,
43 const GrBackendFormat& format,
44 SkISize dimensions,
45 GrAttachment::UsageFlags requiredUsage,
46 int sampleCnt,
Greg Daniele0baf602021-03-02 16:12:55 -050047 GrMipmapped mipmapped,
Greg Daniel5d0330e2020-10-12 16:05:21 -040048 GrProtected isProtected) {
49 SkASSERT(!dimensions.isEmpty());
50
51 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
52 SkASSERT(static_cast<uint32_t>(requiredUsage) < (1u << 8));
53 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1u << (32 - 9)));
54
55 uint64_t formatKey = caps.computeFormatKey(format);
56 (*builder)[0] = dimensions.width();
57 (*builder)[1] = dimensions.height();
58 (*builder)[2] = formatKey & 0xFFFFFFFF;
59 (*builder)[3] = (formatKey >> 32) & 0xFFFFFFFF;
60 (*builder)[4] = (static_cast<uint32_t>(isProtected) << 0) |
61 (static_cast<uint32_t>(requiredUsage) << 1) |
62 (static_cast<uint32_t>(sampleCnt) << 9);
63}
64
Greg Danielb8949bd2020-10-12 15:21:02 -040065void GrAttachment::ComputeSharedAttachmentUniqueKey(const GrCaps& caps,
66 const GrBackendFormat& format,
67 SkISize dimensions,
Greg Danielc0d69152020-10-08 14:59:00 -040068 UsageFlags requiredUsage,
69 int sampleCnt,
Greg Daniele0baf602021-03-02 16:12:55 -050070 GrMipmapped mipmapped,
Greg Danielb8949bd2020-10-12 15:21:02 -040071 GrProtected isProtected,
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 Daniele0baf602021-03-02 16:12:55 -050076 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected);
Greg Daniel5d0330e2020-10-12 16:05:21 -040077}
78
79void GrAttachment::ComputeScratchKey(const GrCaps& caps,
80 const GrBackendFormat& format,
81 SkISize dimensions,
82 UsageFlags requiredUsage,
83 int sampleCnt,
Greg Daniele0baf602021-03-02 16:12:55 -050084 GrMipmapped mipmapped,
Greg Daniel5d0330e2020-10-12 16:05:21 -040085 GrProtected isProtected,
86 GrScratchKey* key) {
87 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
88
Greg Daniel5d0330e2020-10-12 16:05:21 -040089 GrScratchKey::Builder builder(key, kType, 5);
Greg Daniele0baf602021-03-02 16:12:55 -050090 build_key(&builder, caps, format, dimensions, requiredUsage, sampleCnt, mipmapped, isProtected);
Greg Danielc0d69152020-10-08 14:59:00 -040091}
Greg Danielf5323d92020-10-13 16:42:08 -040092
93void GrAttachment::computeScratchKey(GrScratchKey* key) const {
Greg Daniele0baf602021-03-02 16:12:55 -050094 if (!SkToBool(fSupportedUsages & UsageFlags::kStencilAttachment)) {
Greg Danielf5323d92020-10-13 16:42:08 -040095 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
96 ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), this->dimensions(),
Greg Daniele0baf602021-03-02 16:12:55 -050097 fSupportedUsages, this->numSamples(), this->mipmapped(), isProtected,
98 key);
Greg Danielf5323d92020-10-13 16:42:08 -040099 }
100}