blob: 0e2036ced08c7f8d6998b197fd3ebc82cbb098e2 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000012#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000013#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000014#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
caryclark@google.comcf6285b2012-06-06 12:09:01 +000016static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000017static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000020#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021
bsalomon@google.com316f99232011-01-13 21:28:12 +000022// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000024static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000025
reed@google.comac10a2d2010-12-22 21:39:39 +000026#define SKIP_CACHE_CHECK true
27
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000028#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
29 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
30 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
31 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
32#else
33 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
34 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
35 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
36#endif
37
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000038
39///////////////////////////////////////////////////////////////////////////////
40
twiz@google.com0f31ca72011-03-18 17:38:11 +000041static const GrGLenum gXfermodeCoeff2Blend[] = {
42 GR_GL_ZERO,
43 GR_GL_ONE,
44 GR_GL_SRC_COLOR,
45 GR_GL_ONE_MINUS_SRC_COLOR,
46 GR_GL_DST_COLOR,
47 GR_GL_ONE_MINUS_DST_COLOR,
48 GR_GL_SRC_ALPHA,
49 GR_GL_ONE_MINUS_SRC_ALPHA,
50 GR_GL_DST_ALPHA,
51 GR_GL_ONE_MINUS_DST_ALPHA,
52 GR_GL_CONSTANT_COLOR,
53 GR_GL_ONE_MINUS_CONSTANT_COLOR,
54 GR_GL_CONSTANT_ALPHA,
55 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000056
57 // extended blend coeffs
58 GR_GL_SRC1_COLOR,
59 GR_GL_ONE_MINUS_SRC1_COLOR,
60 GR_GL_SRC1_ALPHA,
61 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000062};
63
bsalomon@google.com271cffc2011-05-20 14:13:56 +000064bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000065 static const bool gCoeffReferencesBlendConst[] = {
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 true,
77 true,
78 true,
79 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000080
81 // extended blend coeffs
82 false,
83 false,
84 false,
85 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000086 };
87 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000088 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
89 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000090
bsalomon@google.com47059542012-06-06 20:51:20 +000091 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
92 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
93 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
94 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
95 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
98 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
99 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
104 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000105
bsalomon@google.com47059542012-06-06 20:51:20 +0000106 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
109 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000110
111 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000112 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
113 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000114}
115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116///////////////////////////////////////////////////////////////////////////////
117
rileya@google.come38160c2012-07-03 18:03:04 +0000118static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000119
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000120static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000121
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000122 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000123
twiz@google.com0f31ca72011-03-18 17:38:11 +0000124 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000125 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
126 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000127 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000128 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
129 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000130 // some implementations require texture to be mip-map complete before
131 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000132 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000133 GR_GL_TEXTURE_MIN_FILTER,
134 GR_GL_NEAREST));
135 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
136 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
137 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
138 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
139 GR_GL_COLOR_ATTACHMENT0,
140 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000141 GrGLenum status;
142 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
144 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000145
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000146 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000147}
148
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000149GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
150
151 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000152
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000153 fillInConfigRenderableTable();
154
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000155 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000156
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000157 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000159 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000160 const GrGLubyte* ext;
161 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000162 const GrGLubyte* vendor;
163 const GrGLubyte* renderer;
164 const GrGLubyte* version;
165 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
166 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
167 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000168 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
169 this);
170 GrPrintf("------ VENDOR %s\n", vendor);
171 GrPrintf("------ RENDERER %s\n", renderer);
172 GrPrintf("------ VERSION %s\n", version);
173 GrPrintf("------ EXTENSIONS\n %s \n", ext);
174 }
175
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000176 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000177
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000178 fProgramData = NULL;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000179 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContextInfo()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000180
bsalomon@google.comfe676522011-06-17 18:12:21 +0000181 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000182 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000183 if (false) { // avoid bit rot, suppress warning
184 fbo_test(this->glInterface(), 0, 0);
185 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000186}
187
188GrGpuGL::~GrGpuGL() {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000189 if (fProgramData && 0 != fHWProgramID) {
190 // detach the current program so there is no confusion on OpenGL's part
191 // that we want it to be deleted
192 GrAssert(fHWProgramID == fProgramData->fProgramID);
193 GL_CALL(UseProgram(0));
194 }
195
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000196 delete fProgramCache;
197 fProgramCache = NULL;
198 fProgramData = NULL;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000199
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000200 // This must be called by before the GrDrawTarget destructor
201 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000202 // This subclass must do this before the base class destructor runs
203 // since we will unref the GrGLInterface.
204 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000205}
206
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000207///////////////////////////////////////////////////////////////////////////////
208
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000209void GrGpuGL::initCaps() {
210 GrGLint maxTextureUnits;
211 // check FS and fixed-function texture unit limits
212 // we only use textures in the fragment stage currently.
213 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000214 const GrGLInterface* gl = this->glInterface();
215 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000216 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000217
218 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000219 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000220 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000221 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000222 for (int i = 0; i < numFormats; ++i) {
223 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
224 fCaps.f8BitPaletteSupport = true;
225 break;
226 }
227 }
228
229 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000230 // we could also look for GL_ATI_separate_stencil extension or
231 // GL_EXT_stencil_two_side but they use different function signatures
232 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000233 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000235 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000236 this->hasExtension("GL_EXT_stencil_wrap");
237 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000238 // ES 2 has two sided stencil and stencil wrap
239 fCaps.fTwoSidedStencilSupport = true;
240 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000241 }
242
243 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000244 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
245 // extension includes glMapBuffer.
246 } else {
247 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
248 }
249
250 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000251 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000252 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
253 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000254 } else {
255 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000256 }
257 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000258 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000259 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000260 }
261
262 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
263
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000264 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
265 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000266 // Our render targets are always created with textures as the color
267 // attachment, hence this min:
268 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
269
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000270 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000271 fCaps.fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
272 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000273
274 // Enable supported shader-related caps
275 if (kDesktop_GrGLBinding == this->glBinding()) {
276 fCaps.fDualSourceBlendingSupport =
277 this->glVersion() >= GR_GL_VER(3,3) ||
278 this->hasExtension("GL_ARB_blend_func_extended");
279 fCaps.fShaderDerivativeSupport = true;
280 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
281 fCaps.fGeometryShaderSupport =
282 this->glVersion() >= GR_GL_VER(3,2) &&
283 this->glslGeneration() >= k150_GrGLSLGeneration;
284 } else {
285 fCaps.fShaderDerivativeSupport =
286 this->hasExtension("GL_OES_standard_derivatives");
287 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000288}
289
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000290void GrGpuGL::fillInConfigRenderableTable() {
291
292 // OpenGL < 3.0
293 // no support for render targets unless the GL_ARB_framebuffer_object
294 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
295 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
296 // probably don't get R8 in this case.
297
298 // OpenGL 3.0
299 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
300 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
301
302 // >= OpenGL 3.1
303 // base color renderable: RED, RG, RGB, and RGBA
304 // sized derivatives: R8, RGBA4, RGBA8
305 // if the GL_ARB_compatibility extension is supported then we get back
306 // support for GL_ALPHA and ALPHA8
307
308 // GL_EXT_bgra adds BGRA render targets to any version
309
310 // ES 2.0
311 // color renderable: RGBA4, RGB5_A1, RGB565
312 // GL_EXT_texture_rg adds support for R8 as a color render target
313 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
314 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
315 // added BGRA support
316
317 if (kDesktop_GrGLBinding == this->glBinding()) {
318 // Post 3.0 we will get R8
319 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
320 if (this->glVersion() >= GR_GL_VER(3,0) ||
321 this->hasExtension("GL_ARB_framebuffer_object")) {
322 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
323 }
324 } else {
325 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000326 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
327 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000328 }
329
330 if (kDesktop_GrGLBinding != this->glBinding()) {
331 // only available in ES
332 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
333 }
334
335 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
336 // GL_EXT_framebuffer_object for FBO support. Both of these
337 // allow RGBA4 render targets so this is always supported.
338 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
339
340 if (this->glCaps().rgba8RenderbufferSupport()) {
341 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
342 }
343
344 if (this->glCaps().bgraFormatSupport()) {
345 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
346 }
347
348 // the un-premultiplied formats just inherit the premultiplied setting
349 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
350 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
351 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
352 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
353}
354
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000355bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
356 if (kUnknown_CanPreserveUnpremulRoundtrip ==
357 fCanPreserveUnpremulRoundtrip) {
358
359 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
360 uint32_t* srcData = data.get();
361 uint32_t* firstRead = data.get() + 256 * 256;
362 uint32_t* secondRead = data.get() + 2 * 256 * 256;
363
364 for (int y = 0; y < 256; ++y) {
365 for (int x = 0; x < 256; ++x) {
366 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
367 color[3] = y;
368 color[2] = x;
369 color[1] = x;
370 color[0] = x;
371 }
372 }
373
374 // We have broader support for read/write pixels on render targets
375 // than on textures.
376 GrTextureDesc dstDesc;
377 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
378 kNoStencil_GrTextureFlagBit;
379 dstDesc.fWidth = 256;
380 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000381 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000382
383 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
384 if (!dstTex.get()) {
385 return false;
386 }
387 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
388 GrAssert(NULL != rt);
389
390 bool failed = true;
391 static const UnpremulConversion gMethods[] = {
392 kUpOnWrite_DownOnRead_UnpremulConversion,
393 kDownOnWrite_UpOnRead_UnpremulConversion,
394 };
395
396 // pretend that we can do the roundtrip to avoid recursive calls to
397 // this function
398 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
399 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
400 fUnpremulConversion = gMethods[i];
401 rt->writePixels(0, 0,
402 256, 256,
403 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
404 rt->readPixels(0, 0,
405 256, 256,
406 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
407 rt->writePixels(0, 0,
408 256, 256,
409 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
410 rt->readPixels(0, 0,
411 256, 256,
412 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
413 failed = false;
414 for (int j = 0; j < 256 * 256; ++j) {
415 if (firstRead[j] != secondRead[j]) {
416 failed = true;
417 break;
418 }
419 }
420 }
421 fCanPreserveUnpremulRoundtrip = failed ?
422 kNo_CanPreserveUnpremulRoundtrip :
423 kYes_CanPreserveUnpremulRoundtrip;
424 }
425
426 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
427 return true;
428 } else {
429 return false;
430 }
431}
432
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000433GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000434 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
435 return GrPixelConfigSwapRAndB(config);
436 } else {
437 return config;
438 }
439}
440
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000441GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000442 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000443 return GrPixelConfigSwapRAndB(config);
444 } else {
445 return config;
446 }
447}
448
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000449bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
450 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
451}
452
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000453void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000454 if (gPrintStartupSpew && !fPrintedCaps) {
455 fPrintedCaps = true;
456 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000457 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000458 }
459
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000460 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000461 GL_CALL(Disable(GR_GL_DEPTH_TEST));
462 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000463
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000464 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
465 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000467 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000468 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000469 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000470 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
471 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
472 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
473 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000474 if (this->glCaps().imagingSupport()) {
475 GL_CALL(Disable(GR_GL_COLOR_TABLE));
476 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000477 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
478 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
479 // Since ES doesn't support glPointSize at all we always use the VS to
480 // set the point size
481 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
482
483 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
484 // currently part of our gl interface. There are probably others as
485 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000486 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000487 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000488 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000489
reed@google.comac10a2d2010-12-22 21:39:39 +0000490 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000491 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000492
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000493 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000494 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000496 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000497
tomhudson@google.com93813632011-10-27 20:21:16 +0000498 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000499 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000500 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000501
bsalomon@google.coma3201942012-06-21 19:58:20 +0000502 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000503
bsalomon@google.coma3201942012-06-21 19:58:20 +0000504 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000505
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000506 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000507 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000508
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 fHWGeometryState.fIndexBuffer = NULL;
510 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000511
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000512 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000513
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000514 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000515
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000516 fHWPathMatrixState.invalidate();
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000517 if (fCaps.fPathStencilingSupport) {
518 // we don't use the model view matrix.
519 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
520 GL_CALL(LoadIdentity());
521 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000522
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000523 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000524 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000525 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
526 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000527 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000528 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
529 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000530 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000531 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
532 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000533 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000534 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
535 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000536
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000537 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000538
539 // Third party GL code may have left vertex attributes enabled. Some GL
540 // implementations (osmesa) may read vetex attributes that are not required
541 // by the current shader. Therefore, we have to ensure that only the
542 // attributes we require for the current draw are enabled or we may cause an
543 // invalid read.
544
545 // Disable all vertex layout bits so that next flush will assume all
546 // optional vertex attributes are disabled.
547 fHWGeometryState.fVertexLayout = 0;
548
549 // We always use the this attribute and assume it is always enabled.
550 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
551 GL_CALL(EnableVertexAttribArray(posAttrIdx));
552 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000553 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000554 if (va != posAttrIdx) {
555 GL_CALL(DisableVertexAttribArray(va));
556 }
557 }
558
559 fHWProgramID = 0;
560 fHWConstAttribColor = GrColor_ILLEGAL;
561 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000562}
563
bsalomon@google.come269f212011-11-07 13:29:52 +0000564GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000565 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000566 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000567 return NULL;
568 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000569
robertphillips@google.com32716282012-06-04 12:48:45 +0000570 // next line relies on PlatformTextureDesc's flags matching GrTexture's
571 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000572 glTexDesc.fWidth = desc.fWidth;
573 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000574 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000575 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000576 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
577 glTexDesc.fOwnsID = false;
578 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
579
580 GrGLTexture* texture = NULL;
581 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
582 GrGLRenderTarget::Desc glRTDesc;
583 glRTDesc.fRTFBOID = 0;
584 glRTDesc.fTexFBOID = 0;
585 glRTDesc.fMSColorRenderbufferID = 0;
586 glRTDesc.fOwnIDs = true;
587 glRTDesc.fConfig = desc.fConfig;
588 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000589 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
590 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000591 glTexDesc.fTextureID,
592 &glRTDesc)) {
593 return NULL;
594 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000595 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000596 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000597 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000598 }
599 if (NULL == texture) {
600 return NULL;
601 }
602
603 this->setSpareTextureUnit();
604 return texture;
605}
606
607GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
608 GrGLRenderTarget::Desc glDesc;
609 glDesc.fConfig = desc.fConfig;
610 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
611 glDesc.fMSColorRenderbufferID = 0;
612 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
613 glDesc.fSampleCnt = desc.fSampleCnt;
614 glDesc.fOwnIDs = false;
615 GrGLIRect viewport;
616 viewport.fLeft = 0;
617 viewport.fBottom = 0;
618 viewport.fWidth = desc.fWidth;
619 viewport.fHeight = desc.fHeight;
620
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000621 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
622 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000623 if (desc.fStencilBits) {
624 GrGLStencilBuffer::Format format;
625 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
626 format.fPacked = false;
627 format.fStencilBits = desc.fStencilBits;
628 format.fTotalBits = desc.fStencilBits;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000629 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
630 (this,
631 0,
632 desc.fWidth,
633 desc.fHeight,
634 desc.fSampleCnt,
635 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000636 tgt->setStencilBuffer(sb);
637 sb->unref();
638 }
639 return tgt;
640}
641
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000642////////////////////////////////////////////////////////////////////////////////
643
bsalomon@google.com6f379512011-11-16 20:36:03 +0000644void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
645 int left, int top, int width, int height,
646 GrPixelConfig config, const void* buffer,
647 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000648 if (NULL == buffer) {
649 return;
650 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000651 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
652
bsalomon@google.com6f379512011-11-16 20:36:03 +0000653 this->setSpareTextureUnit();
654 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
655 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000656 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000657 desc.fWidth = glTex->width();
658 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000659 desc.fConfig = glTex->config();
660 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000661 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000662 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000663
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000664 this->uploadTexData(desc, false,
665 left, top, width, height,
666 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000667}
668
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000669namespace {
670bool adjust_pixel_ops_params(int surfaceWidth,
671 int surfaceHeight,
672 size_t bpp,
673 int* left, int* top, int* width, int* height,
674 const void** data,
675 size_t* rowBytes) {
676 if (!*rowBytes) {
677 *rowBytes = *width * bpp;
678 }
679
680 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
681 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
682
683 if (!subRect.intersect(bounds)) {
684 return false;
685 }
686 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
687 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
688
689 *left = subRect.fLeft;
690 *top = subRect.fTop;
691 *width = subRect.width();
692 *height = subRect.height();
693 return true;
694}
695}
696
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000697bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
698 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000699 int left, int top, int width, int height,
700 GrPixelConfig dataConfig,
701 const void* data,
702 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000703 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000704
705 size_t bpp = GrBytesPerPixel(dataConfig);
706 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
707 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000708 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000709 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000710 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000711
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000712 // in case we need a temporary, trimmed copy of the src pixels
713 SkAutoSMalloc<128 * 128> tempStorage;
714
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000715 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000716 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000717 if (useTexStorage) {
718 if (kDesktop_GrGLBinding == this->glBinding()) {
719 // 565 is not a sized internal format on desktop GL. So on desktop
720 // with 565 we always use an unsized internal format to let the
721 // system pick the best sized format to convert the 565 data to.
722 // Since glTexStorage only allows sized internal formats we will
723 // instead fallback to glTexImage2D.
724 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
725 } else {
726 // ES doesn't allow paletted textures to be used with tex storage
727 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
728 }
729 }
730
731 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000732 GrGLenum externalFormat;
733 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000734 // glTexStorage requires sized internal formats on both desktop and ES. ES
735 // glTexImage requires an unsized format.
736 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
737 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000738 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000739 }
740
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000741 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000742 // paletted textures cannot be updated
743 return false;
744 }
745
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000746 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000747 * check whether to allocate a temporary buffer for flipping y or
748 * because our srcData has extra bytes past each row. If so, we need
749 * to trim those off here, since GL ES may not let us specify
750 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000751 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000752 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000753 bool swFlipY = false;
754 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000755 if (NULL != data) {
756 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000757 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000758 glFlipY = true;
759 } else {
760 swFlipY = true;
761 }
762 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000763 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000764 // can't use this for flipping, only non-neg values allowed. :(
765 if (rowBytes != trimRowBytes) {
766 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
767 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
768 restoreGLRowLength = true;
769 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000770 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000771 if (trimRowBytes != rowBytes || swFlipY) {
772 // copy data into our new storage, skipping the trailing bytes
773 size_t trimSize = height * trimRowBytes;
774 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000775 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000776 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000777 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000778 char* dst = (char*)tempStorage.reset(trimSize);
779 for (int y = 0; y < height; y++) {
780 memcpy(dst, src, trimRowBytes);
781 if (swFlipY) {
782 src -= rowBytes;
783 } else {
784 src += rowBytes;
785 }
786 dst += trimRowBytes;
787 }
788 // now point data to our copied version
789 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000790 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000791 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000792 if (glFlipY) {
793 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
794 }
795 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000796 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000797 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000798 if (isNewTexture &&
799 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000800 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000801 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000802 if (useTexStorage) {
803 // We never resize or change formats of textures. We don't use
804 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000805 GL_ALLOC_CALL(this->glInterface(),
806 TexStorage2D(GR_GL_TEXTURE_2D,
807 1, // levels
808 internalFormat,
809 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000810 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000811 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
812 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
813 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000814 GL_ALLOC_CALL(this->glInterface(),
815 CompressedTexImage2D(GR_GL_TEXTURE_2D,
816 0, // level
817 internalFormat,
818 desc.fWidth, desc.fHeight,
819 0, // border
820 imageSize,
821 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000822 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000823 GL_ALLOC_CALL(this->glInterface(),
824 TexImage2D(GR_GL_TEXTURE_2D,
825 0, // level
826 internalFormat,
827 desc.fWidth, desc.fHeight,
828 0, // border
829 externalFormat, externalType,
830 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000831 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000832 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000833 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000834 if (error != GR_GL_NO_ERROR) {
835 succeeded = false;
836 } else {
837 // if we have data and we used TexStorage to create the texture, we
838 // now upload with TexSubImage.
839 if (NULL != data && useTexStorage) {
840 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
841 0, // level
842 left, top,
843 width, height,
844 externalFormat, externalType,
845 data));
846 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000847 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000848 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000849 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000850 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000851 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000852 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
853 0, // level
854 left, top,
855 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000856 externalFormat, externalType, data));
857 }
858
859 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000860 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000861 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000862 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000863 if (glFlipY) {
864 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
865 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000866 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000867}
868
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000869namespace {
870bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
871 int sampleCount,
872 GrGLenum format,
873 int width, int height) {
874 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
875 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
876 bool created = false;
877 if (GrGLCaps::kNVDesktop_CoverageAAType ==
878 ctxInfo.caps().coverageAAType()) {
879 const GrGLCaps::MSAACoverageMode& mode =
880 ctxInfo.caps().getMSAACoverageMode(sampleCount);
881 GL_ALLOC_CALL(ctxInfo.interface(),
882 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
883 mode.fCoverageSampleCnt,
884 mode.fColorSampleCnt,
885 format,
886 width, height));
887 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
888 }
889 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000890 // glRBMS will fail if requested samples is > max samples.
891 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000892 GL_ALLOC_CALL(ctxInfo.interface(),
893 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
894 sampleCount,
895 format,
896 width, height));
897 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
898 }
899 return created;
900}
901}
902
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000903bool GrGpuGL::createRenderTargetObjects(int width, int height,
904 GrGLuint texID,
905 GrGLRenderTarget::Desc* desc) {
906 desc->fMSColorRenderbufferID = 0;
907 desc->fRTFBOID = 0;
908 desc->fTexFBOID = 0;
909 desc->fOwnIDs = true;
910
911 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912
bsalomon@google.comab15d612011-08-09 12:57:56 +0000913 GrGLenum msColorFormat = 0; // suppress warning
914
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000915 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000916 if (!desc->fTexFBOID) {
917 goto FAILED;
918 }
919
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000920
921 // If we are using multisampling we will create two FBOS. We render
922 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000923 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000924 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000925 goto FAILED;
926 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
928 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 if (!desc->fRTFBOID ||
930 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000931 !this->configToGLFormats(desc->fConfig,
932 // GLES requires sized internal formats
933 kES2_GrGLBinding == this->glBinding(),
934 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935 goto FAILED;
936 }
937 } else {
938 desc->fRTFBOID = desc->fTexFBOID;
939 }
940
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000941 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000942 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943 if (desc->fRTFBOID != desc->fTexFBOID) {
944 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000945 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000946 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000947 if (!renderbuffer_storage_msaa(fGLContextInfo,
948 desc->fSampleCnt,
949 msColorFormat,
950 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000951 goto FAILED;
952 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000953 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
954 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000955 GR_GL_COLOR_ATTACHMENT0,
956 GR_GL_RENDERBUFFER,
957 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000958 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000959 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
960 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
961 goto FAILED;
962 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000963 fGLContextInfo.caps().markConfigAsValidColorAttachment(
964 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000965 }
966 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000967 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000968
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000969 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
970 GR_GL_COLOR_ATTACHMENT0,
971 GR_GL_TEXTURE_2D,
972 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000973 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000974 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
975 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
976 goto FAILED;
977 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000978 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000979 }
980
981 return true;
982
983FAILED:
984 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000985 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000986 }
987 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000988 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000989 }
990 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000991 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000992 }
993 return false;
994}
995
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000996// good to set a break-point here to know when createTexture fails
997static GrTexture* return_null_texture() {
998// GrAssert(!"null texture");
999 return NULL;
1000}
1001
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001002#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001003static size_t as_size_t(int x) {
1004 return x;
1005}
1006#endif
1007
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001008GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001009 const void* srcData,
1010 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001011
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001012 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001013 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001014
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001015 // Attempt to catch un- or wrongly initialized sample counts;
1016 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1017
robertphillips@google.com32716282012-06-04 12:48:45 +00001018 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +00001019 glTexDesc.fWidth = desc.fWidth;
1020 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001021 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +00001022 glTexDesc.fSampleCnt = desc.fSampleCnt;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001023 glTexDesc.fClientCacheID = desc.fClientCacheID;
robertphillips@google.com32716282012-06-04 12:48:45 +00001024
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001025 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001026
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001027 glRTDesc.fMSColorRenderbufferID = 0;
1028 glRTDesc.fRTFBOID = 0;
1029 glRTDesc.fTexFBOID = 0;
1030 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001031 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001032
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001033 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001034
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001035 const Caps& caps = this->getCaps();
1036
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001037 // We keep GrRenderTargets in GL's normal orientation so that they
1038 // can be drawn to by the outside world without the client having
1039 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001040 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001041 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001042
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001043 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001044 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001045 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001046 //GrPrintf("MSAA RT requested but not supported on this platform.");
1047 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001048 }
1049
reed@google.comac10a2d2010-12-22 21:39:39 +00001050 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001051 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1052 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001053 return return_null_texture();
1054 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 }
1056
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001057 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001058 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001059 // provides a hint about how this texture will be used
1060 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1061 GR_GL_TEXTURE_USAGE,
1062 GR_GL_FRAMEBUFFER_ATTACHMENT));
1063 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001064 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001065 return return_null_texture();
1066 }
1067
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001068 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001069 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001070
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001071 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1072 // drivers have a bug where an FBO won't be complete if it includes a
1073 // texture that is not mipmap complete (considering the filter in use).
1074 GrGLTexture::TexParams initialTexParams;
1075 // we only set a subset here so invalidate first
1076 initialTexParams.invalidate();
1077 initialTexParams.fFilter = GR_GL_NEAREST;
1078 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1079 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001080 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1081 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001082 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001083 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1084 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001085 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001086 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1087 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001088 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001089 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1090 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001091 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001092 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1093 glTexDesc.fWidth, glTexDesc.fHeight,
1094 desc.fConfig, srcData, rowBytes)) {
1095 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1096 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001097 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001098
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001099 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001101 // unbind the texture from the texture unit before binding it to the frame buffer
1102 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1103
bsalomon@google.com99621082011-11-15 16:47:16 +00001104 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1105 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106 glTexDesc.fTextureID,
1107 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001108 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001109 return return_null_texture();
1110 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001111 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001113 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001114 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001115 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001116#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001117 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1118 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001119#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001120 return tex;
1121}
1122
1123namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001124
1125const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1126
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1128 GrGLuint rb,
1129 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 // we shouldn't ever know one size and not the other
1131 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1132 (kUnknownBitCount == format->fTotalBits));
1133 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001134 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1136 (GrGLint*)&format->fStencilBits);
1137 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001138 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1140 (GrGLint*)&format->fTotalBits);
1141 format->fTotalBits += format->fStencilBits;
1142 } else {
1143 format->fTotalBits = format->fStencilBits;
1144 }
1145 }
1146}
1147}
1148
1149bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1150 int width, int height) {
1151
1152 // All internally created RTs are also textures. We don't create
1153 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1154 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001155 GrAssert(width >= rt->width());
1156 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157
1158 int samples = rt->numSamples();
1159 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 if (!sbID) {
1162 return false;
1163 }
1164
1165 GrGLStencilBuffer* sb = NULL;
1166
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001167 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 // we start with the last stencil format that succeeded in hopes
1171 // that we won't go through this loop more than once after the
1172 // first (painful) stencil creation.
1173 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001174 const GrGLCaps::StencilFormat& sFmt =
1175 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001176 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001177 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001179 bool created;
1180 if (samples > 0) {
1181 created = renderbuffer_storage_msaa(fGLContextInfo,
1182 samples,
1183 sFmt.fInternalFormat,
1184 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001186 GL_ALLOC_CALL(this->glInterface(),
1187 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001188 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001189 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001190 created =
1191 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001193 if (created) {
1194 // After sized formats we attempt an unsized format and take
1195 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001196 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001197 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001198 sb = SkNEW_ARGS(GrGLStencilBuffer,
1199 (this, sbID, width, height,
1200 samples, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1202 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001203 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 return true;
1206 }
1207 sb->abandon(); // otherwise we lose sbID
1208 sb->unref();
1209 }
1210 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001211 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001212 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213}
1214
1215bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1216 GrRenderTarget* rt) {
1217 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1218
1219 GrGLuint fbo = glrt->renderFBOID();
1220
1221 if (NULL == sb) {
1222 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001223 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 GR_GL_STENCIL_ATTACHMENT,
1225 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001226 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 GR_GL_DEPTH_ATTACHMENT,
1228 GR_GL_RENDERBUFFER, 0));
1229#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001230 GrGLenum status;
1231 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1233#endif
1234 }
1235 return true;
1236 } else {
1237 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1238 GrGLuint rb = glsb->renderbufferID();
1239
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001240 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001241 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1242 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 GR_GL_STENCIL_ATTACHMENT,
1244 GR_GL_RENDERBUFFER, rb));
1245 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001246 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247 GR_GL_DEPTH_ATTACHMENT,
1248 GR_GL_RENDERBUFFER, rb));
1249 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 GR_GL_DEPTH_ATTACHMENT,
1252 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001254
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001255 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001256 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001257 glsb->format())) {
1258 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1259 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001260 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001261 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001262 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001263 if (glsb->format().fPacked) {
1264 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1265 GR_GL_DEPTH_ATTACHMENT,
1266 GR_GL_RENDERBUFFER, 0));
1267 }
1268 return false;
1269 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001270 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001271 rt->config(),
1272 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001273 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001274 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001275 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001276 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001277}
1278
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001279////////////////////////////////////////////////////////////////////////////////
1280
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001281GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001282 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001283 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001285 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001286 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001287 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001288 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001289 GL_ALLOC_CALL(this->glInterface(),
1290 BufferData(GR_GL_ARRAY_BUFFER,
1291 size,
1292 NULL, // data ptr
1293 dynamic ? GR_GL_DYNAMIC_DRAW :
1294 GR_GL_STATIC_DRAW));
1295 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001296 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 // deleting bound buffer does implicit bind to 0
1298 fHWGeometryState.fVertexBuffer = NULL;
1299 return NULL;
1300 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001301 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer,
1302 (this, id,
1303 size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 fHWGeometryState.fVertexBuffer = vertexBuffer;
1305 return vertexBuffer;
1306 }
1307 return NULL;
1308}
1309
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001310GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001311 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001312 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001314 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001315 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001317 GL_ALLOC_CALL(this->glInterface(),
1318 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1319 size,
1320 NULL, // data ptr
1321 dynamic ? GR_GL_DYNAMIC_DRAW :
1322 GR_GL_STATIC_DRAW));
1323 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001324 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 // deleting bound buffer does implicit bind to 0
1326 fHWGeometryState.fIndexBuffer = NULL;
1327 return NULL;
1328 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001329 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer,
1330 (this, id, size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 fHWGeometryState.fIndexBuffer = indexBuffer;
1332 return indexBuffer;
1333 }
1334 return NULL;
1335}
1336
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001337GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001338 GrAssert(fCaps.fPathStencilingSupport);
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001339 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001340}
1341
bsalomon@google.coma3201942012-06-21 19:58:20 +00001342void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001343 const GrDrawState& drawState = this->getDrawState();
1344 const GrGLRenderTarget* rt =
1345 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1346
1347 GrAssert(NULL != rt);
1348 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001349
bsalomon@google.coma3201942012-06-21 19:58:20 +00001350 if (fScissorState.fEnabled) {
1351 GrGLIRect scissor;
1352 scissor.setRelativeTo(vp,
1353 fScissorState.fRect.fLeft,
1354 fScissorState.fRect.fTop,
1355 fScissorState.fRect.width(),
1356 fScissorState.fRect.height());
1357 // if the scissor fully contains the viewport then we fall through and
1358 // disable the scissor test.
1359 if (!scissor.contains(vp)) {
1360 if (fHWScissorSettings.fRect != scissor) {
1361 scissor.pushToGLScissor(this->glInterface());
1362 fHWScissorSettings.fRect = scissor;
1363 }
1364 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1365 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1366 fHWScissorSettings.fEnabled = kYes_TriState;
1367 }
1368 return;
1369 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001371 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001372 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001373 fHWScissorSettings.fEnabled = kNo_TriState;
1374 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001375 }
1376}
1377
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001378void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001379 const GrDrawState& drawState = this->getDrawState();
1380 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001381 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001382 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001383
bsalomon@google.com74b98712011-11-11 19:46:16 +00001384 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001385 if (NULL != rect) {
1386 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001387 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001388 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001389 if (clippedRect.intersect(rtRect)) {
1390 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001391 } else {
1392 return;
1393 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001394 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001395 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001396 GrAutoTRestore<ScissorState> asr(&fScissorState);
1397 fScissorState.fEnabled = (NULL != rect);
1398 if (fScissorState.fEnabled) {
1399 fScissorState.fRect = *rect;
1400 }
1401 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001402
1403 GrGLfloat r, g, b, a;
1404 static const GrGLfloat scale255 = 1.f / 255.f;
1405 a = GrColorUnpackA(color) * scale255;
1406 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001407 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001408 scaleRGB *= a;
1409 }
1410 r = GrColorUnpackR(color) * scaleRGB;
1411 g = GrColorUnpackG(color) * scaleRGB;
1412 b = GrColorUnpackB(color) * scaleRGB;
1413
1414 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001415 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001416 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001417 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001418}
1419
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001420void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001421 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001422 return;
1423 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001424
1425 this->flushRenderTarget(&GrIRect::EmptyIRect());
1426
bsalomon@google.coma3201942012-06-21 19:58:20 +00001427 GrAutoTRestore<ScissorState> asr(&fScissorState);
1428 fScissorState.fEnabled = false;
1429 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001430
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001431 GL_CALL(StencilMask(0xffffffff));
1432 GL_CALL(ClearStencil(0));
1433 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001434 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001435}
1436
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001437void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001438 const GrDrawState& drawState = this->getDrawState();
1439 const GrRenderTarget* rt = drawState.getRenderTarget();
1440 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001441
1442 // this should only be called internally when we know we have a
1443 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001444 GrAssert(NULL != rt->getStencilBuffer());
1445 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001446#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001447 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001448 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001449#else
1450 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001451 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001452 // turned into draws. Our contract on GrDrawTarget says that
1453 // changing the clip between stencil passes may or may not
1454 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001455 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001456#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001457 GrGLint value;
1458 if (insideClip) {
1459 value = (1 << (stencilBitCount - 1));
1460 } else {
1461 value = 0;
1462 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001463 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001464
1465 GrAutoTRestore<ScissorState> asr(&fScissorState);
1466 fScissorState.fEnabled = true;
1467 fScissorState.fRect = rect;
1468 this->flushScissor();
1469
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001470 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001471 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001472 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001473 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001474}
1475
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001476void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001477 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001478}
1479
bsalomon@google.comc4364992011-11-07 15:54:49 +00001480bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1481 int left, int top,
1482 int width, int height,
1483 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001484 size_t rowBytes) const {
1485 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001486 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001487 return false;
1488 }
1489
1490 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001491 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001492 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001493 return true;
1494 }
1495 // If we have to do memcpys to handle rowBytes then y-flip is free
1496 // Note the rowBytes might be tight to the passed in data, but if data
1497 // gets clipped in x to the target the rowBytes will no longer be tight.
1498 if (left >= 0 && (left + width) < renderTarget->width()) {
1499 return 0 == rowBytes ||
1500 GrBytesPerPixel(config) * width == rowBytes;
1501 } else {
1502 return false;
1503 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001504}
1505
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001506bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001507 int left, int top,
1508 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001509 GrPixelConfig config,
1510 void* buffer,
1511 size_t rowBytes,
1512 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001513 GrGLenum format;
1514 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001515 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001516 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001517 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001518 size_t bpp = GrBytesPerPixel(config);
1519 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1520 &left, &top, &width, &height,
1521 const_cast<const void**>(&buffer),
1522 &rowBytes)) {
1523 return false;
1524 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001525
bsalomon@google.comc6980972011-11-02 19:57:21 +00001526 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001527 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001528 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001529 switch (tgt->getResolveType()) {
1530 case GrGLRenderTarget::kCantResolve_ResolveType:
1531 return false;
1532 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001533 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001534 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001535 break;
1536 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001537 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001538 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001539 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1540 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001541 break;
1542 default:
1543 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001544 }
1545
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001546 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001547
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001548 // the read rect is viewport-relative
1549 GrGLIRect readRect;
1550 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001551
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001552 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001553 if (0 == rowBytes) {
1554 rowBytes = tightRowBytes;
1555 }
1556 size_t readDstRowBytes = tightRowBytes;
1557 void* readDst = buffer;
1558
1559 // determine if GL can read using the passed rowBytes or if we need
1560 // a scratch buffer.
1561 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1562 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001563 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001564 GrAssert(!(rowBytes % sizeof(GrColor)));
1565 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1566 readDstRowBytes = rowBytes;
1567 } else {
1568 scratch.reset(tightRowBytes * height);
1569 readDst = scratch.get();
1570 }
1571 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001572 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001573 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1574 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001575 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1576 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001577 format, type, readDst));
1578 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001579 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001580 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1581 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001582 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001583 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1584 invertY = true;
1585 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001586
1587 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001588 // API presents top-to-bottom. We must preserve the padding contents. Note
1589 // that the above readPixels did not overwrite the padding.
1590 if (readDst == buffer) {
1591 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001592 if (!invertY) {
1593 scratch.reset(tightRowBytes);
1594 void* tmpRow = scratch.get();
1595 // flip y in-place by rows
1596 const int halfY = height >> 1;
1597 char* top = reinterpret_cast<char*>(buffer);
1598 char* bottom = top + (height - 1) * rowBytes;
1599 for (int y = 0; y < halfY; y++) {
1600 memcpy(tmpRow, top, tightRowBytes);
1601 memcpy(top, bottom, tightRowBytes);
1602 memcpy(bottom, tmpRow, tightRowBytes);
1603 top += rowBytes;
1604 bottom -= rowBytes;
1605 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001606 }
1607 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001608 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001609 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001610 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001611 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001612 char* dst = reinterpret_cast<char*>(buffer);
1613 if (!invertY) {
1614 dst += (height-1) * rowBytes;
1615 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001616 for (int y = 0; y < height; y++) {
1617 memcpy(dst, src, tightRowBytes);
1618 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001619 if (invertY) {
1620 dst += rowBytes;
1621 } else {
1622 dst -= rowBytes;
1623 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001624 }
1625 }
1626 return true;
1627}
1628
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001629void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001630
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001631 GrGLRenderTarget* rt =
1632 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1633 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001634
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001635 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001636 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001637 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001638 GrGLenum status;
1639 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001640 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001641 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 }
1643 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001644 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001645 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001646 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001647 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001648 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001649 }
1650 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001651 if (NULL == bound || !bound->isEmpty()) {
1652 rt->flagAsNeedingResolve(bound);
1653 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001654}
1655
twiz@google.com0f31ca72011-03-18 17:38:11 +00001656GrGLenum gPrimitiveType2GLMode[] = {
1657 GR_GL_TRIANGLES,
1658 GR_GL_TRIANGLE_STRIP,
1659 GR_GL_TRIANGLE_FAN,
1660 GR_GL_POINTS,
1661 GR_GL_LINES,
1662 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001663};
1664
bsalomon@google.comd302f142011-03-03 13:54:13 +00001665#define SWAP_PER_DRAW 0
1666
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001667#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001668 #if GR_MAC_BUILD
1669 #include <AGL/agl.h>
1670 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001671 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001672 void SwapBuf() {
1673 DWORD procID = GetCurrentProcessId();
1674 HWND hwnd = GetTopWindow(GetDesktopWindow());
1675 while(hwnd) {
1676 DWORD wndProcID = 0;
1677 GetWindowThreadProcessId(hwnd, &wndProcID);
1678 if(wndProcID == procID) {
1679 SwapBuffers(GetDC(hwnd));
1680 }
1681 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1682 }
1683 }
1684 #endif
1685#endif
1686
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001687void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1688 uint32_t startVertex,
1689 uint32_t startIndex,
1690 uint32_t vertexCount,
1691 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001692 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1693
twiz@google.com0f31ca72011-03-18 17:38:11 +00001694 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001695
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001696 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1697 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1698
1699 // our setupGeometry better have adjusted this to zero since
1700 // DrawElements always draws from the begining of the arrays for idx 0.
1701 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001702
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001703 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1704 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001705#if SWAP_PER_DRAW
1706 glFlush();
1707 #if GR_MAC_BUILD
1708 aglSwapBuffers(aglGetCurrentContext());
1709 int set_a_break_pt_here = 9;
1710 aglSwapBuffers(aglGetCurrentContext());
1711 #elif GR_WIN32_BUILD
1712 SwapBuf();
1713 int set_a_break_pt_here = 9;
1714 SwapBuf();
1715 #endif
1716#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001717}
1718
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001719void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1720 uint32_t startVertex,
1721 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1723
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001724 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1725
1726 // our setupGeometry better have adjusted this to zero.
1727 // DrawElements doesn't take an offset so we always adjus the startVertex.
1728 GrAssert(0 == startVertex);
1729
1730 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1731 // account for startVertex in the DrawElements case. So we always
1732 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001733 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001734#if SWAP_PER_DRAW
1735 glFlush();
1736 #if GR_MAC_BUILD
1737 aglSwapBuffers(aglGetCurrentContext());
1738 int set_a_break_pt_here = 9;
1739 aglSwapBuffers(aglGetCurrentContext());
1740 #elif GR_WIN32_BUILD
1741 SwapBuf();
1742 int set_a_break_pt_here = 9;
1743 SwapBuf();
1744 #endif
1745#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001746}
1747
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001748namespace {
1749const GrStencilSettings& winding_nv_path_stencil_settings() {
1750 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1751 kIncClamp_StencilOp,
1752 kIncClamp_StencilOp,
1753 kAlwaysIfInClip_StencilFunc,
1754 ~0, ~0, ~0);
1755 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1756}
1757const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1758 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1759 kInvert_StencilOp,
1760 kInvert_StencilOp,
1761 kAlwaysIfInClip_StencilFunc,
1762 ~0, ~0, ~0);
1763 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1764}
1765}
1766
1767
1768void GrGpuGL::setStencilPathSettings(const GrPath&,
1769 GrPathFill fill,
1770 GrStencilSettings* settings) {
1771 switch (fill) {
1772 case kEvenOdd_GrPathFill:
1773 *settings = even_odd_nv_path_stencil_settings();
1774 return;
1775 case kWinding_GrPathFill:
1776 *settings = winding_nv_path_stencil_settings();
1777 return;
1778 default:
1779 GrCrash("Unexpected path fill.");
1780 }
1781}
1782
1783void GrGpuGL::onGpuStencilPath(const GrPath* path, GrPathFill fill) {
1784 GrAssert(fCaps.fPathStencilingSupport);
1785
1786 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1787 GrDrawState* drawState = this->drawState();
1788 GrAssert(NULL != drawState->getRenderTarget());
1789 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1790 return;
1791 }
1792
1793 // Decide how to manipulate the stencil buffer based on the fill rule.
1794 // Also, assert that the stencil settings we set in setStencilPathSettings
1795 // are present.
1796 GrAssert(!fStencilSettings.isTwoSided());
1797 GrGLenum fillMode;
1798 switch (fill) {
1799 case kWinding_GrPathFill:
1800 fillMode = GR_GL_COUNT_UP;
1801 GrAssert(kIncClamp_StencilOp ==
1802 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1803 GrAssert(kIncClamp_StencilOp ==
1804 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1805 break;
1806 case kEvenOdd_GrPathFill:
1807 fillMode = GR_GL_INVERT;
1808 GrAssert(kInvert_StencilOp ==
1809 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1810 GrAssert(kInvert_StencilOp ==
1811 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1812 break;
1813 default:
1814 // Only the above two fill rules are allowed.
1815 GrCrash("Unexpected path fill.");
1816 }
1817 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1818 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001819}
1820
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001821void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1822
1823 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001824
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001825 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001826 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001827 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001828 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1829 rt->renderFBOID()));
1830 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1831 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001832 // make sure we go through flushRenderTarget() since we've modified
1833 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001834 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001835 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001836 const GrIRect dirtyRect = rt->getResolveRect();
1837 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001838 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001839 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001840
bsalomon@google.coma3201942012-06-21 19:58:20 +00001841 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001842 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001843 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001844 asr.reset(&fScissorState);
1845 fScissorState.fEnabled = true;
1846 fScissorState.fRect = dirtyRect;
1847 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001848 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001849 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001850 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001851 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001852 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1853 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001854 asr.reset(&fScissorState);
1855 fScissorState.fEnabled = false;
1856 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001857 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001858 int right = r.fLeft + r.fWidth;
1859 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001860 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1861 r.fLeft, r.fBottom, right, top,
1862 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001863 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001864 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001865 }
1866}
1867
bsalomon@google.com411dad02012-06-05 20:24:20 +00001868namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869
bsalomon@google.com411dad02012-06-05 20:24:20 +00001870GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1871 static const GrGLenum gTable[] = {
1872 GR_GL_ALWAYS, // kAlways_StencilFunc
1873 GR_GL_NEVER, // kNever_StencilFunc
1874 GR_GL_GREATER, // kGreater_StencilFunc
1875 GR_GL_GEQUAL, // kGEqual_StencilFunc
1876 GR_GL_LESS, // kLess_StencilFunc
1877 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1878 GR_GL_EQUAL, // kEqual_StencilFunc,
1879 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1880 };
1881 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1882 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1883 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1884 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1885 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1886 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1887 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1888 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1889 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1890 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1891
1892 return gTable[basicFunc];
1893}
1894
1895GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1896 static const GrGLenum gTable[] = {
1897 GR_GL_KEEP, // kKeep_StencilOp
1898 GR_GL_REPLACE, // kReplace_StencilOp
1899 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1900 GR_GL_INCR, // kIncClamp_StencilOp
1901 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1902 GR_GL_DECR, // kDecClamp_StencilOp
1903 GR_GL_ZERO, // kZero_StencilOp
1904 GR_GL_INVERT, // kInvert_StencilOp
1905 };
1906 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1907 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1908 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1909 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1910 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1911 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1912 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1913 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1914 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1915 GrAssert((unsigned) op < kStencilOpCount);
1916 return gTable[op];
1917}
1918
1919void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001920 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001921 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001922 GrStencilSettings::Face grFace) {
1923 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1924 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1925 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1926
1927 GrGLint ref = settings.funcRef(grFace);
1928 GrGLint mask = settings.funcMask(grFace);
1929 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001930
1931 if (GR_GL_FRONT_AND_BACK == glFace) {
1932 // we call the combined func just in case separate stencil is not
1933 // supported.
1934 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1935 GR_GL_CALL(gl, StencilMask(writeMask));
1936 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1937 } else {
1938 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1939 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1940 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1941 }
1942}
1943}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001944
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001945void GrGpuGL::flushStencil(DrawType type) {
1946 if (kStencilPath_DrawType == type) {
1947 GrAssert(!fStencilSettings.isTwoSided());
1948 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1949 // that draws the path to the SB (glStencilFillPath)
1950 GrGLenum func =
1951 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1952 GL_CALL(PathStencilFunc(func,
1953 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1954 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1955 } else if (fHWStencilSettings != fStencilSettings) {
1956 if (fStencilSettings.isDisabled()) {
1957 if (kNo_TriState != fHWStencilTestEnabled) {
1958 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1959 fHWStencilTestEnabled = kNo_TriState;
1960 }
1961 } else {
1962 if (kYes_TriState != fHWStencilTestEnabled) {
1963 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1964 fHWStencilTestEnabled = kYes_TriState;
1965 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001966 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001967 if (!fStencilSettings.isDisabled()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001968 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001969 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001970 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001971 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001972 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001973 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001974 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001975 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001976 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001977 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001978 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001979 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001980 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001981 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001982 }
1983 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001984 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001985 }
1986}
1987
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001988void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001989 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001990 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001991 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1992 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001993 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001994 bool smoothLines = false;
1995
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001996 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001997 smoothLines = this->willUseHWAALines();
1998 if (smoothLines) {
1999 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
2000 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
2001 fHWAAState.fSmoothLineEnabled = kYes_TriState;
2002 // must disable msaa to use line smoothing
2003 if (rt->isMultisampled() &&
2004 kNo_TriState != fHWAAState.fMSAAEnabled) {
2005 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2006 fHWAAState.fMSAAEnabled = kNo_TriState;
2007 }
2008 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002009 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002010 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
2011 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
2012 fHWAAState.fSmoothLineEnabled = kNo_TriState;
2013 }
2014 }
2015 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002016 if (!smoothLines && rt->isMultisampled()) {
2017 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
2018 // convex hulls of each segment appear to get filled.
2019 bool enableMSAA = kStencilPath_DrawType == type ||
2020 this->getDrawState().isHWAntialiasState();
2021 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002022 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2023 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2024 fHWAAState.fMSAAEnabled = kYes_TriState;
2025 }
2026 } else {
2027 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2028 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2029 fHWAAState.fMSAAEnabled = kNo_TriState;
2030 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002031 }
2032 }
2033 }
2034}
2035
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002036void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002037 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002038 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002039 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002040 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002041 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002042 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002043 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002044 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2045 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2046 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2047 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2048 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2049 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002050 }
2051 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002052 // any optimization to disable blending should
2053 // have already been applied and tweaked the coeffs
2054 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002055 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2056 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002057 if (blendOff) {
2058 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002059 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002060 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002061 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002062 } else {
2063 if (kYes_TriState != fHWBlendState.fEnabled) {
2064 GL_CALL(Enable(GR_GL_BLEND));
2065 fHWBlendState.fEnabled = kYes_TriState;
2066 }
2067 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2068 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002069 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2070 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002071 fHWBlendState.fSrcCoeff = srcCoeff;
2072 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002073 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002074 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002075 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2076 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002077 (!fHWBlendState.fConstColorValid ||
2078 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002079
2080 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002081 GrColorUnpackR(blendConst) / 255.f,
2082 GrColorUnpackG(blendConst) / 255.f,
2083 GrColorUnpackB(blendConst) / 255.f,
2084 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002085 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002086 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002087 fHWBlendState.fConstColor = blendConst;
2088 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002089 }
2090 }
2091 }
2092}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002093namespace {
2094
2095unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002096 switch (filter) {
2097 case GrSamplerState::kBilinear_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002098 return GR_GL_LINEAR;
2099 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002100 return GR_GL_NEAREST;
2101 default:
2102 GrAssert(!"Unknown filter type");
2103 return GR_GL_LINEAR;
2104 }
2105}
2106
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002107// get_swizzle is only called from this .cpp so it is OK to inline it here
2108inline const GrGLenum* get_swizzle(GrPixelConfig config,
2109 const GrSamplerState& sampler,
2110 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002111 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002112 if (glCaps.textureRedSupport()) {
2113 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2114 GR_GL_RED, GR_GL_RED };
2115 return gRedSmear;
2116 } else {
2117 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2118 GR_GL_ALPHA, GR_GL_ALPHA };
2119 return gAlphaSmear;
2120 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002121 } else if (sampler.swapsRAndB()) {
2122 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2123 GR_GL_RED, GR_GL_ALPHA };
2124 return gRedBlueSwap;
2125 } else {
2126 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2127 GR_GL_BLUE, GR_GL_ALPHA };
2128 return gStraight;
2129 }
2130}
2131
2132void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002133 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2134 GR_GL_TEXTURE_SWIZZLE_RGBA,
2135 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002136}
2137}
2138
bsalomon@google.com4c883782012-06-04 19:05:11 +00002139void GrGpuGL::flushBoundTextureAndParams(int stage) {
2140 GrDrawState* drawState = this->drawState();
2141
2142 GrGLTexture* nextTexture =
2143 static_cast<GrGLTexture*>(drawState->getTexture(stage));
2144
2145 // true for now, but maybe not with GrEffect.
2146 GrAssert(NULL != nextTexture);
2147 // if we created a rt/tex and rendered to it without using a
2148 // texture and now we're texturing from the rt it will still be
2149 // the last bound texture, but it needs resolving. So keep this
2150 // out of the "last != next" check.
2151 GrGLRenderTarget* texRT =
2152 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2153 if (NULL != texRT) {
2154 this->onResolveRenderTarget(texRT);
2155 }
2156
2157 if (fHWBoundTextures[stage] != nextTexture) {
2158 this->setTextureUnit(stage);
2159 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002160 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2161 fHWBoundTextures[stage] = nextTexture;
2162 }
2163
2164 const GrSamplerState& sampler = drawState->getSampler(stage);
2165 ResetTimestamp timestamp;
2166 const GrGLTexture::TexParams& oldTexParams =
2167 nextTexture->getCachedTexParams(&timestamp);
2168 bool setAll = timestamp < this->getResetTimestamp();
2169 GrGLTexture::TexParams newTexParams;
2170
2171 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
2172
2173 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
2174 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2175 newTexParams.fWrapT = wraps[sampler.getWrapY()];
2176 memcpy(newTexParams.fSwizzleRGBA,
2177 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
2178 sizeof(newTexParams.fSwizzleRGBA));
2179 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2180 this->setTextureUnit(stage);
2181 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2182 GR_GL_TEXTURE_MAG_FILTER,
2183 newTexParams.fFilter));
2184 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2185 GR_GL_TEXTURE_MIN_FILTER,
2186 newTexParams.fFilter));
2187 }
2188 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2189 this->setTextureUnit(stage);
2190 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2191 GR_GL_TEXTURE_WRAP_S,
2192 newTexParams.fWrapS));
2193 }
2194 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2195 this->setTextureUnit(stage);
2196 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2197 GR_GL_TEXTURE_WRAP_T,
2198 newTexParams.fWrapT));
2199 }
2200 if (this->glCaps().textureSwizzleSupport() &&
2201 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2202 oldTexParams.fSwizzleRGBA,
2203 sizeof(newTexParams.fSwizzleRGBA)))) {
2204 this->setTextureUnit(stage);
2205 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2206 this->glInterface());
2207 }
2208 nextTexture->setCachedTexParams(newTexParams,
2209 this->getResetTimestamp());
2210}
2211
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002212void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002213
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002214 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002215
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002216 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002217 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002218 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002219 fHWDitherEnabled = kYes_TriState;
2220 }
2221 } else {
2222 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002223 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002224 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002225 }
2226 }
2227
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002228 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002229 if (kNo_TriState != fHWWriteToColor) {
2230 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2231 GR_GL_FALSE, GR_GL_FALSE));
2232 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002233 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002234 } else {
2235 if (kYes_TriState != fHWWriteToColor) {
2236 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2237 fHWWriteToColor = kYes_TriState;
2238 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002239 }
2240
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002241 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002242 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002243 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002244 GL_CALL(Enable(GR_GL_CULL_FACE));
2245 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002246 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002247 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002248 GL_CALL(Enable(GR_GL_CULL_FACE));
2249 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002250 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002251 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002252 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002253 break;
2254 default:
2255 GrCrash("Unknown draw face.");
2256 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002257 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002258 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002259}
2260
2261void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002262 if (fHWGeometryState.fVertexBuffer != buffer) {
2263 fHWGeometryState.fArrayPtrsDirty = true;
2264 fHWGeometryState.fVertexBuffer = buffer;
2265 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002266}
2267
2268void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002269 if (fHWGeometryState.fVertexBuffer == buffer) {
2270 // deleting bound buffer does implied bind to 0
2271 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002272 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002273 }
2274}
2275
2276void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002277 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002278}
2279
2280void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 if (fHWGeometryState.fIndexBuffer == buffer) {
2282 // deleting bound buffer does implied bind to 0
2283 fHWGeometryState.fIndexBuffer = NULL;
2284 }
2285}
2286
reed@google.comac10a2d2010-12-22 21:39:39 +00002287void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2288 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002289 if (fHWBoundRenderTarget == renderTarget) {
2290 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002291 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002292}
2293
2294void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002295 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002296 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002297 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002298 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002299 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002300 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002301}
2302
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002303bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2304 bool getSizedInternalFormat,
2305 GrGLenum* internalFormat,
2306 GrGLenum* externalFormat,
2307 GrGLenum* externalType) {
2308 GrGLenum dontCare;
2309 if (NULL == internalFormat) {
2310 internalFormat = &dontCare;
2311 }
2312 if (NULL == externalFormat) {
2313 externalFormat = &dontCare;
2314 }
2315 if (NULL == externalType) {
2316 externalType = &dontCare;
2317 }
2318
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002320 case kRGBA_8888_PM_GrPixelConfig:
2321 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002322 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002323 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002324 if (getSizedInternalFormat) {
2325 *internalFormat = GR_GL_RGBA8;
2326 } else {
2327 *internalFormat = GR_GL_RGBA;
2328 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002329 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002330 break;
2331 case kBGRA_8888_PM_GrPixelConfig:
2332 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002333 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002334 return false;
2335 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002336 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002337 if (getSizedInternalFormat) {
2338 *internalFormat = GR_GL_BGRA8;
2339 } else {
2340 *internalFormat = GR_GL_BGRA;
2341 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002342 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002343 if (getSizedInternalFormat) {
2344 *internalFormat = GR_GL_RGBA8;
2345 } else {
2346 *internalFormat = GR_GL_RGBA;
2347 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002348 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002349 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002350 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002351 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002352 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002353 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002354 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002355 if (getSizedInternalFormat) {
2356 if (this->glBinding() == kDesktop_GrGLBinding) {
2357 return false;
2358 } else {
2359 *internalFormat = GR_GL_RGB565;
2360 }
2361 } else {
2362 *internalFormat = GR_GL_RGB;
2363 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002364 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002365 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002366 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002367 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002368 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002369 if (getSizedInternalFormat) {
2370 *internalFormat = GR_GL_RGBA4;
2371 } else {
2372 *internalFormat = GR_GL_RGBA;
2373 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002374 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002375 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002376 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002377 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002378 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002379 // glCompressedTexImage doesn't take external params
2380 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002381 // no sized/unsized internal format distinction here
2382 *internalFormat = GR_GL_PALETTE8_RGBA8;
2383 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002384 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002385 } else {
2386 return false;
2387 }
2388 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002389 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002390 if (this->glCaps().textureRedSupport()) {
2391 *internalFormat = GR_GL_RED;
2392 *externalFormat = GR_GL_RED;
2393 if (getSizedInternalFormat) {
2394 *internalFormat = GR_GL_R8;
2395 } else {
2396 *internalFormat = GR_GL_RED;
2397 }
2398 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002399 } else {
2400 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002401 *externalFormat = GR_GL_ALPHA;
2402 if (getSizedInternalFormat) {
2403 *internalFormat = GR_GL_ALPHA8;
2404 } else {
2405 *internalFormat = GR_GL_ALPHA;
2406 }
2407 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002408 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002409 break;
2410 default:
2411 return false;
2412 }
2413 return true;
2414}
2415
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002416void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002417 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002418 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002419 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002420 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002421 }
2422}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002423
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002424void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002425 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002426 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002427 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002428 }
2429}
2430
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002431void GrGpuGL::setBuffers(bool indexed,
2432 int* extraVertexOffset,
2433 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002434
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002435 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002436
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002437 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2438
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002440 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002441 case kBuffer_GeometrySrcType:
2442 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002443 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002444 break;
2445 case kArray_GeometrySrcType:
2446 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002447 this->finalizeReservedVertices();
2448 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2449 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002450 break;
2451 default:
2452 vbuf = NULL; // suppress warning
2453 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002454 }
2455
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002456 GrAssert(NULL != vbuf);
2457 GrAssert(!vbuf->isLocked());
2458 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002459 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460 fHWGeometryState.fArrayPtrsDirty = true;
2461 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002462 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002463
2464 if (indexed) {
2465 GrAssert(NULL != extraIndexOffset);
2466
2467 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002468 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002469 case kBuffer_GeometrySrcType:
2470 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002471 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002472 break;
2473 case kArray_GeometrySrcType:
2474 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002475 this->finalizeReservedIndices();
2476 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2477 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002478 break;
2479 default:
2480 ibuf = NULL; // suppress warning
2481 GrCrash("Unknown geometry src type!");
2482 }
2483
2484 GrAssert(NULL != ibuf);
2485 GrAssert(!ibuf->isLocked());
2486 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002487 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002488 fHWGeometryState.fIndexBuffer = ibuf;
2489 }
2490 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002491}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002492