blob: f6216c5c6e6710d41763caaf1844d248df63be64 [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;
83 bool fWriteMaskChanged : 1;
84 int fTextureChangedMask;
85 } fDirtyFlags;
86 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages);
87
88 // clears the dirty flags
89 void resetDirtyFlags();
90
91 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000092 struct {
93 bool fScissorEnabled;
94 GrGLIRect fScissorRect;
95 GrGLIRect fViewportRect;
96 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000097
98 GrGLExts fExts;
99
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000100 // GrGpu overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000101 virtual void drawIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 uint32_t startVertex,
103 uint32_t startIndex,
104 uint32_t vertexCount,
105 uint32_t indexCount);
bsalomon@google.comffca4002011-02-22 20:34:01 +0000106 virtual void drawNonIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 uint32_t vertexCount,
108 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 virtual void flushScissor(const GrIRect* rect);
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 void eraseStencil(uint32_t value, uint32_t mask);
111 virtual void eraseStencilClip();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000112
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000113 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000114 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000115
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000116 // binds appropriate vertex and index buffers, also returns any extra
117 // extra verts or indices to offset by.
118 void setBuffers(bool indexed,
119 int* extraVertexOffset,
120 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000121
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 // flushes state that is common to fixed and programmable GL
123 // dither
124 // line smoothing
125 // blend func
126 // texture binding
127 // sampler state (filtering, tiling)
128 // FBO binding
129 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000130 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000132 // adjusts texture matrix to account for orientation, size, and npotness
133 static void AdjustTextureMatrix(const GrGLTexture* texture,
134 GrSamplerState::SampleMode mode,
135 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000137 // subclass may try to take advantage of identity tex matrices.
138 // This helper determines if matrix will be identity after all
139 // adjustments are applied.
140 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
141 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000143private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 void resetContextHelper();
145
146 // 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);
154 void notifyTextureRemoveRenderTarget(GrGLTexture* texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000155
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000156 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000157
158 void flushRenderTarget();
159 void flushStencil();
160 void resolveTextureRenderTarget(GrGLTexture* texture);
161
162 bool canBeTexture(GrTexture::PixelConfig config,
163 GLenum* internalFormat,
164 GLenum* format,
165 GLenum* type);
166 bool fboInternalFormat(GrTexture::PixelConfig config, GLenum* format);
167
168 friend class GrGLVertexBuffer;
169 friend class GrGLIndexBuffer;
170 friend class GrGLTexture;
171 friend class GrGLRenderTarget;
172
173 bool fHWBlendDisabled;
174
175 GLuint fAASamples[4];
176 enum {
177 kNone_MSFBO = 0,
178 kDesktop_MSFBO,
179 kApple_MSFBO,
180 kIMG_MSFBO
181 } fMSFBOType;
182
183 // Do we have stencil wrap ops.
184 bool fHasStencilWrap;
185
186 // ES requires an extension to support RGBA8 in RenderBufferStorage
187 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000188
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000189 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000190
reed@google.comac10a2d2010-12-22 21:39:39 +0000191 typedef GrGpu INHERITED;
192};
193
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000194#endif