blob: 57a4d97e781e6cc708f0fedbddb9104d9bd8a378 [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.com56d11e02011-11-30 19:59:08 +000032 // GrGpu overrides
bsalomon@google.com0a97be22011-11-08 19:20:57 +000033 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
bsalomon@google.com56d11e02011-11-30 19:59:08 +000034 const SK_OVERRIDE;
bsalomon@google.coma85449d2011-11-19 02:36:05 +000035 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
bsalomon@google.com56d11e02011-11-30 19:59:08 +000036 const SK_OVERRIDE;
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,
bsalomon@google.com56d11e02011-11-30 19:59:08 +000042 size_t rowBytes) const SK_OVERRIDE;
43 virtual bool fullReadPixelsIsFasterThanPartial() const SK_OVERRIDE;
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)
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +000058 , fUnpackFlipYSupport(false)
bsalomon@google.com56d11e02011-11-30 19:59:08 +000059 , fPackRowLengthSupport(false)
60 , fPackFlipYSupport(false) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +000061 memset(fAASamples, 0, sizeof(fAASamples));
62 }
63 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
bsalomon@google.comc4364992011-11-07 15:54:49 +000064
bsalomon@google.com85b505b2011-11-07 14:56:51 +000065 enum {
66 /**
67 * no support for MSAA FBOs
68 */
69 kNone_MSFBO = 0,
70 /**
71 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
72 */
73 kDesktopARB_MSFBO,
74 /**
75 * earlier GL_EXT_framebuffer* extensions
76 */
77 kDesktopEXT_MSFBO,
78 /**
79 * GL_APPLE_framebuffer_multisample ES extension
80 */
81 kAppleES_MSFBO,
82 } fMSFBOType;
bsalomon@google.comc4364992011-11-07 15:54:49 +000083
bsalomon@google.com85b505b2011-11-07 14:56:51 +000084 // TODO: get rid of GrAALevel and use sample cnt directly
85 GrGLuint fAASamples[4];
bsalomon@google.comc4364992011-11-07 15:54:49 +000086
bsalomon@google.com85b505b2011-11-07 14:56:51 +000087 // The maximum number of fragment uniform vectors (GLES has min. 16).
88 int fMaxFragmentUniformVectors;
bsalomon@google.comc4364992011-11-07 15:54:49 +000089
bsalomon@google.com85b505b2011-11-07 14:56:51 +000090 // ES requires an extension to support RGBA8 in RenderBufferStorage
bsalomon@google.com7107fa72011-11-10 14:54:14 +000091 bool fRGBA8RenderbufferSupport;
bsalomon@google.comc4364992011-11-07 15:54:49 +000092
bsalomon@google.com85b505b2011-11-07 14:56:51 +000093 // Is GL_BGRA supported
bsalomon@google.com7107fa72011-11-10 14:54:14 +000094 bool fBGRAFormatSupport;
bsalomon@google.comc4364992011-11-07 15:54:49 +000095
96 // Depending on the ES extensions present the BGRA external format may
97 // correspond either a BGRA or RGBA internalFormat. On desktop GL it is
98 // RGBA
bsalomon@google.com7107fa72011-11-10 14:54:14 +000099 bool fBGRAIsInternalFormat;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000100
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000101 // GL_ARB_texture_swizzle support
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000102 bool fTextureSwizzleSupport;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000103
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000104 // Is there support for GL_UNPACK_ROW_LENGTH
105 bool fUnpackRowLengthSupport;
106
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000107 // Is there support for GL_UNPACK_FLIP_Y
108 bool fUnpackFlipYSupport;
109
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000110 // Is there support for GL_PACK_ROW_LENGTH
111 bool fPackRowLengthSupport;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000112
113 // Is there support for GL_PACK_REVERSE_ROW_ORDER
114 bool fPackFlipYSupport;
115
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000116 void print() const;
117 } fGLCaps;
118
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000120 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 GrVertexLayout fVertexLayout;
122 const GrVertexBuffer* fVertexBuffer;
123 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000124 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 } fHWGeometryState;
126
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000127 struct AAState {
128 bool fMSAAEnabled;
129 bool fSmoothLineEnabled;
130 } fHWAAState;
131
tomhudson@google.com93813632011-10-27 20:21:16 +0000132 GrDrawState fHWDrawState;
133 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000134
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000135 // As flush of GL state proceeds it updates fHDrawState
136 // to reflect the new state. Later parts of the state flush
137 // may perform cascaded changes but cannot refer to fHWDrawState.
138 // These code paths can refer to the dirty flags. Subclass should
139 // call resetDirtyFlags after its flush is complete
140 struct {
141 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000142 int fTextureChangedMask;
143 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +0000144 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000145
146 // clears the dirty flags
147 void resetDirtyFlags();
148
149 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000150 struct {
151 bool fScissorEnabled;
152 GrGLIRect fScissorRect;
153 GrGLIRect fViewportRect;
154 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000155
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000156 const GLCaps& glCaps() const { return fGLCaps; }
157
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000158 // GrGpu overrides
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000159 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000160
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000161 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000162 const void* srcData,
163 size_t rowBytes);
164 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
165 bool dynamic);
166 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
167 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000168 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000169 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
170 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000171 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
172 int width, int height);
173 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
174 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000175
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000176 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000177
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000178 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000179
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000180 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000181 int left, int top,
182 int width, int height,
183 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000184 void* buffer,
185 size_t rowBytes,
186 bool invertY) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000187
bsalomon@google.com6f379512011-11-16 20:36:03 +0000188 virtual void onWriteTexturePixels(GrTexture* texture,
189 int left, int top, int width, int height,
190 GrPixelConfig config, const void* buffer,
191 size_t rowBytes) SK_OVERRIDE;
192
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000193 virtual void onGpuDrawIndexed(GrPrimitiveType type,
194 uint32_t startVertex,
195 uint32_t startIndex,
196 uint32_t vertexCount,
197 uint32_t indexCount);
198 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
199 uint32_t vertexCount,
200 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000202 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000203 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000204 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000205
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000206 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000207 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000208
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000209 // binds appropriate vertex and index buffers, also returns any extra
210 // extra verts or indices to offset by.
211 void setBuffers(bool indexed,
212 int* extraVertexOffset,
213 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000214
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 // flushes state that is common to fixed and programmable GL
216 // dither
217 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 // texture binding
219 // sampler state (filtering, tiling)
220 // FBO binding
221 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000222 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000224 // Subclasses should call this to flush the blend state.
225 // The params should be the final coeffecients to apply
226 // (after any blending optimizations or dual source blending considerations
227 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000228 void flushBlend(GrPrimitiveType type,
229 GrBlendCoeff srcCoeff,
230 GrBlendCoeff dstCoeff);
231
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000232 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000233 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000234 }
235
bsalomon@google.com99621082011-11-15 16:47:16 +0000236 // adjusts texture matrix to account for orientation
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000237 static void AdjustTextureMatrix(const GrGLTexture* texture,
238 GrSamplerState::SampleMode mode,
239 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000241 // subclass may try to take advantage of identity tex matrices.
242 // This helper determines if matrix will be identity after all
243 // adjustments are applied.
244 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
245 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000246
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000247 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000248
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000249private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
251 // additional caps.
252 void initCaps();
253
254 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000255
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000256 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000257 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000258
reed@google.comac10a2d2010-12-22 21:39:39 +0000259 // notify callbacks to update state tracking when related
260 // objects are bound to GL or deleted outside of the class
261 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
262 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
263 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
264 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 void notifyTextureDelete(GrGLTexture* texture);
266 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000267
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000268 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000270 // bound is region that may be modified and therefore has to be resolved.
271 // NULL means whole target. Can be an empty rect.
272 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000274 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000275
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000276 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000277
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000278 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000279 GrGLenum* internalFormat,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000280 GrGLenum* externalFormat,
281 GrGLenum* externalType);
282 // helper for onCreateTexture and writeTexturePixels
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000283 bool uploadTexData(const GrGLTexture::Desc& desc,
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000284 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000285 int left, int top, int width, int height,
286 GrPixelConfig dataConfig,
287 const void* data,
288 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000289
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000290 bool createRenderTargetObjects(int width, int height,
291 GrGLuint texID,
292 GrGLRenderTarget::Desc* desc);
293
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000294 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000295
296 friend class GrGLVertexBuffer;
297 friend class GrGLIndexBuffer;
298 friend class GrGLTexture;
299 friend class GrGLRenderTarget;
300
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000301 // read these once at begining and then never again
302 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000303 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000304
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000305 // we want to clear stencil buffers when they are created. We want to clear
306 // the entire buffer even if it is larger than the color attachment. We
307 // attach it to this fbo with no color attachment to do the initial clear.
308 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000309
reed@google.comac10a2d2010-12-22 21:39:39 +0000310 bool fHWBlendDisabled;
311
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000312 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000313
bsalomon@google.comfe676522011-06-17 18:12:21 +0000314 // we record what stencil format worked last time to hopefully exit early
315 // from our loop that tries stencil formats and calls check fb status.
316 int fLastSuccessfulStencilFmtIdx;
317
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000318 const GrGLInterface* fGL;
319 GrGLBinding fGLBinding;
320
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000321 bool fPrintedCaps;
322
reed@google.comac10a2d2010-12-22 21:39:39 +0000323 typedef GrGpu INHERITED;
324};
325
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000326#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000327