blob: 2d246e8479520be46b5294b9c4bd1de7f64b0f20 [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
reed@google.comac10a2d2010-12-22 21:39:39 +000032protected:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000033 GrGpuGL(const GrGLInterface* glInterface, GrGLBinding glBinding);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000034
reed@google.comac10a2d2010-12-22 21:39:39 +000035 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000036 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000037 GrVertexLayout fVertexLayout;
38 const GrVertexBuffer* fVertexBuffer;
39 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000040 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000041 } fHWGeometryState;
42
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000043 struct AAState {
44 bool fMSAAEnabled;
45 bool fSmoothLineEnabled;
46 } fHWAAState;
47
tomhudson@google.com93813632011-10-27 20:21:16 +000048 GrDrawState fHWDrawState;
49 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000050
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000051 // As flush of GL state proceeds it updates fHDrawState
52 // to reflect the new state. Later parts of the state flush
53 // may perform cascaded changes but cannot refer to fHWDrawState.
54 // These code paths can refer to the dirty flags. Subclass should
55 // call resetDirtyFlags after its flush is complete
56 struct {
57 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000058 int fTextureChangedMask;
59 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +000060 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000061
62 // clears the dirty flags
63 void resetDirtyFlags();
64
65 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000066 struct {
67 bool fScissorEnabled;
68 GrGLIRect fScissorRect;
69 GrGLIRect fViewportRect;
70 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000071
bsalomon@google.com1c13c962011-02-14 16:51:21 +000072 // GrGpu overrides
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000073 virtual void resetContext();
74
bsalomon@google.comfea37b52011-04-25 15:51:06 +000075 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000076 const void* srcData,
77 size_t rowBytes);
78 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
79 bool dynamic);
80 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
81 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000082 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000083 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
84 int width, int height);
85 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
86 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000087
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000088 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000089
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000090 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000091
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000092 virtual bool onReadPixels(GrRenderTarget* target,
93 int left, int top, int width, int height,
94 GrPixelConfig, void* buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000095
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000096 virtual void onGpuDrawIndexed(GrPrimitiveType type,
97 uint32_t startVertex,
98 uint32_t startIndex,
99 uint32_t vertexCount,
100 uint32_t indexCount);
101 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
102 uint32_t vertexCount,
103 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000105 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000106 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000107 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000108
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000109 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000110 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000111
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112 // binds appropriate vertex and index buffers, also returns any extra
113 // extra verts or indices to offset by.
114 void setBuffers(bool indexed,
115 int* extraVertexOffset,
116 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000117
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 // flushes state that is common to fixed and programmable GL
119 // dither
120 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 // texture binding
122 // sampler state (filtering, tiling)
123 // FBO binding
124 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000125 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000127 // Subclasses should call this to flush the blend state.
128 // The params should be the final coeffecients to apply
129 // (after any blending optimizations or dual source blending considerations
130 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000131 void flushBlend(GrPrimitiveType type,
132 GrBlendCoeff srcCoeff,
133 GrBlendCoeff dstCoeff);
134
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000135 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000136 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000137 }
138
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000139 // adjusts texture matrix to account for orientation, size, and npotness
140 static void AdjustTextureMatrix(const GrGLTexture* texture,
141 GrSamplerState::SampleMode mode,
142 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000143
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000144 // subclass may try to take advantage of identity tex matrices.
145 // This helper determines if matrix will be identity after all
146 // adjustments are applied.
147 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
148 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000149
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000150 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000151
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000152private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000153 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
154 // additional caps.
155 void initCaps();
156
157 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000158
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000159 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000160 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000161
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 // notify callbacks to update state tracking when related
163 // objects are bound to GL or deleted outside of the class
164 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
165 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
166 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
167 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 void notifyTextureDelete(GrGLTexture* texture);
169 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000170
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000171 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000172
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000173 // bound is region that may be modified and therefore has to be resolved.
174 // NULL means whole target. Can be an empty rect.
175 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000177 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000178
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000179 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000180
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000181 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000182 GrGLenum* internalFormat,
183 GrGLenum* format,
184 GrGLenum* type);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000185 // helpers for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000186 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
187 GrGLenum internalFormat,
188 const void* data,
189 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000190
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000191 bool createRenderTargetObjects(int width, int height,
192 GrGLuint texID,
193 GrGLRenderTarget::Desc* desc);
194
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000195 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000196
197 friend class GrGLVertexBuffer;
198 friend class GrGLIndexBuffer;
199 friend class GrGLTexture;
200 friend class GrGLRenderTarget;
201
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000202 // read these once at begining and then never again
203 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000204 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000205
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000206 struct GLCaps {
207 // prealloc space for 8 stencil formats
208 GLCaps() : fStencilFormats(8) {}
209 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
210
211 enum {
212 /**
213 * no support for MSAA FBOs
214 */
215 kNone_MSFBO = 0,
216 /**
217 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
218 */
219 kDesktopARB_MSFBO,
220 /**
221 * earlier GL_EXT_framebuffer* extensions
222 */
223 kDesktopEXT_MSFBO,
224 /**
225 * GL_APPLE_framebuffer_multisample ES extension
226 */
227 kAppleES_MSFBO,
228 } fMSFBOType;
229
230 // TODO: get rid of GrAALevel and use sample cnt directly
231 GrGLuint fAASamples[4];
232
233 // The maximum number of fragment uniform vectors (GLES has min. 16).
234 int fMaxFragmentUniformVectors;
235
236 // ES requires an extension to support RGBA8 in RenderBufferStorage
237 bool fRGBA8Renderbuffer;
238
239 void print() const;
240 } fGLCaps;
241
242
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000243 // we want to clear stencil buffers when they are created. We want to clear
244 // the entire buffer even if it is larger than the color attachment. We
245 // attach it to this fbo with no color attachment to do the initial clear.
246 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000247
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 bool fHWBlendDisabled;
249
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000250 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000251
bsalomon@google.comfe676522011-06-17 18:12:21 +0000252 // we record what stencil format worked last time to hopefully exit early
253 // from our loop that tries stencil formats and calls check fb status.
254 int fLastSuccessfulStencilFmtIdx;
255
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000256 const GrGLInterface* fGL;
257 GrGLBinding fGLBinding;
258
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 bool fPrintedCaps;
260
reed@google.comac10a2d2010-12-22 21:39:39 +0000261 typedef GrGpu INHERITED;
262};
263
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000264#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000265