blob: a2905c5d30df5c4b2743dbe6b8bba0a8221513e7 [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"
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
34 // overrides from GrGpu
35 virtual void resetContext();
36
37 virtual GrTexture* createTexture(const TextureDesc& desc,
38 const void* srcData, size_t rowBytes);
39 virtual GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
40 virtual GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
41
42 virtual GrRenderTarget* createPlatformRenderTarget(
43 intptr_t platformRenderTarget,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000044 int stencilBits,
reed@google.comac10a2d2010-12-22 21:39:39 +000045 int width, int height);
46
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000047 virtual GrRenderTarget* createRenderTargetFrom3DApiState();
reed@google.comac10a2d2010-12-22 21:39:39 +000048
49 virtual void eraseColor(GrColor color);
50
51 virtual void forceRenderTargetFlush();
52
53 virtual bool readPixels(int left, int top, int width, int height,
54 GrTexture::PixelConfig, void* buffer);
55
56 /**
57 * Gets the struct containing the GL extensions for the context
58 * underlying the GrGpuGL
59 *
60 * @param struct containing extension function pointers
61 */
62 const GrGLExts& extensions() { return fExts; }
63
64protected:
65 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000066 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000067 GrVertexLayout fVertexLayout;
68 const GrVertexBuffer* fVertexBuffer;
69 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000070 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000071 } fHWGeometryState;
72
reed@google.com8195f672011-01-12 18:14:28 +000073 DrState fHWDrawState;
74 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000075
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000076 // As flush of GL state proceeds it updates fHDrawState
77 // to reflect the new state. Later parts of the state flush
78 // may perform cascaded changes but cannot refer to fHWDrawState.
79 // These code paths can refer to the dirty flags. Subclass should
80 // call resetDirtyFlags after its flush is complete
81 struct {
82 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000083 int fTextureChangedMask;
84 } fDirtyFlags;
85 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages);
86
87 // clears the dirty flags
88 void resetDirtyFlags();
89
90 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000091 struct {
92 bool fScissorEnabled;
93 GrGLIRect fScissorRect;
94 GrGLIRect fViewportRect;
95 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000096
97 GrGLExts fExts;
98
bsalomon@google.com1c13c962011-02-14 16:51:21 +000099 // GrGpu overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000100 virtual void drawIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 uint32_t startVertex,
102 uint32_t startIndex,
103 uint32_t vertexCount,
104 uint32_t indexCount);
bsalomon@google.comffca4002011-02-22 20:34:01 +0000105 virtual void drawNonIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 uint32_t vertexCount,
107 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 virtual void flushScissor(const GrIRect* rect);
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 void eraseStencil(uint32_t value, uint32_t mask);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000110 virtual void eraseStencilClip(const GrIRect& rect);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000111
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000113 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000115 // binds appropriate vertex and index buffers, also returns any extra
116 // extra verts or indices to offset by.
117 void setBuffers(bool indexed,
118 int* extraVertexOffset,
119 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000120
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 // flushes state that is common to fixed and programmable GL
122 // dither
123 // line smoothing
124 // blend func
125 // texture binding
126 // sampler state (filtering, tiling)
127 // FBO binding
128 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000129 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000131 // adjusts texture matrix to account for orientation, size, and npotness
132 static void AdjustTextureMatrix(const GrGLTexture* texture,
133 GrSamplerState::SampleMode mode,
134 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000135
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000136 // subclass may try to take advantage of identity tex matrices.
137 // This helper determines if matrix will be identity after all
138 // adjustments are applied.
139 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
140 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000141
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000142private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000143 void resetContextHelper();
144
145 // notify callbacks to update state tracking when related
146 // objects are bound to GL or deleted outside of the class
147 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
148 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
149 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
150 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000151 void notifyTextureDelete(GrGLTexture* texture);
152 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
153 void notifyTextureRemoveRenderTarget(GrGLTexture* texture);
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.com8895a7a2011-02-18 16:09:55 +0000193#endif