blob: e46c2ceacb50ed51127a6ec61c624c3dc2600767 [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 GrGpuGL_DEFINED
19#define GrGpuGL_DEFINED
20
21#include "GrGpu.h"
22#include "GrGLConfig.h"
23#include "GrGLTexture.h"
24
25#include "GrGLVertexBuffer.h"
26#include "GrGLIndexBuffer.h"
27
28class GrGpuGL : public GrGpu {
29public:
30 GrGpuGL();
31 virtual ~GrGpuGL();
32
33 // overrides from GrGpu
34 virtual void resetContext();
35
36 virtual GrTexture* createTexture(const TextureDesc& desc,
37 const void* srcData, size_t rowBytes);
38 virtual GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
39 virtual GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
40
41 virtual GrRenderTarget* createPlatformRenderTarget(
42 intptr_t platformRenderTarget,
43 int width, int height);
44
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000045 virtual GrRenderTarget* createRenderTargetFrom3DApiState();
reed@google.comac10a2d2010-12-22 21:39:39 +000046
47 virtual void eraseColor(GrColor color);
48
49 virtual void forceRenderTargetFlush();
50
51 virtual bool readPixels(int left, int top, int width, int height,
52 GrTexture::PixelConfig, void* buffer);
53
54 /**
55 * Gets the struct containing the GL extensions for the context
56 * underlying the GrGpuGL
57 *
58 * @param struct containing extension function pointers
59 */
60 const GrGLExts& extensions() { return fExts; }
61
62protected:
63 struct {
64 const void*
65 fPositionPtr;
66 GrVertexLayout fVertexLayout;
67 const GrVertexBuffer* fVertexBuffer;
68 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000069 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000070 } fHWGeometryState;
71
reed@google.com8195f672011-01-12 18:14:28 +000072 DrState fHWDrawState;
73 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000074
75 virtual void drawIndexedHelper(PrimitiveType type,
76 uint32_t startVertex,
77 uint32_t startIndex,
78 uint32_t vertexCount,
79 uint32_t indexCount);
80
81 virtual void drawNonIndexedHelper(PrimitiveType type,
82 uint32_t vertexCount,
83 uint32_t numVertices);
84
85 virtual void flushScissor(const GrIRect* rect);
86
87 void eraseStencil(uint32_t value, uint32_t mask);
88 virtual void eraseStencilClip();
bsalomon@google.com5782d712011-01-21 21:03:59 +000089
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000090 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +000091
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000092 // binds appropriate vertex and index buffers and returns either the ptr
93 // to client memory or offset into a VB of the first vertex
94 const GLvoid* setBuffersAndGetVertexStart(int vertexStride, int startVertex,
95 int startIndex, int vertexCount,
96 int indexCount);
97
reed@google.comac10a2d2010-12-22 21:39:39 +000098 // flushes state that is common to fixed and programmable GL
99 // dither
100 // line smoothing
101 // blend func
102 // texture binding
103 // sampler state (filtering, tiling)
104 // FBO binding
105 // line width
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000106 bool flushGLStateCommon(PrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000107
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 // set when this class changes the rendertarget.
109 // Subclass should notice at flush time, take appropriate action,
110 // and set false.
111 bool fRenderTargetChanged;
112
113 // set by eraseColor or eraseStencil. Picked up in in flushStencil.
114 bool fWriteMaskChanged;
115
116 // last scissor / viewport scissor state seen by the GL.
117 BoundsState fHWBounds;
118
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 GrGLExts fExts;
120
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000121#if GR_GL_NO_CLIENT_SIDE_ARRAYS
122 void putClientVertexDataInBuffer(const void* vertexData,
123 size_t vertexDataSize);
124 void putClientIndexDataInBuffer(const void* indexData,
125 size_t indexDataSize);
126#endif
127
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000128private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 void resetContextHelper();
130
131 // notify callbacks to update state tracking when related
132 // objects are bound to GL or deleted outside of the class
133 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
134 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
135 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
136 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000137 void notifyTextureDelete(GrGLTexture* texture);
138 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
139 void notifyTextureRemoveRenderTarget(GrGLTexture* texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000140
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000141 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
143 void flushRenderTarget();
144 void flushStencil();
145 void resolveTextureRenderTarget(GrGLTexture* texture);
146
147 bool canBeTexture(GrTexture::PixelConfig config,
148 GLenum* internalFormat,
149 GLenum* format,
150 GLenum* type);
151 bool fboInternalFormat(GrTexture::PixelConfig config, GLenum* format);
152
153 friend class GrGLVertexBuffer;
154 friend class GrGLIndexBuffer;
155 friend class GrGLTexture;
156 friend class GrGLRenderTarget;
157
158 bool fHWBlendDisabled;
159
160 GLuint fAASamples[4];
161 enum {
162 kNone_MSFBO = 0,
163 kDesktop_MSFBO,
164 kApple_MSFBO,
165 kIMG_MSFBO
166 } fMSFBOType;
167
168 // Do we have stencil wrap ops.
169 bool fHasStencilWrap;
170
171 // ES requires an extension to support RGBA8 in RenderBufferStorage
172 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000173
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000174 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000175
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 typedef GrGpu INHERITED;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000177
178#if GR_GL_NO_CLIENT_SIDE_ARRAYS
179 GrGLVertexBuffer* fClientArrayVB;
180 GrGLIndexBuffer* fClientArrayIB;
181 int fOversizeVBDrawCnt;
182 int fOversizeIBDrawCnt;
183#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000184};
185
186bool has_gl_extension(const char* ext);
187void gl_version(int* major, int* minor);
188
189/**
190 * GrGL_RestoreResetRowLength() will reset GL_UNPACK_ROW_LENGTH to 0. We write
191 * this wrapper, since GL_UNPACK_ROW_LENGTH is not available on all GL versions
192 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000193#if GR_SUPPORT_GLDESKTOP
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 static inline void GrGL_RestoreResetRowLength() {
195 GR_GL(PixelStorei(GL_UNPACK_ROW_LENGTH, 0));
196 }
197#else
198 #define GrGL_RestoreResetRowLength()
199#endif
200
reed@google.comac20fb92011-01-12 17:14:53 +0000201/*
202 * Some drivers want the var-int arg to be zero-initialized on input.
203 */
204#define GR_GL_INIT_ZERO 0
205#define GR_GL_GetIntegerv(e, p) \
206 do { \
207 *(p) = GR_GL_INIT_ZERO; \
208 GR_GL(GetIntegerv(e, p)); \
209 } while (0)
210
reed@google.comac10a2d2010-12-22 21:39:39 +0000211#endif
212
213