Home | History | Annotate | Download | only in gl
      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 
     16 void 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     this->registerWithCache();
     25 }
     26 
     27 namespace {
     28 GrTextureDesc MakeDesc(GrTextureFlags flags,
     29                        int width, int height,
     30                        GrPixelConfig config, int sampleCnt,
     31                        GrSurfaceOrigin origin) {
     32     GrTextureDesc temp;
     33     temp.fFlags = flags;
     34     temp.fWidth = width;
     35     temp.fHeight = height;
     36     temp.fConfig = config;
     37     temp.fSampleCnt = sampleCnt;
     38     temp.fOrigin = origin;
     39     return temp;
     40 }
     41 
     42 };
     43 
     44 GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
     45                                    const Desc& desc,
     46                                    const GrGLIRect& viewport,
     47                                    GrGLTexID* texID,
     48                                    GrGLTexture* texture)
     49     : INHERITED(gpu,
     50                 desc.fIsWrapped,
     51                 texture,
     52                 MakeDesc(kNone_GrTextureFlags,
     53                          viewport.fWidth, viewport.fHeight,
     54                          desc.fConfig, desc.fSampleCnt,
     55                          desc.fOrigin)) {
     56     SkASSERT(texID);
     57     SkASSERT(texture);
     58     // FBO 0 can't also be a texture, right?
     59     SkASSERT(0 != desc.fRTFBOID);
     60     SkASSERT(0 != desc.fTexFBOID);
     61 
     62     // we assume this is true, TODO: get rid of viewport as a param.
     63     SkASSERT(viewport.fWidth == texture->width());
     64     SkASSERT(viewport.fHeight == texture->height());
     65 
     66     this->init(desc, viewport, texID);
     67 }
     68 
     69 GrGLRenderTarget::GrGLRenderTarget(GrGpuGL* gpu,
     70                                    const Desc& desc,
     71                                    const GrGLIRect& viewport)
     72     : INHERITED(gpu,
     73                 desc.fIsWrapped,
     74                 NULL,
     75                 MakeDesc(kNone_GrTextureFlags,
     76                          viewport.fWidth, viewport.fHeight,
     77                          desc.fConfig, desc.fSampleCnt,
     78                          desc.fOrigin)) {
     79     this->init(desc, viewport, NULL);
     80 }
     81 
     82 void GrGLRenderTarget::onRelease() {
     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 
    101 void GrGLRenderTarget::onAbandon() {
    102     fRTFBOID                = 0;
    103     fTexFBOID               = 0;
    104     fMSColorRenderbufferID  = 0;
    105     if (fTexIDObj.get()) {
    106         fTexIDObj->abandon();
    107         fTexIDObj.reset(NULL);
    108     }
    109     INHERITED::onAbandon();
    110 }
    111