blob: 51eb4aed02b16b993eb01724a24c8040efd962d3 [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,
bsalomon@google.comc6980972011-11-02 19:57:21 +000093 int left, int top,
94 int width, int height,
95 GrPixelConfig,
96 void* buffer, size_t rowBytes) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000097
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000098 virtual void onGpuDrawIndexed(GrPrimitiveType type,
99 uint32_t startVertex,
100 uint32_t startIndex,
101 uint32_t vertexCount,
102 uint32_t indexCount);
103 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
104 uint32_t vertexCount,
105 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000107 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000108 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000109 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000110
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000111 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000112 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000113
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000114 // binds appropriate vertex and index buffers, also returns any extra
115 // extra verts or indices to offset by.
116 void setBuffers(bool indexed,
117 int* extraVertexOffset,
118 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000119
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 // flushes state that is common to fixed and programmable GL
121 // dither
122 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 // texture binding
124 // sampler state (filtering, tiling)
125 // FBO binding
126 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000127 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000128
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000129 // Subclasses should call this to flush the blend state.
130 // The params should be the final coeffecients to apply
131 // (after any blending optimizations or dual source blending considerations
132 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000133 void flushBlend(GrPrimitiveType type,
134 GrBlendCoeff srcCoeff,
135 GrBlendCoeff dstCoeff);
136
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000137 bool hasExtension(const char* ext) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000138 return GrGLHasExtensionFromString(ext, fExtensionString.c_str());
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000139 }
140
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000141 // adjusts texture matrix to account for orientation, size, and npotness
142 static void AdjustTextureMatrix(const GrGLTexture* texture,
143 GrSamplerState::SampleMode mode,
144 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000145
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000146 // subclass may try to take advantage of identity tex matrices.
147 // This helper determines if matrix will be identity after all
148 // adjustments are applied.
149 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
150 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000152 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000153
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000154private:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000155 // Inits GrDrawTarget::Caps and GLCaps, sublcass may enable
156 // additional caps.
157 void initCaps();
158
159 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000160
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000161 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000162 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000163
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 // notify callbacks to update state tracking when related
165 // objects are bound to GL or deleted outside of the class
166 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
167 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
168 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
169 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 void notifyTextureDelete(GrGLTexture* texture);
171 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000172
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000173 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000174
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000175 // bound is region that may be modified and therefore has to be resolved.
176 // NULL means whole target. Can be an empty rect.
177 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000179 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000180
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000181 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000182
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000183 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000184 GrGLenum* internalFormat,
185 GrGLenum* format,
186 GrGLenum* type);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000187 // helpers for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000188 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
189 GrGLenum internalFormat,
190 const void* data,
191 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000192
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000193 bool createRenderTargetObjects(int width, int height,
194 GrGLuint texID,
195 GrGLRenderTarget::Desc* desc);
196
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000197 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000198
199 friend class GrGLVertexBuffer;
200 friend class GrGLIndexBuffer;
201 friend class GrGLTexture;
202 friend class GrGLRenderTarget;
203
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000204 // read these once at begining and then never again
205 SkString fExtensionString;
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000206 GrGLVersion fGLVersion;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000207
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000208 struct GLCaps {
209 // prealloc space for 8 stencil formats
210 GLCaps() : fStencilFormats(8) {}
211 SkTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
212
213 enum {
214 /**
215 * no support for MSAA FBOs
216 */
217 kNone_MSFBO = 0,
218 /**
219 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
220 */
221 kDesktopARB_MSFBO,
222 /**
223 * earlier GL_EXT_framebuffer* extensions
224 */
225 kDesktopEXT_MSFBO,
226 /**
227 * GL_APPLE_framebuffer_multisample ES extension
228 */
229 kAppleES_MSFBO,
230 } fMSFBOType;
231
232 // TODO: get rid of GrAALevel and use sample cnt directly
233 GrGLuint fAASamples[4];
234
235 // The maximum number of fragment uniform vectors (GLES has min. 16).
236 int fMaxFragmentUniformVectors;
237
238 // ES requires an extension to support RGBA8 in RenderBufferStorage
239 bool fRGBA8Renderbuffer;
240
241 void print() const;
242 } fGLCaps;
243
244
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000245 // we want to clear stencil buffers when they are created. We want to clear
246 // the entire buffer even if it is larger than the color attachment. We
247 // attach it to this fbo with no color attachment to do the initial clear.
248 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000249
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 bool fHWBlendDisabled;
251
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000252 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000253
bsalomon@google.comfe676522011-06-17 18:12:21 +0000254 // we record what stencil format worked last time to hopefully exit early
255 // from our loop that tries stencil formats and calls check fb status.
256 int fLastSuccessfulStencilFmtIdx;
257
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000258 const GrGLInterface* fGL;
259 GrGLBinding fGLBinding;
260
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000261 bool fPrintedCaps;
262
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 typedef GrGpu INHERITED;
264};
265
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000266#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000267