blob: 8e6e43c7b5326c626b4e5c56a64eca534eec4b6f [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)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000033 #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
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000178 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContextInfo()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000179
bsalomon@google.comfe676522011-06-17 18:12:21 +0000180 fLastSuccessfulStencilFmtIdx = 0;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000181 if (false) { // avoid bit rot, suppress warning
182 fbo_test(this->glInterface(), 0, 0);
183 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000184}
185
186GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000187 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000188 // detach the current program so there is no confusion on OpenGL's part
189 // that we want it to be deleted
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000190 GrAssert(fHWProgramID == fCurrentProgram->fProgramID);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000191 GL_CALL(UseProgram(0));
192 }
193
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000194 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000195
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000196 // This must be called by before the GrDrawTarget destructor
197 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000198 // This subclass must do this before the base class destructor runs
199 // since we will unref the GrGLInterface.
200 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000201}
202
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000203///////////////////////////////////////////////////////////////////////////////
204
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000205void GrGpuGL::initCaps() {
206 GrGLint maxTextureUnits;
207 // check FS and fixed-function texture unit limits
208 // we only use textures in the fragment stage currently.
209 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000210 const GrGLInterface* gl = this->glInterface();
211 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000212 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000213
214 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000215 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000216 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000217 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000218 for (int i = 0; i < numFormats; ++i) {
219 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
220 fCaps.f8BitPaletteSupport = true;
221 break;
222 }
223 }
224
225 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000226 // we could also look for GL_ATI_separate_stencil extension or
227 // GL_EXT_stencil_two_side but they use different function signatures
228 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000229 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000230 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000231 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000232 this->hasExtension("GL_EXT_stencil_wrap");
233 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000234 // ES 2 has two sided stencil and stencil wrap
235 fCaps.fTwoSidedStencilSupport = true;
236 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 }
238
239 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000240 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
241 // extension includes glMapBuffer.
242 } else {
243 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
244 }
245
246 if (kDesktop_GrGLBinding == this->glBinding()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000247 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000248 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
249 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 } else {
251 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000252 }
253 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000254 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000255 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000256 }
257
258 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
259
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000260 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
261 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 // Our render targets are always created with textures as the color
263 // attachment, hence this min:
264 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
265
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000266 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000267 fCaps.fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
268 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000269
270 // Enable supported shader-related caps
271 if (kDesktop_GrGLBinding == this->glBinding()) {
272 fCaps.fDualSourceBlendingSupport =
273 this->glVersion() >= GR_GL_VER(3,3) ||
274 this->hasExtension("GL_ARB_blend_func_extended");
275 fCaps.fShaderDerivativeSupport = true;
276 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000277 fCaps.fGeometryShaderSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000278 this->glVersion() >= GR_GL_VER(3,2) &&
279 this->glslGeneration() >= k150_GrGLSLGeneration;
280 } else {
281 fCaps.fShaderDerivativeSupport =
282 this->hasExtension("GL_OES_standard_derivatives");
283 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000284}
285
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000286void GrGpuGL::fillInConfigRenderableTable() {
287
288 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000289 // no support for render targets unless the GL_ARB_framebuffer_object
290 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
291 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000292 // probably don't get R8 in this case.
293
294 // OpenGL 3.0
295 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
296 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
297
298 // >= OpenGL 3.1
299 // base color renderable: RED, RG, RGB, and RGBA
300 // sized derivatives: R8, RGBA4, RGBA8
301 // if the GL_ARB_compatibility extension is supported then we get back
302 // support for GL_ALPHA and ALPHA8
303
304 // GL_EXT_bgra adds BGRA render targets to any version
305
306 // ES 2.0
307 // color renderable: RGBA4, RGB5_A1, RGB565
308 // GL_EXT_texture_rg adds support for R8 as a color render target
309 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
310 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
311 // added BGRA support
312
313 if (kDesktop_GrGLBinding == this->glBinding()) {
314 // Post 3.0 we will get R8
315 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
316 if (this->glVersion() >= GR_GL_VER(3,0) ||
317 this->hasExtension("GL_ARB_framebuffer_object")) {
318 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
319 }
320 } else {
321 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000322 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000323 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000324 }
325
326 if (kDesktop_GrGLBinding != this->glBinding()) {
327 // only available in ES
328 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
329 }
330
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000331 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000332 // GL_EXT_framebuffer_object for FBO support. Both of these
333 // allow RGBA4 render targets so this is always supported.
334 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
335
336 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000337 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000338 }
339
340 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000341 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000342 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000343}
344
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000345GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000346 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
347 return GrPixelConfigSwapRAndB(config);
348 } else {
349 return config;
350 }
351}
352
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000353GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000354 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000355 return GrPixelConfigSwapRAndB(config);
356 } else {
357 return config;
358 }
359}
360
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000361bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
362 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
363}
364
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000365void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000366 if (gPrintStartupSpew && !fPrintedCaps) {
367 fPrintedCaps = true;
368 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000369 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000370 }
371
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000372 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000373 GL_CALL(Disable(GR_GL_DEPTH_TEST));
374 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000375
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000376 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
377 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000378
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000379 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000380 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000381 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000382 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
383 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
384 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
385 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000386 if (this->glCaps().imagingSupport()) {
387 GL_CALL(Disable(GR_GL_COLOR_TABLE));
388 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000389 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
390 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
391 // Since ES doesn't support glPointSize at all we always use the VS to
392 // set the point size
393 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
394
395 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000396 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000397 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000398 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000399 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000400 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000401
reed@google.comac10a2d2010-12-22 21:39:39 +0000402 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000403 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000404
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000405 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000406 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000407
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000408 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000409
tomhudson@google.com93813632011-10-27 20:21:16 +0000410 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000411 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000412 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000413
bsalomon@google.coma3201942012-06-21 19:58:20 +0000414 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000415
bsalomon@google.coma3201942012-06-21 19:58:20 +0000416 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000418 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000419 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000420
reed@google.comac10a2d2010-12-22 21:39:39 +0000421 fHWGeometryState.fIndexBuffer = NULL;
422 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000423
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000424 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000425
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000426 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000427
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000428 fHWPathMatrixState.invalidate();
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000429 if (fCaps.fPathStencilingSupport) {
430 // we don't use the model view matrix.
431 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
432 GL_CALL(LoadIdentity());
433 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000434
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000435 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000436 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000437 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
438 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000439 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000440 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
441 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000442 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000443 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
444 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000445 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000446 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
447 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000448
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000449 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000450
451 // Third party GL code may have left vertex attributes enabled. Some GL
452 // implementations (osmesa) may read vetex attributes that are not required
453 // by the current shader. Therefore, we have to ensure that only the
454 // attributes we require for the current draw are enabled or we may cause an
455 // invalid read.
456
457 // Disable all vertex layout bits so that next flush will assume all
458 // optional vertex attributes are disabled.
459 fHWGeometryState.fVertexLayout = 0;
460
461 // We always use the this attribute and assume it is always enabled.
462 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
463 GL_CALL(EnableVertexAttribArray(posAttrIdx));
464 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000465 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000466 if (va != posAttrIdx) {
467 GL_CALL(DisableVertexAttribArray(va));
468 }
469 }
470
471 fHWProgramID = 0;
472 fHWConstAttribColor = GrColor_ILLEGAL;
473 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000474}
475
bsalomon@google.come269f212011-11-07 13:29:52 +0000476GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000477 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000478 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000479 return NULL;
480 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000481
robertphillips@google.com32716282012-06-04 12:48:45 +0000482 // next line relies on PlatformTextureDesc's flags matching GrTexture's
483 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000484 glTexDesc.fWidth = desc.fWidth;
485 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000486 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000487 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000488 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
489 glTexDesc.fOwnsID = false;
490 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
491
492 GrGLTexture* texture = NULL;
493 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
494 GrGLRenderTarget::Desc glRTDesc;
495 glRTDesc.fRTFBOID = 0;
496 glRTDesc.fTexFBOID = 0;
497 glRTDesc.fMSColorRenderbufferID = 0;
498 glRTDesc.fOwnIDs = true;
499 glRTDesc.fConfig = desc.fConfig;
500 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000501 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
502 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000503 glTexDesc.fTextureID,
504 &glRTDesc)) {
505 return NULL;
506 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000507 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000508 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000509 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000510 }
511 if (NULL == texture) {
512 return NULL;
513 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000514
bsalomon@google.come269f212011-11-07 13:29:52 +0000515 this->setSpareTextureUnit();
516 return texture;
517}
518
519GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
520 GrGLRenderTarget::Desc glDesc;
521 glDesc.fConfig = desc.fConfig;
522 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
523 glDesc.fMSColorRenderbufferID = 0;
524 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
525 glDesc.fSampleCnt = desc.fSampleCnt;
526 glDesc.fOwnIDs = false;
527 GrGLIRect viewport;
528 viewport.fLeft = 0;
529 viewport.fBottom = 0;
530 viewport.fWidth = desc.fWidth;
531 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000532
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000533 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
534 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000535 if (desc.fStencilBits) {
536 GrGLStencilBuffer::Format format;
537 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
538 format.fPacked = false;
539 format.fStencilBits = desc.fStencilBits;
540 format.fTotalBits = desc.fStencilBits;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000541 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
542 (this,
543 0,
544 desc.fWidth,
545 desc.fHeight,
546 desc.fSampleCnt,
547 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000548 tgt->setStencilBuffer(sb);
549 sb->unref();
550 }
551 return tgt;
552}
553
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000554////////////////////////////////////////////////////////////////////////////////
555
bsalomon@google.com6f379512011-11-16 20:36:03 +0000556void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
557 int left, int top, int width, int height,
558 GrPixelConfig config, const void* buffer,
559 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000560 if (NULL == buffer) {
561 return;
562 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000563 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
564
bsalomon@google.com6f379512011-11-16 20:36:03 +0000565 this->setSpareTextureUnit();
566 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
567 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000568 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000569 desc.fWidth = glTex->width();
570 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000571 desc.fConfig = glTex->config();
572 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000573 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000574 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000575
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000576 this->uploadTexData(desc, false,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000577 left, top, width, height,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000578 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000579}
580
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000581namespace {
582bool adjust_pixel_ops_params(int surfaceWidth,
583 int surfaceHeight,
584 size_t bpp,
585 int* left, int* top, int* width, int* height,
586 const void** data,
587 size_t* rowBytes) {
588 if (!*rowBytes) {
589 *rowBytes = *width * bpp;
590 }
591
592 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
593 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
594
595 if (!subRect.intersect(bounds)) {
596 return false;
597 }
598 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
599 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
600
601 *left = subRect.fLeft;
602 *top = subRect.fTop;
603 *width = subRect.width();
604 *height = subRect.height();
605 return true;
606}
607}
608
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000609bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
610 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000611 int left, int top, int width, int height,
612 GrPixelConfig dataConfig,
613 const void* data,
614 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000615 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000616
617 size_t bpp = GrBytesPerPixel(dataConfig);
618 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
619 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000620 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000621 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000622 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000623
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000624 // in case we need a temporary, trimmed copy of the src pixels
625 SkAutoSMalloc<128 * 128> tempStorage;
626
bsalomon@google.com313f0192012-07-10 17:21:02 +0000627 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000628 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000629 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000630 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000631
bsalomon@google.com313f0192012-07-10 17:21:02 +0000632 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
633 // 565 is not a sized internal format on desktop GL. So on desktop with
634 // 565 we always use an unsized internal format to let the system pick
635 // the best sized format to convert the 565 data to. Since TexStorage
636 // only allows sized internal formats we will instead use TexImage2D.
637 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000638 }
639
640 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000641 GrGLenum externalFormat;
642 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000643 // glTexStorage requires sized internal formats on both desktop and ES. ES
644 // glTexImage requires an unsized format.
645 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
646 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000647 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000648 }
649
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000650 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000651 // paletted textures cannot be updated
652 return false;
653 }
654
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000655 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000656 * check whether to allocate a temporary buffer for flipping y or
657 * because our srcData has extra bytes past each row. If so, we need
658 * to trim those off here, since GL ES may not let us specify
659 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000660 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000661 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000662 bool swFlipY = false;
663 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000664 if (NULL != data) {
665 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000666 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000667 glFlipY = true;
668 } else {
669 swFlipY = true;
670 }
671 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000672 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000673 // can't use this for flipping, only non-neg values allowed. :(
674 if (rowBytes != trimRowBytes) {
675 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
676 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
677 restoreGLRowLength = true;
678 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000679 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000680 if (trimRowBytes != rowBytes || swFlipY) {
681 // copy data into our new storage, skipping the trailing bytes
682 size_t trimSize = height * trimRowBytes;
683 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000684 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000685 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000686 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000687 char* dst = (char*)tempStorage.reset(trimSize);
688 for (int y = 0; y < height; y++) {
689 memcpy(dst, src, trimRowBytes);
690 if (swFlipY) {
691 src -= rowBytes;
692 } else {
693 src += rowBytes;
694 }
695 dst += trimRowBytes;
696 }
697 // now point data to our copied version
698 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000699 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000700 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000701 if (glFlipY) {
702 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
703 }
704 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000705 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000706 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000707 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000708 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000709 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000710 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000711 if (useTexStorage) {
712 // We never resize or change formats of textures. We don't use
713 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000714 GL_ALLOC_CALL(this->glInterface(),
715 TexStorage2D(GR_GL_TEXTURE_2D,
716 1, // levels
717 internalFormat,
718 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000719 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000720 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
721 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
722 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000723 GL_ALLOC_CALL(this->glInterface(),
724 CompressedTexImage2D(GR_GL_TEXTURE_2D,
725 0, // level
726 internalFormat,
727 desc.fWidth, desc.fHeight,
728 0, // border
729 imageSize,
730 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000731 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000732 GL_ALLOC_CALL(this->glInterface(),
733 TexImage2D(GR_GL_TEXTURE_2D,
734 0, // level
735 internalFormat,
736 desc.fWidth, desc.fHeight,
737 0, // border
738 externalFormat, externalType,
739 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000740 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000741 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000742 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000743 if (error != GR_GL_NO_ERROR) {
744 succeeded = false;
745 } else {
746 // if we have data and we used TexStorage to create the texture, we
747 // now upload with TexSubImage.
748 if (NULL != data && useTexStorage) {
749 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
750 0, // level
751 left, top,
752 width, height,
753 externalFormat, externalType,
754 data));
755 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000756 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000757 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000758 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000759 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000760 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000761 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
762 0, // level
763 left, top,
764 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000765 externalFormat, externalType, data));
766 }
767
768 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000769 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000770 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000771 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000772 if (glFlipY) {
773 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
774 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000775 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000776}
777
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000778namespace {
779bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
780 int sampleCount,
781 GrGLenum format,
782 int width, int height) {
783 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
784 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
785 bool created = false;
786 if (GrGLCaps::kNVDesktop_CoverageAAType ==
787 ctxInfo.caps().coverageAAType()) {
788 const GrGLCaps::MSAACoverageMode& mode =
789 ctxInfo.caps().getMSAACoverageMode(sampleCount);
790 GL_ALLOC_CALL(ctxInfo.interface(),
791 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
792 mode.fCoverageSampleCnt,
793 mode.fColorSampleCnt,
794 format,
795 width, height));
796 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
797 }
798 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000799 // glRBMS will fail if requested samples is > max samples.
800 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000801 GL_ALLOC_CALL(ctxInfo.interface(),
802 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
803 sampleCount,
804 format,
805 width, height));
806 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
807 }
808 return created;
809}
810}
811
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000812bool GrGpuGL::createRenderTargetObjects(int width, int height,
813 GrGLuint texID,
814 GrGLRenderTarget::Desc* desc) {
815 desc->fMSColorRenderbufferID = 0;
816 desc->fRTFBOID = 0;
817 desc->fTexFBOID = 0;
818 desc->fOwnIDs = true;
819
820 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000821
bsalomon@google.comab15d612011-08-09 12:57:56 +0000822 GrGLenum msColorFormat = 0; // suppress warning
823
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000824 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000825 if (!desc->fTexFBOID) {
826 goto FAILED;
827 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000828
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000829
830 // If we are using multisampling we will create two FBOS. We render
831 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000832 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000833 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000834 goto FAILED;
835 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000836 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
837 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000838 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000839 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000840 !this->configToGLFormats(desc->fConfig,
841 // GLES requires sized internal formats
842 kES2_GrGLBinding == this->glBinding(),
843 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000844 goto FAILED;
845 }
846 } else {
847 desc->fRTFBOID = desc->fTexFBOID;
848 }
849
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000850 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000851 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000852 if (desc->fRTFBOID != desc->fTexFBOID) {
853 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000854 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000855 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000856 if (!renderbuffer_storage_msaa(fGLContextInfo,
857 desc->fSampleCnt,
858 msColorFormat,
859 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000860 goto FAILED;
861 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000862 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000863 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000864 GR_GL_COLOR_ATTACHMENT0,
865 GR_GL_RENDERBUFFER,
866 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000867 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000868 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
869 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
870 goto FAILED;
871 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000872 fGLContextInfo.caps().markConfigAsValidColorAttachment(
873 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874 }
875 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000876 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000877
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000878 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
879 GR_GL_COLOR_ATTACHMENT0,
880 GR_GL_TEXTURE_2D,
881 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000882 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000883 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
884 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
885 goto FAILED;
886 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000887 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000888 }
889
890 return true;
891
892FAILED:
893 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000894 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000895 }
896 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000897 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000898 }
899 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000900 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000901 }
902 return false;
903}
904
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000905// good to set a break-point here to know when createTexture fails
906static GrTexture* return_null_texture() {
907// GrAssert(!"null texture");
908 return NULL;
909}
910
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000911#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000912static size_t as_size_t(int x) {
913 return x;
914}
915#endif
916
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000917GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000918 const void* srcData,
919 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000920
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000921 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000922 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000923
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000924 // Attempt to catch un- or wrongly initialized sample counts;
925 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
926
robertphillips@google.com32716282012-06-04 12:48:45 +0000927 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000928 glTexDesc.fWidth = desc.fWidth;
929 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000930 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000931 glTexDesc.fSampleCnt = desc.fSampleCnt;
932
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000933 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000934
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000935 glRTDesc.fMSColorRenderbufferID = 0;
936 glRTDesc.fRTFBOID = 0;
937 glRTDesc.fTexFBOID = 0;
938 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000939 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000940
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000941 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000942
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000943 const Caps& caps = this->getCaps();
944
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000945 // We keep GrRenderTargets in GL's normal orientation so that they
946 // can be drawn to by the outside world without the client having
947 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000948 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000949 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000950
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000951 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000952 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000953 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000954 //GrPrintf("MSAA RT requested but not supported on this platform.");
955 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000956 }
957
reed@google.comac10a2d2010-12-22 21:39:39 +0000958 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +0000959 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
960 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000961 return return_null_texture();
962 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000963 }
964
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000966 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000967 // provides a hint about how this texture will be used
968 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
969 GR_GL_TEXTURE_USAGE,
970 GR_GL_FRAMEBUFFER_ATTACHMENT));
971 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000972 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000973 return return_null_texture();
974 }
975
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000976 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000977 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000978
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000979 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
980 // drivers have a bug where an FBO won't be complete if it includes a
981 // texture that is not mipmap complete (considering the filter in use).
982 GrGLTexture::TexParams initialTexParams;
983 // we only set a subset here so invalidate first
984 initialTexParams.invalidate();
985 initialTexParams.fFilter = GR_GL_NEAREST;
986 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
987 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000988 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
989 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000990 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000991 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
992 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000993 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000994 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
995 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000996 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000997 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
998 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000999 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001000 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1001 glTexDesc.fWidth, glTexDesc.fHeight,
1002 desc.fConfig, srcData, rowBytes)) {
1003 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1004 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001005 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001006
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001008 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001009 // unbind the texture from the texture unit before binding it to the frame buffer
1010 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1011
bsalomon@google.com99621082011-11-15 16:47:16 +00001012 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1013 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001014 glTexDesc.fTextureID,
1015 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001017 return return_null_texture();
1018 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001019 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001020 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001021 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001022 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001023 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001024#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001025 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1026 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001027#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001028 return tex;
1029}
1030
1031namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001032
1033const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1034
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001035void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001036 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001037 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001038 // we shouldn't ever know one size and not the other
1039 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1040 (kUnknownBitCount == format->fTotalBits));
1041 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001042 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001043 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1044 (GrGLint*)&format->fStencilBits);
1045 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001047 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1048 (GrGLint*)&format->fTotalBits);
1049 format->fTotalBits += format->fStencilBits;
1050 } else {
1051 format->fTotalBits = format->fStencilBits;
1052 }
1053 }
1054}
1055}
1056
1057bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1058 int width, int height) {
1059
1060 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001061 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001063 GrAssert(width >= rt->width());
1064 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001065
1066 int samples = rt->numSamples();
1067 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001068 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001069 if (!sbID) {
1070 return false;
1071 }
1072
1073 GrGLStencilBuffer* sb = NULL;
1074
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001075 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001076 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001077 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078 // we start with the last stencil format that succeeded in hopes
1079 // that we won't go through this loop more than once after the
1080 // first (painful) stencil creation.
1081 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001082 const GrGLCaps::StencilFormat& sFmt =
1083 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001084 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001085 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001087 bool created;
1088 if (samples > 0) {
1089 created = renderbuffer_storage_msaa(fGLContextInfo,
1090 samples,
1091 sFmt.fInternalFormat,
1092 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001093 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001094 GL_ALLOC_CALL(this->glInterface(),
1095 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001096 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001097 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001098 created =
1099 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001100 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001101 if (created) {
1102 // After sized formats we attempt an unsized format and take
1103 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001104 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001105 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001106 sb = SkNEW_ARGS(GrGLStencilBuffer,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001107 (this, sbID, width, height,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001108 samples, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1110 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001111 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113 return true;
1114 }
1115 sb->abandon(); // otherwise we lose sbID
1116 sb->unref();
1117 }
1118 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001119 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001120 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121}
1122
1123bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1124 GrRenderTarget* rt) {
1125 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1126
1127 GrGLuint fbo = glrt->renderFBOID();
1128
1129 if (NULL == sb) {
1130 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 GR_GL_STENCIL_ATTACHMENT,
1133 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001134 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135 GR_GL_DEPTH_ATTACHMENT,
1136 GR_GL_RENDERBUFFER, 0));
1137#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001138 GrGLenum status;
1139 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001140 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1141#endif
1142 }
1143 return true;
1144 } else {
1145 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1146 GrGLuint rb = glsb->renderbufferID();
1147
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001148 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001149 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1150 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151 GR_GL_STENCIL_ATTACHMENT,
1152 GR_GL_RENDERBUFFER, rb));
1153 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001154 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 GR_GL_DEPTH_ATTACHMENT,
1156 GR_GL_RENDERBUFFER, rb));
1157 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001158 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 GR_GL_DEPTH_ATTACHMENT,
1160 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001161 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001163 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001164 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001165 glsb->format())) {
1166 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1167 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001168 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001169 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001171 if (glsb->format().fPacked) {
1172 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1173 GR_GL_DEPTH_ATTACHMENT,
1174 GR_GL_RENDERBUFFER, 0));
1175 }
1176 return false;
1177 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001178 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001179 rt->config(),
1180 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001183 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001184 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001185}
1186
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001187////////////////////////////////////////////////////////////////////////////////
1188
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001189GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001190 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001192 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001193 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001194 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001195 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001197 GL_ALLOC_CALL(this->glInterface(),
1198 BufferData(GR_GL_ARRAY_BUFFER,
1199 size,
1200 NULL, // data ptr
1201 dynamic ? GR_GL_DYNAMIC_DRAW :
1202 GR_GL_STATIC_DRAW));
1203 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001205 // deleting bound buffer does implicit bind to 0
1206 fHWGeometryState.fVertexBuffer = NULL;
1207 return NULL;
1208 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001209 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer,
1210 (this, id,
1211 size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001212 fHWGeometryState.fVertexBuffer = vertexBuffer;
1213 return vertexBuffer;
1214 }
1215 return NULL;
1216}
1217
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001218GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001219 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001220 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001222 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001223 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001224 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001225 GL_ALLOC_CALL(this->glInterface(),
1226 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1227 size,
1228 NULL, // data ptr
1229 dynamic ? GR_GL_DYNAMIC_DRAW :
1230 GR_GL_STATIC_DRAW));
1231 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 // deleting bound buffer does implicit bind to 0
1234 fHWGeometryState.fIndexBuffer = NULL;
1235 return NULL;
1236 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001237 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer,
1238 (this, id, size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 fHWGeometryState.fIndexBuffer = indexBuffer;
1240 return indexBuffer;
1241 }
1242 return NULL;
1243}
1244
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001245GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001246 GrAssert(fCaps.fPathStencilingSupport);
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001247 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001248}
1249
bsalomon@google.coma3201942012-06-21 19:58:20 +00001250void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001251 const GrDrawState& drawState = this->getDrawState();
1252 const GrGLRenderTarget* rt =
1253 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1254
1255 GrAssert(NULL != rt);
1256 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001257
bsalomon@google.coma3201942012-06-21 19:58:20 +00001258 if (fScissorState.fEnabled) {
1259 GrGLIRect scissor;
1260 scissor.setRelativeTo(vp,
1261 fScissorState.fRect.fLeft,
1262 fScissorState.fRect.fTop,
1263 fScissorState.fRect.width(),
1264 fScissorState.fRect.height());
1265 // if the scissor fully contains the viewport then we fall through and
1266 // disable the scissor test.
1267 if (!scissor.contains(vp)) {
1268 if (fHWScissorSettings.fRect != scissor) {
1269 scissor.pushToGLScissor(this->glInterface());
1270 fHWScissorSettings.fRect = scissor;
1271 }
1272 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1273 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1274 fHWScissorSettings.fEnabled = kYes_TriState;
1275 }
1276 return;
1277 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001279 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001280 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001281 fHWScissorSettings.fEnabled = kNo_TriState;
1282 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 }
1284}
1285
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001286void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001287 const GrDrawState& drawState = this->getDrawState();
1288 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001289 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001290 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001291
bsalomon@google.com74b98712011-11-11 19:46:16 +00001292 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001293 if (NULL != rect) {
1294 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001295 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001296 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001297 if (clippedRect.intersect(rtRect)) {
1298 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001299 } else {
1300 return;
1301 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001303 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001304 GrAutoTRestore<ScissorState> asr(&fScissorState);
1305 fScissorState.fEnabled = (NULL != rect);
1306 if (fScissorState.fEnabled) {
1307 fScissorState.fRect = *rect;
1308 }
1309 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001310
1311 GrGLfloat r, g, b, a;
1312 static const GrGLfloat scale255 = 1.f / 255.f;
1313 a = GrColorUnpackA(color) * scale255;
1314 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001315 r = GrColorUnpackR(color) * scaleRGB;
1316 g = GrColorUnpackG(color) * scaleRGB;
1317 b = GrColorUnpackB(color) * scaleRGB;
1318
1319 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001320 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001321 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001322 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001323}
1324
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001325void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001326 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001327 return;
1328 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001329
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001330 this->flushRenderTarget(&GrIRect::EmptyIRect());
1331
bsalomon@google.coma3201942012-06-21 19:58:20 +00001332 GrAutoTRestore<ScissorState> asr(&fScissorState);
1333 fScissorState.fEnabled = false;
1334 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001335
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001336 GL_CALL(StencilMask(0xffffffff));
1337 GL_CALL(ClearStencil(0));
1338 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001339 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001340}
1341
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001342void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001343 const GrDrawState& drawState = this->getDrawState();
1344 const GrRenderTarget* rt = drawState.getRenderTarget();
1345 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001346
1347 // this should only be called internally when we know we have a
1348 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001349 GrAssert(NULL != rt->getStencilBuffer());
1350 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001351#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001353 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001354#else
1355 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001356 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001357 // turned into draws. Our contract on GrDrawTarget says that
1358 // changing the clip between stencil passes may or may not
1359 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001360 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001361#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001362 GrGLint value;
1363 if (insideClip) {
1364 value = (1 << (stencilBitCount - 1));
1365 } else {
1366 value = 0;
1367 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001368 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001369
1370 GrAutoTRestore<ScissorState> asr(&fScissorState);
1371 fScissorState.fEnabled = true;
1372 fScissorState.fRect = rect;
1373 this->flushScissor();
1374
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001375 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001376 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001377 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001378 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001379}
1380
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001381void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001382 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001383}
1384
bsalomon@google.comc4364992011-11-07 15:54:49 +00001385bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1386 int left, int top,
1387 int width, int height,
1388 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001389 size_t rowBytes) const {
1390 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001391 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001392 return false;
1393 }
1394
1395 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001396 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001397 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001398 return true;
1399 }
1400 // If we have to do memcpys to handle rowBytes then y-flip is free
1401 // Note the rowBytes might be tight to the passed in data, but if data
1402 // gets clipped in x to the target the rowBytes will no longer be tight.
1403 if (left >= 0 && (left + width) < renderTarget->width()) {
1404 return 0 == rowBytes ||
1405 GrBytesPerPixel(config) * width == rowBytes;
1406 } else {
1407 return false;
1408 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001409}
1410
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001411bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001412 int left, int top,
1413 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001414 GrPixelConfig config,
1415 void* buffer,
1416 size_t rowBytes,
1417 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001418 GrGLenum format;
1419 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001420 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001421 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001422 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001423 size_t bpp = GrBytesPerPixel(config);
1424 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1425 &left, &top, &width, &height,
1426 const_cast<const void**>(&buffer),
1427 &rowBytes)) {
1428 return false;
1429 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001430
bsalomon@google.comc6980972011-11-02 19:57:21 +00001431 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001432 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001433 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001434 switch (tgt->getResolveType()) {
1435 case GrGLRenderTarget::kCantResolve_ResolveType:
1436 return false;
1437 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001438 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001439 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001440 break;
1441 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001442 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001443 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001444 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1445 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001446 break;
1447 default:
1448 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001449 }
1450
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001451 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001452
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001453 // the read rect is viewport-relative
1454 GrGLIRect readRect;
1455 readRect.setRelativeTo(glvp, left, top, width, height);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001456
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001457 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001458 if (0 == rowBytes) {
1459 rowBytes = tightRowBytes;
1460 }
1461 size_t readDstRowBytes = tightRowBytes;
1462 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001463
bsalomon@google.comc6980972011-11-02 19:57:21 +00001464 // determine if GL can read using the passed rowBytes or if we need
1465 // a scratch buffer.
1466 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1467 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001468 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001469 GrAssert(!(rowBytes % sizeof(GrColor)));
1470 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1471 readDstRowBytes = rowBytes;
1472 } else {
1473 scratch.reset(tightRowBytes * height);
1474 readDst = scratch.get();
1475 }
1476 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001477 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001478 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1479 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001480 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1481 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001482 format, type, readDst));
1483 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001484 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001485 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1486 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001487 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001488 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1489 invertY = true;
1490 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001491
1492 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001493 // API presents top-to-bottom. We must preserve the padding contents. Note
1494 // that the above readPixels did not overwrite the padding.
1495 if (readDst == buffer) {
1496 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001497 if (!invertY) {
1498 scratch.reset(tightRowBytes);
1499 void* tmpRow = scratch.get();
1500 // flip y in-place by rows
1501 const int halfY = height >> 1;
1502 char* top = reinterpret_cast<char*>(buffer);
1503 char* bottom = top + (height - 1) * rowBytes;
1504 for (int y = 0; y < halfY; y++) {
1505 memcpy(tmpRow, top, tightRowBytes);
1506 memcpy(top, bottom, tightRowBytes);
1507 memcpy(bottom, tmpRow, tightRowBytes);
1508 top += rowBytes;
1509 bottom -= rowBytes;
1510 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511 }
1512 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001513 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001514 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001515 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001516 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001517 char* dst = reinterpret_cast<char*>(buffer);
1518 if (!invertY) {
1519 dst += (height-1) * rowBytes;
1520 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001521 for (int y = 0; y < height; y++) {
1522 memcpy(dst, src, tightRowBytes);
1523 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001524 if (invertY) {
1525 dst += rowBytes;
1526 } else {
1527 dst -= rowBytes;
1528 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001529 }
1530 }
1531 return true;
1532}
1533
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001534void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001535
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001536 GrGLRenderTarget* rt =
1537 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1538 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001539
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001540 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001541 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001542 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001543 GrGLenum status;
1544 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001545 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001546 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001547 }
1548 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001549 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001550 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001551 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001552 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001553 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001554 }
1555 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001556 if (NULL == bound || !bound->isEmpty()) {
1557 rt->flagAsNeedingResolve(bound);
1558 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001559}
1560
twiz@google.com0f31ca72011-03-18 17:38:11 +00001561GrGLenum gPrimitiveType2GLMode[] = {
1562 GR_GL_TRIANGLES,
1563 GR_GL_TRIANGLE_STRIP,
1564 GR_GL_TRIANGLE_FAN,
1565 GR_GL_POINTS,
1566 GR_GL_LINES,
1567 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001568};
1569
bsalomon@google.comd302f142011-03-03 13:54:13 +00001570#define SWAP_PER_DRAW 0
1571
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001572#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001573 #if GR_MAC_BUILD
1574 #include <AGL/agl.h>
1575 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001576 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001577 void SwapBuf() {
1578 DWORD procID = GetCurrentProcessId();
1579 HWND hwnd = GetTopWindow(GetDesktopWindow());
1580 while(hwnd) {
1581 DWORD wndProcID = 0;
1582 GetWindowThreadProcessId(hwnd, &wndProcID);
1583 if(wndProcID == procID) {
1584 SwapBuffers(GetDC(hwnd));
1585 }
1586 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1587 }
1588 }
1589 #endif
1590#endif
1591
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001592void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1593 uint32_t startVertex,
1594 uint32_t startIndex,
1595 uint32_t vertexCount,
1596 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001597 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1598
twiz@google.com0f31ca72011-03-18 17:38:11 +00001599 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001600
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001601 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1602 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1603
1604 // our setupGeometry better have adjusted this to zero since
1605 // DrawElements always draws from the begining of the arrays for idx 0.
1606 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001607
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001608 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1609 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001610#if SWAP_PER_DRAW
1611 glFlush();
1612 #if GR_MAC_BUILD
1613 aglSwapBuffers(aglGetCurrentContext());
1614 int set_a_break_pt_here = 9;
1615 aglSwapBuffers(aglGetCurrentContext());
1616 #elif GR_WIN32_BUILD
1617 SwapBuf();
1618 int set_a_break_pt_here = 9;
1619 SwapBuf();
1620 #endif
1621#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001622}
1623
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001624void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1625 uint32_t startVertex,
1626 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001627 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1628
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001629 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1630
1631 // our setupGeometry better have adjusted this to zero.
1632 // DrawElements doesn't take an offset so we always adjus the startVertex.
1633 GrAssert(0 == startVertex);
1634
1635 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1636 // account for startVertex in the DrawElements case. So we always
1637 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001638 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001639#if SWAP_PER_DRAW
1640 glFlush();
1641 #if GR_MAC_BUILD
1642 aglSwapBuffers(aglGetCurrentContext());
1643 int set_a_break_pt_here = 9;
1644 aglSwapBuffers(aglGetCurrentContext());
1645 #elif GR_WIN32_BUILD
1646 SwapBuf();
1647 int set_a_break_pt_here = 9;
1648 SwapBuf();
1649 #endif
1650#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001651}
1652
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001653namespace {
1654const GrStencilSettings& winding_nv_path_stencil_settings() {
1655 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1656 kIncClamp_StencilOp,
1657 kIncClamp_StencilOp,
1658 kAlwaysIfInClip_StencilFunc,
1659 ~0, ~0, ~0);
1660 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1661}
1662const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1663 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1664 kInvert_StencilOp,
1665 kInvert_StencilOp,
1666 kAlwaysIfInClip_StencilFunc,
1667 ~0, ~0, ~0);
1668 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1669}
1670}
1671
1672
1673void GrGpuGL::setStencilPathSettings(const GrPath&,
1674 GrPathFill fill,
1675 GrStencilSettings* settings) {
1676 switch (fill) {
1677 case kEvenOdd_GrPathFill:
1678 *settings = even_odd_nv_path_stencil_settings();
1679 return;
1680 case kWinding_GrPathFill:
1681 *settings = winding_nv_path_stencil_settings();
1682 return;
1683 default:
1684 GrCrash("Unexpected path fill.");
1685 }
1686}
1687
1688void GrGpuGL::onGpuStencilPath(const GrPath* path, GrPathFill fill) {
1689 GrAssert(fCaps.fPathStencilingSupport);
1690
1691 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1692 GrDrawState* drawState = this->drawState();
1693 GrAssert(NULL != drawState->getRenderTarget());
1694 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1695 return;
1696 }
1697
1698 // Decide how to manipulate the stencil buffer based on the fill rule.
1699 // Also, assert that the stencil settings we set in setStencilPathSettings
1700 // are present.
1701 GrAssert(!fStencilSettings.isTwoSided());
1702 GrGLenum fillMode;
1703 switch (fill) {
1704 case kWinding_GrPathFill:
1705 fillMode = GR_GL_COUNT_UP;
1706 GrAssert(kIncClamp_StencilOp ==
1707 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1708 GrAssert(kIncClamp_StencilOp ==
1709 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1710 break;
1711 case kEvenOdd_GrPathFill:
1712 fillMode = GR_GL_INVERT;
1713 GrAssert(kInvert_StencilOp ==
1714 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1715 GrAssert(kInvert_StencilOp ==
1716 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1717 break;
1718 default:
1719 // Only the above two fill rules are allowed.
1720 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001721 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001722 }
1723 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1724 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001725}
1726
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001727void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1728
1729 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001730
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001731 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001732 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001734 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1735 rt->renderFBOID()));
1736 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1737 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001738 // make sure we go through flushRenderTarget() since we've modified
1739 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001740 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001741 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001742 const GrIRect dirtyRect = rt->getResolveRect();
1743 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001744 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001745 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001746
bsalomon@google.coma3201942012-06-21 19:58:20 +00001747 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001748 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001749 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001750 asr.reset(&fScissorState);
1751 fScissorState.fEnabled = true;
1752 fScissorState.fRect = dirtyRect;
1753 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001754 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001755 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001756 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001757 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001758 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1759 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001760 asr.reset(&fScissorState);
1761 fScissorState.fEnabled = false;
1762 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001763 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001764 int right = r.fLeft + r.fWidth;
1765 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001766 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1767 r.fLeft, r.fBottom, right, top,
1768 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001770 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001771 }
1772}
1773
bsalomon@google.com411dad02012-06-05 20:24:20 +00001774namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001775
bsalomon@google.com411dad02012-06-05 20:24:20 +00001776GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1777 static const GrGLenum gTable[] = {
1778 GR_GL_ALWAYS, // kAlways_StencilFunc
1779 GR_GL_NEVER, // kNever_StencilFunc
1780 GR_GL_GREATER, // kGreater_StencilFunc
1781 GR_GL_GEQUAL, // kGEqual_StencilFunc
1782 GR_GL_LESS, // kLess_StencilFunc
1783 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1784 GR_GL_EQUAL, // kEqual_StencilFunc,
1785 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1786 };
1787 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1788 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1789 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1790 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1791 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1792 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1793 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1794 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1795 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1796 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1797
1798 return gTable[basicFunc];
1799}
1800
1801GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1802 static const GrGLenum gTable[] = {
1803 GR_GL_KEEP, // kKeep_StencilOp
1804 GR_GL_REPLACE, // kReplace_StencilOp
1805 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1806 GR_GL_INCR, // kIncClamp_StencilOp
1807 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1808 GR_GL_DECR, // kDecClamp_StencilOp
1809 GR_GL_ZERO, // kZero_StencilOp
1810 GR_GL_INVERT, // kInvert_StencilOp
1811 };
1812 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1813 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1814 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1815 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1816 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1817 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1818 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1819 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1820 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1821 GrAssert((unsigned) op < kStencilOpCount);
1822 return gTable[op];
1823}
1824
1825void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001826 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001827 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001828 GrStencilSettings::Face grFace) {
1829 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1830 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1831 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1832
1833 GrGLint ref = settings.funcRef(grFace);
1834 GrGLint mask = settings.funcMask(grFace);
1835 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001836
1837 if (GR_GL_FRONT_AND_BACK == glFace) {
1838 // we call the combined func just in case separate stencil is not
1839 // supported.
1840 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1841 GR_GL_CALL(gl, StencilMask(writeMask));
1842 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1843 } else {
1844 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1845 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1846 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1847 }
1848}
1849}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001850
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001851void GrGpuGL::flushStencil(DrawType type) {
1852 if (kStencilPath_DrawType == type) {
1853 GrAssert(!fStencilSettings.isTwoSided());
1854 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1855 // that draws the path to the SB (glStencilFillPath)
1856 GrGLenum func =
1857 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1858 GL_CALL(PathStencilFunc(func,
1859 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1860 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1861 } else if (fHWStencilSettings != fStencilSettings) {
1862 if (fStencilSettings.isDisabled()) {
1863 if (kNo_TriState != fHWStencilTestEnabled) {
1864 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1865 fHWStencilTestEnabled = kNo_TriState;
1866 }
1867 } else {
1868 if (kYes_TriState != fHWStencilTestEnabled) {
1869 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1870 fHWStencilTestEnabled = kYes_TriState;
1871 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001872 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001873 if (!fStencilSettings.isDisabled()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001874 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001875 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001876 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001877 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001878 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001879 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001880 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001881 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001882 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001884 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001885 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001886 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001887 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001888 }
1889 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001890 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001891 }
1892}
1893
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001894void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001895 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001896 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001897 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1898 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001899 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001900 bool smoothLines = false;
1901
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001902 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001903 smoothLines = this->willUseHWAALines();
1904 if (smoothLines) {
1905 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1906 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1907 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1908 // must disable msaa to use line smoothing
1909 if (rt->isMultisampled() &&
1910 kNo_TriState != fHWAAState.fMSAAEnabled) {
1911 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1912 fHWAAState.fMSAAEnabled = kNo_TriState;
1913 }
1914 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001915 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001916 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1917 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1918 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1919 }
1920 }
1921 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001922 if (!smoothLines && rt->isMultisampled()) {
1923 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1924 // convex hulls of each segment appear to get filled.
1925 bool enableMSAA = kStencilPath_DrawType == type ||
1926 this->getDrawState().isHWAntialiasState();
1927 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001928 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1929 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1930 fHWAAState.fMSAAEnabled = kYes_TriState;
1931 }
1932 } else {
1933 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1934 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1935 fHWAAState.fMSAAEnabled = kNo_TriState;
1936 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001937 }
1938 }
1939 }
1940}
1941
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001942void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001943 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001944 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001945 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001946 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001947 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001948 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001949 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001950 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1951 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1952 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1953 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1954 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1955 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001956 }
1957 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001958 // any optimization to disable blending should
1959 // have already been applied and tweaked the coeffs
1960 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001961 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1962 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001963 if (blendOff) {
1964 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001965 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001966 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001967 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001968 } else {
1969 if (kYes_TriState != fHWBlendState.fEnabled) {
1970 GL_CALL(Enable(GR_GL_BLEND));
1971 fHWBlendState.fEnabled = kYes_TriState;
1972 }
1973 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1974 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001975 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1976 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001977 fHWBlendState.fSrcCoeff = srcCoeff;
1978 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001979 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001980 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001981 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1982 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001983 (!fHWBlendState.fConstColorValid ||
1984 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001985
1986 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001987 GrColorUnpackR(blendConst) / 255.f,
1988 GrColorUnpackG(blendConst) / 255.f,
1989 GrColorUnpackB(blendConst) / 255.f,
1990 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001991 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001992 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001993 fHWBlendState.fConstColor = blendConst;
1994 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001995 }
1996 }
1997 }
1998}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001999namespace {
2000
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002001// get_swizzle is only called from this .cpp so it is OK to inline it here
2002inline const GrGLenum* get_swizzle(GrPixelConfig config,
2003 const GrSamplerState& sampler,
2004 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002005 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002006 if (glCaps.textureRedSupport()) {
2007 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2008 GR_GL_RED, GR_GL_RED };
2009 return gRedSmear;
2010 } else {
2011 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2012 GR_GL_ALPHA, GR_GL_ALPHA };
2013 return gAlphaSmear;
2014 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002015 } else {
2016 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2017 GR_GL_BLUE, GR_GL_ALPHA };
2018 return gStraight;
2019 }
2020}
2021
2022void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002023 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2024 GR_GL_TEXTURE_SWIZZLE_RGBA,
2025 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002026}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002027
2028const GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
2029 static const GrGLenum gWrapModes[] = {
2030 GR_GL_CLAMP_TO_EDGE,
2031 GR_GL_REPEAT,
2032 GR_GL_MIRRORED_REPEAT
2033 };
2034 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2035 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2036 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2037 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2038 return gWrapModes[tm];
2039}
2040
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002041}
2042
bsalomon@google.com4c883782012-06-04 19:05:11 +00002043void GrGpuGL::flushBoundTextureAndParams(int stage) {
2044 GrDrawState* drawState = this->drawState();
bsalomon@google.com0982d352012-07-31 15:33:25 +00002045 // FIXME: Assuming at most one texture per custom stage
bsalomon@google.comcddaf342012-07-30 13:09:05 +00002046 const GrCustomStage* customStage = drawState->sampler(stage)->getCustomStage();
2047 GrGLTexture* nextTexture = static_cast<GrGLTexture*>(customStage->texture(0));
bsalomon@google.com0982d352012-07-31 15:33:25 +00002048 if (NULL != nextTexture) {
2049 // Currently we always use the texture params from the GrSamplerState. Soon custom stages
2050 // will provide their own params.
2051 const GrTextureParams& texParams = drawState->getSampler(stage).getTextureParams();
2052 this->flushBoundTextureAndParams(stage, texParams, nextTexture);
2053 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002054}
2055
bsalomon@google.comb8670992012-07-25 21:27:09 +00002056void GrGpuGL::flushBoundTextureAndParams(int stage,
2057 const GrTextureParams& params,
2058 GrGLTexture* nextTexture) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002059 GrDrawState* drawState = this->drawState();
2060
bsalomon@google.com4c883782012-06-04 19:05:11 +00002061 // true for now, but maybe not with GrEffect.
2062 GrAssert(NULL != nextTexture);
bsalomon@google.comb8670992012-07-25 21:27:09 +00002063 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2064 // from the rt it will still be the last bound texture, but it needs resolving. So keep this
bsalomon@google.com4c883782012-06-04 19:05:11 +00002065 // out of the "last != next" check.
bsalomon@google.comb8670992012-07-25 21:27:09 +00002066 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002067 if (NULL != texRT) {
2068 this->onResolveRenderTarget(texRT);
2069 }
2070
2071 if (fHWBoundTextures[stage] != nextTexture) {
2072 this->setTextureUnit(stage);
2073 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002074 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2075 fHWBoundTextures[stage] = nextTexture;
2076 }
2077
bsalomon@google.com4c883782012-06-04 19:05:11 +00002078 ResetTimestamp timestamp;
2079 const GrGLTexture::TexParams& oldTexParams =
2080 nextTexture->getCachedTexParams(&timestamp);
2081 bool setAll = timestamp < this->getResetTimestamp();
2082 GrGLTexture::TexParams newTexParams;
2083
bsalomon@google.comb8670992012-07-25 21:27:09 +00002084 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002085
bsalomon@google.comb8670992012-07-25 21:27:09 +00002086 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2087 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002088 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.comb8670992012-07-25 21:27:09 +00002089 get_swizzle(nextTexture->config(), drawState->getSampler(stage), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002090 sizeof(newTexParams.fSwizzleRGBA));
2091 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2092 this->setTextureUnit(stage);
2093 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2094 GR_GL_TEXTURE_MAG_FILTER,
2095 newTexParams.fFilter));
2096 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2097 GR_GL_TEXTURE_MIN_FILTER,
2098 newTexParams.fFilter));
2099 }
2100 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2101 this->setTextureUnit(stage);
2102 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2103 GR_GL_TEXTURE_WRAP_S,
2104 newTexParams.fWrapS));
2105 }
2106 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2107 this->setTextureUnit(stage);
2108 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2109 GR_GL_TEXTURE_WRAP_T,
2110 newTexParams.fWrapT));
2111 }
2112 if (this->glCaps().textureSwizzleSupport() &&
2113 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2114 oldTexParams.fSwizzleRGBA,
2115 sizeof(newTexParams.fSwizzleRGBA)))) {
2116 this->setTextureUnit(stage);
2117 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2118 this->glInterface());
2119 }
2120 nextTexture->setCachedTexParams(newTexParams,
2121 this->getResetTimestamp());
2122}
2123
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002124void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002125
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002126 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002127
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002128 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002129 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002130 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002131 fHWDitherEnabled = kYes_TriState;
2132 }
2133 } else {
2134 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002135 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002136 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002137 }
2138 }
2139
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002140 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002141 if (kNo_TriState != fHWWriteToColor) {
2142 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2143 GR_GL_FALSE, GR_GL_FALSE));
2144 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002145 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002146 } else {
2147 if (kYes_TriState != fHWWriteToColor) {
2148 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2149 fHWWriteToColor = kYes_TriState;
2150 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002151 }
2152
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002153 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002154 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002155 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002156 GL_CALL(Enable(GR_GL_CULL_FACE));
2157 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002158 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002159 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002160 GL_CALL(Enable(GR_GL_CULL_FACE));
2161 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002162 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002163 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002164 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002165 break;
2166 default:
2167 GrCrash("Unknown draw face.");
2168 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002169 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002170 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002171}
2172
2173void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002174 if (fHWGeometryState.fVertexBuffer != buffer) {
2175 fHWGeometryState.fArrayPtrsDirty = true;
2176 fHWGeometryState.fVertexBuffer = buffer;
2177 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002178}
2179
2180void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002181 if (fHWGeometryState.fVertexBuffer == buffer) {
2182 // deleting bound buffer does implied bind to 0
2183 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002184 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002185 }
2186}
2187
2188void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002189 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002190}
2191
2192void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002193 if (fHWGeometryState.fIndexBuffer == buffer) {
2194 // deleting bound buffer does implied bind to 0
2195 fHWGeometryState.fIndexBuffer = NULL;
2196 }
2197}
2198
reed@google.comac10a2d2010-12-22 21:39:39 +00002199void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2200 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002201 if (fHWBoundRenderTarget == renderTarget) {
2202 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002203 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002204}
2205
2206void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002207 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002208 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002209 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002210 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002211 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002212 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002213}
2214
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002215bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2216 bool getSizedInternalFormat,
2217 GrGLenum* internalFormat,
2218 GrGLenum* externalFormat,
2219 GrGLenum* externalType) {
2220 GrGLenum dontCare;
2221 if (NULL == internalFormat) {
2222 internalFormat = &dontCare;
2223 }
2224 if (NULL == externalFormat) {
2225 externalFormat = &dontCare;
2226 }
2227 if (NULL == externalType) {
2228 externalType = &dontCare;
2229 }
2230
reed@google.comac10a2d2010-12-22 21:39:39 +00002231 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002232 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002233 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002234 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002235 if (getSizedInternalFormat) {
2236 *internalFormat = GR_GL_RGBA8;
2237 } else {
2238 *internalFormat = GR_GL_RGBA;
2239 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002240 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002241 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002242 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002243 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002244 return false;
2245 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002246 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002247 if (getSizedInternalFormat) {
2248 *internalFormat = GR_GL_BGRA8;
2249 } else {
2250 *internalFormat = GR_GL_BGRA;
2251 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002252 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002253 if (getSizedInternalFormat) {
2254 *internalFormat = GR_GL_RGBA8;
2255 } else {
2256 *internalFormat = GR_GL_RGBA;
2257 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002258 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002259 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002260 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002261 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002262 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002263 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002264 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002265 if (getSizedInternalFormat) {
2266 if (this->glBinding() == kDesktop_GrGLBinding) {
2267 return false;
2268 } else {
2269 *internalFormat = GR_GL_RGB565;
2270 }
2271 } else {
2272 *internalFormat = GR_GL_RGB;
2273 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002274 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002275 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002276 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002277 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002278 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002279 if (getSizedInternalFormat) {
2280 *internalFormat = GR_GL_RGBA4;
2281 } else {
2282 *internalFormat = GR_GL_RGBA;
2283 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002284 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002285 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002286 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002287 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002288 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002289 // glCompressedTexImage doesn't take external params
2290 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002291 // no sized/unsized internal format distinction here
2292 *internalFormat = GR_GL_PALETTE8_RGBA8;
2293 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002294 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002295 } else {
2296 return false;
2297 }
2298 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002299 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002300 if (this->glCaps().textureRedSupport()) {
2301 *internalFormat = GR_GL_RED;
2302 *externalFormat = GR_GL_RED;
2303 if (getSizedInternalFormat) {
2304 *internalFormat = GR_GL_R8;
2305 } else {
2306 *internalFormat = GR_GL_RED;
2307 }
2308 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002309 } else {
2310 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002311 *externalFormat = GR_GL_ALPHA;
2312 if (getSizedInternalFormat) {
2313 *internalFormat = GR_GL_ALPHA8;
2314 } else {
2315 *internalFormat = GR_GL_ALPHA;
2316 }
2317 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002318 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 break;
2320 default:
2321 return false;
2322 }
2323 return true;
2324}
2325
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002326void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002327 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002328 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002329 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002330 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002331 }
2332}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002333
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002334void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002335 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002336 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002337 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002338 }
2339}
2340
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002341void GrGpuGL::setBuffers(bool indexed,
2342 int* extraVertexOffset,
2343 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002344
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002345 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002346
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002347 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2348
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002349 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002350 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002351 case kBuffer_GeometrySrcType:
2352 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002353 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002354 break;
2355 case kArray_GeometrySrcType:
2356 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002357 this->finalizeReservedVertices();
2358 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2359 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002360 break;
2361 default:
2362 vbuf = NULL; // suppress warning
2363 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002364 }
2365
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002366 GrAssert(NULL != vbuf);
2367 GrAssert(!vbuf->isLocked());
2368 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002369 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002370 fHWGeometryState.fArrayPtrsDirty = true;
2371 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002372 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002373
2374 if (indexed) {
2375 GrAssert(NULL != extraIndexOffset);
2376
2377 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002378 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002379 case kBuffer_GeometrySrcType:
2380 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002381 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002382 break;
2383 case kArray_GeometrySrcType:
2384 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002385 this->finalizeReservedIndices();
2386 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2387 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002388 break;
2389 default:
2390 ibuf = NULL; // suppress warning
2391 GrCrash("Unknown geometry src type!");
2392 }
2393
2394 GrAssert(NULL != ibuf);
2395 GrAssert(!ibuf->isLocked());
2396 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002397 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002398 fHWGeometryState.fIndexBuffer = ibuf;
2399 }
2400 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002401}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002402