blob: 3b8e6d74af2ca79496f29ec546e4eb1aba94230e [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 {
25public:
reed@google.comac10a2d2010-12-22 21:39:39 +000026 virtual ~GrGpuGL();
27
bsalomon@google.com0b77d682011-08-19 13:28:54 +000028 const GrGLInterface* glInterface() const { return fGL; }
29 GrGLBinding glBinding() const { return fGLBinding; }
bsalomon@google.comc82b8892011-09-22 14:10:33 +000030 GrGLVersion glVersion() const { return fGLVersion; }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000031
bsalomon@google.com0a97be22011-11-08 19:20:57 +000032 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
33 SK_OVERRIDE;
bsalomon@google.coma85449d2011-11-19 02:36:05 +000034 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
35 SK_OVERRIDE;
bsalomon@google.com0a97be22011-11-08 19:20:57 +000036
bsalomon@google.comc4364992011-11-07 15:54:49 +000037 virtual bool readPixelsWillPayForYFlip(
38 GrRenderTarget* renderTarget,
39 int left, int top,
40 int width, int height,
41 GrPixelConfig config,
42 size_t rowBytes) SK_OVERRIDE;
43
reed@google.comac10a2d2010-12-22 21:39:39 +000044protected:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000045 GrGpuGL(const GrGLInterface* glInterface, GrGLBinding glBinding);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000046
bsalomon@google.com85b505b2011-11-07 14:56:51 +000047 struct GLCaps {
48 GLCaps()
49 // make defaults be the most restrictive
bsalomon@google.comc4364992011-11-07 15:54:49 +000050 : fStencilFormats(8) // prealloc space for stencil formats
51 , fMSFBOType(kNone_MSFBO)
bsalomon@google.com85b505b2011-11-07 14:56:51 +000052 , fMaxFragmentUniformVectors(0)
bsalomon@google.com7107fa72011-11-10 14:54:14 +000053 , fRGBA8RenderbufferSupport(false)
54 , fBGRAFormatSupport(false)
55 , fBGRAIsInternalFormat(false)
56 , fTextureSwizzleSupport(false)
57 , fUnpackRowLengthSupport(false)
58 , fPackRowLengthSupport(false) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +000059 memset(fAASamples, 0, sizeof(fAASamples));
60 }
61 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
bsalomon@google.comc4364992011-11-07 15:54:49 +000062
bsalomon@google.com85b505b2011-11-07 14:56:51 +000063 enum {
64 /**
65 * no support for MSAA FBOs
66 */
67 kNone_MSFBO = 0,
68 /**
69 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
70 */
71 kDesktopARB_MSFBO,
72 /**
73 * earlier GL_EXT_framebuffer* extensions
74 */
75 kDesktopEXT_MSFBO,
76 /**
77 * GL_APPLE_framebuffer_multisample ES extension
78 */
79 kAppleES_MSFBO,
80 } fMSFBOType;
bsalomon@google.comc4364992011-11-07 15:54:49 +000081
bsalomon@google.com85b505b2011-11-07 14:56:51 +000082 // TODO: get rid of GrAALevel and use sample cnt directly
83 GrGLuint fAASamples[4];
bsalomon@google.comc4364992011-11-07 15:54:49 +000084
bsalomon@google.com85b505b2011-11-07 14:56:51 +000085 // The maximum number of fragment uniform vectors (GLES has min. 16).
86 int fMaxFragmentUniformVectors;
bsalomon@google.comc4364992011-11-07 15:54:49 +000087
bsalomon@google.com85b505b2011-11-07 14:56:51 +000088 // ES requires an extension to support RGBA8 in RenderBufferStorage
bsalomon@google.com7107fa72011-11-10 14:54:14 +000089 bool fRGBA8RenderbufferSupport;
bsalomon@google.comc4364992011-11-07 15:54:49 +000090
bsalomon@google.com85b505b2011-11-07 14:56:51 +000091 // Is GL_BGRA supported
bsalomon@google.com7107fa72011-11-10 14:54:14 +000092 bool fBGRAFormatSupport;
bsalomon@google.comc4364992011-11-07 15:54:49 +000093
94 // Depending on the ES extensions present the BGRA external format may
95 // correspond either a BGRA or RGBA internalFormat. On desktop GL it is
96 // RGBA
bsalomon@google.com7107fa72011-11-10 14:54:14 +000097 bool fBGRAIsInternalFormat;
bsalomon@google.comc4364992011-11-07 15:54:49 +000098
bsalomon@google.com85b505b2011-11-07 14:56:51 +000099 // GL_ARB_texture_swizzle support
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000100 bool fTextureSwizzleSupport;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000101
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000102 // Is there support for GL_UNPACK_ROW_LENGTH
103 bool fUnpackRowLengthSupport;
104
105 // Is there support for GL_PACK_ROW_LENGTH
106 bool fPackRowLengthSupport;
107
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000108 void print() const;
109 } fGLCaps;
110
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 GrVertexLayout fVertexLayout;
114 const GrVertexBuffer* fVertexBuffer;
115 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000116 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 } fHWGeometryState;
118
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000119 struct AAState {
120 bool fMSAAEnabled;
121 bool fSmoothLineEnabled;
122 } fHWAAState;
123
tomhudson@google.com93813632011-10-27 20:21:16 +0000124 GrDrawState fHWDrawState;
125 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000127 // As flush of GL state proceeds it updates fHDrawState
128 // to reflect the new state. Later parts of the state flush
129 // may perform cascaded changes but cannot refer to fHWDrawState.
130 // These code paths can refer to the dirty flags. Subclass should
131 // call resetDirtyFlags after its flush is complete
132 struct {
133 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134 int fTextureChangedMask;
135 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +0000136 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000137
138 // clears the dirty flags
139 void resetDirtyFlags();
140
141 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000142 struct {
143 bool fScissorEnabled;
144 GrGLIRect fScissorRect;
145 GrGLIRect fViewportRect;
146 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000147
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000148 const GLCaps& glCaps() const { return fGLCaps; }
149
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000150 // GrGpu overrides
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000151 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000152
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000153 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000154 const void* srcData,
155 size_t rowBytes);
156 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
157 bool dynamic);
158 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
159 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000160 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000161 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
162 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000163 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
164 int width, int height);
165 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
166 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000167
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000168 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000169
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000170 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000171
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000172 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000173 int left, int top,
174 int width, int height,
175 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000176 void* buffer,
177 size_t rowBytes,
178 bool invertY) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000179
bsalomon@google.com6f379512011-11-16 20:36:03 +0000180 virtual void onWriteTexturePixels(GrTexture* texture,
181 int left, int top, int width, int height,
182 GrPixelConfig config, const void* buffer,
183 size_t rowBytes) SK_OVERRIDE;
184
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000185 virtual void onGpuDrawIndexed(GrPrimitiveType type,
186 uint32_t startVertex,
187 uint32_t startIndex,
188 uint32_t vertexCount,
189 uint32_t indexCount);
190 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
191 uint32_t vertexCount,
192 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000193 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000194 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000195 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000196 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000197
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000198 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000199 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000200
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000201 // binds appropriate vertex and index buffers, also returns any extra
202 // extra verts or indices to offset by.
203 void setBuffers(bool indexed,
204 int* extraVertexOffset,
205 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000206
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 // flushes state that is common to fixed and programmable GL
208 // dither
209 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 // texture binding
211 // sampler state (filtering, tiling)
212 // FBO binding
213 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000214 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000215
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000216 // Subclasses should call this to flush the blend state.
217 // The params should be the final coeffecients to apply
218 // (after any blending optimizations or dual source blending considerations
219 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000220 void flushBlend(GrPrimitiveType type,
221 GrBlendCoeff srcCoeff,
222 GrBlendCoeff dstCoeff);
223
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000224 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000225 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000226 }
227
bsalomon@google.com99621082011-11-15 16:47:16 +0000228 // adjusts texture matrix to account for orientation
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000229 static void AdjustTextureMatrix(const GrGLTexture* texture,
230 GrSamplerState::SampleMode mode,
231 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000233 // subclass may try to take advantage of identity tex matrices.
234 // This helper determines if matrix will be identity after all
235 // adjustments are applied.
236 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
237 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000239 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000240
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000241private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000242 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
243 // additional caps.
244 void initCaps();
245
246 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000247
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000248 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000249 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000250
reed@google.comac10a2d2010-12-22 21:39:39 +0000251 // notify callbacks to update state tracking when related
252 // objects are bound to GL or deleted outside of the class
253 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
254 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
255 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
256 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 void notifyTextureDelete(GrGLTexture* texture);
258 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000259
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000260 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000261
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000262 // bound is region that may be modified and therefore has to be resolved.
263 // NULL means whole target. Can be an empty rect.
264 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000266 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000267
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000268 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000270 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000271 GrGLenum* internalFormat,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000272 GrGLenum* externalFormat,
273 GrGLenum* externalType);
274 // helper for onCreateTexture and writeTexturePixels
275 void uploadTexData(const GrGLTexture::Desc& desc,
276 int left, int top, int width, int height,
277 GrPixelConfig dataConfig,
278 const void* data,
279 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000280
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000281 bool createRenderTargetObjects(int width, int height,
282 GrGLuint texID,
283 GrGLRenderTarget::Desc* desc);
284
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000285 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000286
287 friend class GrGLVertexBuffer;
288 friend class GrGLIndexBuffer;
289 friend class GrGLTexture;
290 friend class GrGLRenderTarget;
291
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000292 // read these once at begining and then never again
293 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000294 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000295
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000296 // we want to clear stencil buffers when they are created. We want to clear
297 // the entire buffer even if it is larger than the color attachment. We
298 // attach it to this fbo with no color attachment to do the initial clear.
299 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000300
reed@google.comac10a2d2010-12-22 21:39:39 +0000301 bool fHWBlendDisabled;
302
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000303 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000304
bsalomon@google.comfe676522011-06-17 18:12:21 +0000305 // we record what stencil format worked last time to hopefully exit early
306 // from our loop that tries stencil formats and calls check fb status.
307 int fLastSuccessfulStencilFmtIdx;
308
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000309 const GrGLInterface* fGL;
310 GrGLBinding fGLBinding;
311
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000312 bool fPrintedCaps;
313
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 typedef GrGpu INHERITED;
315};
316
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000317#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000318