blob: 1f9636a181c1bb3a73d1ab9128afc5ecd2f7ec59 [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.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.com79d2dbe2011-06-13 19:28:02 +000062 int fContentWidth;
63 int fContentHeight;
64 int fAllocWidth;
65 int fAllocHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +000066 GrPixelConfig fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000067 GrGLuint fTextureID;
68 bool fOwnsID;
69 GrGLenum fUploadFormat;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000070 GrGLenum fUploadType;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000071 Orientation fOrientation;
72 };
73
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000074 // creates a texture that is also an RT
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000075 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000076 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000077 const GrGLRenderTarget::Desc& rtDesc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000078
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000079 // creates a non-RT texture
80 GrGLTexture(GrGpuGL* gpu,
bsalomon@google.com80d09b92011-11-05 21:21:13 +000081 const Desc& textureDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000082
83
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000085
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000086 // overrides of GrTexture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000087 virtual void uploadTextureData(int x,
88 int y,
89 int width,
90 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +000091 const void* srcData,
92 size_t rowBytes);
bsalomon@google.comcee661a2011-07-26 12:32:36 +000093 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000094
bsalomon@google.com80d09b92011-11-05 21:21:13 +000095 // these functions
96 const TexParams& getCachedTexParams(GrGpu::ResetTimestamp* timestamp) const {
97 *timestamp = fTexParamsTimestamp;
98 return fTexParams;
99 }
100 void setCachedTexParams(const TexParams& texParams,
101 GrGpu::ResetTimestamp timestamp) {
102 fTexParams = texParams;
103 fTexParamsTimestamp = timestamp;
104 }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000105 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000106
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107 GrGLenum uploadFormat() const { return fUploadFormat; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000108 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000109
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000110 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000111 * @return width() / allocWidth()
112 */
113 GrScalar contentScaleX() const { return fScaleX; }
114
115 /**
116 * @return height() / allocHeight()
117 */
118 GrScalar contentScaleY() const { return fScaleY; }
119
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 // Ganesh assumes texture coordinates have their origin
121 // in the top-left corner of the image. OpenGL, however,
122 // has the origin in the lower-left corner. For content that
123 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000124 // (by GL's understanding of the world) in glTex*Image and the
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000125 // addressing just works out. However, content generated by GL
126 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 // and it is up to the GrGpuGL derivative to handle y-mirroing.
128 Orientation orientation() const { return fOrientation; }
129
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000130 static const GrGLenum* WrapMode2GLWrap(GrGLBinding binding);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000131
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000132protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000134 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000135 virtual void onAbandon();
136 virtual void onRelease();
137
reed@google.comac10a2d2010-12-22 21:39:39 +0000138private:
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000139 TexParams fTexParams;
140 GrGpu::ResetTimestamp fTexParamsTimestamp;
141 GrGLTexID* fTexIDObj;
142 GrGLenum fUploadFormat;
143 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000144 // precomputed content / alloc ratios
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000145 GrScalar fScaleX;
146 GrScalar fScaleY;
147 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000148
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000149 void init(GrGpuGL* gpu,
150 const Desc& textureDesc,
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000151 const GrGLRenderTarget::Desc* rtDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000152
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 typedef GrTexture INHERITED;
154};
155
156#endif