blob: 7b7480ca8798781bbe272b7ddfe4124d33daa916 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
bsalomon@google.com1da07462011-03-10 14:51:57 +00002 Copyright 2011 Google Inc.
reed@google.comac10a2d2010-12-22 21:39:39 +00003
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef GrGLTexture_DEFINED
19#define GrGLTexture_DEFINED
20
reed@google.comac10a2d2010-12-22 21:39:39 +000021#include "GrTexture.h"
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000022#include "GrScalar.h"
23#include "GrGLIRect.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
25class GrGpuGL;
26class GrGLTexture;
27
bsalomon@google.com1da07462011-03-10 14:51:57 +000028/**
29 * A ref counted tex id that deletes the texture in its destructor.
30 */
31class GrGLTexID : public GrRefCnt {
32public:
33 GrGLTexID(GLuint texID) : fTexID(texID) {}
34
35 virtual ~GrGLTexID() {
36 if (0 != fTexID) {
37 GR_GL(DeleteTextures(1, &fTexID));
38 }
39 }
40
41 void abandon() { fTexID = 0; }
42 GLuint id() const { return fTexID; }
43
44private:
45 GLuint fTexID;
46};
47
reed@google.comac10a2d2010-12-22 21:39:39 +000048class GrGLRenderTarget : public GrRenderTarget {
reed@google.comac10a2d2010-12-22 21:39:39 +000049public:
50 virtual ~GrGLRenderTarget();
51
52 bool resolveable() const { return fRTFBOID != fTexFBOID; }
53 bool needsResolve() const { return fNeedsResolve; }
54 void setDirty(bool dirty) { fNeedsResolve = resolveable() && dirty; }
55
56 GLuint renderFBOID() const { return fRTFBOID; }
57 GLuint textureFBOID() const { return fTexFBOID; }
58
reed@google.comac10a2d2010-12-22 21:39:39 +000059 void abandon();
60
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000061protected:
62
63 struct GLRenderTargetIDs {
64 GLuint fRTFBOID;
65 GLuint fTexFBOID;
66 GLuint fStencilRenderbufferID;
67 GLuint fMSColorRenderbufferID;
68 bool fOwnIDs;
69 };
70
71 GrGLRenderTarget(const GLRenderTargetIDs& ids,
bsalomon@google.com1da07462011-03-10 14:51:57 +000072 GrGLTexID* texID,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000073 GLuint stencilBits,
74 const GrGLIRect& fViewport,
75 GrGLTexture* texture,
76 GrGpuGL* gl);
77
78 void setViewport(const GrGLIRect& rect) { fViewport = rect; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000079 const GrGLIRect& getViewport() const { return fViewport; }
reed@google.comac10a2d2010-12-22 21:39:39 +000080private:
81 GrGpuGL* fGL;
82 GLuint fRTFBOID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000083 GLuint fTexFBOID;
reed@google.comac10a2d2010-12-22 21:39:39 +000084 GLuint fStencilRenderbufferID;
85 GLuint fMSColorRenderbufferID;
86
87 // Should this object delete IDs when it is destroyed or does someone
88 // else own them.
89 bool fOwnIDs;
90
91 // If there separate Texture and RenderTarget FBO IDs then the rendertarget
92 // must be resolved to the texture FBO before it is used as a texture.
93 bool fNeedsResolve;
94
95 // when we switch to this rendertarget we want to set the viewport to
96 // only render to to content area (as opposed to the whole allocation) and
97 // we want the rendering to be at top left (GL has origin in bottom left)
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000098 GrGLIRect fViewport;
bsalomon@google.com1da07462011-03-10 14:51:57 +000099
100 // non-NULL if this RT was created by Gr with an associated GrGLTexture.
101 GrGLTexID* fTexIDObj;
102
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 friend class GrGpuGL;
104 friend class GrGLTexture;
105
106 typedef GrRenderTarget INHERITED;
107};
108
109class GrGLTexture : public GrTexture {
110public:
111 enum Orientation {
112 kBottomUp_Orientation,
113 kTopDown_Orientation,
114 };
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000115
116 struct TexParams {
117 GLenum fFilter;
118 GLenum fWrapS;
119 GLenum fWrapT;
120 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000121
122protected:
123 struct GLTextureDesc {
124 uint32_t fContentWidth;
125 uint32_t fContentHeight;
126 uint32_t fAllocWidth;
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000127 uint32_t fAllocHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 PixelConfig fFormat;
129 GLuint fTextureID;
130 GLenum fUploadFormat;
131 GLenum fUploadByteCount;
132 GLenum fUploadType;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000133 GLuint fStencilBits;
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 Orientation fOrientation;
135 };
136 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
137 GrGLTexture(const GLTextureDesc& textureDesc,
138 const GLRenderTargetIDs& rtIDs,
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000139 const TexParams& initialTexParams,
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 GrGpuGL* gl);
141
142public:
143 virtual ~GrGLTexture();
144
145 // overloads of GrTexture
146 virtual void abandon();
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 virtual GrRenderTarget* asRenderTarget();
bsalomon@google.com1da07462011-03-10 14:51:57 +0000148 virtual void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 virtual void uploadTextureData(uint32_t x,
150 uint32_t y,
151 uint32_t width,
152 uint32_t height,
153 const void* srcData);
154 virtual intptr_t getTextureHandle();
155
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000156 const TexParams& getTexParams() const { return fTexParams; }
157 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000158 GLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000159
160 GLenum uploadFormat() const { return fUploadFormat; }
161 GLenum uploadByteCount() const { return fUploadByteCount; }
162 GLenum uploadType() const { return fUploadType; }
163
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000164 /**
165 * Retrieves the texture width actually allocated in texels.
166 *
167 * @return the width in texels
168 */
169 int allocWidth() const { return fAllocWidth; }
170
171 /**
172 * Retrieves the texture height actually allocated in texels.
173 *
174 * @return the height in texels
175 */
176 int allocHeight() const { return fAllocHeight; }
177
178 /**
179 * @return width() / allocWidth()
180 */
181 GrScalar contentScaleX() const { return fScaleX; }
182
183 /**
184 * @return height() / allocHeight()
185 */
186 GrScalar contentScaleY() const { return fScaleY; }
187
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 // Ganesh assumes texture coordinates have their origin
189 // in the top-left corner of the image. OpenGL, however,
190 // has the origin in the lower-left corner. For content that
191 // is loaded by Ganesh we just push the content "upside down"
192 // (by GL's understanding of the world ) in glTex*Image and the
193 // addressing just works out. However, content generated by GL
194 // (FBO or externally imported texture) will be updside down
195 // and it is up to the GrGpuGL derivative to handle y-mirroing.
196 Orientation orientation() const { return fOrientation; }
197
198private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000199 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000200 GrGLTexID* fTexIDObj;
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 GLenum fUploadFormat;
202 GLenum fUploadByteCount;
203 GLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000204 int fAllocWidth;
205 int fAllocHeight;
206 // precomputed content / alloc ratios
207 GrScalar fScaleX;
208 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000210 GrGLRenderTarget* fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 GrGpuGL* fGpuGL;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000212
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 static const GLenum gWrapMode2GLWrap[];
214
215 friend class GrGpuGL;
216
217 typedef GrTexture INHERITED;
218};
219
220#endif