blob: ada31512f5243f8e7d90d8c36f5f09fbd21b5864 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
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
21#include "GrGLConfig.h"
22#include "GrGpu.h"
23#include "GrTexture.h"
24#include "GrRect.h"
25
26class GrGpuGL;
27class GrGLTexture;
28
29class GrGLRenderTarget : public GrRenderTarget {
30protected:
31
32 struct GLRenderTargetIDs {
33 GLuint fRTFBOID;
34 GLuint fTexFBOID;
35 GLuint fStencilRenderbufferID;
36 GLuint fMSColorRenderbufferID;
37 bool fOwnIDs;
38 };
39
40 GrGLRenderTarget(const GLRenderTargetIDs& ids,
41 const GrIRect& fViewport,
42 GrGLTexture* texture,
43 GrGpuGL* gl);
44
45 void setViewport(const GrIRect& rect) { GrAssert(rect.height() <= 0);
46 fViewport = rect;}
47
48 virtual uint32_t width() const { return fViewport.width(); }
49 virtual uint32_t height() const { return -fViewport.height(); }
50
51public:
52 virtual ~GrGLRenderTarget();
53
54 bool resolveable() const { return fRTFBOID != fTexFBOID; }
55 bool needsResolve() const { return fNeedsResolve; }
56 void setDirty(bool dirty) { fNeedsResolve = resolveable() && dirty; }
57
58 GLuint renderFBOID() const { return fRTFBOID; }
59 GLuint textureFBOID() const { return fTexFBOID; }
60
61 const GrIRect& viewport() const { return fViewport; }
62 void abandon();
63
64private:
65 GrGpuGL* fGL;
66 GLuint fRTFBOID;
67 GLuint fTexFBOID;
68 GLuint fStencilRenderbufferID;
69 GLuint fMSColorRenderbufferID;
70
71 // Should this object delete IDs when it is destroyed or does someone
72 // else own them.
73 bool fOwnIDs;
74
75 // If there separate Texture and RenderTarget FBO IDs then the rendertarget
76 // must be resolved to the texture FBO before it is used as a texture.
77 bool fNeedsResolve;
78
79 // when we switch to this rendertarget we want to set the viewport to
80 // only render to to content area (as opposed to the whole allocation) and
81 // we want the rendering to be at top left (GL has origin in bottom left)
82 GrIRect fViewport;
83
84 friend class GrGpuGL;
85 friend class GrGLTexture;
86
87 typedef GrRenderTarget INHERITED;
88};
89
90class GrGLTexture : public GrTexture {
91public:
92 enum Orientation {
93 kBottomUp_Orientation,
94 kTopDown_Orientation,
95 };
96
97protected:
98 struct GLTextureDesc {
99 uint32_t fContentWidth;
100 uint32_t fContentHeight;
101 uint32_t fAllocWidth;
102 uint32_t fAllocHeight;
103 PixelConfig fFormat;
104 GLuint fTextureID;
105 GLenum fUploadFormat;
106 GLenum fUploadByteCount;
107 GLenum fUploadType;
108 Orientation fOrientation;
109 };
110 typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs;
111 GrGLTexture(const GLTextureDesc& textureDesc,
112 const GLRenderTargetIDs& rtIDs,
113 GrGpuGL* gl);
114
115public:
116 virtual ~GrGLTexture();
117
118 // overloads of GrTexture
119 virtual void abandon();
120 virtual bool isRenderTarget() const;
121 virtual GrRenderTarget* asRenderTarget();
122 virtual void removeRenderTarget();
123 virtual void uploadTextureData(uint32_t x,
124 uint32_t y,
125 uint32_t width,
126 uint32_t height,
127 const void* srcData);
128 virtual intptr_t getTextureHandle();
129
130 const GrSamplerState& samplerState() const { return fSamplerState; }
131 void setSamplerState(const GrSamplerState& state)
132 { fSamplerState = state; }
133 GLuint textureID() const { return fTextureID; }
134
135 GLenum uploadFormat() const { return fUploadFormat; }
136 GLenum uploadByteCount() const { return fUploadByteCount; }
137 GLenum uploadType() const { return fUploadType; }
138
139 // Ganesh assumes texture coordinates have their origin
140 // in the top-left corner of the image. OpenGL, however,
141 // has the origin in the lower-left corner. For content that
142 // is loaded by Ganesh we just push the content "upside down"
143 // (by GL's understanding of the world ) in glTex*Image and the
144 // addressing just works out. However, content generated by GL
145 // (FBO or externally imported texture) will be updside down
146 // and it is up to the GrGpuGL derivative to handle y-mirroing.
147 Orientation orientation() const { return fOrientation; }
148
149private:
150 GrSamplerState fSamplerState;
151 GLuint fTextureID;
152 GLenum fUploadFormat;
153 GLenum fUploadByteCount;
154 GLenum fUploadType;
155 Orientation fOrientation;
156 GrGLRenderTarget* fRenderTarget;
157 GrGpuGL* fGpuGL;
158
159 static const GLenum gWrapMode2GLWrap[];
160
161 friend class GrGpuGL;
162
163 typedef GrTexture INHERITED;
164};
165
166#endif