GrGLRenderTarget.h revision ed8659b51d9f2bad3f004df6033d72cc32d71c0d
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             fIsWrapped;
32        GrPixelConfig    fConfig;
33        int              fSampleCnt;
34        GrSurfaceOrigin  fOrigin;
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/render target 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 GrBackendObject getRenderTargetHandle() const {
64        return this->renderFBOID();
65    }
66    virtual GrBackendObject 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() SK_OVERRIDE;
85    virtual void onRelease() SK_OVERRIDE;
86
87private:
88    GrGLuint      fRTFBOID;
89    GrGLuint      fTexFBOID;
90
91    GrGLuint      fMSColorRenderbufferID;
92
93    // when we switch to this render target we want to set the viewport to
94    // only render to to content area (as opposed to the whole allocation) and
95    // we want the rendering to be at top left (GL has origin in bottom left)
96    GrGLIRect fViewport;
97
98    // non-NULL if this RT was created by Gr with an associated GrGLTexture.
99    GrGLTexID* fTexIDObj;
100
101    void init(const Desc& desc, const GrGLIRect& viewport, GrGLTexID* texID);
102
103    typedef GrRenderTarget INHERITED;
104};
105
106#endif
107