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