blob: 1fd6f7dd45e59c0600516473206216c011245174 [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
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000012#include "GrGLShaderBuilder.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000013#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000014#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000015#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000016#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
caryclark@google.comcf6285b2012-06-06 12:09:01 +000018static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000019static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000022#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000023
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000032 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000037
38///////////////////////////////////////////////////////////////////////////////
39
twiz@google.com0f31ca72011-03-18 17:38:11 +000040static const GrGLenum gXfermodeCoeff2Blend[] = {
41 GR_GL_ZERO,
42 GR_GL_ONE,
43 GR_GL_SRC_COLOR,
44 GR_GL_ONE_MINUS_SRC_COLOR,
45 GR_GL_DST_COLOR,
46 GR_GL_ONE_MINUS_DST_COLOR,
47 GR_GL_SRC_ALPHA,
48 GR_GL_ONE_MINUS_SRC_ALPHA,
49 GR_GL_DST_ALPHA,
50 GR_GL_ONE_MINUS_DST_ALPHA,
51 GR_GL_CONSTANT_COLOR,
52 GR_GL_ONE_MINUS_CONSTANT_COLOR,
53 GR_GL_CONSTANT_ALPHA,
54 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000055
56 // extended blend coeffs
57 GR_GL_SRC1_COLOR,
58 GR_GL_ONE_MINUS_SRC1_COLOR,
59 GR_GL_SRC1_ALPHA,
60 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000061};
62
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000064 static const bool gCoeffReferencesBlendConst[] = {
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 true,
76 true,
77 true,
78 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000079
80 // extended blend coeffs
81 false,
82 false,
83 false,
84 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000085 };
86 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000087 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
88 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000089
bsalomon@google.com47059542012-06-06 20:51:20 +000090 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
91 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
92 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
93 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
94 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
95 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
97 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
98 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
99 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
101 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
103 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000104
bsalomon@google.com47059542012-06-06 20:51:20 +0000105 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
106 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
108 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000109
110 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000111 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
112 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000113}
114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115///////////////////////////////////////////////////////////////////////////////
116
rileya@google.come38160c2012-07-03 18:03:04 +0000117static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000118
robertphillips@google.com6177e692013-02-28 20:16:25 +0000119GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000120 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000121 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000122
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000123 SkASSERT(ctx.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000124
bsalomon@google.combcce8922013-03-25 15:38:39 +0000125 fCaps.reset(SkRef(ctx.info().caps()));
126
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000127 fHWBoundTextures.reset(ctx.info().caps()->maxFragmentTextureUnits());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000128 fHWTexGenSettings.reset(ctx.info().caps()->maxFixedFunctionTextureCoords());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000129
robertphillips@google.com6177e692013-02-28 20:16:25 +0000130 GrGLClearErr(fGLContext.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000132 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000133 const GrGLubyte* vendor;
134 const GrGLubyte* renderer;
135 const GrGLubyte* version;
136 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
137 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
138 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000139 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
140 this);
141 GrPrintf("------ VENDOR %s\n", vendor);
142 GrPrintf("------ RENDERER %s\n", renderer);
143 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000144 GrPrintf("------ EXTENSIONS\n");
145 ctx.info().extensions().print();
146 GrPrintf("\n");
bsalomon@google.combcce8922013-03-25 15:38:39 +0000147 ctx.info().caps()->print();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000148 }
149
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000150 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000151
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000152 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000153
bsalomon@google.comfe676522011-06-17 18:12:21 +0000154 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000155 fHWProgramID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000156}
157
158GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000159 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000160 // detach the current program so there is no confusion on OpenGL's part
161 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000162 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000163 GL_CALL(UseProgram(0));
164 }
165
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000166 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000167
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000168 // This must be called by before the GrDrawTarget destructor
169 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000170 // This subclass must do this before the base class destructor runs
171 // since we will unref the GrGLInterface.
172 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000173}
174
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000175///////////////////////////////////////////////////////////////////////////////
176
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000177
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000178GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
179 GrPixelConfig surfaceConfig) const {
180 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000181 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000182 } else if (fGLContext.info().isMesa() &&
183 GrBytesPerPixel(readConfig) == 4 &&
184 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000185 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
186 // Perhaps this should be guarded by some compiletime or runtime check.
187 return surfaceConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000188 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
189 !this->glCaps().readPixelsSupported(this->glInterface(),
190 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
191 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000192 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000193 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000194 }
195}
196
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000197GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
198 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000199 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
200 return kBGRA_8888_GrPixelConfig;
201 } else {
202 return writeConfig;
203 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000204}
205
206bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
207 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
208 return false;
209 }
bsalomon@google.com791816a2013-08-15 18:54:39 +0000210 if (srcConfig != texture->config() && kES_GrGLBinding == this->glBinding()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000211 // In general ES2 requires the internal format of the texture and the format of the src
212 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
213 // texture. It depends upon which extension added BGRA. The Apple extension allows it
214 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
215 // internal format).
216 if (this->glCaps().bgraFormatSupport() &&
217 !this->glCaps().bgraIsInternalFormat() &&
218 kBGRA_8888_GrPixelConfig == srcConfig &&
219 kRGBA_8888_GrPixelConfig == texture->config()) {
220 return true;
221 } else {
222 return false;
223 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000224 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000225 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000226 }
227}
228
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000229bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
230 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
231}
232
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000233void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000234 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000235 if (resetBits & kMisc_GrGLBackendState) {
236 GL_CALL(Disable(GR_GL_DEPTH_TEST));
237 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000238
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000239 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
240 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000242 if (kDesktop_GrGLBinding == this->glBinding()) {
243 // Desktop-only state that we never change
244 if (!this->glCaps().isCoreProfile()) {
245 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
246 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
247 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
248 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
249 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
250 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
251 }
252 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
253 // core profile. This seems like a bug since the core spec removes any mention of
254 // GL_ARB_imaging.
255 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
256 GL_CALL(Disable(GR_GL_COLOR_TABLE));
257 }
258 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
259 // Since ES doesn't support glPointSize at all we always use the VS to
260 // set the point size
261 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
262
263 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
264 // currently part of our gl interface. There are probably others as
265 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000266 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000267 fHWWriteToColor = kUnknown_TriState;
268 // we only ever use lines in hairline mode
269 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000270 }
edisonn@google.comba669992013-06-28 16:03:21 +0000271
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000272 if (resetBits & kAA_GrGLBackendState) {
273 fHWAAState.invalidate();
274 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000275
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000276 fHWActiveTextureUnitIdx = -1; // invalid
277
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000278 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000279 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
280 fHWBoundTextures[s] = NULL;
281 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000282 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000283
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000284 if (resetBits & kBlend_GrGLBackendState) {
285 fHWBlendState.invalidate();
286 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000287
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000288 if (resetBits & kView_GrGLBackendState) {
289 fHWScissorSettings.invalidate();
290 fHWViewport.invalidate();
291 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000292
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000293 if (resetBits & kStencil_GrGLBackendState) {
294 fHWStencilSettings.invalidate();
295 fHWStencilTestEnabled = kUnknown_TriState;
296 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000297
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000298 // Vertex
299 if (resetBits & kVertex_GrGLBackendState) {
300 fHWGeometryState.invalidate();
301 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000302
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000303 if (resetBits & kRenderTarget_GrGLBackendState) {
304 fHWBoundRenderTarget = NULL;
305 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000306
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000307 if (resetBits & (kFixedFunction_GrGLBackendState | kPathRendering_GrGLBackendState)) {
308 if (this->glCaps().fixedFunctionSupport()) {
309 fHWProjectionMatrixState.invalidate();
310 // we don't use the model view matrix.
311 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
312 GL_CALL(LoadIdentity());
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000313
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000314 for (int i = 0; i < this->glCaps().maxFixedFunctionTextureCoords(); ++i) {
315 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + i));
316 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S));
317 GL_CALL(Disable(GR_GL_TEXTURE_GEN_T));
318 GL_CALL(Disable(GR_GL_TEXTURE_GEN_Q));
319 GL_CALL(Disable(GR_GL_TEXTURE_GEN_R));
320 if (this->caps()->pathRenderingSupport()) {
321 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
322 }
323 fHWTexGenSettings[i].fMode = GR_GL_NONE;
324 fHWTexGenSettings[i].fNumComponents = 0;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000325 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000326 fHWActiveTexGenSets = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000327 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000328 if (this->caps()->pathRenderingSupport()) {
329 fHWPathStencilSettings.invalidate();
330 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000331 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000332
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000333 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000334 if (resetBits & kPixelStore_GrGLBackendState) {
335 if (this->glCaps().unpackRowLengthSupport()) {
336 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
337 }
338 if (this->glCaps().packRowLengthSupport()) {
339 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
340 }
341 if (this->glCaps().unpackFlipYSupport()) {
342 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
343 }
344 if (this->glCaps().packFlipYSupport()) {
345 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
346 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000347 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000348
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000349 if (resetBits & kProgram_GrGLBackendState) {
350 fHWProgramID = 0;
351 fSharedGLProgramState.invalidate();
352 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000353}
354
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000355namespace {
356
357GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
358 // By default, GrRenderTargets are GL's normal orientation so that they
359 // can be drawn to by the outside world without the client having
360 // to render upside down.
361 if (kDefault_GrSurfaceOrigin == origin) {
362 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
363 } else {
364 return origin;
365 }
366}
367
368}
369
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000370GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000371 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000372 return NULL;
373 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000374
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000375 if (0 == desc.fTextureHandle) {
376 return NULL;
377 }
378
bsalomon@google.combcce8922013-03-25 15:38:39 +0000379 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000380 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
381 return NULL;
382 }
383
384 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000385 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000386 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000387 glTexDesc.fWidth = desc.fWidth;
388 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000389 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000390 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000391 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000392 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000393 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000394 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
395 // assuming the old behaviour, which is that backend textures are always
396 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
397 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
398 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
399 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
400 } else {
401 glTexDesc.fOrigin = desc.fOrigin;
402 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000403
404 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000405 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000406 GrGLRenderTarget::Desc glRTDesc;
407 glRTDesc.fRTFBOID = 0;
408 glRTDesc.fTexFBOID = 0;
409 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000410 glRTDesc.fConfig = desc.fConfig;
411 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000412 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000413 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000414 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
415 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000416 glTexDesc.fTextureID,
417 &glRTDesc)) {
418 return NULL;
419 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000420 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000421 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000422 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000423 }
424 if (NULL == texture) {
425 return NULL;
426 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000427
bsalomon@google.come269f212011-11-07 13:29:52 +0000428 return texture;
429}
430
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000431GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000432 GrGLRenderTarget::Desc glDesc;
433 glDesc.fConfig = desc.fConfig;
434 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
435 glDesc.fMSColorRenderbufferID = 0;
436 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
437 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000438 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000439 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000440
441 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000442 GrGLIRect viewport;
443 viewport.fLeft = 0;
444 viewport.fBottom = 0;
445 viewport.fWidth = desc.fWidth;
446 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000447
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000448 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
449 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000450 if (desc.fStencilBits) {
451 GrGLStencilBuffer::Format format;
452 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
453 format.fPacked = false;
454 format.fStencilBits = desc.fStencilBits;
455 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000456 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000457 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
458 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000459 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000460 0,
461 desc.fWidth,
462 desc.fHeight,
463 desc.fSampleCnt,
464 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000465 tgt->setStencilBuffer(sb);
466 sb->unref();
467 }
468 return tgt;
469}
470
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000471////////////////////////////////////////////////////////////////////////////////
472
bsalomon@google.com9c680582013-02-06 18:17:50 +0000473bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000474 int left, int top, int width, int height,
475 GrPixelConfig config, const void* buffer,
476 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000477 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000478 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000479 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000480 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
481
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000482 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000483 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
484 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000485 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000486 desc.fWidth = glTex->width();
487 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000488 desc.fConfig = glTex->config();
489 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000490 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000491 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000492
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000493 if (this->uploadTexData(desc, false,
494 left, top, width, height,
495 config, buffer, rowBytes)) {
496 texture->dirtyMipMaps(true);
497 return true;
498 } else {
499 return false;
500 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000501}
502
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000503namespace {
504bool adjust_pixel_ops_params(int surfaceWidth,
505 int surfaceHeight,
506 size_t bpp,
507 int* left, int* top, int* width, int* height,
508 const void** data,
509 size_t* rowBytes) {
510 if (!*rowBytes) {
511 *rowBytes = *width * bpp;
512 }
513
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000514 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
515 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000516
517 if (!subRect.intersect(bounds)) {
518 return false;
519 }
520 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
521 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
522
523 *left = subRect.fLeft;
524 *top = subRect.fTop;
525 *width = subRect.width();
526 *height = subRect.height();
527 return true;
528}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000529
530GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
531 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
532 return GR_GL_GET_ERROR(interface);
533 } else {
534 return CHECK_ALLOC_ERROR(interface);
535 }
536}
537
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000538}
539
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000540bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
541 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000542 int left, int top, int width, int height,
543 GrPixelConfig dataConfig,
544 const void* data,
545 size_t rowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000546 SkASSERT(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000547
548 size_t bpp = GrBytesPerPixel(dataConfig);
549 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
550 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000551 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000552 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000553 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000554
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000555 // in case we need a temporary, trimmed copy of the src pixels
556 SkAutoSMalloc<128 * 128> tempStorage;
557
bsalomon@google.com313f0192012-07-10 17:21:02 +0000558 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000559 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000560 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000561 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000562
bsalomon@google.com313f0192012-07-10 17:21:02 +0000563 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
564 // 565 is not a sized internal format on desktop GL. So on desktop with
565 // 565 we always use an unsized internal format to let the system pick
566 // the best sized format to convert the 565 data to. Since TexStorage
567 // only allows sized internal formats we will instead use TexImage2D.
568 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000569 }
570
571 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000572 GrGLenum externalFormat;
573 GrGLenum externalType;
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000574 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
575 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
576 // size of the internal format whenever possible and so only use a sized internal format when
577 // using texture storage.
bsalomon@google.com3323c4d2013-08-20 13:48:33 +0000578 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000579 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000580 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000581 }
582
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000583 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000584 // paletted textures cannot be updated
585 return false;
586 }
587
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000588 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000589 * check whether to allocate a temporary buffer for flipping y or
590 * because our srcData has extra bytes past each row. If so, we need
591 * to trim those off here, since GL ES may not let us specify
592 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000593 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000594 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000595 bool swFlipY = false;
596 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000597 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000598 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000599 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000600 glFlipY = true;
601 } else {
602 swFlipY = true;
603 }
604 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000605 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000606 // can't use this for flipping, only non-neg values allowed. :(
607 if (rowBytes != trimRowBytes) {
608 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
609 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
610 restoreGLRowLength = true;
611 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000612 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000613 if (trimRowBytes != rowBytes || swFlipY) {
614 // copy data into our new storage, skipping the trailing bytes
615 size_t trimSize = height * trimRowBytes;
616 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000617 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000618 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000619 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000620 char* dst = (char*)tempStorage.reset(trimSize);
621 for (int y = 0; y < height; y++) {
622 memcpy(dst, src, trimRowBytes);
623 if (swFlipY) {
624 src -= rowBytes;
625 } else {
626 src += rowBytes;
627 }
628 dst += trimRowBytes;
629 }
630 // now point data to our copied version
631 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000632 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000633 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000634 if (glFlipY) {
635 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
636 }
637 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000638 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000639 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000640 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000641 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000642 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000643 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000644 if (useTexStorage) {
645 // We never resize or change formats of textures. We don't use
646 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000647 GL_ALLOC_CALL(this->glInterface(),
648 TexStorage2D(GR_GL_TEXTURE_2D,
649 1, // levels
650 internalFormat,
651 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000652 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000653 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
654 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
655 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000656 GL_ALLOC_CALL(this->glInterface(),
657 CompressedTexImage2D(GR_GL_TEXTURE_2D,
658 0, // level
659 internalFormat,
660 desc.fWidth, desc.fHeight,
661 0, // border
662 imageSize,
663 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000664 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000665 GL_ALLOC_CALL(this->glInterface(),
666 TexImage2D(GR_GL_TEXTURE_2D,
667 0, // level
668 internalFormat,
669 desc.fWidth, desc.fHeight,
670 0, // border
671 externalFormat, externalType,
672 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000673 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000674 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000675 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000676 if (error != GR_GL_NO_ERROR) {
677 succeeded = false;
678 } else {
679 // if we have data and we used TexStorage to create the texture, we
680 // now upload with TexSubImage.
681 if (NULL != data && useTexStorage) {
682 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
683 0, // level
684 left, top,
685 width, height,
686 externalFormat, externalType,
687 data));
688 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000689 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000690 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000691 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000692 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000693 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000694 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
695 0, // level
696 left, top,
697 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000698 externalFormat, externalType, data));
699 }
700
701 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000702 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000703 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000704 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000705 if (glFlipY) {
706 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
707 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000708 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000709}
710
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000711static bool renderbuffer_storage_msaa(GrGLContext& ctx,
712 int sampleCount,
713 GrGLenum format,
714 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000715 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000716 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.info().caps()->msFBOType());
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +0000717#if GR_GL_IGNORE_ES3_MSAA
robertphillips@google.com6177e692013-02-28 20:16:25 +0000718 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000719 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
720 sampleCount,
721 format,
722 width, height));
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +0000723#else
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000724 switch (ctx.info().caps()->msFBOType()) {
725 case GrGLCaps::kDesktop_ARB_MSFBOType:
726 case GrGLCaps::kDesktop_EXT_MSFBOType:
727 case GrGLCaps::kES_3_0_MSFBOType:
728 GL_ALLOC_CALL(ctx.interface(),
729 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
730 sampleCount,
731 format,
732 width, height));
733 break;
734 case GrGLCaps::kES_Apple_MSFBOType:
735 GL_ALLOC_CALL(ctx.interface(),
736 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
737 sampleCount,
738 format,
739 width, height));
740 break;
741 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
742 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
743 GL_ALLOC_CALL(ctx.interface(),
744 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
745 sampleCount,
746 format,
747 width, height));
748 break;
749 case GrGLCaps::kNone_MSFBOType:
750 GrCrash("Shouldn't be here if we don't support multisampled renderbuffers.");
751 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000752 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000753#endif
754 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000755}
756
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000757bool GrGpuGL::createRenderTargetObjects(int width, int height,
758 GrGLuint texID,
759 GrGLRenderTarget::Desc* desc) {
760 desc->fMSColorRenderbufferID = 0;
761 desc->fRTFBOID = 0;
762 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000763 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000764
765 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000766
bsalomon@google.comab15d612011-08-09 12:57:56 +0000767 GrGLenum msColorFormat = 0; // suppress warning
768
bsalomon@google.com347c3822013-05-01 20:10:01 +0000769 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
770 goto FAILED;
771 }
772
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000773 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000774 if (!desc->fTexFBOID) {
775 goto FAILED;
776 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000777
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000778
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000779 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
780 // the texture bound to the other. The exception is the IMG multisample extension. With this
781 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
782 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000783 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000784 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
785 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000786 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000787 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000788 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000789 // ES2 and ES3 require sized internal formats for rb storage.
bsalomon@google.com791816a2013-08-15 18:54:39 +0000790 kES_GrGLBinding == this->glBinding(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000791 &msColorFormat,
792 NULL,
793 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000794 goto FAILED;
795 }
796 } else {
797 desc->fRTFBOID = desc->fTexFBOID;
798 }
799
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000800 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000801 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000802 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000803 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000804 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000805 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000806 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000807 desc->fSampleCnt,
808 msColorFormat,
809 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000810 goto FAILED;
811 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000812 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000813 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000814 GR_GL_COLOR_ATTACHMENT0,
815 GR_GL_RENDERBUFFER,
816 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000817 if (desc->fCheckAllocation ||
818 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000819 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
820 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
821 goto FAILED;
822 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000823 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000824 }
825 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000826 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000827
bsalomon@google.com347c3822013-05-01 20:10:01 +0000828 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000829 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
830 GR_GL_COLOR_ATTACHMENT0,
831 GR_GL_TEXTURE_2D,
832 texID, 0, desc->fSampleCnt));
833 } else {
834 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
835 GR_GL_COLOR_ATTACHMENT0,
836 GR_GL_TEXTURE_2D,
837 texID, 0));
838 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000839 if (desc->fCheckAllocation ||
840 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000841 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
842 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
843 goto FAILED;
844 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000845 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000846 }
847
848 return true;
849
850FAILED:
851 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000852 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000853 }
854 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000855 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000856 }
857 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000858 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000859 }
860 return false;
861}
862
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000863// good to set a break-point here to know when createTexture fails
864static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000865// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000866 return NULL;
867}
868
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000869#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000870static size_t as_size_t(int x) {
871 return x;
872}
873#endif
874
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000875GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000876 const void* srcData,
877 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000878
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000879 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000880 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000881
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000882 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000883 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000884 // We fail if the MSAA was requested and is not available.
885 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
886 //GrPrintf("MSAA RT requested but not supported on this platform.");
887 return return_null_texture();
888 }
889 // If the sample count exceeds the max then we clamp it.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000890 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000891
robertphillips@google.com32716282012-06-04 12:48:45 +0000892 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000893 glTexDesc.fWidth = desc.fWidth;
894 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000895 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000896 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000897
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000898 glRTDesc.fMSColorRenderbufferID = 0;
899 glRTDesc.fRTFBOID = 0;
900 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000901 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000902 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000903 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000904
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000905 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000906
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000907 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
908 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000909
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000910 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000911 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000912 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000913 //GrPrintf("MSAA RT requested but not supported on this platform.");
914 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000915 }
916
reed@google.comac10a2d2010-12-22 21:39:39 +0000917 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000918 int maxRTSize = this->caps()->maxRenderTargetSize();
919 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000920 return return_null_texture();
921 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000922 } else {
923 int maxSize = this->caps()->maxTextureSize();
924 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
925 return return_null_texture();
926 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000927 }
928
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000929 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000930
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000931 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000932 return return_null_texture();
933 }
934
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000935 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000937
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000938 if (renderTarget && this->glCaps().textureUsageSupport()) {
939 // provides a hint about how this texture will be used
940 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
941 GR_GL_TEXTURE_USAGE,
942 GR_GL_FRAMEBUFFER_ATTACHMENT));
943 }
944
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000945 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
946 // drivers have a bug where an FBO won't be complete if it includes a
947 // texture that is not mipmap complete (considering the filter in use).
948 GrGLTexture::TexParams initialTexParams;
949 // we only set a subset here so invalidate first
950 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000951 initialTexParams.fMinFilter = GR_GL_NEAREST;
952 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000953 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
954 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000955 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
956 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000957 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000958 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
959 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000960 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000961 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
962 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000963 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000964 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
965 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000966 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000967 if (!this->uploadTexData(glTexDesc, true, 0, 0,
968 glTexDesc.fWidth, glTexDesc.fHeight,
969 desc.fConfig, srcData, rowBytes)) {
970 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
971 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000972 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000973
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000974 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000975 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +0000976 // unbind the texture from the texture unit before binding it to the frame buffer
977 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
978
bsalomon@google.com99621082011-11-15 16:47:16 +0000979 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
980 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000981 glTexDesc.fTextureID,
982 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000983 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 return return_null_texture();
985 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000986 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000987 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000988 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +0000989 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000990 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +0000991#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000992 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
993 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000994#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000995 return tex;
996}
997
998namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000999
1000const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1001
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001002void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001003 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001004
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001005 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001006 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007 (kUnknownBitCount == format->fTotalBits));
1008 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001009 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001010 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1011 (GrGLint*)&format->fStencilBits);
1012 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001013 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001014 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1015 (GrGLint*)&format->fTotalBits);
1016 format->fTotalBits += format->fStencilBits;
1017 } else {
1018 format->fTotalBits = format->fStencilBits;
1019 }
1020 }
1021}
1022}
1023
1024bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1025 int width, int height) {
1026
1027 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001028 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001029 SkASSERT(rt->asTexture());
1030 SkASSERT(width >= rt->width());
1031 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001032
1033 int samples = rt->numSamples();
1034 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001035 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001036 if (!sbID) {
1037 return false;
1038 }
1039
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001040 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001041 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001042 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001043 // we start with the last stencil format that succeeded in hopes
1044 // that we won't go through this loop more than once after the
1045 // first (painful) stencil creation.
1046 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001047 const GrGLCaps::StencilFormat& sFmt =
1048 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001049 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001050 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001051 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001052 bool created;
1053 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001054 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001055 samples,
1056 sFmt.fInternalFormat,
1057 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001058 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001059 GL_ALLOC_CALL(this->glInterface(),
1060 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001061 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001062 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001063 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001064 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001065 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001066 if (created) {
1067 // After sized formats we attempt an unsized format and take
1068 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001069 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001070 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001071 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001072 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001073 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001074 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1076 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001077 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001078 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001079 return true;
1080 }
1081 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 }
1083 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001084 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001085 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086}
1087
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001088bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1090
1091 GrGLuint fbo = glrt->renderFBOID();
1092
1093 if (NULL == sb) {
1094 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001095 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001096 GR_GL_STENCIL_ATTACHMENT,
1097 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001098 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001099 GR_GL_DEPTH_ATTACHMENT,
1100 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001101#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001102 GrGLenum status;
1103 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001104 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105#endif
1106 }
1107 return true;
1108 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001109 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 GrGLuint rb = glsb->renderbufferID();
1111
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001112 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1114 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001115 GR_GL_STENCIL_ATTACHMENT,
1116 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001119 GR_GL_DEPTH_ATTACHMENT,
1120 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001123 GR_GL_DEPTH_ATTACHMENT,
1124 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001125 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001126
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001127 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001128 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001129 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1130 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001132 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001134 if (glsb->format().fPacked) {
1135 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1136 GR_GL_DEPTH_ATTACHMENT,
1137 GR_GL_RENDERBUFFER, 0));
1138 }
1139 return false;
1140 } else {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001141 fGLContext.info().caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001142 rt->config(),
1143 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001146 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001148}
1149
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001150////////////////////////////////////////////////////////////////////////////////
1151
robertphillips@google.comadacc702013-10-14 21:53:24 +00001152GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001153 GrGLVertexBuffer::Desc desc;
1154 desc.fDynamic = dynamic;
1155 desc.fSizeInBytes = size;
1156 desc.fIsWrapped = false;
1157
bsalomon@google.com96966a52013-02-21 16:34:21 +00001158 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001159 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001160 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001161 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001162 } else {
1163 GL_CALL(GenBuffers(1, &desc.fID));
1164 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001165 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001166 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1167 // make sure driver can allocate memory for this buffer
1168 GL_ALLOC_CALL(this->glInterface(),
1169 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001170 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001171 NULL, // data ptr
1172 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1173 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1174 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001175 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001176 return NULL;
1177 }
1178 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1179 return vertexBuffer;
1180 }
1181 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001182 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001183}
1184
robertphillips@google.comadacc702013-10-14 21:53:24 +00001185GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001186 GrGLIndexBuffer::Desc desc;
1187 desc.fDynamic = dynamic;
1188 desc.fSizeInBytes = size;
1189 desc.fIsWrapped = false;
1190
bsalomon@google.com96966a52013-02-21 16:34:21 +00001191 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001192 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001193 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001194 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001195 } else {
1196 GL_CALL(GenBuffers(1, &desc.fID));
1197 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001198 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001199 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1200 // make sure driver can allocate memory for this buffer
1201 GL_ALLOC_CALL(this->glInterface(),
1202 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001203 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001204 NULL, // data ptr
1205 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1206 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1207 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001208 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001209 return NULL;
1210 }
1211 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1212 return indexBuffer;
1213 }
1214 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001216}
1217
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001218GrPath* GrGpuGL::onCreatePath(const SkPath& inPath, const SkStrokeRec& stroke) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001219 SkASSERT(this->caps()->pathRenderingSupport());
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001220 return SkNEW_ARGS(GrGLPath, (this, inPath, stroke));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001221}
1222
bsalomon@google.coma3201942012-06-21 19:58:20 +00001223void GrGpuGL::flushScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001224 if (fScissorState.fEnabled) {
commit-bot@chromium.org5c363642013-10-04 15:28:18 +00001225 // Only access the RT if scissoring is being enabled. We can call this before performing
1226 // a glBitframebuffer for a surface->surface copy, which requires no RT to be bound to the
1227 // GrDrawState.
1228 const GrDrawState& drawState = this->getDrawState();
1229 const GrGLRenderTarget* rt =
1230 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1231
1232 SkASSERT(NULL != rt);
1233 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001234 GrGLIRect scissor;
1235 scissor.setRelativeTo(vp,
1236 fScissorState.fRect.fLeft,
1237 fScissorState.fRect.fTop,
1238 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001239 fScissorState.fRect.height(),
1240 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001241 // if the scissor fully contains the viewport then we fall through and
1242 // disable the scissor test.
1243 if (!scissor.contains(vp)) {
1244 if (fHWScissorSettings.fRect != scissor) {
1245 scissor.pushToGLScissor(this->glInterface());
1246 fHWScissorSettings.fRect = scissor;
1247 }
1248 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1249 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1250 fHWScissorSettings.fEnabled = kYes_TriState;
1251 }
1252 return;
1253 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001255 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001256 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001257 fHWScissorSettings.fEnabled = kNo_TriState;
1258 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001259 }
1260}
1261
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001262void GrGpuGL::onClear(const SkIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001263 const GrDrawState& drawState = this->getDrawState();
1264 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001265 // parent class should never let us get here with no RT
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001266 SkASSERT(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001267
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001268 SkIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001269 if (NULL != rect) {
1270 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001271 clippedRect = *rect;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001272 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001273 if (clippedRect.intersect(rtRect)) {
1274 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001275 } else {
1276 return;
1277 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001279 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001280 GrAutoTRestore<ScissorState> asr(&fScissorState);
1281 fScissorState.fEnabled = (NULL != rect);
1282 if (fScissorState.fEnabled) {
1283 fScissorState.fRect = *rect;
1284 }
1285 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001286
1287 GrGLfloat r, g, b, a;
1288 static const GrGLfloat scale255 = 1.f / 255.f;
1289 a = GrColorUnpackA(color) * scale255;
1290 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001291 r = GrColorUnpackR(color) * scaleRGB;
1292 g = GrColorUnpackG(color) * scaleRGB;
1293 b = GrColorUnpackB(color) * scaleRGB;
1294
1295 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001296 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001297 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001298 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001299}
1300
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001301void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001302 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001303 return;
1304 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001305
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001306 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001307
bsalomon@google.coma3201942012-06-21 19:58:20 +00001308 GrAutoTRestore<ScissorState> asr(&fScissorState);
1309 fScissorState.fEnabled = false;
1310 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001311
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001312 GL_CALL(StencilMask(0xffffffff));
1313 GL_CALL(ClearStencil(0));
1314 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001315 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001316}
1317
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001318void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001319 const GrDrawState& drawState = this->getDrawState();
1320 const GrRenderTarget* rt = drawState.getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001321 SkASSERT(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001322
1323 // this should only be called internally when we know we have a
1324 // stencil buffer.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001325 SkASSERT(NULL != rt->getStencilBuffer());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001326 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001327#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001328 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001329 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001330#else
1331 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001332 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001333 // turned into draws. Our contract on GrDrawTarget says that
1334 // changing the clip between stencil passes may or may not
1335 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001336 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001337#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001338 GrGLint value;
1339 if (insideClip) {
1340 value = (1 << (stencilBitCount - 1));
1341 } else {
1342 value = 0;
1343 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001344 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001345
1346 GrAutoTRestore<ScissorState> asr(&fScissorState);
1347 fScissorState.fEnabled = true;
1348 fScissorState.fRect = rect;
1349 this->flushScissor();
1350
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001351 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001352 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001353 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001354 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001355}
1356
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001357void GrGpuGL::onForceRenderTargetFlush() {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001358 this->flushRenderTarget(&SkIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001359}
1360
bsalomon@google.comc4364992011-11-07 15:54:49 +00001361bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1362 int left, int top,
1363 int width, int height,
1364 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001365 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001366 // If this rendertarget is aready TopLeft, we don't need to flip.
1367 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1368 return false;
1369 }
1370
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001371 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001372 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001373 return false;
1374 }
1375
1376 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001377 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001378 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001379 return true;
1380 }
1381 // If we have to do memcpys to handle rowBytes then y-flip is free
1382 // Note the rowBytes might be tight to the passed in data, but if data
1383 // gets clipped in x to the target the rowBytes will no longer be tight.
1384 if (left >= 0 && (left + width) < renderTarget->width()) {
1385 return 0 == rowBytes ||
1386 GrBytesPerPixel(config) * width == rowBytes;
1387 } else {
1388 return false;
1389 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001390}
1391
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001392bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001393 int left, int top,
1394 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001395 GrPixelConfig config,
1396 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001397 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001398 GrGLenum format;
1399 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001400 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001401 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001402 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001403 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001404 size_t bpp = GrBytesPerPixel(config);
1405 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1406 &left, &top, &width, &height,
1407 const_cast<const void**>(&buffer),
1408 &rowBytes)) {
1409 return false;
1410 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001411
bsalomon@google.comc6980972011-11-02 19:57:21 +00001412 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001413 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001414 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001415 switch (tgt->getResolveType()) {
1416 case GrGLRenderTarget::kCantResolve_ResolveType:
1417 return false;
1418 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001419 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001420 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001421 break;
1422 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001423 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001424 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001425 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1426 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001427 break;
1428 default:
1429 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001430 }
1431
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001432 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001433
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001434 // the read rect is viewport-relative
1435 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001436 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001437
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001438 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001439 if (0 == rowBytes) {
1440 rowBytes = tightRowBytes;
1441 }
1442 size_t readDstRowBytes = tightRowBytes;
1443 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001444
bsalomon@google.comc6980972011-11-02 19:57:21 +00001445 // determine if GL can read using the passed rowBytes or if we need
1446 // a scratch buffer.
1447 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1448 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001449 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001450 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001451 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001452 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001453 readDstRowBytes = rowBytes;
1454 } else {
1455 scratch.reset(tightRowBytes * height);
1456 readDst = scratch.get();
1457 }
1458 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001459 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001460 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1461 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001462 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1463 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001464 format, type, readDst));
1465 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001466 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001467 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1468 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001469 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001470 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001471 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001472 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001473
1474 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001475 // API presents top-to-bottom. We must preserve the padding contents. Note
1476 // that the above readPixels did not overwrite the padding.
1477 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001478 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001479 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001480 scratch.reset(tightRowBytes);
1481 void* tmpRow = scratch.get();
1482 // flip y in-place by rows
1483 const int halfY = height >> 1;
1484 char* top = reinterpret_cast<char*>(buffer);
1485 char* bottom = top + (height - 1) * rowBytes;
1486 for (int y = 0; y < halfY; y++) {
1487 memcpy(tmpRow, top, tightRowBytes);
1488 memcpy(top, bottom, tightRowBytes);
1489 memcpy(bottom, tmpRow, tightRowBytes);
1490 top += rowBytes;
1491 bottom -= rowBytes;
1492 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001493 }
1494 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001495 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001496 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001497 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001499 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001500 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001501 dst += (height-1) * rowBytes;
1502 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001503 for (int y = 0; y < height; y++) {
1504 memcpy(dst, src, tightRowBytes);
1505 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001506 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001507 dst += rowBytes;
1508 } else {
1509 dst -= rowBytes;
1510 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001511 }
1512 }
1513 return true;
1514}
1515
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001516void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001517
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001518 GrGLRenderTarget* rt =
1519 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001520 SkASSERT(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001521
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001522 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001523 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001524#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001525 GrGLenum status;
1526 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001527 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001528 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001529 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001530#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001531 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001532 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001533 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001534 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001535 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001536 }
1537 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001538 if (NULL == bound || !bound->isEmpty()) {
1539 rt->flagAsNeedingResolve(bound);
1540 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001541
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001542 GrTexture *texture = rt->asTexture();
1543 if (texture) {
1544 texture->dirtyMipMaps(true);
1545 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001546}
1547
twiz@google.com0f31ca72011-03-18 17:38:11 +00001548GrGLenum gPrimitiveType2GLMode[] = {
1549 GR_GL_TRIANGLES,
1550 GR_GL_TRIANGLE_STRIP,
1551 GR_GL_TRIANGLE_FAN,
1552 GR_GL_POINTS,
1553 GR_GL_LINES,
1554 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001555};
1556
bsalomon@google.comd302f142011-03-03 13:54:13 +00001557#define SWAP_PER_DRAW 0
1558
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001559#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001560 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001561 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001562 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001563 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001564 void SwapBuf() {
1565 DWORD procID = GetCurrentProcessId();
1566 HWND hwnd = GetTopWindow(GetDesktopWindow());
1567 while(hwnd) {
1568 DWORD wndProcID = 0;
1569 GetWindowThreadProcessId(hwnd, &wndProcID);
1570 if(wndProcID == procID) {
1571 SwapBuffers(GetDC(hwnd));
1572 }
1573 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1574 }
1575 }
1576 #endif
1577#endif
1578
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001579void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001580 size_t indexOffsetInBytes;
1581 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001582
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001583 SkASSERT((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001584
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001585 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001586 GrGLvoid* indices =
1587 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001588 // info.startVertex() was accounted for by setupGeometry.
1589 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1590 info.indexCount(),
1591 GR_GL_UNSIGNED_SHORT,
1592 indices));
1593 } else {
1594 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1595 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1596 // accounted for startVertex.
1597 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1598 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001599#if SWAP_PER_DRAW
1600 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001601 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001602 aglSwapBuffers(aglGetCurrentContext());
1603 int set_a_break_pt_here = 9;
1604 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001605 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001606 SwapBuf();
1607 int set_a_break_pt_here = 9;
1608 SwapBuf();
1609 #endif
1610#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001611}
1612
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001613static GrGLenum gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op) {
1614 switch (op) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001615 default:
1616 GrCrash("Unexpected path fill.");
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001617 /* fallthrough */;
1618 case kIncClamp_StencilOp:
1619 return GR_GL_COUNT_UP;
1620 case kInvert_StencilOp:
1621 return GR_GL_INVERT;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001622 }
1623}
1624
sugoi@google.com12b4e272012-12-06 20:13:11 +00001625void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001626 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001627
1628 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001629 SkASSERT(NULL != this->drawState()->getRenderTarget());
1630 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1631
1632 flushPathStencilSettings(fill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001633
1634 // Decide how to manipulate the stencil buffer based on the fill rule.
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001635 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1636
1637 GrGLenum fillMode =
1638 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1639 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001640 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001641}
1642
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001643void GrGpuGL::onGpuDrawPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001644 SkASSERT(this->caps()->pathRenderingSupport());
1645
1646 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1647 SkASSERT(NULL != this->drawState()->getRenderTarget());
1648 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1649 SkASSERT(!fCurrentProgram->hasVertexShader());
1650
1651 flushPathStencilSettings(fill);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001652 const SkStrokeRec& stroke = path->getStroke();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001653
1654 SkPath::FillType nonInvertedFill = SkPath::ConvertToNonInverseFillType(fill);
1655 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1656 GrGLenum fillMode =
1657 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1658 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001659
1660 if (stroke.isFillStyle() || SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) {
1661 GL_CALL(StencilFillPath(id, fillMode, writeMask));
1662 }
1663 if (stroke.needToApply()) {
1664 GL_CALL(StencilStrokePath(id, 0xffff, writeMask));
1665 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001666
1667 if (nonInvertedFill == fill) {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001668 if (stroke.needToApply()) {
1669 GL_CALL(CoverStrokePath(id, GR_GL_BOUNDING_BOX));
1670 } else {
1671 GL_CALL(CoverFillPath(id, GR_GL_BOUNDING_BOX));
1672 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001673 } else {
1674 GrDrawState* drawState = this->drawState();
1675 GrDrawState::AutoViewMatrixRestore avmr;
1676 SkRect bounds = SkRect::MakeLTRB(0, 0,
1677 SkIntToScalar(drawState->getRenderTarget()->width()),
1678 SkIntToScalar(drawState->getRenderTarget()->height()));
1679 SkMatrix vmi;
1680 // mapRect through persp matrix may not be correct
1681 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
1682 vmi.mapRect(&bounds);
1683 // theoretically could set bloat = 0, instead leave it because of matrix inversion
1684 // precision.
1685 SkScalar bloat = drawState->getViewMatrix().getMaxStretch() * SK_ScalarHalf;
1686 bounds.outset(bloat, bloat);
1687 } else {
1688 avmr.setIdentity(drawState);
1689 }
1690
1691 this->drawSimpleRect(bounds, NULL);
1692 }
1693}
1694
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001695void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001696 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001697 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001698 // Some extensions automatically resolves the texture when it is read.
1699 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001700 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001701 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1702 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1703 // make sure we go through flushRenderTarget() since we've modified
1704 // the bound DRAW FBO ID.
1705 fHWBoundRenderTarget = NULL;
1706 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001707 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001708 GrGLIRect r;
1709 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1710 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001711
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001712 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001713 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001714 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001715 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001716 fScissorState.fEnabled = true;
1717 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001718 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001719 GL_CALL(ResolveMultisampleFramebuffer());
1720 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001721 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001722 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001723 asr.reset(&fScissorState);
1724 fScissorState.fEnabled = false;
1725 this->flushScissor();
1726 }
1727 int right = r.fLeft + r.fWidth;
1728 int top = r.fBottom + r.fHeight;
1729 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1730 r.fLeft, r.fBottom, right, top,
1731 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001732 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001734 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001735 }
1736}
1737
bsalomon@google.com411dad02012-06-05 20:24:20 +00001738namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001739
bsalomon@google.com411dad02012-06-05 20:24:20 +00001740GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1741 static const GrGLenum gTable[] = {
1742 GR_GL_ALWAYS, // kAlways_StencilFunc
1743 GR_GL_NEVER, // kNever_StencilFunc
1744 GR_GL_GREATER, // kGreater_StencilFunc
1745 GR_GL_GEQUAL, // kGEqual_StencilFunc
1746 GR_GL_LESS, // kLess_StencilFunc
1747 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1748 GR_GL_EQUAL, // kEqual_StencilFunc,
1749 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1750 };
1751 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1752 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1753 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1754 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1755 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1756 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1757 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1758 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1759 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001760 SkASSERT((unsigned) basicFunc < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001761
1762 return gTable[basicFunc];
1763}
1764
1765GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1766 static const GrGLenum gTable[] = {
1767 GR_GL_KEEP, // kKeep_StencilOp
1768 GR_GL_REPLACE, // kReplace_StencilOp
1769 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1770 GR_GL_INCR, // kIncClamp_StencilOp
1771 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1772 GR_GL_DECR, // kDecClamp_StencilOp
1773 GR_GL_ZERO, // kZero_StencilOp
1774 GR_GL_INVERT, // kInvert_StencilOp
1775 };
1776 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1777 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1778 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1779 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1780 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1781 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1782 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1783 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1784 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001785 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001786 return gTable[op];
1787}
1788
1789void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001790 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001791 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001792 GrStencilSettings::Face grFace) {
1793 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1794 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1795 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1796
1797 GrGLint ref = settings.funcRef(grFace);
1798 GrGLint mask = settings.funcMask(grFace);
1799 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001800
1801 if (GR_GL_FRONT_AND_BACK == glFace) {
1802 // we call the combined func just in case separate stencil is not
1803 // supported.
1804 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1805 GR_GL_CALL(gl, StencilMask(writeMask));
1806 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1807 } else {
1808 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1809 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1810 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1811 }
1812}
1813}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001814
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001815void GrGpuGL::flushStencil(DrawType type) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001816 if (kStencilPath_DrawType != type && fHWStencilSettings != fStencilSettings) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001817 if (fStencilSettings.isDisabled()) {
1818 if (kNo_TriState != fHWStencilTestEnabled) {
1819 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1820 fHWStencilTestEnabled = kNo_TriState;
1821 }
1822 } else {
1823 if (kYes_TriState != fHWStencilTestEnabled) {
1824 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1825 fHWStencilTestEnabled = kYes_TriState;
1826 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001827 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001828 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001829 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001830 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001831 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001832 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001833 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001834 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001835 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001836 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001837 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001838 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001839 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001840 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001841 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001842 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001843 }
1844 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001845 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001846 }
1847}
1848
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001849void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001850// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1851// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1852#if 0
1853 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1854 #define RT_HAS_MSAA rt->isMultisampled()
1855#else
1856 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1857#endif
1858
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001859 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001860 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001861 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1862 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001863 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001864 bool smoothLines = false;
1865
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001866 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001867 smoothLines = this->willUseHWAALines();
1868 if (smoothLines) {
1869 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1870 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1871 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1872 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00001873 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001874 kNo_TriState != fHWAAState.fMSAAEnabled) {
1875 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1876 fHWAAState.fMSAAEnabled = kNo_TriState;
1877 }
1878 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001879 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001880 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1881 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1882 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1883 }
1884 }
1885 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00001886 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001887 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1888 // convex hulls of each segment appear to get filled.
1889 bool enableMSAA = kStencilPath_DrawType == type ||
1890 this->getDrawState().isHWAntialiasState();
1891 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001892 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1893 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1894 fHWAAState.fMSAAEnabled = kYes_TriState;
1895 }
1896 } else {
1897 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1898 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1899 fHWAAState.fMSAAEnabled = kNo_TriState;
1900 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001901 }
1902 }
1903 }
1904}
1905
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001906void GrGpuGL::flushPathStencilSettings(SkPath::FillType fill) {
1907 GrStencilSettings pathStencilSettings;
1908 this->getPathStencilSettingsForFillType(fill, &pathStencilSettings);
1909 if (fHWPathStencilSettings != pathStencilSettings) {
1910 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1911 // that draws the path to the SB (glStencilFillPath)
1912 GrGLenum func =
1913 gr_to_gl_stencil_func(pathStencilSettings.func(GrStencilSettings::kFront_Face));
1914 GL_CALL(PathStencilFunc(func,
1915 pathStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1916 pathStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1917
1918 fHWPathStencilSettings = pathStencilSettings;
1919 }
1920}
1921
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001922void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001923 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001924 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001925 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001926 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001927 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001928 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001929 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001930 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1931 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1932 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1933 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1934 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1935 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001936 }
1937 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001938 // any optimization to disable blending should
1939 // have already been applied and tweaked the coeffs
1940 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001941 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1942 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001943 if (blendOff) {
1944 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001945 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001946 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001947 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001948 } else {
1949 if (kYes_TriState != fHWBlendState.fEnabled) {
1950 GL_CALL(Enable(GR_GL_BLEND));
1951 fHWBlendState.fEnabled = kYes_TriState;
1952 }
1953 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1954 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001955 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1956 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001957 fHWBlendState.fSrcCoeff = srcCoeff;
1958 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001959 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001960 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001961 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1962 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001963 (!fHWBlendState.fConstColorValid ||
1964 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00001965 GrGLfloat c[4];
1966 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001967 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001968 fHWBlendState.fConstColor = blendConst;
1969 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001970 }
1971 }
1972 }
1973}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001974
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00001975static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00001976 static const GrGLenum gWrapModes[] = {
1977 GR_GL_CLAMP_TO_EDGE,
1978 GR_GL_REPEAT,
1979 GR_GL_MIRRORED_REPEAT
1980 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00001981 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00001982 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
1983 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
1984 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
1985 return gWrapModes[tm];
1986}
1987
bsalomon@google.com34cccde2013-01-04 18:34:30 +00001988void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001989 SkASSERT(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001990
bsalomon@google.comb8670992012-07-25 21:27:09 +00001991 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
1992 // from the rt it will still be the last bound texture, but it needs resolving. So keep this
bsalomon@google.com4c883782012-06-04 19:05:11 +00001993 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00001994 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00001995 if (NULL != texRT) {
1996 this->onResolveRenderTarget(texRT);
1997 }
1998
bsalomon@google.com34cccde2013-01-04 18:34:30 +00001999 if (fHWBoundTextures[unitIdx] != texture) {
2000 this->setTextureUnit(unitIdx);
2001 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2002 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002003 }
2004
bsalomon@google.com4c883782012-06-04 19:05:11 +00002005 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002006 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002007 bool setAll = timestamp < this->getResetTimestamp();
2008 GrGLTexture::TexParams newTexParams;
2009
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002010 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002011 GR_GL_NEAREST,
2012 GR_GL_LINEAR,
2013 GR_GL_LINEAR_MIPMAP_LINEAR
2014 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002015 static GrGLenum glMagFilterModes[] = {
2016 GR_GL_NEAREST,
2017 GR_GL_LINEAR,
2018 GR_GL_LINEAR
2019 };
2020 newTexParams.fMinFilter = glMinFilterModes[params.filterMode()];
2021 newTexParams.fMagFilter = glMagFilterModes[params.filterMode()];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002022
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002023#ifndef SKIA_IGNORE_GPU_MIPMAPS
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002024 if (params.filterMode() == GrTextureParams::kMipMap_FilterMode &&
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002025 texture->mipMapsAreDirty()) {
2026// GL_CALL(Hint(GR_GL_GENERATE_MIPMAP_HINT,GR_GL_NICEST));
2027 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2028 texture->dirtyMipMaps(false);
2029 }
2030#endif
bsalomon@google.com4c883782012-06-04 19:05:11 +00002031
bsalomon@google.comb8670992012-07-25 21:27:09 +00002032 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2033 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002034 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002035 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002036 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002037 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002038 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002039 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2040 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002041 newTexParams.fMagFilter));
2042 }
2043 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2044 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002045 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2046 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002047 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002048 }
2049 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002050 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002051 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2052 GR_GL_TEXTURE_WRAP_S,
2053 newTexParams.fWrapS));
2054 }
2055 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002056 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002057 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2058 GR_GL_TEXTURE_WRAP_T,
2059 newTexParams.fWrapT));
2060 }
2061 if (this->glCaps().textureSwizzleSupport() &&
2062 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2063 oldTexParams.fSwizzleRGBA,
2064 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002065 this->setTextureUnit(unitIdx);
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002066 if (this->glBinding() == kES_GrGLBinding) {
2067 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2068 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2069 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2070 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2071 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2072 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2073 } else {
2074 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2075 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2076 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2077 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002078 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002079 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002080}
2081
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002082void GrGpuGL::setProjectionMatrix(const SkMatrix& matrix,
2083 const SkISize& renderTargetSize,
2084 GrSurfaceOrigin renderTargetOrigin) {
2085
2086 SkASSERT(this->glCaps().fixedFunctionSupport());
2087
2088 if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin &&
2089 renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize &&
2090 matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) {
2091 return;
2092 }
2093
2094 fHWProjectionMatrixState.fViewMatrix = matrix;
2095 fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize;
2096 fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin;
2097
2098 GrGLfloat glMatrix[4 * 4];
2099 fHWProjectionMatrixState.getGLMatrix<4>(glMatrix);
2100 GL_CALL(MatrixMode(GR_GL_PROJECTION));
2101 GL_CALL(LoadMatrixf(glMatrix));
2102}
2103
2104void GrGpuGL::enableTexGen(int unitIdx,
2105 TexGenComponents components,
2106 const GrGLfloat* coefficients) {
2107
2108 SkASSERT(this->glCaps().fixedFunctionSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00002109 SkASSERT(this->caps()->pathRenderingSupport());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002110 SkASSERT(components >= kS_TexGenComponents && components <= kSTR_TexGenComponents);
2111
2112 if (GR_GL_OBJECT_LINEAR == fHWTexGenSettings[unitIdx].fMode &&
2113 components == fHWTexGenSettings[unitIdx].fNumComponents &&
2114 !memcmp(coefficients, fHWTexGenSettings[unitIdx].fCoefficients,
2115 3 * components * sizeof(GrGLfloat))) {
2116 return;
2117 }
2118
2119 this->setTextureUnit(unitIdx);
2120
2121 if (GR_GL_OBJECT_LINEAR != fHWTexGenSettings[unitIdx].fMode) {
2122 for (int i = 0; i < 4; i++) {
2123 GL_CALL(TexGeni(GR_GL_S + i, GR_GL_TEXTURE_GEN_MODE, GR_GL_OBJECT_LINEAR));
2124 }
2125 fHWTexGenSettings[unitIdx].fMode = GR_GL_OBJECT_LINEAR;
2126 }
2127
2128 for (int i = fHWTexGenSettings[unitIdx].fNumComponents; i < components; i++) {
2129 GL_CALL(Enable(GR_GL_TEXTURE_GEN_S + i));
2130 }
2131 for (int i = components; i < fHWTexGenSettings[unitIdx].fNumComponents; i++) {
2132 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S + i));
2133 }
2134 fHWTexGenSettings[unitIdx].fNumComponents = components;
2135
2136 for (int i = 0; i < components; i++) {
2137 GrGLfloat plane[] = {coefficients[0 + 3 * i],
2138 coefficients[1 + 3 * i],
2139 0,
2140 coefficients[2 + 3 * i]};
2141 GL_CALL(TexGenfv(GR_GL_S + i, GR_GL_OBJECT_PLANE, plane));
2142 }
2143
2144 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + unitIdx,
2145 GR_GL_OBJECT_LINEAR,
2146 components,
2147 coefficients));
2148
2149 memcpy(fHWTexGenSettings[unitIdx].fCoefficients, coefficients,
2150 3 * components * sizeof(GrGLfloat));
2151
2152 fHWActiveTexGenSets = SkTMax(fHWActiveTexGenSets, unitIdx);
2153}
2154
2155void GrGpuGL::enableTexGen(int unitIdx, TexGenComponents components, const SkMatrix& matrix) {
2156
2157 GrGLfloat coefficients[3 * 3];
2158 SkASSERT(this->glCaps().fixedFunctionSupport());
2159 SkASSERT(components >= kS_TexGenComponents && components <= kSTR_TexGenComponents);
2160
2161 coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]);
2162 coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]);
2163 coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]);
2164
2165 if (components >= kST_TexGenComponents) {
2166 coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]);
2167 coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]);
2168 coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]);
2169 }
2170
2171 if (components >= kSTR_TexGenComponents) {
2172 coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]);
2173 coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]);
2174 coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]);
2175 }
2176
2177 enableTexGen(unitIdx, components, coefficients);
2178}
2179
2180void GrGpuGL::disableUnusedTexGen(int numUsedTexCoordSets) {
2181
2182 SkASSERT(this->glCaps().fixedFunctionSupport());
2183
2184 for (int i = numUsedTexCoordSets; i < fHWActiveTexGenSets; i++) {
2185 if (0 == fHWTexGenSettings[i].fNumComponents) {
2186 continue;
2187 }
2188
2189 this->setTextureUnit(i);
2190 for (int j = 0; j < fHWTexGenSettings[i].fNumComponents; j++) {
2191 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S + j));
2192 }
2193 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
2194 fHWTexGenSettings[i].fNumComponents = 0;
2195 }
2196
2197 fHWActiveTexGenSets = SkTMin(fHWActiveTexGenSets, numUsedTexCoordSets);
2198}
2199
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002200void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002201
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002202 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002203
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002204 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002205 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002206 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002207 fHWDitherEnabled = kYes_TriState;
2208 }
2209 } else {
2210 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002211 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002212 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002213 }
2214 }
2215
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002216 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002217 if (kNo_TriState != fHWWriteToColor) {
2218 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2219 GR_GL_FALSE, GR_GL_FALSE));
2220 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002221 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002222 } else {
2223 if (kYes_TriState != fHWWriteToColor) {
2224 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2225 fHWWriteToColor = kYes_TriState;
2226 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002227 }
2228
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002229 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002230 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002231 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002232 GL_CALL(Enable(GR_GL_CULL_FACE));
2233 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002234 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002235 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002236 GL_CALL(Enable(GR_GL_CULL_FACE));
2237 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002238 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002239 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002240 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002241 break;
2242 default:
2243 GrCrash("Unknown draw face.");
2244 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002245 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002246 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002247}
2248
reed@google.comac10a2d2010-12-22 21:39:39 +00002249void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002250 SkASSERT(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002251 if (fHWBoundRenderTarget == renderTarget) {
2252 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002253 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002254}
2255
2256void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002257 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002258 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002259 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002260 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002261 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002263}
2264
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002265bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2266 bool getSizedInternalFormat,
2267 GrGLenum* internalFormat,
2268 GrGLenum* externalFormat,
2269 GrGLenum* externalType) {
2270 GrGLenum dontCare;
2271 if (NULL == internalFormat) {
2272 internalFormat = &dontCare;
2273 }
2274 if (NULL == externalFormat) {
2275 externalFormat = &dontCare;
2276 }
2277 if (NULL == externalType) {
2278 externalType = &dontCare;
2279 }
2280
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002282 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002283 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002284 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002285 if (getSizedInternalFormat) {
2286 *internalFormat = GR_GL_RGBA8;
2287 } else {
2288 *internalFormat = GR_GL_RGBA;
2289 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002290 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002291 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002292 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002293 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002294 return false;
2295 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002296 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002297 if (getSizedInternalFormat) {
2298 *internalFormat = GR_GL_BGRA8;
2299 } else {
2300 *internalFormat = GR_GL_BGRA;
2301 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002302 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002303 if (getSizedInternalFormat) {
2304 *internalFormat = GR_GL_RGBA8;
2305 } else {
2306 *internalFormat = GR_GL_RGBA;
2307 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002308 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002309 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002310 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002311 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002312 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002313 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002314 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002315 if (getSizedInternalFormat) {
2316 if (this->glBinding() == kDesktop_GrGLBinding) {
2317 return false;
2318 } else {
2319 *internalFormat = GR_GL_RGB565;
2320 }
2321 } else {
2322 *internalFormat = GR_GL_RGB;
2323 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002324 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002325 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002326 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002327 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002328 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002329 if (getSizedInternalFormat) {
2330 *internalFormat = GR_GL_RGBA4;
2331 } else {
2332 *internalFormat = GR_GL_RGBA;
2333 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002334 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002335 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002336 case kIndex_8_GrPixelConfig:
bsalomon@google.combcce8922013-03-25 15:38:39 +00002337 if (this->caps()->eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002338 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002339 // glCompressedTexImage doesn't take external params
2340 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002341 // no sized/unsized internal format distinction here
2342 *internalFormat = GR_GL_PALETTE8_RGBA8;
2343 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002344 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002345 } else {
2346 return false;
2347 }
2348 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002349 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002350 if (this->glCaps().textureRedSupport()) {
2351 *internalFormat = GR_GL_RED;
2352 *externalFormat = GR_GL_RED;
2353 if (getSizedInternalFormat) {
2354 *internalFormat = GR_GL_R8;
2355 } else {
2356 *internalFormat = GR_GL_RED;
2357 }
2358 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002359 } else {
2360 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002361 *externalFormat = GR_GL_ALPHA;
2362 if (getSizedInternalFormat) {
2363 *internalFormat = GR_GL_ALPHA8;
2364 } else {
2365 *internalFormat = GR_GL_ALPHA;
2366 }
2367 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002368 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002369 break;
2370 default:
2371 return false;
2372 }
2373 return true;
2374}
2375
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002376void GrGpuGL::setTextureUnit(int unit) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002377 SkASSERT(unit >= 0 && unit < fHWBoundTextures.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002378 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002379 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002380 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002381 }
2382}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002383
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002384void GrGpuGL::setScratchTextureUnit() {
2385 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
2386 int lastUnitIdx = fHWBoundTextures.count() - 1;
2387 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2388 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2389 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002390 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002391 // clear out the this field so that if a program does use this unit it will rebind the correct
2392 // texture.
2393 fHWBoundTextures[lastUnitIdx] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002394}
2395
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002396namespace {
2397// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002398inline bool can_blit_framebuffer(const GrSurface* dst,
2399 const GrSurface* src,
2400 const GrGpuGL* gpu,
2401 bool* wouldNeedTempFBO = NULL) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002402 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2403 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002404 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002405 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2406 // or the rects are not the same (not just the same size but have the same edges).
2407 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2408 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2409 return false;
2410 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002411 if (NULL != wouldNeedTempFBO) {
2412 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2413 }
2414 return true;
2415 } else {
2416 return false;
2417 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002418}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002419
2420inline bool can_copy_texsubimage(const GrSurface* dst,
2421 const GrSurface* src,
2422 const GrGpuGL* gpu,
2423 bool* wouldNeedTempFBO = NULL) {
2424 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2425 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2426 // many drivers would allow it to work, but ANGLE does not.
bsalomon@google.com791816a2013-08-15 18:54:39 +00002427 if (kES_GrGLBinding == gpu->glBinding() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002428 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2429 return false;
2430 }
2431 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2432 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2433 // then we don't want to copy to the texture but to the MSAA buffer.
2434 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2435 return false;
2436 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002437 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2438 // If the src is multisampled (and uses an extension where there is a separate MSAA
2439 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2440 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2441 return false;
2442 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002443 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
2444 NULL != dst->asTexture() &&
2445 dst->origin() == src->origin() &&
2446 kIndex_8_GrPixelConfig != src->config()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002447 if (NULL != wouldNeedTempFBO) {
2448 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2449 }
2450 return true;
2451 } else {
2452 return false;
2453 }
2454}
2455
2456// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2457// relative to is output.
2458inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2459 GrSurface* surface,
2460 GrGLenum fboTarget,
2461 GrGLIRect* viewport) {
2462 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2463 GrGLuint tempFBOID;
2464 if (NULL == rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002465 SkASSERT(NULL != surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002466 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2467 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2468 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2469 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2470 GR_GL_COLOR_ATTACHMENT0,
2471 GR_GL_TEXTURE_2D,
2472 texID,
2473 0));
2474 viewport->fLeft = 0;
2475 viewport->fBottom = 0;
2476 viewport->fWidth = surface->width();
2477 viewport->fHeight = surface->height();
2478 } else {
2479 tempFBOID = 0;
2480 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2481 *viewport = rt->getViewport();
2482 }
2483 return tempFBOID;
2484}
2485
2486}
2487
2488void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2489 // Check for format issues with glCopyTexSubImage2D
bsalomon@google.com791816a2013-08-15 18:54:39 +00002490 if (kES_GrGLBinding == this->glBinding() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002491 kBGRA_8888_GrPixelConfig == src->config()) {
2492 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002493 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002494 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002495 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002496 } else if (NULL == src->asRenderTarget()) {
2497 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2498 // class handle it by rendering.
2499 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002500 return;
2501 }
2502
2503 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2504 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2505 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2506 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002507 } else {
2508 desc->fConfig = src->config();
2509 desc->fOrigin = src->origin();
2510 desc->fFlags = kNone_GrTextureFlags;
2511 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002512}
2513
2514bool GrGpuGL::onCopySurface(GrSurface* dst,
2515 GrSurface* src,
2516 const SkIRect& srcRect,
2517 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002518 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002519 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002520 bool wouldNeedTempFBO = false;
2521 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2522 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2523 GrGLuint srcFBO;
2524 GrGLIRect srcVP;
2525 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2526 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002527 SkASSERT(NULL != dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002528 // We modified the bound FBO
2529 fHWBoundRenderTarget = NULL;
2530 GrGLIRect srcGLRect;
2531 srcGLRect.setRelativeTo(srcVP,
2532 srcRect.fLeft,
2533 srcRect.fTop,
2534 srcRect.width(),
2535 srcRect.height(),
2536 src->origin());
2537
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002538 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002539 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2540 GrGLint dstY;
2541 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2542 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2543 } else {
2544 dstY = dstPoint.fY;
2545 }
2546 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2547 dstPoint.fX, dstY,
2548 srcGLRect.fLeft, srcGLRect.fBottom,
2549 srcGLRect.fWidth, srcGLRect.fHeight));
2550 copied = true;
2551 if (srcFBO) {
2552 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2553 }
2554 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2555 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002556 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2557 srcRect.width(), srcRect.height());
2558 bool selfOverlap = false;
2559 if (dst->isSameAs(src)) {
2560 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2561 }
2562
2563 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002564 GrGLuint dstFBO;
2565 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002566 GrGLIRect dstVP;
2567 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002568 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2569 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2570 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002571 fHWBoundRenderTarget = NULL;
2572 GrGLIRect srcGLRect;
2573 GrGLIRect dstGLRect;
2574 srcGLRect.setRelativeTo(srcVP,
2575 srcRect.fLeft,
2576 srcRect.fTop,
2577 srcRect.width(),
2578 srcRect.height(),
2579 src->origin());
2580 dstGLRect.setRelativeTo(dstVP,
2581 dstRect.fLeft,
2582 dstRect.fTop,
2583 dstRect.width(),
2584 dstRect.height(),
2585 dst->origin());
2586
2587 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002588 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002589 // The EXT version applies the scissor during the blit, so disable it.
2590 asr.reset(&fScissorState);
2591 fScissorState.fEnabled = false;
2592 this->flushScissor();
2593 }
2594 GrGLint srcY0;
2595 GrGLint srcY1;
2596 // Does the blit need to y-mirror or not?
2597 if (src->origin() == dst->origin()) {
2598 srcY0 = srcGLRect.fBottom;
2599 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2600 } else {
2601 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2602 srcY1 = srcGLRect.fBottom;
2603 }
2604 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2605 srcY0,
2606 srcGLRect.fLeft + srcGLRect.fWidth,
2607 srcY1,
2608 dstGLRect.fLeft,
2609 dstGLRect.fBottom,
2610 dstGLRect.fLeft + dstGLRect.fWidth,
2611 dstGLRect.fBottom + dstGLRect.fHeight,
2612 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2613 if (dstFBO) {
2614 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2615 }
2616 if (srcFBO) {
2617 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2618 }
2619 copied = true;
2620 }
2621 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002622 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002623 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002624 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002625 }
2626 return copied;
2627}
2628
2629bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2630 GrSurface* src,
2631 const SkIRect& srcRect,
2632 const SkIPoint& dstPoint) {
2633 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002634 if (can_copy_texsubimage(dst, src, this)) {
2635 return true;
2636 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002637 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002638 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002639 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2640 srcRect.width(), srcRect.height());
2641 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2642 return true;
2643 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002644 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002645 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002646 }
2647 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002648 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002649}
2650
2651
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002652///////////////////////////////////////////////////////////////////////////////
2653
bsalomon@google.com6918d482013-03-07 19:09:11 +00002654GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2655 GrGpuGL* gpu,
2656 const GrGLVertexBuffer* vbuffer,
2657 const GrGLIndexBuffer* ibuffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002658 SkASSERT(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002659 GrGLAttribArrayState* attribState;
2660
bsalomon@google.com6918d482013-03-07 19:09:11 +00002661 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2662 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2663 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2664 SkSafeUnref(fVBOVertexArray);
2665 GrGLuint arrayID;
2666 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2667 int attrCount = gpu->glCaps().maxVertexAttributes();
2668 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002669 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002670 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2671 } else {
2672 if (NULL != ibuffer) {
2673 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2674 } else {
2675 this->setVertexArrayID(gpu, 0);
2676 }
2677 int attrCount = gpu->glCaps().maxVertexAttributes();
2678 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2679 fDefaultVertexArrayAttribState.resize(attrCount);
2680 }
2681 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002682 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002683 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002684}