blob: e408e5db94b73e30d1973046d523e934f23556d2 [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
junov@chromium.org957ebdd2012-06-12 13:58:36 +000079 virtual intptr_t getTextureHandle() const SK_OVERRIDE;
80
81 virtual void invalidateCachedState() SK_OVERRIDE { fTexParams.invalidate(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000082
bsalomon@google.com80d09b92011-11-05 21:21:13 +000083 // these functions
84 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
85 *timestamp = fTexParamsTimestamp;
86 return fTexParams;
87 }
88 void setCachedTexParams(const TexParams& texParams,
89 GrGpu::ResetTimestamp timestamp) {
90 fTexParams = texParams;
91 fTexParamsTimestamp = timestamp;
92 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000093 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000094
reed@google.comac10a2d2010-12-22 21:39:39 +000095 // Ganesh assumes texture coordinates have their origin
96 // in the top-left corner of the image. OpenGL, however,
97 // has the origin in the lower-left corner. For content that
98 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com80d09b92011-11-05 21:21:13 +000099 // (by GL's understanding of the world) in glTex*Image and the
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000100 // addressing just works out. However, content generated by GL
101 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 // and it is up to the GrGpuGL derivative to handle y-mirroing.
103 Orientation orientation() const { return fOrientation; }
104
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000105 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000106
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000107protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000108
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000109 // overrides of GrTexture
junov@chromium.org957ebdd2012-06-12 13:58:36 +0000110 virtual void onAbandon() SK_OVERRIDE;
111 virtual void onRelease() SK_OVERRIDE;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000112
reed@google.comac10a2d2010-12-22 21:39:39 +0000113private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000114 TexParams fTexParams;
115 GrGpu::ResetTimestamp fTexParamsTimestamp;
116 GrGLTexID* fTexIDObj;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000117 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000118
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000119 void init(GrGpuGL* gpu,
120 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000121 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000122
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 typedef GrTexture INHERITED;
124};
125
126#endif