| 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 |
|
| 48 |
~GrAttachment() override {} |
| 49 |
|
| 50 |
UsageFlags supportedUsages() const { return fSupportedUsages; } |
| 51 |
|
| 52 |
int numSamples() const { return fSampleCnt; } |
| 53 |
|
| 54 |
skgpu::Mipmapped mipmapped() const { return fMipmapped; } |
| 55 |
|
| •56 |
SkIRect clearedArea() const { return fClearedArea; } |
| 57 |
bool hasAreaBeenCleared(SkIRect attachmentArea) const { |
| 58 |
return fClearedArea.contains(attachmentArea); |
| 59 |
} |
| 60 |
void markAreaCleared(SkIRect area) { fClearedArea = area; } |
| 61 |
|
| 62 |
// This unique key is used for attachments of the same dimensions, usage, and sample cnt which |
| 63 |
// are shared between multiple render targets at the same time. Only one usage flag may be |
| 64 |
// passed in. |
| 65 |
// TODO: Once attachments start having multiple usages, we'll need to figure out how to search |
| 66 |
// the cache for an attachment that simply contains the requested usage instead of equaling it. |
| 67 |
static void ComputeSharedAttachmentUniqueKey(const GrCaps& caps, |
| 68 |
const GrBackendFormat& format, |
| 69 |
SkISize dimensions, |
| 70 |
UsageFlags requiredUsage, |
| 71 |
int sampleCnt, |
| 72 |
skgpu::Mipmapped mipmapped, |
| 73 |
GrProtected isProtected, |
| 74 |
GrMemoryless memoryless, |
| 75 |
skgpu::UniqueKey* key); |
| 76 |
|
| 77 |
// TODO: Once attachments start having multiple usages, we'll need to figure out how to search |
| 78 |
// the cache for an attachment that simply contains the requested usage instead of equaling it. |
| 79 |
static void ComputeScratchKey(const GrCaps& caps, |
| 80 |
const GrBackendFormat& format, |
| 81 |
SkISize dimensions, |
| 82 |
UsageFlags requiredUsage, |
| 83 |
int sampleCnt, |
| 84 |
skgpu::Mipmapped mipmapped, |
| 85 |
GrProtected, |
| 86 |
GrMemoryless, |
| 87 |
skgpu::ScratchKey* key); |
| 88 |
|
| 89 |
protected: |
| 90 |
GrAttachment(GrGpu* gpu, |
| 91 |
SkISize dimensions, |
| 92 |
UsageFlags supportedUsages, |
| 93 |
int sampleCnt, |
| 94 |
skgpu::Mipmapped mipmapped, |
| 95 |
GrProtected isProtected, |
| 96 |
std::string_view label, |
| 97 |
GrMemoryless memoryless = GrMemoryless::kNo) |
| 98 |
: INHERITED(gpu, dimensions, isProtected, label) |
| 99 |
, fSupportedUsages(supportedUsages) |
| 100 |
, fSampleCnt(sampleCnt) |
| 101 |
, fMipmapped(mipmapped) |
| 102 |
, fMemoryless(memoryless) {} |
| 103 |
|
| 104 |
private: |
| 105 |
size_t onGpuMemorySize() const final; |
| 106 |
|
| 107 |
void onSetLabel() override{} |
| 108 |
|
| 109 |
void computeScratchKey(skgpu::ScratchKey*) const final; |
| 110 |
|
| 111 |
const char* getResourceType() const override { |
| 112 |
if (fSupportedUsages == UsageFlags::kStencilAttachment) { |
| 113 |
return "StencilAttachment"; |
| 114 |
} |
| 115 |
|
| 116 |
// This is a general grouping of all textures and color attachments. |
| 117 |
return "Surface"; |
| 118 |
} |
| 119 |
|
| 120 |
UsageFlags fSupportedUsages; |
| 121 |
int fSampleCnt; |
| 122 |
skgpu::Mipmapped fMipmapped; |
| •123 |
// Track which area of the attachment has already been cleared to cut down on unnecessary clear |
| 124 |
// operations, which can be more expensive on desktop GPUs than loads. |
| 125 |
SkIRect fClearedArea = SkIRect::MakeEmpty(); |
| 126 |
GrMemoryless fMemoryless; |
| 127 |
|
| 128 |
using INHERITED = GrSurface; |
| 129 |
}; |
| 130 |
|
| 131 |
SK_MAKE_BITFIELD_CLASS_OPS(GrAttachment::UsageFlags) |
| 132 |
|
| 133 |
#endif |