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 GrRenderTarget_DEFINED
9 #define GrRenderTarget_DEFINED
10 
11 #include "GrSurface.h"
12 #include "SkRect.h"
13 
14 class GrCaps;
15 class GrRenderTargetOpList;
16 class GrRenderTargetPriv;
17 class GrStencilAttachment;
18 class GrBackendRenderTarget;
19 
20 /**
21  * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
22  * A context's render target is set by setRenderTarget(). Render targets are
23  * created by a createTexture with the kRenderTarget_SurfaceFlag flag.
24  * Additionally, GrContext provides methods for creating GrRenderTargets
25  * that wrap externally created render targets.
26  */
27 class GrRenderTarget : virtual public GrSurface {
28 public:
alwaysClearStencil()29     virtual bool alwaysClearStencil() const { return false; }
30 
31     // GrSurface overrides
asRenderTarget()32     GrRenderTarget* asRenderTarget() override { return this; }
asRenderTarget()33     const GrRenderTarget* asRenderTarget() const  override { return this; }
34 
35     // GrRenderTarget
isStencilBufferMultisampled()36     bool isStencilBufferMultisampled() const { return fSampleCnt > 1; }
37 
fsaaType()38     GrFSAAType fsaaType() const {
39         SkASSERT(fSampleCnt >= 1);
40         if (fSampleCnt <= 1) {
41             SkASSERT(!this->hasMixedSamples());
42             return GrFSAAType::kNone;
43         }
44         return this->hasMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA;
45     }
46 
47     /**
48      * Returns the number of samples/pixel in the stencil buffer (One if non-MSAA).
49      */
numStencilSamples()50     int numStencilSamples() const { return fSampleCnt; }
51 
52     /**
53      * Returns the number of samples/pixel in the color buffer (One if non-MSAA or mixed sampled).
54      */
numColorSamples()55     int numColorSamples() const {
56         return GrFSAAType::kMixedSamples == this->fsaaType() ? 1 : fSampleCnt;
57     }
58 
59     /**
60      * Call to indicate the multisample contents were modified such that the
61      * render target needs to be resolved before it can be used as texture. Gr
62      * tracks this for its own drawing and thus this only needs to be called
63      * when the render target has been modified outside of Gr. This has no
64      * effect on wrapped backend render targets.
65      *
66      * @param rect  a rect bounding the area needing resolve. NULL indicates
67      *              the whole RT needs resolving.
68      */
69     void flagAsNeedingResolve(const SkIRect* rect = nullptr);
70 
71     /**
72      * Call to override the region that needs to be resolved.
73      */
74     void overrideResolveRect(const SkIRect rect);
75 
76     /**
77      * Call to indicate that GrRenderTarget was externally resolved. This may
78      * allow Gr to skip a redundant resolve step.
79      */
80     void flagAsResolved();
81 
82     /**
83      * @return true if the GrRenderTarget requires MSAA resolving
84      */
needsResolve()85     bool needsResolve() const { return !fResolveRect.isEmpty(); }
86 
87     /**
88      * Returns a rect bounding the region needing resolving.
89      */
getResolveRect()90     const SkIRect& getResolveRect() const { return fResolveRect; }
91 
92     // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
93     // 0 in GL), or be unresolvable because the client didn't give us the
94     // resolve destination.
95     enum ResolveType {
96         kCanResolve_ResolveType,
97         kAutoResolves_ResolveType,
98         kCantResolve_ResolveType,
99     };
100     virtual ResolveType getResolveType() const = 0;
101 
102     virtual GrBackendRenderTarget getBackendRenderTarget() const = 0;
103 
104     // Checked when this object is asked to attach a stencil buffer.
105     virtual bool canAttemptStencilAttachment() const = 0;
106 
107     // Provides access to functions that aren't part of the public API.
108     GrRenderTargetPriv renderTargetPriv();
109     const GrRenderTargetPriv renderTargetPriv() const;
110 
111 protected:
112     GrRenderTarget(GrGpu*, const GrSurfaceDesc&, GrStencilAttachment* = nullptr);
113     ~GrRenderTarget() override;
114 
115     // override of GrResource
116     void onAbandon() override;
117     void onRelease() override;
118 
119 private:
120     // Allows the backends to perform any additional work that is required for attaching a
121     // GrStencilAttachment. When this is called, the GrStencilAttachment has already been put onto
122     // the GrRenderTarget. This function must return false if any failures occur when completing the
123     // stencil attachment.
124     virtual bool completeStencilAttachment() = 0;
125 
126     friend class GrRenderTargetPriv;
127 
128     int                  fSampleCnt;
129     sk_sp<GrStencilAttachment> fStencilAttachment;
130 
131     SkIRect              fResolveRect;
132 
133     typedef GrSurface INHERITED;
134 };
135 
136 #endif
137