blob: ec535b8a1428f4b44d80ee0190934c02eeb6f5ba [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"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000011#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000012#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
twiz@google.com0f31ca72011-03-18 17:38:11 +000014static const GrGLuint GR_MAX_GLUINT = ~0;
15static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com0b77d682011-08-19 13:28:54 +000017#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000018#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019
bsalomon@google.com316f99232011-01-13 21:28:12 +000020// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000022static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000026#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
27 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
30#else
31 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
34#endif
35
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000036
37///////////////////////////////////////////////////////////////////////////////
38
twiz@google.com0f31ca72011-03-18 17:38:11 +000039static const GrGLenum gXfermodeCoeff2Blend[] = {
40 GR_GL_ZERO,
41 GR_GL_ONE,
42 GR_GL_SRC_COLOR,
43 GR_GL_ONE_MINUS_SRC_COLOR,
44 GR_GL_DST_COLOR,
45 GR_GL_ONE_MINUS_DST_COLOR,
46 GR_GL_SRC_ALPHA,
47 GR_GL_ONE_MINUS_SRC_ALPHA,
48 GR_GL_DST_ALPHA,
49 GR_GL_ONE_MINUS_DST_ALPHA,
50 GR_GL_CONSTANT_COLOR,
51 GR_GL_ONE_MINUS_CONSTANT_COLOR,
52 GR_GL_CONSTANT_ALPHA,
53 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000054
55 // extended blend coeffs
56 GR_GL_SRC1_COLOR,
57 GR_GL_ONE_MINUS_SRC1_COLOR,
58 GR_GL_SRC1_ALPHA,
59 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000060};
61
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000063 static const bool gCoeffReferencesBlendConst[] = {
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 true,
75 true,
76 true,
77 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000078
79 // extended blend coeffs
80 false,
81 false,
82 false,
83 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000084 };
85 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000086 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
87
88 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
89 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
90 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
91 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
92 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
93 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
94 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
95 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
96 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
97 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
98 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
99 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
100 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
101 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
102
103 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
104 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
105 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
106 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
107
108 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
109 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000110}
111
reed@google.comac10a2d2010-12-22 21:39:39 +0000112///////////////////////////////////////////////////////////////////////////////
113
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000114static bool gPrintStartupSpew;
115
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000116static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000117
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000118 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000119
twiz@google.com0f31ca72011-03-18 17:38:11 +0000120 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000121 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
122 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000123 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000124 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
125 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000126 // some implementations require texture to be mip-map complete before
127 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000128 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000129 GR_GL_TEXTURE_MIN_FILTER,
130 GR_GL_NEAREST));
131 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
132 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
133 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
134 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
135 GR_GL_COLOR_ATTACHMENT0,
136 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000137 GrGLenum status;
138 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000139 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
140 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000141
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000142 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000143}
144
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000145GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
146
147 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000148
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000149 fillInConfigRenderableTable();
150
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000151 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000152
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000153 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000154
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000155 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000156 const GrGLubyte* ext;
157 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000158 const GrGLubyte* vendor;
159 const GrGLubyte* renderer;
160 const GrGLubyte* version;
161 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
162 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
163 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000164 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
165 this);
166 GrPrintf("------ VENDOR %s\n", vendor);
167 GrPrintf("------ RENDERER %s\n", renderer);
168 GrPrintf("------ VERSION %s\n", version);
169 GrPrintf("------ EXTENSIONS\n %s \n", ext);
170 }
171
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000172 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000173
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000174 fProgramData = NULL;
175 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000176
177#if 0
178 this->programUnitTest();
179#endif
180
bsalomon@google.comfe676522011-06-17 18:12:21 +0000181 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000182 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000183}
184
185GrGpuGL::~GrGpuGL() {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000186 if (fProgramData && 0 != fHWProgramID) {
187 // detach the current program so there is no confusion on OpenGL's part
188 // that we want it to be deleted
189 GrAssert(fHWProgramID == fProgramData->fProgramID);
190 GL_CALL(UseProgram(0));
191 }
192
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000193 delete fProgramCache;
194 fProgramCache = NULL;
195 fProgramData = NULL;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000196
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000197 // This must be called by before the GrDrawTarget destructor
198 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000199 // This subclass must do this before the base class destructor runs
200 // since we will unref the GrGLInterface.
201 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000202}
203
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000204///////////////////////////////////////////////////////////////////////////////
205
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000206void GrGpuGL::initCaps() {
207 GrGLint maxTextureUnits;
208 // check FS and fixed-function texture unit limits
209 // we only use textures in the fragment stage currently.
210 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000211 const GrGLInterface* gl = this->glInterface();
212 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000213 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000214
215 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000216 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000217 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000218 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000219 for (int i = 0; i < numFormats; ++i) {
220 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
221 fCaps.f8BitPaletteSupport = true;
222 break;
223 }
224 }
225
226 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000227 // we could also look for GL_ATI_separate_stencil extension or
228 // GL_EXT_stencil_two_side but they use different function signatures
229 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000230 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000231 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000232 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000233 this->hasExtension("GL_EXT_stencil_wrap");
234 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000235 // ES 2 has two sided stencil and stencil wrap
236 fCaps.fTwoSidedStencilSupport = true;
237 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 }
239
240 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000241 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
242 // extension includes glMapBuffer.
243 } else {
244 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
245 }
246
247 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000248 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000249 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
250 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 } else {
252 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 }
254 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000255 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000256 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000257 }
258
259 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
260
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000261 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
262 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000263 // Our render targets are always created with textures as the color
264 // attachment, hence this min:
265 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
266
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000267 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000268
269 // Enable supported shader-related caps
270 if (kDesktop_GrGLBinding == this->glBinding()) {
271 fCaps.fDualSourceBlendingSupport =
272 this->glVersion() >= GR_GL_VER(3,3) ||
273 this->hasExtension("GL_ARB_blend_func_extended");
274 fCaps.fShaderDerivativeSupport = true;
275 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
276 fCaps.fGeometryShaderSupport =
277 this->glVersion() >= GR_GL_VER(3,2) &&
278 this->glslGeneration() >= k150_GrGLSLGeneration;
279 } else {
280 fCaps.fShaderDerivativeSupport =
281 this->hasExtension("GL_OES_standard_derivatives");
282 }
283
284 GR_GL_GetIntegerv(this->glInterface(),
285 GR_GL_MAX_VERTEX_ATTRIBS,
286 &fMaxVertexAttribs);
287
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000288}
289
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000290void GrGpuGL::fillInConfigRenderableTable() {
291
292 // OpenGL < 3.0
293 // no support for render targets unless the GL_ARB_framebuffer_object
294 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
295 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
296 // probably don't get R8 in this case.
297
298 // OpenGL 3.0
299 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
300 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
301
302 // >= OpenGL 3.1
303 // base color renderable: RED, RG, RGB, and RGBA
304 // sized derivatives: R8, RGBA4, RGBA8
305 // if the GL_ARB_compatibility extension is supported then we get back
306 // support for GL_ALPHA and ALPHA8
307
308 // GL_EXT_bgra adds BGRA render targets to any version
309
310 // ES 2.0
311 // color renderable: RGBA4, RGB5_A1, RGB565
312 // GL_EXT_texture_rg adds support for R8 as a color render target
313 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
314 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
315 // added BGRA support
316
317 if (kDesktop_GrGLBinding == this->glBinding()) {
318 // Post 3.0 we will get R8
319 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
320 if (this->glVersion() >= GR_GL_VER(3,0) ||
321 this->hasExtension("GL_ARB_framebuffer_object")) {
322 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
323 }
324 } else {
325 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000326 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
327 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000328 }
329
330 if (kDesktop_GrGLBinding != this->glBinding()) {
331 // only available in ES
332 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
333 }
334
335 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
336 // GL_EXT_framebuffer_object for FBO support. Both of these
337 // allow RGBA4 render targets so this is always supported.
338 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
339
340 if (this->glCaps().rgba8RenderbufferSupport()) {
341 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
342 }
343
344 if (this->glCaps().bgraFormatSupport()) {
345 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
346 }
347
348 // the un-premultiplied formats just inherit the premultiplied setting
349 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
350 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
351 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
352 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
353}
354
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000355bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
356 if (kUnknown_CanPreserveUnpremulRoundtrip ==
357 fCanPreserveUnpremulRoundtrip) {
358
359 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
360 uint32_t* srcData = data.get();
361 uint32_t* firstRead = data.get() + 256 * 256;
362 uint32_t* secondRead = data.get() + 2 * 256 * 256;
363
364 for (int y = 0; y < 256; ++y) {
365 for (int x = 0; x < 256; ++x) {
366 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
367 color[3] = y;
368 color[2] = x;
369 color[1] = x;
370 color[0] = x;
371 }
372 }
373
374 // We have broader support for read/write pixels on render targets
375 // than on textures.
376 GrTextureDesc dstDesc;
377 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
378 kNoStencil_GrTextureFlagBit;
379 dstDesc.fWidth = 256;
380 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000381 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000382 dstDesc.fSampleCnt = 0;
383
384 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
385 if (!dstTex.get()) {
386 return false;
387 }
388 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
389 GrAssert(NULL != rt);
390
391 bool failed = true;
392 static const UnpremulConversion gMethods[] = {
393 kUpOnWrite_DownOnRead_UnpremulConversion,
394 kDownOnWrite_UpOnRead_UnpremulConversion,
395 };
396
397 // pretend that we can do the roundtrip to avoid recursive calls to
398 // this function
399 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
400 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
401 fUnpremulConversion = gMethods[i];
402 rt->writePixels(0, 0,
403 256, 256,
404 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
405 rt->readPixels(0, 0,
406 256, 256,
407 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
408 rt->writePixels(0, 0,
409 256, 256,
410 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
411 rt->readPixels(0, 0,
412 256, 256,
413 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
414 failed = false;
415 for (int j = 0; j < 256 * 256; ++j) {
416 if (firstRead[j] != secondRead[j]) {
417 failed = true;
418 break;
419 }
420 }
421 }
422 fCanPreserveUnpremulRoundtrip = failed ?
423 kNo_CanPreserveUnpremulRoundtrip :
424 kYes_CanPreserveUnpremulRoundtrip;
425 }
426
427 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
428 return true;
429 } else {
430 return false;
431 }
432}
433
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000434GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000435 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
436 return GrPixelConfigSwapRAndB(config);
437 } else {
438 return config;
439 }
440}
441
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000442GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000443 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000444 return GrPixelConfigSwapRAndB(config);
445 } else {
446 return config;
447 }
448}
449
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000450bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
451 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
452}
453
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000454void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000455 if (gPrintStartupSpew && !fPrintedCaps) {
456 fPrintedCaps = true;
457 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000458 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000459 }
460
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000461 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000462 GL_CALL(Disable(GR_GL_DEPTH_TEST));
463 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000464
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000465 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
466 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000468 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000469 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000470 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000471 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
472 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
473 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
474 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
475 GL_CALL(Disable(GR_GL_COLOR_TABLE));
476 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
477 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
478 // Since ES doesn't support glPointSize at all we always use the VS to
479 // set the point size
480 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
481
482 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
483 // currently part of our gl interface. There are probably others as
484 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000485 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000486 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000487 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000488
reed@google.comac10a2d2010-12-22 21:39:39 +0000489 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000490 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000491
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000492 // invalid
493 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000494
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000495 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000496
tomhudson@google.com93813632011-10-27 20:21:16 +0000497 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000498 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000499 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000500
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000501 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000502 // set to true to force disableScissor to make a GL call.
503 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000504 this->disableScissor();
505
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000506 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000507
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000508 fHWStencilSettings.invalidate();
509 fHWStencilClipMode = kInvalid_StencilClipMode;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000510
511 // TODO: I believe this should actually go in GrGpu::onResetContext
512 // rather than here
513 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000514
515 fHWGeometryState.fIndexBuffer = NULL;
516 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000517
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000518 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000519
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000520 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000521
522 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000523 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000524 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
525 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000526 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000527 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
528 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000529 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000530 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
531 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000532 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000533 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
534 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000535
536 fHWGeometryState.fVertexOffset = ~0;
537
538 // Third party GL code may have left vertex attributes enabled. Some GL
539 // implementations (osmesa) may read vetex attributes that are not required
540 // by the current shader. Therefore, we have to ensure that only the
541 // attributes we require for the current draw are enabled or we may cause an
542 // invalid read.
543
544 // Disable all vertex layout bits so that next flush will assume all
545 // optional vertex attributes are disabled.
546 fHWGeometryState.fVertexLayout = 0;
547
548 // We always use the this attribute and assume it is always enabled.
549 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
550 GL_CALL(EnableVertexAttribArray(posAttrIdx));
551 // Disable all other vertex attributes.
552 for (int va = 0; va < fMaxVertexAttribs; ++va) {
553 if (va != posAttrIdx) {
554 GL_CALL(DisableVertexAttribArray(va));
555 }
556 }
557
558 fHWProgramID = 0;
559 fHWConstAttribColor = GrColor_ILLEGAL;
560 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000561}
562
bsalomon@google.come269f212011-11-07 13:29:52 +0000563GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000564 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000565 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000566 return NULL;
567 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000568
bsalomon@google.com99621082011-11-15 16:47:16 +0000569 glTexDesc.fWidth = desc.fWidth;
570 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000571 glTexDesc.fConfig = desc.fConfig;
572 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
573 glTexDesc.fOwnsID = false;
574 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
575
576 GrGLTexture* texture = NULL;
577 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
578 GrGLRenderTarget::Desc glRTDesc;
579 glRTDesc.fRTFBOID = 0;
580 glRTDesc.fTexFBOID = 0;
581 glRTDesc.fMSColorRenderbufferID = 0;
582 glRTDesc.fOwnIDs = true;
583 glRTDesc.fConfig = desc.fConfig;
584 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000585 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
586 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000587 glTexDesc.fTextureID,
588 &glRTDesc)) {
589 return NULL;
590 }
591 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
592 } else {
593 texture = new GrGLTexture(this, glTexDesc);
594 }
595 if (NULL == texture) {
596 return NULL;
597 }
598
599 this->setSpareTextureUnit();
600 return texture;
601}
602
603GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
604 GrGLRenderTarget::Desc glDesc;
605 glDesc.fConfig = desc.fConfig;
606 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
607 glDesc.fMSColorRenderbufferID = 0;
608 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
609 glDesc.fSampleCnt = desc.fSampleCnt;
610 glDesc.fOwnIDs = false;
611 GrGLIRect viewport;
612 viewport.fLeft = 0;
613 viewport.fBottom = 0;
614 viewport.fWidth = desc.fWidth;
615 viewport.fHeight = desc.fHeight;
616
617 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
618 if (desc.fStencilBits) {
619 GrGLStencilBuffer::Format format;
620 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
621 format.fPacked = false;
622 format.fStencilBits = desc.fStencilBits;
623 format.fTotalBits = desc.fStencilBits;
624 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
625 0,
626 desc.fWidth,
627 desc.fHeight,
628 desc.fSampleCnt,
629 format);
630 tgt->setStencilBuffer(sb);
631 sb->unref();
632 }
633 return tgt;
634}
635
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000636////////////////////////////////////////////////////////////////////////////////
637
bsalomon@google.com6f379512011-11-16 20:36:03 +0000638void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
639 int left, int top, int width, int height,
640 GrPixelConfig config, const void* buffer,
641 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000642 if (NULL == buffer) {
643 return;
644 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000645 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
646
bsalomon@google.com6f379512011-11-16 20:36:03 +0000647 this->setSpareTextureUnit();
648 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
649 GrGLTexture::Desc desc;
650 desc.fConfig = glTex->config();
651 desc.fWidth = glTex->width();
652 desc.fHeight = glTex->height();
653 desc.fOrientation = glTex->orientation();
654 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000655
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000656 this->uploadTexData(desc, false,
657 left, top, width, height,
658 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000659}
660
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000661namespace {
662bool adjust_pixel_ops_params(int surfaceWidth,
663 int surfaceHeight,
664 size_t bpp,
665 int* left, int* top, int* width, int* height,
666 const void** data,
667 size_t* rowBytes) {
668 if (!*rowBytes) {
669 *rowBytes = *width * bpp;
670 }
671
672 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
673 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
674
675 if (!subRect.intersect(bounds)) {
676 return false;
677 }
678 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
679 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
680
681 *left = subRect.fLeft;
682 *top = subRect.fTop;
683 *width = subRect.width();
684 *height = subRect.height();
685 return true;
686}
687}
688
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000689bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
690 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000691 int left, int top, int width, int height,
692 GrPixelConfig dataConfig,
693 const void* data,
694 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000695 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000696
697 size_t bpp = GrBytesPerPixel(dataConfig);
698 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
699 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000700 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000701 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000702 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000703
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000704 // in case we need a temporary, trimmed copy of the src pixels
705 SkAutoSMalloc<128 * 128> tempStorage;
706
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000707 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000708 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000709 if (useTexStorage) {
710 if (kDesktop_GrGLBinding == this->glBinding()) {
711 // 565 is not a sized internal format on desktop GL. So on desktop
712 // with 565 we always use an unsized internal format to let the
713 // system pick the best sized format to convert the 565 data to.
714 // Since glTexStorage only allows sized internal formats we will
715 // instead fallback to glTexImage2D.
716 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
717 } else {
718 // ES doesn't allow paletted textures to be used with tex storage
719 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
720 }
721 }
722
723 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000724 GrGLenum externalFormat;
725 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000726 // glTexStorage requires sized internal formats on both desktop and ES. ES
727 // glTexImage requires an unsized format.
728 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
729 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000730 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000731 }
732
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000733 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000734 // paletted textures cannot be updated
735 return false;
736 }
737
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000738 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000739 * check whether to allocate a temporary buffer for flipping y or
740 * because our srcData has extra bytes past each row. If so, we need
741 * to trim those off here, since GL ES may not let us specify
742 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000743 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000744 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000745 bool swFlipY = false;
746 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000747 if (NULL != data) {
748 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000749 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000750 glFlipY = true;
751 } else {
752 swFlipY = true;
753 }
754 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000755 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000756 // can't use this for flipping, only non-neg values allowed. :(
757 if (rowBytes != trimRowBytes) {
758 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
759 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
760 restoreGLRowLength = true;
761 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000762 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000763 if (trimRowBytes != rowBytes || swFlipY) {
764 // copy data into our new storage, skipping the trailing bytes
765 size_t trimSize = height * trimRowBytes;
766 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000767 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000768 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000769 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000770 char* dst = (char*)tempStorage.reset(trimSize);
771 for (int y = 0; y < height; y++) {
772 memcpy(dst, src, trimRowBytes);
773 if (swFlipY) {
774 src -= rowBytes;
775 } else {
776 src += rowBytes;
777 }
778 dst += trimRowBytes;
779 }
780 // now point data to our copied version
781 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000782 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000783 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000784 if (glFlipY) {
785 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
786 }
787 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000788 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000789 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000790 if (isNewTexture &&
791 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000792 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000793 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000794 if (useTexStorage) {
795 // We never resize or change formats of textures. We don't use
796 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000797 GL_ALLOC_CALL(this->glInterface(),
798 TexStorage2D(GR_GL_TEXTURE_2D,
799 1, // levels
800 internalFormat,
801 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000802 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000803 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
804 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
805 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000806 GL_ALLOC_CALL(this->glInterface(),
807 CompressedTexImage2D(GR_GL_TEXTURE_2D,
808 0, // level
809 internalFormat,
810 desc.fWidth, desc.fHeight,
811 0, // border
812 imageSize,
813 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000814 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000815 GL_ALLOC_CALL(this->glInterface(),
816 TexImage2D(GR_GL_TEXTURE_2D,
817 0, // level
818 internalFormat,
819 desc.fWidth, desc.fHeight,
820 0, // border
821 externalFormat, externalType,
822 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000823 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000824 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000825 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000826 if (error != GR_GL_NO_ERROR) {
827 succeeded = false;
828 } else {
829 // if we have data and we used TexStorage to create the texture, we
830 // now upload with TexSubImage.
831 if (NULL != data && useTexStorage) {
832 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
833 0, // level
834 left, top,
835 width, height,
836 externalFormat, externalType,
837 data));
838 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000839 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000840 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000841 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000842 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000843 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000844 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
845 0, // level
846 left, top,
847 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000848 externalFormat, externalType, data));
849 }
850
851 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000852 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000853 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000854 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000855 if (glFlipY) {
856 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
857 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000858 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000859}
860
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000861namespace {
862bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
863 int sampleCount,
864 GrGLenum format,
865 int width, int height) {
866 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
867 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
868 bool created = false;
869 if (GrGLCaps::kNVDesktop_CoverageAAType ==
870 ctxInfo.caps().coverageAAType()) {
871 const GrGLCaps::MSAACoverageMode& mode =
872 ctxInfo.caps().getMSAACoverageMode(sampleCount);
873 GL_ALLOC_CALL(ctxInfo.interface(),
874 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
875 mode.fCoverageSampleCnt,
876 mode.fColorSampleCnt,
877 format,
878 width, height));
879 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
880 }
881 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000882 // glRBMS will fail if requested samples is > max samples.
883 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000884 GL_ALLOC_CALL(ctxInfo.interface(),
885 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
886 sampleCount,
887 format,
888 width, height));
889 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
890 }
891 return created;
892}
893}
894
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000895bool GrGpuGL::createRenderTargetObjects(int width, int height,
896 GrGLuint texID,
897 GrGLRenderTarget::Desc* desc) {
898 desc->fMSColorRenderbufferID = 0;
899 desc->fRTFBOID = 0;
900 desc->fTexFBOID = 0;
901 desc->fOwnIDs = true;
902
903 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904
bsalomon@google.comab15d612011-08-09 12:57:56 +0000905 GrGLenum msColorFormat = 0; // suppress warning
906
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000907 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 if (!desc->fTexFBOID) {
909 goto FAILED;
910 }
911
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912
913 // If we are using multisampling we will create two FBOS. We render
914 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000915 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000916 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000917 goto FAILED;
918 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
920 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921 if (!desc->fRTFBOID ||
922 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000923 !this->configToGLFormats(desc->fConfig,
924 // GLES requires sized internal formats
925 kES2_GrGLBinding == this->glBinding(),
926 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000927 goto FAILED;
928 }
929 } else {
930 desc->fRTFBOID = desc->fTexFBOID;
931 }
932
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000933 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000934 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935 if (desc->fRTFBOID != desc->fTexFBOID) {
936 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000937 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000938 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000939 if (!renderbuffer_storage_msaa(fGLContextInfo,
940 desc->fSampleCnt,
941 msColorFormat,
942 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943 goto FAILED;
944 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000945 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
946 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000947 GR_GL_COLOR_ATTACHMENT0,
948 GR_GL_RENDERBUFFER,
949 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000950 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000951 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
952 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
953 goto FAILED;
954 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000955 fGLContextInfo.caps().markConfigAsValidColorAttachment(
956 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000957 }
958 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000959 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000960
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000961 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
962 GR_GL_COLOR_ATTACHMENT0,
963 GR_GL_TEXTURE_2D,
964 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000965 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000966 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
967 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
968 goto FAILED;
969 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000970 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 }
972
973 return true;
974
975FAILED:
976 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000977 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000978 }
979 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000980 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000981 }
982 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000983 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000984 }
985 return false;
986}
987
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000988// good to set a break-point here to know when createTexture fails
989static GrTexture* return_null_texture() {
990// GrAssert(!"null texture");
991 return NULL;
992}
993
994#if GR_DEBUG
995static size_t as_size_t(int x) {
996 return x;
997}
998#endif
999
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001000GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001001 const void* srcData,
1002 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
1004#if GR_COLLECT_STATS
1005 ++fStats.fTextureCreateCnt;
1006#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001007
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001008 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001009 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001010
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001011 // Attempt to catch un- or wrongly initialized sample counts;
1012 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1013
bsalomon@google.com99621082011-11-15 16:47:16 +00001014 glTexDesc.fWidth = desc.fWidth;
1015 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001016 glTexDesc.fConfig = desc.fConfig;
1017 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001018
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001019 glRTDesc.fMSColorRenderbufferID = 0;
1020 glRTDesc.fRTFBOID = 0;
1021 glRTDesc.fTexFBOID = 0;
1022 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001023 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001024
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001025 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001026
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001027 const Caps& caps = this->getCaps();
1028
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001029 // We keep GrRenderTargets in GL's normal orientation so that they
1030 // can be drawn to by the outside world without the client having
1031 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001032 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001033 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001034
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001035 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001036 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001037 desc.fSampleCnt) {
1038 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001039 }
1040
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001042 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1043 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001044 return return_null_texture();
1045 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 }
1047
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001049 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001050 // provides a hint about how this texture will be used
1051 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1052 GR_GL_TEXTURE_USAGE,
1053 GR_GL_FRAMEBUFFER_ATTACHMENT));
1054 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001055 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001056 return return_null_texture();
1057 }
1058
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001059 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001060 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001061
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001062 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1063 // drivers have a bug where an FBO won't be complete if it includes a
1064 // texture that is not mipmap complete (considering the filter in use).
1065 GrGLTexture::TexParams initialTexParams;
1066 // we only set a subset here so invalidate first
1067 initialTexParams.invalidate();
1068 initialTexParams.fFilter = GR_GL_NEAREST;
1069 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1070 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001071 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1072 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001073 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001074 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1075 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001076 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001077 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1078 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001079 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001080 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1081 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001082 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001083 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1084 glTexDesc.fWidth, glTexDesc.fHeight,
1085 desc.fConfig, srcData, rowBytes)) {
1086 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1087 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001088 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001089
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001090 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 if (renderTarget) {
1092#if GR_COLLECT_STATS
1093 ++fStats.fRenderTargetCreateCnt;
1094#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001095 // unbind the texture from the texture unit before binding it to the frame buffer
1096 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1097
bsalomon@google.com99621082011-11-15 16:47:16 +00001098 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1099 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001100 glTexDesc.fTextureID,
1101 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001102 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001103 return return_null_texture();
1104 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001105 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001107 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001109 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001110#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001111 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1112 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001113#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 return tex;
1115}
1116
1117namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001118
1119const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1120
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001121void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1122 GrGLuint rb,
1123 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001124 // we shouldn't ever know one size and not the other
1125 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1126 (kUnknownBitCount == format->fTotalBits));
1127 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001128 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001129 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1130 (GrGLint*)&format->fStencilBits);
1131 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001132 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1134 (GrGLint*)&format->fTotalBits);
1135 format->fTotalBits += format->fStencilBits;
1136 } else {
1137 format->fTotalBits = format->fStencilBits;
1138 }
1139 }
1140}
1141}
1142
1143bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1144 int width, int height) {
1145
1146 // All internally created RTs are also textures. We don't create
1147 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1148 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001149 GrAssert(width >= rt->width());
1150 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151
1152 int samples = rt->numSamples();
1153 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001154 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 if (!sbID) {
1156 return false;
1157 }
1158
1159 GrGLStencilBuffer* sb = NULL;
1160
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001161 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001163 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001164 // we start with the last stencil format that succeeded in hopes
1165 // that we won't go through this loop more than once after the
1166 // first (painful) stencil creation.
1167 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001168 const GrGLCaps::StencilFormat& sFmt =
1169 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001170 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001171 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001173 bool created;
1174 if (samples > 0) {
1175 created = renderbuffer_storage_msaa(fGLContextInfo,
1176 samples,
1177 sFmt.fInternalFormat,
1178 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001180 GL_ALLOC_CALL(this->glInterface(),
1181 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001182 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001183 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001184 created =
1185 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001186 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001187 if (created) {
1188 // After sized formats we attempt an unsized format and take
1189 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001190 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001192 sb = new GrGLStencilBuffer(this, sbID, width, height,
1193 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1195 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001196 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 return true;
1199 }
1200 sb->abandon(); // otherwise we lose sbID
1201 sb->unref();
1202 }
1203 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001205 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206}
1207
1208bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1209 GrRenderTarget* rt) {
1210 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1211
1212 GrGLuint fbo = glrt->renderFBOID();
1213
1214 if (NULL == sb) {
1215 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 GR_GL_STENCIL_ATTACHMENT,
1218 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 GR_GL_DEPTH_ATTACHMENT,
1221 GR_GL_RENDERBUFFER, 0));
1222#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001223 GrGLenum status;
1224 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1226#endif
1227 }
1228 return true;
1229 } else {
1230 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1231 GrGLuint rb = glsb->renderbufferID();
1232
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001233 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1235 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 GR_GL_STENCIL_ATTACHMENT,
1237 GR_GL_RENDERBUFFER, rb));
1238 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001239 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 GR_GL_DEPTH_ATTACHMENT,
1241 GR_GL_RENDERBUFFER, rb));
1242 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001243 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 GR_GL_DEPTH_ATTACHMENT,
1245 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001248 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001249 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001250 glsb->format())) {
1251 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1252 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001253 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001254 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001256 if (glsb->format().fPacked) {
1257 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1258 GR_GL_DEPTH_ATTACHMENT,
1259 GR_GL_RENDERBUFFER, 0));
1260 }
1261 return false;
1262 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001263 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001264 rt->config(),
1265 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001266 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001267 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001268 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001270}
1271
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001272////////////////////////////////////////////////////////////////////////////////
1273
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001274GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001275 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001276 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001278 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001279 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001280 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001282 GL_ALLOC_CALL(this->glInterface(),
1283 BufferData(GR_GL_ARRAY_BUFFER,
1284 size,
1285 NULL, // data ptr
1286 dynamic ? GR_GL_DYNAMIC_DRAW :
1287 GR_GL_STATIC_DRAW));
1288 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 // deleting bound buffer does implicit bind to 0
1291 fHWGeometryState.fVertexBuffer = NULL;
1292 return NULL;
1293 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001294 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 size, dynamic);
1296 fHWGeometryState.fVertexBuffer = vertexBuffer;
1297 return vertexBuffer;
1298 }
1299 return NULL;
1300}
1301
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001302GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001303 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001304 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001305 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001306 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001307 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001308 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001309 GL_ALLOC_CALL(this->glInterface(),
1310 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1311 size,
1312 NULL, // data ptr
1313 dynamic ? GR_GL_DYNAMIC_DRAW :
1314 GR_GL_STATIC_DRAW));
1315 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001316 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 // deleting bound buffer does implicit bind to 0
1318 fHWGeometryState.fIndexBuffer = NULL;
1319 return NULL;
1320 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001321 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 size, dynamic);
1323 fHWGeometryState.fIndexBuffer = indexBuffer;
1324 return indexBuffer;
1325 }
1326 return NULL;
1327}
1328
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001329void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001330 const GrDrawState& drawState = this->getDrawState();
1331 const GrGLRenderTarget* rt =
1332 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1333
1334 GrAssert(NULL != rt);
1335 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001336
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001337 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001338 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1339 rect.width(), rect.height());
1340 if (scissor.contains(vp)) {
1341 disableScissor();
1342 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001343 }
1344
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001345 if (fHWBounds.fScissorRect != scissor) {
1346 scissor.pushToGLScissor(this->glInterface());
1347 fHWBounds.fScissorRect = scissor;
1348 }
1349 if (!fHWBounds.fScissorEnabled) {
1350 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1351 fHWBounds.fScissorEnabled = true;
1352 }
1353}
1354
1355void GrGpuGL::disableScissor() {
1356 if (fHWBounds.fScissorEnabled) {
1357 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1358 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001359 }
1360}
1361
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001362void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001363 const GrDrawState& drawState = this->getDrawState();
1364 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001365 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001366 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001367
bsalomon@google.com74b98712011-11-11 19:46:16 +00001368 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001369 if (NULL != rect) {
1370 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001371 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001372 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001373 if (clippedRect.intersect(rtRect)) {
1374 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001375 } else {
1376 return;
1377 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001378 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001379 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001380 if (NULL != rect)
1381 this->enableScissoring(*rect);
1382 else
1383 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001384
1385 GrGLfloat r, g, b, a;
1386 static const GrGLfloat scale255 = 1.f / 255.f;
1387 a = GrColorUnpackA(color) * scale255;
1388 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001389 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001390 scaleRGB *= a;
1391 }
1392 r = GrColorUnpackR(color) * scaleRGB;
1393 g = GrColorUnpackG(color) * scaleRGB;
1394 b = GrColorUnpackB(color) * scaleRGB;
1395
1396 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001397 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001398 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001399 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001400}
1401
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001402void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001403 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001404 return;
1405 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001406
1407 this->flushRenderTarget(&GrIRect::EmptyIRect());
1408
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001409 this->disableScissor();
1410
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001411 GL_CALL(StencilMask(0xffffffff));
1412 GL_CALL(ClearStencil(0));
1413 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001414 fHWStencilSettings.invalidate();
1415 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001416}
1417
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001418void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001419 const GrDrawState& drawState = this->getDrawState();
1420 const GrRenderTarget* rt = drawState.getRenderTarget();
1421 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001422
1423 // this should only be called internally when we know we have a
1424 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001425 GrAssert(NULL != rt->getStencilBuffer());
1426 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001427#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001428 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001429 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001430#else
1431 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001432 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001433 // turned into draws. Our contract on GrDrawTarget says that
1434 // changing the clip between stencil passes may or may not
1435 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001436 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001437#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001438 GrGLint value;
1439 if (insideClip) {
1440 value = (1 << (stencilBitCount - 1));
1441 } else {
1442 value = 0;
1443 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001444 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001445 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001446 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001447 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001448 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001449 fHWStencilSettings.invalidate();
1450 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001451}
1452
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001453void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001454 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001455}
1456
bsalomon@google.comc4364992011-11-07 15:54:49 +00001457bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1458 int left, int top,
1459 int width, int height,
1460 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001461 size_t rowBytes) const {
1462 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001463 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001464 return false;
1465 }
1466
1467 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001468 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001469 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001470 return true;
1471 }
1472 // If we have to do memcpys to handle rowBytes then y-flip is free
1473 // Note the rowBytes might be tight to the passed in data, but if data
1474 // gets clipped in x to the target the rowBytes will no longer be tight.
1475 if (left >= 0 && (left + width) < renderTarget->width()) {
1476 return 0 == rowBytes ||
1477 GrBytesPerPixel(config) * width == rowBytes;
1478 } else {
1479 return false;
1480 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001481}
1482
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001483bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001484 int left, int top,
1485 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001486 GrPixelConfig config,
1487 void* buffer,
1488 size_t rowBytes,
1489 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001490 GrGLenum format;
1491 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001492 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001493 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001494 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001495 size_t bpp = GrBytesPerPixel(config);
1496 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1497 &left, &top, &width, &height,
1498 const_cast<const void**>(&buffer),
1499 &rowBytes)) {
1500 return false;
1501 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001502
bsalomon@google.comc6980972011-11-02 19:57:21 +00001503 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001504 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001505 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001506 switch (tgt->getResolveType()) {
1507 case GrGLRenderTarget::kCantResolve_ResolveType:
1508 return false;
1509 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001510 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001511 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001512 break;
1513 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001514 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001515 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001516 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1517 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001518 break;
1519 default:
1520 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 }
1522
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001523 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001524
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001525 // the read rect is viewport-relative
1526 GrGLIRect readRect;
1527 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001528
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001529 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001530 if (0 == rowBytes) {
1531 rowBytes = tightRowBytes;
1532 }
1533 size_t readDstRowBytes = tightRowBytes;
1534 void* readDst = buffer;
1535
1536 // determine if GL can read using the passed rowBytes or if we need
1537 // a scratch buffer.
1538 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1539 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001540 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001541 GrAssert(!(rowBytes % sizeof(GrColor)));
1542 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1543 readDstRowBytes = rowBytes;
1544 } else {
1545 scratch.reset(tightRowBytes * height);
1546 readDst = scratch.get();
1547 }
1548 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001549 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001550 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1551 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001552 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1553 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001554 format, type, readDst));
1555 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001556 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001557 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1558 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001559 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001560 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1561 invertY = true;
1562 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001563
1564 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001565 // API presents top-to-bottom. We must preserve the padding contents. Note
1566 // that the above readPixels did not overwrite the padding.
1567 if (readDst == buffer) {
1568 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001569 if (!invertY) {
1570 scratch.reset(tightRowBytes);
1571 void* tmpRow = scratch.get();
1572 // flip y in-place by rows
1573 const int halfY = height >> 1;
1574 char* top = reinterpret_cast<char*>(buffer);
1575 char* bottom = top + (height - 1) * rowBytes;
1576 for (int y = 0; y < halfY; y++) {
1577 memcpy(tmpRow, top, tightRowBytes);
1578 memcpy(top, bottom, tightRowBytes);
1579 memcpy(bottom, tmpRow, tightRowBytes);
1580 top += rowBytes;
1581 bottom -= rowBytes;
1582 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001583 }
1584 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001585 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001586 // copy from readDst to buffer while flipping y
1587 const int halfY = height >> 1;
1588 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001589 char* dst = reinterpret_cast<char*>(buffer);
1590 if (!invertY) {
1591 dst += (height-1) * rowBytes;
1592 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001593 for (int y = 0; y < height; y++) {
1594 memcpy(dst, src, tightRowBytes);
1595 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001596 if (invertY) {
1597 dst += rowBytes;
1598 } else {
1599 dst -= rowBytes;
1600 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001601 }
1602 }
1603 return true;
1604}
1605
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001606void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001607
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001608 GrGLRenderTarget* rt =
1609 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1610 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001611
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001612 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001613 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 #if GR_COLLECT_STATS
1615 ++fStats.fRenderTargetChngCnt;
1616 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001618 GrGLenum status;
1619 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001620 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001621 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001622 }
1623 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001624 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001625 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001626 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001627 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 fHWBounds.fViewportRect = vp;
bsalomon@google.com890e3b52012-06-01 19:01:37 +00001629 // View matrices pushed to GL depend upon the viewport. So they
1630 // are now invalid.
1631 fProgramCache->invalidateViewMatrices();
reed@google.comac10a2d2010-12-22 21:39:39 +00001632 }
1633 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001634 if (NULL == bound || !bound->isEmpty()) {
1635 rt->flagAsNeedingResolve(bound);
1636 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001637}
1638
twiz@google.com0f31ca72011-03-18 17:38:11 +00001639GrGLenum gPrimitiveType2GLMode[] = {
1640 GR_GL_TRIANGLES,
1641 GR_GL_TRIANGLE_STRIP,
1642 GR_GL_TRIANGLE_FAN,
1643 GR_GL_POINTS,
1644 GR_GL_LINES,
1645 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001646};
1647
bsalomon@google.comd302f142011-03-03 13:54:13 +00001648#define SWAP_PER_DRAW 0
1649
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001650#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001651 #if GR_MAC_BUILD
1652 #include <AGL/agl.h>
1653 #elif GR_WIN32_BUILD
1654 void SwapBuf() {
1655 DWORD procID = GetCurrentProcessId();
1656 HWND hwnd = GetTopWindow(GetDesktopWindow());
1657 while(hwnd) {
1658 DWORD wndProcID = 0;
1659 GetWindowThreadProcessId(hwnd, &wndProcID);
1660 if(wndProcID == procID) {
1661 SwapBuffers(GetDC(hwnd));
1662 }
1663 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1664 }
1665 }
1666 #endif
1667#endif
1668
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001669void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1670 uint32_t startVertex,
1671 uint32_t startIndex,
1672 uint32_t vertexCount,
1673 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001674 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1675
twiz@google.com0f31ca72011-03-18 17:38:11 +00001676 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001677
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001678 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1679 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1680
1681 // our setupGeometry better have adjusted this to zero since
1682 // DrawElements always draws from the begining of the arrays for idx 0.
1683 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001684
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001685 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1686 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001687#if SWAP_PER_DRAW
1688 glFlush();
1689 #if GR_MAC_BUILD
1690 aglSwapBuffers(aglGetCurrentContext());
1691 int set_a_break_pt_here = 9;
1692 aglSwapBuffers(aglGetCurrentContext());
1693 #elif GR_WIN32_BUILD
1694 SwapBuf();
1695 int set_a_break_pt_here = 9;
1696 SwapBuf();
1697 #endif
1698#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001699}
1700
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001701void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1702 uint32_t startVertex,
1703 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001704 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1705
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001706 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1707
1708 // our setupGeometry better have adjusted this to zero.
1709 // DrawElements doesn't take an offset so we always adjus the startVertex.
1710 GrAssert(0 == startVertex);
1711
1712 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1713 // account for startVertex in the DrawElements case. So we always
1714 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001715 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001716#if SWAP_PER_DRAW
1717 glFlush();
1718 #if GR_MAC_BUILD
1719 aglSwapBuffers(aglGetCurrentContext());
1720 int set_a_break_pt_here = 9;
1721 aglSwapBuffers(aglGetCurrentContext());
1722 #elif GR_WIN32_BUILD
1723 SwapBuf();
1724 int set_a_break_pt_here = 9;
1725 SwapBuf();
1726 #endif
1727#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001728}
1729
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001730void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1731
1732 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001733
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001734 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001735 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001737 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1738 rt->renderFBOID()));
1739 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1740 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001741 #if GR_COLLECT_STATS
1742 ++fStats.fRenderTargetChngCnt;
1743 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001744 // make sure we go through flushRenderTarget() since we've modified
1745 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001746 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001747 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001748 const GrIRect dirtyRect = rt->getResolveRect();
1749 GrGLIRect r;
1750 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1751 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001752
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001753 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001754 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001755#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001756 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1757 GL_CALL(Scissor(r.fLeft, r.fBottom,
1758 r.fWidth, r.fHeight));
1759 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001760 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001762#else
1763 this->enableScissoring(dirtyRect);
1764 GL_CALL(ResolveMultisampleFramebuffer());
1765#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001767 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001768 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001769 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1770 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001771 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001772 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001773 int right = r.fLeft + r.fWidth;
1774 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001775 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1776 r.fLeft, r.fBottom, right, top,
1777 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001778 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001779 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 }
1781}
1782
twiz@google.com0f31ca72011-03-18 17:38:11 +00001783static const GrGLenum grToGLStencilFunc[] = {
1784 GR_GL_ALWAYS, // kAlways_StencilFunc
1785 GR_GL_NEVER, // kNever_StencilFunc
1786 GR_GL_GREATER, // kGreater_StencilFunc
1787 GR_GL_GEQUAL, // kGEqual_StencilFunc
1788 GR_GL_LESS, // kLess_StencilFunc
1789 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1790 GR_GL_EQUAL, // kEqual_StencilFunc,
1791 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001792};
1793GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1794GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1795GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1796GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1797GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1798GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1799GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1800GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1801GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1802
twiz@google.com0f31ca72011-03-18 17:38:11 +00001803static const GrGLenum grToGLStencilOp[] = {
1804 GR_GL_KEEP, // kKeep_StencilOp
1805 GR_GL_REPLACE, // kReplace_StencilOp
1806 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1807 GR_GL_INCR, // kIncClamp_StencilOp
1808 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1809 GR_GL_DECR, // kDecClamp_StencilOp
1810 GR_GL_ZERO, // kZero_StencilOp
1811 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001812};
1813GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1814GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1815GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1816GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1817GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1818GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1819GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1820GR_STATIC_ASSERT(6 == kZero_StencilOp);
1821GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1822
reed@google.comac10a2d2010-12-22 21:39:39 +00001823void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001824 const GrDrawState& drawState = this->getDrawState();
1825
1826 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001827
1828 // use stencil for clipping if clipping is enabled and the clip
1829 // has been written into the stencil.
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001830 StencilClipMode clipMode;
1831 if (fClipMaskManager.isClipInStencil() &&
1832 drawState.isClipState()) {
1833 clipMode = kUseClip_StencilClipMode;
1834 // We can't be modifying the clip and respecting it at the same time.
1835 GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1836 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
1837 clipMode = kModifyClip_StencilClipMode;
1838 } else {
1839 clipMode = kIgnoreClip_StencilClipMode;
1840 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001841
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001842 bool stencilChange = (fHWStencilSettings != *settings) ||
1843 (fHWStencilClipMode != clipMode);
reed@google.comac10a2d2010-12-22 21:39:39 +00001844 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001845
bsalomon@google.comd302f142011-03-03 13:54:13 +00001846 if (settings->isDisabled()) {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001847 if (kUseClip_StencilClipMode == clipMode) {
digit@google.com9b482c42012-02-16 22:03:26 +00001848 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001849 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001850 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001851
1852 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001853 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001854 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001855 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001856 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001857 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001858 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1859 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1860 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1861 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1862 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1863 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1864 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1865 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866 }
1867 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001868 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001869 GrStencilBuffer* stencilBuffer =
1870 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001871 if (NULL != stencilBuffer) {
1872 stencilBits = stencilBuffer->bits();
1873 }
1874 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001875 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001876
1877 GrGLuint clipStencilMask = 0;
1878 GrGLuint userStencilMask = ~0;
1879 if (stencilBits > 0) {
1880 clipStencilMask = 1 << (stencilBits - 1);
1881 userStencilMask = clipStencilMask - 1;
1882 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001884 unsigned int frontRef = settings->frontFuncRef();
1885 unsigned int frontMask = settings->frontFuncMask();
1886 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001887 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001888
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001889 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001890 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1891 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001892 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001893 bool useClip = kUseClip_StencilClipMode == clipMode;
1894 frontFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1895 settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001896
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001897 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001898 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001899 clipStencilMask,
1900 userStencilMask,
1901 &frontRef,
1902 &frontMask);
1903 frontWriteMask &= userStencilMask;
1904 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001905 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001906 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001907 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001908 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001909 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001910 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001911 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001912 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001913 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001914 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001915
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001916 unsigned int backRef = settings->backFuncRef();
1917 unsigned int backMask = settings->backFuncMask();
1918 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001919
1920
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001921 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001922 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1923 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001924 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001925 bool useClip = kUseClip_StencilClipMode == clipMode;
1926 backFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1927 settings->backFunc())];
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001928 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001929 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930 clipStencilMask,
1931 userStencilMask,
1932 &backRef,
1933 &backMask);
1934 backWriteMask &= userStencilMask;
1935 }
1936
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1938 frontRef, frontMask));
1939 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1940 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1941 backRef, backMask));
1942 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1943 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001944 grToGLStencilOp[settings->frontFailOp()],
1945 grToGLStencilOp[settings->frontPassOp()],
1946 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001947
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001948 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001949 grToGLStencilOp[settings->backFailOp()],
1950 grToGLStencilOp[settings->backPassOp()],
1951 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001952 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001953 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1954 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001955 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1956 grToGLStencilOp[settings->frontPassOp()],
1957 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001958 }
1959 }
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001960 fHWStencilSettings = *settings;
1961 fHWStencilClipMode = clipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001962 }
1963}
1964
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001965void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001966 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001967 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001968 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1969 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001970 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001971 bool smoothLines = false;
1972
bsalomon@google.com0650e812011-04-08 18:07:53 +00001973 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001974 smoothLines = this->willUseHWAALines();
1975 if (smoothLines) {
1976 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1977 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1978 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1979 // must disable msaa to use line smoothing
1980 if (rt->isMultisampled() &&
1981 kNo_TriState != fHWAAState.fMSAAEnabled) {
1982 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1983 fHWAAState.fMSAAEnabled = kNo_TriState;
1984 }
1985 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001986 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001987 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1988 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1989 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1990 }
1991 }
1992 }
1993 if (!smoothLines && rt->isMultisampled()) {
1994 if (this->getDrawState().isHWAntialiasState()) {
1995 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1996 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1997 fHWAAState.fMSAAEnabled = kYes_TriState;
1998 }
1999 } else {
2000 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2001 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2002 fHWAAState.fMSAAEnabled = kNo_TriState;
2003 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002004 }
2005 }
2006 }
2007}
2008
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002009void GrGpuGL::flushBlend(GrPrimitiveType type,
2010 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002011 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002012 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002013 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002014 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002015 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002016 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002017 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
2018 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002019 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2020 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002021 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
2022 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002023 }
2024 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002025 // any optimization to disable blending should
2026 // have already been applied and tweaked the coeffs
2027 // to (1, 0).
2028 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2029 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002030 if (blendOff) {
2031 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002032 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002033 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002034 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002035 } else {
2036 if (kYes_TriState != fHWBlendState.fEnabled) {
2037 GL_CALL(Enable(GR_GL_BLEND));
2038 fHWBlendState.fEnabled = kYes_TriState;
2039 }
2040 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2041 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002042 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2043 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002044 fHWBlendState.fSrcCoeff = srcCoeff;
2045 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002046 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002047 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002048 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2049 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002050 (!fHWBlendState.fConstColorValid ||
2051 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002052
2053 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002054 GrColorUnpackR(blendConst) / 255.f,
2055 GrColorUnpackG(blendConst) / 255.f,
2056 GrColorUnpackB(blendConst) / 255.f,
2057 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002058 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002059 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002060 fHWBlendState.fConstColor = blendConst;
2061 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002062 }
2063 }
2064 }
2065}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002066namespace {
2067
2068unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002069 switch (filter) {
2070 case GrSamplerState::kBilinear_Filter:
2071 case GrSamplerState::k4x4Downsample_Filter:
2072 return GR_GL_LINEAR;
2073 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002074 return GR_GL_NEAREST;
2075 default:
2076 GrAssert(!"Unknown filter type");
2077 return GR_GL_LINEAR;
2078 }
2079}
2080
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002081// get_swizzle is only called from this .cpp so it is OK to inline it here
2082inline const GrGLenum* get_swizzle(GrPixelConfig config,
2083 const GrSamplerState& sampler,
2084 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002085 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002086 if (glCaps.textureRedSupport()) {
2087 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2088 GR_GL_RED, GR_GL_RED };
2089 return gRedSmear;
2090 } else {
2091 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2092 GR_GL_ALPHA, GR_GL_ALPHA };
2093 return gAlphaSmear;
2094 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002095 } else if (sampler.swapsRAndB()) {
2096 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2097 GR_GL_RED, GR_GL_ALPHA };
2098 return gRedBlueSwap;
2099 } else {
2100 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2101 GR_GL_BLUE, GR_GL_ALPHA };
2102 return gStraight;
2103 }
2104}
2105
2106void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002107 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2108 GR_GL_TEXTURE_SWIZZLE_RGBA,
2109 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002110}
2111}
2112
bsalomon@google.comffca4002011-02-22 20:34:01 +00002113bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002114
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002115 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002116 // GrGpu::setupClipAndFlushState should have already checked this
2117 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002118 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002119
tomhudson@google.com93813632011-10-27 20:21:16 +00002120 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002121 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002122 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002123 GrGLTexture* nextTexture =
2124 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002125
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002126 // true for now, but maybe not with GrEffect.
2127 GrAssert(NULL != nextTexture);
2128 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002129 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002130 // the last bound texture, but it needs resolving. So keep this
2131 // out of the "last != next" check.
2132 GrGLRenderTarget* texRT =
2133 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2134 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002135 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002136 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002137
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002138 if (fHWBoundTextures[s] != nextTexture) {
2139 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002140 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002141 #if GR_COLLECT_STATS
2142 ++fStats.fTextureChngCnt;
2143 #endif
2144 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002145 fHWBoundTextures[s] = nextTexture;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002146 }
2147
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002148 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002149 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002150 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002151 nextTexture->getCachedTexParams(&timestamp);
2152 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002153 GrGLTexture::TexParams newTexParams;
2154
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002155 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002156
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002157 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002158 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2159 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002160 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002161 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002162 sizeof(newTexParams.fSwizzleRGBA));
2163 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002164 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002166 GR_GL_TEXTURE_MAG_FILTER,
2167 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002168 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002169 GR_GL_TEXTURE_MIN_FILTER,
2170 newTexParams.fFilter));
2171 }
2172 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002173 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002174 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002175 GR_GL_TEXTURE_WRAP_S,
2176 newTexParams.fWrapS));
2177 }
2178 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002179 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002180 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002181 GR_GL_TEXTURE_WRAP_T,
2182 newTexParams.fWrapT));
2183 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002184 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002185 (setAll ||
2186 memcmp(newTexParams.fSwizzleRGBA,
2187 oldTexParams.fSwizzleRGBA,
2188 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002189 this->setTextureUnit(s);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002190 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2191 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002192 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002193 nextTexture->setCachedTexParams(newTexParams,
2194 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002195 }
2196 }
2197
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002198 GrIRect* rect = NULL;
2199 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002200 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002201 fClip.hasConservativeBounds()) {
2202 fClip.getConservativeBounds().roundOut(&clipBounds);
2203 rect = &clipBounds;
2204 }
2205 this->flushRenderTarget(rect);
2206 this->flushAAState(type);
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002207
2208 if (drawState->isDitherState()) {
2209 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002210 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002211 fHWDitherEnabled = kYes_TriState;
2212 }
2213 } else {
2214 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002215 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002216 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002217 }
2218 }
2219
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002220 if (drawState->isColorWriteDisabled()) {
2221 if (kNo_TriState != fHWWriteToColor) {
2222 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2223 GR_GL_FALSE, GR_GL_FALSE));
2224 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002225 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002226 } else {
2227 if (kYes_TriState != fHWWriteToColor) {
2228 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2229 fHWWriteToColor = kYes_TriState;
2230 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002231 }
2232
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002233 if (fHWDrawFace != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002234 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002235 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002236 GL_CALL(Enable(GR_GL_CULL_FACE));
2237 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002238 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002239 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002240 GL_CALL(Enable(GR_GL_CULL_FACE));
2241 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002242 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002243 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002244 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002245 break;
2246 default:
2247 GrCrash("Unknown draw face.");
2248 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002249 fHWDrawFace = drawState->getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002250 }
2251
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002252#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002253 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002254 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002255 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002256 NULL == drawState->getRenderTarget() ||
2257 NULL == drawState->getTexture(s) ||
2258 drawState->getTexture(s)->asRenderTarget() !=
2259 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002260 }
2261#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002262
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002263 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002264
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002265 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002266}
2267
2268void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002269 if (fHWGeometryState.fVertexBuffer != buffer) {
2270 fHWGeometryState.fArrayPtrsDirty = true;
2271 fHWGeometryState.fVertexBuffer = buffer;
2272 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002273}
2274
2275void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002276 if (fHWGeometryState.fVertexBuffer == buffer) {
2277 // deleting bound buffer does implied bind to 0
2278 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002279 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 }
2281}
2282
2283void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002284 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002285}
2286
2287void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 if (fHWGeometryState.fIndexBuffer == buffer) {
2289 // deleting bound buffer does implied bind to 0
2290 fHWGeometryState.fIndexBuffer = NULL;
2291 }
2292}
2293
reed@google.comac10a2d2010-12-22 21:39:39 +00002294void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2295 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002296 GrDrawState* drawState = this->drawState();
2297 if (drawState->getRenderTarget() == renderTarget) {
2298 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002299 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002300 if (fHWBoundRenderTarget == renderTarget) {
2301 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002302 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002303}
2304
2305void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002306 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002307 GrDrawState* drawState = this->drawState();
2308 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002309 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002310 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002311 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002312 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002313 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002314 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002315 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002316}
2317
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002318bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2319 bool getSizedInternalFormat,
2320 GrGLenum* internalFormat,
2321 GrGLenum* externalFormat,
2322 GrGLenum* externalType) {
2323 GrGLenum dontCare;
2324 if (NULL == internalFormat) {
2325 internalFormat = &dontCare;
2326 }
2327 if (NULL == externalFormat) {
2328 externalFormat = &dontCare;
2329 }
2330 if (NULL == externalType) {
2331 externalType = &dontCare;
2332 }
2333
reed@google.comac10a2d2010-12-22 21:39:39 +00002334 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002335 case kRGBA_8888_PM_GrPixelConfig:
2336 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002337 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002338 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002339 if (getSizedInternalFormat) {
2340 *internalFormat = GR_GL_RGBA8;
2341 } else {
2342 *internalFormat = GR_GL_RGBA;
2343 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002344 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002345 break;
2346 case kBGRA_8888_PM_GrPixelConfig:
2347 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002348 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002349 return false;
2350 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002351 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002352 if (getSizedInternalFormat) {
2353 *internalFormat = GR_GL_BGRA8;
2354 } else {
2355 *internalFormat = GR_GL_BGRA;
2356 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002357 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002358 if (getSizedInternalFormat) {
2359 *internalFormat = GR_GL_RGBA8;
2360 } else {
2361 *internalFormat = GR_GL_RGBA;
2362 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002363 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002364 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002365 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002366 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002367 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002368 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002369 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002370 if (getSizedInternalFormat) {
2371 if (this->glBinding() == kDesktop_GrGLBinding) {
2372 return false;
2373 } else {
2374 *internalFormat = GR_GL_RGB565;
2375 }
2376 } else {
2377 *internalFormat = GR_GL_RGB;
2378 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002379 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002380 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002381 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002382 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002383 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002384 if (getSizedInternalFormat) {
2385 *internalFormat = GR_GL_RGBA4;
2386 } else {
2387 *internalFormat = GR_GL_RGBA;
2388 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002389 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002390 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002391 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002392 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002393 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002394 // glCompressedTexImage doesn't take external params
2395 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002396 // no sized/unsized internal format distinction here
2397 *internalFormat = GR_GL_PALETTE8_RGBA8;
2398 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002399 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002400 } else {
2401 return false;
2402 }
2403 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002404 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002405 if (this->glCaps().textureRedSupport()) {
2406 *internalFormat = GR_GL_RED;
2407 *externalFormat = GR_GL_RED;
2408 if (getSizedInternalFormat) {
2409 *internalFormat = GR_GL_R8;
2410 } else {
2411 *internalFormat = GR_GL_RED;
2412 }
2413 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002414 } else {
2415 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002416 *externalFormat = GR_GL_ALPHA;
2417 if (getSizedInternalFormat) {
2418 *internalFormat = GR_GL_ALPHA8;
2419 } else {
2420 *internalFormat = GR_GL_ALPHA;
2421 }
2422 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002423 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002424 break;
2425 default:
2426 return false;
2427 }
2428 return true;
2429}
2430
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002431void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002432 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002433 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002434 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002435 fActiveTextureUnitIdx = unit;
2436 }
2437}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002438
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002439void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002440 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002441 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002442 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2443 }
2444}
2445
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002446void GrGpuGL::setBuffers(bool indexed,
2447 int* extraVertexOffset,
2448 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002449
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002450 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002451
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002452 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2453
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002454 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002455 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002456 case kBuffer_GeometrySrcType:
2457 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002458 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459 break;
2460 case kArray_GeometrySrcType:
2461 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002462 this->finalizeReservedVertices();
2463 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2464 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002465 break;
2466 default:
2467 vbuf = NULL; // suppress warning
2468 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002469 }
2470
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002471 GrAssert(NULL != vbuf);
2472 GrAssert(!vbuf->isLocked());
2473 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002474 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002475 fHWGeometryState.fArrayPtrsDirty = true;
2476 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002477 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002478
2479 if (indexed) {
2480 GrAssert(NULL != extraIndexOffset);
2481
2482 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002483 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002484 case kBuffer_GeometrySrcType:
2485 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002486 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002487 break;
2488 case kArray_GeometrySrcType:
2489 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002490 this->finalizeReservedIndices();
2491 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2492 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002493 break;
2494 default:
2495 ibuf = NULL; // suppress warning
2496 GrCrash("Unknown geometry src type!");
2497 }
2498
2499 GrAssert(NULL != ibuf);
2500 GrAssert(!ibuf->isLocked());
2501 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002502 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002503 fHWGeometryState.fIndexBuffer = ibuf;
2504 }
2505 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002506}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002507