blob: fc12ee08780f6bf949143ec5f9157445b73c460d [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
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000039 GLuint getStencilBits() const { return fStencilBits; }
40
41 const GrGLIRect& viewport() const { return fViewport; }
reed@google.comac10a2d2010-12-22 21:39:39 +000042 void abandon();
43
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000044protected:
45
46 struct GLRenderTargetIDs {
47 GLuint fRTFBOID;
48 GLuint fTexFBOID;
49 GLuint fStencilRenderbufferID;
50 GLuint fMSColorRenderbufferID;
51 bool fOwnIDs;
52 };
53
54 GrGLRenderTarget(const GLRenderTargetIDs& ids,
55 GLuint stencilBits,
56 const GrGLIRect& fViewport,
57 GrGLTexture* texture,
58 GrGpuGL* gl);
59
60 void setViewport(const GrGLIRect& rect) { fViewport = rect; }
61
62 virtual int width() const { return fViewport.fWidth; }
63 virtual int height() const { return fViewport.fHeight; }
64
reed@google.comac10a2d2010-12-22 21:39:39 +000065private:
66 GrGpuGL* fGL;
67 GLuint fRTFBOID;
68 GLuint fTexFBOID;
69 GLuint fStencilRenderbufferID;
70 GLuint fMSColorRenderbufferID;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000071 GLuint fStencilBits;
reed@google.comac10a2d2010-12-22 21:39:39 +000072
73 // Should this object delete IDs when it is destroyed or does someone
74 // else own them.
75 bool fOwnIDs;
76
77 // If there separate Texture and RenderTarget FBO IDs then the rendertarget
78 // must be resolved to the texture FBO before it is used as a texture.
79 bool fNeedsResolve;
80
81 // when we switch to this rendertarget we want to set the viewport to
82 // only render to to content area (as opposed to the whole allocation) and
83 // we want the rendering to be at top left (GL has origin in bottom left)
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000084 GrGLIRect fViewport;
reed@google.comac10a2d2010-12-22 21:39:39 +000085
86 friend class GrGpuGL;
87 friend class GrGLTexture;
88
89 typedef GrRenderTarget INHERITED;
90};
91
92class GrGLTexture : public GrTexture {
93public:
94 enum Orientation {
95 kBottomUp_Orientation,
96 kTopDown_Orientation,
97 };
bsalomon@google.comda96ea02010-12-23 16:53:57 +000098
99 struct TexParams {
100 GLenum fFilter;
101 GLenum fWrapS;
102 GLenum fWrapT;
103 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000104
105protected:
106 struct GLTextureDesc {
107 uint32_t fContentWidth;
108 uint32_t fContentHeight;
109 uint32_t fAllocWidth;
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000110 uint32_t fAllocHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 PixelConfig fFormat;
112 GLuint fTextureID;
113 GLenum fUploadFormat;
114 GLenum fUploadByteCount;
115 GLenum fUploadType;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000116 GLuint fStencilBits;
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 Orientation fOrientation;
118 };
119 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
120 GrGLTexture(const GLTextureDesc& textureDesc,
121 const GLRenderTargetIDs& rtIDs,
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000122 const TexParams& initialTexParams,
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 GrGpuGL* gl);
124
125public:
126 virtual ~GrGLTexture();
127
128 // overloads of GrTexture
129 virtual void abandon();
130 virtual bool isRenderTarget() const;
131 virtual GrRenderTarget* asRenderTarget();
132 virtual void removeRenderTarget();
133 virtual void uploadTextureData(uint32_t x,
134 uint32_t y,
135 uint32_t width,
136 uint32_t height,
137 const void* srcData);
138 virtual intptr_t getTextureHandle();
139
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000140 const TexParams& getTexParams() const { return fTexParams; }
141 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 GLuint textureID() const { return fTextureID; }
143
144 GLenum uploadFormat() const { return fUploadFormat; }
145 GLenum uploadByteCount() const { return fUploadByteCount; }
146 GLenum uploadType() const { return fUploadType; }
147
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000148 /**
149 * Retrieves the texture width actually allocated in texels.
150 *
151 * @return the width in texels
152 */
153 int allocWidth() const { return fAllocWidth; }
154
155 /**
156 * Retrieves the texture height actually allocated in texels.
157 *
158 * @return the height in texels
159 */
160 int allocHeight() const { return fAllocHeight; }
161
162 /**
163 * @return width() / allocWidth()
164 */
165 GrScalar contentScaleX() const { return fScaleX; }
166
167 /**
168 * @return height() / allocHeight()
169 */
170 GrScalar contentScaleY() const { return fScaleY; }
171
reed@google.comac10a2d2010-12-22 21:39:39 +0000172 // Ganesh assumes texture coordinates have their origin
173 // in the top-left corner of the image. OpenGL, however,
174 // has the origin in the lower-left corner. For content that
175 // is loaded by Ganesh we just push the content "upside down"
176 // (by GL's understanding of the world ) in glTex*Image and the
177 // addressing just works out. However, content generated by GL
178 // (FBO or externally imported texture) will be updside down
179 // and it is up to the GrGpuGL derivative to handle y-mirroing.
180 Orientation orientation() const { return fOrientation; }
181
182private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000183 TexParams fTexParams;
reed@google.comac10a2d2010-12-22 21:39:39 +0000184 GLuint fTextureID;
185 GLenum fUploadFormat;
186 GLenum fUploadByteCount;
187 GLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000188 int fAllocWidth;
189 int fAllocHeight;
190 // precomputed content / alloc ratios
191 GrScalar fScaleX;
192 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000193 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000194 GrGLRenderTarget* fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 GrGpuGL* fGpuGL;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000196
reed@google.comac10a2d2010-12-22 21:39:39 +0000197 static const GLenum gWrapMode2GLWrap[];
198
199 friend class GrGpuGL;
200
201 typedef GrTexture INHERITED;
202};
203
204#endif