blob: 004702feb5a19e687d24c9b4a11aed1ad4c7ae98 [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
bsalomon@google.comfe676522011-06-17 18:12:21 +0000177 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000178 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000179}
180
181GrGpuGL::~GrGpuGL() {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000182 if (fProgramData && 0 != fHWProgramID) {
183 // detach the current program so there is no confusion on OpenGL's part
184 // that we want it to be deleted
185 GrAssert(fHWProgramID == fProgramData->fProgramID);
186 GL_CALL(UseProgram(0));
187 }
188
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000189 delete fProgramCache;
190 fProgramCache = NULL;
191 fProgramData = NULL;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000192
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000193 // This must be called by before the GrDrawTarget destructor
194 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000195 // This subclass must do this before the base class destructor runs
196 // since we will unref the GrGLInterface.
197 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000198}
199
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000200///////////////////////////////////////////////////////////////////////////////
201
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000202void GrGpuGL::initCaps() {
203 GrGLint maxTextureUnits;
204 // check FS and fixed-function texture unit limits
205 // we only use textures in the fragment stage currently.
206 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000207 const GrGLInterface* gl = this->glInterface();
208 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000209 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210
211 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000212 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000213 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000214 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000215 for (int i = 0; i < numFormats; ++i) {
216 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
217 fCaps.f8BitPaletteSupport = true;
218 break;
219 }
220 }
221
222 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000223 // we could also look for GL_ATI_separate_stencil extension or
224 // GL_EXT_stencil_two_side but they use different function signatures
225 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000226 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000227 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000228 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000229 this->hasExtension("GL_EXT_stencil_wrap");
230 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000231 // ES 2 has two sided stencil and stencil wrap
232 fCaps.fTwoSidedStencilSupport = true;
233 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234 }
235
236 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
238 // extension includes glMapBuffer.
239 } else {
240 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
241 }
242
243 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000244 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000245 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
246 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000247 } else {
248 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000249 }
250 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000251 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000252 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 }
254
255 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
256
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000257 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
258 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 // Our render targets are always created with textures as the color
260 // attachment, hence this min:
261 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
262
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000263 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000264
265 // Enable supported shader-related caps
266 if (kDesktop_GrGLBinding == this->glBinding()) {
267 fCaps.fDualSourceBlendingSupport =
268 this->glVersion() >= GR_GL_VER(3,3) ||
269 this->hasExtension("GL_ARB_blend_func_extended");
270 fCaps.fShaderDerivativeSupport = true;
271 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
272 fCaps.fGeometryShaderSupport =
273 this->glVersion() >= GR_GL_VER(3,2) &&
274 this->glslGeneration() >= k150_GrGLSLGeneration;
275 } else {
276 fCaps.fShaderDerivativeSupport =
277 this->hasExtension("GL_OES_standard_derivatives");
278 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000279}
280
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000281void GrGpuGL::fillInConfigRenderableTable() {
282
283 // OpenGL < 3.0
284 // no support for render targets unless the GL_ARB_framebuffer_object
285 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
286 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
287 // probably don't get R8 in this case.
288
289 // OpenGL 3.0
290 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
291 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
292
293 // >= OpenGL 3.1
294 // base color renderable: RED, RG, RGB, and RGBA
295 // sized derivatives: R8, RGBA4, RGBA8
296 // if the GL_ARB_compatibility extension is supported then we get back
297 // support for GL_ALPHA and ALPHA8
298
299 // GL_EXT_bgra adds BGRA render targets to any version
300
301 // ES 2.0
302 // color renderable: RGBA4, RGB5_A1, RGB565
303 // GL_EXT_texture_rg adds support for R8 as a color render target
304 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
305 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
306 // added BGRA support
307
308 if (kDesktop_GrGLBinding == this->glBinding()) {
309 // Post 3.0 we will get R8
310 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
311 if (this->glVersion() >= GR_GL_VER(3,0) ||
312 this->hasExtension("GL_ARB_framebuffer_object")) {
313 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
314 }
315 } else {
316 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000317 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
318 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000319 }
320
321 if (kDesktop_GrGLBinding != this->glBinding()) {
322 // only available in ES
323 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
324 }
325
326 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
327 // GL_EXT_framebuffer_object for FBO support. Both of these
328 // allow RGBA4 render targets so this is always supported.
329 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
330
331 if (this->glCaps().rgba8RenderbufferSupport()) {
332 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
333 }
334
335 if (this->glCaps().bgraFormatSupport()) {
336 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
337 }
338
339 // the un-premultiplied formats just inherit the premultiplied setting
340 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
341 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
342 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
343 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
344}
345
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000346bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
347 if (kUnknown_CanPreserveUnpremulRoundtrip ==
348 fCanPreserveUnpremulRoundtrip) {
349
350 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
351 uint32_t* srcData = data.get();
352 uint32_t* firstRead = data.get() + 256 * 256;
353 uint32_t* secondRead = data.get() + 2 * 256 * 256;
354
355 for (int y = 0; y < 256; ++y) {
356 for (int x = 0; x < 256; ++x) {
357 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
358 color[3] = y;
359 color[2] = x;
360 color[1] = x;
361 color[0] = x;
362 }
363 }
364
365 // We have broader support for read/write pixels on render targets
366 // than on textures.
367 GrTextureDesc dstDesc;
368 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
369 kNoStencil_GrTextureFlagBit;
370 dstDesc.fWidth = 256;
371 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000372 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000373 dstDesc.fSampleCnt = 0;
374
375 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
376 if (!dstTex.get()) {
377 return false;
378 }
379 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
380 GrAssert(NULL != rt);
381
382 bool failed = true;
383 static const UnpremulConversion gMethods[] = {
384 kUpOnWrite_DownOnRead_UnpremulConversion,
385 kDownOnWrite_UpOnRead_UnpremulConversion,
386 };
387
388 // pretend that we can do the roundtrip to avoid recursive calls to
389 // this function
390 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
391 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
392 fUnpremulConversion = gMethods[i];
393 rt->writePixels(0, 0,
394 256, 256,
395 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
396 rt->readPixels(0, 0,
397 256, 256,
398 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
399 rt->writePixels(0, 0,
400 256, 256,
401 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
402 rt->readPixels(0, 0,
403 256, 256,
404 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
405 failed = false;
406 for (int j = 0; j < 256 * 256; ++j) {
407 if (firstRead[j] != secondRead[j]) {
408 failed = true;
409 break;
410 }
411 }
412 }
413 fCanPreserveUnpremulRoundtrip = failed ?
414 kNo_CanPreserveUnpremulRoundtrip :
415 kYes_CanPreserveUnpremulRoundtrip;
416 }
417
418 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
419 return true;
420 } else {
421 return false;
422 }
423}
424
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000425GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000426 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
427 return GrPixelConfigSwapRAndB(config);
428 } else {
429 return config;
430 }
431}
432
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000433GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000434 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000435 return GrPixelConfigSwapRAndB(config);
436 } else {
437 return config;
438 }
439}
440
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000441bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
442 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
443}
444
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000445void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000446 if (gPrintStartupSpew && !fPrintedCaps) {
447 fPrintedCaps = true;
448 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000449 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000450 }
451
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000452 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000453 GL_CALL(Disable(GR_GL_DEPTH_TEST));
454 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000455
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000456 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
457 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000458
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000459 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000460 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000461 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000462 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
463 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
464 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
465 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
466 GL_CALL(Disable(GR_GL_COLOR_TABLE));
467 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
468 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
469 // Since ES doesn't support glPointSize at all we always use the VS to
470 // set the point size
471 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
472
473 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
474 // currently part of our gl interface. There are probably others as
475 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000476 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000477 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000478 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000479
reed@google.comac10a2d2010-12-22 21:39:39 +0000480 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000481 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000482
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000483 // invalid
484 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000486 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000487
tomhudson@google.com93813632011-10-27 20:21:16 +0000488 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000489 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000490 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000491
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000492 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000493 // set to true to force disableScissor to make a GL call.
494 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000495 this->disableScissor();
496
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000497 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000498
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000499 fHWStencilSettings.invalidate();
500 fHWStencilClipMode = kInvalid_StencilClipMode;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000501
502 // TODO: I believe this should actually go in GrGpu::onResetContext
503 // rather than here
504 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000505
506 fHWGeometryState.fIndexBuffer = NULL;
507 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000508
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000509 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000510
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000511 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000512
513 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000514 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000515 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
516 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000517 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000518 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
519 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000520 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000521 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
522 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000523 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000524 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
525 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000526
527 fHWGeometryState.fVertexOffset = ~0;
528
529 // Third party GL code may have left vertex attributes enabled. Some GL
530 // implementations (osmesa) may read vetex attributes that are not required
531 // by the current shader. Therefore, we have to ensure that only the
532 // attributes we require for the current draw are enabled or we may cause an
533 // invalid read.
534
535 // Disable all vertex layout bits so that next flush will assume all
536 // optional vertex attributes are disabled.
537 fHWGeometryState.fVertexLayout = 0;
538
539 // We always use the this attribute and assume it is always enabled.
540 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
541 GL_CALL(EnableVertexAttribArray(posAttrIdx));
542 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000543 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000544 if (va != posAttrIdx) {
545 GL_CALL(DisableVertexAttribArray(va));
546 }
547 }
548
549 fHWProgramID = 0;
550 fHWConstAttribColor = GrColor_ILLEGAL;
551 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000552}
553
bsalomon@google.come269f212011-11-07 13:29:52 +0000554GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000555 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000556 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000557 return NULL;
558 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000559
bsalomon@google.com99621082011-11-15 16:47:16 +0000560 glTexDesc.fWidth = desc.fWidth;
561 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000562 glTexDesc.fConfig = desc.fConfig;
563 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
564 glTexDesc.fOwnsID = false;
565 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
566
567 GrGLTexture* texture = NULL;
568 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
569 GrGLRenderTarget::Desc glRTDesc;
570 glRTDesc.fRTFBOID = 0;
571 glRTDesc.fTexFBOID = 0;
572 glRTDesc.fMSColorRenderbufferID = 0;
573 glRTDesc.fOwnIDs = true;
574 glRTDesc.fConfig = desc.fConfig;
575 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000576 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
577 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000578 glTexDesc.fTextureID,
579 &glRTDesc)) {
580 return NULL;
581 }
582 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
583 } else {
584 texture = new GrGLTexture(this, glTexDesc);
585 }
586 if (NULL == texture) {
587 return NULL;
588 }
589
590 this->setSpareTextureUnit();
591 return texture;
592}
593
594GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
595 GrGLRenderTarget::Desc glDesc;
596 glDesc.fConfig = desc.fConfig;
597 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
598 glDesc.fMSColorRenderbufferID = 0;
599 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
600 glDesc.fSampleCnt = desc.fSampleCnt;
601 glDesc.fOwnIDs = false;
602 GrGLIRect viewport;
603 viewport.fLeft = 0;
604 viewport.fBottom = 0;
605 viewport.fWidth = desc.fWidth;
606 viewport.fHeight = desc.fHeight;
607
608 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
609 if (desc.fStencilBits) {
610 GrGLStencilBuffer::Format format;
611 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
612 format.fPacked = false;
613 format.fStencilBits = desc.fStencilBits;
614 format.fTotalBits = desc.fStencilBits;
615 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
616 0,
617 desc.fWidth,
618 desc.fHeight,
619 desc.fSampleCnt,
620 format);
621 tgt->setStencilBuffer(sb);
622 sb->unref();
623 }
624 return tgt;
625}
626
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000627////////////////////////////////////////////////////////////////////////////////
628
bsalomon@google.com6f379512011-11-16 20:36:03 +0000629void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
630 int left, int top, int width, int height,
631 GrPixelConfig config, const void* buffer,
632 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000633 if (NULL == buffer) {
634 return;
635 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000636 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
637
bsalomon@google.com6f379512011-11-16 20:36:03 +0000638 this->setSpareTextureUnit();
639 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
640 GrGLTexture::Desc desc;
641 desc.fConfig = glTex->config();
642 desc.fWidth = glTex->width();
643 desc.fHeight = glTex->height();
644 desc.fOrientation = glTex->orientation();
645 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000646
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000647 this->uploadTexData(desc, false,
648 left, top, width, height,
649 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000650}
651
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000652namespace {
653bool adjust_pixel_ops_params(int surfaceWidth,
654 int surfaceHeight,
655 size_t bpp,
656 int* left, int* top, int* width, int* height,
657 const void** data,
658 size_t* rowBytes) {
659 if (!*rowBytes) {
660 *rowBytes = *width * bpp;
661 }
662
663 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
664 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
665
666 if (!subRect.intersect(bounds)) {
667 return false;
668 }
669 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
670 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
671
672 *left = subRect.fLeft;
673 *top = subRect.fTop;
674 *width = subRect.width();
675 *height = subRect.height();
676 return true;
677}
678}
679
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000680bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
681 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000682 int left, int top, int width, int height,
683 GrPixelConfig dataConfig,
684 const void* data,
685 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000686 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000687
688 size_t bpp = GrBytesPerPixel(dataConfig);
689 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
690 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000691 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000692 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000693 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000694
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000695 // in case we need a temporary, trimmed copy of the src pixels
696 SkAutoSMalloc<128 * 128> tempStorage;
697
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000698 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000699 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000700 if (useTexStorage) {
701 if (kDesktop_GrGLBinding == this->glBinding()) {
702 // 565 is not a sized internal format on desktop GL. So on desktop
703 // with 565 we always use an unsized internal format to let the
704 // system pick the best sized format to convert the 565 data to.
705 // Since glTexStorage only allows sized internal formats we will
706 // instead fallback to glTexImage2D.
707 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
708 } else {
709 // ES doesn't allow paletted textures to be used with tex storage
710 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
711 }
712 }
713
714 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000715 GrGLenum externalFormat;
716 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000717 // glTexStorage requires sized internal formats on both desktop and ES. ES
718 // glTexImage requires an unsized format.
719 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
720 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000721 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000722 }
723
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000724 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000725 // paletted textures cannot be updated
726 return false;
727 }
728
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000729 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000730 * check whether to allocate a temporary buffer for flipping y or
731 * because our srcData has extra bytes past each row. If so, we need
732 * to trim those off here, since GL ES may not let us specify
733 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000734 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000735 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000736 bool swFlipY = false;
737 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000738 if (NULL != data) {
739 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000740 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000741 glFlipY = true;
742 } else {
743 swFlipY = true;
744 }
745 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000746 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000747 // can't use this for flipping, only non-neg values allowed. :(
748 if (rowBytes != trimRowBytes) {
749 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
750 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
751 restoreGLRowLength = true;
752 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000753 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000754 if (trimRowBytes != rowBytes || swFlipY) {
755 // copy data into our new storage, skipping the trailing bytes
756 size_t trimSize = height * trimRowBytes;
757 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000758 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000759 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000760 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000761 char* dst = (char*)tempStorage.reset(trimSize);
762 for (int y = 0; y < height; y++) {
763 memcpy(dst, src, trimRowBytes);
764 if (swFlipY) {
765 src -= rowBytes;
766 } else {
767 src += rowBytes;
768 }
769 dst += trimRowBytes;
770 }
771 // now point data to our copied version
772 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000773 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000774 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000775 if (glFlipY) {
776 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
777 }
778 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000779 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000780 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000781 if (isNewTexture &&
782 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000783 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000784 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000785 if (useTexStorage) {
786 // We never resize or change formats of textures. We don't use
787 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000788 GL_ALLOC_CALL(this->glInterface(),
789 TexStorage2D(GR_GL_TEXTURE_2D,
790 1, // levels
791 internalFormat,
792 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000793 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000794 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
795 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
796 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000797 GL_ALLOC_CALL(this->glInterface(),
798 CompressedTexImage2D(GR_GL_TEXTURE_2D,
799 0, // level
800 internalFormat,
801 desc.fWidth, desc.fHeight,
802 0, // border
803 imageSize,
804 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000805 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000806 GL_ALLOC_CALL(this->glInterface(),
807 TexImage2D(GR_GL_TEXTURE_2D,
808 0, // level
809 internalFormat,
810 desc.fWidth, desc.fHeight,
811 0, // border
812 externalFormat, externalType,
813 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000814 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000815 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000816 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000817 if (error != GR_GL_NO_ERROR) {
818 succeeded = false;
819 } else {
820 // if we have data and we used TexStorage to create the texture, we
821 // now upload with TexSubImage.
822 if (NULL != data && useTexStorage) {
823 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
824 0, // level
825 left, top,
826 width, height,
827 externalFormat, externalType,
828 data));
829 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000830 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000831 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000832 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000833 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000834 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000835 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
836 0, // level
837 left, top,
838 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000839 externalFormat, externalType, data));
840 }
841
842 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000843 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000844 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000845 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000846 if (glFlipY) {
847 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
848 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000849 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000850}
851
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000852namespace {
853bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
854 int sampleCount,
855 GrGLenum format,
856 int width, int height) {
857 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
858 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
859 bool created = false;
860 if (GrGLCaps::kNVDesktop_CoverageAAType ==
861 ctxInfo.caps().coverageAAType()) {
862 const GrGLCaps::MSAACoverageMode& mode =
863 ctxInfo.caps().getMSAACoverageMode(sampleCount);
864 GL_ALLOC_CALL(ctxInfo.interface(),
865 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
866 mode.fCoverageSampleCnt,
867 mode.fColorSampleCnt,
868 format,
869 width, height));
870 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
871 }
872 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000873 // glRBMS will fail if requested samples is > max samples.
874 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000875 GL_ALLOC_CALL(ctxInfo.interface(),
876 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
877 sampleCount,
878 format,
879 width, height));
880 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
881 }
882 return created;
883}
884}
885
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000886bool GrGpuGL::createRenderTargetObjects(int width, int height,
887 GrGLuint texID,
888 GrGLRenderTarget::Desc* desc) {
889 desc->fMSColorRenderbufferID = 0;
890 desc->fRTFBOID = 0;
891 desc->fTexFBOID = 0;
892 desc->fOwnIDs = true;
893
894 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000895
bsalomon@google.comab15d612011-08-09 12:57:56 +0000896 GrGLenum msColorFormat = 0; // suppress warning
897
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000898 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 if (!desc->fTexFBOID) {
900 goto FAILED;
901 }
902
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000903
904 // If we are using multisampling we will create two FBOS. We render
905 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000906 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000907 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000908 goto FAILED;
909 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000910 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
911 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 if (!desc->fRTFBOID ||
913 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000914 !this->configToGLFormats(desc->fConfig,
915 // GLES requires sized internal formats
916 kES2_GrGLBinding == this->glBinding(),
917 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000918 goto FAILED;
919 }
920 } else {
921 desc->fRTFBOID = desc->fTexFBOID;
922 }
923
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000924 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000925 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000926 if (desc->fRTFBOID != desc->fTexFBOID) {
927 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000928 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000930 if (!renderbuffer_storage_msaa(fGLContextInfo,
931 desc->fSampleCnt,
932 msColorFormat,
933 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000934 goto FAILED;
935 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
937 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000938 GR_GL_COLOR_ATTACHMENT0,
939 GR_GL_RENDERBUFFER,
940 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000941 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000942 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
943 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
944 goto FAILED;
945 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000946 fGLContextInfo.caps().markConfigAsValidColorAttachment(
947 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948 }
949 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000950 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000951
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000952 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
953 GR_GL_COLOR_ATTACHMENT0,
954 GR_GL_TEXTURE_2D,
955 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000956 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000957 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
958 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
959 goto FAILED;
960 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000961 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000962 }
963
964 return true;
965
966FAILED:
967 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000968 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000969 }
970 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000971 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000972 }
973 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000974 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000975 }
976 return false;
977}
978
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000979// good to set a break-point here to know when createTexture fails
980static GrTexture* return_null_texture() {
981// GrAssert(!"null texture");
982 return NULL;
983}
984
985#if GR_DEBUG
986static size_t as_size_t(int x) {
987 return x;
988}
989#endif
990
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000991GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000992 const void* srcData,
993 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000994
995#if GR_COLLECT_STATS
996 ++fStats.fTextureCreateCnt;
997#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000998
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000999 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001000 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001001
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001002 // Attempt to catch un- or wrongly initialized sample counts;
1003 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1004
bsalomon@google.com99621082011-11-15 16:47:16 +00001005 glTexDesc.fWidth = desc.fWidth;
1006 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001007 glTexDesc.fConfig = desc.fConfig;
1008 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001009
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001010 glRTDesc.fMSColorRenderbufferID = 0;
1011 glRTDesc.fRTFBOID = 0;
1012 glRTDesc.fTexFBOID = 0;
1013 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001014 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001015
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001016 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001017
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001018 const Caps& caps = this->getCaps();
1019
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001020 // We keep GrRenderTargets in GL's normal orientation so that they
1021 // can be drawn to by the outside world without the client having
1022 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001023 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001024 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001025
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001026 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001027 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001028 desc.fSampleCnt) {
1029 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001030 }
1031
reed@google.comac10a2d2010-12-22 21:39:39 +00001032 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001033 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1034 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001035 return return_null_texture();
1036 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001037 }
1038
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001039 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001040 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001041 // provides a hint about how this texture will be used
1042 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1043 GR_GL_TEXTURE_USAGE,
1044 GR_GL_FRAMEBUFFER_ATTACHMENT));
1045 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001046 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001047 return return_null_texture();
1048 }
1049
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001050 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001051 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001052
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001053 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1054 // drivers have a bug where an FBO won't be complete if it includes a
1055 // texture that is not mipmap complete (considering the filter in use).
1056 GrGLTexture::TexParams initialTexParams;
1057 // we only set a subset here so invalidate first
1058 initialTexParams.invalidate();
1059 initialTexParams.fFilter = GR_GL_NEAREST;
1060 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1061 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001062 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1063 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001064 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001065 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1066 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001067 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001068 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1069 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001070 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001071 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1072 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001073 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001074 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1075 glTexDesc.fWidth, glTexDesc.fHeight,
1076 desc.fConfig, srcData, rowBytes)) {
1077 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1078 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001079 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001080
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001081 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001082 if (renderTarget) {
1083#if GR_COLLECT_STATS
1084 ++fStats.fRenderTargetCreateCnt;
1085#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001086 // unbind the texture from the texture unit before binding it to the frame buffer
1087 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1088
bsalomon@google.com99621082011-11-15 16:47:16 +00001089 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1090 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 glTexDesc.fTextureID,
1092 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001093 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 return return_null_texture();
1095 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001096 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001098 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001099 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001100 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001101#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001102 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1103 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001104#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 return tex;
1106}
1107
1108namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001109
1110const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1111
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001112void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1113 GrGLuint rb,
1114 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 // we shouldn't ever know one size and not the other
1116 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1117 (kUnknownBitCount == format->fTotalBits));
1118 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001119 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001120 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1121 (GrGLint*)&format->fStencilBits);
1122 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001123 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001124 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1125 (GrGLint*)&format->fTotalBits);
1126 format->fTotalBits += format->fStencilBits;
1127 } else {
1128 format->fTotalBits = format->fStencilBits;
1129 }
1130 }
1131}
1132}
1133
1134bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1135 int width, int height) {
1136
1137 // All internally created RTs are also textures. We don't create
1138 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1139 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001140 GrAssert(width >= rt->width());
1141 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142
1143 int samples = rt->numSamples();
1144 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001145 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 if (!sbID) {
1147 return false;
1148 }
1149
1150 GrGLStencilBuffer* sb = NULL;
1151
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001152 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001153 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001154 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 // we start with the last stencil format that succeeded in hopes
1156 // that we won't go through this loop more than once after the
1157 // first (painful) stencil creation.
1158 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001159 const GrGLCaps::StencilFormat& sFmt =
1160 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001161 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001162 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001164 bool created;
1165 if (samples > 0) {
1166 created = renderbuffer_storage_msaa(fGLContextInfo,
1167 samples,
1168 sFmt.fInternalFormat,
1169 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001171 GL_ALLOC_CALL(this->glInterface(),
1172 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001173 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001174 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001175 created =
1176 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001178 if (created) {
1179 // After sized formats we attempt an unsized format and take
1180 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001181 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001182 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001183 sb = new GrGLStencilBuffer(this, sbID, width, height,
1184 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1186 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001187 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 return true;
1190 }
1191 sb->abandon(); // otherwise we lose sbID
1192 sb->unref();
1193 }
1194 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001195 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001196 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197}
1198
1199bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1200 GrRenderTarget* rt) {
1201 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1202
1203 GrGLuint fbo = glrt->renderFBOID();
1204
1205 if (NULL == sb) {
1206 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001207 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 GR_GL_STENCIL_ATTACHMENT,
1209 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001210 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 GR_GL_DEPTH_ATTACHMENT,
1212 GR_GL_RENDERBUFFER, 0));
1213#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001214 GrGLenum status;
1215 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1217#endif
1218 }
1219 return true;
1220 } else {
1221 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1222 GrGLuint rb = glsb->renderbufferID();
1223
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001224 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1226 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 GR_GL_STENCIL_ATTACHMENT,
1228 GR_GL_RENDERBUFFER, rb));
1229 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001230 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231 GR_GL_DEPTH_ATTACHMENT,
1232 GR_GL_RENDERBUFFER, rb));
1233 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 GR_GL_DEPTH_ATTACHMENT,
1236 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001237 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001239 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001240 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001241 glsb->format())) {
1242 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1243 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001244 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001245 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001246 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001247 if (glsb->format().fPacked) {
1248 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1249 GR_GL_DEPTH_ATTACHMENT,
1250 GR_GL_RENDERBUFFER, 0));
1251 }
1252 return false;
1253 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001254 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001255 rt->config(),
1256 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001257 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001258 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001259 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001261}
1262
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001263////////////////////////////////////////////////////////////////////////////////
1264
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001265GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001266 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001267 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001269 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001270 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001271 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001273 GL_ALLOC_CALL(this->glInterface(),
1274 BufferData(GR_GL_ARRAY_BUFFER,
1275 size,
1276 NULL, // data ptr
1277 dynamic ? GR_GL_DYNAMIC_DRAW :
1278 GR_GL_STATIC_DRAW));
1279 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001280 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 // deleting bound buffer does implicit bind to 0
1282 fHWGeometryState.fVertexBuffer = NULL;
1283 return NULL;
1284 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001285 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 size, dynamic);
1287 fHWGeometryState.fVertexBuffer = vertexBuffer;
1288 return vertexBuffer;
1289 }
1290 return NULL;
1291}
1292
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001293GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001294 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001295 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001297 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001298 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001300 GL_ALLOC_CALL(this->glInterface(),
1301 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1302 size,
1303 NULL, // data ptr
1304 dynamic ? GR_GL_DYNAMIC_DRAW :
1305 GR_GL_STATIC_DRAW));
1306 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001307 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001308 // deleting bound buffer does implicit bind to 0
1309 fHWGeometryState.fIndexBuffer = NULL;
1310 return NULL;
1311 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001312 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 size, dynamic);
1314 fHWGeometryState.fIndexBuffer = indexBuffer;
1315 return indexBuffer;
1316 }
1317 return NULL;
1318}
1319
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001320void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001321 const GrDrawState& drawState = this->getDrawState();
1322 const GrGLRenderTarget* rt =
1323 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1324
1325 GrAssert(NULL != rt);
1326 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001327
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001328 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001329 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1330 rect.width(), rect.height());
1331 if (scissor.contains(vp)) {
1332 disableScissor();
1333 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 }
1335
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001336 if (fHWBounds.fScissorRect != scissor) {
1337 scissor.pushToGLScissor(this->glInterface());
1338 fHWBounds.fScissorRect = scissor;
1339 }
1340 if (!fHWBounds.fScissorEnabled) {
1341 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1342 fHWBounds.fScissorEnabled = true;
1343 }
1344}
1345
1346void GrGpuGL::disableScissor() {
1347 if (fHWBounds.fScissorEnabled) {
1348 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1349 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 }
1351}
1352
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001353void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001354 const GrDrawState& drawState = this->getDrawState();
1355 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001356 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001357 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001358
bsalomon@google.com74b98712011-11-11 19:46:16 +00001359 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001360 if (NULL != rect) {
1361 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001362 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001363 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001364 if (clippedRect.intersect(rtRect)) {
1365 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001366 } else {
1367 return;
1368 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001369 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001370 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001371 if (NULL != rect)
1372 this->enableScissoring(*rect);
1373 else
1374 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001375
1376 GrGLfloat r, g, b, a;
1377 static const GrGLfloat scale255 = 1.f / 255.f;
1378 a = GrColorUnpackA(color) * scale255;
1379 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001380 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001381 scaleRGB *= a;
1382 }
1383 r = GrColorUnpackR(color) * scaleRGB;
1384 g = GrColorUnpackG(color) * scaleRGB;
1385 b = GrColorUnpackB(color) * scaleRGB;
1386
1387 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001388 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001389 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001390 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001391}
1392
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001393void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001394 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001395 return;
1396 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001397
1398 this->flushRenderTarget(&GrIRect::EmptyIRect());
1399
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001400 this->disableScissor();
1401
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001402 GL_CALL(StencilMask(0xffffffff));
1403 GL_CALL(ClearStencil(0));
1404 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001405 fHWStencilSettings.invalidate();
1406 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001407}
1408
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001409void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001410 const GrDrawState& drawState = this->getDrawState();
1411 const GrRenderTarget* rt = drawState.getRenderTarget();
1412 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001413
1414 // this should only be called internally when we know we have a
1415 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001416 GrAssert(NULL != rt->getStencilBuffer());
1417 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001418#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001419 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001420 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001421#else
1422 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001423 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001424 // turned into draws. Our contract on GrDrawTarget says that
1425 // changing the clip between stencil passes may or may not
1426 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001427 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001428#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001429 GrGLint value;
1430 if (insideClip) {
1431 value = (1 << (stencilBitCount - 1));
1432 } else {
1433 value = 0;
1434 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001435 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001436 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001437 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001438 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001439 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001440 fHWStencilSettings.invalidate();
1441 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001442}
1443
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001444void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001445 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001446}
1447
bsalomon@google.comc4364992011-11-07 15:54:49 +00001448bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1449 int left, int top,
1450 int width, int height,
1451 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001452 size_t rowBytes) const {
1453 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001454 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001455 return false;
1456 }
1457
1458 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001459 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001460 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001461 return true;
1462 }
1463 // If we have to do memcpys to handle rowBytes then y-flip is free
1464 // Note the rowBytes might be tight to the passed in data, but if data
1465 // gets clipped in x to the target the rowBytes will no longer be tight.
1466 if (left >= 0 && (left + width) < renderTarget->width()) {
1467 return 0 == rowBytes ||
1468 GrBytesPerPixel(config) * width == rowBytes;
1469 } else {
1470 return false;
1471 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001472}
1473
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001474bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001475 int left, int top,
1476 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001477 GrPixelConfig config,
1478 void* buffer,
1479 size_t rowBytes,
1480 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001481 GrGLenum format;
1482 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001483 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001484 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001485 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001486 size_t bpp = GrBytesPerPixel(config);
1487 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1488 &left, &top, &width, &height,
1489 const_cast<const void**>(&buffer),
1490 &rowBytes)) {
1491 return false;
1492 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001493
bsalomon@google.comc6980972011-11-02 19:57:21 +00001494 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001495 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001496 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001497 switch (tgt->getResolveType()) {
1498 case GrGLRenderTarget::kCantResolve_ResolveType:
1499 return false;
1500 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001501 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001502 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001503 break;
1504 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001505 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001506 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001507 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1508 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001509 break;
1510 default:
1511 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001512 }
1513
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001514 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001515
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001516 // the read rect is viewport-relative
1517 GrGLIRect readRect;
1518 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001519
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001520 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001521 if (0 == rowBytes) {
1522 rowBytes = tightRowBytes;
1523 }
1524 size_t readDstRowBytes = tightRowBytes;
1525 void* readDst = buffer;
1526
1527 // determine if GL can read using the passed rowBytes or if we need
1528 // a scratch buffer.
1529 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1530 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001531 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001532 GrAssert(!(rowBytes % sizeof(GrColor)));
1533 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1534 readDstRowBytes = rowBytes;
1535 } else {
1536 scratch.reset(tightRowBytes * height);
1537 readDst = scratch.get();
1538 }
1539 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001540 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001541 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1542 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001543 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1544 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001545 format, type, readDst));
1546 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001547 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001548 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1549 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001550 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001551 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1552 invertY = true;
1553 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001554
1555 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001556 // API presents top-to-bottom. We must preserve the padding contents. Note
1557 // that the above readPixels did not overwrite the padding.
1558 if (readDst == buffer) {
1559 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001560 if (!invertY) {
1561 scratch.reset(tightRowBytes);
1562 void* tmpRow = scratch.get();
1563 // flip y in-place by rows
1564 const int halfY = height >> 1;
1565 char* top = reinterpret_cast<char*>(buffer);
1566 char* bottom = top + (height - 1) * rowBytes;
1567 for (int y = 0; y < halfY; y++) {
1568 memcpy(tmpRow, top, tightRowBytes);
1569 memcpy(top, bottom, tightRowBytes);
1570 memcpy(bottom, tmpRow, tightRowBytes);
1571 top += rowBytes;
1572 bottom -= rowBytes;
1573 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001574 }
1575 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001576 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001577 // copy from readDst to buffer while flipping y
1578 const int halfY = height >> 1;
1579 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001580 char* dst = reinterpret_cast<char*>(buffer);
1581 if (!invertY) {
1582 dst += (height-1) * rowBytes;
1583 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001584 for (int y = 0; y < height; y++) {
1585 memcpy(dst, src, tightRowBytes);
1586 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001587 if (invertY) {
1588 dst += rowBytes;
1589 } else {
1590 dst -= rowBytes;
1591 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001592 }
1593 }
1594 return true;
1595}
1596
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001597void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001598
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001599 GrGLRenderTarget* rt =
1600 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1601 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001602
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001603 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001604 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001605 #if GR_COLLECT_STATS
1606 ++fStats.fRenderTargetChngCnt;
1607 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001608 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001609 GrGLenum status;
1610 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001611 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001612 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 }
1614 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001615 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001616 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001617 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001618 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001619 fHWBounds.fViewportRect = vp;
bsalomon@google.com890e3b52012-06-01 19:01:37 +00001620 // View matrices pushed to GL depend upon the viewport. So they
1621 // are now invalid.
1622 fProgramCache->invalidateViewMatrices();
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 }
1624 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001625 if (NULL == bound || !bound->isEmpty()) {
1626 rt->flagAsNeedingResolve(bound);
1627 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001628}
1629
twiz@google.com0f31ca72011-03-18 17:38:11 +00001630GrGLenum gPrimitiveType2GLMode[] = {
1631 GR_GL_TRIANGLES,
1632 GR_GL_TRIANGLE_STRIP,
1633 GR_GL_TRIANGLE_FAN,
1634 GR_GL_POINTS,
1635 GR_GL_LINES,
1636 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001637};
1638
bsalomon@google.comd302f142011-03-03 13:54:13 +00001639#define SWAP_PER_DRAW 0
1640
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001641#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001642 #if GR_MAC_BUILD
1643 #include <AGL/agl.h>
1644 #elif GR_WIN32_BUILD
1645 void SwapBuf() {
1646 DWORD procID = GetCurrentProcessId();
1647 HWND hwnd = GetTopWindow(GetDesktopWindow());
1648 while(hwnd) {
1649 DWORD wndProcID = 0;
1650 GetWindowThreadProcessId(hwnd, &wndProcID);
1651 if(wndProcID == procID) {
1652 SwapBuffers(GetDC(hwnd));
1653 }
1654 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1655 }
1656 }
1657 #endif
1658#endif
1659
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001660void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1661 uint32_t startVertex,
1662 uint32_t startIndex,
1663 uint32_t vertexCount,
1664 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001665 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1666
twiz@google.com0f31ca72011-03-18 17:38:11 +00001667 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001668
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001669 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1670 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1671
1672 // our setupGeometry better have adjusted this to zero since
1673 // DrawElements always draws from the begining of the arrays for idx 0.
1674 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001675
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001676 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1677 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001678#if SWAP_PER_DRAW
1679 glFlush();
1680 #if GR_MAC_BUILD
1681 aglSwapBuffers(aglGetCurrentContext());
1682 int set_a_break_pt_here = 9;
1683 aglSwapBuffers(aglGetCurrentContext());
1684 #elif GR_WIN32_BUILD
1685 SwapBuf();
1686 int set_a_break_pt_here = 9;
1687 SwapBuf();
1688 #endif
1689#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001690}
1691
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001692void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1693 uint32_t startVertex,
1694 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001695 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1696
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001697 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1698
1699 // our setupGeometry better have adjusted this to zero.
1700 // DrawElements doesn't take an offset so we always adjus the startVertex.
1701 GrAssert(0 == startVertex);
1702
1703 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1704 // account for startVertex in the DrawElements case. So we always
1705 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001706 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001707#if SWAP_PER_DRAW
1708 glFlush();
1709 #if GR_MAC_BUILD
1710 aglSwapBuffers(aglGetCurrentContext());
1711 int set_a_break_pt_here = 9;
1712 aglSwapBuffers(aglGetCurrentContext());
1713 #elif GR_WIN32_BUILD
1714 SwapBuf();
1715 int set_a_break_pt_here = 9;
1716 SwapBuf();
1717 #endif
1718#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001719}
1720
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001721void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1722
1723 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001724
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001725 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001726 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001727 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001728 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1729 rt->renderFBOID()));
1730 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1731 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001732 #if GR_COLLECT_STATS
1733 ++fStats.fRenderTargetChngCnt;
1734 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001735 // make sure we go through flushRenderTarget() since we've modified
1736 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001737 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001738 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001739 const GrIRect dirtyRect = rt->getResolveRect();
1740 GrGLIRect r;
1741 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1742 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001743
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001744 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001745 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001746#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001747 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1748 GL_CALL(Scissor(r.fLeft, r.fBottom,
1749 r.fWidth, r.fHeight));
1750 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001751 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001753#else
1754 this->enableScissoring(dirtyRect);
1755 GL_CALL(ResolveMultisampleFramebuffer());
1756#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001758 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001759 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001760 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1761 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001762 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001763 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001764 int right = r.fLeft + r.fWidth;
1765 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001766 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1767 r.fLeft, r.fBottom, right, top,
1768 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001770 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001771 }
1772}
1773
twiz@google.com0f31ca72011-03-18 17:38:11 +00001774static const GrGLenum grToGLStencilFunc[] = {
1775 GR_GL_ALWAYS, // kAlways_StencilFunc
1776 GR_GL_NEVER, // kNever_StencilFunc
1777 GR_GL_GREATER, // kGreater_StencilFunc
1778 GR_GL_GEQUAL, // kGEqual_StencilFunc
1779 GR_GL_LESS, // kLess_StencilFunc
1780 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1781 GR_GL_EQUAL, // kEqual_StencilFunc,
1782 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001783};
1784GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1785GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1786GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1787GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1788GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1789GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1790GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1791GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1792GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1793
twiz@google.com0f31ca72011-03-18 17:38:11 +00001794static const GrGLenum grToGLStencilOp[] = {
1795 GR_GL_KEEP, // kKeep_StencilOp
1796 GR_GL_REPLACE, // kReplace_StencilOp
1797 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1798 GR_GL_INCR, // kIncClamp_StencilOp
1799 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1800 GR_GL_DECR, // kDecClamp_StencilOp
1801 GR_GL_ZERO, // kZero_StencilOp
1802 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803};
1804GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1805GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1806GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1807GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1808GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1809GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1810GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1811GR_STATIC_ASSERT(6 == kZero_StencilOp);
1812GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1813
reed@google.comac10a2d2010-12-22 21:39:39 +00001814void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001815 const GrDrawState& drawState = this->getDrawState();
1816
1817 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001818
1819 // use stencil for clipping if clipping is enabled and the clip
1820 // has been written into the stencil.
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001821 StencilClipMode clipMode;
1822 if (fClipMaskManager.isClipInStencil() &&
1823 drawState.isClipState()) {
1824 clipMode = kUseClip_StencilClipMode;
1825 // We can't be modifying the clip and respecting it at the same time.
1826 GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1827 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
1828 clipMode = kModifyClip_StencilClipMode;
1829 } else {
1830 clipMode = kIgnoreClip_StencilClipMode;
1831 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001832
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001833 bool stencilChange = (fHWStencilSettings != *settings) ||
1834 (fHWStencilClipMode != clipMode);
reed@google.comac10a2d2010-12-22 21:39:39 +00001835 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001836
bsalomon@google.comd302f142011-03-03 13:54:13 +00001837 if (settings->isDisabled()) {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001838 if (kUseClip_StencilClipMode == clipMode) {
digit@google.com9b482c42012-02-16 22:03:26 +00001839 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001840 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001841 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001842
1843 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001844 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001845 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001846 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001847 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001848 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001849 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1850 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1851 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1852 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1853 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1854 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1855 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1856 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001857 }
1858 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001859 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001860 GrStencilBuffer* stencilBuffer =
1861 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001862 if (NULL != stencilBuffer) {
1863 stencilBits = stencilBuffer->bits();
1864 }
1865 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001866 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001867
1868 GrGLuint clipStencilMask = 0;
1869 GrGLuint userStencilMask = ~0;
1870 if (stencilBits > 0) {
1871 clipStencilMask = 1 << (stencilBits - 1);
1872 userStencilMask = clipStencilMask - 1;
1873 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001874
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001875 unsigned int frontRef = settings->frontFuncRef();
1876 unsigned int frontMask = settings->frontFuncMask();
1877 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001878 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001879
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001880 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001881 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1882 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001884 bool useClip = kUseClip_StencilClipMode == clipMode;
1885 frontFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1886 settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001888 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001889 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001890 clipStencilMask,
1891 userStencilMask,
1892 &frontRef,
1893 &frontMask);
1894 frontWriteMask &= userStencilMask;
1895 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001896 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001897 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001898 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001899 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001900 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001901 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001902 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001903 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001904 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001905 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001906
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001907 unsigned int backRef = settings->backFuncRef();
1908 unsigned int backMask = settings->backFuncMask();
1909 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001910
1911
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001912 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001913 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1914 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001915 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001916 bool useClip = kUseClip_StencilClipMode == clipMode;
1917 backFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1918 settings->backFunc())];
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001919 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001920 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001921 clipStencilMask,
1922 userStencilMask,
1923 &backRef,
1924 &backMask);
1925 backWriteMask &= userStencilMask;
1926 }
1927
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001928 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1929 frontRef, frontMask));
1930 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1931 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1932 backRef, backMask));
1933 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1934 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001935 grToGLStencilOp[settings->frontFailOp()],
1936 grToGLStencilOp[settings->frontPassOp()],
1937 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001938
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001939 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001940 grToGLStencilOp[settings->backFailOp()],
1941 grToGLStencilOp[settings->backPassOp()],
1942 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001943 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001944 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1945 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001946 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1947 grToGLStencilOp[settings->frontPassOp()],
1948 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001949 }
1950 }
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001951 fHWStencilSettings = *settings;
1952 fHWStencilClipMode = clipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001953 }
1954}
1955
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001957 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1960 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001961 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001962 bool smoothLines = false;
1963
bsalomon@google.com0650e812011-04-08 18:07:53 +00001964 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001965 smoothLines = this->willUseHWAALines();
1966 if (smoothLines) {
1967 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1968 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1969 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1970 // must disable msaa to use line smoothing
1971 if (rt->isMultisampled() &&
1972 kNo_TriState != fHWAAState.fMSAAEnabled) {
1973 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1974 fHWAAState.fMSAAEnabled = kNo_TriState;
1975 }
1976 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001977 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001978 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1979 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1980 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1981 }
1982 }
1983 }
1984 if (!smoothLines && rt->isMultisampled()) {
1985 if (this->getDrawState().isHWAntialiasState()) {
1986 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1987 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1988 fHWAAState.fMSAAEnabled = kYes_TriState;
1989 }
1990 } else {
1991 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1992 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1993 fHWAAState.fMSAAEnabled = kNo_TriState;
1994 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001995 }
1996 }
1997 }
1998}
1999
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002000void GrGpuGL::flushBlend(GrPrimitiveType type,
2001 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002002 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002003 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002004 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002005 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002006 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002007 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002008 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
2009 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002010 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2011 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002012 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
2013 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002014 }
2015 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002016 // any optimization to disable blending should
2017 // have already been applied and tweaked the coeffs
2018 // to (1, 0).
2019 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2020 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002021 if (blendOff) {
2022 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002023 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002024 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002025 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002026 } else {
2027 if (kYes_TriState != fHWBlendState.fEnabled) {
2028 GL_CALL(Enable(GR_GL_BLEND));
2029 fHWBlendState.fEnabled = kYes_TriState;
2030 }
2031 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2032 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002033 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2034 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002035 fHWBlendState.fSrcCoeff = srcCoeff;
2036 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002037 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002038 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002039 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2040 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002041 (!fHWBlendState.fConstColorValid ||
2042 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002043
2044 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002045 GrColorUnpackR(blendConst) / 255.f,
2046 GrColorUnpackG(blendConst) / 255.f,
2047 GrColorUnpackB(blendConst) / 255.f,
2048 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002049 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002050 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002051 fHWBlendState.fConstColor = blendConst;
2052 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002053 }
2054 }
2055 }
2056}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002057namespace {
2058
2059unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002060 switch (filter) {
2061 case GrSamplerState::kBilinear_Filter:
2062 case GrSamplerState::k4x4Downsample_Filter:
2063 return GR_GL_LINEAR;
2064 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002065 return GR_GL_NEAREST;
2066 default:
2067 GrAssert(!"Unknown filter type");
2068 return GR_GL_LINEAR;
2069 }
2070}
2071
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002072// get_swizzle is only called from this .cpp so it is OK to inline it here
2073inline const GrGLenum* get_swizzle(GrPixelConfig config,
2074 const GrSamplerState& sampler,
2075 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002076 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002077 if (glCaps.textureRedSupport()) {
2078 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2079 GR_GL_RED, GR_GL_RED };
2080 return gRedSmear;
2081 } else {
2082 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2083 GR_GL_ALPHA, GR_GL_ALPHA };
2084 return gAlphaSmear;
2085 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002086 } else if (sampler.swapsRAndB()) {
2087 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2088 GR_GL_RED, GR_GL_ALPHA };
2089 return gRedBlueSwap;
2090 } else {
2091 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2092 GR_GL_BLUE, GR_GL_ALPHA };
2093 return gStraight;
2094 }
2095}
2096
2097void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002098 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2099 GR_GL_TEXTURE_SWIZZLE_RGBA,
2100 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002101}
2102}
2103
bsalomon@google.comffca4002011-02-22 20:34:01 +00002104bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002105
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002106 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002107 // GrGpu::setupClipAndFlushState should have already checked this
2108 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002109 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002110
tomhudson@google.com93813632011-10-27 20:21:16 +00002111 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002112 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002113 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002114 GrGLTexture* nextTexture =
2115 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002116
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002117 // true for now, but maybe not with GrEffect.
2118 GrAssert(NULL != nextTexture);
2119 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002120 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002121 // the last bound texture, but it needs resolving. So keep this
2122 // out of the "last != next" check.
2123 GrGLRenderTarget* texRT =
2124 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2125 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002126 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002127 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002128
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002129 if (fHWBoundTextures[s] != nextTexture) {
2130 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002131 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002132 #if GR_COLLECT_STATS
2133 ++fStats.fTextureChngCnt;
2134 #endif
2135 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002136 fHWBoundTextures[s] = nextTexture;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002137 }
2138
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002139 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002140 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002141 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002142 nextTexture->getCachedTexParams(&timestamp);
2143 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002144 GrGLTexture::TexParams newTexParams;
2145
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002146 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002147
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002148 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002149 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2150 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002151 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002152 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002153 sizeof(newTexParams.fSwizzleRGBA));
2154 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002155 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002156 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002157 GR_GL_TEXTURE_MAG_FILTER,
2158 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002159 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002160 GR_GL_TEXTURE_MIN_FILTER,
2161 newTexParams.fFilter));
2162 }
2163 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
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_WRAP_S,
2167 newTexParams.fWrapS));
2168 }
2169 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002170 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002171 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002172 GR_GL_TEXTURE_WRAP_T,
2173 newTexParams.fWrapT));
2174 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002175 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002176 (setAll ||
2177 memcmp(newTexParams.fSwizzleRGBA,
2178 oldTexParams.fSwizzleRGBA,
2179 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002180 this->setTextureUnit(s);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002181 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2182 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002183 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002184 nextTexture->setCachedTexParams(newTexParams,
2185 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002186 }
2187 }
2188
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002189 GrIRect* rect = NULL;
2190 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002191 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002192 fClip.hasConservativeBounds()) {
2193 fClip.getConservativeBounds().roundOut(&clipBounds);
2194 rect = &clipBounds;
2195 }
2196 this->flushRenderTarget(rect);
2197 this->flushAAState(type);
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002198
2199 if (drawState->isDitherState()) {
2200 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002201 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002202 fHWDitherEnabled = kYes_TriState;
2203 }
2204 } else {
2205 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002206 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002207 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002208 }
2209 }
2210
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002211 if (drawState->isColorWriteDisabled()) {
2212 if (kNo_TriState != fHWWriteToColor) {
2213 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2214 GR_GL_FALSE, GR_GL_FALSE));
2215 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002216 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002217 } else {
2218 if (kYes_TriState != fHWWriteToColor) {
2219 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2220 fHWWriteToColor = kYes_TriState;
2221 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002222 }
2223
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002224 if (fHWDrawFace != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002225 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002226 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002227 GL_CALL(Enable(GR_GL_CULL_FACE));
2228 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002229 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002230 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002231 GL_CALL(Enable(GR_GL_CULL_FACE));
2232 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002233 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002234 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002235 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002236 break;
2237 default:
2238 GrCrash("Unknown draw face.");
2239 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002240 fHWDrawFace = drawState->getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002241 }
2242
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002243#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002244 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002245 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002246 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002247 NULL == drawState->getRenderTarget() ||
2248 NULL == drawState->getTexture(s) ||
2249 drawState->getTexture(s)->asRenderTarget() !=
2250 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002251 }
2252#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002253
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002254 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002255
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002256 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002257}
2258
2259void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002260 if (fHWGeometryState.fVertexBuffer != buffer) {
2261 fHWGeometryState.fArrayPtrsDirty = true;
2262 fHWGeometryState.fVertexBuffer = buffer;
2263 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002264}
2265
2266void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002267 if (fHWGeometryState.fVertexBuffer == buffer) {
2268 // deleting bound buffer does implied bind to 0
2269 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002270 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002271 }
2272}
2273
2274void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002275 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002276}
2277
2278void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002279 if (fHWGeometryState.fIndexBuffer == buffer) {
2280 // deleting bound buffer does implied bind to 0
2281 fHWGeometryState.fIndexBuffer = NULL;
2282 }
2283}
2284
reed@google.comac10a2d2010-12-22 21:39:39 +00002285void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2286 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002287 GrDrawState* drawState = this->drawState();
2288 if (drawState->getRenderTarget() == renderTarget) {
2289 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002291 if (fHWBoundRenderTarget == renderTarget) {
2292 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002293 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002294}
2295
2296void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002297 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002298 GrDrawState* drawState = this->drawState();
2299 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002300 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002301 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002302 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002303 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002304 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002305 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002306 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002307}
2308
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002309bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2310 bool getSizedInternalFormat,
2311 GrGLenum* internalFormat,
2312 GrGLenum* externalFormat,
2313 GrGLenum* externalType) {
2314 GrGLenum dontCare;
2315 if (NULL == internalFormat) {
2316 internalFormat = &dontCare;
2317 }
2318 if (NULL == externalFormat) {
2319 externalFormat = &dontCare;
2320 }
2321 if (NULL == externalType) {
2322 externalType = &dontCare;
2323 }
2324
reed@google.comac10a2d2010-12-22 21:39:39 +00002325 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002326 case kRGBA_8888_PM_GrPixelConfig:
2327 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002328 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002329 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002330 if (getSizedInternalFormat) {
2331 *internalFormat = GR_GL_RGBA8;
2332 } else {
2333 *internalFormat = GR_GL_RGBA;
2334 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002335 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002336 break;
2337 case kBGRA_8888_PM_GrPixelConfig:
2338 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002339 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002340 return false;
2341 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002342 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002343 if (getSizedInternalFormat) {
2344 *internalFormat = GR_GL_BGRA8;
2345 } else {
2346 *internalFormat = GR_GL_BGRA;
2347 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002348 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002349 if (getSizedInternalFormat) {
2350 *internalFormat = GR_GL_RGBA8;
2351 } else {
2352 *internalFormat = GR_GL_RGBA;
2353 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002354 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002355 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002356 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002357 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002358 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002359 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002360 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002361 if (getSizedInternalFormat) {
2362 if (this->glBinding() == kDesktop_GrGLBinding) {
2363 return false;
2364 } else {
2365 *internalFormat = GR_GL_RGB565;
2366 }
2367 } else {
2368 *internalFormat = GR_GL_RGB;
2369 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002370 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002371 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002372 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002373 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002374 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002375 if (getSizedInternalFormat) {
2376 *internalFormat = GR_GL_RGBA4;
2377 } else {
2378 *internalFormat = GR_GL_RGBA;
2379 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002380 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002381 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002382 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002383 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002384 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002385 // glCompressedTexImage doesn't take external params
2386 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002387 // no sized/unsized internal format distinction here
2388 *internalFormat = GR_GL_PALETTE8_RGBA8;
2389 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002390 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002391 } else {
2392 return false;
2393 }
2394 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002395 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002396 if (this->glCaps().textureRedSupport()) {
2397 *internalFormat = GR_GL_RED;
2398 *externalFormat = GR_GL_RED;
2399 if (getSizedInternalFormat) {
2400 *internalFormat = GR_GL_R8;
2401 } else {
2402 *internalFormat = GR_GL_RED;
2403 }
2404 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002405 } else {
2406 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002407 *externalFormat = GR_GL_ALPHA;
2408 if (getSizedInternalFormat) {
2409 *internalFormat = GR_GL_ALPHA8;
2410 } else {
2411 *internalFormat = GR_GL_ALPHA;
2412 }
2413 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002414 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002415 break;
2416 default:
2417 return false;
2418 }
2419 return true;
2420}
2421
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002422void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002423 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002424 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002425 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002426 fActiveTextureUnitIdx = unit;
2427 }
2428}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002429
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002430void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002431 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002432 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002433 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2434 }
2435}
2436
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002437void GrGpuGL::setBuffers(bool indexed,
2438 int* extraVertexOffset,
2439 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002440
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002441 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002442
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002443 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2444
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002446 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002447 case kBuffer_GeometrySrcType:
2448 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002449 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002450 break;
2451 case kArray_GeometrySrcType:
2452 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002453 this->finalizeReservedVertices();
2454 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2455 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002456 break;
2457 default:
2458 vbuf = NULL; // suppress warning
2459 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002460 }
2461
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462 GrAssert(NULL != vbuf);
2463 GrAssert(!vbuf->isLocked());
2464 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002465 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002466 fHWGeometryState.fArrayPtrsDirty = true;
2467 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002468 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002469
2470 if (indexed) {
2471 GrAssert(NULL != extraIndexOffset);
2472
2473 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002474 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002475 case kBuffer_GeometrySrcType:
2476 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002477 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002478 break;
2479 case kArray_GeometrySrcType:
2480 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002481 this->finalizeReservedIndices();
2482 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2483 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002484 break;
2485 default:
2486 ibuf = NULL; // suppress warning
2487 GrCrash("Unknown geometry src type!");
2488 }
2489
2490 GrAssert(NULL != ibuf);
2491 GrAssert(!ibuf->isLocked());
2492 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002493 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002494 fHWGeometryState.fIndexBuffer = ibuf;
2495 }
2496 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002497}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002498