blob: 4193519357e67777c6857bdf2edc989c07ab7184 [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,
31 GrPixelConfig config, int sampleCnt,
32 uint64_t clientCacheID) {
33 GrTextureDesc temp;
34 temp.fFlags = flags;
35 temp.fWidth = width;
36 temp.fHeight = height;
37 temp.fConfig = config;
38 temp.fSampleCnt = sampleCnt;
39 temp.fClientCacheID = clientCacheID;
40 return temp;
41}
42
43};
44
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000045GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
46 const Desc& desc,
47 const GrGLIRect& viewport,
48 GrGLTexID* texID,
49 GrGLTexture* texture)
bsalomon@google.com0168afc2011-08-08 13:21:05 +000050 : INHERITED(gpu,
51 texture,
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000052 MakeDesc(kNone_GrTextureFlags,
53 viewport.fWidth, viewport.fHeight,
54 desc.fConfig, desc.fSampleCnt,
55 kDefault_CacheID)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000056 GrAssert(NULL != texID);
57 GrAssert(NULL != texture);
58 // FBO 0 can't also be a texture, right?
59 GrAssert(0 != desc.fRTFBOID);
60 GrAssert(0 != desc.fTexFBOID);
bsalomon@google.com99621082011-11-15 16:47:16 +000061
62 // we assume this is true, TODO: get rid of viewport as a param.
63 GrAssert(viewport.fWidth == texture->width());
64 GrAssert(viewport.fHeight == texture->height());
65
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000066 this->init(desc, viewport, texID);
67}
68
69GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
70 const Desc& desc,
71 const GrGLIRect& viewport)
bsalomon@google.com0168afc2011-08-08 13:21:05 +000072 : INHERITED(gpu,
73 NULL,
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000074 MakeDesc(kNone_GrTextureFlags,
75 viewport.fWidth, viewport.fHeight,
76 desc.fConfig, desc.fSampleCnt,
77 kDefault_CacheID)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000078 this->init(desc, viewport, NULL);
79}
80
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000081void GrGLRenderTarget::onRelease() {
82 GPUGL->notifyRenderTargetDelete(this);
83 if (fOwnIDs) {
84 if (fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000085 GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000086 }
87 if (fRTFBOID && fRTFBOID != fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000088 GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000089 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000090 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000091 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000092 }
93 }
94 fRTFBOID = 0;
95 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000096 fMSColorRenderbufferID = 0;
97 GrSafeUnref(fTexIDObj);
98 fTexIDObj = NULL;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000099 this->setStencilBuffer(NULL);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000100}
101
102void GrGLRenderTarget::onAbandon() {
103 fRTFBOID = 0;
104 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000105 fMSColorRenderbufferID = 0;
106 if (NULL != fTexIDObj) {
107 fTexIDObj->abandon();
108 fTexIDObj = NULL;
109 }
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000110 this->setStencilBuffer(NULL);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000111}
112