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#include "GrGLRenderTarget.h"
9
10#include "GrGpuGL.h"
11
12#define GPUGL static_cast<GrGpuGL*>(getGpu())
13
14#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
15
16void GrGLRenderTarget::init(const Desc& desc,
17                            const GrGLIRect& viewport,
18                            GrGLTexID* texID) {
19    fRTFBOID                = desc.fRTFBOID;
20    fTexFBOID               = desc.fTexFBOID;
21    fMSColorRenderbufferID  = desc.fMSColorRenderbufferID;
22    fViewport               = viewport;
23    fTexIDObj.reset(SkSafeRef(texID));
24}
25
26namespace {
27GrTextureDesc MakeDesc(GrTextureFlags flags,
28                       int width, int height,
29                       GrPixelConfig config, int sampleCnt,
30                       GrSurfaceOrigin origin) {
31    GrTextureDesc temp;
32    temp.fFlags = flags;
33    temp.fWidth = width;
34    temp.fHeight = height;
35    temp.fConfig = config;
36    temp.fSampleCnt = sampleCnt;
37    temp.fOrigin = origin;
38    return temp;
39}
40
41};
42
43GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
44                                   const Desc& desc,
45                                   const GrGLIRect& viewport,
46                                   GrGLTexID* texID,
47                                   GrGLTexture* texture)
48    : INHERITED(gpu,
49                desc.fIsWrapped,
50                texture,
51                MakeDesc(kNone_GrTextureFlags,
52                         viewport.fWidth, viewport.fHeight,
53                         desc.fConfig, desc.fSampleCnt,
54                         desc.fOrigin)) {
55    SkASSERT(NULL != texID);
56    SkASSERT(NULL != texture);
57    // FBO 0 can't also be a texture, right?
58    SkASSERT(0 != desc.fRTFBOID);
59    SkASSERT(0 != desc.fTexFBOID);
60
61    // we assume this is true, TODO: get rid of viewport as a param.
62    SkASSERT(viewport.fWidth == texture->width());
63    SkASSERT(viewport.fHeight == texture->height());
64
65    this->init(desc, viewport, texID);
66}
67
68GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
69                                   const Desc& desc,
70                                   const GrGLIRect& viewport)
71    : INHERITED(gpu,
72                desc.fIsWrapped,
73                NULL,
74                MakeDesc(kNone_GrTextureFlags,
75                         viewport.fWidth, viewport.fHeight,
76                         desc.fConfig, desc.fSampleCnt,
77                         desc.fOrigin)) {
78    this->init(desc, viewport, NULL);
79}
80
81void GrGLRenderTarget::onRelease() {
82    GPUGL->notifyRenderTargetDelete(this);
83    if (!this->isWrapped()) {
84        if (fTexFBOID) {
85            GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
86        }
87        if (fRTFBOID && fRTFBOID != fTexFBOID) {
88            GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
89        }
90        if (fMSColorRenderbufferID) {
91            GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
92        }
93    }
94    fRTFBOID                = 0;
95    fTexFBOID               = 0;
96    fMSColorRenderbufferID  = 0;
97    fTexIDObj.reset(NULL);
98    INHERITED::onRelease();
99}
100
101void GrGLRenderTarget::onAbandon() {
102    fRTFBOID                = 0;
103    fTexFBOID               = 0;
104    fMSColorRenderbufferID  = 0;
105    if (NULL != fTexIDObj.get()) {
106        fTexIDObj->abandon();
107        fTexIDObj.reset(NULL);
108    }
109    INHERITED::onAbandon();
110}
111