blob: 08edb27a7e31398ea7fed2c8cabbb9c6ee887217 [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"
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000022#include "GrGLIRect.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023#include "GrGLTexture.h"
24
25#include "GrGLVertexBuffer.h"
26#include "GrGLIndexBuffer.h"
27
28class GrGpuGL : public GrGpu {
29public:
30 GrGpuGL();
31 virtual ~GrGpuGL();
32
reed@google.comac10a2d2010-12-22 21:39:39 +000033protected:
34 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000035 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000036 GrVertexLayout fVertexLayout;
37 const GrVertexBuffer* fVertexBuffer;
38 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000039 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000040 } fHWGeometryState;
41
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000042 struct AAState {
43 bool fMSAAEnabled;
44 bool fSmoothLineEnabled;
45 } fHWAAState;
46
reed@google.com8195f672011-01-12 18:14:28 +000047 DrState fHWDrawState;
48 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000049
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000050 // As flush of GL state proceeds it updates fHDrawState
51 // to reflect the new state. Later parts of the state flush
52 // may perform cascaded changes but cannot refer to fHWDrawState.
53 // These code paths can refer to the dirty flags. Subclass should
54 // call resetDirtyFlags after its flush is complete
55 struct {
56 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000057 int fTextureChangedMask;
58 } fDirtyFlags;
59 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages);
60
61 // clears the dirty flags
62 void resetDirtyFlags();
63
64 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000065 struct {
66 bool fScissorEnabled;
67 GrGLIRect fScissorRect;
68 GrGLIRect fViewportRect;
69 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000070
bsalomon@google.com1c13c962011-02-14 16:51:21 +000071 // GrGpu overrides
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000072 // overrides from GrGpu
73 virtual void resetContext();
74
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000075 virtual GrTexture* onCreateTexture(const TextureDesc& desc,
76 const void* srcData,
77 size_t rowBytes);
78 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
79 bool dynamic);
80 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
81 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000082 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000083 virtual GrRenderTarget* onCreatePlatformRenderTarget(
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000084 intptr_t platformRenderTarget,
85 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000086 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000087 int width, int height);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000088 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000089
bsalomon@google.com398109c2011-04-14 18:40:27 +000090 virtual void onClear(GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000091
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000092 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000093
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000094 virtual bool onReadPixels(GrRenderTarget* target,
95 int left, int top, int width, int height,
96 GrPixelConfig, void* buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000097
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000098 virtual void onDrawIndexed(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +000099 uint32_t startVertex,
100 uint32_t startIndex,
101 uint32_t vertexCount,
102 uint32_t indexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000103 virtual void onDrawNonIndexed(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 uint32_t vertexCount,
105 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000107 void clearStencil(uint32_t value, uint32_t mask);
108 virtual void clearStencilClip(const GrIRect& rect);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000109
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000110 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000111 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000112
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000113 // binds appropriate vertex and index buffers, also returns any extra
114 // extra verts or indices to offset by.
115 void setBuffers(bool indexed,
116 int* extraVertexOffset,
117 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000118
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 // flushes state that is common to fixed and programmable GL
120 // dither
121 // line smoothing
122 // blend func
123 // texture binding
124 // sampler state (filtering, tiling)
125 // FBO binding
126 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000127 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000128
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000129 // adjusts texture matrix to account for orientation, size, and npotness
130 static void AdjustTextureMatrix(const GrGLTexture* texture,
131 GrSamplerState::SampleMode mode,
132 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134 // subclass may try to take advantage of identity tex matrices.
135 // This helper determines if matrix will be identity after all
136 // adjustments are applied.
137 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
138 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
bsalomon@google.com080773c2011-03-15 19:09:25 +0000140 static bool BlendCoefReferencesConstant(GrBlendCoeff coeff);
141
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000142private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000143 // notify callbacks to update state tracking when related
144 // objects are bound to GL or deleted outside of the class
145 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
146 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
147 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
148 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 void notifyTextureDelete(GrGLTexture* texture);
150 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000151
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000152 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000153
bsalomon@google.com0650e812011-04-08 18:07:53 +0000154 bool useSmoothLines();
155
reed@google.comac10a2d2010-12-22 21:39:39 +0000156 void flushRenderTarget();
157 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000158 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000159 void flushBlend(GrPrimitiveType type);
160
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000161 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000162
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000163 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000164 GrGLenum* internalFormat,
165 GrGLenum* format,
166 GrGLenum* type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000167
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000168 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000169
170 friend class GrGLVertexBuffer;
171 friend class GrGLIndexBuffer;
172 friend class GrGLTexture;
173 friend class GrGLRenderTarget;
174
175 bool fHWBlendDisabled;
176
twiz@google.com0f31ca72011-03-18 17:38:11 +0000177 GrGLuint fAASamples[4];
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 enum {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000179 kNone_MSFBO = 0, //<! no support for MSAA FBOs
180 kDesktopARB_MSFBO,//<! GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
181 kDesktopEXT_MSFBO,//<! earlier GL_EXT_framebuffer* extensions
182 kAppleES_MSFBO, //<! GL_APPLE_framebuffer_multisample ES extension
reed@google.comac10a2d2010-12-22 21:39:39 +0000183 } fMSFBOType;
184
185 // Do we have stencil wrap ops.
186 bool fHasStencilWrap;
187
188 // ES requires an extension to support RGBA8 in RenderBufferStorage
189 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000190
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000191 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000192
reed@google.comac10a2d2010-12-22 21:39:39 +0000193 typedef GrGpu INHERITED;
194};
195
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000196#endif