GrRenderTarget.h revision 5236cf480daf82b2f36e42795abdbbc915533a59
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 GrStencilBuffer;
15
16/**
17 * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
18 * A context's render target is set by setRenderTarget(). Render targets are
19 * created by a createTexture with the kRenderTarget_SurfaceFlag flag.
20 * Additionally, GrContext provides methods for creating GrRenderTargets
21 * that wrap externally created render targets.
22 */
23class GrRenderTarget : virtual public GrSurface {
24public:
25    SK_DECLARE_INST_COUNT(GrRenderTarget)
26
27    // GrSurface overrides
28    GrRenderTarget* asRenderTarget() SK_OVERRIDE { return this; }
29    const GrRenderTarget* asRenderTarget() const  SK_OVERRIDE { return this; }
30
31    // GrRenderTarget
32    /**
33     * If this RT is multisampled, this is the multisample buffer
34     * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
35     */
36    virtual GrBackendObject getRenderTargetHandle() const = 0;
37
38    /**
39     * If this RT is multisampled, this is the buffer it is resolved to.
40     * Otherwise, same as getRenderTargetHandle().
41     * (In GL a separate FBO ID is used for the MSAA and resolved buffers)
42     * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
43     */
44    virtual GrBackendObject getRenderTargetResolvedHandle() const = 0;
45
46    /**
47     * @return true if the surface is multisampled, false otherwise
48     */
49    bool isMultisampled() const { return 0 != fDesc.fSampleCnt; }
50
51    /**
52     * @return the number of samples-per-pixel or zero if non-MSAA.
53     */
54    int numSamples() const { return fDesc.fSampleCnt; }
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    /**
90     * Provide a performance hint that the render target's contents are allowed
91     * to become undefined.
92     */
93    void discard();
94
95    // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
96    // 0 in GL), or be unresolvable because the client didn't give us the
97    // resolve destination.
98    enum ResolveType {
99        kCanResolve_ResolveType,
100        kAutoResolves_ResolveType,
101        kCantResolve_ResolveType,
102    };
103    virtual ResolveType getResolveType() const = 0;
104
105    /**
106     * GrStencilBuffer is not part of the public API.
107     */
108    GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; }
109    void setStencilBuffer(GrStencilBuffer* stencilBuffer);
110
111protected:
112    GrRenderTarget(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc)
113        : INHERITED(gpu, lifeCycle, desc)
114        , fStencilBuffer(NULL) {
115        fResolveRect.setLargestInverted();
116    }
117
118    // override of GrResource
119    void onAbandon() SK_OVERRIDE;
120    void onRelease() SK_OVERRIDE;
121
122private:
123    GrStencilBuffer*  fStencilBuffer;
124
125    SkIRect           fResolveRect;
126
127    typedef GrSurface INHERITED;
128};
129
130#endif
131