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
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef GrGLTexture_DEFINED
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define GrGLTexture_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "GrGpu.h"
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "GrGLRenderTarget.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/**
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * A ref counted tex id that deletes the texture in its destructor.
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
180a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenbergerclass GrGLTexID : public SkRefCnt {
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SK_DECLARE_INST_COUNT(GrGLTexID)
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
22d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger    GrGLTexID(const GrGLInterface* gl, GrGLuint texID, bool isWrapped)
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        : fGL(gl)
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        , fTexID(texID)
25d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger        , fIsWrapped(isWrapped) {
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual ~GrGLTexID() {
29d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger        if (0 != fTexID && !fIsWrapped) {
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            GR_GL_CALL(fGL, DeleteTextures(1, &fTexID));
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void abandon() { fTexID = 0; }
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLuint id() const { return fTexID; }
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const GrGLInterface* fGL;
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLuint             fTexID;
40d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger    bool                 fIsWrapped;
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
420a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    typedef SkRefCnt INHERITED;
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru////////////////////////////////////////////////////////////////////////////////
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass GrGLTexture : public GrTexture {
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    struct TexParams {
52e27eefc4844477cee5d32f51ab45ff62020cdb36Derek Sollenberger        GrGLenum fMinFilter;
53e27eefc4844477cee5d32f51ab45ff62020cdb36Derek Sollenberger        GrGLenum fMagFilter;
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GrGLenum fWrapS;
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GrGLenum fWrapT;
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GrGLenum fSwizzleRGBA[4];
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    struct Desc : public GrTextureDesc {
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GrGLuint        fTextureID;
62d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger        bool            fIsWrapped;
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // creates a texture that is also an RT
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLTexture(GrGpuGL* gpu,
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                const Desc& textureDesc,
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                const GrGLRenderTarget::Desc& rtDesc);
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // creates a non-RT texture
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLTexture(GrGpuGL* gpu,
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                const Desc& textureDesc);
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual ~GrGLTexture() { this->release(); }
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
76363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger    virtual GrBackendObject getTextureHandle() const SK_OVERRIDE;
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void invalidateCachedState() SK_OVERRIDE { fTexParams.invalidate(); }
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8058190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger    // These functions are used to track the texture parameters associated with the texture.
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        *timestamp = fTexParamsTimestamp;
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return fTexParams;
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8558190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setCachedTexParams(const TexParams& texParams,
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                            GrGpu::ResetTimestamp timestamp) {
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fTexParams = texParams;
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fTexParamsTimestamp = timestamp;
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9258190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger    GrGLuint textureID() const { return (NULL != fTexIDObj.get()) ? fTexIDObj->id() : 0; }
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9458190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenbergerprotected:
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // overrides of GrTexture
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void onAbandon() SK_OVERRIDE;
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void onRelease() SK_OVERRIDE;
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    TexParams                       fTexParams;
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGpu::ResetTimestamp           fTexParamsTimestamp;
10258190644c30e1c4aa8e527f3503c58f841e0fcf3Derek Sollenberger    SkAutoTUnref<GrGLTexID>         fTexIDObj;
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void init(GrGpuGL* gpu,
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru              const Desc& textureDesc,
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru              const GrGLRenderTarget::Desc* rtDesc);
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef GrTexture INHERITED;
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
112