blob: f9a6987772b02f5bf3c233d6e40e7f8d85a2afb7 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
bsalomon@google.com1da07462011-03-10 14:51:57 +00002 Copyright 2011 Google Inc.
reed@google.comac10a2d2010-12-22 21:39:39 +00003
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"
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000023#include "GrGLIRect.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024#include "GrGLTexture.h"
25
26#include "GrGLVertexBuffer.h"
27#include "GrGLIndexBuffer.h"
28
29class GrGpuGL : public GrGpu {
30public:
31 GrGpuGL();
32 virtual ~GrGpuGL();
33
reed@google.comac10a2d2010-12-22 21:39:39 +000034 /**
35 * Gets the struct containing the GL extensions for the context
36 * underlying the GrGpuGL
37 *
38 * @param struct containing extension function pointers
39 */
40 const GrGLExts& extensions() { return fExts; }
41
42protected:
43 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000045 GrVertexLayout fVertexLayout;
46 const GrVertexBuffer* fVertexBuffer;
47 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000048 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000049 } fHWGeometryState;
50
reed@google.com8195f672011-01-12 18:14:28 +000051 DrState fHWDrawState;
52 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000053
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000054 // As flush of GL state proceeds it updates fHDrawState
55 // to reflect the new state. Later parts of the state flush
56 // may perform cascaded changes but cannot refer to fHWDrawState.
57 // These code paths can refer to the dirty flags. Subclass should
58 // call resetDirtyFlags after its flush is complete
59 struct {
60 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000061 int fTextureChangedMask;
62 } fDirtyFlags;
63 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages);
64
65 // clears the dirty flags
66 void resetDirtyFlags();
67
68 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000069 struct {
70 bool fScissorEnabled;
71 GrGLIRect fScissorRect;
72 GrGLIRect fViewportRect;
73 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000074
75 GrGLExts fExts;
76
bsalomon@google.com1c13c962011-02-14 16:51:21 +000077 // GrGpu overrides
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000078 // overrides from GrGpu
79 virtual void resetContext();
80
81 virtual GrTexture* createTextureHelper(const TextureDesc& desc,
82 const void* srcData,
83 size_t rowBytes);
84 virtual GrVertexBuffer* createVertexBufferHelper(uint32_t size,
85 bool dynamic);
86 virtual GrIndexBuffer* createIndexBufferHelper(uint32_t size,
87 bool dynamic);
88
89 virtual GrRenderTarget* createPlatformRenderTargetHelper(
90 intptr_t platformRenderTarget,
91 int stencilBits,
92 int width, int height);
93
94 virtual GrRenderTarget* createRenderTargetFrom3DApiStateHelper();
95
96 virtual void eraseColorHelper(GrColor color);
97
98 virtual void forceRenderTargetFlushHelper();
99
100 virtual bool readPixelsHelper(int left, int top, int width, int height,
101 GrTexture::PixelConfig, void* buffer);
102
bsalomon@google.comffca4002011-02-22 20:34:01 +0000103 virtual void drawIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 uint32_t startVertex,
105 uint32_t startIndex,
106 uint32_t vertexCount,
107 uint32_t indexCount);
bsalomon@google.comffca4002011-02-22 20:34:01 +0000108 virtual void drawNonIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 uint32_t vertexCount,
110 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 virtual void flushScissor(const GrIRect* rect);
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 void eraseStencil(uint32_t value, uint32_t mask);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000113 virtual void eraseStencilClip(const GrIRect& rect);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000114
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000115 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000116 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000117
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000118 // binds appropriate vertex and index buffers, also returns any extra
119 // extra verts or indices to offset by.
120 void setBuffers(bool indexed,
121 int* extraVertexOffset,
122 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000123
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 // flushes state that is common to fixed and programmable GL
125 // dither
126 // line smoothing
127 // blend func
128 // texture binding
129 // sampler state (filtering, tiling)
130 // FBO binding
131 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000132 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134 // adjusts texture matrix to account for orientation, size, and npotness
135 static void AdjustTextureMatrix(const GrGLTexture* texture,
136 GrSamplerState::SampleMode mode,
137 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000138
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000139 // subclass may try to take advantage of identity tex matrices.
140 // This helper determines if matrix will be identity after all
141 // adjustments are applied.
142 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
143 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000145private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 // notify callbacks to update state tracking when related
147 // objects are bound to GL or deleted outside of the class
148 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
149 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
150 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
151 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 void notifyTextureDelete(GrGLTexture* texture);
153 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000154
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000155 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000156
157 void flushRenderTarget();
158 void flushStencil();
159 void resolveTextureRenderTarget(GrGLTexture* texture);
160
161 bool canBeTexture(GrTexture::PixelConfig config,
162 GLenum* internalFormat,
163 GLenum* format,
164 GLenum* type);
165 bool fboInternalFormat(GrTexture::PixelConfig config, GLenum* format);
166
167 friend class GrGLVertexBuffer;
168 friend class GrGLIndexBuffer;
169 friend class GrGLTexture;
170 friend class GrGLRenderTarget;
171
172 bool fHWBlendDisabled;
173
174 GLuint fAASamples[4];
175 enum {
176 kNone_MSFBO = 0,
177 kDesktop_MSFBO,
178 kApple_MSFBO,
179 kIMG_MSFBO
180 } fMSFBOType;
181
182 // Do we have stencil wrap ops.
183 bool fHasStencilWrap;
184
185 // ES requires an extension to support RGBA8 in RenderBufferStorage
186 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000187
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000188 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000189
reed@google.comac10a2d2010-12-22 21:39:39 +0000190 typedef GrGpu INHERITED;
191};
192
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000193#endif