blob: 7f13ea11a494ce2d22a7f10335960e6de897c7d6 [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
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +000028#include "SkString.h"
29
reed@google.comac10a2d2010-12-22 21:39:39 +000030class GrGpuGL : public GrGpu {
31public:
reed@google.comac10a2d2010-12-22 21:39:39 +000032 virtual ~GrGpuGL();
33
reed@google.comac10a2d2010-12-22 21:39:39 +000034protected:
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000035 GrGpuGL();
36
reed@google.comac10a2d2010-12-22 21:39:39 +000037 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000038 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000039 GrVertexLayout fVertexLayout;
40 const GrVertexBuffer* fVertexBuffer;
41 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000042 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000043 } fHWGeometryState;
44
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000045 struct AAState {
46 bool fMSAAEnabled;
47 bool fSmoothLineEnabled;
48 } fHWAAState;
49
reed@google.com8195f672011-01-12 18:14:28 +000050 DrState fHWDrawState;
51 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000052
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +000053 // read these once at begining and then never again
54 SkString fExtensionString;
55 float fGLVersion;
56
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000057 // As flush of GL state proceeds it updates fHDrawState
58 // to reflect the new state. Later parts of the state flush
59 // may perform cascaded changes but cannot refer to fHWDrawState.
60 // These code paths can refer to the dirty flags. Subclass should
61 // call resetDirtyFlags after its flush is complete
62 struct {
63 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000064 int fTextureChangedMask;
65 } fDirtyFlags;
66 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages);
67
68 // clears the dirty flags
69 void resetDirtyFlags();
70
71 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000072 struct {
73 bool fScissorEnabled;
74 GrGLIRect fScissorRect;
75 GrGLIRect fViewportRect;
76 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000077
bsalomon@google.com1c13c962011-02-14 16:51:21 +000078 // GrGpu overrides
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000079 // overrides from GrGpu
80 virtual void resetContext();
81
bsalomon@google.comfea37b52011-04-25 15:51:06 +000082 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000083 const void* srcData,
84 size_t rowBytes);
85 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
86 bool dynamic);
87 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
88 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000089 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000090 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000091
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000092 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000093
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000094 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000095
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000096 virtual bool onReadPixels(GrRenderTarget* target,
97 int left, int top, int width, int height,
98 GrPixelConfig, void* buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000099
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000100 virtual void onGpuDrawIndexed(GrPrimitiveType type,
101 uint32_t startVertex,
102 uint32_t startIndex,
103 uint32_t vertexCount,
104 uint32_t indexCount);
105 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
106 uint32_t vertexCount,
107 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000109 void clearStencil(uint32_t value, uint32_t mask);
110 virtual void clearStencilClip(const GrIRect& rect);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000111 virtual int getMaxEdges() const;
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
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 // 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.com271cffc2011-05-20 14:13:56 +0000131 // subclass should call this to flush the blend state
132 void flushBlend(GrPrimitiveType type,
133 GrBlendCoeff srcCoeff,
134 GrBlendCoeff dstCoeff);
135
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000136 bool hasExtension(const char* ext) {
137 return has_gl_extension_from_string(ext, fExtensionString.c_str());
138 }
139
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000140 // adjusts texture matrix to account for orientation, size, and npotness
141 static void AdjustTextureMatrix(const GrGLTexture* texture,
142 GrSamplerState::SampleMode mode,
143 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000145 // subclass may try to take advantage of identity tex matrices.
146 // This helper determines if matrix will be identity after all
147 // adjustments are applied.
148 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
149 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000150
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000151 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000152
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000153private:
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000154
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000155 // determines valid stencil formats
156 void setupStencilFormats();
157
reed@google.comac10a2d2010-12-22 21:39:39 +0000158 // notify callbacks to update state tracking when related
159 // objects are bound to GL or deleted outside of the class
160 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
161 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
162 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
163 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 void notifyTextureDelete(GrGLTexture* texture);
165 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000166
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000167 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000168
bsalomon@google.com0650e812011-04-08 18:07:53 +0000169 bool useSmoothLines();
170
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000171 // bound is region that may be modified and therefore has to be resolved.
172 // NULL means whole target. Can be an empty rect.
173 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000175 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000176
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000177 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000179 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000180 GrGLenum* internalFormat,
181 GrGLenum* format,
182 GrGLenum* type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000183
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000184 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
186 friend class GrGLVertexBuffer;
187 friend class GrGLIndexBuffer;
188 friend class GrGLTexture;
189 friend class GrGLRenderTarget;
190
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000191 static const GrGLuint gUNKNOWN_BITCOUNT = ~0;
192
193 struct StencilFormat {
194 GrGLenum fEnum;
195 GrGLuint fBits;
196 bool fPacked;
197 };
198
199 GrTArray<StencilFormat, true> fStencilFormats;
200
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 bool fHWBlendDisabled;
202
twiz@google.com0f31ca72011-03-18 17:38:11 +0000203 GrGLuint fAASamples[4];
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 enum {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000205 kNone_MSFBO = 0, //<! no support for MSAA FBOs
206 kDesktopARB_MSFBO,//<! GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
207 kDesktopEXT_MSFBO,//<! earlier GL_EXT_framebuffer* extensions
208 kAppleES_MSFBO, //<! GL_APPLE_framebuffer_multisample ES extension
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 } fMSFBOType;
210
211 // Do we have stencil wrap ops.
212 bool fHasStencilWrap;
213
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000214 // The maximum number of fragment uniform vectors (GLES has min. 16).
215 int fMaxFragmentUniformVectors;
216
reed@google.comac10a2d2010-12-22 21:39:39 +0000217 // ES requires an extension to support RGBA8 in RenderBufferStorage
218 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000219
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000220 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000221
bsalomon@google.comfe676522011-06-17 18:12:21 +0000222 // we record what stencil format worked last time to hopefully exit early
223 // from our loop that tries stencil formats and calls check fb status.
224 int fLastSuccessfulStencilFmtIdx;
225
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 typedef GrGpu INHERITED;
227};
228
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000229#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000230