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