blob: cfb8b520ac3c03085a04fee415752004c649efd4 [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
commit-bot@chromium.orgc9424b82013-10-30 20:03:16 +000034 const GrGLContext& glContext() const { return fGLContext; }
35
robertphillips@google.com6177e692013-02-28 20:16:25 +000036 const GrGLInterface* glInterface() const { return fGLContext.interface(); }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +000037 const GrGLContextInfo& ctxInfo() const { return fGLContext; }
38 GrGLStandard glStandard() const { return fGLContext.standard(); }
39 GrGLVersion glVersion() const { return fGLContext.version(); }
40 GrGLSLGeneration glslGeneration() const { return fGLContext.glslGeneration(); }
41 const GrGLCaps& glCaps() const { return *fGLContext.caps(); }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000042
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000043 virtual void discard(GrRenderTarget*) SK_OVERRIDE;
44
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +000045 // Used by GrGLProgram and GrGLPathTexGenProgramEffects to configure OpenGL
46 // state.
bsalomon@google.com34cccde2013-01-04 18:34:30 +000047 void bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000048 void setProjectionMatrix(const SkMatrix& matrix,
49 const SkISize& renderTargetSize,
50 GrSurfaceOrigin renderTargetOrigin);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +000051 enum PathTexGenComponents {
52 kS_PathTexGenComponents = 1,
53 kST_PathTexGenComponents = 2,
54 kSTR_PathTexGenComponents = 3
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000055 };
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +000056 void enablePathTexGen(int unitIdx, PathTexGenComponents, const GrGLfloat* coefficients);
57 void enablePathTexGen(int unitIdx, PathTexGenComponents, const SkMatrix& matrix);
58 void flushPathTexGenSettings(int numUsedTexCoordSets);
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +000059 bool shouldUseFixedFunctionTexturing() const {
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +000060 return this->glCaps().pathRenderingSupport();
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +000061 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +000062
bsalomon@google.coma45afcf2013-02-04 18:46:49 +000063 bool programUnitTest(int maxStages);
bsalomon@google.com34cccde2013-01-04 18:34:30 +000064
bsalomon@google.com56d11e02011-11-30 19:59:08 +000065 // GrGpu overrides
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +000066 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
67 GrPixelConfig surfaceConfig) const SK_OVERRIDE;
68 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
69 GrPixelConfig surfaceConfig) const SK_OVERRIDE;
bsalomon@google.com9c680582013-02-06 18:17:50 +000070 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const SK_OVERRIDE;
bsalomon@google.comc4364992011-11-07 15:54:49 +000071 virtual bool readPixelsWillPayForYFlip(
72 GrRenderTarget* renderTarget,
73 int left, int top,
74 int width, int height,
75 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +000076 size_t rowBytes) const SK_OVERRIDE;
77 virtual bool fullReadPixelsIsFasterThanPartial() const SK_OVERRIDE;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000078
bsalomon@google.comeb851172013-04-15 13:51:00 +000079 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) SK_OVERRIDE;
80
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000081 virtual void abandonResources() SK_OVERRIDE;
82
bsalomon@google.com6918d482013-03-07 19:09:11 +000083 // 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 +000084 // bindings. Making the equivalent glBind calls directly will confuse the state tracking.
bsalomon@google.com6918d482013-03-07 19:09:11 +000085 void bindVertexArray(GrGLuint id) {
86 fHWGeometryState.setVertexArrayID(this, id);
87 }
88 void bindIndexBufferAndDefaultVertexArray(GrGLuint id) {
89 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, id);
90 }
91 void bindVertexBuffer(GrGLuint id) {
92 fHWGeometryState.setVertexBufferID(this, id);
93 }
94
95 // These callbacks update state tracking when GL objects are deleted. They are called from
96 // GrGLResource onRelease functions.
97 void notifyVertexArrayDelete(GrGLuint id) {
98 fHWGeometryState.notifyVertexArrayDelete(id);
99 }
100 void notifyVertexBufferDelete(GrGLuint id) {
101 fHWGeometryState.notifyVertexBufferDelete(id);
102 }
103 void notifyIndexBufferDelete(GrGLuint id) {
104 fHWGeometryState.notifyIndexBufferDelete(id);
105 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000106 void notifyTextureDelete(GrGLTexture* texture);
107 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
108
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000109protected:
110 virtual bool onCopySurface(GrSurface* dst,
111 GrSurface* src,
112 const SkIRect& srcRect,
113 const SkIPoint& dstPoint) SK_OVERRIDE;
114
115 virtual bool onCanCopySurface(GrSurface* dst,
116 GrSurface* src,
117 const SkIRect& srcRect,
118 const SkIPoint& dstPoint) SK_OVERRIDE;
119
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000120private:
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000121 // GrGpu overrides
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000122 virtual void onResetContext(uint32_t resetBits) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000123
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000124 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000125 const void* srcData,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000126 size_t rowBytes) SK_OVERRIDE;
krajcevski9c0e6292014-06-02 07:38:14 -0700127 virtual GrTexture* onCreateCompressedTexture(const GrTextureDesc& desc,
128 const void* srcData) SK_OVERRIDE;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000129 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE;
130 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000131 virtual GrPath* onCreatePath(const SkPath&, const SkStrokeRec&) SK_OVERRIDE;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000132 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE;
133 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000134 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000135 int width,
136 int height) SK_OVERRIDE;
137 virtual bool attachStencilBufferToRenderTarget(
138 GrStencilBuffer* sb,
139 GrRenderTarget* rt) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000140
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000141 virtual void onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000142
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000143 virtual bool onReadPixels(GrRenderTarget* target,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000144 int left, int top,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000145 int width, int height,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000146 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000147 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000148 size_t rowBytes) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000149
bsalomon@google.com9c680582013-02-06 18:17:50 +0000150 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000151 int left, int top, int width, int height,
152 GrPixelConfig config, const void* buffer,
153 size_t rowBytes) SK_OVERRIDE;
154
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000155 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE;
156
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000157 virtual void onGpuDraw(const DrawInfo&) SK_OVERRIDE;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000158
sugoi@google.com12b4e272012-12-06 20:13:11 +0000159 virtual void onGpuStencilPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000160 virtual void onGpuDrawPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000161 virtual void onGpuDrawPaths(int, const GrPath**, const SkMatrix*,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000162 SkPath::FillType,
163 SkStrokeRec::Style) SK_OVERRIDE;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000164
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000165 virtual void clearStencil() SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000166 virtual void clearStencilClip(const SkIRect& rect,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000167 bool insideClip) SK_OVERRIDE;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000168 virtual bool flushGraphicsState(DrawType, const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000169
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000170 // GrDrawTarget ovverides
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000171 virtual void didAddGpuTraceMarker() SK_OVERRIDE;
172 virtual void didRemoveGpuTraceMarker() SK_OVERRIDE;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000173
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000174 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000175 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000176
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000177 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset
178 // an into the index buffer. It does not account for drawInfo.startIndex() but rather the start
179 // index is relative to the returned offset.
180 void setupGeometry(const DrawInfo& info, size_t* indexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000181
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000182 // Subclasses should call this to flush the blend state.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000183 // The params should be the final coefficients to apply
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000184 // (after any blending optimizations or dual source blending considerations
185 // have been accounted for).
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000186 void flushBlend(bool isLines, GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000187
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000188 bool hasExtension(const char* ext) const { return fGLContext.hasExtension(ext); }
bsalomon@google.com96399942012-02-13 14:39:16 +0000189
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000190 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000191
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000192 class ProgramCache : public ::SkNoncopyable {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000193 public:
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000194 ProgramCache(GrGpuGL* gpu);
jvanverth@google.com94878772013-03-12 16:00:54 +0000195 ~ProgramCache();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000196
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000197 void abandon();
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000198 GrGLProgram* getProgram(const GrGLProgramDesc& desc,
199 const GrEffectStage* colorStages[],
200 const GrEffectStage* coverageStages[]);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000201
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000202 private:
203 enum {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000204 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
205 // shader before evicting from the cache.
commit-bot@chromium.orgb5564222014-03-28 15:52:18 +0000206 kMaxEntries = 128,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000207 kHashBits = 6,
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000208 };
209
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000210 struct Entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000211
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000212 struct ProgDescLess;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000213
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000214 // binary search for entry matching desc. returns index into fEntries that matches desc or ~
215 // of the index of where it should be inserted.
216 int search(const GrGLProgramDesc& desc) const;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000217
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000218 // sorted array of all the entries
219 Entry* fEntries[kMaxEntries];
220 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary
221 // searching fEntries.
222 Entry* fHashTable[1 << kHashBits];
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000223
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000224 int fCount;
225 unsigned int fCurrLRUStamp;
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000226 GrGpuGL* fGpu;
jvanverth@google.com94878772013-03-12 16:00:54 +0000227#ifdef PROGRAM_CACHE_STATS
228 int fTotalRequests;
229 int fCacheMisses;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000230 int fHashMisses; // cache hit but hash table missed
jvanverth@google.com94878772013-03-12 16:00:54 +0000231#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000232 };
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000233
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000234 // flushes dithering, color-mask, and face culling stat
235 void flushMiscFixedFunctionState();
236
bsalomon@google.coma3201942012-06-21 19:58:20 +0000237 // flushes the scissor. see the note on flushBoundTextureAndParams about
238 // flushing the scissor after that function is called.
239 void flushScissor();
240
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000241 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000242
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000243 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000244 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000245
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000246 // sets a texture unit to use for texture operations other than binding a texture to a program.
247 // ensures that such operations don't negatively interact with tracking bound textures.
248 void setScratchTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000249
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000250 // bound is region that may be modified and therefore has to be resolved.
251 // NULL means whole target. Can be an empty rect.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000252 void flushRenderTarget(const SkIRect* bound);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000253 void flushStencil(DrawType);
254 void flushAAState(DrawType);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000255 void flushPathStencilSettings(SkPath::FillType fill);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000256
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000257 bool configToGLFormats(GrPixelConfig config,
258 bool getSizedInternal,
259 GrGLenum* internalFormat,
260 GrGLenum* externalFormat,
261 GrGLenum* externalType);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000262 // helper for onCreateTexture and writeTexturePixels
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000263 bool uploadTexData(const GrGLTexture::Desc& desc,
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000264 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000265 int left, int top, int width, int height,
266 GrPixelConfig dataConfig,
267 const void* data,
268 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000269
krajcevski9c0e6292014-06-02 07:38:14 -0700270 // helper for onCreateCompressedTexture
271 bool uploadCompressedTexData(const GrGLTexture::Desc& desc,
272 const void* data);
273
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000274 bool createRenderTargetObjects(int width, int height,
275 GrGLuint texID,
276 GrGLRenderTarget::Desc* desc);
277
robertphillips@google.com6177e692013-02-28 20:16:25 +0000278 GrGLContext fGLContext;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000279
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000280 // GL program-related state
281 ProgramCache* fProgramCache;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000282 SkAutoTUnref<GrGLProgram> fCurrentProgram;
bsalomon@google.com49209392012-06-05 15:13:46 +0000283
284 ///////////////////////////////////////////////////////////////////////////
285 ///@name Caching of GL State
286 ///@{
287 int fHWActiveTextureUnitIdx;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000288 GrGLuint fHWProgramID;
bsalomon@google.com91207482013-02-12 21:45:24 +0000289
290 GrGLProgram::SharedGLState fSharedGLProgramState;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000291
bsalomon@google.com49209392012-06-05 15:13:46 +0000292 enum TriState {
293 kNo_TriState,
294 kYes_TriState,
295 kUnknown_TriState
296 };
297
bsalomon@google.coma3201942012-06-21 19:58:20 +0000298 // last scissor / viewport scissor state seen by the GL.
299 struct {
300 TriState fEnabled;
301 GrGLIRect fRect;
302 void invalidate() {
303 fEnabled = kUnknown_TriState;
304 fRect.invalidate();
305 }
306 } fHWScissorSettings;
307
308 GrGLIRect fHWViewport;
309
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000310 /**
311 * Tracks bound vertex and index buffers and vertex attrib array state.
312 */
313 class HWGeometryState {
314 public:
bsalomon@google.com6918d482013-03-07 19:09:11 +0000315 HWGeometryState() { fVBOVertexArray = NULL; this->invalidate(); }
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000316
bsalomon@google.com6918d482013-03-07 19:09:11 +0000317 ~HWGeometryState() { SkSafeUnref(fVBOVertexArray); }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000318
bsalomon@google.com6918d482013-03-07 19:09:11 +0000319 void invalidate() {
320 fBoundVertexArrayIDIsValid = false;
321 fBoundVertexBufferIDIsValid = false;
322 fDefaultVertexArrayBoundIndexBufferID = false;
323 fDefaultVertexArrayBoundIndexBufferIDIsValid = false;
324 fDefaultVertexArrayAttribState.invalidate();
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000325 if (NULL != fVBOVertexArray) {
326 fVBOVertexArray->invalidateCachedState();
327 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000328 }
329
330 void notifyVertexArrayDelete(GrGLuint id) {
331 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
332 // Does implicit bind to 0
333 fBoundVertexArrayID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000334 }
335 }
336
bsalomon@google.com6918d482013-03-07 19:09:11 +0000337 void setVertexArrayID(GrGpuGL* gpu, GrGLuint arrayID) {
338 if (!gpu->glCaps().vertexArrayObjectSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000339 SkASSERT(0 == arrayID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000340 return;
341 }
342 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
343 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
344 fBoundVertexArrayIDIsValid = true;
345 fBoundVertexArrayID = arrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000346 }
347 }
348
bsalomon@google.come49ad452013-02-20 19:33:20 +0000349 void notifyVertexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000350 if (fBoundVertexBufferIDIsValid && id == fBoundVertexBufferID) {
351 fBoundVertexBufferID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000352 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000353 if (NULL != fVBOVertexArray) {
354 fVBOVertexArray->notifyVertexBufferDelete(id);
355 }
356 fDefaultVertexArrayAttribState.notifyVertexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000357 }
358
bsalomon@google.come49ad452013-02-20 19:33:20 +0000359 void notifyIndexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000360 if (fDefaultVertexArrayBoundIndexBufferIDIsValid &&
361 id == fDefaultVertexArrayBoundIndexBufferID) {
362 fDefaultVertexArrayBoundIndexBufferID = 0;
363 }
364 if (NULL != fVBOVertexArray) {
365 fVBOVertexArray->notifyIndexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000366 }
367 }
368
bsalomon@google.com6918d482013-03-07 19:09:11 +0000369 void setVertexBufferID(GrGpuGL* gpu, GrGLuint id) {
370 if (!fBoundVertexBufferIDIsValid || id != fBoundVertexBufferID) {
371 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ARRAY_BUFFER, id));
372 fBoundVertexBufferIDIsValid = true;
373 fBoundVertexBufferID = id;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000374 }
375 }
376
bsalomon@google.com6918d482013-03-07 19:09:11 +0000377 /**
378 * Binds the default vertex array and binds the index buffer. This is used when binding
379 * an index buffer in order to update it.
380 */
381 void setIndexBufferIDOnDefaultVertexArray(GrGpuGL* gpu, GrGLuint id) {
382 this->setVertexArrayID(gpu, 0);
383 if (!fDefaultVertexArrayBoundIndexBufferIDIsValid ||
384 id != fDefaultVertexArrayBoundIndexBufferID) {
385 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
386 fDefaultVertexArrayBoundIndexBufferIDIsValid = true;
387 fDefaultVertexArrayBoundIndexBufferID = id;
388 }
389 }
390
391 /**
392 * Binds the vertex array object that should be used to render from the vertex buffer.
393 * The vertex array is bound and its attrib array state object is returned. The vertex
394 * 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 +0000395 * returned GrGLAttribArrayState should be used to set vertex attribute arrays.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000396 */
397 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGpuGL* gpu,
398 const GrGLVertexBuffer* vbuffer,
399 const GrGLIndexBuffer* ibuffer);
400
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000401 private:
bsalomon@google.com6918d482013-03-07 19:09:11 +0000402 GrGLuint fBoundVertexArrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000403 GrGLuint fBoundVertexBufferID;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000404 bool fBoundVertexArrayIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000405 bool fBoundVertexBufferIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000406
bsalomon@google.com6918d482013-03-07 19:09:11 +0000407 GrGLuint fDefaultVertexArrayBoundIndexBufferID;
408 bool fDefaultVertexArrayBoundIndexBufferIDIsValid;
409 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
410 // is bound. However, this class is internal to GrGpuGL and this object never leaks out of
411 // GrGpuGL.
412 GrGLAttribArrayState fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000413
bsalomon@google.com6918d482013-03-07 19:09:11 +0000414 // This is used when we're using a core profile and the vertices are in a VBO.
415 GrGLVertexArray* fVBOVertexArray;
bsalomon@google.com49209392012-06-05 15:13:46 +0000416 } fHWGeometryState;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000417
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000418 struct {
419 GrBlendCoeff fSrcCoeff;
420 GrBlendCoeff fDstCoeff;
421 GrColor fConstColor;
422 bool fConstColorValid;
423 TriState fEnabled;
424
425 void invalidate() {
bsalomon@google.com47059542012-06-06 20:51:20 +0000426 fSrcCoeff = kInvalid_GrBlendCoeff;
427 fDstCoeff = kInvalid_GrBlendCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000428 fConstColorValid = false;
429 fEnabled = kUnknown_TriState;
430 }
431 } fHWBlendState;
432
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000433 struct {
434 TriState fMSAAEnabled;
435 TriState fSmoothLineEnabled;
436 void invalidate() {
437 fMSAAEnabled = kUnknown_TriState;
438 fSmoothLineEnabled = kUnknown_TriState;
439 }
440 } fHWAAState;
441
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000442
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000443 GrGLProgram::MatrixState fHWProjectionMatrixState;
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000444
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000445 GrStencilSettings fHWStencilSettings;
446 TriState fHWStencilTestEnabled;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000447 GrStencilSettings fHWPathStencilSettings;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000448
449 GrDrawState::DrawFace fHWDrawFace;
450 TriState fHWWriteToColor;
451 TriState fHWDitherEnabled;
452 GrRenderTarget* fHWBoundRenderTarget;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000453 SkTArray<GrTexture*, true> fHWBoundTextures;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000454
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000455 struct PathTexGenData {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000456 GrGLenum fMode;
457 GrGLint fNumComponents;
458 GrGLfloat fCoefficients[3 * 3];
459 };
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000460 int fHWActivePathTexGenSets;
461 SkTArray<PathTexGenData, true> fHWPathTexGenSettings;
bsalomon@google.com49209392012-06-05 15:13:46 +0000462 ///@}
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000463
bsalomon@google.comfe676522011-06-17 18:12:21 +0000464 // we record what stencil format worked last time to hopefully exit early
465 // from our loop that tries stencil formats and calls check fb status.
466 int fLastSuccessfulStencilFmtIdx;
467
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 typedef GrGpu INHERITED;
469};
470
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000471#endif