blob: 8089d100c2f08e966197ad6394faf04b48900f5e [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000012#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000013#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000014#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
caryclark@google.comcf6285b2012-06-06 12:09:01 +000016static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000017static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000020#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021
bsalomon@google.com316f99232011-01-13 21:28:12 +000022// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000024static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000025
reed@google.comac10a2d2010-12-22 21:39:39 +000026#define SKIP_CACHE_CHECK true
27
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000028#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
29 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
30 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
31 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
32#else
33 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
34 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
35 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
36#endif
37
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000038
39///////////////////////////////////////////////////////////////////////////////
40
twiz@google.com0f31ca72011-03-18 17:38:11 +000041static const GrGLenum gXfermodeCoeff2Blend[] = {
42 GR_GL_ZERO,
43 GR_GL_ONE,
44 GR_GL_SRC_COLOR,
45 GR_GL_ONE_MINUS_SRC_COLOR,
46 GR_GL_DST_COLOR,
47 GR_GL_ONE_MINUS_DST_COLOR,
48 GR_GL_SRC_ALPHA,
49 GR_GL_ONE_MINUS_SRC_ALPHA,
50 GR_GL_DST_ALPHA,
51 GR_GL_ONE_MINUS_DST_ALPHA,
52 GR_GL_CONSTANT_COLOR,
53 GR_GL_ONE_MINUS_CONSTANT_COLOR,
54 GR_GL_CONSTANT_ALPHA,
55 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000056
57 // extended blend coeffs
58 GR_GL_SRC1_COLOR,
59 GR_GL_ONE_MINUS_SRC1_COLOR,
60 GR_GL_SRC1_ALPHA,
61 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000062};
63
bsalomon@google.com271cffc2011-05-20 14:13:56 +000064bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000065 static const bool gCoeffReferencesBlendConst[] = {
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 true,
77 true,
78 true,
79 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000080
81 // extended blend coeffs
82 false,
83 false,
84 false,
85 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000086 };
87 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000088 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
89 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000090
bsalomon@google.com47059542012-06-06 20:51:20 +000091 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
92 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
93 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
94 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
95 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
98 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
99 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
104 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000105
bsalomon@google.com47059542012-06-06 20:51:20 +0000106 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
109 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000110
111 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000112 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
113 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000114}
115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116///////////////////////////////////////////////////////////////////////////////
117
rileya@google.come38160c2012-07-03 18:03:04 +0000118static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000119
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000120static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000121
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000122 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000123
twiz@google.com0f31ca72011-03-18 17:38:11 +0000124 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000125 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
126 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000127 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000128 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
129 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000130 // some implementations require texture to be mip-map complete before
131 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000132 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000133 GR_GL_TEXTURE_MIN_FILTER,
134 GR_GL_NEAREST));
135 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
136 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
137 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
138 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
139 GR_GL_COLOR_ATTACHMENT0,
140 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000141 GrGLenum status;
142 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
144 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000145
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000146 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000147}
148
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000149GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
150
151 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000152
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000153 fillInConfigRenderableTable();
154
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000155 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000156
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000157 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000159 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000160 const GrGLubyte* ext;
161 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000162 const GrGLubyte* vendor;
163 const GrGLubyte* renderer;
164 const GrGLubyte* version;
165 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
166 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
167 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000168 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
169 this);
170 GrPrintf("------ VENDOR %s\n", vendor);
171 GrPrintf("------ RENDERER %s\n", renderer);
172 GrPrintf("------ VERSION %s\n", version);
173 GrPrintf("------ EXTENSIONS\n %s \n", ext);
174 }
175
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000176 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000177
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;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000181 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000182 if (false) { // avoid bit rot, suppress warning
183 fbo_test(this->glInterface(), 0, 0);
184 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000185}
186
187GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000188 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000189 // detach the current program so there is no confusion on OpenGL's part
190 // that we want it to be deleted
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000191 GrAssert(fHWProgramID == fCurrentProgram->fProgramID);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000192 GL_CALL(UseProgram(0));
193 }
194
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000195 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000196
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000197 // This must be called by before the GrDrawTarget destructor
198 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000199 // This subclass must do this before the base class destructor runs
200 // since we will unref the GrGLInterface.
201 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000202}
203
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000204///////////////////////////////////////////////////////////////////////////////
205
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000206void GrGpuGL::initCaps() {
207 GrGLint maxTextureUnits;
208 // check FS and fixed-function texture unit limits
209 // we only use textures in the fragment stage currently.
210 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000211 const GrGLInterface* gl = this->glInterface();
212 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000213 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000214
215 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000216 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000217 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000218 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000219 for (int i = 0; i < numFormats; ++i) {
220 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
221 fCaps.f8BitPaletteSupport = true;
222 break;
223 }
224 }
225
226 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000227 // we could also look for GL_ATI_separate_stencil extension or
228 // GL_EXT_stencil_two_side but they use different function signatures
229 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000230 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000231 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000232 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000233 this->hasExtension("GL_EXT_stencil_wrap");
234 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000235 // ES 2 has two sided stencil and stencil wrap
236 fCaps.fTwoSidedStencilSupport = true;
237 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 }
239
240 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000241 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
242 // extension includes glMapBuffer.
243 } else {
244 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
245 }
246
247 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000248 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000249 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
250 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 } else {
252 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 }
254 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000255 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000256 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000257 }
258
259 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
260
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000261 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
262 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000263 // Our render targets are always created with textures as the color
264 // attachment, hence this min:
265 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
266
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000267 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000268 fCaps.fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
269 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000270
271 // Enable supported shader-related caps
272 if (kDesktop_GrGLBinding == this->glBinding()) {
273 fCaps.fDualSourceBlendingSupport =
274 this->glVersion() >= GR_GL_VER(3,3) ||
275 this->hasExtension("GL_ARB_blend_func_extended");
276 fCaps.fShaderDerivativeSupport = true;
277 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
278 fCaps.fGeometryShaderSupport =
279 this->glVersion() >= GR_GL_VER(3,2) &&
280 this->glslGeneration() >= k150_GrGLSLGeneration;
281 } else {
282 fCaps.fShaderDerivativeSupport =
283 this->hasExtension("GL_OES_standard_derivatives");
284 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000285}
286
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000287void GrGpuGL::fillInConfigRenderableTable() {
288
289 // OpenGL < 3.0
290 // no support for render targets unless the GL_ARB_framebuffer_object
291 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
292 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
293 // probably don't get R8 in this case.
294
295 // OpenGL 3.0
296 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
297 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
298
299 // >= OpenGL 3.1
300 // base color renderable: RED, RG, RGB, and RGBA
301 // sized derivatives: R8, RGBA4, RGBA8
302 // if the GL_ARB_compatibility extension is supported then we get back
303 // support for GL_ALPHA and ALPHA8
304
305 // GL_EXT_bgra adds BGRA render targets to any version
306
307 // ES 2.0
308 // color renderable: RGBA4, RGB5_A1, RGB565
309 // GL_EXT_texture_rg adds support for R8 as a color render target
310 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
311 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
312 // added BGRA support
313
314 if (kDesktop_GrGLBinding == this->glBinding()) {
315 // Post 3.0 we will get R8
316 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
317 if (this->glVersion() >= GR_GL_VER(3,0) ||
318 this->hasExtension("GL_ARB_framebuffer_object")) {
319 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
320 }
321 } else {
322 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000323 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
324 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000325 }
326
327 if (kDesktop_GrGLBinding != this->glBinding()) {
328 // only available in ES
329 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
330 }
331
332 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
333 // GL_EXT_framebuffer_object for FBO support. Both of these
334 // allow RGBA4 render targets so this is always supported.
335 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
336
337 if (this->glCaps().rgba8RenderbufferSupport()) {
338 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
339 }
340
341 if (this->glCaps().bgraFormatSupport()) {
342 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
343 }
344
345 // the un-premultiplied formats just inherit the premultiplied setting
346 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
347 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
348 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
349 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
350}
351
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000352bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
353 if (kUnknown_CanPreserveUnpremulRoundtrip ==
354 fCanPreserveUnpremulRoundtrip) {
355
356 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
357 uint32_t* srcData = data.get();
358 uint32_t* firstRead = data.get() + 256 * 256;
359 uint32_t* secondRead = data.get() + 2 * 256 * 256;
360
361 for (int y = 0; y < 256; ++y) {
362 for (int x = 0; x < 256; ++x) {
363 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
364 color[3] = y;
365 color[2] = x;
366 color[1] = x;
367 color[0] = x;
368 }
369 }
370
371 // We have broader support for read/write pixels on render targets
372 // than on textures.
373 GrTextureDesc dstDesc;
374 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
375 kNoStencil_GrTextureFlagBit;
376 dstDesc.fWidth = 256;
377 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000378 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000379
380 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
381 if (!dstTex.get()) {
382 return false;
383 }
384 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
385 GrAssert(NULL != rt);
386
387 bool failed = true;
388 static const UnpremulConversion gMethods[] = {
389 kUpOnWrite_DownOnRead_UnpremulConversion,
390 kDownOnWrite_UpOnRead_UnpremulConversion,
391 };
392
393 // pretend that we can do the roundtrip to avoid recursive calls to
394 // this function
395 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
396 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
397 fUnpremulConversion = gMethods[i];
398 rt->writePixels(0, 0,
399 256, 256,
400 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
401 rt->readPixels(0, 0,
402 256, 256,
403 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
404 rt->writePixels(0, 0,
405 256, 256,
406 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
407 rt->readPixels(0, 0,
408 256, 256,
409 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
410 failed = false;
411 for (int j = 0; j < 256 * 256; ++j) {
412 if (firstRead[j] != secondRead[j]) {
413 failed = true;
414 break;
415 }
416 }
417 }
418 fCanPreserveUnpremulRoundtrip = failed ?
419 kNo_CanPreserveUnpremulRoundtrip :
420 kYes_CanPreserveUnpremulRoundtrip;
421 }
422
423 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
424 return true;
425 } else {
426 return false;
427 }
428}
429
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000430GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000431 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
432 return GrPixelConfigSwapRAndB(config);
433 } else {
434 return config;
435 }
436}
437
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000438GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000439 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000440 return GrPixelConfigSwapRAndB(config);
441 } else {
442 return config;
443 }
444}
445
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000446bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
447 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
448}
449
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000450void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000451 if (gPrintStartupSpew && !fPrintedCaps) {
452 fPrintedCaps = true;
453 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000454 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000455 }
456
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000457 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000458 GL_CALL(Disable(GR_GL_DEPTH_TEST));
459 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000460
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000461 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
462 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000464 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000465 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000466 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000467 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
468 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
469 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
470 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000471 if (this->glCaps().imagingSupport()) {
472 GL_CALL(Disable(GR_GL_COLOR_TABLE));
473 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000474 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
475 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
476 // Since ES doesn't support glPointSize at all we always use the VS to
477 // set the point size
478 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
479
480 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
481 // currently part of our gl interface. There are probably others as
482 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000483 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000484 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000485 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000486
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000488 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000489
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000490 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000491 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000492
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000493 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000494
tomhudson@google.com93813632011-10-27 20:21:16 +0000495 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000496 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000497 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000498
bsalomon@google.coma3201942012-06-21 19:58:20 +0000499 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000500
bsalomon@google.coma3201942012-06-21 19:58:20 +0000501 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000503 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000504 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000505
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 fHWGeometryState.fIndexBuffer = NULL;
507 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000508
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000509 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000510
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000511 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000512
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000513 fHWPathMatrixState.invalidate();
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000514 if (fCaps.fPathStencilingSupport) {
515 // we don't use the model view matrix.
516 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
517 GL_CALL(LoadIdentity());
518 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000519
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000520 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000521 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000522 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
523 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000524 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000525 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
526 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000527 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000528 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
529 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000530 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000531 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
532 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000533
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000534 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000535
536 // Third party GL code may have left vertex attributes enabled. Some GL
537 // implementations (osmesa) may read vetex attributes that are not required
538 // by the current shader. Therefore, we have to ensure that only the
539 // attributes we require for the current draw are enabled or we may cause an
540 // invalid read.
541
542 // Disable all vertex layout bits so that next flush will assume all
543 // optional vertex attributes are disabled.
544 fHWGeometryState.fVertexLayout = 0;
545
546 // We always use the this attribute and assume it is always enabled.
547 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
548 GL_CALL(EnableVertexAttribArray(posAttrIdx));
549 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000550 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000551 if (va != posAttrIdx) {
552 GL_CALL(DisableVertexAttribArray(va));
553 }
554 }
555
556 fHWProgramID = 0;
557 fHWConstAttribColor = GrColor_ILLEGAL;
558 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000559}
560
bsalomon@google.come269f212011-11-07 13:29:52 +0000561GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000562 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000563 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000564 return NULL;
565 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000566
robertphillips@google.com32716282012-06-04 12:48:45 +0000567 // next line relies on PlatformTextureDesc's flags matching GrTexture's
568 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000569 glTexDesc.fWidth = desc.fWidth;
570 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000571 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000572 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000573 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
574 glTexDesc.fOwnsID = false;
575 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
576
577 GrGLTexture* texture = NULL;
578 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
579 GrGLRenderTarget::Desc glRTDesc;
580 glRTDesc.fRTFBOID = 0;
581 glRTDesc.fTexFBOID = 0;
582 glRTDesc.fMSColorRenderbufferID = 0;
583 glRTDesc.fOwnIDs = true;
584 glRTDesc.fConfig = desc.fConfig;
585 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000586 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
587 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000588 glTexDesc.fTextureID,
589 &glRTDesc)) {
590 return NULL;
591 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000592 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000593 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000594 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000595 }
596 if (NULL == texture) {
597 return NULL;
598 }
599
600 this->setSpareTextureUnit();
601 return texture;
602}
603
604GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
605 GrGLRenderTarget::Desc glDesc;
606 glDesc.fConfig = desc.fConfig;
607 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
608 glDesc.fMSColorRenderbufferID = 0;
609 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
610 glDesc.fSampleCnt = desc.fSampleCnt;
611 glDesc.fOwnIDs = false;
612 GrGLIRect viewport;
613 viewport.fLeft = 0;
614 viewport.fBottom = 0;
615 viewport.fWidth = desc.fWidth;
616 viewport.fHeight = desc.fHeight;
617
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000618 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
619 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000620 if (desc.fStencilBits) {
621 GrGLStencilBuffer::Format format;
622 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
623 format.fPacked = false;
624 format.fStencilBits = desc.fStencilBits;
625 format.fTotalBits = desc.fStencilBits;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000626 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
627 (this,
628 0,
629 desc.fWidth,
630 desc.fHeight,
631 desc.fSampleCnt,
632 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000633 tgt->setStencilBuffer(sb);
634 sb->unref();
635 }
636 return tgt;
637}
638
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000639////////////////////////////////////////////////////////////////////////////////
640
bsalomon@google.com6f379512011-11-16 20:36:03 +0000641void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
642 int left, int top, int width, int height,
643 GrPixelConfig config, const void* buffer,
644 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000645 if (NULL == buffer) {
646 return;
647 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000648 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
649
bsalomon@google.com6f379512011-11-16 20:36:03 +0000650 this->setSpareTextureUnit();
651 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
652 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000653 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000654 desc.fWidth = glTex->width();
655 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000656 desc.fConfig = glTex->config();
657 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000658 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000659 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000660
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000661 this->uploadTexData(desc, false,
662 left, top, width, height,
663 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000664}
665
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000666namespace {
667bool adjust_pixel_ops_params(int surfaceWidth,
668 int surfaceHeight,
669 size_t bpp,
670 int* left, int* top, int* width, int* height,
671 const void** data,
672 size_t* rowBytes) {
673 if (!*rowBytes) {
674 *rowBytes = *width * bpp;
675 }
676
677 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
678 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
679
680 if (!subRect.intersect(bounds)) {
681 return false;
682 }
683 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
684 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
685
686 *left = subRect.fLeft;
687 *top = subRect.fTop;
688 *width = subRect.width();
689 *height = subRect.height();
690 return true;
691}
692}
693
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000694bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
695 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000696 int left, int top, int width, int height,
697 GrPixelConfig dataConfig,
698 const void* data,
699 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000700 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000701
702 size_t bpp = GrBytesPerPixel(dataConfig);
703 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
704 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000705 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000706 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000707 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000708
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000709 // in case we need a temporary, trimmed copy of the src pixels
710 SkAutoSMalloc<128 * 128> tempStorage;
711
bsalomon@google.com313f0192012-07-10 17:21:02 +0000712 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000713 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000714 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000715 this->glCaps().texStorageSupport();
bsalomon@google.com313f0192012-07-10 17:21:02 +0000716
717 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
718 // 565 is not a sized internal format on desktop GL. So on desktop with
719 // 565 we always use an unsized internal format to let the system pick
720 // the best sized format to convert the 565 data to. Since TexStorage
721 // only allows sized internal formats we will instead use TexImage2D.
722 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000723 }
724
725 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000726 GrGLenum externalFormat;
727 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000728 // glTexStorage requires sized internal formats on both desktop and ES. ES
729 // glTexImage requires an unsized format.
730 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
731 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000732 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000733 }
734
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000735 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000736 // paletted textures cannot be updated
737 return false;
738 }
739
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000740 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000741 * check whether to allocate a temporary buffer for flipping y or
742 * because our srcData has extra bytes past each row. If so, we need
743 * to trim those off here, since GL ES may not let us specify
744 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000745 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000746 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000747 bool swFlipY = false;
748 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000749 if (NULL != data) {
750 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000751 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000752 glFlipY = true;
753 } else {
754 swFlipY = true;
755 }
756 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000757 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000758 // can't use this for flipping, only non-neg values allowed. :(
759 if (rowBytes != trimRowBytes) {
760 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
761 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
762 restoreGLRowLength = true;
763 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000764 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000765 if (trimRowBytes != rowBytes || swFlipY) {
766 // copy data into our new storage, skipping the trailing bytes
767 size_t trimSize = height * trimRowBytes;
768 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000769 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000770 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000771 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000772 char* dst = (char*)tempStorage.reset(trimSize);
773 for (int y = 0; y < height; y++) {
774 memcpy(dst, src, trimRowBytes);
775 if (swFlipY) {
776 src -= rowBytes;
777 } else {
778 src += rowBytes;
779 }
780 dst += trimRowBytes;
781 }
782 // now point data to our copied version
783 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000784 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000785 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000786 if (glFlipY) {
787 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
788 }
789 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000790 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000791 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000792 if (isNewTexture &&
793 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000794 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000795 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000796 if (useTexStorage) {
797 // We never resize or change formats of textures. We don't use
798 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000799 GL_ALLOC_CALL(this->glInterface(),
800 TexStorage2D(GR_GL_TEXTURE_2D,
801 1, // levels
802 internalFormat,
803 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000804 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000805 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
806 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
807 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000808 GL_ALLOC_CALL(this->glInterface(),
809 CompressedTexImage2D(GR_GL_TEXTURE_2D,
810 0, // level
811 internalFormat,
812 desc.fWidth, desc.fHeight,
813 0, // border
814 imageSize,
815 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000816 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000817 GL_ALLOC_CALL(this->glInterface(),
818 TexImage2D(GR_GL_TEXTURE_2D,
819 0, // level
820 internalFormat,
821 desc.fWidth, desc.fHeight,
822 0, // border
823 externalFormat, externalType,
824 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000825 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000826 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000827 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000828 if (error != GR_GL_NO_ERROR) {
829 succeeded = false;
830 } else {
831 // if we have data and we used TexStorage to create the texture, we
832 // now upload with TexSubImage.
833 if (NULL != data && useTexStorage) {
834 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
835 0, // level
836 left, top,
837 width, height,
838 externalFormat, externalType,
839 data));
840 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000841 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000842 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000843 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000844 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000845 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000846 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
847 0, // level
848 left, top,
849 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000850 externalFormat, externalType, data));
851 }
852
853 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000854 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000855 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000856 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000857 if (glFlipY) {
858 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
859 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000860 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000861}
862
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000863namespace {
864bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
865 int sampleCount,
866 GrGLenum format,
867 int width, int height) {
868 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
869 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
870 bool created = false;
871 if (GrGLCaps::kNVDesktop_CoverageAAType ==
872 ctxInfo.caps().coverageAAType()) {
873 const GrGLCaps::MSAACoverageMode& mode =
874 ctxInfo.caps().getMSAACoverageMode(sampleCount);
875 GL_ALLOC_CALL(ctxInfo.interface(),
876 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
877 mode.fCoverageSampleCnt,
878 mode.fColorSampleCnt,
879 format,
880 width, height));
881 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
882 }
883 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000884 // glRBMS will fail if requested samples is > max samples.
885 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000886 GL_ALLOC_CALL(ctxInfo.interface(),
887 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
888 sampleCount,
889 format,
890 width, height));
891 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
892 }
893 return created;
894}
895}
896
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897bool GrGpuGL::createRenderTargetObjects(int width, int height,
898 GrGLuint texID,
899 GrGLRenderTarget::Desc* desc) {
900 desc->fMSColorRenderbufferID = 0;
901 desc->fRTFBOID = 0;
902 desc->fTexFBOID = 0;
903 desc->fOwnIDs = true;
904
905 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000906
bsalomon@google.comab15d612011-08-09 12:57:56 +0000907 GrGLenum msColorFormat = 0; // suppress warning
908
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910 if (!desc->fTexFBOID) {
911 goto FAILED;
912 }
913
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000914
915 // If we are using multisampling we will create two FBOS. We render
916 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000917 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000918 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000919 goto FAILED;
920 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000921 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
922 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000923 if (!desc->fRTFBOID ||
924 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000925 !this->configToGLFormats(desc->fConfig,
926 // GLES requires sized internal formats
927 kES2_GrGLBinding == this->glBinding(),
928 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 goto FAILED;
930 }
931 } else {
932 desc->fRTFBOID = desc->fTexFBOID;
933 }
934
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000935 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000936 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000937 if (desc->fRTFBOID != desc->fTexFBOID) {
938 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000939 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000941 if (!renderbuffer_storage_msaa(fGLContextInfo,
942 desc->fSampleCnt,
943 msColorFormat,
944 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945 goto FAILED;
946 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000947 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
948 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000949 GR_GL_COLOR_ATTACHMENT0,
950 GR_GL_RENDERBUFFER,
951 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000952 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000953 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
954 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
955 goto FAILED;
956 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000957 fGLContextInfo.caps().markConfigAsValidColorAttachment(
958 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000959 }
960 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000961 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000962
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000963 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
964 GR_GL_COLOR_ATTACHMENT0,
965 GR_GL_TEXTURE_2D,
966 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000967 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000968 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
969 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
970 goto FAILED;
971 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000972 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000973 }
974
975 return true;
976
977FAILED:
978 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000979 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000980 }
981 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000982 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000983 }
984 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000985 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000986 }
987 return false;
988}
989
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000990// good to set a break-point here to know when createTexture fails
991static GrTexture* return_null_texture() {
992// GrAssert(!"null texture");
993 return NULL;
994}
995
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000996#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000997static size_t as_size_t(int x) {
998 return x;
999}
1000#endif
1001
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001002GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001003 const void* srcData,
1004 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001005
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001006 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001007 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001008
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001009 // Attempt to catch un- or wrongly initialized sample counts;
1010 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1011
robertphillips@google.com32716282012-06-04 12:48:45 +00001012 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +00001013 glTexDesc.fWidth = desc.fWidth;
1014 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001015 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +00001016 glTexDesc.fSampleCnt = desc.fSampleCnt;
1017
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001018 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001019
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001020 glRTDesc.fMSColorRenderbufferID = 0;
1021 glRTDesc.fRTFBOID = 0;
1022 glRTDesc.fTexFBOID = 0;
1023 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001024 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001025
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001026 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001027
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001028 const Caps& caps = this->getCaps();
1029
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001030 // We keep GrRenderTargets in GL's normal orientation so that they
1031 // can be drawn to by the outside world without the client having
1032 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001033 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001034 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001035
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001036 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001037 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001038 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001039 //GrPrintf("MSAA RT requested but not supported on this platform.");
1040 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 }
1042
reed@google.comac10a2d2010-12-22 21:39:39 +00001043 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001044 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1045 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001046 return return_null_texture();
1047 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001048 }
1049
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001050 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001051 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001052 // provides a hint about how this texture will be used
1053 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1054 GR_GL_TEXTURE_USAGE,
1055 GR_GL_FRAMEBUFFER_ATTACHMENT));
1056 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001057 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001058 return return_null_texture();
1059 }
1060
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001061 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001062 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001063
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001064 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1065 // drivers have a bug where an FBO won't be complete if it includes a
1066 // texture that is not mipmap complete (considering the filter in use).
1067 GrGLTexture::TexParams initialTexParams;
1068 // we only set a subset here so invalidate first
1069 initialTexParams.invalidate();
1070 initialTexParams.fFilter = GR_GL_NEAREST;
1071 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1072 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001073 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1074 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001075 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1077 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001078 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1080 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001081 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1083 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001084 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001085 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1086 glTexDesc.fWidth, glTexDesc.fHeight,
1087 desc.fConfig, srcData, rowBytes)) {
1088 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1089 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001090 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001091
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001092 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001094 // unbind the texture from the texture unit before binding it to the frame buffer
1095 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1096
bsalomon@google.com99621082011-11-15 16:47:16 +00001097 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1098 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001099 glTexDesc.fTextureID,
1100 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001101 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001102 return return_null_texture();
1103 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001104 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001106 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001107 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001108 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001109#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001110 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1111 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001112#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113 return tex;
1114}
1115
1116namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001117
1118const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1119
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1121 GrGLuint rb,
1122 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123 // we shouldn't ever know one size and not the other
1124 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1125 (kUnknownBitCount == format->fTotalBits));
1126 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1129 (GrGLint*)&format->fStencilBits);
1130 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1133 (GrGLint*)&format->fTotalBits);
1134 format->fTotalBits += format->fStencilBits;
1135 } else {
1136 format->fTotalBits = format->fStencilBits;
1137 }
1138 }
1139}
1140}
1141
1142bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1143 int width, int height) {
1144
1145 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001146 // 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 +00001147 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001148 GrAssert(width >= rt->width());
1149 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150
1151 int samples = rt->numSamples();
1152 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001153 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 if (!sbID) {
1155 return false;
1156 }
1157
1158 GrGLStencilBuffer* sb = NULL;
1159
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001160 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001162 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 // we start with the last stencil format that succeeded in hopes
1164 // that we won't go through this loop more than once after the
1165 // first (painful) stencil creation.
1166 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001167 const GrGLCaps::StencilFormat& sFmt =
1168 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001169 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001170 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001172 bool created;
1173 if (samples > 0) {
1174 created = renderbuffer_storage_msaa(fGLContextInfo,
1175 samples,
1176 sFmt.fInternalFormat,
1177 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001179 GL_ALLOC_CALL(this->glInterface(),
1180 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001181 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001182 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001183 created =
1184 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001186 if (created) {
1187 // After sized formats we attempt an unsized format and take
1188 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001189 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001190 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001191 sb = SkNEW_ARGS(GrGLStencilBuffer,
1192 (this, sbID, width, height,
1193 samples, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1195 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001196 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 return true;
1199 }
1200 sb->abandon(); // otherwise we lose sbID
1201 sb->unref();
1202 }
1203 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001205 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206}
1207
1208bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1209 GrRenderTarget* rt) {
1210 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1211
1212 GrGLuint fbo = glrt->renderFBOID();
1213
1214 if (NULL == sb) {
1215 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 GR_GL_STENCIL_ATTACHMENT,
1218 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 GR_GL_DEPTH_ATTACHMENT,
1221 GR_GL_RENDERBUFFER, 0));
1222#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001223 GrGLenum status;
1224 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1226#endif
1227 }
1228 return true;
1229 } else {
1230 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1231 GrGLuint rb = glsb->renderbufferID();
1232
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001233 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1235 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 GR_GL_STENCIL_ATTACHMENT,
1237 GR_GL_RENDERBUFFER, rb));
1238 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001239 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 GR_GL_DEPTH_ATTACHMENT,
1241 GR_GL_RENDERBUFFER, rb));
1242 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001243 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 GR_GL_DEPTH_ATTACHMENT,
1245 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001248 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001249 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001250 glsb->format())) {
1251 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1252 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001253 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001254 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001256 if (glsb->format().fPacked) {
1257 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1258 GR_GL_DEPTH_ATTACHMENT,
1259 GR_GL_RENDERBUFFER, 0));
1260 }
1261 return false;
1262 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001263 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001264 rt->config(),
1265 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001266 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001267 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001268 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001270}
1271
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001272////////////////////////////////////////////////////////////////////////////////
1273
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001274GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001275 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001276 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001278 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001279 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001280 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001282 GL_ALLOC_CALL(this->glInterface(),
1283 BufferData(GR_GL_ARRAY_BUFFER,
1284 size,
1285 NULL, // data ptr
1286 dynamic ? GR_GL_DYNAMIC_DRAW :
1287 GR_GL_STATIC_DRAW));
1288 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 // deleting bound buffer does implicit bind to 0
1291 fHWGeometryState.fVertexBuffer = NULL;
1292 return NULL;
1293 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001294 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer,
1295 (this, id,
1296 size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 fHWGeometryState.fVertexBuffer = vertexBuffer;
1298 return vertexBuffer;
1299 }
1300 return NULL;
1301}
1302
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001303GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001304 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001305 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001306 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001307 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001308 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001310 GL_ALLOC_CALL(this->glInterface(),
1311 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1312 size,
1313 NULL, // data ptr
1314 dynamic ? GR_GL_DYNAMIC_DRAW :
1315 GR_GL_STATIC_DRAW));
1316 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001317 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001318 // deleting bound buffer does implicit bind to 0
1319 fHWGeometryState.fIndexBuffer = NULL;
1320 return NULL;
1321 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001322 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer,
1323 (this, id, size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 fHWGeometryState.fIndexBuffer = indexBuffer;
1325 return indexBuffer;
1326 }
1327 return NULL;
1328}
1329
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001330GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001331 GrAssert(fCaps.fPathStencilingSupport);
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001332 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001333}
1334
bsalomon@google.coma3201942012-06-21 19:58:20 +00001335void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001336 const GrDrawState& drawState = this->getDrawState();
1337 const GrGLRenderTarget* rt =
1338 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1339
1340 GrAssert(NULL != rt);
1341 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001342
bsalomon@google.coma3201942012-06-21 19:58:20 +00001343 if (fScissorState.fEnabled) {
1344 GrGLIRect scissor;
1345 scissor.setRelativeTo(vp,
1346 fScissorState.fRect.fLeft,
1347 fScissorState.fRect.fTop,
1348 fScissorState.fRect.width(),
1349 fScissorState.fRect.height());
1350 // if the scissor fully contains the viewport then we fall through and
1351 // disable the scissor test.
1352 if (!scissor.contains(vp)) {
1353 if (fHWScissorSettings.fRect != scissor) {
1354 scissor.pushToGLScissor(this->glInterface());
1355 fHWScissorSettings.fRect = scissor;
1356 }
1357 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1358 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1359 fHWScissorSettings.fEnabled = kYes_TriState;
1360 }
1361 return;
1362 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001364 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001365 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001366 fHWScissorSettings.fEnabled = kNo_TriState;
1367 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001368 }
1369}
1370
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001371void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001372 const GrDrawState& drawState = this->getDrawState();
1373 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001374 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001375 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001376
bsalomon@google.com74b98712011-11-11 19:46:16 +00001377 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001378 if (NULL != rect) {
1379 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001380 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001381 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001382 if (clippedRect.intersect(rtRect)) {
1383 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001384 } else {
1385 return;
1386 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001387 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001388 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001389 GrAutoTRestore<ScissorState> asr(&fScissorState);
1390 fScissorState.fEnabled = (NULL != rect);
1391 if (fScissorState.fEnabled) {
1392 fScissorState.fRect = *rect;
1393 }
1394 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001395
1396 GrGLfloat r, g, b, a;
1397 static const GrGLfloat scale255 = 1.f / 255.f;
1398 a = GrColorUnpackA(color) * scale255;
1399 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001400 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001401 scaleRGB *= a;
1402 }
1403 r = GrColorUnpackR(color) * scaleRGB;
1404 g = GrColorUnpackG(color) * scaleRGB;
1405 b = GrColorUnpackB(color) * scaleRGB;
1406
1407 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001408 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001409 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001410 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001411}
1412
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001413void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001414 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001415 return;
1416 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001417
1418 this->flushRenderTarget(&GrIRect::EmptyIRect());
1419
bsalomon@google.coma3201942012-06-21 19:58:20 +00001420 GrAutoTRestore<ScissorState> asr(&fScissorState);
1421 fScissorState.fEnabled = false;
1422 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001423
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001424 GL_CALL(StencilMask(0xffffffff));
1425 GL_CALL(ClearStencil(0));
1426 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001427 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001428}
1429
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001430void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001431 const GrDrawState& drawState = this->getDrawState();
1432 const GrRenderTarget* rt = drawState.getRenderTarget();
1433 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001434
1435 // this should only be called internally when we know we have a
1436 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001437 GrAssert(NULL != rt->getStencilBuffer());
1438 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001439#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001440 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001441 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001442#else
1443 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001444 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001445 // turned into draws. Our contract on GrDrawTarget says that
1446 // changing the clip between stencil passes may or may not
1447 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001448 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001449#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001450 GrGLint value;
1451 if (insideClip) {
1452 value = (1 << (stencilBitCount - 1));
1453 } else {
1454 value = 0;
1455 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001456 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001457
1458 GrAutoTRestore<ScissorState> asr(&fScissorState);
1459 fScissorState.fEnabled = true;
1460 fScissorState.fRect = rect;
1461 this->flushScissor();
1462
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001463 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001464 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001465 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001466 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001467}
1468
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001469void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001470 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001471}
1472
bsalomon@google.comc4364992011-11-07 15:54:49 +00001473bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1474 int left, int top,
1475 int width, int height,
1476 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001477 size_t rowBytes) const {
1478 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001479 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001480 return false;
1481 }
1482
1483 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001484 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001485 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001486 return true;
1487 }
1488 // If we have to do memcpys to handle rowBytes then y-flip is free
1489 // Note the rowBytes might be tight to the passed in data, but if data
1490 // gets clipped in x to the target the rowBytes will no longer be tight.
1491 if (left >= 0 && (left + width) < renderTarget->width()) {
1492 return 0 == rowBytes ||
1493 GrBytesPerPixel(config) * width == rowBytes;
1494 } else {
1495 return false;
1496 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001497}
1498
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001499bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001500 int left, int top,
1501 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001502 GrPixelConfig config,
1503 void* buffer,
1504 size_t rowBytes,
1505 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001506 GrGLenum format;
1507 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001508 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001509 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001511 size_t bpp = GrBytesPerPixel(config);
1512 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1513 &left, &top, &width, &height,
1514 const_cast<const void**>(&buffer),
1515 &rowBytes)) {
1516 return false;
1517 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001518
bsalomon@google.comc6980972011-11-02 19:57:21 +00001519 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001520 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001521 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001522 switch (tgt->getResolveType()) {
1523 case GrGLRenderTarget::kCantResolve_ResolveType:
1524 return false;
1525 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001526 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001527 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001528 break;
1529 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001530 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001531 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001532 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1533 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001534 break;
1535 default:
1536 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001537 }
1538
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001539 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001540
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001541 // the read rect is viewport-relative
1542 GrGLIRect readRect;
1543 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001544
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001545 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001546 if (0 == rowBytes) {
1547 rowBytes = tightRowBytes;
1548 }
1549 size_t readDstRowBytes = tightRowBytes;
1550 void* readDst = buffer;
1551
1552 // determine if GL can read using the passed rowBytes or if we need
1553 // a scratch buffer.
1554 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1555 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001556 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001557 GrAssert(!(rowBytes % sizeof(GrColor)));
1558 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1559 readDstRowBytes = rowBytes;
1560 } else {
1561 scratch.reset(tightRowBytes * height);
1562 readDst = scratch.get();
1563 }
1564 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001565 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001566 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1567 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001568 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1569 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001570 format, type, readDst));
1571 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001572 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001573 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1574 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001575 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001576 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1577 invertY = true;
1578 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001579
1580 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001581 // API presents top-to-bottom. We must preserve the padding contents. Note
1582 // that the above readPixels did not overwrite the padding.
1583 if (readDst == buffer) {
1584 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001585 if (!invertY) {
1586 scratch.reset(tightRowBytes);
1587 void* tmpRow = scratch.get();
1588 // flip y in-place by rows
1589 const int halfY = height >> 1;
1590 char* top = reinterpret_cast<char*>(buffer);
1591 char* bottom = top + (height - 1) * rowBytes;
1592 for (int y = 0; y < halfY; y++) {
1593 memcpy(tmpRow, top, tightRowBytes);
1594 memcpy(top, bottom, tightRowBytes);
1595 memcpy(bottom, tmpRow, tightRowBytes);
1596 top += rowBytes;
1597 bottom -= rowBytes;
1598 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001599 }
1600 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001601 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001602 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001603 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001604 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001605 char* dst = reinterpret_cast<char*>(buffer);
1606 if (!invertY) {
1607 dst += (height-1) * rowBytes;
1608 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001609 for (int y = 0; y < height; y++) {
1610 memcpy(dst, src, tightRowBytes);
1611 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001612 if (invertY) {
1613 dst += rowBytes;
1614 } else {
1615 dst -= rowBytes;
1616 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 }
1618 }
1619 return true;
1620}
1621
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001622void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001623
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001624 GrGLRenderTarget* rt =
1625 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1626 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001627
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001628 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001629 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001631 GrGLenum status;
1632 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001633 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001634 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001635 }
1636 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001637 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001638 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001639 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001640 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001641 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 }
1643 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001644 if (NULL == bound || !bound->isEmpty()) {
1645 rt->flagAsNeedingResolve(bound);
1646 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001647}
1648
twiz@google.com0f31ca72011-03-18 17:38:11 +00001649GrGLenum gPrimitiveType2GLMode[] = {
1650 GR_GL_TRIANGLES,
1651 GR_GL_TRIANGLE_STRIP,
1652 GR_GL_TRIANGLE_FAN,
1653 GR_GL_POINTS,
1654 GR_GL_LINES,
1655 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001656};
1657
bsalomon@google.comd302f142011-03-03 13:54:13 +00001658#define SWAP_PER_DRAW 0
1659
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001660#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001661 #if GR_MAC_BUILD
1662 #include <AGL/agl.h>
1663 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001664 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001665 void SwapBuf() {
1666 DWORD procID = GetCurrentProcessId();
1667 HWND hwnd = GetTopWindow(GetDesktopWindow());
1668 while(hwnd) {
1669 DWORD wndProcID = 0;
1670 GetWindowThreadProcessId(hwnd, &wndProcID);
1671 if(wndProcID == procID) {
1672 SwapBuffers(GetDC(hwnd));
1673 }
1674 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1675 }
1676 }
1677 #endif
1678#endif
1679
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001680void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1681 uint32_t startVertex,
1682 uint32_t startIndex,
1683 uint32_t vertexCount,
1684 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001685 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1686
twiz@google.com0f31ca72011-03-18 17:38:11 +00001687 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001688
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001689 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1690 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1691
1692 // our setupGeometry better have adjusted this to zero since
1693 // DrawElements always draws from the begining of the arrays for idx 0.
1694 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001695
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001696 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1697 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001698#if SWAP_PER_DRAW
1699 glFlush();
1700 #if GR_MAC_BUILD
1701 aglSwapBuffers(aglGetCurrentContext());
1702 int set_a_break_pt_here = 9;
1703 aglSwapBuffers(aglGetCurrentContext());
1704 #elif GR_WIN32_BUILD
1705 SwapBuf();
1706 int set_a_break_pt_here = 9;
1707 SwapBuf();
1708 #endif
1709#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001710}
1711
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001712void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1713 uint32_t startVertex,
1714 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001715 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1716
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001717 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1718
1719 // our setupGeometry better have adjusted this to zero.
1720 // DrawElements doesn't take an offset so we always adjus the startVertex.
1721 GrAssert(0 == startVertex);
1722
1723 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1724 // account for startVertex in the DrawElements case. So we always
1725 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001726 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001727#if SWAP_PER_DRAW
1728 glFlush();
1729 #if GR_MAC_BUILD
1730 aglSwapBuffers(aglGetCurrentContext());
1731 int set_a_break_pt_here = 9;
1732 aglSwapBuffers(aglGetCurrentContext());
1733 #elif GR_WIN32_BUILD
1734 SwapBuf();
1735 int set_a_break_pt_here = 9;
1736 SwapBuf();
1737 #endif
1738#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001739}
1740
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001741namespace {
1742const GrStencilSettings& winding_nv_path_stencil_settings() {
1743 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1744 kIncClamp_StencilOp,
1745 kIncClamp_StencilOp,
1746 kAlwaysIfInClip_StencilFunc,
1747 ~0, ~0, ~0);
1748 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1749}
1750const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1751 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1752 kInvert_StencilOp,
1753 kInvert_StencilOp,
1754 kAlwaysIfInClip_StencilFunc,
1755 ~0, ~0, ~0);
1756 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1757}
1758}
1759
1760
1761void GrGpuGL::setStencilPathSettings(const GrPath&,
1762 GrPathFill fill,
1763 GrStencilSettings* settings) {
1764 switch (fill) {
1765 case kEvenOdd_GrPathFill:
1766 *settings = even_odd_nv_path_stencil_settings();
1767 return;
1768 case kWinding_GrPathFill:
1769 *settings = winding_nv_path_stencil_settings();
1770 return;
1771 default:
1772 GrCrash("Unexpected path fill.");
1773 }
1774}
1775
1776void GrGpuGL::onGpuStencilPath(const GrPath* path, GrPathFill fill) {
1777 GrAssert(fCaps.fPathStencilingSupport);
1778
1779 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1780 GrDrawState* drawState = this->drawState();
1781 GrAssert(NULL != drawState->getRenderTarget());
1782 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1783 return;
1784 }
1785
1786 // Decide how to manipulate the stencil buffer based on the fill rule.
1787 // Also, assert that the stencil settings we set in setStencilPathSettings
1788 // are present.
1789 GrAssert(!fStencilSettings.isTwoSided());
1790 GrGLenum fillMode;
1791 switch (fill) {
1792 case kWinding_GrPathFill:
1793 fillMode = GR_GL_COUNT_UP;
1794 GrAssert(kIncClamp_StencilOp ==
1795 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1796 GrAssert(kIncClamp_StencilOp ==
1797 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1798 break;
1799 case kEvenOdd_GrPathFill:
1800 fillMode = GR_GL_INVERT;
1801 GrAssert(kInvert_StencilOp ==
1802 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1803 GrAssert(kInvert_StencilOp ==
1804 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1805 break;
1806 default:
1807 // Only the above two fill rules are allowed.
1808 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001809 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001810 }
1811 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1812 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001813}
1814
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001815void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1816
1817 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001818
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001819 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001820 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001821 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001822 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1823 rt->renderFBOID()));
1824 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1825 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001826 // make sure we go through flushRenderTarget() since we've modified
1827 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001828 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001829 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001830 const GrIRect dirtyRect = rt->getResolveRect();
1831 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001832 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001833 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001834
bsalomon@google.coma3201942012-06-21 19:58:20 +00001835 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001836 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001837 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001838 asr.reset(&fScissorState);
1839 fScissorState.fEnabled = true;
1840 fScissorState.fRect = dirtyRect;
1841 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001842 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001843 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001844 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001845 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001846 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1847 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001848 asr.reset(&fScissorState);
1849 fScissorState.fEnabled = false;
1850 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001851 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001852 int right = r.fLeft + r.fWidth;
1853 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001854 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1855 r.fLeft, r.fBottom, right, top,
1856 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001857 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001858 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001859 }
1860}
1861
bsalomon@google.com411dad02012-06-05 20:24:20 +00001862namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863
bsalomon@google.com411dad02012-06-05 20:24:20 +00001864GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1865 static const GrGLenum gTable[] = {
1866 GR_GL_ALWAYS, // kAlways_StencilFunc
1867 GR_GL_NEVER, // kNever_StencilFunc
1868 GR_GL_GREATER, // kGreater_StencilFunc
1869 GR_GL_GEQUAL, // kGEqual_StencilFunc
1870 GR_GL_LESS, // kLess_StencilFunc
1871 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1872 GR_GL_EQUAL, // kEqual_StencilFunc,
1873 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1874 };
1875 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1876 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1877 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1878 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1879 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1880 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1881 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1882 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1883 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1884 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1885
1886 return gTable[basicFunc];
1887}
1888
1889GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1890 static const GrGLenum gTable[] = {
1891 GR_GL_KEEP, // kKeep_StencilOp
1892 GR_GL_REPLACE, // kReplace_StencilOp
1893 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1894 GR_GL_INCR, // kIncClamp_StencilOp
1895 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1896 GR_GL_DECR, // kDecClamp_StencilOp
1897 GR_GL_ZERO, // kZero_StencilOp
1898 GR_GL_INVERT, // kInvert_StencilOp
1899 };
1900 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1901 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1902 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1903 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1904 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1905 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1906 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1907 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1908 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1909 GrAssert((unsigned) op < kStencilOpCount);
1910 return gTable[op];
1911}
1912
1913void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001914 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001915 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001916 GrStencilSettings::Face grFace) {
1917 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1918 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1919 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1920
1921 GrGLint ref = settings.funcRef(grFace);
1922 GrGLint mask = settings.funcMask(grFace);
1923 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001924
1925 if (GR_GL_FRONT_AND_BACK == glFace) {
1926 // we call the combined func just in case separate stencil is not
1927 // supported.
1928 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1929 GR_GL_CALL(gl, StencilMask(writeMask));
1930 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1931 } else {
1932 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1933 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1934 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1935 }
1936}
1937}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001938
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001939void GrGpuGL::flushStencil(DrawType type) {
1940 if (kStencilPath_DrawType == type) {
1941 GrAssert(!fStencilSettings.isTwoSided());
1942 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1943 // that draws the path to the SB (glStencilFillPath)
1944 GrGLenum func =
1945 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1946 GL_CALL(PathStencilFunc(func,
1947 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1948 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1949 } else if (fHWStencilSettings != fStencilSettings) {
1950 if (fStencilSettings.isDisabled()) {
1951 if (kNo_TriState != fHWStencilTestEnabled) {
1952 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1953 fHWStencilTestEnabled = kNo_TriState;
1954 }
1955 } else {
1956 if (kYes_TriState != fHWStencilTestEnabled) {
1957 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1958 fHWStencilTestEnabled = kYes_TriState;
1959 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001960 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001961 if (!fStencilSettings.isDisabled()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001962 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001963 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001964 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001965 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001966 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001967 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001968 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001969 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001970 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001971 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001972 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001973 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001974 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001975 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001976 }
1977 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001978 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001979 }
1980}
1981
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001982void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001983 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001984 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001985 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1986 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001987 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001988 bool smoothLines = false;
1989
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001990 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001991 smoothLines = this->willUseHWAALines();
1992 if (smoothLines) {
1993 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1994 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1995 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1996 // must disable msaa to use line smoothing
1997 if (rt->isMultisampled() &&
1998 kNo_TriState != fHWAAState.fMSAAEnabled) {
1999 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2000 fHWAAState.fMSAAEnabled = kNo_TriState;
2001 }
2002 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002003 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002004 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
2005 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
2006 fHWAAState.fSmoothLineEnabled = kNo_TriState;
2007 }
2008 }
2009 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002010 if (!smoothLines && rt->isMultisampled()) {
2011 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
2012 // convex hulls of each segment appear to get filled.
2013 bool enableMSAA = kStencilPath_DrawType == type ||
2014 this->getDrawState().isHWAntialiasState();
2015 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002016 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2017 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2018 fHWAAState.fMSAAEnabled = kYes_TriState;
2019 }
2020 } else {
2021 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2022 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2023 fHWAAState.fMSAAEnabled = kNo_TriState;
2024 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002025 }
2026 }
2027 }
2028}
2029
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002030void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002031 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002032 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002033 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002034 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002035 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002036 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002037 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002038 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2039 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2040 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2041 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2042 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2043 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002044 }
2045 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002046 // any optimization to disable blending should
2047 // have already been applied and tweaked the coeffs
2048 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002049 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2050 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002051 if (blendOff) {
2052 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002053 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002054 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002055 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002056 } else {
2057 if (kYes_TriState != fHWBlendState.fEnabled) {
2058 GL_CALL(Enable(GR_GL_BLEND));
2059 fHWBlendState.fEnabled = kYes_TriState;
2060 }
2061 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2062 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002063 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2064 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002065 fHWBlendState.fSrcCoeff = srcCoeff;
2066 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002067 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002068 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002069 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2070 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002071 (!fHWBlendState.fConstColorValid ||
2072 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002073
2074 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002075 GrColorUnpackR(blendConst) / 255.f,
2076 GrColorUnpackG(blendConst) / 255.f,
2077 GrColorUnpackB(blendConst) / 255.f,
2078 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002079 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002080 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002081 fHWBlendState.fConstColor = blendConst;
2082 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002083 }
2084 }
2085 }
2086}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002087namespace {
2088
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002089// get_swizzle is only called from this .cpp so it is OK to inline it here
2090inline const GrGLenum* get_swizzle(GrPixelConfig config,
2091 const GrSamplerState& sampler,
2092 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002093 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002094 if (glCaps.textureRedSupport()) {
2095 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2096 GR_GL_RED, GR_GL_RED };
2097 return gRedSmear;
2098 } else {
2099 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2100 GR_GL_ALPHA, GR_GL_ALPHA };
2101 return gAlphaSmear;
2102 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002103 } else if (sampler.swapsRAndB()) {
2104 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2105 GR_GL_RED, GR_GL_ALPHA };
2106 return gRedBlueSwap;
2107 } else {
2108 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2109 GR_GL_BLUE, GR_GL_ALPHA };
2110 return gStraight;
2111 }
2112}
2113
2114void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002115 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2116 GR_GL_TEXTURE_SWIZZLE_RGBA,
2117 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002118}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002119
2120const GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
2121 static const GrGLenum gWrapModes[] = {
2122 GR_GL_CLAMP_TO_EDGE,
2123 GR_GL_REPEAT,
2124 GR_GL_MIRRORED_REPEAT
2125 };
2126 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2127 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2128 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2129 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2130 return gWrapModes[tm];
2131}
2132
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002133}
2134
bsalomon@google.com4c883782012-06-04 19:05:11 +00002135void GrGpuGL::flushBoundTextureAndParams(int stage) {
2136 GrDrawState* drawState = this->drawState();
bsalomon@google.com0982d352012-07-31 15:33:25 +00002137 // FIXME: Assuming at most one texture per custom stage
bsalomon@google.comcddaf342012-07-30 13:09:05 +00002138 const GrCustomStage* customStage = drawState->sampler(stage)->getCustomStage();
2139 GrGLTexture* nextTexture = static_cast<GrGLTexture*>(customStage->texture(0));
bsalomon@google.com0982d352012-07-31 15:33:25 +00002140 if (NULL != nextTexture) {
2141 // Currently we always use the texture params from the GrSamplerState. Soon custom stages
2142 // will provide their own params.
2143 const GrTextureParams& texParams = drawState->getSampler(stage).getTextureParams();
2144 this->flushBoundTextureAndParams(stage, texParams, nextTexture);
2145 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002146}
2147
bsalomon@google.comb8670992012-07-25 21:27:09 +00002148void GrGpuGL::flushBoundTextureAndParams(int stage,
2149 const GrTextureParams& params,
2150 GrGLTexture* nextTexture) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002151 GrDrawState* drawState = this->drawState();
2152
bsalomon@google.com4c883782012-06-04 19:05:11 +00002153 // true for now, but maybe not with GrEffect.
2154 GrAssert(NULL != nextTexture);
bsalomon@google.comb8670992012-07-25 21:27:09 +00002155 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2156 // 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 +00002157 // out of the "last != next" check.
bsalomon@google.comb8670992012-07-25 21:27:09 +00002158 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002159 if (NULL != texRT) {
2160 this->onResolveRenderTarget(texRT);
2161 }
2162
2163 if (fHWBoundTextures[stage] != nextTexture) {
2164 this->setTextureUnit(stage);
2165 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002166 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2167 fHWBoundTextures[stage] = nextTexture;
2168 }
2169
bsalomon@google.com4c883782012-06-04 19:05:11 +00002170 ResetTimestamp timestamp;
2171 const GrGLTexture::TexParams& oldTexParams =
2172 nextTexture->getCachedTexParams(&timestamp);
2173 bool setAll = timestamp < this->getResetTimestamp();
2174 GrGLTexture::TexParams newTexParams;
2175
bsalomon@google.comb8670992012-07-25 21:27:09 +00002176 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002177
bsalomon@google.comb8670992012-07-25 21:27:09 +00002178 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2179 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002180 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.comb8670992012-07-25 21:27:09 +00002181 get_swizzle(nextTexture->config(), drawState->getSampler(stage), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002182 sizeof(newTexParams.fSwizzleRGBA));
2183 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2184 this->setTextureUnit(stage);
2185 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2186 GR_GL_TEXTURE_MAG_FILTER,
2187 newTexParams.fFilter));
2188 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2189 GR_GL_TEXTURE_MIN_FILTER,
2190 newTexParams.fFilter));
2191 }
2192 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2193 this->setTextureUnit(stage);
2194 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2195 GR_GL_TEXTURE_WRAP_S,
2196 newTexParams.fWrapS));
2197 }
2198 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2199 this->setTextureUnit(stage);
2200 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2201 GR_GL_TEXTURE_WRAP_T,
2202 newTexParams.fWrapT));
2203 }
2204 if (this->glCaps().textureSwizzleSupport() &&
2205 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2206 oldTexParams.fSwizzleRGBA,
2207 sizeof(newTexParams.fSwizzleRGBA)))) {
2208 this->setTextureUnit(stage);
2209 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2210 this->glInterface());
2211 }
2212 nextTexture->setCachedTexParams(newTexParams,
2213 this->getResetTimestamp());
2214}
2215
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002216void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002217
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002218 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002219
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002220 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002221 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002222 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002223 fHWDitherEnabled = kYes_TriState;
2224 }
2225 } else {
2226 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002227 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002228 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002229 }
2230 }
2231
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002232 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002233 if (kNo_TriState != fHWWriteToColor) {
2234 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2235 GR_GL_FALSE, GR_GL_FALSE));
2236 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002237 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002238 } else {
2239 if (kYes_TriState != fHWWriteToColor) {
2240 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2241 fHWWriteToColor = kYes_TriState;
2242 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002243 }
2244
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002245 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002246 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002247 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002248 GL_CALL(Enable(GR_GL_CULL_FACE));
2249 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002250 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002251 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002252 GL_CALL(Enable(GR_GL_CULL_FACE));
2253 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002254 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002255 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002256 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002257 break;
2258 default:
2259 GrCrash("Unknown draw face.");
2260 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002261 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002263}
2264
2265void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002266 if (fHWGeometryState.fVertexBuffer != buffer) {
2267 fHWGeometryState.fArrayPtrsDirty = true;
2268 fHWGeometryState.fVertexBuffer = buffer;
2269 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002270}
2271
2272void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002273 if (fHWGeometryState.fVertexBuffer == buffer) {
2274 // deleting bound buffer does implied bind to 0
2275 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002276 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002277 }
2278}
2279
2280void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002281 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002282}
2283
2284void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002285 if (fHWGeometryState.fIndexBuffer == buffer) {
2286 // deleting bound buffer does implied bind to 0
2287 fHWGeometryState.fIndexBuffer = NULL;
2288 }
2289}
2290
reed@google.comac10a2d2010-12-22 21:39:39 +00002291void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2292 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002293 if (fHWBoundRenderTarget == renderTarget) {
2294 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002296}
2297
2298void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002299 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002300 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002301 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002302 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002303 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002304 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002305}
2306
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002307bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2308 bool getSizedInternalFormat,
2309 GrGLenum* internalFormat,
2310 GrGLenum* externalFormat,
2311 GrGLenum* externalType) {
2312 GrGLenum dontCare;
2313 if (NULL == internalFormat) {
2314 internalFormat = &dontCare;
2315 }
2316 if (NULL == externalFormat) {
2317 externalFormat = &dontCare;
2318 }
2319 if (NULL == externalType) {
2320 externalType = &dontCare;
2321 }
2322
reed@google.comac10a2d2010-12-22 21:39:39 +00002323 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002324 case kRGBA_8888_PM_GrPixelConfig:
2325 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002326 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002327 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002328 if (getSizedInternalFormat) {
2329 *internalFormat = GR_GL_RGBA8;
2330 } else {
2331 *internalFormat = GR_GL_RGBA;
2332 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002333 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002334 break;
2335 case kBGRA_8888_PM_GrPixelConfig:
2336 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002337 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002338 return false;
2339 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002340 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002341 if (getSizedInternalFormat) {
2342 *internalFormat = GR_GL_BGRA8;
2343 } else {
2344 *internalFormat = GR_GL_BGRA;
2345 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002346 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002347 if (getSizedInternalFormat) {
2348 *internalFormat = GR_GL_RGBA8;
2349 } else {
2350 *internalFormat = GR_GL_RGBA;
2351 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002352 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002353 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002354 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002355 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002356 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002357 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002358 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002359 if (getSizedInternalFormat) {
2360 if (this->glBinding() == kDesktop_GrGLBinding) {
2361 return false;
2362 } else {
2363 *internalFormat = GR_GL_RGB565;
2364 }
2365 } else {
2366 *internalFormat = GR_GL_RGB;
2367 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002368 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002369 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002370 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002371 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002372 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002373 if (getSizedInternalFormat) {
2374 *internalFormat = GR_GL_RGBA4;
2375 } else {
2376 *internalFormat = GR_GL_RGBA;
2377 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002378 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002379 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002380 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002381 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002382 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002383 // glCompressedTexImage doesn't take external params
2384 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002385 // no sized/unsized internal format distinction here
2386 *internalFormat = GR_GL_PALETTE8_RGBA8;
2387 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002388 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002389 } else {
2390 return false;
2391 }
2392 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002393 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002394 if (this->glCaps().textureRedSupport()) {
2395 *internalFormat = GR_GL_RED;
2396 *externalFormat = GR_GL_RED;
2397 if (getSizedInternalFormat) {
2398 *internalFormat = GR_GL_R8;
2399 } else {
2400 *internalFormat = GR_GL_RED;
2401 }
2402 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002403 } else {
2404 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002405 *externalFormat = GR_GL_ALPHA;
2406 if (getSizedInternalFormat) {
2407 *internalFormat = GR_GL_ALPHA8;
2408 } else {
2409 *internalFormat = GR_GL_ALPHA;
2410 }
2411 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002412 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002413 break;
2414 default:
2415 return false;
2416 }
2417 return true;
2418}
2419
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002420void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002421 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002422 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002423 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002424 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002425 }
2426}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002427
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002428void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002429 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002430 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002431 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002432 }
2433}
2434
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002435void GrGpuGL::setBuffers(bool indexed,
2436 int* extraVertexOffset,
2437 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002438
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002440
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002441 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2442
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002444 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 case kBuffer_GeometrySrcType:
2446 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002447 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002448 break;
2449 case kArray_GeometrySrcType:
2450 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002451 this->finalizeReservedVertices();
2452 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2453 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002454 break;
2455 default:
2456 vbuf = NULL; // suppress warning
2457 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002458 }
2459
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460 GrAssert(NULL != vbuf);
2461 GrAssert(!vbuf->isLocked());
2462 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002463 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002464 fHWGeometryState.fArrayPtrsDirty = true;
2465 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002466 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002467
2468 if (indexed) {
2469 GrAssert(NULL != extraIndexOffset);
2470
2471 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002472 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002473 case kBuffer_GeometrySrcType:
2474 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002475 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002476 break;
2477 case kArray_GeometrySrcType:
2478 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002479 this->finalizeReservedIndices();
2480 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2481 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002482 break;
2483 default:
2484 ibuf = NULL; // suppress warning
2485 GrCrash("Unknown geometry src type!");
2486 }
2487
2488 GrAssert(NULL != ibuf);
2489 GrAssert(!ibuf->isLocked());
2490 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002491 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002492 fHWGeometryState.fIndexBuffer = ibuf;
2493 }
2494 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002495}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002496