blob: 3f10967f76e8207487990745f4bb51bfaba6c335 [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
tomhudson@google.com93813632011-10-27 20:21:16 +000014#include "GrDrawState.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrGpu.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000016#include "GrGLIndexBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000017#include "GrGLIRect.h"
18#include "GrGLStencilBuffer.h"
19#include "GrGLTexture.h"
20#include "GrGLVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +000022#include "SkString.h"
23
reed@google.comac10a2d2010-12-22 21:39:39 +000024class GrGpuGL : public GrGpu {
bsalomon@google.com85b505b2011-11-07 14:56:51 +000025
reed@google.comac10a2d2010-12-22 21:39:39 +000026public:
reed@google.comac10a2d2010-12-22 21:39:39 +000027 virtual ~GrGpuGL();
28
bsalomon@google.com0b77d682011-08-19 13:28:54 +000029 const GrGLInterface* glInterface() const { return fGL; }
30 GrGLBinding glBinding() const { return fGLBinding; }
bsalomon@google.comc82b8892011-09-22 14:10:33 +000031 GrGLVersion glVersion() const { return fGLVersion; }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033protected:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000034 GrGpuGL(const GrGLInterface* glInterface, GrGLBinding glBinding);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000035
bsalomon@google.com85b505b2011-11-07 14:56:51 +000036 struct GLCaps {
37 GLCaps()
38 // make defaults be the most restrictive
39 : fMSFBOType(kNone_MSFBO)
40 , fMaxFragmentUniformVectors(0)
41 , fRGBA8Renderbuffer(false)
42 , fBGRAFormat(false)
43 , fStencilFormats(8) // prealloc space for stencil formats
44 , fTextureSwizzle(false) {
45 memset(fAASamples, 0, sizeof(fAASamples));
46 }
47 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
48
49 enum {
50 /**
51 * no support for MSAA FBOs
52 */
53 kNone_MSFBO = 0,
54 /**
55 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
56 */
57 kDesktopARB_MSFBO,
58 /**
59 * earlier GL_EXT_framebuffer* extensions
60 */
61 kDesktopEXT_MSFBO,
62 /**
63 * GL_APPLE_framebuffer_multisample ES extension
64 */
65 kAppleES_MSFBO,
66 } fMSFBOType;
67
68 // TODO: get rid of GrAALevel and use sample cnt directly
69 GrGLuint fAASamples[4];
70
71 // The maximum number of fragment uniform vectors (GLES has min. 16).
72 int fMaxFragmentUniformVectors;
73
74 // ES requires an extension to support RGBA8 in RenderBufferStorage
75 bool fRGBA8Renderbuffer;
76
77 // Is GL_BGRA supported
78 bool fBGRAFormat;
79
80 // GL_ARB_texture_swizzle support
81 bool fTextureSwizzle;
82
83 void print() const;
84 } fGLCaps;
85
reed@google.comac10a2d2010-12-22 21:39:39 +000086 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000087 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000088 GrVertexLayout fVertexLayout;
89 const GrVertexBuffer* fVertexBuffer;
90 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000091 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000092 } fHWGeometryState;
93
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000094 struct AAState {
95 bool fMSAAEnabled;
96 bool fSmoothLineEnabled;
97 } fHWAAState;
98
tomhudson@google.com93813632011-10-27 20:21:16 +000099 GrDrawState fHWDrawState;
100 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000101
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000102 // As flush of GL state proceeds it updates fHDrawState
103 // to reflect the new state. Later parts of the state flush
104 // may perform cascaded changes but cannot refer to fHWDrawState.
105 // These code paths can refer to the dirty flags. Subclass should
106 // call resetDirtyFlags after its flush is complete
107 struct {
108 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000109 int fTextureChangedMask;
110 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +0000111 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000112
113 // clears the dirty flags
114 void resetDirtyFlags();
115
116 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000117 struct {
118 bool fScissorEnabled;
119 GrGLIRect fScissorRect;
120 GrGLIRect fViewportRect;
121 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000122
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000123 const GLCaps& glCaps() const { return fGLCaps; }
124
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000125 // GrGpu overrides
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000126 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000127
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000128 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000129 const void* srcData,
130 size_t rowBytes);
131 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
132 bool dynamic);
133 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
134 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000135 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000136 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
137 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000138 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
139 int width, int height);
140 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
141 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000142
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000143 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000144
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000145 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000146
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000147 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000148 int left, int top,
149 int width, int height,
150 GrPixelConfig,
151 void* buffer, size_t rowBytes) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000152
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000153 virtual void onGpuDrawIndexed(GrPrimitiveType type,
154 uint32_t startVertex,
155 uint32_t startIndex,
156 uint32_t vertexCount,
157 uint32_t indexCount);
158 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
159 uint32_t vertexCount,
160 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000162 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000163 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000164 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000165
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000166 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000167 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000168
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000169 // binds appropriate vertex and index buffers, also returns any extra
170 // extra verts or indices to offset by.
171 void setBuffers(bool indexed,
172 int* extraVertexOffset,
173 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000174
reed@google.comac10a2d2010-12-22 21:39:39 +0000175 // flushes state that is common to fixed and programmable GL
176 // dither
177 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 // texture binding
179 // sampler state (filtering, tiling)
180 // FBO binding
181 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000182 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000184 // Subclasses should call this to flush the blend state.
185 // The params should be the final coeffecients to apply
186 // (after any blending optimizations or dual source blending considerations
187 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000188 void flushBlend(GrPrimitiveType type,
189 GrBlendCoeff srcCoeff,
190 GrBlendCoeff dstCoeff);
191
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000192 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000193 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000194 }
195
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000196 // adjusts texture matrix to account for orientation, size, and npotness
197 static void AdjustTextureMatrix(const GrGLTexture* texture,
198 GrSamplerState::SampleMode mode,
199 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000200
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000201 // subclass may try to take advantage of identity tex matrices.
202 // This helper determines if matrix will be identity after all
203 // adjustments are applied.
204 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
205 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000206
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000207 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000208
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000209private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
211 // additional caps.
212 void initCaps();
213
214 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000215
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000216 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000217 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000218
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 // notify callbacks to update state tracking when related
220 // objects are bound to GL or deleted outside of the class
221 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
222 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
223 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
224 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 void notifyTextureDelete(GrGLTexture* texture);
226 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000227
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000228 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000230 // bound is region that may be modified and therefore has to be resolved.
231 // NULL means whole target. Can be an empty rect.
232 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000234 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000235
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000236 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000238 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000239 GrGLenum* internalFormat,
240 GrGLenum* format,
241 GrGLenum* type);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000242 // helpers for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000243 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
244 GrGLenum internalFormat,
245 const void* data,
246 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000247
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000248 bool createRenderTargetObjects(int width, int height,
249 GrGLuint texID,
250 GrGLRenderTarget::Desc* desc);
251
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000252 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000253
254 friend class GrGLVertexBuffer;
255 friend class GrGLIndexBuffer;
256 friend class GrGLTexture;
257 friend class GrGLRenderTarget;
258
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000259 // read these once at begining and then never again
260 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000261 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000262
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000263 // we want to clear stencil buffers when they are created. We want to clear
264 // the entire buffer even if it is larger than the color attachment. We
265 // attach it to this fbo with no color attachment to do the initial clear.
266 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000267
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 bool fHWBlendDisabled;
269
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000270 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000271
bsalomon@google.comfe676522011-06-17 18:12:21 +0000272 // we record what stencil format worked last time to hopefully exit early
273 // from our loop that tries stencil formats and calls check fb status.
274 int fLastSuccessfulStencilFmtIdx;
275
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000276 const GrGLInterface* fGL;
277 GrGLBinding fGLBinding;
278
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000279 bool fPrintedCaps;
280
reed@google.comac10a2d2010-12-22 21:39:39 +0000281 typedef GrGpu INHERITED;
282};
283
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000284#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000285