blob: 84c62f83017c0dacc683d96d5296b5c8affdf2ac [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)
51 , fRGBA8Renderbuffer(false)
52 , fBGRAFormat(false)
bsalomon@google.comc4364992011-11-07 15:54:49 +000053 , fBGRAInternalFormat(false)
bsalomon@google.com85b505b2011-11-07 14:56:51 +000054 , fTextureSwizzle(false) {
55 memset(fAASamples, 0, sizeof(fAASamples));
56 }
57 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
bsalomon@google.comc4364992011-11-07 15:54:49 +000058
bsalomon@google.com85b505b2011-11-07 14:56:51 +000059 enum {
60 /**
61 * no support for MSAA FBOs
62 */
63 kNone_MSFBO = 0,
64 /**
65 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
66 */
67 kDesktopARB_MSFBO,
68 /**
69 * earlier GL_EXT_framebuffer* extensions
70 */
71 kDesktopEXT_MSFBO,
72 /**
73 * GL_APPLE_framebuffer_multisample ES extension
74 */
75 kAppleES_MSFBO,
76 } fMSFBOType;
bsalomon@google.comc4364992011-11-07 15:54:49 +000077
bsalomon@google.com85b505b2011-11-07 14:56:51 +000078 // TODO: get rid of GrAALevel and use sample cnt directly
79 GrGLuint fAASamples[4];
bsalomon@google.comc4364992011-11-07 15:54:49 +000080
bsalomon@google.com85b505b2011-11-07 14:56:51 +000081 // The maximum number of fragment uniform vectors (GLES has min. 16).
82 int fMaxFragmentUniformVectors;
bsalomon@google.comc4364992011-11-07 15:54:49 +000083
bsalomon@google.com85b505b2011-11-07 14:56:51 +000084 // ES requires an extension to support RGBA8 in RenderBufferStorage
85 bool fRGBA8Renderbuffer;
bsalomon@google.comc4364992011-11-07 15:54:49 +000086
bsalomon@google.com85b505b2011-11-07 14:56:51 +000087 // Is GL_BGRA supported
88 bool fBGRAFormat;
bsalomon@google.comc4364992011-11-07 15:54:49 +000089
90 // Depending on the ES extensions present the BGRA external format may
91 // correspond either a BGRA or RGBA internalFormat. On desktop GL it is
92 // RGBA
93 bool fBGRAInternalFormat;
94
bsalomon@google.com85b505b2011-11-07 14:56:51 +000095 // GL_ARB_texture_swizzle support
96 bool fTextureSwizzle;
97
98 void print() const;
99 } fGLCaps;
100
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000102 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 GrVertexLayout fVertexLayout;
104 const GrVertexBuffer* fVertexBuffer;
105 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000106 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 } fHWGeometryState;
108
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000109 struct AAState {
110 bool fMSAAEnabled;
111 bool fSmoothLineEnabled;
112 } fHWAAState;
113
tomhudson@google.com93813632011-10-27 20:21:16 +0000114 GrDrawState fHWDrawState;
115 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000116
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000117 // As flush of GL state proceeds it updates fHDrawState
118 // to reflect the new state. Later parts of the state flush
119 // may perform cascaded changes but cannot refer to fHWDrawState.
120 // These code paths can refer to the dirty flags. Subclass should
121 // call resetDirtyFlags after its flush is complete
122 struct {
123 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000124 int fTextureChangedMask;
125 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +0000126 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000127
128 // clears the dirty flags
129 void resetDirtyFlags();
130
131 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000132 struct {
133 bool fScissorEnabled;
134 GrGLIRect fScissorRect;
135 GrGLIRect fViewportRect;
136 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000137
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000138 const GLCaps& glCaps() const { return fGLCaps; }
139
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000140 // GrGpu overrides
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000141 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000142
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000143 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000144 const void* srcData,
145 size_t rowBytes);
146 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
147 bool dynamic);
148 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
149 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000150 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000151 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
152 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000153 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
154 int width, int height);
155 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
156 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000157
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000158 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000159
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000160 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000161
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000162 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000163 int left, int top,
164 int width, int height,
165 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000166 void* buffer,
167 size_t rowBytes,
168 bool invertY) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000169
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000170 virtual void onGpuDrawIndexed(GrPrimitiveType type,
171 uint32_t startVertex,
172 uint32_t startIndex,
173 uint32_t vertexCount,
174 uint32_t indexCount);
175 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
176 uint32_t vertexCount,
177 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000179 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000180 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000181 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000182
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000183 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000184 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000186 // binds appropriate vertex and index buffers, also returns any extra
187 // extra verts or indices to offset by.
188 void setBuffers(bool indexed,
189 int* extraVertexOffset,
190 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000191
reed@google.comac10a2d2010-12-22 21:39:39 +0000192 // flushes state that is common to fixed and programmable GL
193 // dither
194 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 // texture binding
196 // sampler state (filtering, tiling)
197 // FBO binding
198 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000199 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000200
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000201 // Subclasses should call this to flush the blend state.
202 // The params should be the final coeffecients to apply
203 // (after any blending optimizations or dual source blending considerations
204 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000205 void flushBlend(GrPrimitiveType type,
206 GrBlendCoeff srcCoeff,
207 GrBlendCoeff dstCoeff);
208
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000209 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000210 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000211 }
212
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000213 // adjusts texture matrix to account for orientation, size, and npotness
214 static void AdjustTextureMatrix(const GrGLTexture* texture,
215 GrSamplerState::SampleMode mode,
216 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000217
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000218 // subclass may try to take advantage of identity tex matrices.
219 // This helper determines if matrix will be identity after all
220 // adjustments are applied.
221 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
222 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000224 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000225
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000226private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000227 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
228 // additional caps.
229 void initCaps();
230
231 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000232
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000233 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000235
reed@google.comac10a2d2010-12-22 21:39:39 +0000236 // notify callbacks to update state tracking when related
237 // objects are bound to GL or deleted outside of the class
238 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
239 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
240 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
241 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 void notifyTextureDelete(GrGLTexture* texture);
243 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000244
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000245 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000246
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000247 // bound is region that may be modified and therefore has to be resolved.
248 // NULL means whole target. Can be an empty rect.
249 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000251 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000252
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000253 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000254
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000255 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000256 GrGLenum* internalFormat,
257 GrGLenum* format,
258 GrGLenum* type);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000259 // helpers for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000260 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
261 GrGLenum internalFormat,
262 const void* data,
263 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000264
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000265 bool createRenderTargetObjects(int width, int height,
266 GrGLuint texID,
267 GrGLRenderTarget::Desc* desc);
268
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000269 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
271 friend class GrGLVertexBuffer;
272 friend class GrGLIndexBuffer;
273 friend class GrGLTexture;
274 friend class GrGLRenderTarget;
275
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000276 // read these once at begining and then never again
277 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000278 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000279
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000280 // we want to clear stencil buffers when they are created. We want to clear
281 // the entire buffer even if it is larger than the color attachment. We
282 // attach it to this fbo with no color attachment to do the initial clear.
283 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000284
reed@google.comac10a2d2010-12-22 21:39:39 +0000285 bool fHWBlendDisabled;
286
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000287 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000288
bsalomon@google.comfe676522011-06-17 18:12:21 +0000289 // we record what stencil format worked last time to hopefully exit early
290 // from our loop that tries stencil formats and calls check fb status.
291 int fLastSuccessfulStencilFmtIdx;
292
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000293 const GrGLInterface* fGL;
294 GrGLBinding fGLBinding;
295
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000296 bool fPrintedCaps;
297
reed@google.comac10a2d2010-12-22 21:39:39 +0000298 typedef GrGpu INHERITED;
299};
300
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000301#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000302