blob: 0a1c107469b9eef40a987497ca0caba12e362601 [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:
twiz@google.com0f31ca72011-03-18 17:38:11 +000033 GrGLTexID(GrGLuint texID) : fTexID(texID) {}
bsalomon@google.com1da07462011-03-10 14:51:57 +000034
35 virtual ~GrGLTexID() {
36 if (0 != fTexID) {
37 GR_GL(DeleteTextures(1, &fTexID));
38 }
39 }
40
41 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000042 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000043
44private:
twiz@google.com0f31ca72011-03-18 17:38:11 +000045 GrGLuint fTexID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000046};
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
twiz@google.com0f31ca72011-03-18 17:38:11 +000056 GrGLuint renderFBOID() const { return fRTFBOID; }
57 GrGLuint textureFBOID() const { return fTexFBOID; }
reed@google.comac10a2d2010-12-22 21:39:39 +000058
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 {
twiz@google.com0f31ca72011-03-18 17:38:11 +000064 GrGLuint fRTFBOID;
65 GrGLuint fTexFBOID;
66 GrGLuint fStencilRenderbufferID;
67 GrGLuint fMSColorRenderbufferID;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000068 bool fOwnIDs;
69 };
70
71 GrGLRenderTarget(const GLRenderTargetIDs& ids,
bsalomon@google.com1da07462011-03-10 14:51:57 +000072 GrGLTexID* texID,
twiz@google.com0f31ca72011-03-18 17:38:11 +000073 GrGLuint stencilBits,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000074 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;
twiz@google.com0f31ca72011-03-18 17:38:11 +000082 GrGLuint fRTFBOID;
83 GrGLuint fTexFBOID;
84 GrGLuint fStencilRenderbufferID;
85 GrGLuint fMSColorRenderbufferID;
reed@google.comac10a2d2010-12-22 21:39:39 +000086
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 {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000117 GrGLenum fFilter;
118 GrGLenum fWrapS;
119 GrGLenum fWrapT;
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000120 };
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;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000129 GrGLuint fTextureID;
130 GrGLenum fUploadFormat;
131 GrGLenum fUploadByteCount;
132 GrGLenum fUploadType;
133 GrGLuint 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; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000158 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000159
twiz@google.com0f31ca72011-03-18 17:38:11 +0000160 GrGLenum uploadFormat() const { return fUploadFormat; }
161 GrGLenum uploadByteCount() const { return fUploadByteCount; }
162 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000163
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;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000201 GrGLenum fUploadFormat;
202 GrGLenum fUploadByteCount;
203 GrGLenum 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
twiz@google.com0f31ca72011-03-18 17:38:11 +0000213 static const GrGLenum gWrapMode2GLWrap[];
reed@google.comac10a2d2010-12-22 21:39:39 +0000214
215 friend class GrGpuGL;
216
217 typedef GrTexture INHERITED;
218};
219
220#endif