xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrAttachment.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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 #ifndef GrAttachment_DEFINED
9 #define GrAttachment_DEFINED
10 
11 #include "include/core/SkSize.h"
12 #include "include/gpu/ganesh/GrBackendSurface.h"
13 #include "include/gpu/ganesh/GrTypes.h"
14 #include "include/private/base/SkMacros.h"
15 #include "include/private/gpu/ganesh/GrTypesPriv.h"
16 #include "src/gpu/ganesh/GrSurface.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string_view>
21 
22 class GrCaps;
23 class GrGpu;
24 
25 namespace skgpu {
26 class ScratchKey;
27 class UniqueKey;
28 enum class Mipmapped : bool;
29 }  // namespace skgpu
30 
31 /**
32  * This is a generic attachment class for out GrSurfaces. It always represents a single gpu
33  * allocation. It contains usage flags so that we know what the attachment can be used for.
34  *
35  * TODO: Once we can pull out GrRenderTarget to be more of a framebuffer and break apart our
36  * texture render target diamond, we will merge this class with GrSurface. Until then this will
37  * act as the staging class for the new surface and framebuffer world.
38  */
39 class GrAttachment : public GrSurface {
40 public:
41     enum class UsageFlags : uint8_t {
42         kStencilAttachment = 0x1,
43         kColorAttachment   = 0x2,
44         kTexture           = 0x4,
45     };
46     SK_DECL_BITFIELD_CLASS_OPS_FRIENDS(UsageFlags);
47 
~GrAttachment()48     ~GrAttachment() override {}
49 
supportedUsages()50     UsageFlags supportedUsages() const { return fSupportedUsages; }
51 
numSamples()52     int numSamples() const { return fSampleCnt; }
53 
mipmapped()54     skgpu::Mipmapped mipmapped() const { return fMipmapped; }
55 
hasPerformedInitialClear()56     bool hasPerformedInitialClear() const { return fHasPerformedInitialClear; }
markHasPerformedInitialClear()57     void markHasPerformedInitialClear() { fHasPerformedInitialClear = true; }
58 
59     // This unique key is used for attachments of the same dimensions, usage, and sample cnt which
60     // are shared between multiple render targets at the same time. Only one usage flag may be
61     // passed in.
62     // TODO: Once attachments start having multiple usages, we'll need to figure out how to search
63     // the cache for an attachment that simply contains the requested usage instead of equaling it.
64     static void ComputeSharedAttachmentUniqueKey(const GrCaps& caps,
65                                                  const GrBackendFormat& format,
66                                                  SkISize dimensions,
67                                                  UsageFlags requiredUsage,
68                                                  int sampleCnt,
69                                                  skgpu::Mipmapped mipmapped,
70                                                  GrProtected isProtected,
71                                                  GrMemoryless memoryless,
72                                                  skgpu::UniqueKey* key);
73 
74     // TODO: Once attachments start having multiple usages, we'll need to figure out how to search
75     // the cache for an attachment that simply contains the requested usage instead of equaling it.
76     static void ComputeScratchKey(const GrCaps& caps,
77                                   const GrBackendFormat& format,
78                                   SkISize dimensions,
79                                   UsageFlags requiredUsage,
80                                   int sampleCnt,
81                                   skgpu::Mipmapped mipmapped,
82                                   GrProtected,
83                                   GrMemoryless,
84                                   skgpu::ScratchKey* key);
85 
86 protected:
87     GrAttachment(GrGpu* gpu,
88                  SkISize dimensions,
89                  UsageFlags supportedUsages,
90                  int sampleCnt,
91                  skgpu::Mipmapped mipmapped,
92                  GrProtected isProtected,
93                  std::string_view label,
94                  GrMemoryless memoryless = GrMemoryless::kNo)
INHERITED(gpu,dimensions,isProtected,label)95             : INHERITED(gpu, dimensions, isProtected, label)
96             , fSupportedUsages(supportedUsages)
97             , fSampleCnt(sampleCnt)
98             , fMipmapped(mipmapped)
99             , fMemoryless(memoryless) {}
100 
101 private:
102     size_t onGpuMemorySize() const final;
103 
onSetLabel()104     void onSetLabel() override{}
105 
106     void computeScratchKey(skgpu::ScratchKey*) const final;
107 
getResourceType()108     const char* getResourceType() const override {
109         if (fSupportedUsages == UsageFlags::kStencilAttachment) {
110             return "StencilAttachment";
111         }
112 
113         // This is a general grouping of all textures and color attachments.
114         return "Surface";
115     }
116 
117     UsageFlags fSupportedUsages;
118     int fSampleCnt;
119     skgpu::Mipmapped fMipmapped;
120     bool fHasPerformedInitialClear = false;
121     GrMemoryless fMemoryless;
122 
123     using INHERITED = GrSurface;
124 };
125 
126 SK_MAKE_BITFIELD_CLASS_OPS(GrAttachment::UsageFlags)
127 
128 #endif
129