blob: d48d69eccb18e30b60e713501b53e3a4e5418645 [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:
reed@google.comac10a2d2010-12-22 21:39:39 +000030 virtual ~GrGpuGL();
31
reed@google.comac10a2d2010-12-22 21:39:39 +000032protected:
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000033 GrGpuGL();
34
reed@google.comac10a2d2010-12-22 21:39:39 +000035 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000036 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000037 GrVertexLayout fVertexLayout;
38 const GrVertexBuffer* fVertexBuffer;
39 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000040 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000041 } fHWGeometryState;
42
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000043 struct AAState {
44 bool fMSAAEnabled;
45 bool fSmoothLineEnabled;
46 } fHWAAState;
47
reed@google.com8195f672011-01-12 18:14:28 +000048 DrState fHWDrawState;
49 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000050
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000051 // As flush of GL state proceeds it updates fHDrawState
52 // to reflect the new state. Later parts of the state flush
53 // may perform cascaded changes but cannot refer to fHWDrawState.
54 // These code paths can refer to the dirty flags. Subclass should
55 // call resetDirtyFlags after its flush is complete
56 struct {
57 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000058 int fTextureChangedMask;
59 } fDirtyFlags;
60 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages);
61
62 // clears the dirty flags
63 void resetDirtyFlags();
64
65 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000066 struct {
67 bool fScissorEnabled;
68 GrGLIRect fScissorRect;
69 GrGLIRect fViewportRect;
70 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000071
bsalomon@google.com1c13c962011-02-14 16:51:21 +000072 // GrGpu overrides
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000073 // overrides from GrGpu
74 virtual void resetContext();
75
bsalomon@google.comfea37b52011-04-25 15:51:06 +000076 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000077 const void* srcData,
78 size_t rowBytes);
79 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
80 bool dynamic);
81 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
82 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000083 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000084 virtual GrRenderTarget* onCreatePlatformRenderTarget(
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000085 intptr_t platformRenderTarget,
86 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000087 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000088 int width, int height);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000089 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000090
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000091 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000092
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000093 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000094
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000095 virtual bool onReadPixels(GrRenderTarget* target,
96 int left, int top, int width, int height,
97 GrPixelConfig, void* buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000098
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000099 virtual void onDrawIndexed(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 uint32_t startVertex,
101 uint32_t startIndex,
102 uint32_t vertexCount,
103 uint32_t indexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000104 virtual void onDrawNonIndexed(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 uint32_t vertexCount,
106 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000108 void clearStencil(uint32_t value, uint32_t mask);
109 virtual void clearStencilClip(const GrIRect& rect);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000110 virtual int getMaxEdges() const;
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.com080773c2011-03-15 19:09:25 +0000142 static bool BlendCoefReferencesConstant(GrBlendCoeff coeff);
143
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000144private:
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000145
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
bsalomon@google.com0650e812011-04-08 18:07:53 +0000157 bool useSmoothLines();
158
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000159 // bound is region that may be modified and therefore has to be resolved.
160 // NULL means whole target. Can be an empty rect.
161 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000163 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000164 void flushBlend(GrPrimitiveType type);
165
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000166 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000167
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000168 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000169 GrGLenum* internalFormat,
170 GrGLenum* format,
171 GrGLenum* type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000172
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000173 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000174
175 friend class GrGLVertexBuffer;
176 friend class GrGLIndexBuffer;
177 friend class GrGLTexture;
178 friend class GrGLRenderTarget;
179
180 bool fHWBlendDisabled;
181
twiz@google.com0f31ca72011-03-18 17:38:11 +0000182 GrGLuint fAASamples[4];
reed@google.comac10a2d2010-12-22 21:39:39 +0000183 enum {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000184 kNone_MSFBO = 0, //<! no support for MSAA FBOs
185 kDesktopARB_MSFBO,//<! GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
186 kDesktopEXT_MSFBO,//<! earlier GL_EXT_framebuffer* extensions
187 kAppleES_MSFBO, //<! GL_APPLE_framebuffer_multisample ES extension
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 } fMSFBOType;
189
190 // Do we have stencil wrap ops.
191 bool fHasStencilWrap;
192
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000193 // The maximum number of fragment uniform vectors (GLES has min. 16).
194 int fMaxFragmentUniformVectors;
195
reed@google.comac10a2d2010-12-22 21:39:39 +0000196 // ES requires an extension to support RGBA8 in RenderBufferStorage
197 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000198
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000199 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000200
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 typedef GrGpu INHERITED;
202};
203
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000204#endif