blob: 32c2f205c30896dab004606291132f7ba9f830f9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#ifndef GrGLTexture_DEFINED
11#define GrGLTexture_DEFINED
12
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrTexture.h"
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000014#include "GrScalar.h"
15#include "GrGLIRect.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
17class GrGpuGL;
18class GrGLTexture;
19
bsalomon@google.com1da07462011-03-10 14:51:57 +000020/**
21 * A ref counted tex id that deletes the texture in its destructor.
22 */
23class GrGLTexID : public GrRefCnt {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000024
bsalomon@google.com1da07462011-03-10 14:51:57 +000025public:
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000026 GrGLTexID(GrGLuint texID, bool ownsID) : fTexID(texID), fOwnsID(ownsID) {}
bsalomon@google.com1da07462011-03-10 14:51:57 +000027
28 virtual ~GrGLTexID() {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000029 if (0 != fTexID && fOwnsID) {
bsalomon@google.com1da07462011-03-10 14:51:57 +000030 GR_GL(DeleteTextures(1, &fTexID));
31 }
32 }
33
34 void abandon() { fTexID = 0; }
twiz@google.com0f31ca72011-03-18 17:38:11 +000035 GrGLuint id() const { return fTexID; }
bsalomon@google.com1da07462011-03-10 14:51:57 +000036
37private:
twiz@google.com0f31ca72011-03-18 17:38:11 +000038 GrGLuint fTexID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000039 bool fOwnsID;
bsalomon@google.com1da07462011-03-10 14:51:57 +000040};
41
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000042////////////////////////////////////////////////////////////////////////////////
43
reed@google.comac10a2d2010-12-22 21:39:39 +000044class GrGLRenderTarget : public GrRenderTarget {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000045
reed@google.comac10a2d2010-12-22 21:39:39 +000046public:
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000047 // set fTexFBOID to this value to indicate that it is multisampled but
48 // Gr doesn't know how to resolve it.
49 enum { kUnresolvableFBOID = 0 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000050
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000051 struct GLRenderTargetIDs {
twiz@google.com0f31ca72011-03-18 17:38:11 +000052 GrGLuint fRTFBOID;
53 GrGLuint fTexFBOID;
54 GrGLuint fStencilRenderbufferID;
55 GrGLuint fMSColorRenderbufferID;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000056 bool fOwnIDs;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000057 void reset() { memset(this, 0, sizeof(GLRenderTargetIDs)); }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000058 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +000059
60 GrGLRenderTarget(GrGpuGL* gpu,
61 const GLRenderTargetIDs& ids,
bsalomon@google.com1da07462011-03-10 14:51:57 +000062 GrGLTexID* texID,
bsalomon@google.comcee661a2011-07-26 12:32:36 +000063 GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +000064 GrGLuint stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000065 bool isMultisampled,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000066 const GrGLIRect& fViewport,
bsalomon@google.com8fe72472011-03-30 21:26:44 +000067 GrGLTexture* texture);
68
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000069 virtual ~GrGLRenderTarget() { this->release(); }
70
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000071 void setViewport(const GrGLIRect& rect) { fViewport = rect; }
bsalomon@google.comd302f142011-03-03 13:54:13 +000072 const GrGLIRect& getViewport() const { return fViewport; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000073
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000074 // The following two functions return the same ID when a
75 // texture-rendertarget is multisampled, and different IDs when
76 // it is.
77 // FBO ID used to render into
78 GrGLuint renderFBOID() const { return fRTFBOID; }
79 // FBO ID that has texture ID attached.
80 GrGLuint textureFBOID() const { return fTexFBOID; }
81
82 // override of GrRenderTarget
bsalomon@google.com0b96a842011-07-13 21:53:49 +000083 virtual intptr_t getRenderTargetHandle() const {
84 return this->renderFBOID();
85 }
86 virtual intptr_t getRenderTargetResolvedHandle() const {
87 return this->textureFBOID();
88 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000089 virtual ResolveType getResolveType() const {
90 if (fRTFBOID == fTexFBOID) {
91 // catches FBO 0 and non MSAA case
92 return kAutoResolves_ResolveType;
93 } else if (kUnresolvableFBOID == fTexFBOID) {
94 return kCantResolve_ResolveType;
95 } else {
96 return kCanResolve_ResolveType;
97 }
98 }
99
100protected:
101 // override of GrResource
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000102 virtual void onAbandon();
103 virtual void onRelease();
104
reed@google.comac10a2d2010-12-22 21:39:39 +0000105private:
twiz@google.com0f31ca72011-03-18 17:38:11 +0000106 GrGLuint fRTFBOID;
107 GrGLuint fTexFBOID;
108 GrGLuint fStencilRenderbufferID;
109 GrGLuint fMSColorRenderbufferID;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000110
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 // Should this object delete IDs when it is destroyed or does someone
112 // else own them.
113 bool fOwnIDs;
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000114
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115 // when we switch to this rendertarget we want to set the viewport to
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 // only render to to content area (as opposed to the whole allocation) and
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000117 // we want the rendering to be at top left (GL has origin in bottom left)
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000118 GrGLIRect fViewport;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000119
120 // non-NULL if this RT was created by Gr with an associated GrGLTexture.
121 GrGLTexID* fTexIDObj;
122
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 typedef GrRenderTarget INHERITED;
124};
125
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000126////////////////////////////////////////////////////////////////////////////////
127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128class GrGLTexture : public GrTexture {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000129
reed@google.comac10a2d2010-12-22 21:39:39 +0000130public:
131 enum Orientation {
132 kBottomUp_Orientation,
133 kTopDown_Orientation,
134 };
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000135
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000136 struct TexParams {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000137 GrGLenum fFilter;
138 GrGLenum fWrapS;
139 GrGLenum fWrapT;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000140 void invalidate() { memset(this, 0xff, sizeof(TexParams)); }
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000141 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000143 struct GLTextureDesc {
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000144 int fContentWidth;
145 int fContentHeight;
146 int fAllocWidth;
147 int fAllocHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000148 GrPixelConfig fFormat;
149 GrGLuint fTextureID;
150 bool fOwnsID;
151 GrGLenum fUploadFormat;
152 GrGLenum fUploadByteCount;
153 GrGLenum fUploadType;
154 GrGLuint fStencilBits;
155 Orientation fOrientation;
156 };
157
158 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
159
160 GrGLTexture(GrGpuGL* gpu,
161 const GLTextureDesc& textureDesc,
162 const GLRenderTargetIDs& rtIDs,
163 const TexParams& initialTexParams);
164
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000165 virtual ~GrGLTexture() { this->release(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000166
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000167 // overrides of GrTexture
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000168 virtual void uploadTextureData(int x,
169 int y,
170 int width,
171 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +0000172 const void* srcData,
173 size_t rowBytes);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000174 virtual intptr_t getTextureHandle() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000175
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000176 const TexParams& getTexParams() const { return fTexParams; }
177 void setTexParams(const TexParams& texParams) { fTexParams = texParams; }
twiz@google.com0f31ca72011-03-18 17:38:11 +0000178 GrGLuint textureID() const { return fTexIDObj->id(); }
reed@google.comac10a2d2010-12-22 21:39:39 +0000179
twiz@google.com0f31ca72011-03-18 17:38:11 +0000180 GrGLenum uploadFormat() const { return fUploadFormat; }
181 GrGLenum uploadByteCount() const { return fUploadByteCount; }
182 GrGLenum uploadType() const { return fUploadType; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000184 /**
185 * Retrieves the texture width actually allocated in texels.
186 *
187 * @return the width in texels
188 */
189 int allocWidth() const { return fAllocWidth; }
190
191 /**
192 * Retrieves the texture height actually allocated in texels.
193 *
194 * @return the height in texels
195 */
196 int allocHeight() const { return fAllocHeight; }
197
198 /**
199 * @return width() / allocWidth()
200 */
201 GrScalar contentScaleX() const { return fScaleX; }
202
203 /**
204 * @return height() / allocHeight()
205 */
206 GrScalar contentScaleY() const { return fScaleY; }
207
reed@google.comac10a2d2010-12-22 21:39:39 +0000208 // Ganesh assumes texture coordinates have their origin
209 // in the top-left corner of the image. OpenGL, however,
210 // has the origin in the lower-left corner. For content that
211 // is loaded by Ganesh we just push the content "upside down"
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000212 // (by GL's understanding of the world ) in glTex*Image and the
213 // addressing just works out. However, content generated by GL
214 // (FBO or externally imported texture) will be updside down
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 // and it is up to the GrGpuGL derivative to handle y-mirroing.
216 Orientation orientation() const { return fOrientation; }
217
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000218 static const GrGLenum* WrapMode2GLWrap();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000219
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000220protected:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000221
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000222 // overrides of GrTexture
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000223 virtual void onAbandon();
224 virtual void onRelease();
225
reed@google.comac10a2d2010-12-22 21:39:39 +0000226private:
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000227 TexParams fTexParams;
bsalomon@google.com1da07462011-03-10 14:51:57 +0000228 GrGLTexID* fTexIDObj;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000229 GrGLenum fUploadFormat;
230 GrGLenum fUploadByteCount;
231 GrGLenum fUploadType;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000232 int fAllocWidth;
233 int fAllocHeight;
234 // precomputed content / alloc ratios
235 GrScalar fScaleX;
236 GrScalar fScaleY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000237 Orientation fOrientation;
reed@google.comac10a2d2010-12-22 21:39:39 +0000238 GrGpuGL* fGpuGL;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000239
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 typedef GrTexture INHERITED;
241};
242
243#endif