blob: 778ee160e1da9998b7d015cc46cbaceac0162527 [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
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000011#include "GrDrawState.h"
robertphillips@google.com6177e692013-02-28 20:16:25 +000012#include "GrGLContext.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000013#include "GrGLIRect.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000014#include "GrGLIndexBuffer.h"
kkinnunenccdaa042014-08-20 01:36:23 -070015#include "GrGLPathRendering.h"
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000016#include "GrGLProgram.h"
bsalomon37dd3312014-11-03 08:47:23 -080017#include "GrGLRenderTarget.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000018#include "GrGLStencilBuffer.h"
19#include "GrGLTexture.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000020#include "GrGLVertexArray.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000021#include "GrGLVertexBuffer.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000022#include "GrGpu.h"
joshualitt79f8fae2014-10-28 17:59:26 -070023#include "GrOptDrawState.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000024#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
commit-bot@chromium.org174db772013-03-21 12:46:01 +000026#ifdef SK_DEVELOPER
jvanverth@google.com94878772013-03-12 16:00:54 +000027#define PROGRAM_CACHE_STATS
28#endif
29
reed@google.comac10a2d2010-12-22 21:39:39 +000030class GrGpuGL : public GrGpu {
31public:
robertphillips@google.com6177e692013-02-28 20:16:25 +000032 GrGpuGL(const GrGLContext& ctx, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000033 virtual ~GrGpuGL();
34
robertphillipse3371302014-09-17 06:01:06 -070035 virtual void contextAbandoned() SK_OVERRIDE;
bsalomonc8dc1f72014-08-21 13:02:13 -070036
commit-bot@chromium.orgc9424b82013-10-30 20:03:16 +000037 const GrGLContext& glContext() const { return fGLContext; }
38
robertphillips@google.com6177e692013-02-28 20:16:25 +000039 const GrGLInterface* glInterface() const { return fGLContext.interface(); }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +000040 const GrGLContextInfo& ctxInfo() const { return fGLContext; }
41 GrGLStandard glStandard() const { return fGLContext.standard(); }
42 GrGLVersion glVersion() const { return fGLContext.version(); }
43 GrGLSLGeneration glslGeneration() const { return fGLContext.glslGeneration(); }
44 const GrGLCaps& glCaps() const { return *fGLContext.caps(); }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000045
kkinnunenccdaa042014-08-20 01:36:23 -070046 GrGLPathRendering* glPathRendering() {
cdaltonc7103a12014-08-11 14:05:05 -070047 SkASSERT(glCaps().pathRenderingSupport());
kkinnunenccdaa042014-08-20 01:36:23 -070048 return static_cast<GrGLPathRendering*>(pathRendering());
cdaltonc7103a12014-08-11 14:05:05 -070049 }
50
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000051 virtual void discard(GrRenderTarget*) SK_OVERRIDE;
52
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +000053 // Used by GrGLProgram and GrGLPathTexGenProgramEffects to configure OpenGL
54 // state.
bsalomon@google.com34cccde2013-01-04 18:34:30 +000055 void bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture);
kkinnunenccdaa042014-08-20 01:36:23 -070056
bsalomon@google.com56d11e02011-11-30 19:59:08 +000057 // GrGpu overrides
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +000058 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
59 GrPixelConfig surfaceConfig) const SK_OVERRIDE;
60 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
61 GrPixelConfig surfaceConfig) const SK_OVERRIDE;
bsalomon@google.com9c680582013-02-06 18:17:50 +000062 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const SK_OVERRIDE;
bsalomon@google.comc4364992011-11-07 15:54:49 +000063 virtual bool readPixelsWillPayForYFlip(
64 GrRenderTarget* renderTarget,
65 int left, int top,
66 int width, int height,
67 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +000068 size_t rowBytes) const SK_OVERRIDE;
69 virtual bool fullReadPixelsIsFasterThanPartial() const SK_OVERRIDE;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000070
bsalomonf2703d82014-10-28 14:33:06 -070071 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE;
bsalomon@google.comeb851172013-04-15 13:51:00 +000072
bsalomon@google.com6918d482013-03-07 19:09:11 +000073 // 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 +000074 // bindings. Making the equivalent glBind calls directly will confuse the state tracking.
bsalomon@google.com6918d482013-03-07 19:09:11 +000075 void bindVertexArray(GrGLuint id) {
76 fHWGeometryState.setVertexArrayID(this, id);
77 }
78 void bindIndexBufferAndDefaultVertexArray(GrGLuint id) {
79 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, id);
80 }
81 void bindVertexBuffer(GrGLuint id) {
82 fHWGeometryState.setVertexBufferID(this, id);
83 }
84
85 // These callbacks update state tracking when GL objects are deleted. They are called from
86 // GrGLResource onRelease functions.
87 void notifyVertexArrayDelete(GrGLuint id) {
88 fHWGeometryState.notifyVertexArrayDelete(id);
89 }
90 void notifyVertexBufferDelete(GrGLuint id) {
91 fHWGeometryState.notifyVertexBufferDelete(id);
92 }
93 void notifyIndexBufferDelete(GrGLuint id) {
94 fHWGeometryState.notifyIndexBufferDelete(id);
95 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +000096
joshualitta7024152014-11-03 14:16:35 -080097 // DrawTarget overrides
98 virtual bool copySurface(GrSurface* dst,
99 GrSurface* src,
100 const SkIRect& srcRect,
101 const SkIPoint& dstPoint) SK_OVERRIDE;
102
joshualitt9853cce2014-11-17 14:22:48 -0800103 virtual bool canCopySurface(const GrSurface* dst,
104 const GrSurface* src,
joshualitta7024152014-11-03 14:16:35 -0800105 const SkIRect& srcRect,
106 const SkIPoint& dstPoint) SK_OVERRIDE;
107
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000108protected:
joshualitt79f8fae2014-10-28 17:59:26 -0700109 virtual void buildProgramDesc(const GrOptDrawState&,
110 const GrProgramDesc::DescInfo&,
111 GrGpu::DrawType,
112 const GrDeviceCoordTexture* dstCopy,
113 GrProgramDesc*) SK_OVERRIDE;
114
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000115private:
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000116 // GrGpu overrides
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000117 virtual void onResetContext(uint32_t resetBits) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000118
bsalomonf2703d82014-10-28 14:33:06 -0700119 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000120 const void* srcData,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000121 size_t rowBytes) SK_OVERRIDE;
bsalomonf2703d82014-10-28 14:33:06 -0700122 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
krajcevski9c0e6292014-06-02 07:38:14 -0700123 const void* srcData) 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;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000126 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE;
127 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000128 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000129 int width,
130 int height) SK_OVERRIDE;
131 virtual bool attachStencilBufferToRenderTarget(
132 GrStencilBuffer* sb,
133 GrRenderTarget* rt) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000134
joshualitt4b68ec02014-11-07 14:11:45 -0800135 virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
136 bool canIgnoreRect) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000137
joshualitt6db519c2014-10-29 08:48:18 -0700138 virtual void onClearStencilClip(GrRenderTarget*,
139 const SkIRect& rect,
140 bool insideClip) SK_OVERRIDE;
141
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000142 virtual bool onReadPixels(GrRenderTarget* target,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000143 int left, int top,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000144 int width, int height,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000146 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000147 size_t rowBytes) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000148
bsalomon@google.com9c680582013-02-06 18:17:50 +0000149 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000150 int left, int top, int width, int height,
151 GrPixelConfig config, const void* buffer,
152 size_t rowBytes) SK_OVERRIDE;
153
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000154 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE;
155
joshualittd53a8272014-11-10 16:03:14 -0800156 virtual void onDraw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&) SK_OVERRIDE;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000157
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000158
bsalomonb0bd4f62014-09-03 07:19:50 -0700159 virtual void clearStencil(GrRenderTarget*) SK_OVERRIDE;
joshualittd53a8272014-11-10 16:03:14 -0800160 virtual bool flushGraphicsState(const GrOptDrawState&,
161 DrawType,
joshualitt6db519c2014-10-29 08:48:18 -0700162 const GrClipMaskManager::ScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700163 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000164
robertphillips754f4e92014-09-18 13:52:08 -0700165 // GrDrawTarget overrides
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000166 virtual void didAddGpuTraceMarker() SK_OVERRIDE;
167 virtual void didRemoveGpuTraceMarker() SK_OVERRIDE;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000168
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000169 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000170 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000171
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000172 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset
173 // an into the index buffer. It does not account for drawInfo.startIndex() but rather the start
174 // index is relative to the returned offset.
joshualittd53a8272014-11-10 16:03:14 -0800175 void setupGeometry(const GrOptDrawState&,
176 const GrDrawTarget::DrawInfo& info,
177 size_t* indexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000178
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000179 // Subclasses should call this to flush the blend state.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000180 // The params should be the final coefficients to apply
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000181 // (after any blending optimizations or dual source blending considerations
182 // have been accounted for).
egdanield632ea42014-09-24 10:30:12 -0700183 void flushBlend(const GrOptDrawState& optState, bool isLines,
184 GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000185
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000186 bool hasExtension(const char* ext) const { return fGLContext.hasExtension(ext); }
bsalomon@google.com96399942012-02-13 14:39:16 +0000187
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000188 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000189
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000190 class ProgramCache : public ::SkNoncopyable {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000191 public:
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000192 ProgramCache(GrGpuGL* gpu);
jvanverth@google.com94878772013-03-12 16:00:54 +0000193 ~ProgramCache();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000194
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000195 void abandon();
joshualitt79f8fae2014-10-28 17:59:26 -0700196 GrGLProgram* getProgram(const GrOptDrawState&, DrawType);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000197
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000198 private:
199 enum {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000200 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
201 // shader before evicting from the cache.
commit-bot@chromium.orgb5564222014-03-28 15:52:18 +0000202 kMaxEntries = 128,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000203 kHashBits = 6,
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000204 };
205
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000206 struct Entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000207
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000208 struct ProgDescLess;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000209
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000210 // binary search for entry matching desc. returns index into fEntries that matches desc or ~
211 // of the index of where it should be inserted.
joshualitt79f8fae2014-10-28 17:59:26 -0700212 int search(const GrProgramDesc& desc) const;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000213
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000214 // sorted array of all the entries
215 Entry* fEntries[kMaxEntries];
216 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary
217 // searching fEntries.
218 Entry* fHashTable[1 << kHashBits];
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000219
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000220 int fCount;
221 unsigned int fCurrLRUStamp;
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000222 GrGpuGL* fGpu;
jvanverth@google.com94878772013-03-12 16:00:54 +0000223#ifdef PROGRAM_CACHE_STATS
224 int fTotalRequests;
225 int fCacheMisses;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000226 int fHashMisses; // cache hit but hash table missed
jvanverth@google.com94878772013-03-12 16:00:54 +0000227#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000228 };
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000229
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000230 // flushes dithering, color-mask, and face culling stat
egdanield632ea42014-09-24 10:30:12 -0700231 void flushMiscFixedFunctionState(const GrOptDrawState&);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000232
bsalomon@google.coma3201942012-06-21 19:58:20 +0000233 // flushes the scissor. see the note on flushBoundTextureAndParams about
234 // flushing the scissor after that function is called.
joshualitt6db519c2014-10-29 08:48:18 -0700235 void flushScissor(const GrClipMaskManager::ScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700236 const GrGLIRect& rtViewport,
237 GrSurfaceOrigin rtOrigin);
238
239 // disables the scissor
240 void disableScissor();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000241
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000242 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000243
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000244 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000245 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000246
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000247 // sets a texture unit to use for texture operations other than binding a texture to a program.
248 // ensures that such operations don't negatively interact with tracking bound textures.
249 void setScratchTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000250
bsalomonb0bd4f62014-09-03 07:19:50 -0700251 // bounds is region that may be modified and therefore has to be resolved.
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000252 // NULL means whole target. Can be an empty rect.
bsalomonb0bd4f62014-09-03 07:19:50 -0700253 void flushRenderTarget(GrGLRenderTarget*, const SkIRect* bounds);
254
joshualitta58fe352014-10-27 08:39:00 -0700255 void flushStencil(const GrStencilSettings&, DrawType);
egdanield632ea42014-09-24 10:30:12 -0700256 void flushAAState(const GrOptDrawState&, DrawType);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000257
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000258 bool configToGLFormats(GrPixelConfig config,
259 bool getSizedInternal,
260 GrGLenum* internalFormat,
261 GrGLenum* externalFormat,
262 GrGLenum* externalType);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000263 // helper for onCreateTexture and writeTexturePixels
bsalomonb15b4c12014-10-29 12:41:57 -0700264 bool uploadTexData(const GrSurfaceDesc& desc,
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000265 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000266 int left, int top, int width, int height,
267 GrPixelConfig dataConfig,
268 const void* data,
269 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000270
krajcevski145d48c2014-06-11 16:07:50 -0700271 // helper for onCreateCompressedTexture. If width and height are
272 // set to -1, then this function will use desc.fWidth and desc.fHeight
273 // for the size of the data. The isNewTexture flag should be set to true
274 // whenever a new texture needs to be created. Otherwise, we assume that
275 // the texture is already in GPU memory and that it's going to be updated
276 // with new data.
bsalomonb15b4c12014-10-29 12:41:57 -0700277 bool uploadCompressedTexData(const GrSurfaceDesc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700278 const void* data,
279 bool isNewTexture = true,
280 int left = 0, int top = 0,
281 int width = -1, int height = -1);
krajcevski9c0e6292014-06-02 07:38:14 -0700282
bsalomonb15b4c12014-10-29 12:41:57 -0700283 bool createRenderTargetObjects(const GrSurfaceDesc&, GrGLuint texID, GrGLRenderTarget::IDDesc*);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000284
robertphillips754f4e92014-09-18 13:52:08 -0700285 GrGLuint bindSurfaceAsFBO(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport);
286
robertphillips@google.com6177e692013-02-28 20:16:25 +0000287 GrGLContext fGLContext;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000288
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000289 // GL program-related state
290 ProgramCache* fProgramCache;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000291 SkAutoTUnref<GrGLProgram> fCurrentProgram;
bsalomon@google.com49209392012-06-05 15:13:46 +0000292
293 ///////////////////////////////////////////////////////////////////////////
294 ///@name Caching of GL State
295 ///@{
296 int fHWActiveTextureUnitIdx;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000297 GrGLuint fHWProgramID;
bsalomon@google.com91207482013-02-12 21:45:24 +0000298
bsalomon@google.com49209392012-06-05 15:13:46 +0000299 enum TriState {
300 kNo_TriState,
301 kYes_TriState,
302 kUnknown_TriState
303 };
304
bsalomon@google.coma3201942012-06-21 19:58:20 +0000305 // last scissor / viewport scissor state seen by the GL.
306 struct {
307 TriState fEnabled;
308 GrGLIRect fRect;
309 void invalidate() {
310 fEnabled = kUnknown_TriState;
311 fRect.invalidate();
312 }
313 } fHWScissorSettings;
314
315 GrGLIRect fHWViewport;
316
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000317 /**
318 * Tracks bound vertex and index buffers and vertex attrib array state.
319 */
320 class HWGeometryState {
321 public:
bsalomon@google.com6918d482013-03-07 19:09:11 +0000322 HWGeometryState() { fVBOVertexArray = NULL; this->invalidate(); }
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000323
bsalomon@google.com6918d482013-03-07 19:09:11 +0000324 ~HWGeometryState() { SkSafeUnref(fVBOVertexArray); }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000325
bsalomon@google.com6918d482013-03-07 19:09:11 +0000326 void invalidate() {
327 fBoundVertexArrayIDIsValid = false;
328 fBoundVertexBufferIDIsValid = false;
329 fDefaultVertexArrayBoundIndexBufferID = false;
330 fDefaultVertexArrayBoundIndexBufferIDIsValid = false;
331 fDefaultVertexArrayAttribState.invalidate();
bsalomon49f085d2014-09-05 13:34:00 -0700332 if (fVBOVertexArray) {
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000333 fVBOVertexArray->invalidateCachedState();
334 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000335 }
336
337 void notifyVertexArrayDelete(GrGLuint id) {
338 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
339 // Does implicit bind to 0
340 fBoundVertexArrayID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000341 }
342 }
343
bsalomon@google.com6918d482013-03-07 19:09:11 +0000344 void setVertexArrayID(GrGpuGL* gpu, GrGLuint arrayID) {
345 if (!gpu->glCaps().vertexArrayObjectSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000346 SkASSERT(0 == arrayID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000347 return;
348 }
349 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
350 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
351 fBoundVertexArrayIDIsValid = true;
352 fBoundVertexArrayID = arrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000353 }
354 }
355
bsalomon@google.come49ad452013-02-20 19:33:20 +0000356 void notifyVertexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000357 if (fBoundVertexBufferIDIsValid && id == fBoundVertexBufferID) {
358 fBoundVertexBufferID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000359 }
bsalomon49f085d2014-09-05 13:34:00 -0700360 if (fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000361 fVBOVertexArray->notifyVertexBufferDelete(id);
362 }
363 fDefaultVertexArrayAttribState.notifyVertexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000364 }
365
bsalomon@google.come49ad452013-02-20 19:33:20 +0000366 void notifyIndexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000367 if (fDefaultVertexArrayBoundIndexBufferIDIsValid &&
368 id == fDefaultVertexArrayBoundIndexBufferID) {
369 fDefaultVertexArrayBoundIndexBufferID = 0;
370 }
bsalomon49f085d2014-09-05 13:34:00 -0700371 if (fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000372 fVBOVertexArray->notifyIndexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000373 }
374 }
375
bsalomon@google.com6918d482013-03-07 19:09:11 +0000376 void setVertexBufferID(GrGpuGL* gpu, GrGLuint id) {
377 if (!fBoundVertexBufferIDIsValid || id != fBoundVertexBufferID) {
378 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ARRAY_BUFFER, id));
379 fBoundVertexBufferIDIsValid = true;
380 fBoundVertexBufferID = id;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000381 }
382 }
383
bsalomon@google.com6918d482013-03-07 19:09:11 +0000384 /**
385 * Binds the default vertex array and binds the index buffer. This is used when binding
386 * an index buffer in order to update it.
387 */
388 void setIndexBufferIDOnDefaultVertexArray(GrGpuGL* gpu, GrGLuint id) {
389 this->setVertexArrayID(gpu, 0);
390 if (!fDefaultVertexArrayBoundIndexBufferIDIsValid ||
391 id != fDefaultVertexArrayBoundIndexBufferID) {
392 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
393 fDefaultVertexArrayBoundIndexBufferIDIsValid = true;
394 fDefaultVertexArrayBoundIndexBufferID = id;
395 }
396 }
397
398 /**
399 * Binds the vertex array object that should be used to render from the vertex buffer.
400 * The vertex array is bound and its attrib array state object is returned. The vertex
401 * 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 +0000402 * returned GrGLAttribArrayState should be used to set vertex attribute arrays.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000403 */
404 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGpuGL* gpu,
405 const GrGLVertexBuffer* vbuffer,
406 const GrGLIndexBuffer* ibuffer);
407
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000408 private:
bsalomon@google.com6918d482013-03-07 19:09:11 +0000409 GrGLuint fBoundVertexArrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000410 GrGLuint fBoundVertexBufferID;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000411 bool fBoundVertexArrayIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000412 bool fBoundVertexBufferIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000413
bsalomon@google.com6918d482013-03-07 19:09:11 +0000414 GrGLuint fDefaultVertexArrayBoundIndexBufferID;
415 bool fDefaultVertexArrayBoundIndexBufferIDIsValid;
416 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
417 // is bound. However, this class is internal to GrGpuGL and this object never leaks out of
418 // GrGpuGL.
419 GrGLAttribArrayState fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000420
bsalomon@google.com6918d482013-03-07 19:09:11 +0000421 // This is used when we're using a core profile and the vertices are in a VBO.
422 GrGLVertexArray* fVBOVertexArray;
bsalomon@google.com49209392012-06-05 15:13:46 +0000423 } fHWGeometryState;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000424
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000425 struct {
426 GrBlendCoeff fSrcCoeff;
427 GrBlendCoeff fDstCoeff;
428 GrColor fConstColor;
429 bool fConstColorValid;
430 TriState fEnabled;
431
432 void invalidate() {
bsalomon@google.com47059542012-06-06 20:51:20 +0000433 fSrcCoeff = kInvalid_GrBlendCoeff;
434 fDstCoeff = kInvalid_GrBlendCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000435 fConstColorValid = false;
436 fEnabled = kUnknown_TriState;
437 }
438 } fHWBlendState;
439
egdanielb414f252014-07-29 13:15:47 -0700440 TriState fMSAAEnabled;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000441
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000442 GrStencilSettings fHWStencilSettings;
443 TriState fHWStencilTestEnabled;
kkinnunenccdaa042014-08-20 01:36:23 -0700444
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000445
egdaniel89af44a2014-09-26 06:15:04 -0700446 GrOptDrawState::DrawFace fHWDrawFace;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000447 TriState fHWWriteToColor;
448 TriState fHWDitherEnabled;
bsalomon1c63bf62014-07-22 13:09:46 -0700449 uint32_t fHWBoundRenderTargetUniqueID;
450 SkTArray<uint32_t, true> fHWBoundTextureUniqueIDs;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000451
bsalomon@google.com49209392012-06-05 15:13:46 +0000452 ///@}
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000453
bsalomon@google.comfe676522011-06-17 18:12:21 +0000454 // we record what stencil format worked last time to hopefully exit early
455 // from our loop that tries stencil formats and calls check fb status.
456 int fLastSuccessfulStencilFmtIdx;
457
reed@google.comac10a2d2010-12-22 21:39:39 +0000458 typedef GrGpu INHERITED;
kkinnunenccdaa042014-08-20 01:36:23 -0700459 friend class GrGLPathRendering; // For accessing setTextureUnit.
reed@google.comac10a2d2010-12-22 21:39:39 +0000460};
461
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000462#endif