blob: 5baca24a3768aeaa5286e8e70d789b2c1321445d [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;
179 fProgramCache = new 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 }
595 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
596 } else {
597 texture = new GrGLTexture(this, glTexDesc);
598 }
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
621 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
622 if (desc.fStencilBits) {
623 GrGLStencilBuffer::Format format;
624 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
625 format.fPacked = false;
626 format.fStencilBits = desc.fStencilBits;
627 format.fTotalBits = desc.fStencilBits;
628 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
629 0,
630 desc.fWidth,
631 desc.fHeight,
632 desc.fSampleCnt,
633 format);
634 tgt->setStencilBuffer(sb);
635 sb->unref();
636 }
637 return tgt;
638}
639
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000640////////////////////////////////////////////////////////////////////////////////
641
bsalomon@google.com6f379512011-11-16 20:36:03 +0000642void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
643 int left, int top, int width, int height,
644 GrPixelConfig config, const void* buffer,
645 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000646 if (NULL == buffer) {
647 return;
648 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000649 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
650
bsalomon@google.com6f379512011-11-16 20:36:03 +0000651 this->setSpareTextureUnit();
652 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
653 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000654 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000655 desc.fWidth = glTex->width();
656 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000657 desc.fConfig = glTex->config();
658 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000659 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000660 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000661
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000662 this->uploadTexData(desc, false,
663 left, top, width, height,
664 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000665}
666
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000667namespace {
668bool adjust_pixel_ops_params(int surfaceWidth,
669 int surfaceHeight,
670 size_t bpp,
671 int* left, int* top, int* width, int* height,
672 const void** data,
673 size_t* rowBytes) {
674 if (!*rowBytes) {
675 *rowBytes = *width * bpp;
676 }
677
678 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
679 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
680
681 if (!subRect.intersect(bounds)) {
682 return false;
683 }
684 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
685 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
686
687 *left = subRect.fLeft;
688 *top = subRect.fTop;
689 *width = subRect.width();
690 *height = subRect.height();
691 return true;
692}
693}
694
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000695bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
696 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000697 int left, int top, int width, int height,
698 GrPixelConfig dataConfig,
699 const void* data,
700 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000701 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000702
703 size_t bpp = GrBytesPerPixel(dataConfig);
704 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
705 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000706 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000707 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000708 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000709
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000710 // in case we need a temporary, trimmed copy of the src pixels
711 SkAutoSMalloc<128 * 128> tempStorage;
712
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000713 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000714 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000715 if (useTexStorage) {
716 if (kDesktop_GrGLBinding == this->glBinding()) {
717 // 565 is not a sized internal format on desktop GL. So on desktop
718 // with 565 we always use an unsized internal format to let the
719 // system pick the best sized format to convert the 565 data to.
720 // Since glTexStorage only allows sized internal formats we will
721 // instead fallback to glTexImage2D.
722 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
723 } else {
724 // ES doesn't allow paletted textures to be used with tex storage
725 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
726 }
727 }
728
729 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000730 GrGLenum externalFormat;
731 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000732 // glTexStorage requires sized internal formats on both desktop and ES. ES
733 // glTexImage requires an unsized format.
734 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
735 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000736 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000737 }
738
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000739 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000740 // paletted textures cannot be updated
741 return false;
742 }
743
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000744 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000745 * check whether to allocate a temporary buffer for flipping y or
746 * because our srcData has extra bytes past each row. If so, we need
747 * to trim those off here, since GL ES may not let us specify
748 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000749 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000750 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000751 bool swFlipY = false;
752 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000753 if (NULL != data) {
754 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000755 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000756 glFlipY = true;
757 } else {
758 swFlipY = true;
759 }
760 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000761 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000762 // can't use this for flipping, only non-neg values allowed. :(
763 if (rowBytes != trimRowBytes) {
764 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
765 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
766 restoreGLRowLength = true;
767 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000768 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000769 if (trimRowBytes != rowBytes || swFlipY) {
770 // copy data into our new storage, skipping the trailing bytes
771 size_t trimSize = height * trimRowBytes;
772 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000773 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000774 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000775 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000776 char* dst = (char*)tempStorage.reset(trimSize);
777 for (int y = 0; y < height; y++) {
778 memcpy(dst, src, trimRowBytes);
779 if (swFlipY) {
780 src -= rowBytes;
781 } else {
782 src += rowBytes;
783 }
784 dst += trimRowBytes;
785 }
786 // now point data to our copied version
787 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000788 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000789 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000790 if (glFlipY) {
791 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
792 }
793 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000794 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000795 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000796 if (isNewTexture &&
797 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000798 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000799 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000800 if (useTexStorage) {
801 // We never resize or change formats of textures. We don't use
802 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000803 GL_ALLOC_CALL(this->glInterface(),
804 TexStorage2D(GR_GL_TEXTURE_2D,
805 1, // levels
806 internalFormat,
807 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000808 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000809 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
810 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
811 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000812 GL_ALLOC_CALL(this->glInterface(),
813 CompressedTexImage2D(GR_GL_TEXTURE_2D,
814 0, // level
815 internalFormat,
816 desc.fWidth, desc.fHeight,
817 0, // border
818 imageSize,
819 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000820 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000821 GL_ALLOC_CALL(this->glInterface(),
822 TexImage2D(GR_GL_TEXTURE_2D,
823 0, // level
824 internalFormat,
825 desc.fWidth, desc.fHeight,
826 0, // border
827 externalFormat, externalType,
828 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000829 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000830 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000831 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000832 if (error != GR_GL_NO_ERROR) {
833 succeeded = false;
834 } else {
835 // if we have data and we used TexStorage to create the texture, we
836 // now upload with TexSubImage.
837 if (NULL != data && useTexStorage) {
838 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
839 0, // level
840 left, top,
841 width, height,
842 externalFormat, externalType,
843 data));
844 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000845 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000847 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000848 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000849 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000850 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
851 0, // level
852 left, top,
853 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000854 externalFormat, externalType, data));
855 }
856
857 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000858 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000859 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000860 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000861 if (glFlipY) {
862 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
863 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000864 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000865}
866
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000867namespace {
868bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
869 int sampleCount,
870 GrGLenum format,
871 int width, int height) {
872 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
873 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
874 bool created = false;
875 if (GrGLCaps::kNVDesktop_CoverageAAType ==
876 ctxInfo.caps().coverageAAType()) {
877 const GrGLCaps::MSAACoverageMode& mode =
878 ctxInfo.caps().getMSAACoverageMode(sampleCount);
879 GL_ALLOC_CALL(ctxInfo.interface(),
880 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
881 mode.fCoverageSampleCnt,
882 mode.fColorSampleCnt,
883 format,
884 width, height));
885 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
886 }
887 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000888 // glRBMS will fail if requested samples is > max samples.
889 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000890 GL_ALLOC_CALL(ctxInfo.interface(),
891 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
892 sampleCount,
893 format,
894 width, height));
895 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
896 }
897 return created;
898}
899}
900
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000901bool GrGpuGL::createRenderTargetObjects(int width, int height,
902 GrGLuint texID,
903 GrGLRenderTarget::Desc* desc) {
904 desc->fMSColorRenderbufferID = 0;
905 desc->fRTFBOID = 0;
906 desc->fTexFBOID = 0;
907 desc->fOwnIDs = true;
908
909 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910
bsalomon@google.comab15d612011-08-09 12:57:56 +0000911 GrGLenum msColorFormat = 0; // suppress warning
912
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000913 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000914 if (!desc->fTexFBOID) {
915 goto FAILED;
916 }
917
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000918
919 // If we are using multisampling we will create two FBOS. We render
920 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000921 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000922 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000923 goto FAILED;
924 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000925 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
926 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000927 if (!desc->fRTFBOID ||
928 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000929 !this->configToGLFormats(desc->fConfig,
930 // GLES requires sized internal formats
931 kES2_GrGLBinding == this->glBinding(),
932 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000933 goto FAILED;
934 }
935 } else {
936 desc->fRTFBOID = desc->fTexFBOID;
937 }
938
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000939 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000940 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000941 if (desc->fRTFBOID != desc->fTexFBOID) {
942 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000943 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000944 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000945 if (!renderbuffer_storage_msaa(fGLContextInfo,
946 desc->fSampleCnt,
947 msColorFormat,
948 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000949 goto FAILED;
950 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000951 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
952 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000953 GR_GL_COLOR_ATTACHMENT0,
954 GR_GL_RENDERBUFFER,
955 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000956 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000957 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
958 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
959 goto FAILED;
960 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000961 fGLContextInfo.caps().markConfigAsValidColorAttachment(
962 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000963 }
964 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000966
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000967 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
968 GR_GL_COLOR_ATTACHMENT0,
969 GR_GL_TEXTURE_2D,
970 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000971 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000972 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
973 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
974 goto FAILED;
975 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000976 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000977 }
978
979 return true;
980
981FAILED:
982 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000983 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000984 }
985 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000986 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000987 }
988 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000989 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000990 }
991 return false;
992}
993
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000994// good to set a break-point here to know when createTexture fails
995static GrTexture* return_null_texture() {
996// GrAssert(!"null texture");
997 return NULL;
998}
999
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001000#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001001static size_t as_size_t(int x) {
1002 return x;
1003}
1004#endif
1005
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001006GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001007 const void* srcData,
1008 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001009
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001010 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001011 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001012
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001013 // Attempt to catch un- or wrongly initialized sample counts;
1014 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1015
robertphillips@google.com32716282012-06-04 12:48:45 +00001016 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +00001017 glTexDesc.fWidth = desc.fWidth;
1018 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001019 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +00001020 glTexDesc.fSampleCnt = desc.fSampleCnt;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001021 glTexDesc.fClientCacheID = desc.fClientCacheID;
robertphillips@google.com32716282012-06-04 12:48:45 +00001022
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001023 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001024
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001025 glRTDesc.fMSColorRenderbufferID = 0;
1026 glRTDesc.fRTFBOID = 0;
1027 glRTDesc.fTexFBOID = 0;
1028 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001029 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001030
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001031 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001032
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001033 const Caps& caps = this->getCaps();
1034
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001035 // We keep GrRenderTargets in GL's normal orientation so that they
1036 // can be drawn to by the outside world without the client having
1037 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001038 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001039 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001040
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001041 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001042 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001043 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001044 //GrPrintf("MSAA RT requested but not supported on this platform.");
1045 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 }
1047
reed@google.comac10a2d2010-12-22 21:39:39 +00001048 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001049 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1050 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001051 return return_null_texture();
1052 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001053 }
1054
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001055 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001056 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001057 // provides a hint about how this texture will be used
1058 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1059 GR_GL_TEXTURE_USAGE,
1060 GR_GL_FRAMEBUFFER_ATTACHMENT));
1061 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001062 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001063 return return_null_texture();
1064 }
1065
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001066 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001067 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001068
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001069 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1070 // drivers have a bug where an FBO won't be complete if it includes a
1071 // texture that is not mipmap complete (considering the filter in use).
1072 GrGLTexture::TexParams initialTexParams;
1073 // we only set a subset here so invalidate first
1074 initialTexParams.invalidate();
1075 initialTexParams.fFilter = GR_GL_NEAREST;
1076 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1077 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1079 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001080 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001081 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1082 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001083 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001084 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1085 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001086 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001087 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1088 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001089 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001090 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1091 glTexDesc.fWidth, glTexDesc.fHeight,
1092 desc.fConfig, srcData, rowBytes)) {
1093 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1094 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001095 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001096
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001098 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001099 // unbind the texture from the texture unit before binding it to the frame buffer
1100 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1101
bsalomon@google.com99621082011-11-15 16:47:16 +00001102 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1103 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001104 glTexDesc.fTextureID,
1105 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001106 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001107 return return_null_texture();
1108 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001109 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001111 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001112 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001113 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001114#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001115 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1116 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001117#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118 return tex;
1119}
1120
1121namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001122
1123const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1124
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001125void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1126 GrGLuint rb,
1127 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 // we shouldn't ever know one size and not the other
1129 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1130 (kUnknownBitCount == format->fTotalBits));
1131 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001132 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1134 (GrGLint*)&format->fStencilBits);
1135 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001136 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1138 (GrGLint*)&format->fTotalBits);
1139 format->fTotalBits += format->fStencilBits;
1140 } else {
1141 format->fTotalBits = format->fStencilBits;
1142 }
1143 }
1144}
1145}
1146
1147bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1148 int width, int height) {
1149
1150 // All internally created RTs are also textures. We don't create
1151 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1152 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001153 GrAssert(width >= rt->width());
1154 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155
1156 int samples = rt->numSamples();
1157 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001158 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 if (!sbID) {
1160 return false;
1161 }
1162
1163 GrGLStencilBuffer* sb = NULL;
1164
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001165 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001167 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 // we start with the last stencil format that succeeded in hopes
1169 // that we won't go through this loop more than once after the
1170 // first (painful) stencil creation.
1171 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001172 const GrGLCaps::StencilFormat& sFmt =
1173 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001174 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001175 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001177 bool created;
1178 if (samples > 0) {
1179 created = renderbuffer_storage_msaa(fGLContextInfo,
1180 samples,
1181 sFmt.fInternalFormat,
1182 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001184 GL_ALLOC_CALL(this->glInterface(),
1185 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001186 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001187 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001188 created =
1189 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001191 if (created) {
1192 // After sized formats we attempt an unsized format and take
1193 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001194 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001195 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001196 sb = new GrGLStencilBuffer(this, sbID, width, height,
1197 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1199 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001200 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 return true;
1203 }
1204 sb->abandon(); // otherwise we lose sbID
1205 sb->unref();
1206 }
1207 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001208 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001209 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210}
1211
1212bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1213 GrRenderTarget* rt) {
1214 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1215
1216 GrGLuint fbo = glrt->renderFBOID();
1217
1218 if (NULL == sb) {
1219 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 GR_GL_STENCIL_ATTACHMENT,
1222 GR_GL_RENDERBUFFER, 0));
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_DEPTH_ATTACHMENT,
1225 GR_GL_RENDERBUFFER, 0));
1226#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001227 GrGLenum status;
1228 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1230#endif
1231 }
1232 return true;
1233 } else {
1234 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1235 GrGLuint rb = glsb->renderbufferID();
1236
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001237 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001238 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1239 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 GR_GL_STENCIL_ATTACHMENT,
1241 GR_GL_RENDERBUFFER, rb));
1242 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001243 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 GR_GL_DEPTH_ATTACHMENT,
1245 GR_GL_RENDERBUFFER, rb));
1246 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001247 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248 GR_GL_DEPTH_ATTACHMENT,
1249 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001250 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001252 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001253 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001254 glsb->format())) {
1255 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1256 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001257 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001258 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001259 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001260 if (glsb->format().fPacked) {
1261 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1262 GR_GL_DEPTH_ATTACHMENT,
1263 GR_GL_RENDERBUFFER, 0));
1264 }
1265 return false;
1266 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001267 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001268 rt->config(),
1269 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001270 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001271 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001272 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001274}
1275
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001276////////////////////////////////////////////////////////////////////////////////
1277
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001278GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001279 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001280 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001282 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001283 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001284 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001286 GL_ALLOC_CALL(this->glInterface(),
1287 BufferData(GR_GL_ARRAY_BUFFER,
1288 size,
1289 NULL, // data ptr
1290 dynamic ? GR_GL_DYNAMIC_DRAW :
1291 GR_GL_STATIC_DRAW));
1292 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 // deleting bound buffer does implicit bind to 0
1295 fHWGeometryState.fVertexBuffer = NULL;
1296 return NULL;
1297 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001298 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 size, dynamic);
1300 fHWGeometryState.fVertexBuffer = vertexBuffer;
1301 return vertexBuffer;
1302 }
1303 return NULL;
1304}
1305
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001306GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001307 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001308 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001310 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001311 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001313 GL_ALLOC_CALL(this->glInterface(),
1314 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1315 size,
1316 NULL, // data ptr
1317 dynamic ? GR_GL_DYNAMIC_DRAW :
1318 GR_GL_STATIC_DRAW));
1319 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001320 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001321 // deleting bound buffer does implicit bind to 0
1322 fHWGeometryState.fIndexBuffer = NULL;
1323 return NULL;
1324 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001325 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 size, dynamic);
1327 fHWGeometryState.fIndexBuffer = indexBuffer;
1328 return indexBuffer;
1329 }
1330 return NULL;
1331}
1332
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001333GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001334 GrAssert(fCaps.fPathStencilingSupport);
1335 return new GrGLPath(this, inPath);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001336}
1337
bsalomon@google.coma3201942012-06-21 19:58:20 +00001338void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001339 const GrDrawState& drawState = this->getDrawState();
1340 const GrGLRenderTarget* rt =
1341 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1342
1343 GrAssert(NULL != rt);
1344 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001345
bsalomon@google.coma3201942012-06-21 19:58:20 +00001346 if (fScissorState.fEnabled) {
1347 GrGLIRect scissor;
1348 scissor.setRelativeTo(vp,
1349 fScissorState.fRect.fLeft,
1350 fScissorState.fRect.fTop,
1351 fScissorState.fRect.width(),
1352 fScissorState.fRect.height());
1353 // if the scissor fully contains the viewport then we fall through and
1354 // disable the scissor test.
1355 if (!scissor.contains(vp)) {
1356 if (fHWScissorSettings.fRect != scissor) {
1357 scissor.pushToGLScissor(this->glInterface());
1358 fHWScissorSettings.fRect = scissor;
1359 }
1360 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1361 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1362 fHWScissorSettings.fEnabled = kYes_TriState;
1363 }
1364 return;
1365 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001366 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001367 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001368 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001369 fHWScissorSettings.fEnabled = kNo_TriState;
1370 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001371 }
1372}
1373
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001374void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001375 const GrDrawState& drawState = this->getDrawState();
1376 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001377 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001378 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001379
bsalomon@google.com74b98712011-11-11 19:46:16 +00001380 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001381 if (NULL != rect) {
1382 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001383 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001384 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001385 if (clippedRect.intersect(rtRect)) {
1386 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001387 } else {
1388 return;
1389 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001390 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001391 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001392 GrAutoTRestore<ScissorState> asr(&fScissorState);
1393 fScissorState.fEnabled = (NULL != rect);
1394 if (fScissorState.fEnabled) {
1395 fScissorState.fRect = *rect;
1396 }
1397 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001398
1399 GrGLfloat r, g, b, a;
1400 static const GrGLfloat scale255 = 1.f / 255.f;
1401 a = GrColorUnpackA(color) * scale255;
1402 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001403 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001404 scaleRGB *= a;
1405 }
1406 r = GrColorUnpackR(color) * scaleRGB;
1407 g = GrColorUnpackG(color) * scaleRGB;
1408 b = GrColorUnpackB(color) * scaleRGB;
1409
1410 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001411 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001412 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001413 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001414}
1415
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001416void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001417 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001418 return;
1419 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001420
1421 this->flushRenderTarget(&GrIRect::EmptyIRect());
1422
bsalomon@google.coma3201942012-06-21 19:58:20 +00001423 GrAutoTRestore<ScissorState> asr(&fScissorState);
1424 fScissorState.fEnabled = false;
1425 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001426
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001427 GL_CALL(StencilMask(0xffffffff));
1428 GL_CALL(ClearStencil(0));
1429 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001430 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001431}
1432
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001433void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001434 const GrDrawState& drawState = this->getDrawState();
1435 const GrRenderTarget* rt = drawState.getRenderTarget();
1436 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001437
1438 // this should only be called internally when we know we have a
1439 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001440 GrAssert(NULL != rt->getStencilBuffer());
1441 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001442#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001443 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001444 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001445#else
1446 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001447 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001448 // turned into draws. Our contract on GrDrawTarget says that
1449 // changing the clip between stencil passes may or may not
1450 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001451 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001452#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001453 GrGLint value;
1454 if (insideClip) {
1455 value = (1 << (stencilBitCount - 1));
1456 } else {
1457 value = 0;
1458 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001459 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001460
1461 GrAutoTRestore<ScissorState> asr(&fScissorState);
1462 fScissorState.fEnabled = true;
1463 fScissorState.fRect = rect;
1464 this->flushScissor();
1465
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001466 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001467 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001468 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001469 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001470}
1471
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001472void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001473 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001474}
1475
bsalomon@google.comc4364992011-11-07 15:54:49 +00001476bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1477 int left, int top,
1478 int width, int height,
1479 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001480 size_t rowBytes) const {
1481 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001482 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001483 return false;
1484 }
1485
1486 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001487 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001488 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001489 return true;
1490 }
1491 // If we have to do memcpys to handle rowBytes then y-flip is free
1492 // Note the rowBytes might be tight to the passed in data, but if data
1493 // gets clipped in x to the target the rowBytes will no longer be tight.
1494 if (left >= 0 && (left + width) < renderTarget->width()) {
1495 return 0 == rowBytes ||
1496 GrBytesPerPixel(config) * width == rowBytes;
1497 } else {
1498 return false;
1499 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001500}
1501
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001502bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001503 int left, int top,
1504 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001505 GrPixelConfig config,
1506 void* buffer,
1507 size_t rowBytes,
1508 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001509 GrGLenum format;
1510 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001511 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001512 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001513 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001514 size_t bpp = GrBytesPerPixel(config);
1515 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1516 &left, &top, &width, &height,
1517 const_cast<const void**>(&buffer),
1518 &rowBytes)) {
1519 return false;
1520 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001521
bsalomon@google.comc6980972011-11-02 19:57:21 +00001522 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001523 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001524 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001525 switch (tgt->getResolveType()) {
1526 case GrGLRenderTarget::kCantResolve_ResolveType:
1527 return false;
1528 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001529 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001530 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001531 break;
1532 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001533 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001534 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001535 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1536 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001537 break;
1538 default:
1539 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001540 }
1541
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001542 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001543
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001544 // the read rect is viewport-relative
1545 GrGLIRect readRect;
1546 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001547
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001548 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001549 if (0 == rowBytes) {
1550 rowBytes = tightRowBytes;
1551 }
1552 size_t readDstRowBytes = tightRowBytes;
1553 void* readDst = buffer;
1554
1555 // determine if GL can read using the passed rowBytes or if we need
1556 // a scratch buffer.
1557 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1558 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001559 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001560 GrAssert(!(rowBytes % sizeof(GrColor)));
1561 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1562 readDstRowBytes = rowBytes;
1563 } else {
1564 scratch.reset(tightRowBytes * height);
1565 readDst = scratch.get();
1566 }
1567 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001568 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001569 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1570 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001571 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1572 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001573 format, type, readDst));
1574 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001575 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001576 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1577 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001578 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001579 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1580 invertY = true;
1581 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001582
1583 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001584 // API presents top-to-bottom. We must preserve the padding contents. Note
1585 // that the above readPixels did not overwrite the padding.
1586 if (readDst == buffer) {
1587 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001588 if (!invertY) {
1589 scratch.reset(tightRowBytes);
1590 void* tmpRow = scratch.get();
1591 // flip y in-place by rows
1592 const int halfY = height >> 1;
1593 char* top = reinterpret_cast<char*>(buffer);
1594 char* bottom = top + (height - 1) * rowBytes;
1595 for (int y = 0; y < halfY; y++) {
1596 memcpy(tmpRow, top, tightRowBytes);
1597 memcpy(top, bottom, tightRowBytes);
1598 memcpy(bottom, tmpRow, tightRowBytes);
1599 top += rowBytes;
1600 bottom -= rowBytes;
1601 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001602 }
1603 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001604 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001605 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001606 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001607 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001608 char* dst = reinterpret_cast<char*>(buffer);
1609 if (!invertY) {
1610 dst += (height-1) * rowBytes;
1611 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001612 for (int y = 0; y < height; y++) {
1613 memcpy(dst, src, tightRowBytes);
1614 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001615 if (invertY) {
1616 dst += rowBytes;
1617 } else {
1618 dst -= rowBytes;
1619 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 }
1621 }
1622 return true;
1623}
1624
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001625void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001626
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001627 GrGLRenderTarget* rt =
1628 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1629 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001630
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001631 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001632 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001633 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001634 GrGLenum status;
1635 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001636 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001637 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001638 }
1639 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001640 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001641 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001642 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001643 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001644 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001645 }
1646 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001647 if (NULL == bound || !bound->isEmpty()) {
1648 rt->flagAsNeedingResolve(bound);
1649 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001650}
1651
twiz@google.com0f31ca72011-03-18 17:38:11 +00001652GrGLenum gPrimitiveType2GLMode[] = {
1653 GR_GL_TRIANGLES,
1654 GR_GL_TRIANGLE_STRIP,
1655 GR_GL_TRIANGLE_FAN,
1656 GR_GL_POINTS,
1657 GR_GL_LINES,
1658 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001659};
1660
bsalomon@google.comd302f142011-03-03 13:54:13 +00001661#define SWAP_PER_DRAW 0
1662
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001663#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001664 #if GR_MAC_BUILD
1665 #include <AGL/agl.h>
1666 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001667 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001668 void SwapBuf() {
1669 DWORD procID = GetCurrentProcessId();
1670 HWND hwnd = GetTopWindow(GetDesktopWindow());
1671 while(hwnd) {
1672 DWORD wndProcID = 0;
1673 GetWindowThreadProcessId(hwnd, &wndProcID);
1674 if(wndProcID == procID) {
1675 SwapBuffers(GetDC(hwnd));
1676 }
1677 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1678 }
1679 }
1680 #endif
1681#endif
1682
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001683void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1684 uint32_t startVertex,
1685 uint32_t startIndex,
1686 uint32_t vertexCount,
1687 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001688 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1689
twiz@google.com0f31ca72011-03-18 17:38:11 +00001690 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001691
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001692 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1693 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1694
1695 // our setupGeometry better have adjusted this to zero since
1696 // DrawElements always draws from the begining of the arrays for idx 0.
1697 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001698
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001699 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1700 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001701#if SWAP_PER_DRAW
1702 glFlush();
1703 #if GR_MAC_BUILD
1704 aglSwapBuffers(aglGetCurrentContext());
1705 int set_a_break_pt_here = 9;
1706 aglSwapBuffers(aglGetCurrentContext());
1707 #elif GR_WIN32_BUILD
1708 SwapBuf();
1709 int set_a_break_pt_here = 9;
1710 SwapBuf();
1711 #endif
1712#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001713}
1714
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001715void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1716 uint32_t startVertex,
1717 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001718 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1719
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001720 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1721
1722 // our setupGeometry better have adjusted this to zero.
1723 // DrawElements doesn't take an offset so we always adjus the startVertex.
1724 GrAssert(0 == startVertex);
1725
1726 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1727 // account for startVertex in the DrawElements case. So we always
1728 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001729 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001730#if SWAP_PER_DRAW
1731 glFlush();
1732 #if GR_MAC_BUILD
1733 aglSwapBuffers(aglGetCurrentContext());
1734 int set_a_break_pt_here = 9;
1735 aglSwapBuffers(aglGetCurrentContext());
1736 #elif GR_WIN32_BUILD
1737 SwapBuf();
1738 int set_a_break_pt_here = 9;
1739 SwapBuf();
1740 #endif
1741#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001742}
1743
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001744namespace {
1745const GrStencilSettings& winding_nv_path_stencil_settings() {
1746 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1747 kIncClamp_StencilOp,
1748 kIncClamp_StencilOp,
1749 kAlwaysIfInClip_StencilFunc,
1750 ~0, ~0, ~0);
1751 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1752}
1753const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1754 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1755 kInvert_StencilOp,
1756 kInvert_StencilOp,
1757 kAlwaysIfInClip_StencilFunc,
1758 ~0, ~0, ~0);
1759 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1760}
1761}
1762
1763
1764void GrGpuGL::setStencilPathSettings(const GrPath&,
1765 GrPathFill fill,
1766 GrStencilSettings* settings) {
1767 switch (fill) {
1768 case kEvenOdd_GrPathFill:
1769 *settings = even_odd_nv_path_stencil_settings();
1770 return;
1771 case kWinding_GrPathFill:
1772 *settings = winding_nv_path_stencil_settings();
1773 return;
1774 default:
1775 GrCrash("Unexpected path fill.");
1776 }
1777}
1778
1779void GrGpuGL::onGpuStencilPath(const GrPath* path, GrPathFill fill) {
1780 GrAssert(fCaps.fPathStencilingSupport);
1781
1782 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1783 GrDrawState* drawState = this->drawState();
1784 GrAssert(NULL != drawState->getRenderTarget());
1785 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1786 return;
1787 }
1788
1789 // Decide how to manipulate the stencil buffer based on the fill rule.
1790 // Also, assert that the stencil settings we set in setStencilPathSettings
1791 // are present.
1792 GrAssert(!fStencilSettings.isTwoSided());
1793 GrGLenum fillMode;
1794 switch (fill) {
1795 case kWinding_GrPathFill:
1796 fillMode = GR_GL_COUNT_UP;
1797 GrAssert(kIncClamp_StencilOp ==
1798 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1799 GrAssert(kIncClamp_StencilOp ==
1800 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1801 break;
1802 case kEvenOdd_GrPathFill:
1803 fillMode = GR_GL_INVERT;
1804 GrAssert(kInvert_StencilOp ==
1805 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1806 GrAssert(kInvert_StencilOp ==
1807 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1808 break;
1809 default:
1810 // Only the above two fill rules are allowed.
1811 GrCrash("Unexpected path fill.");
1812 }
1813 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1814 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001815}
1816
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001817void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1818
1819 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001820
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001821 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001822 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001823 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001824 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1825 rt->renderFBOID()));
1826 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1827 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001828 // make sure we go through flushRenderTarget() since we've modified
1829 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001830 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001831 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001832 const GrIRect dirtyRect = rt->getResolveRect();
1833 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001834 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001835 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001836
bsalomon@google.coma3201942012-06-21 19:58:20 +00001837 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001838 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001839 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001840 asr.reset(&fScissorState);
1841 fScissorState.fEnabled = true;
1842 fScissorState.fRect = dirtyRect;
1843 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001844 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001845 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001846 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001847 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001848 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1849 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001850 asr.reset(&fScissorState);
1851 fScissorState.fEnabled = false;
1852 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001853 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001854 int right = r.fLeft + r.fWidth;
1855 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001856 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1857 r.fLeft, r.fBottom, right, top,
1858 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001859 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001860 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001861 }
1862}
1863
bsalomon@google.com411dad02012-06-05 20:24:20 +00001864namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001865
bsalomon@google.com411dad02012-06-05 20:24:20 +00001866GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1867 static const GrGLenum gTable[] = {
1868 GR_GL_ALWAYS, // kAlways_StencilFunc
1869 GR_GL_NEVER, // kNever_StencilFunc
1870 GR_GL_GREATER, // kGreater_StencilFunc
1871 GR_GL_GEQUAL, // kGEqual_StencilFunc
1872 GR_GL_LESS, // kLess_StencilFunc
1873 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1874 GR_GL_EQUAL, // kEqual_StencilFunc,
1875 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1876 };
1877 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1878 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1879 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1880 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1881 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1882 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1883 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1884 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1885 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1886 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1887
1888 return gTable[basicFunc];
1889}
1890
1891GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1892 static const GrGLenum gTable[] = {
1893 GR_GL_KEEP, // kKeep_StencilOp
1894 GR_GL_REPLACE, // kReplace_StencilOp
1895 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1896 GR_GL_INCR, // kIncClamp_StencilOp
1897 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1898 GR_GL_DECR, // kDecClamp_StencilOp
1899 GR_GL_ZERO, // kZero_StencilOp
1900 GR_GL_INVERT, // kInvert_StencilOp
1901 };
1902 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1903 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1904 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1905 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1906 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1907 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1908 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1909 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1910 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1911 GrAssert((unsigned) op < kStencilOpCount);
1912 return gTable[op];
1913}
1914
1915void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001916 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001917 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001918 GrStencilSettings::Face grFace) {
1919 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1920 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1921 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1922
1923 GrGLint ref = settings.funcRef(grFace);
1924 GrGLint mask = settings.funcMask(grFace);
1925 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001926
1927 if (GR_GL_FRONT_AND_BACK == glFace) {
1928 // we call the combined func just in case separate stencil is not
1929 // supported.
1930 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1931 GR_GL_CALL(gl, StencilMask(writeMask));
1932 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1933 } else {
1934 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1935 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1936 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1937 }
1938}
1939}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001940
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001941void GrGpuGL::flushStencil(DrawType type) {
1942 if (kStencilPath_DrawType == type) {
1943 GrAssert(!fStencilSettings.isTwoSided());
1944 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1945 // that draws the path to the SB (glStencilFillPath)
1946 GrGLenum func =
1947 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1948 GL_CALL(PathStencilFunc(func,
1949 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1950 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1951 } else if (fHWStencilSettings != fStencilSettings) {
1952 if (fStencilSettings.isDisabled()) {
1953 if (kNo_TriState != fHWStencilTestEnabled) {
1954 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1955 fHWStencilTestEnabled = kNo_TriState;
1956 }
1957 } else {
1958 if (kYes_TriState != fHWStencilTestEnabled) {
1959 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1960 fHWStencilTestEnabled = kYes_TriState;
1961 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001962 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001963 if (!fStencilSettings.isDisabled()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001964 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001965 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001966 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001967 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001968 GrStencilSettings::kFront_Face);
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_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001972 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001973 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001974 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001975 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001976 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001977 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001978 }
1979 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001980 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001981 }
1982}
1983
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001984void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001985 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001986 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001987 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1988 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001989 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001990 bool smoothLines = false;
1991
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001992 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001993 smoothLines = this->willUseHWAALines();
1994 if (smoothLines) {
1995 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1996 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1997 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1998 // must disable msaa to use line smoothing
1999 if (rt->isMultisampled() &&
2000 kNo_TriState != fHWAAState.fMSAAEnabled) {
2001 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2002 fHWAAState.fMSAAEnabled = kNo_TriState;
2003 }
2004 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002005 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002006 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
2007 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
2008 fHWAAState.fSmoothLineEnabled = kNo_TriState;
2009 }
2010 }
2011 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002012 if (!smoothLines && rt->isMultisampled()) {
2013 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
2014 // convex hulls of each segment appear to get filled.
2015 bool enableMSAA = kStencilPath_DrawType == type ||
2016 this->getDrawState().isHWAntialiasState();
2017 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002018 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2019 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2020 fHWAAState.fMSAAEnabled = kYes_TriState;
2021 }
2022 } else {
2023 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2024 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2025 fHWAAState.fMSAAEnabled = kNo_TriState;
2026 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002027 }
2028 }
2029 }
2030}
2031
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002032void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002033 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002034 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002035 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002036 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002037 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002038 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002039 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002040 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2041 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2042 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2043 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2044 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2045 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002046 }
2047 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002048 // any optimization to disable blending should
2049 // have already been applied and tweaked the coeffs
2050 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002051 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2052 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002053 if (blendOff) {
2054 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002055 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002056 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002057 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002058 } else {
2059 if (kYes_TriState != fHWBlendState.fEnabled) {
2060 GL_CALL(Enable(GR_GL_BLEND));
2061 fHWBlendState.fEnabled = kYes_TriState;
2062 }
2063 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2064 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002065 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2066 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002067 fHWBlendState.fSrcCoeff = srcCoeff;
2068 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002069 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002070 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002071 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2072 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002073 (!fHWBlendState.fConstColorValid ||
2074 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002075
2076 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002077 GrColorUnpackR(blendConst) / 255.f,
2078 GrColorUnpackG(blendConst) / 255.f,
2079 GrColorUnpackB(blendConst) / 255.f,
2080 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002081 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002082 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002083 fHWBlendState.fConstColor = blendConst;
2084 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002085 }
2086 }
2087 }
2088}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002089namespace {
2090
2091unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002092 switch (filter) {
2093 case GrSamplerState::kBilinear_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002094 return GR_GL_LINEAR;
2095 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002096 return GR_GL_NEAREST;
2097 default:
2098 GrAssert(!"Unknown filter type");
2099 return GR_GL_LINEAR;
2100 }
2101}
2102
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002103// get_swizzle is only called from this .cpp so it is OK to inline it here
2104inline const GrGLenum* get_swizzle(GrPixelConfig config,
2105 const GrSamplerState& sampler,
2106 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002107 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002108 if (glCaps.textureRedSupport()) {
2109 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2110 GR_GL_RED, GR_GL_RED };
2111 return gRedSmear;
2112 } else {
2113 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2114 GR_GL_ALPHA, GR_GL_ALPHA };
2115 return gAlphaSmear;
2116 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002117 } else if (sampler.swapsRAndB()) {
2118 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2119 GR_GL_RED, GR_GL_ALPHA };
2120 return gRedBlueSwap;
2121 } else {
2122 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2123 GR_GL_BLUE, GR_GL_ALPHA };
2124 return gStraight;
2125 }
2126}
2127
2128void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002129 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2130 GR_GL_TEXTURE_SWIZZLE_RGBA,
2131 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002132}
2133}
2134
bsalomon@google.com4c883782012-06-04 19:05:11 +00002135void GrGpuGL::flushBoundTextureAndParams(int stage) {
2136 GrDrawState* drawState = this->drawState();
2137
2138 GrGLTexture* nextTexture =
2139 static_cast<GrGLTexture*>(drawState->getTexture(stage));
2140
2141 // true for now, but maybe not with GrEffect.
2142 GrAssert(NULL != nextTexture);
2143 // if we created a rt/tex and rendered to it without using a
2144 // texture and now we're texturing from the rt it will still be
2145 // the last bound texture, but it needs resolving. So keep this
2146 // out of the "last != next" check.
2147 GrGLRenderTarget* texRT =
2148 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2149 if (NULL != texRT) {
2150 this->onResolveRenderTarget(texRT);
2151 }
2152
2153 if (fHWBoundTextures[stage] != nextTexture) {
2154 this->setTextureUnit(stage);
2155 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002156 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2157 fHWBoundTextures[stage] = nextTexture;
2158 }
2159
2160 const GrSamplerState& sampler = drawState->getSampler(stage);
2161 ResetTimestamp timestamp;
2162 const GrGLTexture::TexParams& oldTexParams =
2163 nextTexture->getCachedTexParams(&timestamp);
2164 bool setAll = timestamp < this->getResetTimestamp();
2165 GrGLTexture::TexParams newTexParams;
2166
2167 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
2168
2169 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
2170 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2171 newTexParams.fWrapT = wraps[sampler.getWrapY()];
2172 memcpy(newTexParams.fSwizzleRGBA,
2173 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
2174 sizeof(newTexParams.fSwizzleRGBA));
2175 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2176 this->setTextureUnit(stage);
2177 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2178 GR_GL_TEXTURE_MAG_FILTER,
2179 newTexParams.fFilter));
2180 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2181 GR_GL_TEXTURE_MIN_FILTER,
2182 newTexParams.fFilter));
2183 }
2184 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2185 this->setTextureUnit(stage);
2186 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2187 GR_GL_TEXTURE_WRAP_S,
2188 newTexParams.fWrapS));
2189 }
2190 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2191 this->setTextureUnit(stage);
2192 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2193 GR_GL_TEXTURE_WRAP_T,
2194 newTexParams.fWrapT));
2195 }
2196 if (this->glCaps().textureSwizzleSupport() &&
2197 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2198 oldTexParams.fSwizzleRGBA,
2199 sizeof(newTexParams.fSwizzleRGBA)))) {
2200 this->setTextureUnit(stage);
2201 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2202 this->glInterface());
2203 }
2204 nextTexture->setCachedTexParams(newTexParams,
2205 this->getResetTimestamp());
2206}
2207
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002208void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002209
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002210 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002211
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002212 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002213 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002214 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002215 fHWDitherEnabled = kYes_TriState;
2216 }
2217 } else {
2218 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002220 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002221 }
2222 }
2223
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002224 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002225 if (kNo_TriState != fHWWriteToColor) {
2226 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2227 GR_GL_FALSE, GR_GL_FALSE));
2228 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002229 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002230 } else {
2231 if (kYes_TriState != fHWWriteToColor) {
2232 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2233 fHWWriteToColor = kYes_TriState;
2234 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002235 }
2236
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002237 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002238 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002239 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002240 GL_CALL(Enable(GR_GL_CULL_FACE));
2241 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002242 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002243 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002244 GL_CALL(Enable(GR_GL_CULL_FACE));
2245 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002246 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002247 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002248 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002249 break;
2250 default:
2251 GrCrash("Unknown draw face.");
2252 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002253 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002255}
2256
2257void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002258 if (fHWGeometryState.fVertexBuffer != buffer) {
2259 fHWGeometryState.fArrayPtrsDirty = true;
2260 fHWGeometryState.fVertexBuffer = buffer;
2261 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002262}
2263
2264void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002265 if (fHWGeometryState.fVertexBuffer == buffer) {
2266 // deleting bound buffer does implied bind to 0
2267 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002268 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002269 }
2270}
2271
2272void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002273 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002274}
2275
2276void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002277 if (fHWGeometryState.fIndexBuffer == buffer) {
2278 // deleting bound buffer does implied bind to 0
2279 fHWGeometryState.fIndexBuffer = NULL;
2280 }
2281}
2282
reed@google.comac10a2d2010-12-22 21:39:39 +00002283void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2284 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002285 if (fHWBoundRenderTarget == renderTarget) {
2286 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002287 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002288}
2289
2290void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002291 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002292 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002293 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002294 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002296 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002297}
2298
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002299bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2300 bool getSizedInternalFormat,
2301 GrGLenum* internalFormat,
2302 GrGLenum* externalFormat,
2303 GrGLenum* externalType) {
2304 GrGLenum dontCare;
2305 if (NULL == internalFormat) {
2306 internalFormat = &dontCare;
2307 }
2308 if (NULL == externalFormat) {
2309 externalFormat = &dontCare;
2310 }
2311 if (NULL == externalType) {
2312 externalType = &dontCare;
2313 }
2314
reed@google.comac10a2d2010-12-22 21:39:39 +00002315 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002316 case kRGBA_8888_PM_GrPixelConfig:
2317 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002318 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002319 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002320 if (getSizedInternalFormat) {
2321 *internalFormat = GR_GL_RGBA8;
2322 } else {
2323 *internalFormat = GR_GL_RGBA;
2324 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002325 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002326 break;
2327 case kBGRA_8888_PM_GrPixelConfig:
2328 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002329 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002330 return false;
2331 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002332 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002333 if (getSizedInternalFormat) {
2334 *internalFormat = GR_GL_BGRA8;
2335 } else {
2336 *internalFormat = GR_GL_BGRA;
2337 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002338 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002339 if (getSizedInternalFormat) {
2340 *internalFormat = GR_GL_RGBA8;
2341 } else {
2342 *internalFormat = GR_GL_RGBA;
2343 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002344 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002345 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002346 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002347 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002348 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002349 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002350 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002351 if (getSizedInternalFormat) {
2352 if (this->glBinding() == kDesktop_GrGLBinding) {
2353 return false;
2354 } else {
2355 *internalFormat = GR_GL_RGB565;
2356 }
2357 } else {
2358 *internalFormat = GR_GL_RGB;
2359 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002360 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002361 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002362 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002363 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002364 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002365 if (getSizedInternalFormat) {
2366 *internalFormat = GR_GL_RGBA4;
2367 } else {
2368 *internalFormat = GR_GL_RGBA;
2369 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002370 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002371 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002372 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002373 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002374 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002375 // glCompressedTexImage doesn't take external params
2376 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002377 // no sized/unsized internal format distinction here
2378 *internalFormat = GR_GL_PALETTE8_RGBA8;
2379 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002380 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002381 } else {
2382 return false;
2383 }
2384 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002385 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002386 if (this->glCaps().textureRedSupport()) {
2387 *internalFormat = GR_GL_RED;
2388 *externalFormat = GR_GL_RED;
2389 if (getSizedInternalFormat) {
2390 *internalFormat = GR_GL_R8;
2391 } else {
2392 *internalFormat = GR_GL_RED;
2393 }
2394 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002395 } else {
2396 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002397 *externalFormat = GR_GL_ALPHA;
2398 if (getSizedInternalFormat) {
2399 *internalFormat = GR_GL_ALPHA8;
2400 } else {
2401 *internalFormat = GR_GL_ALPHA;
2402 }
2403 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002404 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002405 break;
2406 default:
2407 return false;
2408 }
2409 return true;
2410}
2411
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002412void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002413 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002414 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002415 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002416 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002417 }
2418}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002419
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002420void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002421 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002422 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002423 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002424 }
2425}
2426
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002427void GrGpuGL::setBuffers(bool indexed,
2428 int* extraVertexOffset,
2429 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002430
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002431 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002432
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002433 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2434
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002435 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002436 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002437 case kBuffer_GeometrySrcType:
2438 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002439 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002440 break;
2441 case kArray_GeometrySrcType:
2442 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002443 this->finalizeReservedVertices();
2444 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2445 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002446 break;
2447 default:
2448 vbuf = NULL; // suppress warning
2449 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002450 }
2451
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002452 GrAssert(NULL != vbuf);
2453 GrAssert(!vbuf->isLocked());
2454 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002455 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002456 fHWGeometryState.fArrayPtrsDirty = true;
2457 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002458 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459
2460 if (indexed) {
2461 GrAssert(NULL != extraIndexOffset);
2462
2463 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002464 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002465 case kBuffer_GeometrySrcType:
2466 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002467 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002468 break;
2469 case kArray_GeometrySrcType:
2470 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002471 this->finalizeReservedIndices();
2472 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2473 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002474 break;
2475 default:
2476 ibuf = NULL; // suppress warning
2477 GrCrash("Unknown geometry src type!");
2478 }
2479
2480 GrAssert(NULL != ibuf);
2481 GrAssert(!ibuf->isLocked());
2482 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002483 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002484 fHWGeometryState.fIndexBuffer = ibuf;
2485 }
2486 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002487}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002488