blob: 77e1c327483e0d92a86a6f0daf92f432bfe654b4 [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
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000061 struct Desc {
bsalomon@google.com99621082011-11-15 16:47:16 +000062 int fWidth;
63 int fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +000064 GrPixelConfig fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000065 GrGLuint fTextureID;
66 bool fOwnsID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000067 Orientation fOrientation;
68 };
69
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000070 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000071 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000072 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000073 const GrGLRenderTarget::Desc& rtDesc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000074
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000075 // creates a non-RT texture
76 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000077 const Desc& textureDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000078
79
bsalomon@google.com8fe72472011-03-30 21:26:44 +000080 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000081
bsalomon@google.comcee661a2011-07-26 12:32:36 +000082 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000083
bsalomon@google.com80d09b92011-11-05 21:21:13 +000084 // these functions
85 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
86 *timestamp = fTexParamsTimestamp;
87 return fTexParams;
88 }
89 void setCachedTexParams(const TexParams& texParams,
90 GrGpu::ResetTimestamp timestamp) {
91 fTexParams = texParams;
92 fTexParamsTimestamp = timestamp;
93 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000094 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000095
reed@google.comac10a2d2010-12-22 21:39:39 +000096 // Ganesh assumes texture coordinates have their origin
97 // in the top-left corner of the image. OpenGL, however,
98 // has the origin in the lower-left corner. For content that
99 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000100 // (by GL's understanding of the world) in glTex*Image and the
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000101 // addressing just works out. However, content generated by GL
102 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 // and it is up to the GrGpuGL derivative to handle y-mirroing.
104 Orientation orientation() const { return fOrientation; }
105
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000106 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000108protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000110 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000111 virtual void onAbandon();
112 virtual void onRelease();
113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000115 TexParams fTexParams;
116 GrGpu::ResetTimestamp fTexParamsTimestamp;
117 GrGLTexID* fTexIDObj;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000118 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000119
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000120 void init(GrGpuGL* gpu,
121 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000122 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000123
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 typedef GrTexture INHERITED;
125};
126
127#endif