blob: 14370abb80222e70fc69650b7f98948304e1afed [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
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
28class GrGLRenderTarget : public GrRenderTarget {
reed@google.comac10a2d2010-12-22 21:39:39 +000029public:
30 virtual ~GrGLRenderTarget();
31
32 bool resolveable() const { return fRTFBOID != fTexFBOID; }
33 bool needsResolve() const { return fNeedsResolve; }
34 void setDirty(bool dirty) { fNeedsResolve = resolveable() && dirty; }
35
36 GLuint renderFBOID() const { return fRTFBOID; }
37 GLuint textureFBOID() const { return fTexFBOID; }
38
reed@google.comac10a2d2010-12-22 21:39:39 +000039 void abandon();
40
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000041protected:
42
43 struct GLRenderTargetIDs {
44 GLuint fRTFBOID;
45 GLuint fTexFBOID;
46 GLuint fStencilRenderbufferID;
47 GLuint fMSColorRenderbufferID;
48 bool fOwnIDs;
49 };
50
51 GrGLRenderTarget(const GLRenderTargetIDs& ids,
52 GLuint stencilBits,
53 const GrGLIRect& fViewport,
54 GrGLTexture* texture,
55 GrGpuGL* gl);
56
57 void setViewport(const GrGLIRect& rect) { fViewport = rect; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000058 const GrGLIRect& getViewport() const { return fViewport; }
reed@google.comac10a2d2010-12-22 21:39:39 +000059private:
60 GrGpuGL* fGL;
61 GLuint fRTFBOID;
62 GLuint fTexFBOID;
63 GLuint fStencilRenderbufferID;
64 GLuint fMSColorRenderbufferID;
65
66 // Should this object delete IDs when it is destroyed or does someone
67 // else own them.
68 bool fOwnIDs;
69
70 // If there separate Texture and RenderTarget FBO IDs then the rendertarget
71 // must be resolved to the texture FBO before it is used as a texture.
72 bool fNeedsResolve;
73
74 // when we switch to this rendertarget we want to set the viewport to
75 // only render to to content area (as opposed to the whole allocation) and
76 // we want the rendering to be at top left (GL has origin in bottom left)
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000077 GrGLIRect fViewport;
reed@google.comac10a2d2010-12-22 21:39:39 +000078
79 friend class GrGpuGL;
80 friend class GrGLTexture;
81
82 typedef GrRenderTarget INHERITED;
83};
84
85class GrGLTexture : public GrTexture {
86public:
87 enum Orientation {
88 kBottomUp_Orientation,
89 kTopDown_Orientation,
90 };
bsalomon@google.comda96ea02010-12-23 16:53:57 +000091
92 struct TexParams {
93 GLenum fFilter;
94 GLenum fWrapS;
95 GLenum fWrapT;
96 };
reed@google.comac10a2d2010-12-22 21:39:39 +000097
98protected:
99 struct GLTextureDesc {
100 uint32_t fContentWidth;
101 uint32_t fContentHeight;
102 uint32_t fAllocWidth;
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000103 uint32_t fAllocHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 PixelConfig fFormat;
105 GLuint fTextureID;
106 GLenum fUploadFormat;
107 GLenum fUploadByteCount;
108 GLenum fUploadType;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000109 GLuint fStencilBits;
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 Orientation fOrientation;
111 };
112 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
113 GrGLTexture(const GLTextureDesc& textureDesc,
114 const GLRenderTargetIDs& rtIDs,
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000115 const TexParams& initialTexParams,
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 GrGpuGL* gl);
117
118public:
119 virtual ~GrGLTexture();
120
121 // overloads of GrTexture
122 virtual void abandon();
123 virtual bool isRenderTarget() const;
124 virtual GrRenderTarget* asRenderTarget();
125 virtual void removeRenderTarget();
126 virtual void uploadTextureData(uint32_t x,
127 uint32_t y,
128 uint32_t width,
129 uint32_t height,
130 const void* srcData);
131 virtual intptr_t getTextureHandle();
132
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000133 const TexParams& getTexParams() const { return fTexParams; }
134 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 GLuint textureID() const { return fTextureID; }
136
137 GLenum uploadFormat() const { return fUploadFormat; }
138 GLenum uploadByteCount() const { return fUploadByteCount; }
139 GLenum uploadType() const { return fUploadType; }
140
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000141 /**
142 * Retrieves the texture width actually allocated in texels.
143 *
144 * @return the width in texels
145 */
146 int allocWidth() const { return fAllocWidth; }
147
148 /**
149 * Retrieves the texture height actually allocated in texels.
150 *
151 * @return the height in texels
152 */
153 int allocHeight() const { return fAllocHeight; }
154
155 /**
156 * @return width() / allocWidth()
157 */
158 GrScalar contentScaleX() const { return fScaleX; }
159
160 /**
161 * @return height() / allocHeight()
162 */
163 GrScalar contentScaleY() const { return fScaleY; }
164
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 // Ganesh assumes texture coordinates have their origin
166 // in the top-left corner of the image. OpenGL, however,
167 // has the origin in the lower-left corner. For content that
168 // is loaded by Ganesh we just push the content "upside down"
169 // (by GL's understanding of the world ) in glTex*Image and the
170 // addressing just works out. However, content generated by GL
171 // (FBO or externally imported texture) will be updside down
172 // and it is up to the GrGpuGL derivative to handle y-mirroing.
173 Orientation orientation() const { return fOrientation; }
174
175private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000176 TexParams fTexParams;
reed@google.comac10a2d2010-12-22 21:39:39 +0000177 GLuint fTextureID;
178 GLenum fUploadFormat;
179 GLenum fUploadByteCount;
180 GLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000181 int fAllocWidth;
182 int fAllocHeight;
183 // precomputed content / alloc ratios
184 GrScalar fScaleX;
185 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000186 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000187 GrGLRenderTarget* fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 GrGpuGL* fGpuGL;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000189
reed@google.comac10a2d2010-12-22 21:39:39 +0000190 static const GLenum gWrapMode2GLWrap[];
191
192 friend class GrGpuGL;
193
194 typedef GrTexture INHERITED;
195};
196
197#endif