blob: ce30e11741b876f9864677252207265c641dd1de [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
bsalomon@google.com80d09b92011-11-05 21:21:13 +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.com79d2dbe2011-06-13 19:28:02 +000063 int fContentWidth;
64 int fContentHeight;
65 int fAllocWidth;
66 int fAllocHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +000067 GrPixelConfig fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000068 GrGLuint fTextureID;
69 bool fOwnsID;
70 GrGLenum fUploadFormat;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000071 GrGLenum fUploadType;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000072 Orientation fOrientation;
73 };
74
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000075 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000076 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000077 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000078 const GrGLRenderTarget::Desc& rtDesc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000079
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000080 // creates a non-RT texture
81 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000082 const Desc& textureDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000083
84
bsalomon@google.com8fe72472011-03-30 21:26:44 +000085 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000086
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000087 // overrides of GrTexture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000088 virtual void uploadTextureData(int x,
89 int y,
90 int width,
91 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +000092 const void* srcData,
93 size_t rowBytes);
bsalomon@google.comcee661a2011-07-26 12:32:36 +000094 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000095
bsalomon@google.com80d09b92011-11-05 21:21:13 +000096 // these functions
97 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
98 *timestamp = fTexParamsTimestamp;
99 return fTexParams;
100 }
101 void setCachedTexParams(const TexParams& texParams,
102 GrGpu::ResetTimestamp timestamp) {
103 fTexParams = texParams;
104 fTexParamsTimestamp = timestamp;
105 }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000106 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000107
twiz@google.com0f31ca72011-03-18 17:38:11 +0000108 GrGLenum uploadFormat() const { return fUploadFormat; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000111 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000112 * @return width() / allocWidth()
113 */
114 GrScalar contentScaleX() const { return fScaleX; }
115
116 /**
117 * @return height() / allocHeight()
118 */
119 GrScalar contentScaleY() const { return fScaleY; }
120
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 // Ganesh assumes texture coordinates have their origin
122 // in the top-left corner of the image. OpenGL, however,
123 // has the origin in the lower-left corner. For content that
124 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000125 // (by GL's understanding of the world) in glTex*Image and the
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000126 // addressing just works out. However, content generated by GL
127 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 // and it is up to the GrGpuGL derivative to handle y-mirroing.
129 Orientation orientation() const { return fOrientation; }
130
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000131 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000132
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000133protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000135 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000136 virtual void onAbandon();
137 virtual void onRelease();
138
reed@google.comac10a2d2010-12-22 21:39:39 +0000139private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000140 TexParams fTexParams;
141 GrGpu::ResetTimestamp fTexParamsTimestamp;
142 GrGLTexID* fTexIDObj;
143 GrGLenum fUploadFormat;
144 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000145 // precomputed content / alloc ratios
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000146 GrScalar fScaleX;
147 GrScalar fScaleY;
148 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000149
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000150 void init(GrGpuGL* gpu,
151 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000152 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000153
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 typedef GrTexture INHERITED;
155};
156
157#endif