1/*
2 * Copyright 2016 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 GrRenderTargetProxy_DEFINED
9#define GrRenderTargetProxy_DEFINED
10
11#include "include/private/GrTypesPriv.h"
12#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrNativeRect.h"
14#include "src/gpu/GrSurfaceProxy.h"
15#include "src/gpu/GrSwizzle.h"
16
17class GrResourceProvider;
18class GrRenderTargetProxyPriv;
19
20// This class delays the acquisition of RenderTargets until they are actually
21// required
22// Beware: the uniqueID of the RenderTargetProxy will usually be different than
23// the uniqueID of the RenderTarget it represents!
24class GrRenderTargetProxy : virtual public GrSurfaceProxy {
25public:
26 GrRenderTargetProxy* asRenderTargetProxy() override { return this; }
27 const GrRenderTargetProxy* asRenderTargetProxy() const override { return this; }
28
29 // Actually instantiate the backing rendertarget, if necessary.
30 bool instantiate(GrResourceProvider*) override;
31
32 bool canUseMixedSamples(const GrCaps& caps) const {
33 return caps.mixedSamplesSupport() && !this->glRTFBOIDIs0() &&
34 caps.internalMultisampleCount(this->backendFormat()) > 0 &&
35 this->canChangeStencilAttachment();
36 }
37
38 /*
39 * Indicate that a draw to this proxy requires stencil, and how many stencil samples it needs.
40 * The number of stencil samples on this proxy will be equal to the largest sample count passed
41 * to this method.
42 */
43 void setNeedsStencil(int8_t numStencilSamples) {
44 SkASSERT(numStencilSamples >= fSampleCnt);
45 fNumStencilSamples = std::max(numStencilSamples, fNumStencilSamples);
46 }
47
48 /**
49 * Returns the number of stencil samples this proxy will use, or 0 if it does not use stencil.
50 */
51 int numStencilSamples() const { return fNumStencilSamples; }
52
53 /**
54 * Returns the number of samples/pixel in the color buffer (One if non-MSAA).
55 */
56 int numSamples() const { return fSampleCnt; }
57
58 int maxWindowRectangles(const GrCaps& caps) const;
59
60 bool wrapsVkSecondaryCB() const { return fWrapsVkSecondaryCB == WrapsVkSecondaryCB::kYes; }
61
62 void markMSAADirty(const SkIRect& dirtyRect, GrSurfaceOrigin origin) {
63 SkASSERT(SkIRect::MakeSize(this->dimensions()).contains(dirtyRect));
64 SkASSERT(this->requiresManualMSAAResolve());
65 auto nativeRect = GrNativeRect::MakeRelativeTo(
66 origin, this->backingStoreDimensions().height(), dirtyRect);
67 fMSAADirtyRect.join(nativeRect.asSkIRect());
68 }
69 void markMSAAResolved() {
70 SkASSERT(this->requiresManualMSAAResolve());
71 fMSAADirtyRect.setEmpty();
72 }
73 bool isMSAADirty() const {
74 SkASSERT(fMSAADirtyRect.isEmpty() || this->requiresManualMSAAResolve());
75 return this->requiresManualMSAAResolve() && !fMSAADirtyRect.isEmpty();
76 }
77 const SkIRect& msaaDirtyRect() const {
78 SkASSERT(this->requiresManualMSAAResolve());
79 return fMSAADirtyRect;
80 }
81
82 // TODO: move this to a priv class!
83 bool refsWrappedObjects() const;
84
85 // Provides access to special purpose functions.
86 GrRenderTargetProxyPriv rtPriv();
87 const GrRenderTargetProxyPriv rtPriv() const;
88
89protected:
90 friend class GrProxyProvider; // for ctors
91 friend class GrRenderTargetProxyPriv;
92
93 // Deferred version
94 GrRenderTargetProxy(const GrCaps&,
95 const GrBackendFormat&,
96 SkISize,
97 int sampleCount,
98 SkBackingFit,
99 SkBudgeted,
100 GrProtected,
101 GrInternalSurfaceFlags,
102 UseAllocator);
103
104 enum class WrapsVkSecondaryCB : bool { kNo = false, kYes = true };
105
106 // Lazy-callback version
107 // There are two main use cases for lazily-instantiated proxies:
108 // basic knowledge - width, height, config, samples, origin are known
109 // minimal knowledge - only config is known.
110 //
111 // The basic knowledge version is used for DDL where we know the type of proxy we are going to
112 // use, but we don't have access to the GPU yet to instantiate it.
113 //
114 // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not
115 // know the final size until flush time.
116 GrRenderTargetProxy(LazyInstantiateCallback&&,
117 const GrBackendFormat&,
118 SkISize,
119 int sampleCount,
120 SkBackingFit,
121 SkBudgeted,
122 GrProtected,
123 GrInternalSurfaceFlags,
124 UseAllocator,
125 WrapsVkSecondaryCB);
126
127 // Wrapped version
128 GrRenderTargetProxy(sk_sp<GrSurface>,
129 UseAllocator,
130 WrapsVkSecondaryCB = WrapsVkSecondaryCB::kNo);
131
132 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
133
134private:
135 void setGLRTFBOIDIs0() {
136 fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
137 }
138 bool glRTFBOIDIs0() const {
139 return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0;
140 }
141 bool canChangeStencilAttachment() const;
142
143 size_t onUninstantiatedGpuMemorySize(const GrCaps&) const override;
144 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;)
145
146 // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings
147 // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes
148 // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for
149 // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this'
150 // in the constructors, and always looks for the full 16 byte alignment, even if the fields in
151 // that particular class don't require it. Changing the size of this object can move the start
152 // address of other types, leading to this problem.
153 int8_t fSampleCnt;
154 int8_t fNumStencilSamples = 0;
155 WrapsVkSecondaryCB fWrapsVkSecondaryCB;
156 SkIRect fMSAADirtyRect = SkIRect::MakeEmpty();
157 // This is to fix issue in large comment above. Without the padding we can end up with the
158 // GrTextureProxy starting 8 byte aligned by not 16. This happens when the RT ends at bytes 1-8.
159 // Note: with the virtual inheritance an 8 byte pointer is at the start of GrRenderTargetProxy.
160 //
161 // In the current world we end the RT proxy at 12 bytes. Technically any padding between 0-4
162 // will work, but we use 4 to be more explicit about getting it to 16 byte alignment.
163 char fDummyPadding[4];
164
165 typedef GrSurfaceProxy INHERITED;
166};
167
168#endif
169