blob: 6f1b9615bf77893c5594511ae4fc41bab9f0dd23 [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
21#include "GrGLConfig.h"
22#include "GrGpu.h"
23#include "GrTexture.h"
24#include "GrRect.h"
25
26class GrGpuGL;
27class GrGLTexture;
28
29class GrGLRenderTarget : public GrRenderTarget {
30protected:
31
32 struct GLRenderTargetIDs {
33 GLuint fRTFBOID;
34 GLuint fTexFBOID;
35 GLuint fStencilRenderbufferID;
36 GLuint fMSColorRenderbufferID;
37 bool fOwnIDs;
38 };
39
40 GrGLRenderTarget(const GLRenderTargetIDs& ids,
41 const GrIRect& fViewport,
42 GrGLTexture* texture,
43 GrGpuGL* gl);
44
45 void setViewport(const GrIRect& rect) { GrAssert(rect.height() <= 0);
46 fViewport = rect;}
47
48 virtual uint32_t width() const { return fViewport.width(); }
49 virtual uint32_t height() const { return -fViewport.height(); }
50
51public:
52 virtual ~GrGLRenderTarget();
53
54 bool resolveable() const { return fRTFBOID != fTexFBOID; }
55 bool needsResolve() const { return fNeedsResolve; }
56 void setDirty(bool dirty) { fNeedsResolve = resolveable() && dirty; }
57
58 GLuint renderFBOID() const { return fRTFBOID; }
59 GLuint textureFBOID() const { return fTexFBOID; }
60
61 const GrIRect& viewport() const { return fViewport; }
62 void abandon();
63
64private:
65 GrGpuGL* fGL;
66 GLuint fRTFBOID;
67 GLuint fTexFBOID;
68 GLuint fStencilRenderbufferID;
69 GLuint fMSColorRenderbufferID;
70
71 // Should this object delete IDs when it is destroyed or does someone
72 // else own them.
73 bool fOwnIDs;
74
75 // If there separate Texture and RenderTarget FBO IDs then the rendertarget
76 // must be resolved to the texture FBO before it is used as a texture.
77 bool fNeedsResolve;
78
79 // when we switch to this rendertarget we want to set the viewport to
80 // only render to to content area (as opposed to the whole allocation) and
81 // we want the rendering to be at top left (GL has origin in bottom left)
82 GrIRect fViewport;
83
84 friend class GrGpuGL;
85 friend class GrGLTexture;
86
87 typedef GrRenderTarget INHERITED;
88};
89
90class GrGLTexture : public GrTexture {
91public:
92 enum Orientation {
93 kBottomUp_Orientation,
94 kTopDown_Orientation,
95 };
bsalomon@google.comda96ea02010-12-23 16:53:57 +000096
97 struct TexParams {
98 GLenum fFilter;
99 GLenum fWrapS;
100 GLenum fWrapT;
101 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000102
103protected:
104 struct GLTextureDesc {
105 uint32_t fContentWidth;
106 uint32_t fContentHeight;
107 uint32_t fAllocWidth;
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000108 uint32_t fAllocHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 PixelConfig fFormat;
110 GLuint fTextureID;
111 GLenum fUploadFormat;
112 GLenum fUploadByteCount;
113 GLenum fUploadType;
114 Orientation fOrientation;
115 };
116 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
117 GrGLTexture(const GLTextureDesc& textureDesc,
118 const GLRenderTargetIDs& rtIDs,
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000119 const TexParams& initialTexParams,
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 GrGpuGL* gl);
121
122public:
123 virtual ~GrGLTexture();
124
125 // overloads of GrTexture
126 virtual void abandon();
127 virtual bool isRenderTarget() const;
128 virtual GrRenderTarget* asRenderTarget();
129 virtual void removeRenderTarget();
130 virtual void uploadTextureData(uint32_t x,
131 uint32_t y,
132 uint32_t width,
133 uint32_t height,
134 const void* srcData);
135 virtual intptr_t getTextureHandle();
136
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000137 const TexParams& getTexParams() const { return fTexParams; }
138 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 GLuint textureID() const { return fTextureID; }
140
141 GLenum uploadFormat() const { return fUploadFormat; }
142 GLenum uploadByteCount() const { return fUploadByteCount; }
143 GLenum uploadType() const { return fUploadType; }
144
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000145 /**
146 * Retrieves the texture width actually allocated in texels.
147 *
148 * @return the width in texels
149 */
150 int allocWidth() const { return fAllocWidth; }
151
152 /**
153 * Retrieves the texture height actually allocated in texels.
154 *
155 * @return the height in texels
156 */
157 int allocHeight() const { return fAllocHeight; }
158
159 /**
160 * @return width() / allocWidth()
161 */
162 GrScalar contentScaleX() const { return fScaleX; }
163
164 /**
165 * @return height() / allocHeight()
166 */
167 GrScalar contentScaleY() const { return fScaleY; }
168
reed@google.comac10a2d2010-12-22 21:39:39 +0000169 // Ganesh assumes texture coordinates have their origin
170 // in the top-left corner of the image. OpenGL, however,
171 // has the origin in the lower-left corner. For content that
172 // is loaded by Ganesh we just push the content "upside down"
173 // (by GL's understanding of the world ) in glTex*Image and the
174 // addressing just works out. However, content generated by GL
175 // (FBO or externally imported texture) will be updside down
176 // and it is up to the GrGpuGL derivative to handle y-mirroing.
177 Orientation orientation() const { return fOrientation; }
178
179private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000180 TexParams fTexParams;
reed@google.comac10a2d2010-12-22 21:39:39 +0000181 GLuint fTextureID;
182 GLenum fUploadFormat;
183 GLenum fUploadByteCount;
184 GLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000185 int fAllocWidth;
186 int fAllocHeight;
187 // precomputed content / alloc ratios
188 GrScalar fScaleX;
189 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000190 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000191 GrGLRenderTarget* fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000192 GrGpuGL* fGpuGL;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000193
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 static const GLenum gWrapMode2GLWrap[];
195
196 friend class GrGpuGL;
197
198 typedef GrTexture INHERITED;
199};
200
201#endif