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 
9 #ifndef GrGLRenderTarget_DEFINED
10 #define GrGLRenderTarget_DEFINED
11 
12 #include "GrBackendSurface.h"
13 #include "GrGLIRect.h"
14 #include "GrRenderTarget.h"
15 #include "SkScalar.h"
16 
17 class GrGLCaps;
18 class GrGLGpu;
19 class GrGLStencilAttachment;
20 
21 class GrGLRenderTarget : public GrRenderTarget {
22 public:
23     bool alwaysClearStencil() const override { return 0 == fRTFBOID; }
24 
25     // set fTexFBOID to this value to indicate that it is multisampled but
26     // Gr doesn't know how to resolve it.
27     enum { kUnresolvableFBOID = 0 };
28 
29     struct IDDesc {
30         GrGLuint                   fRTFBOID;
31         GrBackendObjectOwnership   fRTFBOOwnership;
32         GrGLuint                   fTexFBOID;
33         GrGLuint                   fMSColorRenderbufferID;
34         bool                       fIsMixedSampled;
35     };
36 
37     static sk_sp<GrGLRenderTarget> MakeWrapped(GrGLGpu*,
38                                                const GrSurfaceDesc&,
39                                                const IDDesc&,
40                                                int stencilBits);
41 
42     void setViewport(const GrGLIRect& rect) { fViewport = rect; }
43     const GrGLIRect& getViewport() const { return fViewport; }
44 
45     // The following two functions return the same ID when a texture/render target is not
46     // multisampled, and different IDs when it is multisampled.
47     // FBO ID used to render into
48     GrGLuint renderFBOID() const { return fRTFBOID; }
49     // FBO ID that has texture ID attached.
50     GrGLuint textureFBOID() const { return fTexFBOID; }
51 
52     // override of GrRenderTarget
53     ResolveType getResolveType() const override {
54         if (GrFSAAType::kUnifiedMSAA != this->fsaaType() || fRTFBOID == fTexFBOID) {
55             // catches FBO 0 and non unified-MSAA case
56             return kAutoResolves_ResolveType;
57         } else if (kUnresolvableFBOID == fTexFBOID) {
58             return kCantResolve_ResolveType;
59         } else {
60             return kCanResolve_ResolveType;
61         }
62     }
63 
64     GrBackendObject getRenderTargetHandle() const override { return fRTFBOID; }
65 
66     GrBackendRenderTarget getBackendRenderTarget() const override;
67 
68     bool canAttemptStencilAttachment() const override;
69 
70     // GrGLRenderTarget overrides dumpMemoryStatistics so it can log its texture and renderbuffer
71     // components seperately.
72     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const override;
73 
74 protected:
75     // Constructor for subclasses.
76     GrGLRenderTarget(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&);
77 
78     void init(const GrSurfaceDesc&, const IDDesc&);
79 
80     void onAbandon() override;
81     void onRelease() override;
82 
83     int numSamplesOwnedPerPixel() const { return fNumSamplesOwnedPerPixel; }
84 
85 private:
86     // Constructor for instances wrapping backend objects.
87     GrGLRenderTarget(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&, GrGLStencilAttachment*);
88 
89     static GrRenderTargetFlags ComputeFlags(const GrGLCaps&, const IDDesc&);
90 
91     GrGLGpu* getGLGpu() const;
92     bool completeStencilAttachment() override;
93 
94     size_t onGpuMemorySize() const override;
95 
96     int msaaSamples() const;
97     // The number total number of samples, including both MSAA and resolve texture samples.
98     int totalSamples() const;
99 
100     GrGLuint    fRTFBOID;
101     GrGLuint    fTexFBOID;
102     GrGLuint    fMSColorRenderbufferID;
103 
104     GrBackendObjectOwnership fRTFBOOwnership;
105 
106     // when we switch to this render target we want to set the viewport to
107     // only render to content area (as opposed to the whole allocation) and
108     // we want the rendering to be at top left (GL has origin in bottom left)
109     GrGLIRect   fViewport;
110 
111     // The RenderTarget needs to be able to report its VRAM footprint even after abandon and
112     // release have potentially zeroed out the IDs (e.g., so the cache can reset itself). Since
113     // the IDs are just required for the computation in totalSamples we cache that result here.
114     int         fNumSamplesOwnedPerPixel;
115 
116     typedef GrRenderTarget INHERITED;
117 };
118 
119 #endif
120