blob: a2ccc12c56a2db10256675d179a031cb48634eef [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.comc6cf7232011-02-17 16:43:10 +000074 // As flush of GL state proceeds it updates fHDrawState
75 // to reflect the new state. Later parts of the state flush
76 // may perform cascaded changes but cannot refer to fHWDrawState.
77 // These code paths can refer to the dirty flags. Subclass should
78 // call resetDirtyFlags after its flush is complete
79 struct {
80 bool fRenderTargetChanged : 1;
81 bool fWriteMaskChanged : 1;
82 int fTextureChangedMask;
83 } fDirtyFlags;
84 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages);
85
86 // clears the dirty flags
87 void resetDirtyFlags();
88
89 // last scissor / viewport scissor state seen by the GL.
90 BoundsState fHWBounds;
91
92 GrGLExts fExts;
93
bsalomon@google.com1c13c962011-02-14 16:51:21 +000094 // GrGpu overrides
reed@google.comac10a2d2010-12-22 21:39:39 +000095 virtual void drawIndexedHelper(PrimitiveType type,
96 uint32_t startVertex,
97 uint32_t startIndex,
98 uint32_t vertexCount,
99 uint32_t indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 virtual void drawNonIndexedHelper(PrimitiveType type,
101 uint32_t vertexCount,
102 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 virtual void flushScissor(const GrIRect* rect);
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 void eraseStencil(uint32_t value, uint32_t mask);
105 virtual void eraseStencilClip();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000106
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000107 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000108 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000109
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000110 // binds appropriate vertex and index buffers, also returns any extra
111 // extra verts or indices to offset by.
112 void setBuffers(bool indexed,
113 int* extraVertexOffset,
114 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 // flushes state that is common to fixed and programmable GL
117 // dither
118 // line smoothing
119 // blend func
120 // texture binding
121 // sampler state (filtering, tiling)
122 // FBO binding
123 // line width
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000124 bool flushGLStateCommon(PrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000125
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000126 // adjusts texture matrix to account for orientation, size, and npotness
127 static void AdjustTextureMatrix(const GrGLTexture* texture,
128 GrSamplerState::SampleMode mode,
129 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000131 // subclass may try to take advantage of identity tex matrices.
132 // This helper determines if matrix will be identity after all
133 // adjustments are applied.
134 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
135 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000137private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000138 void resetContextHelper();
139
140 // notify callbacks to update state tracking when related
141 // objects are bound to GL or deleted outside of the class
142 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
143 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
144 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
145 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 void notifyTextureDelete(GrGLTexture* texture);
147 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
148 void notifyTextureRemoveRenderTarget(GrGLTexture* texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000149
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000150 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
152 void flushRenderTarget();
153 void flushStencil();
154 void resolveTextureRenderTarget(GrGLTexture* texture);
155
156 bool canBeTexture(GrTexture::PixelConfig config,
157 GLenum* internalFormat,
158 GLenum* format,
159 GLenum* type);
160 bool fboInternalFormat(GrTexture::PixelConfig config, GLenum* format);
161
162 friend class GrGLVertexBuffer;
163 friend class GrGLIndexBuffer;
164 friend class GrGLTexture;
165 friend class GrGLRenderTarget;
166
167 bool fHWBlendDisabled;
168
169 GLuint fAASamples[4];
170 enum {
171 kNone_MSFBO = 0,
172 kDesktop_MSFBO,
173 kApple_MSFBO,
174 kIMG_MSFBO
175 } fMSFBOType;
176
177 // Do we have stencil wrap ops.
178 bool fHasStencilWrap;
179
180 // ES requires an extension to support RGBA8 in RenderBufferStorage
181 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000182
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000183 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000184
reed@google.comac10a2d2010-12-22 21:39:39 +0000185 typedef GrGpu INHERITED;
186};
187
188bool has_gl_extension(const char* ext);
189void gl_version(int* major, int* minor);
190
191/**
192 * GrGL_RestoreResetRowLength() will reset GL_UNPACK_ROW_LENGTH to 0. We write
193 * this wrapper, since GL_UNPACK_ROW_LENGTH is not available on all GL versions
194 */
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000195#if GR_SUPPORT_GLDESKTOP
reed@google.comac10a2d2010-12-22 21:39:39 +0000196 static inline void GrGL_RestoreResetRowLength() {
197 GR_GL(PixelStorei(GL_UNPACK_ROW_LENGTH, 0));
198 }
199#else
200 #define GrGL_RestoreResetRowLength()
201#endif
202
reed@google.comac20fb92011-01-12 17:14:53 +0000203/*
204 * Some drivers want the var-int arg to be zero-initialized on input.
205 */
206#define GR_GL_INIT_ZERO 0
207#define GR_GL_GetIntegerv(e, p) \
208 do { \
209 *(p) = GR_GL_INIT_ZERO; \
210 GR_GL(GetIntegerv(e, p)); \
211 } while (0)
212
reed@google.comac10a2d2010-12-22 21:39:39 +0000213#endif
214
215