blob: 1dd9c80fd3b013e1a76697555cbc1965d5324b12 [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.comc4364992011-11-07 15:54:49 +000032 virtual bool readPixelsWillPayForYFlip(
33 GrRenderTarget* renderTarget,
34 int left, int top,
35 int width, int height,
36 GrPixelConfig config,
37 size_t rowBytes) SK_OVERRIDE;
38
reed@google.comac10a2d2010-12-22 21:39:39 +000039protected:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000040 GrGpuGL(const GrGLInterface* glInterface, GrGLBinding glBinding);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000041
bsalomon@google.com85b505b2011-11-07 14:56:51 +000042 struct GLCaps {
43 GLCaps()
44 // make defaults be the most restrictive
bsalomon@google.comc4364992011-11-07 15:54:49 +000045 : fStencilFormats(8) // prealloc space for stencil formats
46 , fMSFBOType(kNone_MSFBO)
bsalomon@google.com85b505b2011-11-07 14:56:51 +000047 , fMaxFragmentUniformVectors(0)
48 , fRGBA8Renderbuffer(false)
49 , fBGRAFormat(false)
bsalomon@google.comc4364992011-11-07 15:54:49 +000050 , fBGRAInternalFormat(false)
bsalomon@google.com85b505b2011-11-07 14:56:51 +000051 , fTextureSwizzle(false) {
52 memset(fAASamples, 0, sizeof(fAASamples));
53 }
54 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
bsalomon@google.comc4364992011-11-07 15:54:49 +000055
bsalomon@google.com85b505b2011-11-07 14:56:51 +000056 enum {
57 /**
58 * no support for MSAA FBOs
59 */
60 kNone_MSFBO = 0,
61 /**
62 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
63 */
64 kDesktopARB_MSFBO,
65 /**
66 * earlier GL_EXT_framebuffer* extensions
67 */
68 kDesktopEXT_MSFBO,
69 /**
70 * GL_APPLE_framebuffer_multisample ES extension
71 */
72 kAppleES_MSFBO,
73 } fMSFBOType;
bsalomon@google.comc4364992011-11-07 15:54:49 +000074
bsalomon@google.com85b505b2011-11-07 14:56:51 +000075 // TODO: get rid of GrAALevel and use sample cnt directly
76 GrGLuint fAASamples[4];
bsalomon@google.comc4364992011-11-07 15:54:49 +000077
bsalomon@google.com85b505b2011-11-07 14:56:51 +000078 // The maximum number of fragment uniform vectors (GLES has min. 16).
79 int fMaxFragmentUniformVectors;
bsalomon@google.comc4364992011-11-07 15:54:49 +000080
bsalomon@google.com85b505b2011-11-07 14:56:51 +000081 // ES requires an extension to support RGBA8 in RenderBufferStorage
82 bool fRGBA8Renderbuffer;
bsalomon@google.comc4364992011-11-07 15:54:49 +000083
bsalomon@google.com85b505b2011-11-07 14:56:51 +000084 // Is GL_BGRA supported
85 bool fBGRAFormat;
bsalomon@google.comc4364992011-11-07 15:54:49 +000086
87 // Depending on the ES extensions present the BGRA external format may
88 // correspond either a BGRA or RGBA internalFormat. On desktop GL it is
89 // RGBA
90 bool fBGRAInternalFormat;
91
bsalomon@google.com85b505b2011-11-07 14:56:51 +000092 // GL_ARB_texture_swizzle support
93 bool fTextureSwizzle;
94
95 void print() const;
96 } fGLCaps;
97
reed@google.comac10a2d2010-12-22 21:39:39 +000098 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000099 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 GrVertexLayout fVertexLayout;
101 const GrVertexBuffer* fVertexBuffer;
102 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000103 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 } fHWGeometryState;
105
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000106 struct AAState {
107 bool fMSAAEnabled;
108 bool fSmoothLineEnabled;
109 } fHWAAState;
110
tomhudson@google.com93813632011-10-27 20:21:16 +0000111 GrDrawState fHWDrawState;
112 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000113
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000114 // As flush of GL state proceeds it updates fHDrawState
115 // to reflect the new state. Later parts of the state flush
116 // may perform cascaded changes but cannot refer to fHWDrawState.
117 // These code paths can refer to the dirty flags. Subclass should
118 // call resetDirtyFlags after its flush is complete
119 struct {
120 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000121 int fTextureChangedMask;
122 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +0000123 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000124
125 // clears the dirty flags
126 void resetDirtyFlags();
127
128 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000129 struct {
130 bool fScissorEnabled;
131 GrGLIRect fScissorRect;
132 GrGLIRect fViewportRect;
133 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000135 const GLCaps& glCaps() const { return fGLCaps; }
136
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000137 // GrGpu overrides
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000138 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000139
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000140 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000141 const void* srcData,
142 size_t rowBytes);
143 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
144 bool dynamic);
145 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
146 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000147 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000148 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
149 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000150 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
151 int width, int height);
152 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
153 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000154
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000155 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000156
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000157 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000158
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000159 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000160 int left, int top,
161 int width, int height,
162 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000163 void* buffer,
164 size_t rowBytes,
165 bool invertY) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000166
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000167 virtual void onGpuDrawIndexed(GrPrimitiveType type,
168 uint32_t startVertex,
169 uint32_t startIndex,
170 uint32_t vertexCount,
171 uint32_t indexCount);
172 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
173 uint32_t vertexCount,
174 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000175 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000176 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000177 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000178 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000179
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000180 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000181 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000182
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000183 // binds appropriate vertex and index buffers, also returns any extra
184 // extra verts or indices to offset by.
185 void setBuffers(bool indexed,
186 int* extraVertexOffset,
187 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000188
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 // flushes state that is common to fixed and programmable GL
190 // dither
191 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000192 // texture binding
193 // sampler state (filtering, tiling)
194 // FBO binding
195 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000196 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000197
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000198 // Subclasses should call this to flush the blend state.
199 // The params should be the final coeffecients to apply
200 // (after any blending optimizations or dual source blending considerations
201 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000202 void flushBlend(GrPrimitiveType type,
203 GrBlendCoeff srcCoeff,
204 GrBlendCoeff dstCoeff);
205
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000206 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000207 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000208 }
209
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000210 // adjusts texture matrix to account for orientation, size, and npotness
211 static void AdjustTextureMatrix(const GrGLTexture* texture,
212 GrSamplerState::SampleMode mode,
213 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000214
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000215 // subclass may try to take advantage of identity tex matrices.
216 // This helper determines if matrix will be identity after all
217 // adjustments are applied.
218 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
219 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000221 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000222
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000223private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000224 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
225 // additional caps.
226 void initCaps();
227
228 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000229
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000230 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000231 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000232
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 // notify callbacks to update state tracking when related
234 // objects are bound to GL or deleted outside of the class
235 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
236 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
237 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
238 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 void notifyTextureDelete(GrGLTexture* texture);
240 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000241
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000242 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000243
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000244 // bound is region that may be modified and therefore has to be resolved.
245 // NULL means whole target. Can be an empty rect.
246 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000248 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000249
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000250 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000251
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000252 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000253 GrGLenum* internalFormat,
254 GrGLenum* format,
255 GrGLenum* type);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000256 // helpers for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000257 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
258 GrGLenum internalFormat,
259 const void* data,
260 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000261
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000262 bool createRenderTargetObjects(int width, int height,
263 GrGLuint texID,
264 GrGLRenderTarget::Desc* desc);
265
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000266 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000267
268 friend class GrGLVertexBuffer;
269 friend class GrGLIndexBuffer;
270 friend class GrGLTexture;
271 friend class GrGLRenderTarget;
272
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000273 // read these once at begining and then never again
274 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000275 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000276
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000277 // we want to clear stencil buffers when they are created. We want to clear
278 // the entire buffer even if it is larger than the color attachment. We
279 // attach it to this fbo with no color attachment to do the initial clear.
280 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000281
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 bool fHWBlendDisabled;
283
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000284 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000285
bsalomon@google.comfe676522011-06-17 18:12:21 +0000286 // we record what stencil format worked last time to hopefully exit early
287 // from our loop that tries stencil formats and calls check fb status.
288 int fLastSuccessfulStencilFmtIdx;
289
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000290 const GrGLInterface* fGL;
291 GrGLBinding fGLBinding;
292
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000293 bool fPrintedCaps;
294
reed@google.comac10a2d2010-12-22 21:39:39 +0000295 typedef GrGpu INHERITED;
296};
297
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000298#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000299