blob: b5b5b1525886fbc3ebb22cf2fc1aad3dcdc42544 [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;
34
bsalomon@google.comc4364992011-11-07 15:54:49 +000035 virtual bool readPixelsWillPayForYFlip(
36 GrRenderTarget* renderTarget,
37 int left, int top,
38 int width, int height,
39 GrPixelConfig config,
40 size_t rowBytes) SK_OVERRIDE;
41
reed@google.comac10a2d2010-12-22 21:39:39 +000042protected:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000043 GrGpuGL(const GrGLInterface* glInterface, GrGLBinding glBinding);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000044
bsalomon@google.com85b505b2011-11-07 14:56:51 +000045 struct GLCaps {
46 GLCaps()
47 // make defaults be the most restrictive
bsalomon@google.comc4364992011-11-07 15:54:49 +000048 : fStencilFormats(8) // prealloc space for stencil formats
49 , fMSFBOType(kNone_MSFBO)
bsalomon@google.com85b505b2011-11-07 14:56:51 +000050 , fMaxFragmentUniformVectors(0)
bsalomon@google.com7107fa72011-11-10 14:54:14 +000051 , fRGBA8RenderbufferSupport(false)
52 , fBGRAFormatSupport(false)
53 , fBGRAIsInternalFormat(false)
54 , fTextureSwizzleSupport(false)
55 , fUnpackRowLengthSupport(false)
56 , fPackRowLengthSupport(false) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +000057 memset(fAASamples, 0, sizeof(fAASamples));
58 }
59 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
bsalomon@google.comc4364992011-11-07 15:54:49 +000060
bsalomon@google.com85b505b2011-11-07 14:56:51 +000061 enum {
62 /**
63 * no support for MSAA FBOs
64 */
65 kNone_MSFBO = 0,
66 /**
67 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
68 */
69 kDesktopARB_MSFBO,
70 /**
71 * earlier GL_EXT_framebuffer* extensions
72 */
73 kDesktopEXT_MSFBO,
74 /**
75 * GL_APPLE_framebuffer_multisample ES extension
76 */
77 kAppleES_MSFBO,
78 } fMSFBOType;
bsalomon@google.comc4364992011-11-07 15:54:49 +000079
bsalomon@google.com85b505b2011-11-07 14:56:51 +000080 // TODO: get rid of GrAALevel and use sample cnt directly
81 GrGLuint fAASamples[4];
bsalomon@google.comc4364992011-11-07 15:54:49 +000082
bsalomon@google.com85b505b2011-11-07 14:56:51 +000083 // The maximum number of fragment uniform vectors (GLES has min. 16).
84 int fMaxFragmentUniformVectors;
bsalomon@google.comc4364992011-11-07 15:54:49 +000085
bsalomon@google.com85b505b2011-11-07 14:56:51 +000086 // ES requires an extension to support RGBA8 in RenderBufferStorage
bsalomon@google.com7107fa72011-11-10 14:54:14 +000087 bool fRGBA8RenderbufferSupport;
bsalomon@google.comc4364992011-11-07 15:54:49 +000088
bsalomon@google.com85b505b2011-11-07 14:56:51 +000089 // Is GL_BGRA supported
bsalomon@google.com7107fa72011-11-10 14:54:14 +000090 bool fBGRAFormatSupport;
bsalomon@google.comc4364992011-11-07 15:54:49 +000091
92 // Depending on the ES extensions present the BGRA external format may
93 // correspond either a BGRA or RGBA internalFormat. On desktop GL it is
94 // RGBA
bsalomon@google.com7107fa72011-11-10 14:54:14 +000095 bool fBGRAIsInternalFormat;
bsalomon@google.comc4364992011-11-07 15:54:49 +000096
bsalomon@google.com85b505b2011-11-07 14:56:51 +000097 // GL_ARB_texture_swizzle support
bsalomon@google.com7107fa72011-11-10 14:54:14 +000098 bool fTextureSwizzleSupport;
bsalomon@google.com85b505b2011-11-07 14:56:51 +000099
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000100 // Is there support for GL_UNPACK_ROW_LENGTH
101 bool fUnpackRowLengthSupport;
102
103 // Is there support for GL_PACK_ROW_LENGTH
104 bool fPackRowLengthSupport;
105
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000106 void print() const;
107 } fGLCaps;
108
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000110 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 GrVertexLayout fVertexLayout;
112 const GrVertexBuffer* fVertexBuffer;
113 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000114 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 } fHWGeometryState;
116
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000117 struct AAState {
118 bool fMSAAEnabled;
119 bool fSmoothLineEnabled;
120 } fHWAAState;
121
tomhudson@google.com93813632011-10-27 20:21:16 +0000122 GrDrawState fHWDrawState;
123 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000124
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000125 // As flush of GL state proceeds it updates fHDrawState
126 // to reflect the new state. Later parts of the state flush
127 // may perform cascaded changes but cannot refer to fHWDrawState.
128 // These code paths can refer to the dirty flags. Subclass should
129 // call resetDirtyFlags after its flush is complete
130 struct {
131 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000132 int fTextureChangedMask;
133 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +0000134 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000135
136 // clears the dirty flags
137 void resetDirtyFlags();
138
139 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000140 struct {
141 bool fScissorEnabled;
142 GrGLIRect fScissorRect;
143 GrGLIRect fViewportRect;
144 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000145
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000146 const GLCaps& glCaps() const { return fGLCaps; }
147
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000148 // GrGpu overrides
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000149 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000150
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000151 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000152 const void* srcData,
153 size_t rowBytes);
154 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
155 bool dynamic);
156 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
157 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000158 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000159 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
160 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000161 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
162 int width, int height);
163 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
164 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000165
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000166 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000167
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000168 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000169
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000170 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000171 int left, int top,
172 int width, int height,
173 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000174 void* buffer,
175 size_t rowBytes,
176 bool invertY) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000177
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000178 virtual void onGpuDrawIndexed(GrPrimitiveType type,
179 uint32_t startVertex,
180 uint32_t startIndex,
181 uint32_t vertexCount,
182 uint32_t indexCount);
183 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
184 uint32_t vertexCount,
185 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000186 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000187 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000188 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000189 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000190
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000191 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000192 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000193
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000194 // binds appropriate vertex and index buffers, also returns any extra
195 // extra verts or indices to offset by.
196 void setBuffers(bool indexed,
197 int* extraVertexOffset,
198 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000199
reed@google.comac10a2d2010-12-22 21:39:39 +0000200 // flushes state that is common to fixed and programmable GL
201 // dither
202 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000203 // texture binding
204 // sampler state (filtering, tiling)
205 // FBO binding
206 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000207 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000208
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000209 // Subclasses should call this to flush the blend state.
210 // The params should be the final coeffecients to apply
211 // (after any blending optimizations or dual source blending considerations
212 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000213 void flushBlend(GrPrimitiveType type,
214 GrBlendCoeff srcCoeff,
215 GrBlendCoeff dstCoeff);
216
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000217 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000218 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000219 }
220
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000221 // adjusts texture matrix to account for orientation, size, and npotness
222 static void AdjustTextureMatrix(const GrGLTexture* texture,
223 GrSamplerState::SampleMode mode,
224 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000226 // subclass may try to take advantage of identity tex matrices.
227 // This helper determines if matrix will be identity after all
228 // adjustments are applied.
229 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
230 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000232 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000233
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000234private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
236 // additional caps.
237 void initCaps();
238
239 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000240
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000241 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000242 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000243
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 // notify callbacks to update state tracking when related
245 // objects are bound to GL or deleted outside of the class
246 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
247 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
248 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
249 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 void notifyTextureDelete(GrGLTexture* texture);
251 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000252
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000253 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000254
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000255 // bound is region that may be modified and therefore has to be resolved.
256 // NULL means whole target. Can be an empty rect.
257 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000258 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000259 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000260
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000261 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000262
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000263 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000264 GrGLenum* internalFormat,
265 GrGLenum* format,
266 GrGLenum* type);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000267 // helpers for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000268 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
269 GrGLenum internalFormat,
270 const void* data,
271 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000272
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000273 bool createRenderTargetObjects(int width, int height,
274 GrGLuint texID,
275 GrGLRenderTarget::Desc* desc);
276
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000277 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000278
279 friend class GrGLVertexBuffer;
280 friend class GrGLIndexBuffer;
281 friend class GrGLTexture;
282 friend class GrGLRenderTarget;
283
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000284 // read these once at begining and then never again
285 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000286 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000287
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000288 // we want to clear stencil buffers when they are created. We want to clear
289 // the entire buffer even if it is larger than the color attachment. We
290 // attach it to this fbo with no color attachment to do the initial clear.
291 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000292
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 bool fHWBlendDisabled;
294
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000295 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000296
bsalomon@google.comfe676522011-06-17 18:12:21 +0000297 // we record what stencil format worked last time to hopefully exit early
298 // from our loop that tries stencil formats and calls check fb status.
299 int fLastSuccessfulStencilFmtIdx;
300
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000301 const GrGLInterface* fGL;
302 GrGLBinding fGLBinding;
303
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000304 bool fPrintedCaps;
305
reed@google.comac10a2d2010-12-22 21:39:39 +0000306 typedef GrGpu INHERITED;
307};
308
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000309#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000310