blob: d89b8d54d7a14539bca0ef84416c1790dda5eac8 [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 {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000064 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000065 GrVertexLayout fVertexLayout;
66 const GrVertexBuffer* fVertexBuffer;
67 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000068 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000069 } fHWGeometryState;
70
reed@google.com8195f672011-01-12 18:14:28 +000071 DrState fHWDrawState;
72 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000073
bsalomon@google.com1c13c962011-02-14 16:51:21 +000074 // GrGpu overrides
reed@google.comac10a2d2010-12-22 21:39:39 +000075 virtual void drawIndexedHelper(PrimitiveType type,
76 uint32_t startVertex,
77 uint32_t startIndex,
78 uint32_t vertexCount,
79 uint32_t indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +000080 virtual void drawNonIndexedHelper(PrimitiveType type,
81 uint32_t vertexCount,
82 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +000083 virtual void flushScissor(const GrIRect* rect);
reed@google.comac10a2d2010-12-22 21:39:39 +000084 void eraseStencil(uint32_t value, uint32_t mask);
85 virtual void eraseStencilClip();
bsalomon@google.com5782d712011-01-21 21:03:59 +000086
bsalomon@google.com1c13c962011-02-14 16:51:21 +000087 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000088 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +000089
bsalomon@google.com1c13c962011-02-14 16:51:21 +000090 // binds appropriate vertex and index buffers, also returns any extra
91 // extra verts or indices to offset by.
92 void setBuffers(bool indexed,
93 int* extraVertexOffset,
94 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000095
reed@google.comac10a2d2010-12-22 21:39:39 +000096 // flushes state that is common to fixed and programmable GL
97 // dither
98 // line smoothing
99 // blend func
100 // texture binding
101 // sampler state (filtering, tiling)
102 // FBO binding
103 // line width
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000104 bool flushGLStateCommon(PrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000105
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 // set when this class changes the rendertarget.
107 // Subclass should notice at flush time, take appropriate action,
108 // and set false.
109 bool fRenderTargetChanged;
110
111 // set by eraseColor or eraseStencil. Picked up in in flushStencil.
112 bool fWriteMaskChanged;
113
114 // last scissor / viewport scissor state seen by the GL.
115 BoundsState fHWBounds;
116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 GrGLExts fExts;
118
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000119private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 void resetContextHelper();
121
122 // notify callbacks to update state tracking when related
123 // objects are bound to GL or deleted outside of the class
124 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
125 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
126 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
127 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 void notifyTextureDelete(GrGLTexture* texture);
129 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
130 void notifyTextureRemoveRenderTarget(GrGLTexture* texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000131
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000132 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
134 void flushRenderTarget();
135 void flushStencil();
136 void resolveTextureRenderTarget(GrGLTexture* texture);
137
138 bool canBeTexture(GrTexture::PixelConfig config,
139 GLenum* internalFormat,
140 GLenum* format,
141 GLenum* type);
142 bool fboInternalFormat(GrTexture::PixelConfig config, GLenum* format);
143
144 friend class GrGLVertexBuffer;
145 friend class GrGLIndexBuffer;
146 friend class GrGLTexture;
147 friend class GrGLRenderTarget;
148
149 bool fHWBlendDisabled;
150
151 GLuint fAASamples[4];
152 enum {
153 kNone_MSFBO = 0,
154 kDesktop_MSFBO,
155 kApple_MSFBO,
156 kIMG_MSFBO
157 } fMSFBOType;
158
159 // Do we have stencil wrap ops.
160 bool fHasStencilWrap;
161
162 // ES requires an extension to support RGBA8 in RenderBufferStorage
163 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000164
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000165 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000166
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 typedef GrGpu INHERITED;
168};
169
170bool has_gl_extension(const char* ext);
171void gl_version(int* major, int* minor);
172
173/**
174 * GrGL_RestoreResetRowLength() will reset GL_UNPACK_ROW_LENGTH to 0. We write
175 * this wrapper, since GL_UNPACK_ROW_LENGTH is not available on all GL versions
176 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000177#if GR_SUPPORT_GLDESKTOP
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 static inline void GrGL_RestoreResetRowLength() {
179 GR_GL(PixelStorei(GL_UNPACK_ROW_LENGTH, 0));
180 }
181#else
182 #define GrGL_RestoreResetRowLength()
183#endif
184
reed@google.comac20fb92011-01-12 17:14:53 +0000185/*
186 * Some drivers want the var-int arg to be zero-initialized on input.
187 */
188#define GR_GL_INIT_ZERO 0
189#define GR_GL_GetIntegerv(e, p) \
190 do { \
191 *(p) = GR_GL_INIT_ZERO; \
192 GR_GL(GetIntegerv(e, p)); \
193 } while (0)
194
reed@google.comac10a2d2010-12-22 21:39:39 +0000195#endif
196
197