blob: dd6419eb716120268c42b039b4bd690a79635dac [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#ifndef GrGLTexture_DEFINED
10#define GrGLTexture_DEFINED
11
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012#include "GrGpu.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000013#include "GrGLRenderTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
bsalomon@google.com1da07462011-03-10 14:51:57 +000015/**
16 * A ref counted tex id that deletes the texture in its destructor.
17 */
18class GrGLTexID : public GrRefCnt {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000019
bsalomon@google.com1da07462011-03-10 14:51:57 +000020public:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021 GrGLTexID(const GrGLInterface* gl, GrGLuint texID, bool ownsID)
22 : fGL(gl)
23 , fTexID(texID)
24 , fOwnsID(ownsID) {
25 }
bsalomon@google.com1da07462011-03-10 14:51:57 +000026
27 virtual ~GrGLTexID() {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000028 if (0 != fTexID && fOwnsID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000029 GR_GL_CALL(fGL, DeleteTextures(1, &fTexID));
bsalomon@google.com1da07462011-03-10 14:51:57 +000030 }
31 }
32
33 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000034 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000035
36private:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000037 const GrGLInterface* fGL;
38 GrGLuint fTexID;
39 bool fOwnsID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000040};
41
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000042////////////////////////////////////////////////////////////////////////////////
43
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000044
reed@google.comac10a2d2010-12-22 21:39:39 +000045class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000046
reed@google.comac10a2d2010-12-22 21:39:39 +000047public:
48 enum Orientation {
49 kBottomUp_Orientation,
50 kTopDown_Orientation,
51 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000052
bsalomon@google.comda96ea02010-12-23 16:53:57 +000053 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +000054 GrGLenum fFilter;
55 GrGLenum fWrapS;
56 GrGLenum fWrapT;
bsalomon@google.com0a97be22011-11-08 19:20:57 +000057 GrGLenum fSwizzleRGBA[4];
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000058 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +000059 };
reed@google.comac10a2d2010-12-22 21:39:39 +000060
robertphillips@google.com32716282012-06-04 12:48:45 +000061 struct Desc : public GrTextureDesc {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000062 GrGLuint fTextureID;
63 bool fOwnsID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000064 Orientation fOrientation;
65 };
66
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000067 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000068 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000069 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000070 const GrGLRenderTarget::Desc& rtDesc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000071
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000072 // creates a non-RT texture
73 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000074 const Desc& textureDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000075
76
bsalomon@google.com8fe72472011-03-30 21:26:44 +000077 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000078
bsalomon@google.comcee661a2011-07-26 12:32:36 +000079 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000080
bsalomon@google.com80d09b92011-11-05 21:21:13 +000081 // these functions
82 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
83 *timestamp = fTexParamsTimestamp;
84 return fTexParams;
85 }
86 void setCachedTexParams(const TexParams& texParams,
87 GrGpu::ResetTimestamp timestamp) {
88 fTexParams = texParams;
89 fTexParamsTimestamp = timestamp;
90 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000091 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000092
reed@google.comac10a2d2010-12-22 21:39:39 +000093 // Ganesh assumes texture coordinates have their origin
94 // in the top-left corner of the image. OpenGL, however,
95 // has the origin in the lower-left corner. For content that
96 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com80d09b92011-11-05 21:21:13 +000097 // (by GL's understanding of the world) in glTex*Image and the
bsalomon@google.com8fe72472011-03-30 21:26:44 +000098 // addressing just works out. However, content generated by GL
99 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 // and it is up to the GrGpuGL derivative to handle y-mirroing.
101 Orientation orientation() const { return fOrientation; }
102
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000103 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000105protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000106
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000107 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000108 virtual void onAbandon();
109 virtual void onRelease();
110
reed@google.comac10a2d2010-12-22 21:39:39 +0000111private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000112 TexParams fTexParams;
113 GrGpu::ResetTimestamp fTexParamsTimestamp;
114 GrGLTexID* fTexIDObj;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000115 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000116
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000117 void init(GrGpuGL* gpu,
118 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000119 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000120
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 typedef GrTexture INHERITED;
122};
123
124#endif