blob: 61a3f755fa2a9de786eded3eea60d34fb449c31d [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.comaa5b6732011-07-29 15:13:20 +000013#include "GrGLRenderTarget.h"
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000014#include "GrScalar.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com1da07462011-03-10 14:51:57 +000017/**
18 * A ref counted tex id that deletes the texture in its destructor.
19 */
20class GrGLTexID : public GrRefCnt {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000021
bsalomon@google.com1da07462011-03-10 14:51:57 +000022public:
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000023 GrGLTexID(GrGLuint texID, bool ownsID) : fTexID(texID), fOwnsID(ownsID) {}
bsalomon@google.com1da07462011-03-10 14:51:57 +000024
25 virtual ~GrGLTexID() {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000026 if (0 != fTexID && fOwnsID) {
bsalomon@google.com1da07462011-03-10 14:51:57 +000027 GR_GL(DeleteTextures(1, &fTexID));
28 }
29 }
30
31 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000032 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000033
34private:
twiz@google.com0f31ca72011-03-18 17:38:11 +000035 GrGLuint fTexID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000036 bool fOwnsID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000037};
38
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000039////////////////////////////////////////////////////////////////////////////////
40
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000041
reed@google.comac10a2d2010-12-22 21:39:39 +000042class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044public:
45 enum Orientation {
46 kBottomUp_Orientation,
47 kTopDown_Orientation,
48 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000049
bsalomon@google.comda96ea02010-12-23 16:53:57 +000050 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +000051 GrGLenum fFilter;
52 GrGLenum fWrapS;
53 GrGLenum fWrapT;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000054 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +000055 };
reed@google.comac10a2d2010-12-22 21:39:39 +000056
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000057 struct Desc {
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000058 int fContentWidth;
59 int fContentHeight;
60 int fAllocWidth;
61 int fAllocHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000062 GrPixelConfig fFormat;
63 GrGLuint fTextureID;
64 bool fOwnsID;
65 GrGLenum fUploadFormat;
66 GrGLenum fUploadByteCount;
67 GrGLenum fUploadType;
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,
74 const GrGLRenderTarget::Desc& rtDesc,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000075 const TexParams& initialTexParams);
76
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000077 // creates a non-RT texture
78 GrGLTexture(GrGpuGL* gpu,
79 const Desc& textureDesc,
80 const TexParams& initialTexParams);
81
82
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000084
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000085 // overrides of GrTexture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000086 virtual void uploadTextureData(int x,
87 int y,
88 int width,
89 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +000090 const void* srcData,
91 size_t rowBytes);
bsalomon@google.comcee661a2011-07-26 12:32:36 +000092 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +000093
bsalomon@google.comda96ea02010-12-23 16:53:57 +000094 const TexParams& getTexParams() const { return fTexParams; }
95 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000096 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000097
twiz@google.com0f31ca72011-03-18 17:38:11 +000098 GrGLenum uploadFormat() const { return fUploadFormat; }
99 GrGLenum uploadByteCount() const { return fUploadByteCount; }
100 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000101
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000102 /**
103 * Retrieves the texture width actually allocated in texels.
104 *
105 * @return the width in texels
106 */
107 int allocWidth() const { return fAllocWidth; }
108
109 /**
110 * Retrieves the texture height actually allocated in texels.
111 *
112 * @return the height in texels
113 */
114 int allocHeight() const { return fAllocHeight; }
115
116 /**
117 * @return width() / allocWidth()
118 */
119 GrScalar contentScaleX() const { return fScaleX; }
120
121 /**
122 * @return height() / allocHeight()
123 */
124 GrScalar contentScaleY() const { return fScaleY; }
125
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 // Ganesh assumes texture coordinates have their origin
127 // in the top-left corner of the image. OpenGL, however,
128 // has the origin in the lower-left corner. For content that
129 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 // (by GL's understanding of the world ) in glTex*Image and the
131 // addressing just works out. However, content generated by GL
132 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 // and it is up to the GrGpuGL derivative to handle y-mirroing.
134 Orientation orientation() const { return fOrientation; }
135
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000136 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000137
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000138protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000139
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000140 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141 virtual void onAbandon();
142 virtual void onRelease();
143
reed@google.comac10a2d2010-12-22 21:39:39 +0000144private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000145 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000146 GrGLTexID* fTexIDObj;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000147 GrGLenum fUploadFormat;
148 GrGLenum fUploadByteCount;
149 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000150 int fAllocWidth;
151 int fAllocHeight;
152 // precomputed content / alloc ratios
153 GrScalar fScaleX;
154 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000156
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000157 void init(GrGpuGL* gpu,
158 const Desc& textureDesc,
159 const GrGLRenderTarget::Desc* rtDesc,
160 const TexParams& initialTexParams);
161
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 typedef GrTexture INHERITED;
163};
164
165#endif