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 "GrGLIRect.h"
13 #include "GrRenderTarget.h"
14 #include "SkScalar.h"
15 
16 class GrGLGpu;
17 class GrGLStencilAttachment;
18 
19 class GrGLRenderTarget : public GrRenderTarget {
20 public:
21     // set fTexFBOID to this value to indicate that it is multisampled but
22     // Gr doesn't know how to resolve it.
23     enum { kUnresolvableFBOID = 0 };
24 
25     struct IDDesc {
26         GrGLuint                     fRTFBOID;
27         GrGLuint                     fTexFBOID;
28         GrGLuint                     fMSColorRenderbufferID;
29         GrGpuResource::LifeCycle     fLifeCycle;
30         GrRenderTarget::SampleConfig fSampleConfig;
31     };
32 
33     static GrGLRenderTarget* CreateWrapped(GrGLGpu*,
34                                            const GrSurfaceDesc&,
35                                            const IDDesc&,
36                                            int stencilBits);
37 
setViewport(const GrGLIRect & rect)38     void setViewport(const GrGLIRect& rect) { fViewport = rect; }
getViewport()39     const GrGLIRect& getViewport() const { return fViewport; }
40 
41     // The following two functions return the same ID when a
42     // texture/render target is multisampled, and different IDs when
43     // it is.
44     // FBO ID used to render into
renderFBOID()45     GrGLuint renderFBOID() const { return fRTFBOID; }
46     // FBO ID that has texture ID attached.
textureFBOID()47     GrGLuint textureFBOID() const { return fTexFBOID; }
48 
49     // override of GrRenderTarget
getResolveType()50     ResolveType getResolveType() const override {
51         if (!this->isUnifiedMultisampled() ||
52             fRTFBOID == fTexFBOID) {
53             // catches FBO 0 and non MSAA case
54             return kAutoResolves_ResolveType;
55         } else if (kUnresolvableFBOID == fTexFBOID) {
56             return kCantResolve_ResolveType;
57         } else {
58             return kCanResolve_ResolveType;
59         }
60     }
61 
getRenderTargetHandle()62     GrBackendObject getRenderTargetHandle() const override { return fRTFBOID; }
63 
64     /** When we don't own the FBO ID we don't attempt to modify its attachments. */
canAttemptStencilAttachment()65     bool canAttemptStencilAttachment() const override {
66         return kCached_LifeCycle == fRTLifecycle || kUncached_LifeCycle == fRTLifecycle;
67     }
68 
69     // GrGLRenderTarget overrides dumpMemoryStatistics so it can log its texture and renderbuffer
70     // components seperately.
71     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const override;
72 
73 protected:
74     // The public constructor registers this object with the cache. However, only the most derived
75     // class should register with the cache. This constructor does not do the registration and
76     // rather moves that burden onto the derived class.
77     enum Derived { kDerived };
78     GrGLRenderTarget(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&, Derived);
79 
80     void init(const GrSurfaceDesc&, const IDDesc&);
81 
82     void onAbandon() override;
83     void onRelease() override;
84 
85     // In protected because subclass GrGLTextureRenderTarget calls this version.
86     size_t onGpuMemorySize() const override;
87 
88 private:
89     // This ctor is used only for creating wrapped render targets and is only called for the static
90     // create function CreateWrapped(...).
91     GrGLRenderTarget(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&, GrGLStencilAttachment*);
92 
93     GrGLGpu* getGLGpu() const;
94     bool completeStencilAttachment() override;
95 
96     // The total size of the resource (including all pixels) for a single sample.
97     size_t totalBytesPerSample() const;
98     int msaaSamples() const;
99     // The number total number of samples, including both MSAA and resolve texture samples.
100     int totalSamples() const;
101 
102     GrGLuint    fRTFBOID;
103     GrGLuint    fTexFBOID;
104     GrGLuint    fMSColorRenderbufferID;
105 
106     // We track this separately from GrGpuResource because this may be both a texture and a render
107     // target, and the texture may be wrapped while the render target is not.
108     LifeCycle   fRTLifecycle;
109 
110     // when we switch to this render target we want to set the viewport to
111     // only render to content area (as opposed to the whole allocation) and
112     // we want the rendering to be at top left (GL has origin in bottom left)
113     GrGLIRect   fViewport;
114 
115     // onGpuMemorySize() needs to know the VRAM footprint of the FBO(s). However, abandon and
116     // release zero out the IDs and the cache needs to know the size even after those actions.
117     size_t      fGpuMemorySize;
118 
119     typedef GrRenderTarget INHERITED;
120 };
121 
122 #endif
123