blob: 0addf4a2adfb1b11a641baf85891a1673f7d4e7f [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.com1bf1c212011-11-05 12:18:58 +000073 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000074
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.come269f212011-11-07 13:29:52 +000083 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
84 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000085 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
86 int width, int height);
87 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
88 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000089
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000090 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000091
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000092 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000093
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000094 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +000095 int left, int top,
96 int width, int height,
97 GrPixelConfig,
98 void* buffer, size_t rowBytes) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000099
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000100 virtual void onGpuDrawIndexed(GrPrimitiveType type,
101 uint32_t startVertex,
102 uint32_t startIndex,
103 uint32_t vertexCount,
104 uint32_t indexCount);
105 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
106 uint32_t vertexCount,
107 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000109 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000110 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000111 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000112
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000113 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000114 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000115
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000116 // binds appropriate vertex and index buffers, also returns any extra
117 // extra verts or indices to offset by.
118 void setBuffers(bool indexed,
119 int* extraVertexOffset,
120 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000121
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 // flushes state that is common to fixed and programmable GL
123 // dither
124 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 // texture binding
126 // sampler state (filtering, tiling)
127 // FBO binding
128 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000129 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000131 // Subclasses should call this to flush the blend state.
132 // The params should be the final coeffecients to apply
133 // (after any blending optimizations or dual source blending considerations
134 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000135 void flushBlend(GrPrimitiveType type,
136 GrBlendCoeff srcCoeff,
137 GrBlendCoeff dstCoeff);
138
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000139 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000140 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000141 }
142
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000143 // adjusts texture matrix to account for orientation, size, and npotness
144 static void AdjustTextureMatrix(const GrGLTexture* texture,
145 GrSamplerState::SampleMode mode,
146 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000147
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000148 // subclass may try to take advantage of identity tex matrices.
149 // This helper determines if matrix will be identity after all
150 // adjustments are applied.
151 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
152 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000153
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000154 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000155
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000156private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000157 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
158 // additional caps.
159 void initCaps();
160
161 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000162
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000163 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000164 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000165
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 // notify callbacks to update state tracking when related
167 // objects are bound to GL or deleted outside of the class
168 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
169 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
170 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
171 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000172 void notifyTextureDelete(GrGLTexture* texture);
173 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000174
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000175 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000176
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000177 // bound is region that may be modified and therefore has to be resolved.
178 // NULL means whole target. Can be an empty rect.
179 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000180 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000181 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000182
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000183 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000185 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000186 GrGLenum* internalFormat,
187 GrGLenum* format,
188 GrGLenum* type);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000189 // helpers for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000190 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
191 GrGLenum internalFormat,
192 const void* data,
193 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000194
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000195 bool createRenderTargetObjects(int width, int height,
196 GrGLuint texID,
197 GrGLRenderTarget::Desc* desc);
198
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000199 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000200
201 friend class GrGLVertexBuffer;
202 friend class GrGLIndexBuffer;
203 friend class GrGLTexture;
204 friend class GrGLRenderTarget;
205
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000206 // read these once at begining and then never again
207 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000208 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000209
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210 struct GLCaps {
211 // prealloc space for 8 stencil formats
212 GLCaps() : fStencilFormats(8) {}
213 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
214
215 enum {
216 /**
217 * no support for MSAA FBOs
218 */
219 kNone_MSFBO = 0,
220 /**
221 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
222 */
223 kDesktopARB_MSFBO,
224 /**
225 * earlier GL_EXT_framebuffer* extensions
226 */
227 kDesktopEXT_MSFBO,
228 /**
229 * GL_APPLE_framebuffer_multisample ES extension
230 */
231 kAppleES_MSFBO,
232 } fMSFBOType;
233
234 // TODO: get rid of GrAALevel and use sample cnt directly
235 GrGLuint fAASamples[4];
236
237 // The maximum number of fragment uniform vectors (GLES has min. 16).
238 int fMaxFragmentUniformVectors;
239
240 // ES requires an extension to support RGBA8 in RenderBufferStorage
241 bool fRGBA8Renderbuffer;
242
243 void print() const;
244 } fGLCaps;
245
246
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000247 // we want to clear stencil buffers when they are created. We want to clear
248 // the entire buffer even if it is larger than the color attachment. We
249 // attach it to this fbo with no color attachment to do the initial clear.
250 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000251
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 bool fHWBlendDisabled;
253
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000254 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000255
bsalomon@google.comfe676522011-06-17 18:12:21 +0000256 // we record what stencil format worked last time to hopefully exit early
257 // from our loop that tries stencil formats and calls check fb status.
258 int fLastSuccessfulStencilFmtIdx;
259
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000260 const GrGLInterface* fGL;
261 GrGLBinding fGLBinding;
262
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000263 bool fPrintedCaps;
264
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 typedef GrGpu INHERITED;
266};
267
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000268#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000269