GrGLRenderTarget.h revision 363e546ed626b6dbbc42f5db87b3594bc0b5944b
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
16class GrGpuGL;
17class GrGLTexture;
18class GrGLTexID;
19
20class GrGLRenderTarget : public GrRenderTarget {
21
22public:
23    // set fTexFBOID to this value to indicate that it is multisampled but
24    // Gr doesn't know how to resolve it.
25    enum { kUnresolvableFBOID = 0 };
26
27    struct Desc {
28        GrGLuint      fRTFBOID;
29        GrGLuint      fTexFBOID;
30        GrGLuint      fMSColorRenderbufferID;
31        bool          fOwnIDs;
32        GrPixelConfig fConfig;
33        int           fSampleCnt;
34    };
35
36    // creates a GrGLRenderTarget associated with a texture
37    GrGLRenderTarget(GrGpuGL*          gpu,
38                     const Desc&       desc,
39                     const GrGLIRect&  viewport,
40                     GrGLTexID*        texID,
41                     GrGLTexture*      texture);
42
43    // creates an independent GrGLRenderTarget
44    GrGLRenderTarget(GrGpuGL*          gpu,
45                     const Desc&       desc,
46                     const GrGLIRect&  viewport);
47
48    virtual ~GrGLRenderTarget() { this->release(); }
49
50    void setViewport(const GrGLIRect& rect) { fViewport = rect; }
51    const GrGLIRect& getViewport() const { return fViewport; }
52
53    // The following two functions return the same ID when a
54    // texture/render target is multisampled, and different IDs when
55    // it is.
56    // FBO ID used to render into
57    GrGLuint renderFBOID() const { return fRTFBOID; }
58    // FBO ID that has texture ID attached.
59    GrGLuint textureFBOID() const { return fTexFBOID; }
60
61    // override of GrRenderTarget
62    virtual GrBackendObject getRenderTargetHandle() const {
63        return this->renderFBOID();
64    }
65    virtual GrBackendObject getRenderTargetResolvedHandle() const {
66        return this->textureFBOID();
67    }
68    virtual ResolveType getResolveType() const {
69
70        if (!this->isMultisampled() ||
71            fRTFBOID == fTexFBOID) {
72            // catches FBO 0 and non MSAA case
73            return kAutoResolves_ResolveType;
74        } else if (kUnresolvableFBOID == fTexFBOID) {
75            return kCantResolve_ResolveType;
76        } else {
77            return kCanResolve_ResolveType;
78        }
79    }
80
81protected:
82    // override of GrResource
83    virtual void onAbandon() SK_OVERRIDE;
84    virtual void onRelease() SK_OVERRIDE;
85
86private:
87    GrGLuint      fRTFBOID;
88    GrGLuint      fTexFBOID;
89
90    GrGLuint      fMSColorRenderbufferID;
91
92    // Should this object delete IDs when it is destroyed or does someone
93    // else own them.
94    bool        fOwnIDs;
95
96    // when we switch to this render target we want to set the viewport to
97    // only render to to content area (as opposed to the whole allocation) and
98    // we want the rendering to be at top left (GL has origin in bottom left)
99    GrGLIRect fViewport;
100
101    // non-NULL if this RT was created by Gr with an associated GrGLTexture.
102    GrGLTexID* fTexIDObj;
103
104    void init(const Desc& desc, const GrGLIRect& viewport, GrGLTexID* texID);
105
106    typedef GrRenderTarget INHERITED;
107};
108
109#endif
110