blob: d13fc4463aecf925d71d2f53bc728afdca58b227 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#ifndef GrGLTexture_DEFINED
11#define GrGLTexture_DEFINED
12
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000013#include "../GrGpu.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000014#include "GrGLRenderTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
bsalomon@google.com1da07462011-03-10 14:51:57 +000016/**
17 * A ref counted tex id that deletes the texture in its destructor.
18 */
19class GrGLTexID : public GrRefCnt {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000020
bsalomon@google.com1da07462011-03-10 14:51:57 +000021public:
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;
bsalomon@google.com1da07462011-03-10 14:51:57 +000041};
42
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000043////////////////////////////////////////////////////////////////////////////////
44
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000045
reed@google.comac10a2d2010-12-22 21:39:39 +000046class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000047
reed@google.comac10a2d2010-12-22 21:39:39 +000048public:
49 enum Orientation {
50 kBottomUp_Orientation,
51 kTopDown_Orientation,
52 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000053
bsalomon@google.comda96ea02010-12-23 16:53:57 +000054 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +000055 GrGLenum fFilter;
56 GrGLenum fWrapS;
57 GrGLenum fWrapT;
bsalomon@google.com0a97be22011-11-08 19:20:57 +000058 GrGLenum fSwizzleRGBA[4];
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000059 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +000060 };
reed@google.comac10a2d2010-12-22 21:39:39 +000061
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000062 struct Desc {
bsalomon@google.com99621082011-11-15 16:47:16 +000063 int fWidth;
64 int fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +000065 GrPixelConfig fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000066 GrGLuint fTextureID;
67 bool fOwnsID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000068 Orientation fOrientation;
69 };
70
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000071 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000072 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000073 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000074 const GrGLRenderTarget::Desc& rtDesc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000075
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000076 // creates a non-RT texture
77 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000078 const Desc& textureDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000079
80
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000082
bsalomon@google.comcee661a2011-07-26 12:32:36 +000083 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000084
bsalomon@google.com80d09b92011-11-05 21:21:13 +000085 // these functions
86 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
87 *timestamp = fTexParamsTimestamp;
88 return fTexParams;
89 }
90 void setCachedTexParams(const TexParams& texParams,
91 GrGpu::ResetTimestamp timestamp) {
92 fTexParams = texParams;
93 fTexParamsTimestamp = timestamp;
94 }
twiz@google.com0f31ca72011-03-18 17:38:11 +000095 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000096
reed@google.comac10a2d2010-12-22 21:39:39 +000097 // Ganesh assumes texture coordinates have their origin
98 // in the top-left corner of the image. OpenGL, however,
99 // has the origin in the lower-left corner. For content that
100 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000101 // (by GL's understanding of the world) in glTex*Image and the
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000102 // addressing just works out. However, content generated by GL
103 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 // and it is up to the GrGpuGL derivative to handle y-mirroing.
105 Orientation orientation() const { return fOrientation; }
106
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000107 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000108
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000109protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000110
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000111 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000112 virtual void onAbandon();
113 virtual void onRelease();
114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000116 TexParams fTexParams;
117 GrGpu::ResetTimestamp fTexParamsTimestamp;
118 GrGLTexID* fTexIDObj;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000119 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000121 void init(GrGpuGL* gpu,
122 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000123 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000124
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 typedef GrTexture INHERITED;
126};
127
128#endif