GrGLRenderTarget.cpp revision 1e0bf7e113f994a512a351cc2b25ddb1e742b7d4
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 "GrGLGpu.h"
11
12#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
13#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
14
15// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
16GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc)
17    : GrSurface(gpu, idDesc.fLifeCycle, desc)
18    , INHERITED(gpu, idDesc.fLifeCycle, desc) {
19    this->init(desc, idDesc);
20    this->registerWithCache();
21}
22
23GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
24                                   Derived)
25    : GrSurface(gpu, idDesc.fLifeCycle, desc)
26    , INHERITED(gpu, idDesc.fLifeCycle, desc) {
27    this->init(desc, idDesc);
28}
29
30void GrGLRenderTarget::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
31    fRTFBOID                = idDesc.fRTFBOID;
32    fTexFBOID               = idDesc.fTexFBOID;
33    fMSColorRenderbufferID  = idDesc.fMSColorRenderbufferID;
34    fIsWrapped              = kWrapped_LifeCycle == idDesc.fLifeCycle;
35
36    fViewport.fLeft   = 0;
37    fViewport.fBottom = 0;
38    fViewport.fWidth  = desc.fWidth;
39    fViewport.fHeight = desc.fHeight;
40
41    // We own one color value for each MSAA sample.
42    fColorValuesPerPixel = SkTMax(1, fDesc.fSampleCnt);
43    if (fTexFBOID != fRTFBOID) {
44        // If we own the resolve buffer then that is one more sample per pixel.
45        fColorValuesPerPixel += 1;
46    }
47}
48
49size_t GrGLRenderTarget::onGpuMemorySize() const {
50    SkASSERT(kUnknown_GrPixelConfig != fDesc.fConfig);
51    SkASSERT(!GrPixelConfigIsCompressed(fDesc.fConfig));
52    size_t colorBytes = GrBytesPerPixel(fDesc.fConfig);
53    SkASSERT(colorBytes > 0);
54    return fColorValuesPerPixel * fDesc.fWidth * fDesc.fHeight * colorBytes;
55}
56
57void GrGLRenderTarget::onRelease() {
58    if (!fIsWrapped) {
59        if (fTexFBOID) {
60            GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
61        }
62        if (fRTFBOID && fRTFBOID != fTexFBOID) {
63            GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
64        }
65        if (fMSColorRenderbufferID) {
66            GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
67        }
68    }
69    fRTFBOID                = 0;
70    fTexFBOID               = 0;
71    fMSColorRenderbufferID  = 0;
72    fIsWrapped              = false;
73    INHERITED::onRelease();
74}
75
76void GrGLRenderTarget::onAbandon() {
77    fRTFBOID                = 0;
78    fTexFBOID               = 0;
79    fMSColorRenderbufferID  = 0;
80    fIsWrapped              = false;
81    INHERITED::onAbandon();
82}
83