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