blob: 8e8c8c5da4dc47bdc781ae3733806c531d3e67f9 [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 {
19public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000020 SK_DECLARE_INST_COUNT(GrGLTexID)
21
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022 GrGLTexID(const GrGLInterface* gl, GrGLuint texID, bool ownsID)
23 : fGL(gl)
24 , fTexID(texID)
25 , fOwnsID(ownsID) {
26 }
bsalomon@google.com1da07462011-03-10 14:51:57 +000027
28 virtual ~GrGLTexID() {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000029 if (0 != fTexID && fOwnsID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +000030 GR_GL_CALL(fGL, DeleteTextures(1, &fTexID));
bsalomon@google.com1da07462011-03-10 14:51:57 +000031 }
32 }
33
34 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000035 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000036
37private:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000038 const GrGLInterface* fGL;
39 GrGLuint fTexID;
40 bool fOwnsID;
reed@google.comfa35e3d2012-06-26 20:16:17 +000041
42 typedef GrRefCnt INHERITED;
bsalomon@google.com1da07462011-03-10 14:51:57 +000043};
44
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000045////////////////////////////////////////////////////////////////////////////////
46
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000047
reed@google.comac10a2d2010-12-22 21:39:39 +000048class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000049
reed@google.comac10a2d2010-12-22 21:39:39 +000050public:
51 enum Orientation {
52 kBottomUp_Orientation,
53 kTopDown_Orientation,
54 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000055
bsalomon@google.comda96ea02010-12-23 16:53:57 +000056 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +000057 GrGLenum fFilter;
58 GrGLenum fWrapS;
59 GrGLenum fWrapT;
bsalomon@google.com0a97be22011-11-08 19:20:57 +000060 GrGLenum fSwizzleRGBA[4];
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000061 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +000062 };
reed@google.comac10a2d2010-12-22 21:39:39 +000063
robertphillips@google.com32716282012-06-04 12:48:45 +000064 struct Desc : public GrTextureDesc {
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
junov@chromium.org957ebdd2012-06-12 13:58:36 +000082 virtual intptr_t getTextureHandle() const SK_OVERRIDE;
83
84 virtual void invalidateCachedState() SK_OVERRIDE { fTexParams.invalidate(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000085
bsalomon@google.com80d09b92011-11-05 21:21:13 +000086 // these functions
87 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
88 *timestamp = fTexParamsTimestamp;
89 return fTexParams;
90 }
91 void setCachedTexParams(const TexParams& texParams,
92 GrGpu::ResetTimestamp timestamp) {
93 fTexParams = texParams;
94 fTexParamsTimestamp = timestamp;
95 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000096 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000097
reed@google.comac10a2d2010-12-22 21:39:39 +000098 // Ganesh assumes texture coordinates have their origin
99 // in the top-left corner of the image. OpenGL, however,
100 // has the origin in the lower-left corner. For content that
101 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000102 // (by GL's understanding of the world) in glTex*Image and the
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000103 // addressing just works out. However, content generated by GL
104 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 // and it is up to the GrGpuGL derivative to handle y-mirroing.
106 Orientation orientation() const { return fOrientation; }
107
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000108 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000110protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000111
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000112 // overrides of GrTexture
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000113 virtual void onAbandon() SK_OVERRIDE;
114 virtual void onRelease() SK_OVERRIDE;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000117 TexParams fTexParams;
118 GrGpu::ResetTimestamp fTexParamsTimestamp;
119 GrGLTexID* fTexIDObj;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000120 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000121
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000122 void init(GrGpuGL* gpu,
123 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000124 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000125
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 typedef GrTexture INHERITED;
127};
128
129#endif