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