blob: fe2fa286907897f134eb3fd797ab06f728e54d14 [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
reed@google.comac10a2d2010-12-22 21:39:39 +000017#ifndef GrGLTexture_DEFINED
18#define GrGLTexture_DEFINED
19
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "GrTexture.h"
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000021#include "GrScalar.h"
22#include "GrGLIRect.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24class GrGpuGL;
25class GrGLTexture;
26
bsalomon@google.com1da07462011-03-10 14:51:57 +000027/**
28 * A ref counted tex id that deletes the texture in its destructor.
29 */
30class GrGLTexID : public GrRefCnt {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000031
bsalomon@google.com1da07462011-03-10 14:51:57 +000032public:
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000033 GrGLTexID(GrGLuint texID, bool ownsID) : fTexID(texID), fOwnsID(ownsID) {}
bsalomon@google.com1da07462011-03-10 14:51:57 +000034
35 virtual ~GrGLTexID() {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000036 if (0 != fTexID && fOwnsID) {
bsalomon@google.com1da07462011-03-10 14:51:57 +000037 GR_GL(DeleteTextures(1, &fTexID));
38 }
39 }
40
41 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000042 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000043
44private:
twiz@google.com0f31ca72011-03-18 17:38:11 +000045 GrGLuint fTexID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000046 bool fOwnsID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000047};
48
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000049////////////////////////////////////////////////////////////////////////////////
50
reed@google.comac10a2d2010-12-22 21:39:39 +000051class GrGLRenderTarget : public GrRenderTarget {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000052
reed@google.comac10a2d2010-12-22 21:39:39 +000053public:
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000054 // set fTexFBOID to this value to indicate that it is multisampled but
55 // Gr doesn't know how to resolve it.
56 enum { kUnresolvableFBOID = 0 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000057
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000058 struct GLRenderTargetIDs {
twiz@google.com0f31ca72011-03-18 17:38:11 +000059 GrGLuint fRTFBOID;
60 GrGLuint fTexFBOID;
61 GrGLuint fStencilRenderbufferID;
62 GrGLuint fMSColorRenderbufferID;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000063 bool fOwnIDs;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000064 void reset() { memset(this, 0, sizeof(GLRenderTargetIDs)); }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000065 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000066
67 GrGLRenderTarget(GrGpuGL* gpu,
68 const GLRenderTargetIDs& ids,
bsalomon@google.com1da07462011-03-10 14:51:57 +000069 GrGLTexID* texID,
twiz@google.com0f31ca72011-03-18 17:38:11 +000070 GrGLuint stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000071 bool isMultisampled,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000072 const GrGLIRect& fViewport,
bsalomon@google.com8fe72472011-03-30 21:26:44 +000073 GrGLTexture* texture);
74
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000075 virtual ~GrGLRenderTarget() { this->release(); }
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
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000080 // The following two functions return the same ID when a
81 // texture-rendertarget is multisampled, and different IDs when
82 // it is.
83 // FBO ID used to render into
84 GrGLuint renderFBOID() const { return fRTFBOID; }
85 // FBO ID that has texture ID attached.
86 GrGLuint textureFBOID() const { return fTexFBOID; }
87
88 // override of GrRenderTarget
bsalomon@google.com0b96a842011-07-13 21:53:49 +000089 virtual intptr_t getRenderTargetHandle() const {
90 return this->renderFBOID();
91 }
92 virtual intptr_t getRenderTargetResolvedHandle() const {
93 return this->textureFBOID();
94 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000095 virtual ResolveType getResolveType() const {
96 if (fRTFBOID == fTexFBOID) {
97 // catches FBO 0 and non MSAA case
98 return kAutoResolves_ResolveType;
99 } else if (kUnresolvableFBOID == fTexFBOID) {
100 return kCantResolve_ResolveType;
101 } else {
102 return kCanResolve_ResolveType;
103 }
104 }
105
106protected:
107 // override of GrResource
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000108 virtual void onAbandon();
109 virtual void onRelease();
110
reed@google.comac10a2d2010-12-22 21:39:39 +0000111private:
twiz@google.com0f31ca72011-03-18 17:38:11 +0000112 GrGLuint fRTFBOID;
113 GrGLuint fTexFBOID;
114 GrGLuint fStencilRenderbufferID;
115 GrGLuint fMSColorRenderbufferID;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 // Should this object delete IDs when it is destroyed or does someone
118 // else own them.
119 bool fOwnIDs;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000120
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000121 // when we switch to this rendertarget we want to set the viewport to
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 // only render to to content area (as opposed to the whole allocation) and
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000123 // we want the rendering to be at top left (GL has origin in bottom left)
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000124 GrGLIRect fViewport;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000125
126 // non-NULL if this RT was created by Gr with an associated GrGLTexture.
127 GrGLTexID* fTexIDObj;
128
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 typedef GrRenderTarget INHERITED;
130};
131
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000132////////////////////////////////////////////////////////////////////////////////
133
reed@google.comac10a2d2010-12-22 21:39:39 +0000134class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000135
reed@google.comac10a2d2010-12-22 21:39:39 +0000136public:
137 enum Orientation {
138 kBottomUp_Orientation,
139 kTopDown_Orientation,
140 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000142 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000143 GrGLenum fFilter;
144 GrGLenum fWrapS;
145 GrGLenum fWrapT;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000146 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000147 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000148
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000149 struct GLTextureDesc {
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000150 int fContentWidth;
151 int fContentHeight;
152 int fAllocWidth;
153 int fAllocHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000154 GrPixelConfig fFormat;
155 GrGLuint fTextureID;
156 bool fOwnsID;
157 GrGLenum fUploadFormat;
158 GrGLenum fUploadByteCount;
159 GrGLenum fUploadType;
160 GrGLuint fStencilBits;
161 Orientation fOrientation;
162 };
163
164 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
165
166 GrGLTexture(GrGpuGL* gpu,
167 const GLTextureDesc& textureDesc,
168 const GLRenderTargetIDs& rtIDs,
169 const TexParams& initialTexParams);
170
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000171 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000172
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000173 // overrides of GrTexture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000174 virtual void uploadTextureData(int x,
175 int y,
176 int width,
177 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +0000178 const void* srcData,
179 size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000180 virtual intptr_t getTextureHandle();
181
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000182 const TexParams& getTexParams() const { return fTexParams; }
183 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000184 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
twiz@google.com0f31ca72011-03-18 17:38:11 +0000186 GrGLenum uploadFormat() const { return fUploadFormat; }
187 GrGLenum uploadByteCount() const { return fUploadByteCount; }
188 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000189
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000190 /**
191 * Retrieves the texture width actually allocated in texels.
192 *
193 * @return the width in texels
194 */
195 int allocWidth() const { return fAllocWidth; }
196
197 /**
198 * Retrieves the texture height actually allocated in texels.
199 *
200 * @return the height in texels
201 */
202 int allocHeight() const { return fAllocHeight; }
203
204 /**
205 * @return width() / allocWidth()
206 */
207 GrScalar contentScaleX() const { return fScaleX; }
208
209 /**
210 * @return height() / allocHeight()
211 */
212 GrScalar contentScaleY() const { return fScaleY; }
213
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 // Ganesh assumes texture coordinates have their origin
215 // in the top-left corner of the image. OpenGL, however,
216 // has the origin in the lower-left corner. For content that
217 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000218 // (by GL's understanding of the world ) in glTex*Image and the
219 // addressing just works out. However, content generated by GL
220 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 // and it is up to the GrGpuGL derivative to handle y-mirroing.
222 Orientation orientation() const { return fOrientation; }
223
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000224 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000225
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000226protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000227
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000228 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000229 virtual void onAbandon();
230 virtual void onRelease();
231
reed@google.comac10a2d2010-12-22 21:39:39 +0000232private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000233 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000234 GrGLTexID* fTexIDObj;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000235 GrGLenum fUploadFormat;
236 GrGLenum fUploadByteCount;
237 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000238 int fAllocWidth;
239 int fAllocHeight;
240 // precomputed content / alloc ratios
241 GrScalar fScaleX;
242 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 Orientation fOrientation;
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 GrGpuGL* fGpuGL;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000245
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 typedef GrTexture INHERITED;
247};
248
249#endif