blob: a2630c8b2c644586a574cc5f79ff7e7247b5eb41 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrGpuGL_DEFINED
12#define GrGpuGL_DEFINED
13
14#include "GrGpu.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000015#include "GrGLIndexBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000016#include "GrGLIRect.h"
17#include "GrGLStencilBuffer.h"
18#include "GrGLTexture.h"
19#include "GrGLVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +000021#include "SkString.h"
22
reed@google.comac10a2d2010-12-22 21:39:39 +000023class GrGpuGL : public GrGpu {
24public:
reed@google.comac10a2d2010-12-22 21:39:39 +000025 virtual ~GrGpuGL();
26
bsalomon@google.com0b77d682011-08-19 13:28:54 +000027 const GrGLInterface* glInterface() const { return fGL; }
28 GrGLBinding glBinding() const { return fGLBinding; }
bsalomon@google.comc82b8892011-09-22 14:10:33 +000029 GrGLVersion glVersion() const { return fGLVersion; }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000030
reed@google.comac10a2d2010-12-22 21:39:39 +000031protected:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000032 GrGpuGL(const GrGLInterface* glInterface, GrGLBinding glBinding);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000033
reed@google.comac10a2d2010-12-22 21:39:39 +000034 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 virtual void resetContext();
73
bsalomon@google.comfea37b52011-04-25 15:51:06 +000074 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000075 const void* srcData,
76 size_t rowBytes);
77 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
78 bool dynamic);
79 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
80 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000081 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000082 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
83 int width, int height);
84 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
85 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000086
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000087 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000088
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000089 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000090
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000091 virtual bool onReadPixels(GrRenderTarget* target,
92 int left, int top, int width, int height,
93 GrPixelConfig, void* buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000094
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000095 virtual void onGpuDrawIndexed(GrPrimitiveType type,
96 uint32_t startVertex,
97 uint32_t startIndex,
98 uint32_t vertexCount,
99 uint32_t indexCount);
100 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
101 uint32_t vertexCount,
102 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000104 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000105 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000106 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000107
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000108 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000109 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000111 // binds appropriate vertex and index buffers, also returns any extra
112 // extra verts or indices to offset by.
113 void setBuffers(bool indexed,
114 int* extraVertexOffset,
115 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000116
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 // flushes state that is common to fixed and programmable GL
118 // dither
119 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 // texture binding
121 // sampler state (filtering, tiling)
122 // FBO binding
123 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000124 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000125
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000126 // Subclasses should call this to flush the blend state.
127 // The params should be the final coeffecients to apply
128 // (after any blending optimizations or dual source blending considerations
129 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000130 void flushBlend(GrPrimitiveType type,
131 GrBlendCoeff srcCoeff,
132 GrBlendCoeff dstCoeff);
133
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000134 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000135 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000136 }
137
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000138 // adjusts texture matrix to account for orientation, size, and npotness
139 static void AdjustTextureMatrix(const GrGLTexture* texture,
140 GrSamplerState::SampleMode mode,
141 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000143 // subclass may try to take advantage of identity tex matrices.
144 // This helper determines if matrix will be identity after all
145 // adjustments are applied.
146 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
147 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000148
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000150
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000151private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000152 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
153 // additional caps.
154 void initCaps();
155
156 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000157
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000158 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000159 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000160
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 // notify callbacks to update state tracking when related
162 // objects are bound to GL or deleted outside of the class
163 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
164 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
165 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
166 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 void notifyTextureDelete(GrGLTexture* texture);
168 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000169
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000170 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000171
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000172 // bound is region that may be modified and therefore has to be resolved.
173 // NULL means whole target. Can be an empty rect.
174 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000175 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000176 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000177
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000178 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000179
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000180 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000181 GrGLenum* internalFormat,
182 GrGLenum* format,
183 GrGLenum* type);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000184 // helpers for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000185 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
186 GrGLenum internalFormat,
187 const void* data,
188 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000189
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000190 bool createRenderTargetObjects(int width, int height,
191 GrGLuint texID,
192 GrGLRenderTarget::Desc* desc);
193
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000194 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000195
196 friend class GrGLVertexBuffer;
197 friend class GrGLIndexBuffer;
198 friend class GrGLTexture;
199 friend class GrGLRenderTarget;
200
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000201 // read these once at begining and then never again
202 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000203 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000204
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000205 struct GLCaps {
206 // prealloc space for 8 stencil formats
207 GLCaps() : fStencilFormats(8) {}
208 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
209
210 enum {
211 /**
212 * no support for MSAA FBOs
213 */
214 kNone_MSFBO = 0,
215 /**
216 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
217 */
218 kDesktopARB_MSFBO,
219 /**
220 * earlier GL_EXT_framebuffer* extensions
221 */
222 kDesktopEXT_MSFBO,
223 /**
224 * GL_APPLE_framebuffer_multisample ES extension
225 */
226 kAppleES_MSFBO,
227 } fMSFBOType;
228
229 // TODO: get rid of GrAALevel and use sample cnt directly
230 GrGLuint fAASamples[4];
231
232 // The maximum number of fragment uniform vectors (GLES has min. 16).
233 int fMaxFragmentUniformVectors;
234
235 // ES requires an extension to support RGBA8 in RenderBufferStorage
236 bool fRGBA8Renderbuffer;
237
238 void print() const;
239 } fGLCaps;
240
241
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000242 // we want to clear stencil buffers when they are created. We want to clear
243 // the entire buffer even if it is larger than the color attachment. We
244 // attach it to this fbo with no color attachment to do the initial clear.
245 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000246
reed@google.comac10a2d2010-12-22 21:39:39 +0000247 bool fHWBlendDisabled;
248
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000249 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000250
bsalomon@google.comfe676522011-06-17 18:12:21 +0000251 // we record what stencil format worked last time to hopefully exit early
252 // from our loop that tries stencil formats and calls check fb status.
253 int fLastSuccessfulStencilFmtIdx;
254
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000255 const GrGLInterface* fGL;
256 GrGLBinding fGLBinding;
257
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000258 bool fPrintedCaps;
259
reed@google.comac10a2d2010-12-22 21:39:39 +0000260 typedef GrGpu INHERITED;
261};
262
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000263#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000264