180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "GrGLTexture.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "GrGpuGL.h"
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define GPUGL static_cast<GrGpuGL*>(getGpu())
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid GrGLTexture::init(GrGpuGL* gpu,
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                       const Desc& textureDesc,
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                       const GrGLRenderTarget::Desc* rtDesc) {
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
190a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    SkASSERT(0 != textureDesc.fTextureID);
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fTexParams.invalidate();
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
2358190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger    fTexIDObj.reset(SkNEW_ARGS(GrGLTexID, (GPUGL->glInterface(),
2458190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger                                           textureDesc.fTextureID,
2558190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger                                           textureDesc.fIsWrapped)));
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL != rtDesc) {
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GrGLIRect vp;
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        vp.fLeft   = 0;
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        vp.fWidth  = textureDesc.fWidth;
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        vp.fBottom = 0;
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        vp.fHeight = textureDesc.fHeight;
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
347839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger        fRenderTarget.reset(SkNEW_ARGS(GrGLRenderTarget, (gpu, *rtDesc, vp, fTexIDObj, this)));
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruGrGLTexture::GrGLTexture(GrGpuGL* gpu,
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                         const Desc& textureDesc)
40096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    : INHERITED(gpu, textureDesc.fIsWrapped, textureDesc) {
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->init(gpu, textureDesc, NULL);
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruGrGLTexture::GrGLTexture(GrGpuGL* gpu,
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                         const Desc& textureDesc,
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                         const GrGLRenderTarget::Desc& rtDesc)
47096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    : INHERITED(gpu, textureDesc.fIsWrapped, textureDesc) {
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->init(gpu, textureDesc, &rtDesc);
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid GrGLTexture::onRelease() {
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GPUGL->notifyTextureDelete(this);
5358190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger    fTexIDObj.reset(NULL);
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    INHERITED::onRelease();
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid GrGLTexture::onAbandon() {
5858190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger    if (NULL != fTexIDObj.get()) {
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fTexIDObj->abandon();
6058190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger        fTexIDObj.reset(NULL);
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    INHERITED::onAbandon();
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
66363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek SollenbergerGrBackendObject GrGLTexture::getTextureHandle() const {
6758190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger    return static_cast<GrBackendObject>(this->textureID());
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
69