blob: 16d7f1b0b2d0149176208434b49b04cae325b474 [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001/*
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
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00008#include "GrGLRenderTarget.h"
9
10#include "GrGpuGL.h"
11
12#define GPUGL static_cast<GrGpuGL*>(getGpu())
13
bsalomon@google.com0b77d682011-08-19 13:28:54 +000014#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
15
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000016void GrGLRenderTarget::init(const Desc& desc,
17 const GrGLIRect& viewport,
18 GrGLTexID* texID) {
19 fRTFBOID = desc.fRTFBOID;
20 fTexFBOID = desc.fTexFBOID;
21 fMSColorRenderbufferID = desc.fMSColorRenderbufferID;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000022 fViewport = viewport;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000023 fOwnIDs = desc.fOwnIDs;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000024 fTexIDObj = texID;
25 GrSafeRef(fTexIDObj);
26}
27
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000028namespace {
29GrTextureDesc MakeDesc(GrTextureFlags flags,
30 int width, int height,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +000031 GrPixelConfig config, int sampleCnt) {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000032 GrTextureDesc temp;
33 temp.fFlags = flags;
34 temp.fWidth = width;
35 temp.fHeight = height;
36 temp.fConfig = config;
37 temp.fSampleCnt = sampleCnt;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000038 return temp;
39}
40
41};
42
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000043GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
44 const Desc& desc,
45 const GrGLIRect& viewport,
46 GrGLTexID* texID,
47 GrGLTexture* texture)
bsalomon@google.com0168afc2011-08-08 13:21:05 +000048 : INHERITED(gpu,
49 texture,
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000050 MakeDesc(kNone_GrTextureFlags,
51 viewport.fWidth, viewport.fHeight,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +000052 desc.fConfig, desc.fSampleCnt)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000053 GrAssert(NULL != texID);
54 GrAssert(NULL != texture);
55 // FBO 0 can't also be a texture, right?
56 GrAssert(0 != desc.fRTFBOID);
57 GrAssert(0 != desc.fTexFBOID);
bsalomon@google.com99621082011-11-15 16:47:16 +000058
59 // we assume this is true, TODO: get rid of viewport as a param.
60 GrAssert(viewport.fWidth == texture->width());
61 GrAssert(viewport.fHeight == texture->height());
62
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000063 this->init(desc, viewport, texID);
64}
65
66GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
67 const Desc& desc,
68 const GrGLIRect& viewport)
bsalomon@google.com0168afc2011-08-08 13:21:05 +000069 : INHERITED(gpu,
70 NULL,
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000071 MakeDesc(kNone_GrTextureFlags,
72 viewport.fWidth, viewport.fHeight,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +000073 desc.fConfig, desc.fSampleCnt)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000074 this->init(desc, viewport, NULL);
75}
76
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000077void GrGLRenderTarget::onRelease() {
78 GPUGL->notifyRenderTargetDelete(this);
79 if (fOwnIDs) {
80 if (fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000081 GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000082 }
83 if (fRTFBOID && fRTFBOID != fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000084 GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000085 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000086 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000087 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000088 }
89 }
90 fRTFBOID = 0;
91 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000092 fMSColorRenderbufferID = 0;
93 GrSafeUnref(fTexIDObj);
94 fTexIDObj = NULL;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000095 this->setStencilBuffer(NULL);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000096}
97
98void GrGLRenderTarget::onAbandon() {
99 fRTFBOID = 0;
100 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000101 fMSColorRenderbufferID = 0;
102 if (NULL != fTexIDObj) {
103 fTexIDObj->abandon();
104 fTexIDObj = NULL;
105 }
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000106 this->setStencilBuffer(NULL);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000107}
108