1aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com/*
2aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Copyright 2011 Google Inc.
3aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com *
4aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Use of this source code is governed by a BSD-style license that can be
5aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * found in the LICENSE file.
6aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */
7aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
8aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrGLRenderTarget.h"
9aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
10aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrGpuGL.h"
11aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
12aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#define GPUGL static_cast<GrGpuGL*>(getGpu())
13aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
140b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
150b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com
165bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.comvoid GrGLRenderTarget::init(const Desc& desc,
175bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com                            const GrGLIRect& viewport,
185bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com                            GrGLTexID* texID) {
195bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com    fRTFBOID                = desc.fRTFBOID;
205bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com    fTexFBOID               = desc.fTexFBOID;
215bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com    fMSColorRenderbufferID  = desc.fMSColorRenderbufferID;
22aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    fViewport               = viewport;
2359e16e4e1d30eee3ce564e0b513d952579065cb3commit-bot@chromium.org    fTexIDObj.reset(SkSafeRef(texID));
24aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com}
25aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
267d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comnamespace {
277d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comGrTextureDesc MakeDesc(GrTextureFlags flags,
287d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                       int width, int height,
293cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org                       GrPixelConfig config, int sampleCnt,
303cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org                       GrSurfaceOrigin origin) {
317d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    GrTextureDesc temp;
327d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    temp.fFlags = flags;
337d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    temp.fWidth = width;
347d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    temp.fHeight = height;
357d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    temp.fConfig = config;
367d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    temp.fSampleCnt = sampleCnt;
373cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org    temp.fOrigin = origin;
387d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    return temp;
397d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com}
407d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
417d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com};
427d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
435bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.comGrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
445bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com                                   const Desc& desc,
455bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com                                   const GrGLIRect& viewport,
465bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com                                   GrGLTexID* texID,
475bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com                                   GrGLTexture* texture)
480168afc7196fe82da8af230c93c56f3035d54d2bbsalomon@google.com    : INHERITED(gpu,
49728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com                desc.fIsWrapped,
500168afc7196fe82da8af230c93c56f3035d54d2bbsalomon@google.com                texture,
517d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                MakeDesc(kNone_GrTextureFlags,
527d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                         viewport.fWidth, viewport.fHeight,
533cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org                         desc.fConfig, desc.fSampleCnt,
543cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org                         desc.fOrigin)) {
55f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    SkASSERT(NULL != texID);
56f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    SkASSERT(NULL != texture);
575bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com    // FBO 0 can't also be a texture, right?
58f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    SkASSERT(0 != desc.fRTFBOID);
59f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    SkASSERT(0 != desc.fTexFBOID);
609962108c400dadcae913614e8a5ab781780981d8bsalomon@google.com
619962108c400dadcae913614e8a5ab781780981d8bsalomon@google.com    // we assume this is true, TODO: get rid of viewport as a param.
62f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    SkASSERT(viewport.fWidth == texture->width());
63f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    SkASSERT(viewport.fHeight == texture->height());
649962108c400dadcae913614e8a5ab781780981d8bsalomon@google.com
655bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com    this->init(desc, viewport, texID);
665bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com}
675bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com
685bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.comGrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
695bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com                                   const Desc& desc,
705bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com                                   const GrGLIRect& viewport)
710168afc7196fe82da8af230c93c56f3035d54d2bbsalomon@google.com    : INHERITED(gpu,
72728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com                desc.fIsWrapped,
730168afc7196fe82da8af230c93c56f3035d54d2bbsalomon@google.com                NULL,
747d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                MakeDesc(kNone_GrTextureFlags,
757d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                         viewport.fWidth, viewport.fHeight,
763cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org                         desc.fConfig, desc.fSampleCnt,
773cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org                         desc.fOrigin)) {
785bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com    this->init(desc, viewport, NULL);
795bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com}
805bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com
81aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comvoid GrGLRenderTarget::onRelease() {
82aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    GPUGL->notifyRenderTargetDelete(this);
83728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    if (!this->isWrapped()) {
84aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        if (fTexFBOID) {
850b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com            GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
86aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        }
87aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        if (fRTFBOID && fRTFBOID != fTexFBOID) {
880b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com            GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
89aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        }
90aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        if (fMSColorRenderbufferID) {
910b77d6892b067ad402c9678b0226bff70599fbe2bsalomon@google.com            GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
92aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        }
93aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
94aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    fRTFBOID                = 0;
95aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    fTexFBOID               = 0;
96aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    fMSColorRenderbufferID  = 0;
9759e16e4e1d30eee3ce564e0b513d952579065cb3commit-bot@chromium.org    fTexIDObj.reset(NULL);
98d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com    INHERITED::onRelease();
99aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com}
100aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
101aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comvoid GrGLRenderTarget::onAbandon() {
102aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    fRTFBOID                = 0;
103aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    fTexFBOID               = 0;
104aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    fMSColorRenderbufferID  = 0;
10559e16e4e1d30eee3ce564e0b513d952579065cb3commit-bot@chromium.org    if (NULL != fTexIDObj.get()) {
106aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        fTexIDObj->abandon();
10759e16e4e1d30eee3ce564e0b513d952579065cb3commit-bot@chromium.org        fTexIDObj.reset(NULL);
108aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
109d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com    INHERITED::onAbandon();
110aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com}
111