blob: 7924cc3f00bbe42d12b40906fd152ee6653a88cb [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 {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000032
bsalomon@google.com1da07462011-03-10 14:51:57 +000033public:
twiz@google.com0f31ca72011-03-18 17:38:11 +000034 GrGLTexID(GrGLuint texID) : fTexID(texID) {}
bsalomon@google.com1da07462011-03-10 14:51:57 +000035
36 virtual ~GrGLTexID() {
37 if (0 != fTexID) {
38 GR_GL(DeleteTextures(1, &fTexID));
39 }
40 }
41
42 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000043 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000044
45private:
twiz@google.com0f31ca72011-03-18 17:38:11 +000046 GrGLuint fTexID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000047};
48
reed@google.comac10a2d2010-12-22 21:39:39 +000049class GrGLRenderTarget : public GrRenderTarget {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000050
reed@google.comac10a2d2010-12-22 21:39:39 +000051public:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000052 virtual ~GrGLRenderTarget() { this->release(); }
53
reed@google.comac10a2d2010-12-22 21:39:39 +000054 bool resolveable() const { return fRTFBOID != fTexFBOID; }
55 bool needsResolve() const { return fNeedsResolve; }
56 void setDirty(bool dirty) { fNeedsResolve = resolveable() && dirty; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000057
twiz@google.com0f31ca72011-03-18 17:38:11 +000058 GrGLuint renderFBOID() const { return fRTFBOID; }
59 GrGLuint textureFBOID() const { return fTexFBOID; }
reed@google.comac10a2d2010-12-22 21:39:39 +000060
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000061protected:
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000062 struct GLRenderTargetIDs {
twiz@google.com0f31ca72011-03-18 17:38:11 +000063 GrGLuint fRTFBOID;
64 GrGLuint fTexFBOID;
65 GrGLuint fStencilRenderbufferID;
66 GrGLuint fMSColorRenderbufferID;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000067 bool fOwnIDs;
68 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000069
70 GrGLRenderTarget(GrGpuGL* gpu,
71 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,
bsalomon@google.com8fe72472011-03-30 21:26:44 +000075 GrGLTexture* texture);
76
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000077 void setViewport(const GrGLIRect& rect) { fViewport = rect; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000078 const GrGLIRect& getViewport() const { return fViewport; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000079
80 // overloads of GrResource
81 virtual void onAbandon();
82 virtual void onRelease();
83
reed@google.comac10a2d2010-12-22 21:39:39 +000084private:
twiz@google.com0f31ca72011-03-18 17:38:11 +000085 GrGLuint fRTFBOID;
86 GrGLuint fTexFBOID;
87 GrGLuint fStencilRenderbufferID;
88 GrGLuint fMSColorRenderbufferID;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000089
reed@google.comac10a2d2010-12-22 21:39:39 +000090 // Should this object delete IDs when it is destroyed or does someone
91 // else own them.
92 bool fOwnIDs;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093
reed@google.comac10a2d2010-12-22 21:39:39 +000094 // If there separate Texture and RenderTarget FBO IDs then the rendertarget
95 // must be resolved to the texture FBO before it is used as a texture.
96 bool fNeedsResolve;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000097
98 // when we switch to this rendertarget we want to set the viewport to
reed@google.comac10a2d2010-12-22 21:39:39 +000099 // only render to to content area (as opposed to the whole allocation) and
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000100 // we want the rendering to be at top left (GL has origin in bottom left)
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000101 GrGLIRect fViewport;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000102
103 // non-NULL if this RT was created by Gr with an associated GrGLTexture.
104 GrGLTexID* fTexIDObj;
105
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 friend class GrGpuGL;
107 friend class GrGLTexture;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000108
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 typedef GrRenderTarget INHERITED;
110};
111
112class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114public:
115 enum Orientation {
116 kBottomUp_Orientation,
117 kTopDown_Orientation,
118 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000119
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000120 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000121 GrGLenum fFilter;
122 GrGLenum fWrapS;
123 GrGLenum fWrapT;
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000124 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000125
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000126 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000128 // overrides of GrTexture
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 virtual void uploadTextureData(uint32_t x,
130 uint32_t y,
131 uint32_t width,
132 uint32_t height,
133 const void* srcData);
134 virtual intptr_t getTextureHandle();
135
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000136 const TexParams& getTexParams() const { return fTexParams; }
137 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000138 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
twiz@google.com0f31ca72011-03-18 17:38:11 +0000140 GrGLenum uploadFormat() const { return fUploadFormat; }
141 GrGLenum uploadByteCount() const { return fUploadByteCount; }
142 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000143
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000144 /**
145 * Retrieves the texture width actually allocated in texels.
146 *
147 * @return the width in texels
148 */
149 int allocWidth() const { return fAllocWidth; }
150
151 /**
152 * Retrieves the texture height actually allocated in texels.
153 *
154 * @return the height in texels
155 */
156 int allocHeight() const { return fAllocHeight; }
157
158 /**
159 * @return width() / allocWidth()
160 */
161 GrScalar contentScaleX() const { return fScaleX; }
162
163 /**
164 * @return height() / allocHeight()
165 */
166 GrScalar contentScaleY() const { return fScaleY; }
167
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 // Ganesh assumes texture coordinates have their origin
169 // in the top-left corner of the image. OpenGL, however,
170 // has the origin in the lower-left corner. For content that
171 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000172 // (by GL's understanding of the world ) in glTex*Image and the
173 // addressing just works out. However, content generated by GL
174 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000175 // and it is up to the GrGpuGL derivative to handle y-mirroing.
176 Orientation orientation() const { return fOrientation; }
177
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000178protected:
179
180 struct GLTextureDesc {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000181 uint32_t fContentWidth;
182 uint32_t fContentHeight;
183 uint32_t fAllocWidth;
184 uint32_t fAllocHeight;
185 GrPixelConfig fFormat;
186 GrGLuint fTextureID;
187 GrGLenum fUploadFormat;
188 GrGLenum fUploadByteCount;
189 GrGLenum fUploadType;
190 GrGLuint fStencilBits;
191 Orientation fOrientation;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000192 };
193 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
194 GrGLTexture(GrGpuGL* gpu,
195 const GLTextureDesc& textureDesc,
196 const GLRenderTargetIDs& rtIDs,
197 const TexParams& initialTexParams);
198
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000199 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000200 virtual void onAbandon();
201 virtual void onRelease();
202
reed@google.comac10a2d2010-12-22 21:39:39 +0000203private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000204 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000205 GrGLTexID* fTexIDObj;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000206 GrGLenum fUploadFormat;
207 GrGLenum fUploadByteCount;
208 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000209 int fAllocWidth;
210 int fAllocHeight;
211 // precomputed content / alloc ratios
212 GrScalar fScaleX;
213 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 Orientation fOrientation;
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 GrGpuGL* fGpuGL;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000216
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000217 static const GrGLenum* WrapMode2GLWrap();
reed@google.comac10a2d2010-12-22 21:39:39 +0000218
219 friend class GrGpuGL;
220
221 typedef GrTexture INHERITED;
222};
223
224#endif