blob: 03462e743bb4e93a13417951665ff61c9be29513 [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()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000338 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000339 }
340
341 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000342 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000343 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000344}
345
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000346bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
347 if (kUnknown_CanPreserveUnpremulRoundtrip ==
348 fCanPreserveUnpremulRoundtrip) {
349
350 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
351 uint32_t* srcData = data.get();
352 uint32_t* firstRead = data.get() + 256 * 256;
353 uint32_t* secondRead = data.get() + 2 * 256 * 256;
354
355 for (int y = 0; y < 256; ++y) {
356 for (int x = 0; x < 256; ++x) {
357 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
358 color[3] = y;
359 color[2] = x;
360 color[1] = x;
361 color[0] = x;
362 }
363 }
364
365 // We have broader support for read/write pixels on render targets
366 // than on textures.
367 GrTextureDesc dstDesc;
368 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
369 kNoStencil_GrTextureFlagBit;
370 dstDesc.fWidth = 256;
371 dstDesc.fHeight = 256;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000372 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000373
374 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
375 if (!dstTex.get()) {
376 return false;
377 }
378 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
379 GrAssert(NULL != rt);
380
381 bool failed = true;
382 static const UnpremulConversion gMethods[] = {
383 kUpOnWrite_DownOnRead_UnpremulConversion,
384 kDownOnWrite_UpOnRead_UnpremulConversion,
385 };
386
387 // pretend that we can do the roundtrip to avoid recursive calls to
388 // this function
389 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
390 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
391 fUnpremulConversion = gMethods[i];
392 rt->writePixels(0, 0,
393 256, 256,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000394 kRGBA_8888_GrPixelConfig, srcData, 0,
395 GrContext::kUnpremul_PixelOpsFlag);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000396 rt->readPixels(0, 0,
397 256, 256,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000398 kRGBA_8888_GrPixelConfig, firstRead, 0,
399 GrContext::kUnpremul_PixelOpsFlag);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000400 rt->writePixels(0, 0,
401 256, 256,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000402 kRGBA_8888_GrPixelConfig, firstRead, 0,
403 GrContext::kUnpremul_PixelOpsFlag);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000404 rt->readPixels(0, 0,
405 256, 256,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000406 kRGBA_8888_GrPixelConfig, secondRead, 0,
407 GrContext::kUnpremul_PixelOpsFlag);
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000408 failed = false;
409 for (int j = 0; j < 256 * 256; ++j) {
410 if (firstRead[j] != secondRead[j]) {
411 failed = true;
412 break;
413 }
414 }
415 }
416 fCanPreserveUnpremulRoundtrip = failed ?
417 kNo_CanPreserveUnpremulRoundtrip :
418 kYes_CanPreserveUnpremulRoundtrip;
419 }
420
421 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
422 return true;
423 } else {
424 return false;
425 }
426}
427
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000428GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000429 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
430 return GrPixelConfigSwapRAndB(config);
431 } else {
432 return config;
433 }
434}
435
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000436GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000437 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000438 return GrPixelConfigSwapRAndB(config);
439 } else {
440 return config;
441 }
442}
443
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000444bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
445 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
446}
447
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000448void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000449 if (gPrintStartupSpew && !fPrintedCaps) {
450 fPrintedCaps = true;
451 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000452 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000453 }
454
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000455 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000456 GL_CALL(Disable(GR_GL_DEPTH_TEST));
457 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000458
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000459 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
460 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000461
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000462 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000463 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000464 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000465 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
466 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
467 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
468 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000469 if (this->glCaps().imagingSupport()) {
470 GL_CALL(Disable(GR_GL_COLOR_TABLE));
471 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000472 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
473 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
474 // Since ES doesn't support glPointSize at all we always use the VS to
475 // set the point size
476 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
477
478 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
479 // currently part of our gl interface. There are probably others as
480 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000481 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000482 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000483 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000484
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000486 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000487
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000488 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000489 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000490
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000491 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000492
tomhudson@google.com93813632011-10-27 20:21:16 +0000493 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000494 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000495 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000496
bsalomon@google.coma3201942012-06-21 19:58:20 +0000497 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000498
bsalomon@google.coma3201942012-06-21 19:58:20 +0000499 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000501 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000502 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000503
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 fHWGeometryState.fIndexBuffer = NULL;
505 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000506
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000507 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000508
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000509 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000510
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000511 fHWPathMatrixState.invalidate();
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000512 if (fCaps.fPathStencilingSupport) {
513 // we don't use the model view matrix.
514 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
515 GL_CALL(LoadIdentity());
516 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000517
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000518 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000519 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000520 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
521 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000522 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000523 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
524 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000525 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000526 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
527 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000528 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000529 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
530 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000531
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000532 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000533
534 // Third party GL code may have left vertex attributes enabled. Some GL
535 // implementations (osmesa) may read vetex attributes that are not required
536 // by the current shader. Therefore, we have to ensure that only the
537 // attributes we require for the current draw are enabled or we may cause an
538 // invalid read.
539
540 // Disable all vertex layout bits so that next flush will assume all
541 // optional vertex attributes are disabled.
542 fHWGeometryState.fVertexLayout = 0;
543
544 // We always use the this attribute and assume it is always enabled.
545 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
546 GL_CALL(EnableVertexAttribArray(posAttrIdx));
547 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000548 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000549 if (va != posAttrIdx) {
550 GL_CALL(DisableVertexAttribArray(va));
551 }
552 }
553
554 fHWProgramID = 0;
555 fHWConstAttribColor = GrColor_ILLEGAL;
556 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000557}
558
bsalomon@google.come269f212011-11-07 13:29:52 +0000559GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000560 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000561 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000562 return NULL;
563 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000564
robertphillips@google.com32716282012-06-04 12:48:45 +0000565 // next line relies on PlatformTextureDesc's flags matching GrTexture's
566 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000567 glTexDesc.fWidth = desc.fWidth;
568 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000569 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000570 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000571 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
572 glTexDesc.fOwnsID = false;
573 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
574
575 GrGLTexture* texture = NULL;
576 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
577 GrGLRenderTarget::Desc glRTDesc;
578 glRTDesc.fRTFBOID = 0;
579 glRTDesc.fTexFBOID = 0;
580 glRTDesc.fMSColorRenderbufferID = 0;
581 glRTDesc.fOwnIDs = true;
582 glRTDesc.fConfig = desc.fConfig;
583 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000584 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
585 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000586 glTexDesc.fTextureID,
587 &glRTDesc)) {
588 return NULL;
589 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000590 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000591 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000592 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000593 }
594 if (NULL == texture) {
595 return NULL;
596 }
597
598 this->setSpareTextureUnit();
599 return texture;
600}
601
602GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
603 GrGLRenderTarget::Desc glDesc;
604 glDesc.fConfig = desc.fConfig;
605 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
606 glDesc.fMSColorRenderbufferID = 0;
607 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
608 glDesc.fSampleCnt = desc.fSampleCnt;
609 glDesc.fOwnIDs = false;
610 GrGLIRect viewport;
611 viewport.fLeft = 0;
612 viewport.fBottom = 0;
613 viewport.fWidth = desc.fWidth;
614 viewport.fHeight = desc.fHeight;
615
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000616 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
617 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000618 if (desc.fStencilBits) {
619 GrGLStencilBuffer::Format format;
620 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
621 format.fPacked = false;
622 format.fStencilBits = desc.fStencilBits;
623 format.fTotalBits = desc.fStencilBits;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000624 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
625 (this,
626 0,
627 desc.fWidth,
628 desc.fHeight,
629 desc.fSampleCnt,
630 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000631 tgt->setStencilBuffer(sb);
632 sb->unref();
633 }
634 return tgt;
635}
636
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000637////////////////////////////////////////////////////////////////////////////////
638
bsalomon@google.com6f379512011-11-16 20:36:03 +0000639void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
640 int left, int top, int width, int height,
641 GrPixelConfig config, const void* buffer,
642 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000643 if (NULL == buffer) {
644 return;
645 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000646 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
647
bsalomon@google.com6f379512011-11-16 20:36:03 +0000648 this->setSpareTextureUnit();
649 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
650 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000651 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000652 desc.fWidth = glTex->width();
653 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000654 desc.fConfig = glTex->config();
655 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000656 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000657 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000658
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000659 this->uploadTexData(desc, false,
660 left, top, width, height,
661 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000662}
663
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000664namespace {
665bool adjust_pixel_ops_params(int surfaceWidth,
666 int surfaceHeight,
667 size_t bpp,
668 int* left, int* top, int* width, int* height,
669 const void** data,
670 size_t* rowBytes) {
671 if (!*rowBytes) {
672 *rowBytes = *width * bpp;
673 }
674
675 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
676 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
677
678 if (!subRect.intersect(bounds)) {
679 return false;
680 }
681 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
682 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
683
684 *left = subRect.fLeft;
685 *top = subRect.fTop;
686 *width = subRect.width();
687 *height = subRect.height();
688 return true;
689}
690}
691
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000692bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
693 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000694 int left, int top, int width, int height,
695 GrPixelConfig dataConfig,
696 const void* data,
697 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000698 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000699
700 size_t bpp = GrBytesPerPixel(dataConfig);
701 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
702 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000703 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000704 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000705 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000706
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000707 // in case we need a temporary, trimmed copy of the src pixels
708 SkAutoSMalloc<128 * 128> tempStorage;
709
bsalomon@google.com313f0192012-07-10 17:21:02 +0000710 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000711 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000712 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000713 this->glCaps().texStorageSupport();
bsalomon@google.com313f0192012-07-10 17:21:02 +0000714
715 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
716 // 565 is not a sized internal format on desktop GL. So on desktop with
717 // 565 we always use an unsized internal format to let the system pick
718 // the best sized format to convert the 565 data to. Since TexStorage
719 // only allows sized internal formats we will instead use TexImage2D.
720 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000721 }
722
723 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000724 GrGLenum externalFormat;
725 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000726 // glTexStorage requires sized internal formats on both desktop and ES. ES
727 // glTexImage requires an unsized format.
728 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
729 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000730 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000731 }
732
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000733 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000734 // paletted textures cannot be updated
735 return false;
736 }
737
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000738 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000739 * check whether to allocate a temporary buffer for flipping y or
740 * because our srcData has extra bytes past each row. If so, we need
741 * to trim those off here, since GL ES may not let us specify
742 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000743 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000744 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000745 bool swFlipY = false;
746 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000747 if (NULL != data) {
748 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000749 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000750 glFlipY = true;
751 } else {
752 swFlipY = true;
753 }
754 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000755 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000756 // can't use this for flipping, only non-neg values allowed. :(
757 if (rowBytes != trimRowBytes) {
758 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
759 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
760 restoreGLRowLength = true;
761 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000762 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000763 if (trimRowBytes != rowBytes || swFlipY) {
764 // copy data into our new storage, skipping the trailing bytes
765 size_t trimSize = height * trimRowBytes;
766 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000767 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000768 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000769 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000770 char* dst = (char*)tempStorage.reset(trimSize);
771 for (int y = 0; y < height; y++) {
772 memcpy(dst, src, trimRowBytes);
773 if (swFlipY) {
774 src -= rowBytes;
775 } else {
776 src += rowBytes;
777 }
778 dst += trimRowBytes;
779 }
780 // now point data to our copied version
781 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000782 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000783 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000784 if (glFlipY) {
785 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
786 }
787 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000788 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000789 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000790 if (isNewTexture &&
791 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000792 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000793 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000794 if (useTexStorage) {
795 // We never resize or change formats of textures. We don't use
796 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000797 GL_ALLOC_CALL(this->glInterface(),
798 TexStorage2D(GR_GL_TEXTURE_2D,
799 1, // levels
800 internalFormat,
801 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000802 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000803 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
804 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
805 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000806 GL_ALLOC_CALL(this->glInterface(),
807 CompressedTexImage2D(GR_GL_TEXTURE_2D,
808 0, // level
809 internalFormat,
810 desc.fWidth, desc.fHeight,
811 0, // border
812 imageSize,
813 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000814 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000815 GL_ALLOC_CALL(this->glInterface(),
816 TexImage2D(GR_GL_TEXTURE_2D,
817 0, // level
818 internalFormat,
819 desc.fWidth, desc.fHeight,
820 0, // border
821 externalFormat, externalType,
822 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000823 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000824 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000825 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000826 if (error != GR_GL_NO_ERROR) {
827 succeeded = false;
828 } else {
829 // if we have data and we used TexStorage to create the texture, we
830 // now upload with TexSubImage.
831 if (NULL != data && useTexStorage) {
832 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
833 0, // level
834 left, top,
835 width, height,
836 externalFormat, externalType,
837 data));
838 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000839 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000840 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000841 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000842 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000843 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000844 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
845 0, // level
846 left, top,
847 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000848 externalFormat, externalType, data));
849 }
850
851 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000852 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000853 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000854 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000855 if (glFlipY) {
856 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
857 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000858 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000859}
860
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000861namespace {
862bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
863 int sampleCount,
864 GrGLenum format,
865 int width, int height) {
866 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
867 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
868 bool created = false;
869 if (GrGLCaps::kNVDesktop_CoverageAAType ==
870 ctxInfo.caps().coverageAAType()) {
871 const GrGLCaps::MSAACoverageMode& mode =
872 ctxInfo.caps().getMSAACoverageMode(sampleCount);
873 GL_ALLOC_CALL(ctxInfo.interface(),
874 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
875 mode.fCoverageSampleCnt,
876 mode.fColorSampleCnt,
877 format,
878 width, height));
879 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
880 }
881 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000882 // glRBMS will fail if requested samples is > max samples.
883 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000884 GL_ALLOC_CALL(ctxInfo.interface(),
885 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
886 sampleCount,
887 format,
888 width, height));
889 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
890 }
891 return created;
892}
893}
894
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000895bool GrGpuGL::createRenderTargetObjects(int width, int height,
896 GrGLuint texID,
897 GrGLRenderTarget::Desc* desc) {
898 desc->fMSColorRenderbufferID = 0;
899 desc->fRTFBOID = 0;
900 desc->fTexFBOID = 0;
901 desc->fOwnIDs = true;
902
903 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904
bsalomon@google.comab15d612011-08-09 12:57:56 +0000905 GrGLenum msColorFormat = 0; // suppress warning
906
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000907 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 if (!desc->fTexFBOID) {
909 goto FAILED;
910 }
911
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912
913 // If we are using multisampling we will create two FBOS. We render
914 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000915 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000916 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000917 goto FAILED;
918 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
920 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921 if (!desc->fRTFBOID ||
922 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000923 !this->configToGLFormats(desc->fConfig,
924 // GLES requires sized internal formats
925 kES2_GrGLBinding == this->glBinding(),
926 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000927 goto FAILED;
928 }
929 } else {
930 desc->fRTFBOID = desc->fTexFBOID;
931 }
932
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000933 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000934 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935 if (desc->fRTFBOID != desc->fTexFBOID) {
936 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000937 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000938 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000939 if (!renderbuffer_storage_msaa(fGLContextInfo,
940 desc->fSampleCnt,
941 msColorFormat,
942 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943 goto FAILED;
944 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000945 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
946 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000947 GR_GL_COLOR_ATTACHMENT0,
948 GR_GL_RENDERBUFFER,
949 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000950 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000951 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
952 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
953 goto FAILED;
954 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000955 fGLContextInfo.caps().markConfigAsValidColorAttachment(
956 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000957 }
958 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000959 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000960
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000961 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
962 GR_GL_COLOR_ATTACHMENT0,
963 GR_GL_TEXTURE_2D,
964 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000965 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000966 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
967 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
968 goto FAILED;
969 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000970 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 }
972
973 return true;
974
975FAILED:
976 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000977 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000978 }
979 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000980 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000981 }
982 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000983 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000984 }
985 return false;
986}
987
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000988// good to set a break-point here to know when createTexture fails
989static GrTexture* return_null_texture() {
990// GrAssert(!"null texture");
991 return NULL;
992}
993
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000994#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000995static size_t as_size_t(int x) {
996 return x;
997}
998#endif
999
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001000GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001001 const void* srcData,
1002 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001004 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001005 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001006
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001007 // Attempt to catch un- or wrongly initialized sample counts;
1008 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1009
robertphillips@google.com32716282012-06-04 12:48:45 +00001010 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +00001011 glTexDesc.fWidth = desc.fWidth;
1012 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001013 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +00001014 glTexDesc.fSampleCnt = desc.fSampleCnt;
1015
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001016 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001017
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001018 glRTDesc.fMSColorRenderbufferID = 0;
1019 glRTDesc.fRTFBOID = 0;
1020 glRTDesc.fTexFBOID = 0;
1021 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001022 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001023
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001024 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001025
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001026 const Caps& caps = this->getCaps();
1027
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001028 // We keep GrRenderTargets in GL's normal orientation so that they
1029 // can be drawn to by the outside world without the client having
1030 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001031 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001032 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001033
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001034 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001035 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001036 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001037 //GrPrintf("MSAA RT requested but not supported on this platform.");
1038 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001039 }
1040
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001042 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1043 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001044 return return_null_texture();
1045 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 }
1047
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001049 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001050 // provides a hint about how this texture will be used
1051 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1052 GR_GL_TEXTURE_USAGE,
1053 GR_GL_FRAMEBUFFER_ATTACHMENT));
1054 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001055 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001056 return return_null_texture();
1057 }
1058
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001059 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001060 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001061
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001062 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1063 // drivers have a bug where an FBO won't be complete if it includes a
1064 // texture that is not mipmap complete (considering the filter in use).
1065 GrGLTexture::TexParams initialTexParams;
1066 // we only set a subset here so invalidate first
1067 initialTexParams.invalidate();
1068 initialTexParams.fFilter = GR_GL_NEAREST;
1069 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1070 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001071 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1072 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001073 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001074 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1075 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001076 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001077 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1078 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001079 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001080 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1081 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001082 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001083 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1084 glTexDesc.fWidth, glTexDesc.fHeight,
1085 desc.fConfig, srcData, rowBytes)) {
1086 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1087 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001088 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001089
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001090 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001092 // unbind the texture from the texture unit before binding it to the frame buffer
1093 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1094
bsalomon@google.com99621082011-11-15 16:47:16 +00001095 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1096 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 glTexDesc.fTextureID,
1098 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001099 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 return return_null_texture();
1101 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001102 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001104 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001105 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001106 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001107#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001108 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1109 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001110#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001111 return tex;
1112}
1113
1114namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001115
1116const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1117
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1119 GrGLuint rb,
1120 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 // we shouldn't ever know one size and not the other
1122 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1123 (kUnknownBitCount == format->fTotalBits));
1124 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001125 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001126 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1127 (GrGLint*)&format->fStencilBits);
1128 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001129 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1131 (GrGLint*)&format->fTotalBits);
1132 format->fTotalBits += format->fStencilBits;
1133 } else {
1134 format->fTotalBits = format->fStencilBits;
1135 }
1136 }
1137}
1138}
1139
1140bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1141 int width, int height) {
1142
1143 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001144 // 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 +00001145 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001146 GrAssert(width >= rt->width());
1147 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148
1149 int samples = rt->numSamples();
1150 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001151 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 if (!sbID) {
1153 return false;
1154 }
1155
1156 GrGLStencilBuffer* sb = NULL;
1157
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001158 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 // we start with the last stencil format that succeeded in hopes
1162 // that we won't go through this loop more than once after the
1163 // first (painful) stencil creation.
1164 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001165 const GrGLCaps::StencilFormat& sFmt =
1166 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001167 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001168 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001170 bool created;
1171 if (samples > 0) {
1172 created = renderbuffer_storage_msaa(fGLContextInfo,
1173 samples,
1174 sFmt.fInternalFormat,
1175 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001177 GL_ALLOC_CALL(this->glInterface(),
1178 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001179 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001180 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001181 created =
1182 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001184 if (created) {
1185 // After sized formats we attempt an unsized format and take
1186 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001187 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001188 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001189 sb = SkNEW_ARGS(GrGLStencilBuffer,
1190 (this, sbID, width, height,
1191 samples, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1193 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001194 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 return true;
1197 }
1198 sb->abandon(); // otherwise we lose sbID
1199 sb->unref();
1200 }
1201 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001203 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204}
1205
1206bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1207 GrRenderTarget* rt) {
1208 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1209
1210 GrGLuint fbo = glrt->renderFBOID();
1211
1212 if (NULL == sb) {
1213 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001214 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 GR_GL_STENCIL_ATTACHMENT,
1216 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 GR_GL_DEPTH_ATTACHMENT,
1219 GR_GL_RENDERBUFFER, 0));
1220#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001221 GrGLenum status;
1222 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1224#endif
1225 }
1226 return true;
1227 } else {
1228 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1229 GrGLuint rb = glsb->renderbufferID();
1230
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001231 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1233 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 GR_GL_STENCIL_ATTACHMENT,
1235 GR_GL_RENDERBUFFER, rb));
1236 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 GR_GL_DEPTH_ATTACHMENT,
1239 GR_GL_RENDERBUFFER, rb));
1240 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001241 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242 GR_GL_DEPTH_ATTACHMENT,
1243 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001244 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001246 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001247 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001248 glsb->format())) {
1249 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1250 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001251 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001252 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001253 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001254 if (glsb->format().fPacked) {
1255 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1256 GR_GL_DEPTH_ATTACHMENT,
1257 GR_GL_RENDERBUFFER, 0));
1258 }
1259 return false;
1260 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001261 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001262 rt->config(),
1263 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001264 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001265 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001266 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001268}
1269
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001270////////////////////////////////////////////////////////////////////////////////
1271
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001272GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001273 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001274 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001275 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001276 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001277 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001278 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001280 GL_ALLOC_CALL(this->glInterface(),
1281 BufferData(GR_GL_ARRAY_BUFFER,
1282 size,
1283 NULL, // data ptr
1284 dynamic ? GR_GL_DYNAMIC_DRAW :
1285 GR_GL_STATIC_DRAW));
1286 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001287 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001288 // deleting bound buffer does implicit bind to 0
1289 fHWGeometryState.fVertexBuffer = NULL;
1290 return NULL;
1291 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001292 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer,
1293 (this, id,
1294 size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 fHWGeometryState.fVertexBuffer = vertexBuffer;
1296 return vertexBuffer;
1297 }
1298 return NULL;
1299}
1300
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001301GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001302 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001303 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001305 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001306 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001308 GL_ALLOC_CALL(this->glInterface(),
1309 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1310 size,
1311 NULL, // data ptr
1312 dynamic ? GR_GL_DYNAMIC_DRAW :
1313 GR_GL_STATIC_DRAW));
1314 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001315 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 // deleting bound buffer does implicit bind to 0
1317 fHWGeometryState.fIndexBuffer = NULL;
1318 return NULL;
1319 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001320 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer,
1321 (this, id, size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 fHWGeometryState.fIndexBuffer = indexBuffer;
1323 return indexBuffer;
1324 }
1325 return NULL;
1326}
1327
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001328GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001329 GrAssert(fCaps.fPathStencilingSupport);
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001330 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001331}
1332
bsalomon@google.coma3201942012-06-21 19:58:20 +00001333void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001334 const GrDrawState& drawState = this->getDrawState();
1335 const GrGLRenderTarget* rt =
1336 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1337
1338 GrAssert(NULL != rt);
1339 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001340
bsalomon@google.coma3201942012-06-21 19:58:20 +00001341 if (fScissorState.fEnabled) {
1342 GrGLIRect scissor;
1343 scissor.setRelativeTo(vp,
1344 fScissorState.fRect.fLeft,
1345 fScissorState.fRect.fTop,
1346 fScissorState.fRect.width(),
1347 fScissorState.fRect.height());
1348 // if the scissor fully contains the viewport then we fall through and
1349 // disable the scissor test.
1350 if (!scissor.contains(vp)) {
1351 if (fHWScissorSettings.fRect != scissor) {
1352 scissor.pushToGLScissor(this->glInterface());
1353 fHWScissorSettings.fRect = scissor;
1354 }
1355 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1356 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1357 fHWScissorSettings.fEnabled = kYes_TriState;
1358 }
1359 return;
1360 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001361 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001362 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001363 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001364 fHWScissorSettings.fEnabled = kNo_TriState;
1365 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001366 }
1367}
1368
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001369void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001370 const GrDrawState& drawState = this->getDrawState();
1371 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001372 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001373 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001374
bsalomon@google.com74b98712011-11-11 19:46:16 +00001375 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001376 if (NULL != rect) {
1377 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001378 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001379 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001380 if (clippedRect.intersect(rtRect)) {
1381 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001382 } else {
1383 return;
1384 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001385 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001386 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001387 GrAutoTRestore<ScissorState> asr(&fScissorState);
1388 fScissorState.fEnabled = (NULL != rect);
1389 if (fScissorState.fEnabled) {
1390 fScissorState.fRect = *rect;
1391 }
1392 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001393
1394 GrGLfloat r, g, b, a;
1395 static const GrGLfloat scale255 = 1.f / 255.f;
1396 a = GrColorUnpackA(color) * scale255;
1397 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001398 r = GrColorUnpackR(color) * scaleRGB;
1399 g = GrColorUnpackG(color) * scaleRGB;
1400 b = GrColorUnpackB(color) * scaleRGB;
1401
1402 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001403 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001404 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001405 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001406}
1407
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001408void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001409 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001410 return;
1411 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001412
1413 this->flushRenderTarget(&GrIRect::EmptyIRect());
1414
bsalomon@google.coma3201942012-06-21 19:58:20 +00001415 GrAutoTRestore<ScissorState> asr(&fScissorState);
1416 fScissorState.fEnabled = false;
1417 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001418
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001419 GL_CALL(StencilMask(0xffffffff));
1420 GL_CALL(ClearStencil(0));
1421 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001422 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001423}
1424
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001425void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001426 const GrDrawState& drawState = this->getDrawState();
1427 const GrRenderTarget* rt = drawState.getRenderTarget();
1428 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001429
1430 // this should only be called internally when we know we have a
1431 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001432 GrAssert(NULL != rt->getStencilBuffer());
1433 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001434#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001435 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001436 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001437#else
1438 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001439 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001440 // turned into draws. Our contract on GrDrawTarget says that
1441 // changing the clip between stencil passes may or may not
1442 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001443 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001444#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001445 GrGLint value;
1446 if (insideClip) {
1447 value = (1 << (stencilBitCount - 1));
1448 } else {
1449 value = 0;
1450 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001451 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001452
1453 GrAutoTRestore<ScissorState> asr(&fScissorState);
1454 fScissorState.fEnabled = true;
1455 fScissorState.fRect = rect;
1456 this->flushScissor();
1457
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001458 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001459 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001460 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001461 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001462}
1463
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001464void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001465 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001466}
1467
bsalomon@google.comc4364992011-11-07 15:54:49 +00001468bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1469 int left, int top,
1470 int width, int height,
1471 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001472 size_t rowBytes) const {
1473 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001474 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001475 return false;
1476 }
1477
1478 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001479 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001480 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001481 return true;
1482 }
1483 // If we have to do memcpys to handle rowBytes then y-flip is free
1484 // Note the rowBytes might be tight to the passed in data, but if data
1485 // gets clipped in x to the target the rowBytes will no longer be tight.
1486 if (left >= 0 && (left + width) < renderTarget->width()) {
1487 return 0 == rowBytes ||
1488 GrBytesPerPixel(config) * width == rowBytes;
1489 } else {
1490 return false;
1491 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001492}
1493
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001494bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001495 int left, int top,
1496 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001497 GrPixelConfig config,
1498 void* buffer,
1499 size_t rowBytes,
1500 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001501 GrGLenum format;
1502 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001503 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001504 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001505 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001506 size_t bpp = GrBytesPerPixel(config);
1507 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1508 &left, &top, &width, &height,
1509 const_cast<const void**>(&buffer),
1510 &rowBytes)) {
1511 return false;
1512 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001513
bsalomon@google.comc6980972011-11-02 19:57:21 +00001514 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001515 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001516 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001517 switch (tgt->getResolveType()) {
1518 case GrGLRenderTarget::kCantResolve_ResolveType:
1519 return false;
1520 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001521 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001522 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001523 break;
1524 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001525 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001526 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001527 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1528 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001529 break;
1530 default:
1531 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001532 }
1533
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001534 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001535
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001536 // the read rect is viewport-relative
1537 GrGLIRect readRect;
1538 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001540 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001541 if (0 == rowBytes) {
1542 rowBytes = tightRowBytes;
1543 }
1544 size_t readDstRowBytes = tightRowBytes;
1545 void* readDst = buffer;
1546
1547 // determine if GL can read using the passed rowBytes or if we need
1548 // a scratch buffer.
1549 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1550 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001551 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001552 GrAssert(!(rowBytes % sizeof(GrColor)));
1553 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1554 readDstRowBytes = rowBytes;
1555 } else {
1556 scratch.reset(tightRowBytes * height);
1557 readDst = scratch.get();
1558 }
1559 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001560 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001561 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1562 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001563 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1564 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001565 format, type, readDst));
1566 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001567 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001568 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1569 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001570 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001571 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1572 invertY = true;
1573 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001574
1575 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001576 // API presents top-to-bottom. We must preserve the padding contents. Note
1577 // that the above readPixels did not overwrite the padding.
1578 if (readDst == buffer) {
1579 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001580 if (!invertY) {
1581 scratch.reset(tightRowBytes);
1582 void* tmpRow = scratch.get();
1583 // flip y in-place by rows
1584 const int halfY = height >> 1;
1585 char* top = reinterpret_cast<char*>(buffer);
1586 char* bottom = top + (height - 1) * rowBytes;
1587 for (int y = 0; y < halfY; y++) {
1588 memcpy(tmpRow, top, tightRowBytes);
1589 memcpy(top, bottom, tightRowBytes);
1590 memcpy(bottom, tmpRow, tightRowBytes);
1591 top += rowBytes;
1592 bottom -= rowBytes;
1593 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001594 }
1595 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001596 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001597 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001598 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001599 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001600 char* dst = reinterpret_cast<char*>(buffer);
1601 if (!invertY) {
1602 dst += (height-1) * rowBytes;
1603 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001604 for (int y = 0; y < height; y++) {
1605 memcpy(dst, src, tightRowBytes);
1606 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001607 if (invertY) {
1608 dst += rowBytes;
1609 } else {
1610 dst -= rowBytes;
1611 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 }
1613 }
1614 return true;
1615}
1616
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001617void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001618
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001619 GrGLRenderTarget* rt =
1620 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1621 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001622
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001623 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001624 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001625 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001626 GrGLenum status;
1627 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001628 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001629 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 }
1631 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001632 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001633 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001634 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001635 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001636 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001637 }
1638 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001639 if (NULL == bound || !bound->isEmpty()) {
1640 rt->flagAsNeedingResolve(bound);
1641 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001642}
1643
twiz@google.com0f31ca72011-03-18 17:38:11 +00001644GrGLenum gPrimitiveType2GLMode[] = {
1645 GR_GL_TRIANGLES,
1646 GR_GL_TRIANGLE_STRIP,
1647 GR_GL_TRIANGLE_FAN,
1648 GR_GL_POINTS,
1649 GR_GL_LINES,
1650 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001651};
1652
bsalomon@google.comd302f142011-03-03 13:54:13 +00001653#define SWAP_PER_DRAW 0
1654
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001655#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001656 #if GR_MAC_BUILD
1657 #include <AGL/agl.h>
1658 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001659 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001660 void SwapBuf() {
1661 DWORD procID = GetCurrentProcessId();
1662 HWND hwnd = GetTopWindow(GetDesktopWindow());
1663 while(hwnd) {
1664 DWORD wndProcID = 0;
1665 GetWindowThreadProcessId(hwnd, &wndProcID);
1666 if(wndProcID == procID) {
1667 SwapBuffers(GetDC(hwnd));
1668 }
1669 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1670 }
1671 }
1672 #endif
1673#endif
1674
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001675void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1676 uint32_t startVertex,
1677 uint32_t startIndex,
1678 uint32_t vertexCount,
1679 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001680 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1681
twiz@google.com0f31ca72011-03-18 17:38:11 +00001682 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001683
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001684 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1685 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1686
1687 // our setupGeometry better have adjusted this to zero since
1688 // DrawElements always draws from the begining of the arrays for idx 0.
1689 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001690
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001691 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1692 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001693#if SWAP_PER_DRAW
1694 glFlush();
1695 #if GR_MAC_BUILD
1696 aglSwapBuffers(aglGetCurrentContext());
1697 int set_a_break_pt_here = 9;
1698 aglSwapBuffers(aglGetCurrentContext());
1699 #elif GR_WIN32_BUILD
1700 SwapBuf();
1701 int set_a_break_pt_here = 9;
1702 SwapBuf();
1703 #endif
1704#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001705}
1706
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001707void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1708 uint32_t startVertex,
1709 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001710 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1711
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001712 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1713
1714 // our setupGeometry better have adjusted this to zero.
1715 // DrawElements doesn't take an offset so we always adjus the startVertex.
1716 GrAssert(0 == startVertex);
1717
1718 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1719 // account for startVertex in the DrawElements case. So we always
1720 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001721 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001722#if SWAP_PER_DRAW
1723 glFlush();
1724 #if GR_MAC_BUILD
1725 aglSwapBuffers(aglGetCurrentContext());
1726 int set_a_break_pt_here = 9;
1727 aglSwapBuffers(aglGetCurrentContext());
1728 #elif GR_WIN32_BUILD
1729 SwapBuf();
1730 int set_a_break_pt_here = 9;
1731 SwapBuf();
1732 #endif
1733#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001734}
1735
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001736namespace {
1737const GrStencilSettings& winding_nv_path_stencil_settings() {
1738 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1739 kIncClamp_StencilOp,
1740 kIncClamp_StencilOp,
1741 kAlwaysIfInClip_StencilFunc,
1742 ~0, ~0, ~0);
1743 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1744}
1745const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1746 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1747 kInvert_StencilOp,
1748 kInvert_StencilOp,
1749 kAlwaysIfInClip_StencilFunc,
1750 ~0, ~0, ~0);
1751 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1752}
1753}
1754
1755
1756void GrGpuGL::setStencilPathSettings(const GrPath&,
1757 GrPathFill fill,
1758 GrStencilSettings* settings) {
1759 switch (fill) {
1760 case kEvenOdd_GrPathFill:
1761 *settings = even_odd_nv_path_stencil_settings();
1762 return;
1763 case kWinding_GrPathFill:
1764 *settings = winding_nv_path_stencil_settings();
1765 return;
1766 default:
1767 GrCrash("Unexpected path fill.");
1768 }
1769}
1770
1771void GrGpuGL::onGpuStencilPath(const GrPath* path, GrPathFill fill) {
1772 GrAssert(fCaps.fPathStencilingSupport);
1773
1774 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1775 GrDrawState* drawState = this->drawState();
1776 GrAssert(NULL != drawState->getRenderTarget());
1777 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1778 return;
1779 }
1780
1781 // Decide how to manipulate the stencil buffer based on the fill rule.
1782 // Also, assert that the stencil settings we set in setStencilPathSettings
1783 // are present.
1784 GrAssert(!fStencilSettings.isTwoSided());
1785 GrGLenum fillMode;
1786 switch (fill) {
1787 case kWinding_GrPathFill:
1788 fillMode = GR_GL_COUNT_UP;
1789 GrAssert(kIncClamp_StencilOp ==
1790 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1791 GrAssert(kIncClamp_StencilOp ==
1792 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1793 break;
1794 case kEvenOdd_GrPathFill:
1795 fillMode = GR_GL_INVERT;
1796 GrAssert(kInvert_StencilOp ==
1797 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1798 GrAssert(kInvert_StencilOp ==
1799 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1800 break;
1801 default:
1802 // Only the above two fill rules are allowed.
1803 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001804 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001805 }
1806 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1807 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001808}
1809
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001810void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1811
1812 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001813
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001814 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001815 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001816 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001817 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1818 rt->renderFBOID()));
1819 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1820 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001821 // make sure we go through flushRenderTarget() since we've modified
1822 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001823 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001824 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001825 const GrIRect dirtyRect = rt->getResolveRect();
1826 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001827 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001828 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001829
bsalomon@google.coma3201942012-06-21 19:58:20 +00001830 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001831 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001832 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001833 asr.reset(&fScissorState);
1834 fScissorState.fEnabled = true;
1835 fScissorState.fRect = dirtyRect;
1836 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001837 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001838 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001839 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001840 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001841 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1842 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001843 asr.reset(&fScissorState);
1844 fScissorState.fEnabled = false;
1845 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001846 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001847 int right = r.fLeft + r.fWidth;
1848 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001849 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1850 r.fLeft, r.fBottom, right, top,
1851 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001852 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001853 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001854 }
1855}
1856
bsalomon@google.com411dad02012-06-05 20:24:20 +00001857namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858
bsalomon@google.com411dad02012-06-05 20:24:20 +00001859GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1860 static const GrGLenum gTable[] = {
1861 GR_GL_ALWAYS, // kAlways_StencilFunc
1862 GR_GL_NEVER, // kNever_StencilFunc
1863 GR_GL_GREATER, // kGreater_StencilFunc
1864 GR_GL_GEQUAL, // kGEqual_StencilFunc
1865 GR_GL_LESS, // kLess_StencilFunc
1866 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1867 GR_GL_EQUAL, // kEqual_StencilFunc,
1868 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1869 };
1870 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1871 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1872 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1873 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1874 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1875 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1876 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1877 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1878 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1879 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1880
1881 return gTable[basicFunc];
1882}
1883
1884GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1885 static const GrGLenum gTable[] = {
1886 GR_GL_KEEP, // kKeep_StencilOp
1887 GR_GL_REPLACE, // kReplace_StencilOp
1888 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1889 GR_GL_INCR, // kIncClamp_StencilOp
1890 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1891 GR_GL_DECR, // kDecClamp_StencilOp
1892 GR_GL_ZERO, // kZero_StencilOp
1893 GR_GL_INVERT, // kInvert_StencilOp
1894 };
1895 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1896 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1897 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1898 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1899 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1900 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1901 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1902 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1903 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1904 GrAssert((unsigned) op < kStencilOpCount);
1905 return gTable[op];
1906}
1907
1908void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001909 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001910 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001911 GrStencilSettings::Face grFace) {
1912 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1913 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1914 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1915
1916 GrGLint ref = settings.funcRef(grFace);
1917 GrGLint mask = settings.funcMask(grFace);
1918 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001919
1920 if (GR_GL_FRONT_AND_BACK == glFace) {
1921 // we call the combined func just in case separate stencil is not
1922 // supported.
1923 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1924 GR_GL_CALL(gl, StencilMask(writeMask));
1925 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1926 } else {
1927 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1928 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1929 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1930 }
1931}
1932}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001933
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001934void GrGpuGL::flushStencil(DrawType type) {
1935 if (kStencilPath_DrawType == type) {
1936 GrAssert(!fStencilSettings.isTwoSided());
1937 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1938 // that draws the path to the SB (glStencilFillPath)
1939 GrGLenum func =
1940 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1941 GL_CALL(PathStencilFunc(func,
1942 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1943 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1944 } else if (fHWStencilSettings != fStencilSettings) {
1945 if (fStencilSettings.isDisabled()) {
1946 if (kNo_TriState != fHWStencilTestEnabled) {
1947 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1948 fHWStencilTestEnabled = kNo_TriState;
1949 }
1950 } else {
1951 if (kYes_TriState != fHWStencilTestEnabled) {
1952 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1953 fHWStencilTestEnabled = kYes_TriState;
1954 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001955 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001956 if (!fStencilSettings.isDisabled()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001957 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001958 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001959 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001960 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001961 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001962 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001963 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001964 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001965 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001966 } else {
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_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001970 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001971 }
1972 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001973 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001974 }
1975}
1976
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001977void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001978 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001980 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1981 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001982 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001983 bool smoothLines = false;
1984
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001985 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001986 smoothLines = this->willUseHWAALines();
1987 if (smoothLines) {
1988 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1989 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1990 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1991 // must disable msaa to use line smoothing
1992 if (rt->isMultisampled() &&
1993 kNo_TriState != fHWAAState.fMSAAEnabled) {
1994 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1995 fHWAAState.fMSAAEnabled = kNo_TriState;
1996 }
1997 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001998 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001999 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
2000 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
2001 fHWAAState.fSmoothLineEnabled = kNo_TriState;
2002 }
2003 }
2004 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002005 if (!smoothLines && rt->isMultisampled()) {
2006 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
2007 // convex hulls of each segment appear to get filled.
2008 bool enableMSAA = kStencilPath_DrawType == type ||
2009 this->getDrawState().isHWAntialiasState();
2010 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002011 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2012 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2013 fHWAAState.fMSAAEnabled = kYes_TriState;
2014 }
2015 } else {
2016 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2017 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2018 fHWAAState.fMSAAEnabled = kNo_TriState;
2019 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002020 }
2021 }
2022 }
2023}
2024
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002025void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002026 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002027 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002028 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002029 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002030 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002031 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002032 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002033 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2034 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2035 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2036 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2037 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2038 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002039 }
2040 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002041 // any optimization to disable blending should
2042 // have already been applied and tweaked the coeffs
2043 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002044 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2045 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002046 if (blendOff) {
2047 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002048 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002049 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002050 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002051 } else {
2052 if (kYes_TriState != fHWBlendState.fEnabled) {
2053 GL_CALL(Enable(GR_GL_BLEND));
2054 fHWBlendState.fEnabled = kYes_TriState;
2055 }
2056 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2057 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002058 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2059 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002060 fHWBlendState.fSrcCoeff = srcCoeff;
2061 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002062 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002063 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002064 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2065 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002066 (!fHWBlendState.fConstColorValid ||
2067 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002068
2069 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002070 GrColorUnpackR(blendConst) / 255.f,
2071 GrColorUnpackG(blendConst) / 255.f,
2072 GrColorUnpackB(blendConst) / 255.f,
2073 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002074 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002075 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002076 fHWBlendState.fConstColor = blendConst;
2077 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002078 }
2079 }
2080 }
2081}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002082namespace {
2083
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002084// get_swizzle is only called from this .cpp so it is OK to inline it here
2085inline const GrGLenum* get_swizzle(GrPixelConfig config,
2086 const GrSamplerState& sampler,
2087 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002088 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002089 if (glCaps.textureRedSupport()) {
2090 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2091 GR_GL_RED, GR_GL_RED };
2092 return gRedSmear;
2093 } else {
2094 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2095 GR_GL_ALPHA, GR_GL_ALPHA };
2096 return gAlphaSmear;
2097 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002098 } else if (sampler.swapsRAndB()) {
2099 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2100 GR_GL_RED, GR_GL_ALPHA };
2101 return gRedBlueSwap;
2102 } else {
2103 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2104 GR_GL_BLUE, GR_GL_ALPHA };
2105 return gStraight;
2106 }
2107}
2108
2109void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002110 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2111 GR_GL_TEXTURE_SWIZZLE_RGBA,
2112 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002113}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002114
2115const GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
2116 static const GrGLenum gWrapModes[] = {
2117 GR_GL_CLAMP_TO_EDGE,
2118 GR_GL_REPEAT,
2119 GR_GL_MIRRORED_REPEAT
2120 };
2121 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2122 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2123 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2124 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2125 return gWrapModes[tm];
2126}
2127
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002128}
2129
bsalomon@google.com4c883782012-06-04 19:05:11 +00002130void GrGpuGL::flushBoundTextureAndParams(int stage) {
2131 GrDrawState* drawState = this->drawState();
bsalomon@google.com0982d352012-07-31 15:33:25 +00002132 // FIXME: Assuming at most one texture per custom stage
bsalomon@google.comcddaf342012-07-30 13:09:05 +00002133 const GrCustomStage* customStage = drawState->sampler(stage)->getCustomStage();
2134 GrGLTexture* nextTexture = static_cast<GrGLTexture*>(customStage->texture(0));
bsalomon@google.com0982d352012-07-31 15:33:25 +00002135 if (NULL != nextTexture) {
2136 // Currently we always use the texture params from the GrSamplerState. Soon custom stages
2137 // will provide their own params.
2138 const GrTextureParams& texParams = drawState->getSampler(stage).getTextureParams();
2139 this->flushBoundTextureAndParams(stage, texParams, nextTexture);
2140 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002141}
2142
bsalomon@google.comb8670992012-07-25 21:27:09 +00002143void GrGpuGL::flushBoundTextureAndParams(int stage,
2144 const GrTextureParams& params,
2145 GrGLTexture* nextTexture) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002146 GrDrawState* drawState = this->drawState();
2147
bsalomon@google.com4c883782012-06-04 19:05:11 +00002148 // true for now, but maybe not with GrEffect.
2149 GrAssert(NULL != nextTexture);
bsalomon@google.comb8670992012-07-25 21:27:09 +00002150 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2151 // 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 +00002152 // out of the "last != next" check.
bsalomon@google.comb8670992012-07-25 21:27:09 +00002153 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002154 if (NULL != texRT) {
2155 this->onResolveRenderTarget(texRT);
2156 }
2157
2158 if (fHWBoundTextures[stage] != nextTexture) {
2159 this->setTextureUnit(stage);
2160 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002161 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2162 fHWBoundTextures[stage] = nextTexture;
2163 }
2164
bsalomon@google.com4c883782012-06-04 19:05:11 +00002165 ResetTimestamp timestamp;
2166 const GrGLTexture::TexParams& oldTexParams =
2167 nextTexture->getCachedTexParams(&timestamp);
2168 bool setAll = timestamp < this->getResetTimestamp();
2169 GrGLTexture::TexParams newTexParams;
2170
bsalomon@google.comb8670992012-07-25 21:27:09 +00002171 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002172
bsalomon@google.comb8670992012-07-25 21:27:09 +00002173 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2174 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002175 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.comb8670992012-07-25 21:27:09 +00002176 get_swizzle(nextTexture->config(), drawState->getSampler(stage), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002177 sizeof(newTexParams.fSwizzleRGBA));
2178 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2179 this->setTextureUnit(stage);
2180 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2181 GR_GL_TEXTURE_MAG_FILTER,
2182 newTexParams.fFilter));
2183 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2184 GR_GL_TEXTURE_MIN_FILTER,
2185 newTexParams.fFilter));
2186 }
2187 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2188 this->setTextureUnit(stage);
2189 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2190 GR_GL_TEXTURE_WRAP_S,
2191 newTexParams.fWrapS));
2192 }
2193 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2194 this->setTextureUnit(stage);
2195 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2196 GR_GL_TEXTURE_WRAP_T,
2197 newTexParams.fWrapT));
2198 }
2199 if (this->glCaps().textureSwizzleSupport() &&
2200 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2201 oldTexParams.fSwizzleRGBA,
2202 sizeof(newTexParams.fSwizzleRGBA)))) {
2203 this->setTextureUnit(stage);
2204 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2205 this->glInterface());
2206 }
2207 nextTexture->setCachedTexParams(newTexParams,
2208 this->getResetTimestamp());
2209}
2210
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002211void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002212
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002213 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002214
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002215 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002216 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002217 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002218 fHWDitherEnabled = kYes_TriState;
2219 }
2220 } else {
2221 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002222 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002223 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002224 }
2225 }
2226
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002227 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002228 if (kNo_TriState != fHWWriteToColor) {
2229 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2230 GR_GL_FALSE, GR_GL_FALSE));
2231 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002232 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002233 } else {
2234 if (kYes_TriState != fHWWriteToColor) {
2235 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2236 fHWWriteToColor = kYes_TriState;
2237 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002238 }
2239
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002240 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002241 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002242 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002243 GL_CALL(Enable(GR_GL_CULL_FACE));
2244 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002245 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002246 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002247 GL_CALL(Enable(GR_GL_CULL_FACE));
2248 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002249 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002250 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002251 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002252 break;
2253 default:
2254 GrCrash("Unknown draw face.");
2255 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002256 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002257 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002258}
2259
2260void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002261 if (fHWGeometryState.fVertexBuffer != buffer) {
2262 fHWGeometryState.fArrayPtrsDirty = true;
2263 fHWGeometryState.fVertexBuffer = buffer;
2264 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002265}
2266
2267void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002268 if (fHWGeometryState.fVertexBuffer == buffer) {
2269 // deleting bound buffer does implied bind to 0
2270 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002271 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002272 }
2273}
2274
2275void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002276 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002277}
2278
2279void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 if (fHWGeometryState.fIndexBuffer == buffer) {
2281 // deleting bound buffer does implied bind to 0
2282 fHWGeometryState.fIndexBuffer = NULL;
2283 }
2284}
2285
reed@google.comac10a2d2010-12-22 21:39:39 +00002286void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2287 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002288 if (fHWBoundRenderTarget == renderTarget) {
2289 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002291}
2292
2293void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002294 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002295 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002296 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002297 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002298 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002299 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002300}
2301
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002302bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2303 bool getSizedInternalFormat,
2304 GrGLenum* internalFormat,
2305 GrGLenum* externalFormat,
2306 GrGLenum* externalType) {
2307 GrGLenum dontCare;
2308 if (NULL == internalFormat) {
2309 internalFormat = &dontCare;
2310 }
2311 if (NULL == externalFormat) {
2312 externalFormat = &dontCare;
2313 }
2314 if (NULL == externalType) {
2315 externalType = &dontCare;
2316 }
2317
reed@google.comac10a2d2010-12-22 21:39:39 +00002318 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002319 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002320 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002321 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002322 if (getSizedInternalFormat) {
2323 *internalFormat = GR_GL_RGBA8;
2324 } else {
2325 *internalFormat = GR_GL_RGBA;
2326 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002327 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002328 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002329 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002330 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002331 return false;
2332 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002333 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002334 if (getSizedInternalFormat) {
2335 *internalFormat = GR_GL_BGRA8;
2336 } else {
2337 *internalFormat = GR_GL_BGRA;
2338 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002339 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002340 if (getSizedInternalFormat) {
2341 *internalFormat = GR_GL_RGBA8;
2342 } else {
2343 *internalFormat = GR_GL_RGBA;
2344 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002345 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002346 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002347 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002348 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002349 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002350 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002351 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002352 if (getSizedInternalFormat) {
2353 if (this->glBinding() == kDesktop_GrGLBinding) {
2354 return false;
2355 } else {
2356 *internalFormat = GR_GL_RGB565;
2357 }
2358 } else {
2359 *internalFormat = GR_GL_RGB;
2360 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002361 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002362 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002363 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002364 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002365 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002366 if (getSizedInternalFormat) {
2367 *internalFormat = GR_GL_RGBA4;
2368 } else {
2369 *internalFormat = GR_GL_RGBA;
2370 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002371 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002372 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002373 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002374 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002375 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002376 // glCompressedTexImage doesn't take external params
2377 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002378 // no sized/unsized internal format distinction here
2379 *internalFormat = GR_GL_PALETTE8_RGBA8;
2380 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002381 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002382 } else {
2383 return false;
2384 }
2385 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002386 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002387 if (this->glCaps().textureRedSupport()) {
2388 *internalFormat = GR_GL_RED;
2389 *externalFormat = GR_GL_RED;
2390 if (getSizedInternalFormat) {
2391 *internalFormat = GR_GL_R8;
2392 } else {
2393 *internalFormat = GR_GL_RED;
2394 }
2395 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002396 } else {
2397 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002398 *externalFormat = GR_GL_ALPHA;
2399 if (getSizedInternalFormat) {
2400 *internalFormat = GR_GL_ALPHA8;
2401 } else {
2402 *internalFormat = GR_GL_ALPHA;
2403 }
2404 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002405 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002406 break;
2407 default:
2408 return false;
2409 }
2410 return true;
2411}
2412
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002413void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002414 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002415 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002416 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002417 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002418 }
2419}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002420
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002421void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002422 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002423 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002424 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002425 }
2426}
2427
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002428void GrGpuGL::setBuffers(bool indexed,
2429 int* extraVertexOffset,
2430 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002431
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002432 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002433
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002434 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2435
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002436 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002437 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002438 case kBuffer_GeometrySrcType:
2439 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002440 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002441 break;
2442 case kArray_GeometrySrcType:
2443 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002444 this->finalizeReservedVertices();
2445 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2446 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002447 break;
2448 default:
2449 vbuf = NULL; // suppress warning
2450 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002451 }
2452
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002453 GrAssert(NULL != vbuf);
2454 GrAssert(!vbuf->isLocked());
2455 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002456 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002457 fHWGeometryState.fArrayPtrsDirty = true;
2458 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002459 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460
2461 if (indexed) {
2462 GrAssert(NULL != extraIndexOffset);
2463
2464 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002465 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002466 case kBuffer_GeometrySrcType:
2467 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002468 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002469 break;
2470 case kArray_GeometrySrcType:
2471 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002472 this->finalizeReservedIndices();
2473 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2474 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002475 break;
2476 default:
2477 ibuf = NULL; // suppress warning
2478 GrCrash("Unknown geometry src type!");
2479 }
2480
2481 GrAssert(NULL != ibuf);
2482 GrAssert(!ibuf->isLocked());
2483 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002484 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002485 fHWGeometryState.fIndexBuffer = ibuf;
2486 }
2487 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002488}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002489