blob: 94ae7b49d5969227fa694ccbeeca78381aceb6f9 [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.comf954d8d2011-04-06 17:50:02 +000067 bool fOwnIDs;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000068 };
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.comf954d8d2011-04-06 17:50:02 +000074 bool isMultisampled,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000075 const GrGLIRect& fViewport,
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076 GrGLTexture* texture);
77
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000078 void setViewport(const GrGLIRect& rect) { fViewport = rect; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000079 const GrGLIRect& getViewport() const { return fViewport; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000080
81 // overloads of GrResource
82 virtual void onAbandon();
83 virtual void onRelease();
84
reed@google.comac10a2d2010-12-22 21:39:39 +000085private:
twiz@google.com0f31ca72011-03-18 17:38:11 +000086 GrGLuint fRTFBOID;
87 GrGLuint fTexFBOID;
88 GrGLuint fStencilRenderbufferID;
89 GrGLuint fMSColorRenderbufferID;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000090
reed@google.comac10a2d2010-12-22 21:39:39 +000091 // Should this object delete IDs when it is destroyed or does someone
92 // else own them.
93 bool fOwnIDs;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000094
reed@google.comac10a2d2010-12-22 21:39:39 +000095 // If there separate Texture and RenderTarget FBO IDs then the rendertarget
96 // must be resolved to the texture FBO before it is used as a texture.
97 bool fNeedsResolve;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000098
99 // when we switch to this rendertarget we want to set the viewport to
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 // only render to to content area (as opposed to the whole allocation) and
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000101 // we want the rendering to be at top left (GL has origin in bottom left)
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000102 GrGLIRect fViewport;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000103
104 // non-NULL if this RT was created by Gr with an associated GrGLTexture.
105 GrGLTexID* fTexIDObj;
106
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 friend class GrGpuGL;
108 friend class GrGLTexture;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 typedef GrRenderTarget INHERITED;
111};
112
113class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115public:
116 enum Orientation {
117 kBottomUp_Orientation,
118 kTopDown_Orientation,
119 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000120
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000121 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000122 GrGLenum fFilter;
123 GrGLenum fWrapS;
124 GrGLenum fWrapT;
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000125 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000127 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000128
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000129 // overrides of GrTexture
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 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; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000139 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000140
twiz@google.com0f31ca72011-03-18 17:38:11 +0000141 GrGLenum uploadFormat() const { return fUploadFormat; }
142 GrGLenum uploadByteCount() const { return fUploadByteCount; }
143 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
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"
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000173 // (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
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 // and it is up to the GrGpuGL derivative to handle y-mirroing.
177 Orientation orientation() const { return fOrientation; }
178
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000179protected:
180
181 struct GLTextureDesc {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000182 uint32_t fContentWidth;
183 uint32_t fContentHeight;
184 uint32_t fAllocWidth;
185 uint32_t fAllocHeight;
186 GrPixelConfig fFormat;
187 GrGLuint fTextureID;
188 GrGLenum fUploadFormat;
189 GrGLenum fUploadByteCount;
190 GrGLenum fUploadType;
191 GrGLuint fStencilBits;
192 Orientation fOrientation;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000193 };
194 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
195 GrGLTexture(GrGpuGL* gpu,
196 const GLTextureDesc& textureDesc,
197 const GLRenderTargetIDs& rtIDs,
198 const TexParams& initialTexParams);
199
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000200 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000201 virtual void onAbandon();
202 virtual void onRelease();
203
reed@google.comac10a2d2010-12-22 21:39:39 +0000204private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000205 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000206 GrGLTexID* fTexIDObj;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000207 GrGLenum fUploadFormat;
208 GrGLenum fUploadByteCount;
209 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000210 int fAllocWidth;
211 int fAllocHeight;
212 // precomputed content / alloc ratios
213 GrScalar fScaleX;
214 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 Orientation fOrientation;
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 GrGpuGL* fGpuGL;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000217
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000218 static const GrGLenum* WrapMode2GLWrap();
reed@google.comac10a2d2010-12-22 21:39:39 +0000219
220 friend class GrGpuGL;
221
222 typedef GrTexture INHERITED;
223};
224
225#endif