blob: 0624f405cc74018c5475e13e8224c4ea3dae3ca0 [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
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 // overloads of GrTexture
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 virtual GrRenderTarget* asRenderTarget();
bsalomon@google.com1da07462011-03-10 14:51:57 +0000130 virtual void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000131 virtual void uploadTextureData(uint32_t x,
132 uint32_t y,
133 uint32_t width,
134 uint32_t height,
135 const void* srcData);
136 virtual intptr_t getTextureHandle();
137
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000138 const TexParams& getTexParams() const { return fTexParams; }
139 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000140 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000141
twiz@google.com0f31ca72011-03-18 17:38:11 +0000142 GrGLenum uploadFormat() const { return fUploadFormat; }
143 GrGLenum uploadByteCount() const { return fUploadByteCount; }
144 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000145
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000146 /**
147 * Retrieves the texture width actually allocated in texels.
148 *
149 * @return the width in texels
150 */
151 int allocWidth() const { return fAllocWidth; }
152
153 /**
154 * Retrieves the texture height actually allocated in texels.
155 *
156 * @return the height in texels
157 */
158 int allocHeight() const { return fAllocHeight; }
159
160 /**
161 * @return width() / allocWidth()
162 */
163 GrScalar contentScaleX() const { return fScaleX; }
164
165 /**
166 * @return height() / allocHeight()
167 */
168 GrScalar contentScaleY() const { return fScaleY; }
169
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 // Ganesh assumes texture coordinates have their origin
171 // in the top-left corner of the image. OpenGL, however,
172 // has the origin in the lower-left corner. For content that
173 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000174 // (by GL's understanding of the world ) in glTex*Image and the
175 // addressing just works out. However, content generated by GL
176 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000177 // and it is up to the GrGpuGL derivative to handle y-mirroing.
178 Orientation orientation() const { return fOrientation; }
179
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000180protected:
181
182 struct GLTextureDesc {
183 uint32_t fContentWidth;
184 uint32_t fContentHeight;
185 uint32_t fAllocWidth;
186 uint32_t fAllocHeight;
187 PixelConfig fFormat;
188 GrGLuint fTextureID;
189 GrGLenum fUploadFormat;
190 GrGLenum fUploadByteCount;
191 GrGLenum fUploadType;
192 GrGLuint fStencilBits;
193 Orientation fOrientation;
194 };
195 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
196 GrGLTexture(GrGpuGL* gpu,
197 const GLTextureDesc& textureDesc,
198 const GLRenderTargetIDs& rtIDs,
199 const TexParams& initialTexParams);
200
201 // overloads of GrResource
202 virtual void onAbandon();
203 virtual void onRelease();
204
reed@google.comac10a2d2010-12-22 21:39:39 +0000205private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000206 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000207 GrGLTexID* fTexIDObj;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000208 GrGLenum fUploadFormat;
209 GrGLenum fUploadByteCount;
210 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000211 int fAllocWidth;
212 int fAllocHeight;
213 // precomputed content / alloc ratios
214 GrScalar fScaleX;
215 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 Orientation fOrientation;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000217 GrGLRenderTarget* fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 GrGpuGL* fGpuGL;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000219
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000220 static const GrGLenum* WrapMode2GLWrap();
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
222 friend class GrGpuGL;
223
224 typedef GrTexture INHERITED;
225};
226
227#endif