blob: fc104c1c842283bae6055f768a3f1f24cd3ed2b9 [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
14#include "GrGpu.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000015#include "GrGLIndexBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000016#include "GrGLIRect.h"
17#include "GrGLStencilBuffer.h"
18#include "GrGLTexture.h"
19#include "GrGLVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +000021#include "SkString.h"
22
reed@google.comac10a2d2010-12-22 21:39:39 +000023class GrGpuGL : public GrGpu {
24public:
reed@google.comac10a2d2010-12-22 21:39:39 +000025 virtual ~GrGpuGL();
26
bsalomon@google.com0b77d682011-08-19 13:28:54 +000027 const GrGLInterface* glInterface() const { return fGL; }
28 GrGLBinding glBinding() const { return fGLBinding; }
29
reed@google.comac10a2d2010-12-22 21:39:39 +000030protected:
bsalomon@google.com0b77d682011-08-19 13:28:54 +000031 GrGpuGL(const GrGLInterface* glInterface, GrGLBinding glBinding);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000034 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000035 GrVertexLayout fVertexLayout;
36 const GrVertexBuffer* fVertexBuffer;
37 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000038 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000039 } fHWGeometryState;
40
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000041 struct AAState {
42 bool fMSAAEnabled;
43 bool fSmoothLineEnabled;
44 } fHWAAState;
45
reed@google.com8195f672011-01-12 18:14:28 +000046 DrState fHWDrawState;
47 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000048
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +000049 // read these once at begining and then never again
50 SkString fExtensionString;
51 float fGLVersion;
52
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000053 // As flush of GL state proceeds it updates fHDrawState
54 // to reflect the new state. Later parts of the state flush
55 // may perform cascaded changes but cannot refer to fHWDrawState.
56 // These code paths can refer to the dirty flags. Subclass should
57 // call resetDirtyFlags after its flush is complete
58 struct {
59 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000060 int fTextureChangedMask;
61 } fDirtyFlags;
62 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages);
63
64 // clears the dirty flags
65 void resetDirtyFlags();
66
67 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000068 struct {
69 bool fScissorEnabled;
70 GrGLIRect fScissorRect;
71 GrGLIRect fViewportRect;
72 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000073
bsalomon@google.com1c13c962011-02-14 16:51:21 +000074 // GrGpu overrides
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000075 virtual void resetContext();
76
bsalomon@google.comfea37b52011-04-25 15:51:06 +000077 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000078 const void* srcData,
79 size_t rowBytes);
80 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
81 bool dynamic);
82 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
83 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000084 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000085 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000086 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
87 int width, int height);
88 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
89 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000090
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000091 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000092
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000093 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000094
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000095 virtual bool onReadPixels(GrRenderTarget* target,
96 int left, int top, int width, int height,
97 GrPixelConfig, void* buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000098
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000099 virtual void onGpuDrawIndexed(GrPrimitiveType type,
100 uint32_t startVertex,
101 uint32_t startIndex,
102 uint32_t vertexCount,
103 uint32_t indexCount);
104 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
105 uint32_t vertexCount,
106 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000108 virtual void clearStencil();
bsalomon@google.com398109c2011-04-14 18:40:27 +0000109 virtual void clearStencilClip(const GrIRect& rect);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000110 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000111
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000113 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000115 // binds appropriate vertex and index buffers, also returns any extra
116 // extra verts or indices to offset by.
117 void setBuffers(bool indexed,
118 int* extraVertexOffset,
119 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000120
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 // flushes state that is common to fixed and programmable GL
122 // dither
123 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 // texture binding
125 // sampler state (filtering, tiling)
126 // FBO binding
127 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000128 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000129
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000130 // subclass should call this to flush the blend state
131 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) {
136 return has_gl_extension_from_string(ext, fExtensionString.c_str());
137 }
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.com6aa25c32011-04-27 19:55:29 +0000153
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000154 // determines valid stencil formats
155 void setupStencilFormats();
156
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 // notify callbacks to update state tracking when related
158 // objects are bound to GL or deleted outside of the class
159 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
160 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
161 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
162 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000163 void notifyTextureDelete(GrGLTexture* texture);
164 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000165
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000166 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000167
bsalomon@google.com0650e812011-04-08 18:07:53 +0000168 bool useSmoothLines();
169
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000170 // bound is region that may be modified and therefore has to be resolved.
171 // NULL means whole target. Can be an empty rect.
172 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000174 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000175
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000176 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000177
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000178 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000179 GrGLenum* internalFormat,
180 GrGLenum* format,
181 GrGLenum* type);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000182 // helpers for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000183 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
184 GrGLenum internalFormat,
185 const void* data,
186 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000187
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000188 bool createRenderTargetObjects(int width, int height,
189 GrGLuint texID,
190 GrGLRenderTarget::Desc* desc);
191
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000192 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000193
194 friend class GrGLVertexBuffer;
195 friend class GrGLIndexBuffer;
196 friend class GrGLTexture;
197 friend class GrGLRenderTarget;
198
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000199
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000200 GrTArray<GrGLStencilBuffer::Format, true> fStencilFormats;
201 // we want to clear stencil buffers when they are created. We want to clear
202 // the entire buffer even if it is larger than the color attachment. We
203 // attach it to this fbo with no color attachment to do the initial clear.
204 GrGLuint fStencilClearFBO;
205
reed@google.comac10a2d2010-12-22 21:39:39 +0000206 bool fHWBlendDisabled;
207
twiz@google.com0f31ca72011-03-18 17:38:11 +0000208 GrGLuint fAASamples[4];
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 enum {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000210 kNone_MSFBO = 0, //<! no support for MSAA FBOs
211 kDesktopARB_MSFBO,//<! GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
212 kDesktopEXT_MSFBO,//<! earlier GL_EXT_framebuffer* extensions
213 kAppleES_MSFBO, //<! GL_APPLE_framebuffer_multisample ES extension
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 } fMSFBOType;
215
216 // Do we have stencil wrap ops.
217 bool fHasStencilWrap;
218
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000219 // The maximum number of fragment uniform vectors (GLES has min. 16).
220 int fMaxFragmentUniformVectors;
221
reed@google.comac10a2d2010-12-22 21:39:39 +0000222 // ES requires an extension to support RGBA8 in RenderBufferStorage
223 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000224
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000225 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000226
bsalomon@google.comfe676522011-06-17 18:12:21 +0000227 // we record what stencil format worked last time to hopefully exit early
228 // from our loop that tries stencil formats and calls check fb status.
229 int fLastSuccessfulStencilFmtIdx;
230
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000231 const GrGLInterface* fGL;
232 GrGLBinding fGLBinding;
233
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 typedef GrGpu INHERITED;
235};
236
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000237#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000238