blob: 2a0587be11b3e7845f369681273ae5ed37063bb2 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrGpuGL_DEFINED
9#define GrGpuGL_DEFINED
10
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000011#include "GrBinHashKey.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012#include "GrDrawState.h"
robertphillips@google.com6177e692013-02-28 20:16:25 +000013#include "GrGLContext.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000014#include "GrGLIRect.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000015#include "GrGLIndexBuffer.h"
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000016#include "GrGLProgram.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000017#include "GrGLStencilBuffer.h"
18#include "GrGLTexture.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000019#include "GrGLVertexArray.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000020#include "GrGLVertexBuffer.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000021#include "GrGpu.h"
mtklein@google.com4c2af742013-10-21 21:04:06 +000022#include "GrTHashTable.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000023#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
commit-bot@chromium.org174db772013-03-21 12:46:01 +000025#ifdef SK_DEVELOPER
jvanverth@google.com94878772013-03-12 16:00:54 +000026#define PROGRAM_CACHE_STATS
27#endif
28
reed@google.comac10a2d2010-12-22 21:39:39 +000029class GrGpuGL : public GrGpu {
30public:
robertphillips@google.com6177e692013-02-28 20:16:25 +000031 GrGpuGL(const GrGLContext& ctx, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000032 virtual ~GrGpuGL();
33
robertphillips@google.com6177e692013-02-28 20:16:25 +000034 const GrGLInterface* glInterface() const { return fGLContext.interface(); }
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000035 const GrGLContextInfo& ctxInfo() const { return fGLContext.info(); }
robertphillips@google.com6177e692013-02-28 20:16:25 +000036 GrGLBinding glBinding() const { return fGLContext.info().binding(); }
37 GrGLVersion glVersion() const { return fGLContext.info().version(); }
38 GrGLSLGeneration glslGeneration() const { return fGLContext.info().glslGeneration(); }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000039
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000040 // Used by GrGLProgram and GrGLTexGenProgramEffects to configure OpenGL state.
bsalomon@google.com34cccde2013-01-04 18:34:30 +000041 void bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000042 void setProjectionMatrix(const SkMatrix& matrix,
43 const SkISize& renderTargetSize,
44 GrSurfaceOrigin renderTargetOrigin);
45 enum TexGenComponents {
46 kS_TexGenComponents = 1,
47 kST_TexGenComponents = 2,
48 kSTR_TexGenComponents = 3
49 };
50 void enableTexGen(int unitIdx, TexGenComponents, const GrGLfloat* coefficients);
51 void enableTexGen(int unitIdx, TexGenComponents, const SkMatrix& matrix);
52 void disableUnusedTexGen(int numUsedTexCoordSets);
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +000053 bool shouldUseFixedFunctionTexturing() const {
54 return this->glCaps().fixedFunctionSupport() &&
55 this->glCaps().pathRenderingSupport();
56 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +000057
bsalomon@google.coma45afcf2013-02-04 18:46:49 +000058 bool programUnitTest(int maxStages);
bsalomon@google.com34cccde2013-01-04 18:34:30 +000059
bsalomon@google.com56d11e02011-11-30 19:59:08 +000060 // GrGpu overrides
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +000061 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
62 GrPixelConfig surfaceConfig) const SK_OVERRIDE;
63 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
64 GrPixelConfig surfaceConfig) const SK_OVERRIDE;
bsalomon@google.com9c680582013-02-06 18:17:50 +000065 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const SK_OVERRIDE;
bsalomon@google.comc4364992011-11-07 15:54:49 +000066 virtual bool readPixelsWillPayForYFlip(
67 GrRenderTarget* renderTarget,
68 int left, int top,
69 int width, int height,
70 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +000071 size_t rowBytes) const SK_OVERRIDE;
72 virtual bool fullReadPixelsIsFasterThanPartial() const SK_OVERRIDE;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000073
bsalomon@google.comeb851172013-04-15 13:51:00 +000074 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) SK_OVERRIDE;
75
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000076 virtual void abandonResources() SK_OVERRIDE;
77
bsalomon@google.combcce8922013-03-25 15:38:39 +000078 const GrGLCaps& glCaps() const { return *fGLContext.info().caps(); }
bsalomon@google.com91207482013-02-12 21:45:24 +000079
bsalomon@google.com6918d482013-03-07 19:09:11 +000080 // These functions should be used to bind GL objects. They track the GL state and skip redundant
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +000081 // bindings. Making the equivalent glBind calls directly will confuse the state tracking.
bsalomon@google.com6918d482013-03-07 19:09:11 +000082 void bindVertexArray(GrGLuint id) {
83 fHWGeometryState.setVertexArrayID(this, id);
84 }
85 void bindIndexBufferAndDefaultVertexArray(GrGLuint id) {
86 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, id);
87 }
88 void bindVertexBuffer(GrGLuint id) {
89 fHWGeometryState.setVertexBufferID(this, id);
90 }
91
92 // These callbacks update state tracking when GL objects are deleted. They are called from
93 // GrGLResource onRelease functions.
94 void notifyVertexArrayDelete(GrGLuint id) {
95 fHWGeometryState.notifyVertexArrayDelete(id);
96 }
97 void notifyVertexBufferDelete(GrGLuint id) {
98 fHWGeometryState.notifyVertexBufferDelete(id);
99 }
100 void notifyIndexBufferDelete(GrGLuint id) {
101 fHWGeometryState.notifyIndexBufferDelete(id);
102 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000103 void notifyTextureDelete(GrGLTexture* texture);
104 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
105
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000106protected:
107 virtual bool onCopySurface(GrSurface* dst,
108 GrSurface* src,
109 const SkIRect& srcRect,
110 const SkIPoint& dstPoint) SK_OVERRIDE;
111
112 virtual bool onCanCopySurface(GrSurface* dst,
113 GrSurface* src,
114 const SkIRect& srcRect,
115 const SkIPoint& dstPoint) SK_OVERRIDE;
116
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000117private:
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000118 // GrGpu overrides
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000119 virtual void onResetContext(uint32_t resetBits) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000120
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000121 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000122 const void* srcData,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000123 size_t rowBytes) SK_OVERRIDE;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000124 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE;
125 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000126 virtual GrPath* onCreatePath(const SkPath&, const SkStrokeRec&) SK_OVERRIDE;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000127 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE;
128 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000129 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000130 int width,
131 int height) SK_OVERRIDE;
132 virtual bool attachStencilBufferToRenderTarget(
133 GrStencilBuffer* sb,
134 GrRenderTarget* rt) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000135
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000136 virtual void onClear(const SkIRect* rect, GrColor color) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000137
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000138 virtual void onForceRenderTargetFlush() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000139
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000140 virtual bool onReadPixels(GrRenderTarget* target,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141 int left, int top,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000142 int width, int height,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000143 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000144 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000145 size_t rowBytes) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000146
bsalomon@google.com9c680582013-02-06 18:17:50 +0000147 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000148 int left, int top, int width, int height,
149 GrPixelConfig config, const void* buffer,
150 size_t rowBytes) SK_OVERRIDE;
151
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000152 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE;
153
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000154 virtual void onGpuDraw(const DrawInfo&) SK_OVERRIDE;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000155
sugoi@google.com12b4e272012-12-06 20:13:11 +0000156 virtual void onGpuStencilPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000157 virtual void onGpuDrawPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000158
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000159 virtual void clearStencil() SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000160 virtual void clearStencilClip(const SkIRect& rect,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000161 bool insideClip) SK_OVERRIDE;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000162 virtual bool flushGraphicsState(DrawType, const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000163
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000164 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000165 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000166
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000167 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset
168 // an into the index buffer. It does not account for drawInfo.startIndex() but rather the start
169 // index is relative to the returned offset.
170 void setupGeometry(const DrawInfo& info, size_t* indexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000171
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000172 // Subclasses should call this to flush the blend state.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000173 // The params should be the final coefficients to apply
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000174 // (after any blending optimizations or dual source blending considerations
175 // have been accounted for).
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000176 void flushBlend(bool isLines, GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000177
robertphillips@google.com6177e692013-02-28 20:16:25 +0000178 bool hasExtension(const char* ext) const { return fGLContext.info().hasExtension(ext); }
bsalomon@google.com96399942012-02-13 14:39:16 +0000179
robertphillips@google.com6177e692013-02-28 20:16:25 +0000180 const GrGLContext& glContext() const { return fGLContext; }
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000181
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000182 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000183
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000184 class ProgramCache : public ::SkNoncopyable {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000185 public:
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000186 ProgramCache(GrGpuGL* gpu);
jvanverth@google.com94878772013-03-12 16:00:54 +0000187 ~ProgramCache();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000188
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000189 void abandon();
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000190 GrGLProgram* getProgram(const GrGLProgramDesc& desc,
191 const GrEffectStage* colorStages[],
192 const GrEffectStage* coverageStages[]);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000193
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000194 private:
195 enum {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000196 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
197 // shader before evicting from the cache.
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000198 kMaxEntries = 32,
199 kHashBits = 6,
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000200 };
201
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000202 struct Entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000203
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000204 struct ProgDescLess;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000205
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000206 // binary search for entry matching desc. returns index into fEntries that matches desc or ~
207 // of the index of where it should be inserted.
208 int search(const GrGLProgramDesc& desc) const;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000209
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000210 // sorted array of all the entries
211 Entry* fEntries[kMaxEntries];
212 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary
213 // searching fEntries.
214 Entry* fHashTable[1 << kHashBits];
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000215
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000216 int fCount;
217 unsigned int fCurrLRUStamp;
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000218 GrGpuGL* fGpu;
jvanverth@google.com94878772013-03-12 16:00:54 +0000219#ifdef PROGRAM_CACHE_STATS
220 int fTotalRequests;
221 int fCacheMisses;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000222 int fHashMisses; // cache hit but hash table missed
jvanverth@google.com94878772013-03-12 16:00:54 +0000223#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000224 };
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000225
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000226 // flushes dithering, color-mask, and face culling stat
227 void flushMiscFixedFunctionState();
228
bsalomon@google.coma3201942012-06-21 19:58:20 +0000229 // flushes the scissor. see the note on flushBoundTextureAndParams about
230 // flushing the scissor after that function is called.
231 void flushScissor();
232
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000233 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000234
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000235 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000236 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000237
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000238 // sets a texture unit to use for texture operations other than binding a texture to a program.
239 // ensures that such operations don't negatively interact with tracking bound textures.
240 void setScratchTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000242 // bound is region that may be modified and therefore has to be resolved.
243 // NULL means whole target. Can be an empty rect.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000244 void flushRenderTarget(const SkIRect* bound);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000245 void flushStencil(DrawType);
246 void flushAAState(DrawType);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000247 void flushPathStencilSettings(SkPath::FillType fill);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000248
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000249 bool configToGLFormats(GrPixelConfig config,
250 bool getSizedInternal,
251 GrGLenum* internalFormat,
252 GrGLenum* externalFormat,
253 GrGLenum* externalType);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000254 // helper for onCreateTexture and writeTexturePixels
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000255 bool uploadTexData(const GrGLTexture::Desc& desc,
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000256 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000257 int left, int top, int width, int height,
258 GrPixelConfig dataConfig,
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
robertphillips@google.com6177e692013-02-28 20:16:25 +0000266 GrGLContext fGLContext;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000267
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000268 // GL program-related state
269 ProgramCache* fProgramCache;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000270 SkAutoTUnref<GrGLProgram> fCurrentProgram;
bsalomon@google.com49209392012-06-05 15:13:46 +0000271
272 ///////////////////////////////////////////////////////////////////////////
273 ///@name Caching of GL State
274 ///@{
275 int fHWActiveTextureUnitIdx;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000276 GrGLuint fHWProgramID;
bsalomon@google.com91207482013-02-12 21:45:24 +0000277
278 GrGLProgram::SharedGLState fSharedGLProgramState;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000279
bsalomon@google.com49209392012-06-05 15:13:46 +0000280 enum TriState {
281 kNo_TriState,
282 kYes_TriState,
283 kUnknown_TriState
284 };
285
bsalomon@google.coma3201942012-06-21 19:58:20 +0000286 // last scissor / viewport scissor state seen by the GL.
287 struct {
288 TriState fEnabled;
289 GrGLIRect fRect;
290 void invalidate() {
291 fEnabled = kUnknown_TriState;
292 fRect.invalidate();
293 }
294 } fHWScissorSettings;
295
296 GrGLIRect fHWViewport;
297
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000298 /**
299 * Tracks bound vertex and index buffers and vertex attrib array state.
300 */
301 class HWGeometryState {
302 public:
bsalomon@google.com6918d482013-03-07 19:09:11 +0000303 HWGeometryState() { fVBOVertexArray = NULL; this->invalidate(); }
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000304
bsalomon@google.com6918d482013-03-07 19:09:11 +0000305 ~HWGeometryState() { SkSafeUnref(fVBOVertexArray); }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000306
bsalomon@google.com6918d482013-03-07 19:09:11 +0000307 void invalidate() {
308 fBoundVertexArrayIDIsValid = false;
309 fBoundVertexBufferIDIsValid = false;
310 fDefaultVertexArrayBoundIndexBufferID = false;
311 fDefaultVertexArrayBoundIndexBufferIDIsValid = false;
312 fDefaultVertexArrayAttribState.invalidate();
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000313 if (NULL != fVBOVertexArray) {
314 fVBOVertexArray->invalidateCachedState();
315 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000316 }
317
318 void notifyVertexArrayDelete(GrGLuint id) {
319 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
320 // Does implicit bind to 0
321 fBoundVertexArrayID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000322 }
323 }
324
bsalomon@google.com6918d482013-03-07 19:09:11 +0000325 void setVertexArrayID(GrGpuGL* gpu, GrGLuint arrayID) {
326 if (!gpu->glCaps().vertexArrayObjectSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000327 SkASSERT(0 == arrayID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000328 return;
329 }
330 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
331 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
332 fBoundVertexArrayIDIsValid = true;
333 fBoundVertexArrayID = arrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000334 }
335 }
336
bsalomon@google.come49ad452013-02-20 19:33:20 +0000337 void notifyVertexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000338 if (fBoundVertexBufferIDIsValid && id == fBoundVertexBufferID) {
339 fBoundVertexBufferID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000340 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000341 if (NULL != fVBOVertexArray) {
342 fVBOVertexArray->notifyVertexBufferDelete(id);
343 }
344 fDefaultVertexArrayAttribState.notifyVertexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000345 }
346
bsalomon@google.come49ad452013-02-20 19:33:20 +0000347 void notifyIndexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000348 if (fDefaultVertexArrayBoundIndexBufferIDIsValid &&
349 id == fDefaultVertexArrayBoundIndexBufferID) {
350 fDefaultVertexArrayBoundIndexBufferID = 0;
351 }
352 if (NULL != fVBOVertexArray) {
353 fVBOVertexArray->notifyIndexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000354 }
355 }
356
bsalomon@google.com6918d482013-03-07 19:09:11 +0000357 void setVertexBufferID(GrGpuGL* gpu, GrGLuint id) {
358 if (!fBoundVertexBufferIDIsValid || id != fBoundVertexBufferID) {
359 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ARRAY_BUFFER, id));
360 fBoundVertexBufferIDIsValid = true;
361 fBoundVertexBufferID = id;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000362 }
363 }
364
bsalomon@google.com6918d482013-03-07 19:09:11 +0000365 /**
366 * Binds the default vertex array and binds the index buffer. This is used when binding
367 * an index buffer in order to update it.
368 */
369 void setIndexBufferIDOnDefaultVertexArray(GrGpuGL* gpu, GrGLuint id) {
370 this->setVertexArrayID(gpu, 0);
371 if (!fDefaultVertexArrayBoundIndexBufferIDIsValid ||
372 id != fDefaultVertexArrayBoundIndexBufferID) {
373 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
374 fDefaultVertexArrayBoundIndexBufferIDIsValid = true;
375 fDefaultVertexArrayBoundIndexBufferID = id;
376 }
377 }
378
379 /**
380 * Binds the vertex array object that should be used to render from the vertex buffer.
381 * The vertex array is bound and its attrib array state object is returned. The vertex
382 * buffer is bound. The index buffer (if non-NULL) is bound to the vertex array. The
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000383 * returned GrGLAttribArrayState should be used to set vertex attribute arrays.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000384 */
385 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGpuGL* gpu,
386 const GrGLVertexBuffer* vbuffer,
387 const GrGLIndexBuffer* ibuffer);
388
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000389 private:
bsalomon@google.com6918d482013-03-07 19:09:11 +0000390 GrGLuint fBoundVertexArrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000391 GrGLuint fBoundVertexBufferID;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000392 bool fBoundVertexArrayIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000393 bool fBoundVertexBufferIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000394
bsalomon@google.com6918d482013-03-07 19:09:11 +0000395 GrGLuint fDefaultVertexArrayBoundIndexBufferID;
396 bool fDefaultVertexArrayBoundIndexBufferIDIsValid;
397 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
398 // is bound. However, this class is internal to GrGpuGL and this object never leaks out of
399 // GrGpuGL.
400 GrGLAttribArrayState fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000401
bsalomon@google.com6918d482013-03-07 19:09:11 +0000402 // This is used when we're using a core profile and the vertices are in a VBO.
403 GrGLVertexArray* fVBOVertexArray;
bsalomon@google.com49209392012-06-05 15:13:46 +0000404 } fHWGeometryState;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000405
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000406 struct {
407 GrBlendCoeff fSrcCoeff;
408 GrBlendCoeff fDstCoeff;
409 GrColor fConstColor;
410 bool fConstColorValid;
411 TriState fEnabled;
412
413 void invalidate() {
bsalomon@google.com47059542012-06-06 20:51:20 +0000414 fSrcCoeff = kInvalid_GrBlendCoeff;
415 fDstCoeff = kInvalid_GrBlendCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000416 fConstColorValid = false;
417 fEnabled = kUnknown_TriState;
418 }
419 } fHWBlendState;
420
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000421 struct {
422 TriState fMSAAEnabled;
423 TriState fSmoothLineEnabled;
424 void invalidate() {
425 fMSAAEnabled = kUnknown_TriState;
426 fSmoothLineEnabled = kUnknown_TriState;
427 }
428 } fHWAAState;
429
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000430
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000431 GrGLProgram::MatrixState fHWProjectionMatrixState;
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000432
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000433 GrStencilSettings fHWStencilSettings;
434 TriState fHWStencilTestEnabled;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000435 GrStencilSettings fHWPathStencilSettings;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000436
437 GrDrawState::DrawFace fHWDrawFace;
438 TriState fHWWriteToColor;
439 TriState fHWDitherEnabled;
440 GrRenderTarget* fHWBoundRenderTarget;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000441 SkTArray<GrTexture*, true> fHWBoundTextures;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000442
443 struct TexGenData {
444 GrGLenum fMode;
445 GrGLint fNumComponents;
446 GrGLfloat fCoefficients[3 * 3];
447 };
448 int fHWActiveTexGenSets;
449 SkTArray<TexGenData, true> fHWTexGenSettings;
bsalomon@google.com49209392012-06-05 15:13:46 +0000450 ///@}
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000451
bsalomon@google.comfe676522011-06-17 18:12:21 +0000452 // we record what stencil format worked last time to hopefully exit early
453 // from our loop that tries stencil formats and calls check fb status.
454 int fLastSuccessfulStencilFmtIdx;
455
reed@google.comac10a2d2010-12-22 21:39:39 +0000456 typedef GrGpu INHERITED;
457};
458
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000459#endif