blob: 82a65fd451566a179eb886558ac3b2edd514965e [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"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000011#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000012#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
caryclark@google.comcf6285b2012-06-06 12:09:01 +000014static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com0b77d682011-08-19 13:28:54 +000017#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000018#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019
bsalomon@google.com316f99232011-01-13 21:28:12 +000020// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000022static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000026#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
27 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
30#else
31 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
34#endif
35
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000036
37///////////////////////////////////////////////////////////////////////////////
38
twiz@google.com0f31ca72011-03-18 17:38:11 +000039static const GrGLenum gXfermodeCoeff2Blend[] = {
40 GR_GL_ZERO,
41 GR_GL_ONE,
42 GR_GL_SRC_COLOR,
43 GR_GL_ONE_MINUS_SRC_COLOR,
44 GR_GL_DST_COLOR,
45 GR_GL_ONE_MINUS_DST_COLOR,
46 GR_GL_SRC_ALPHA,
47 GR_GL_ONE_MINUS_SRC_ALPHA,
48 GR_GL_DST_ALPHA,
49 GR_GL_ONE_MINUS_DST_ALPHA,
50 GR_GL_CONSTANT_COLOR,
51 GR_GL_ONE_MINUS_CONSTANT_COLOR,
52 GR_GL_CONSTANT_ALPHA,
53 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000054
55 // extended blend coeffs
56 GR_GL_SRC1_COLOR,
57 GR_GL_ONE_MINUS_SRC1_COLOR,
58 GR_GL_SRC1_ALPHA,
59 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000060};
61
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000063 static const bool gCoeffReferencesBlendConst[] = {
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 true,
75 true,
76 true,
77 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000078
79 // extended blend coeffs
80 false,
81 false,
82 false,
83 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000084 };
85 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000086 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
87
88 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
89 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
90 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
91 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
92 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
93 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
94 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
95 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
96 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
97 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
98 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
99 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
100 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
101 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
102
103 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
104 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
105 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
106 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
107
108 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
109 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000110}
111
reed@google.comac10a2d2010-12-22 21:39:39 +0000112///////////////////////////////////////////////////////////////////////////////
113
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000114static bool gPrintStartupSpew;
115
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000116static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000117
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000118 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000119
twiz@google.com0f31ca72011-03-18 17:38:11 +0000120 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000121 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
122 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000123 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000124 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
125 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000126 // some implementations require texture to be mip-map complete before
127 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000128 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000129 GR_GL_TEXTURE_MIN_FILTER,
130 GR_GL_NEAREST));
131 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
132 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
133 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
134 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
135 GR_GL_COLOR_ATTACHMENT0,
136 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000137 GrGLenum status;
138 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000139 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
140 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000141
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000142 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000143}
144
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000145GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
146
147 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000148
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000149 fillInConfigRenderableTable();
150
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000151 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000152
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000153 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000154
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000155 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000156 const GrGLubyte* ext;
157 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000158 const GrGLubyte* vendor;
159 const GrGLubyte* renderer;
160 const GrGLubyte* version;
161 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
162 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
163 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000164 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
165 this);
166 GrPrintf("------ VENDOR %s\n", vendor);
167 GrPrintf("------ RENDERER %s\n", renderer);
168 GrPrintf("------ VERSION %s\n", version);
169 GrPrintf("------ EXTENSIONS\n %s \n", ext);
170 }
171
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000172 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000173
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000174 fProgramData = NULL;
175 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000176
bsalomon@google.comfe676522011-06-17 18:12:21 +0000177 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000178 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000179 if (false) { // avoid bit rot, suppress warning
180 fbo_test(this->glInterface(), 0, 0);
181 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000182}
183
184GrGpuGL::~GrGpuGL() {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000185 if (fProgramData && 0 != fHWProgramID) {
186 // detach the current program so there is no confusion on OpenGL's part
187 // that we want it to be deleted
188 GrAssert(fHWProgramID == fProgramData->fProgramID);
189 GL_CALL(UseProgram(0));
190 }
191
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000192 delete fProgramCache;
193 fProgramCache = NULL;
194 fProgramData = NULL;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000195
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000196 // This must be called by before the GrDrawTarget destructor
197 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000198 // This subclass must do this before the base class destructor runs
199 // since we will unref the GrGLInterface.
200 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000201}
202
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000203///////////////////////////////////////////////////////////////////////////////
204
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000205void GrGpuGL::initCaps() {
206 GrGLint maxTextureUnits;
207 // check FS and fixed-function texture unit limits
208 // we only use textures in the fragment stage currently.
209 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000210 const GrGLInterface* gl = this->glInterface();
211 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000212 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000213
214 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000215 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000216 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000217 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000218 for (int i = 0; i < numFormats; ++i) {
219 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
220 fCaps.f8BitPaletteSupport = true;
221 break;
222 }
223 }
224
225 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000226 // we could also look for GL_ATI_separate_stencil extension or
227 // GL_EXT_stencil_two_side but they use different function signatures
228 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000229 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000230 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000231 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000232 this->hasExtension("GL_EXT_stencil_wrap");
233 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000234 // ES 2 has two sided stencil and stencil wrap
235 fCaps.fTwoSidedStencilSupport = true;
236 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 }
238
239 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000240 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
241 // extension includes glMapBuffer.
242 } else {
243 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
244 }
245
246 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000247 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000248 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
249 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 } else {
251 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000252 }
253 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000254 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000255 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000256 }
257
258 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
259
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000260 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
261 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 // Our render targets are always created with textures as the color
263 // attachment, hence this min:
264 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
265
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000266 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000267
268 // Enable supported shader-related caps
269 if (kDesktop_GrGLBinding == this->glBinding()) {
270 fCaps.fDualSourceBlendingSupport =
271 this->glVersion() >= GR_GL_VER(3,3) ||
272 this->hasExtension("GL_ARB_blend_func_extended");
273 fCaps.fShaderDerivativeSupport = true;
274 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
275 fCaps.fGeometryShaderSupport =
276 this->glVersion() >= GR_GL_VER(3,2) &&
277 this->glslGeneration() >= k150_GrGLSLGeneration;
278 } else {
279 fCaps.fShaderDerivativeSupport =
280 this->hasExtension("GL_OES_standard_derivatives");
281 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000282}
283
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000284void GrGpuGL::fillInConfigRenderableTable() {
285
286 // OpenGL < 3.0
287 // no support for render targets unless the GL_ARB_framebuffer_object
288 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
289 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
290 // probably don't get R8 in this case.
291
292 // OpenGL 3.0
293 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
294 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
295
296 // >= OpenGL 3.1
297 // base color renderable: RED, RG, RGB, and RGBA
298 // sized derivatives: R8, RGBA4, RGBA8
299 // if the GL_ARB_compatibility extension is supported then we get back
300 // support for GL_ALPHA and ALPHA8
301
302 // GL_EXT_bgra adds BGRA render targets to any version
303
304 // ES 2.0
305 // color renderable: RGBA4, RGB5_A1, RGB565
306 // GL_EXT_texture_rg adds support for R8 as a color render target
307 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
308 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
309 // added BGRA support
310
311 if (kDesktop_GrGLBinding == this->glBinding()) {
312 // Post 3.0 we will get R8
313 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
314 if (this->glVersion() >= GR_GL_VER(3,0) ||
315 this->hasExtension("GL_ARB_framebuffer_object")) {
316 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
317 }
318 } else {
319 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000320 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
321 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000322 }
323
324 if (kDesktop_GrGLBinding != this->glBinding()) {
325 // only available in ES
326 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
327 }
328
329 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
330 // GL_EXT_framebuffer_object for FBO support. Both of these
331 // allow RGBA4 render targets so this is always supported.
332 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
333
334 if (this->glCaps().rgba8RenderbufferSupport()) {
335 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
336 }
337
338 if (this->glCaps().bgraFormatSupport()) {
339 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
340 }
341
342 // the un-premultiplied formats just inherit the premultiplied setting
343 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
344 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
345 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
346 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
347}
348
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000349bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
350 if (kUnknown_CanPreserveUnpremulRoundtrip ==
351 fCanPreserveUnpremulRoundtrip) {
352
353 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
354 uint32_t* srcData = data.get();
355 uint32_t* firstRead = data.get() + 256 * 256;
356 uint32_t* secondRead = data.get() + 2 * 256 * 256;
357
358 for (int y = 0; y < 256; ++y) {
359 for (int x = 0; x < 256; ++x) {
360 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
361 color[3] = y;
362 color[2] = x;
363 color[1] = x;
364 color[0] = x;
365 }
366 }
367
368 // We have broader support for read/write pixels on render targets
369 // than on textures.
370 GrTextureDesc dstDesc;
371 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
372 kNoStencil_GrTextureFlagBit;
373 dstDesc.fWidth = 256;
374 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000375 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000376 dstDesc.fSampleCnt = 0;
377
378 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
379 if (!dstTex.get()) {
380 return false;
381 }
382 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
383 GrAssert(NULL != rt);
384
385 bool failed = true;
386 static const UnpremulConversion gMethods[] = {
387 kUpOnWrite_DownOnRead_UnpremulConversion,
388 kDownOnWrite_UpOnRead_UnpremulConversion,
389 };
390
391 // pretend that we can do the roundtrip to avoid recursive calls to
392 // this function
393 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
394 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
395 fUnpremulConversion = gMethods[i];
396 rt->writePixels(0, 0,
397 256, 256,
398 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
399 rt->readPixels(0, 0,
400 256, 256,
401 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
402 rt->writePixels(0, 0,
403 256, 256,
404 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
405 rt->readPixels(0, 0,
406 256, 256,
407 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
408 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));
469 GL_CALL(Disable(GR_GL_COLOR_TABLE));
470 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
471 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
472 // Since ES doesn't support glPointSize at all we always use the VS to
473 // set the point size
474 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
475
476 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
477 // currently part of our gl interface. There are probably others as
478 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000479 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000480 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000481 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000482
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000484 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000486 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000487 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000488
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000489 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000490
tomhudson@google.com93813632011-10-27 20:21:16 +0000491 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000492 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000493 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000494
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000495 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000496 // set to true to force disableScissor to make a GL call.
497 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000498 this->disableScissor();
499
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000500 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000501
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000502 fHWStencilSettings.invalidate();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000503 // This is arbitrary. The above invalidate ensures a full setup of the
504 // stencil on the next draw.
505 fHWStencilClipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000506
507 // TODO: I believe this should actually go in GrGpu::onResetContext
508 // rather than here
509 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000510
511 fHWGeometryState.fIndexBuffer = NULL;
512 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000513
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000514 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000515
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000516 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000517
518 // 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 }
590 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
591 } else {
592 texture = new GrGLTexture(this, glTexDesc);
593 }
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
616 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
617 if (desc.fStencilBits) {
618 GrGLStencilBuffer::Format format;
619 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
620 format.fPacked = false;
621 format.fStencilBits = desc.fStencilBits;
622 format.fTotalBits = desc.fStencilBits;
623 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
624 0,
625 desc.fWidth,
626 desc.fHeight,
627 desc.fSampleCnt,
628 format);
629 tgt->setStencilBuffer(sb);
630 sb->unref();
631 }
632 return tgt;
633}
634
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000635////////////////////////////////////////////////////////////////////////////////
636
bsalomon@google.com6f379512011-11-16 20:36:03 +0000637void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
638 int left, int top, int width, int height,
639 GrPixelConfig config, const void* buffer,
640 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000641 if (NULL == buffer) {
642 return;
643 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000644 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
645
bsalomon@google.com6f379512011-11-16 20:36:03 +0000646 this->setSpareTextureUnit();
647 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
648 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000649 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000650 desc.fWidth = glTex->width();
651 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000652 desc.fConfig = glTex->config();
653 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000654 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000655 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000656
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000657 this->uploadTexData(desc, false,
658 left, top, width, height,
659 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000660}
661
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000662namespace {
663bool adjust_pixel_ops_params(int surfaceWidth,
664 int surfaceHeight,
665 size_t bpp,
666 int* left, int* top, int* width, int* height,
667 const void** data,
668 size_t* rowBytes) {
669 if (!*rowBytes) {
670 *rowBytes = *width * bpp;
671 }
672
673 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
674 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
675
676 if (!subRect.intersect(bounds)) {
677 return false;
678 }
679 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
680 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
681
682 *left = subRect.fLeft;
683 *top = subRect.fTop;
684 *width = subRect.width();
685 *height = subRect.height();
686 return true;
687}
688}
689
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000690bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
691 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000692 int left, int top, int width, int height,
693 GrPixelConfig dataConfig,
694 const void* data,
695 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000696 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000697
698 size_t bpp = GrBytesPerPixel(dataConfig);
699 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
700 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000701 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000702 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000703 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000704
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000705 // in case we need a temporary, trimmed copy of the src pixels
706 SkAutoSMalloc<128 * 128> tempStorage;
707
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000708 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000709 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000710 if (useTexStorage) {
711 if (kDesktop_GrGLBinding == this->glBinding()) {
712 // 565 is not a sized internal format on desktop GL. So on desktop
713 // with 565 we always use an unsized internal format to let the
714 // system pick the best sized format to convert the 565 data to.
715 // Since glTexStorage only allows sized internal formats we will
716 // instead fallback to glTexImage2D.
717 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
718 } else {
719 // ES doesn't allow paletted textures to be used with tex storage
720 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
721 }
722 }
723
724 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000725 GrGLenum externalFormat;
726 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000727 // glTexStorage requires sized internal formats on both desktop and ES. ES
728 // glTexImage requires an unsized format.
729 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
730 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000731 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000732 }
733
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000734 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000735 // paletted textures cannot be updated
736 return false;
737 }
738
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000739 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000740 * check whether to allocate a temporary buffer for flipping y or
741 * because our srcData has extra bytes past each row. If so, we need
742 * to trim those off here, since GL ES may not let us specify
743 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000744 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000745 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000746 bool swFlipY = false;
747 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000748 if (NULL != data) {
749 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000750 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000751 glFlipY = true;
752 } else {
753 swFlipY = true;
754 }
755 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000756 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000757 // can't use this for flipping, only non-neg values allowed. :(
758 if (rowBytes != trimRowBytes) {
759 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
760 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
761 restoreGLRowLength = true;
762 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000763 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000764 if (trimRowBytes != rowBytes || swFlipY) {
765 // copy data into our new storage, skipping the trailing bytes
766 size_t trimSize = height * trimRowBytes;
767 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000768 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000769 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000771 char* dst = (char*)tempStorage.reset(trimSize);
772 for (int y = 0; y < height; y++) {
773 memcpy(dst, src, trimRowBytes);
774 if (swFlipY) {
775 src -= rowBytes;
776 } else {
777 src += rowBytes;
778 }
779 dst += trimRowBytes;
780 }
781 // now point data to our copied version
782 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000783 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000784 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000785 if (glFlipY) {
786 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
787 }
788 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000789 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000790 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000791 if (isNewTexture &&
792 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000793 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000794 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000795 if (useTexStorage) {
796 // We never resize or change formats of textures. We don't use
797 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000798 GL_ALLOC_CALL(this->glInterface(),
799 TexStorage2D(GR_GL_TEXTURE_2D,
800 1, // levels
801 internalFormat,
802 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000803 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000804 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
805 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
806 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000807 GL_ALLOC_CALL(this->glInterface(),
808 CompressedTexImage2D(GR_GL_TEXTURE_2D,
809 0, // level
810 internalFormat,
811 desc.fWidth, desc.fHeight,
812 0, // border
813 imageSize,
814 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000815 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000816 GL_ALLOC_CALL(this->glInterface(),
817 TexImage2D(GR_GL_TEXTURE_2D,
818 0, // level
819 internalFormat,
820 desc.fWidth, desc.fHeight,
821 0, // border
822 externalFormat, externalType,
823 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000824 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000825 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000826 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000827 if (error != GR_GL_NO_ERROR) {
828 succeeded = false;
829 } else {
830 // if we have data and we used TexStorage to create the texture, we
831 // now upload with TexSubImage.
832 if (NULL != data && useTexStorage) {
833 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
834 0, // level
835 left, top,
836 width, height,
837 externalFormat, externalType,
838 data));
839 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000840 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000841 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000842 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000843 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000844 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000845 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
846 0, // level
847 left, top,
848 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000849 externalFormat, externalType, data));
850 }
851
852 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000853 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000854 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000855 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000856 if (glFlipY) {
857 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
858 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000859 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000860}
861
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000862namespace {
863bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
864 int sampleCount,
865 GrGLenum format,
866 int width, int height) {
867 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
868 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
869 bool created = false;
870 if (GrGLCaps::kNVDesktop_CoverageAAType ==
871 ctxInfo.caps().coverageAAType()) {
872 const GrGLCaps::MSAACoverageMode& mode =
873 ctxInfo.caps().getMSAACoverageMode(sampleCount);
874 GL_ALLOC_CALL(ctxInfo.interface(),
875 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
876 mode.fCoverageSampleCnt,
877 mode.fColorSampleCnt,
878 format,
879 width, height));
880 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
881 }
882 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000883 // glRBMS will fail if requested samples is > max samples.
884 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000885 GL_ALLOC_CALL(ctxInfo.interface(),
886 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
887 sampleCount,
888 format,
889 width, height));
890 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
891 }
892 return created;
893}
894}
895
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000896bool GrGpuGL::createRenderTargetObjects(int width, int height,
897 GrGLuint texID,
898 GrGLRenderTarget::Desc* desc) {
899 desc->fMSColorRenderbufferID = 0;
900 desc->fRTFBOID = 0;
901 desc->fTexFBOID = 0;
902 desc->fOwnIDs = true;
903
904 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905
bsalomon@google.comab15d612011-08-09 12:57:56 +0000906 GrGLenum msColorFormat = 0; // suppress warning
907
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000908 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 if (!desc->fTexFBOID) {
910 goto FAILED;
911 }
912
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913
914 // If we are using multisampling we will create two FBOS. We render
915 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000916 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000917 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000918 goto FAILED;
919 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000920 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
921 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000922 if (!desc->fRTFBOID ||
923 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000924 !this->configToGLFormats(desc->fConfig,
925 // GLES requires sized internal formats
926 kES2_GrGLBinding == this->glBinding(),
927 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 goto FAILED;
929 }
930 } else {
931 desc->fRTFBOID = desc->fTexFBOID;
932 }
933
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000934 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000935 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000936 if (desc->fRTFBOID != desc->fTexFBOID) {
937 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000938 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000940 if (!renderbuffer_storage_msaa(fGLContextInfo,
941 desc->fSampleCnt,
942 msColorFormat,
943 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000944 goto FAILED;
945 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000946 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
947 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948 GR_GL_COLOR_ATTACHMENT0,
949 GR_GL_RENDERBUFFER,
950 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000951 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000952 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
953 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
954 goto FAILED;
955 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000956 fGLContextInfo.caps().markConfigAsValidColorAttachment(
957 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000958 }
959 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000960 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000961
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000962 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
963 GR_GL_COLOR_ATTACHMENT0,
964 GR_GL_TEXTURE_2D,
965 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000966 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000967 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
968 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
969 goto FAILED;
970 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000971 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000972 }
973
974 return true;
975
976FAILED:
977 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000978 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000979 }
980 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000981 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000982 }
983 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000984 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000985 }
986 return false;
987}
988
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000989// good to set a break-point here to know when createTexture fails
990static GrTexture* return_null_texture() {
991// GrAssert(!"null texture");
992 return NULL;
993}
994
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000995#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000996static size_t as_size_t(int x) {
997 return x;
998}
999#endif
1000
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001001GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001002 const void* srcData,
1003 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001004
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001005 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001006 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001007
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001008 // Attempt to catch un- or wrongly initialized sample counts;
1009 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1010
robertphillips@google.com32716282012-06-04 12:48:45 +00001011 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +00001012 glTexDesc.fWidth = desc.fWidth;
1013 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001014 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +00001015 glTexDesc.fSampleCnt = desc.fSampleCnt;
1016
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001017 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001018
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001019 glRTDesc.fMSColorRenderbufferID = 0;
1020 glRTDesc.fRTFBOID = 0;
1021 glRTDesc.fTexFBOID = 0;
1022 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001023 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001024
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001025 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001026
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001027 const Caps& caps = this->getCaps();
1028
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001029 // We keep GrRenderTargets in GL's normal orientation so that they
1030 // can be drawn to by the outside world without the client having
1031 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001032 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001033 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001034
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001035 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001036 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001037 desc.fSampleCnt) {
1038 GrPrintf("MSAA RT requested but not supported on this platform.");
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 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001102 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001104 tex = new 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
1144 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1145 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);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001189 sb = new GrGLStencilBuffer(this, sbID, width, height,
1190 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1192 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001193 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 return true;
1196 }
1197 sb->abandon(); // otherwise we lose sbID
1198 sb->unref();
1199 }
1200 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001202 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203}
1204
1205bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1206 GrRenderTarget* rt) {
1207 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1208
1209 GrGLuint fbo = glrt->renderFBOID();
1210
1211 if (NULL == sb) {
1212 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001214 GR_GL_STENCIL_ATTACHMENT,
1215 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 GR_GL_DEPTH_ATTACHMENT,
1218 GR_GL_RENDERBUFFER, 0));
1219#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001220 GrGLenum status;
1221 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1223#endif
1224 }
1225 return true;
1226 } else {
1227 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1228 GrGLuint rb = glsb->renderbufferID();
1229
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001230 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1232 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 GR_GL_STENCIL_ATTACHMENT,
1234 GR_GL_RENDERBUFFER, rb));
1235 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001236 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237 GR_GL_DEPTH_ATTACHMENT,
1238 GR_GL_RENDERBUFFER, rb));
1239 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 GR_GL_DEPTH_ATTACHMENT,
1242 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001243 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001245 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001246 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001247 glsb->format())) {
1248 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1249 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001251 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001253 if (glsb->format().fPacked) {
1254 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1255 GR_GL_DEPTH_ATTACHMENT,
1256 GR_GL_RENDERBUFFER, 0));
1257 }
1258 return false;
1259 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001260 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001261 rt->config(),
1262 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001263 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001264 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001265 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001267}
1268
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001269////////////////////////////////////////////////////////////////////////////////
1270
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001271GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001272 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001273 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001275 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001276 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001277 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001279 GL_ALLOC_CALL(this->glInterface(),
1280 BufferData(GR_GL_ARRAY_BUFFER,
1281 size,
1282 NULL, // data ptr
1283 dynamic ? GR_GL_DYNAMIC_DRAW :
1284 GR_GL_STATIC_DRAW));
1285 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001286 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 // deleting bound buffer does implicit bind to 0
1288 fHWGeometryState.fVertexBuffer = NULL;
1289 return NULL;
1290 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001291 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001292 size, dynamic);
1293 fHWGeometryState.fVertexBuffer = vertexBuffer;
1294 return vertexBuffer;
1295 }
1296 return NULL;
1297}
1298
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001299GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001300 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001301 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001303 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001304 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001305 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001306 GL_ALLOC_CALL(this->glInterface(),
1307 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1308 size,
1309 NULL, // data ptr
1310 dynamic ? GR_GL_DYNAMIC_DRAW :
1311 GR_GL_STATIC_DRAW));
1312 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001313 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 // deleting bound buffer does implicit bind to 0
1315 fHWGeometryState.fIndexBuffer = NULL;
1316 return NULL;
1317 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001318 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001319 size, dynamic);
1320 fHWGeometryState.fIndexBuffer = indexBuffer;
1321 return indexBuffer;
1322 }
1323 return NULL;
1324}
1325
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001326void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001327 const GrDrawState& drawState = this->getDrawState();
1328 const GrGLRenderTarget* rt =
1329 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1330
1331 GrAssert(NULL != rt);
1332 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001333
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001334 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001335 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1336 rect.width(), rect.height());
1337 if (scissor.contains(vp)) {
1338 disableScissor();
1339 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 }
1341
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001342 if (fHWBounds.fScissorRect != scissor) {
1343 scissor.pushToGLScissor(this->glInterface());
1344 fHWBounds.fScissorRect = scissor;
1345 }
1346 if (!fHWBounds.fScissorEnabled) {
1347 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1348 fHWBounds.fScissorEnabled = true;
1349 }
1350}
1351
1352void GrGpuGL::disableScissor() {
1353 if (fHWBounds.fScissorEnabled) {
1354 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1355 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 }
1357}
1358
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001359void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001360 const GrDrawState& drawState = this->getDrawState();
1361 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001362 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001363 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001364
bsalomon@google.com74b98712011-11-11 19:46:16 +00001365 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001366 if (NULL != rect) {
1367 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001368 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001369 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001370 if (clippedRect.intersect(rtRect)) {
1371 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001372 } else {
1373 return;
1374 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001375 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001376 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001377 if (NULL != rect)
1378 this->enableScissoring(*rect);
1379 else
1380 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001381
1382 GrGLfloat r, g, b, a;
1383 static const GrGLfloat scale255 = 1.f / 255.f;
1384 a = GrColorUnpackA(color) * scale255;
1385 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001386 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001387 scaleRGB *= a;
1388 }
1389 r = GrColorUnpackR(color) * scaleRGB;
1390 g = GrColorUnpackG(color) * scaleRGB;
1391 b = GrColorUnpackB(color) * scaleRGB;
1392
1393 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001394 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001395 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001396 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001397}
1398
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001399void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001400 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001401 return;
1402 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001403
1404 this->flushRenderTarget(&GrIRect::EmptyIRect());
1405
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001406 this->disableScissor();
1407
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001408 GL_CALL(StencilMask(0xffffffff));
1409 GL_CALL(ClearStencil(0));
1410 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001411 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001412}
1413
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001414void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001415 const GrDrawState& drawState = this->getDrawState();
1416 const GrRenderTarget* rt = drawState.getRenderTarget();
1417 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001418
1419 // this should only be called internally when we know we have a
1420 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001421 GrAssert(NULL != rt->getStencilBuffer());
1422 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001423#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001424 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001425 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001426#else
1427 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001428 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001429 // turned into draws. Our contract on GrDrawTarget says that
1430 // changing the clip between stencil passes may or may not
1431 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001432 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001433#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001434 GrGLint value;
1435 if (insideClip) {
1436 value = (1 << (stencilBitCount - 1));
1437 } else {
1438 value = 0;
1439 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001440 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001441 this->enableScissoring(rect);
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001442 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001443 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001444 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001445 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001446}
1447
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001448void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001449 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001450}
1451
bsalomon@google.comc4364992011-11-07 15:54:49 +00001452bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1453 int left, int top,
1454 int width, int height,
1455 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001456 size_t rowBytes) const {
1457 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001458 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001459 return false;
1460 }
1461
1462 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001463 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001464 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001465 return true;
1466 }
1467 // If we have to do memcpys to handle rowBytes then y-flip is free
1468 // Note the rowBytes might be tight to the passed in data, but if data
1469 // gets clipped in x to the target the rowBytes will no longer be tight.
1470 if (left >= 0 && (left + width) < renderTarget->width()) {
1471 return 0 == rowBytes ||
1472 GrBytesPerPixel(config) * width == rowBytes;
1473 } else {
1474 return false;
1475 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001476}
1477
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001478bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001479 int left, int top,
1480 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001481 GrPixelConfig config,
1482 void* buffer,
1483 size_t rowBytes,
1484 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001485 GrGLenum format;
1486 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001487 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001488 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001489 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001490 size_t bpp = GrBytesPerPixel(config);
1491 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1492 &left, &top, &width, &height,
1493 const_cast<const void**>(&buffer),
1494 &rowBytes)) {
1495 return false;
1496 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001497
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001499 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001500 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001501 switch (tgt->getResolveType()) {
1502 case GrGLRenderTarget::kCantResolve_ResolveType:
1503 return false;
1504 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001505 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001506 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001507 break;
1508 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001509 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001510 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001511 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1512 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001513 break;
1514 default:
1515 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001516 }
1517
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001518 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001519
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001520 // the read rect is viewport-relative
1521 GrGLIRect readRect;
1522 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001523
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001524 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001525 if (0 == rowBytes) {
1526 rowBytes = tightRowBytes;
1527 }
1528 size_t readDstRowBytes = tightRowBytes;
1529 void* readDst = buffer;
1530
1531 // determine if GL can read using the passed rowBytes or if we need
1532 // a scratch buffer.
1533 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1534 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001535 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001536 GrAssert(!(rowBytes % sizeof(GrColor)));
1537 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1538 readDstRowBytes = rowBytes;
1539 } else {
1540 scratch.reset(tightRowBytes * height);
1541 readDst = scratch.get();
1542 }
1543 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001544 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001545 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1546 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001547 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1548 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001549 format, type, readDst));
1550 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001551 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001552 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1553 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001554 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001555 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1556 invertY = true;
1557 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001558
1559 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001560 // API presents top-to-bottom. We must preserve the padding contents. Note
1561 // that the above readPixels did not overwrite the padding.
1562 if (readDst == buffer) {
1563 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001564 if (!invertY) {
1565 scratch.reset(tightRowBytes);
1566 void* tmpRow = scratch.get();
1567 // flip y in-place by rows
1568 const int halfY = height >> 1;
1569 char* top = reinterpret_cast<char*>(buffer);
1570 char* bottom = top + (height - 1) * rowBytes;
1571 for (int y = 0; y < halfY; y++) {
1572 memcpy(tmpRow, top, tightRowBytes);
1573 memcpy(top, bottom, tightRowBytes);
1574 memcpy(bottom, tmpRow, tightRowBytes);
1575 top += rowBytes;
1576 bottom -= rowBytes;
1577 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001578 }
1579 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001580 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001581 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001582 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001583 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001584 char* dst = reinterpret_cast<char*>(buffer);
1585 if (!invertY) {
1586 dst += (height-1) * rowBytes;
1587 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001588 for (int y = 0; y < height; y++) {
1589 memcpy(dst, src, tightRowBytes);
1590 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001591 if (invertY) {
1592 dst += rowBytes;
1593 } else {
1594 dst -= rowBytes;
1595 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001596 }
1597 }
1598 return true;
1599}
1600
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001601void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001602
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001603 GrGLRenderTarget* rt =
1604 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1605 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001606
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001607 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001608 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001610 GrGLenum status;
1611 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001612 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001613 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 }
1615 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001616 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001617 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001618 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001619 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 fHWBounds.fViewportRect = vp;
1621 }
1622 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001623 if (NULL == bound || !bound->isEmpty()) {
1624 rt->flagAsNeedingResolve(bound);
1625 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001626}
1627
twiz@google.com0f31ca72011-03-18 17:38:11 +00001628GrGLenum gPrimitiveType2GLMode[] = {
1629 GR_GL_TRIANGLES,
1630 GR_GL_TRIANGLE_STRIP,
1631 GR_GL_TRIANGLE_FAN,
1632 GR_GL_POINTS,
1633 GR_GL_LINES,
1634 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001635};
1636
bsalomon@google.comd302f142011-03-03 13:54:13 +00001637#define SWAP_PER_DRAW 0
1638
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001639#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001640 #if GR_MAC_BUILD
1641 #include <AGL/agl.h>
1642 #elif GR_WIN32_BUILD
1643 void SwapBuf() {
1644 DWORD procID = GetCurrentProcessId();
1645 HWND hwnd = GetTopWindow(GetDesktopWindow());
1646 while(hwnd) {
1647 DWORD wndProcID = 0;
1648 GetWindowThreadProcessId(hwnd, &wndProcID);
1649 if(wndProcID == procID) {
1650 SwapBuffers(GetDC(hwnd));
1651 }
1652 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1653 }
1654 }
1655 #endif
1656#endif
1657
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001658void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1659 uint32_t startVertex,
1660 uint32_t startIndex,
1661 uint32_t vertexCount,
1662 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001663 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1664
twiz@google.com0f31ca72011-03-18 17:38:11 +00001665 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001666
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001667 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1668 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1669
1670 // our setupGeometry better have adjusted this to zero since
1671 // DrawElements always draws from the begining of the arrays for idx 0.
1672 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001673
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001674 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1675 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001676#if SWAP_PER_DRAW
1677 glFlush();
1678 #if GR_MAC_BUILD
1679 aglSwapBuffers(aglGetCurrentContext());
1680 int set_a_break_pt_here = 9;
1681 aglSwapBuffers(aglGetCurrentContext());
1682 #elif GR_WIN32_BUILD
1683 SwapBuf();
1684 int set_a_break_pt_here = 9;
1685 SwapBuf();
1686 #endif
1687#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001688}
1689
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001690void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1691 uint32_t startVertex,
1692 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001693 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1694
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001695 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1696
1697 // our setupGeometry better have adjusted this to zero.
1698 // DrawElements doesn't take an offset so we always adjus the startVertex.
1699 GrAssert(0 == startVertex);
1700
1701 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1702 // account for startVertex in the DrawElements case. So we always
1703 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001704 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001705#if SWAP_PER_DRAW
1706 glFlush();
1707 #if GR_MAC_BUILD
1708 aglSwapBuffers(aglGetCurrentContext());
1709 int set_a_break_pt_here = 9;
1710 aglSwapBuffers(aglGetCurrentContext());
1711 #elif GR_WIN32_BUILD
1712 SwapBuf();
1713 int set_a_break_pt_here = 9;
1714 SwapBuf();
1715 #endif
1716#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001717}
1718
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001719void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1720
1721 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001722
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001723 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001724 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001725 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001726 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1727 rt->renderFBOID()));
1728 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1729 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001730 // make sure we go through flushRenderTarget() since we've modified
1731 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001732 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001733 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001734 const GrIRect dirtyRect = rt->getResolveRect();
1735 GrGLIRect r;
1736 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1737 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001738
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001739 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001740 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001741#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001742 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1743 GL_CALL(Scissor(r.fLeft, r.fBottom,
1744 r.fWidth, r.fHeight));
1745 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001746 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001748#else
1749 this->enableScissoring(dirtyRect);
1750 GL_CALL(ResolveMultisampleFramebuffer());
1751#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001753 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001754 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001755 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1756 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001757 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001758 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001759 int right = r.fLeft + r.fWidth;
1760 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001761 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1762 r.fLeft, r.fBottom, right, top,
1763 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001765 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 }
1767}
1768
bsalomon@google.com411dad02012-06-05 20:24:20 +00001769namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001770
bsalomon@google.com411dad02012-06-05 20:24:20 +00001771GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1772 static const GrGLenum gTable[] = {
1773 GR_GL_ALWAYS, // kAlways_StencilFunc
1774 GR_GL_NEVER, // kNever_StencilFunc
1775 GR_GL_GREATER, // kGreater_StencilFunc
1776 GR_GL_GEQUAL, // kGEqual_StencilFunc
1777 GR_GL_LESS, // kLess_StencilFunc
1778 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1779 GR_GL_EQUAL, // kEqual_StencilFunc,
1780 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1781 };
1782 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1783 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1784 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1785 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1786 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1787 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1788 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1789 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1790 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1791 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1792
1793 return gTable[basicFunc];
1794}
1795
1796GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1797 static const GrGLenum gTable[] = {
1798 GR_GL_KEEP, // kKeep_StencilOp
1799 GR_GL_REPLACE, // kReplace_StencilOp
1800 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1801 GR_GL_INCR, // kIncClamp_StencilOp
1802 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1803 GR_GL_DECR, // kDecClamp_StencilOp
1804 GR_GL_ZERO, // kZero_StencilOp
1805 GR_GL_INVERT, // kInvert_StencilOp
1806 };
1807 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1808 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1809 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1810 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1811 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1812 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1813 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1814 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1815 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1816 GrAssert((unsigned) op < kStencilOpCount);
1817 return gTable[op];
1818}
1819
1820void set_gl_stencil(const GrGLInterface* gl,
1821 GrGLenum glFace,
1822 GrStencilFunc func,
1823 GrStencilOp failOp,
1824 GrStencilOp passOp,
1825 unsigned int ref,
1826 unsigned int mask,
1827 unsigned int writeMask) {
1828 GrGLenum glFunc = gr_to_gl_stencil_func(func);
1829 GrGLenum glFailOp = gr_to_gl_stencil_op(failOp);
1830 GrGLenum glPassOp = gr_to_gl_stencil_op(passOp);
1831
1832 if (GR_GL_FRONT_AND_BACK == glFace) {
1833 // we call the combined func just in case separate stencil is not
1834 // supported.
1835 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1836 GR_GL_CALL(gl, StencilMask(writeMask));
1837 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1838 } else {
1839 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1840 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1841 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1842 }
1843}
1844}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001845
reed@google.comac10a2d2010-12-22 21:39:39 +00001846void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001847 const GrDrawState& drawState = this->getDrawState();
1848
reed@google.comac10a2d2010-12-22 21:39:39 +00001849 // use stencil for clipping if clipping is enabled and the clip
1850 // has been written into the stencil.
bsalomon@google.com411dad02012-06-05 20:24:20 +00001851 GrClipMaskManager::StencilClipMode clipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001852 if (fClipMaskManager.isClipInStencil() &&
1853 drawState.isClipState()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001854 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001855 // We can't be modifying the clip and respecting it at the same time.
1856 GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1857 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001858 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001859 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001860 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001861 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001862
bsalomon@google.com411dad02012-06-05 20:24:20 +00001863 // The caller may not be using the stencil buffer but we may need to enable
1864 // it in order to respect a stencil clip.
1865 const GrStencilSettings* settings = &drawState.getStencil();
1866 if (settings->isDisabled() &&
1867 GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1868 settings = GetClipStencilSettings();
1869 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001870
bsalomon@google.com411dad02012-06-05 20:24:20 +00001871 // TODO: dynamically attach a stencil buffer
1872 int stencilBits = 0;
1873 GrStencilBuffer* stencilBuffer =
1874 drawState.getRenderTarget()->getStencilBuffer();
1875 if (NULL != stencilBuffer) {
1876 stencilBits = stencilBuffer->bits();
1877 }
1878 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.comd302f142011-03-03 13:54:13 +00001879
bsalomon@google.com411dad02012-06-05 20:24:20 +00001880 bool updateStencilSettings = stencilBits > 0 &&
1881 ((fHWStencilSettings != *settings) ||
1882 (fHWStencilClipMode != clipMode));
1883 if (updateStencilSettings) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001884 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001885 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001886 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001887 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001888 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001889 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001890 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1891 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1892 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1893 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1894 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1895 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1896 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1897 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001898 }
1899 #endif
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001901 unsigned int frontRef = settings->frontFuncRef();
1902 unsigned int frontMask = settings->frontFuncMask();
1903 unsigned int frontWriteMask = settings->frontWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001904
bsalomon@google.com411dad02012-06-05 20:24:20 +00001905 GrStencilFunc frontFunc =
1906 fClipMaskManager.adjustStencilParams(settings->frontFunc(),
1907 clipMode,
1908 stencilBits,
1909 &frontRef,
1910 &frontMask,
1911 &frontWriteMask);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001912 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001913 unsigned int backRef = settings->backFuncRef();
1914 unsigned int backMask = settings->backFuncMask();
1915 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001916
bsalomon@google.com411dad02012-06-05 20:24:20 +00001917 GrStencilFunc backFunc =
1918 fClipMaskManager.adjustStencilParams(settings->frontFunc(),
1919 clipMode,
1920 stencilBits,
1921 &backRef,
1922 &backMask,
1923 &backWriteMask);
1924 set_gl_stencil(this->glInterface(),
1925 GR_GL_FRONT,
1926 frontFunc,
1927 settings->frontFailOp(),
1928 settings->frontPassOp(),
1929 frontRef,
1930 frontMask,
1931 frontWriteMask);
1932 set_gl_stencil(this->glInterface(),
1933 GR_GL_BACK,
1934 backFunc,
1935 settings->backFailOp(),
1936 settings->backPassOp(),
1937 backRef,
1938 backMask,
1939 backWriteMask);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001940 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001941 set_gl_stencil(this->glInterface(),
1942 GR_GL_FRONT_AND_BACK,
1943 frontFunc,
1944 settings->frontFailOp(),
1945 settings->frontPassOp(),
1946 frontRef,
1947 frontMask,
1948 frontWriteMask);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001949 }
1950 }
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001951 fHWStencilSettings = *settings;
1952 fHWStencilClipMode = clipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001953 }
1954}
1955
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001957 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1960 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001961 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001962 bool smoothLines = false;
1963
bsalomon@google.com0650e812011-04-08 18:07:53 +00001964 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001965 smoothLines = this->willUseHWAALines();
1966 if (smoothLines) {
1967 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1968 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1969 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1970 // must disable msaa to use line smoothing
1971 if (rt->isMultisampled() &&
1972 kNo_TriState != fHWAAState.fMSAAEnabled) {
1973 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1974 fHWAAState.fMSAAEnabled = kNo_TriState;
1975 }
1976 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001977 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001978 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1979 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1980 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1981 }
1982 }
1983 }
1984 if (!smoothLines && rt->isMultisampled()) {
1985 if (this->getDrawState().isHWAntialiasState()) {
1986 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1987 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1988 fHWAAState.fMSAAEnabled = kYes_TriState;
1989 }
1990 } else {
1991 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1992 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1993 fHWAAState.fMSAAEnabled = kNo_TriState;
1994 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001995 }
1996 }
1997 }
1998}
1999
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002000void GrGpuGL::flushBlend(GrPrimitiveType type,
2001 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002002 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002003 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002004 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002005 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002006 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002007 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002008 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
2009 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002010 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2011 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002012 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
2013 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002014 }
2015 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002016 // any optimization to disable blending should
2017 // have already been applied and tweaked the coeffs
2018 // to (1, 0).
2019 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2020 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002021 if (blendOff) {
2022 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002023 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002024 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002025 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002026 } else {
2027 if (kYes_TriState != fHWBlendState.fEnabled) {
2028 GL_CALL(Enable(GR_GL_BLEND));
2029 fHWBlendState.fEnabled = kYes_TriState;
2030 }
2031 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2032 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002033 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2034 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002035 fHWBlendState.fSrcCoeff = srcCoeff;
2036 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002037 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002038 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002039 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2040 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002041 (!fHWBlendState.fConstColorValid ||
2042 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002043
2044 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002045 GrColorUnpackR(blendConst) / 255.f,
2046 GrColorUnpackG(blendConst) / 255.f,
2047 GrColorUnpackB(blendConst) / 255.f,
2048 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002049 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002050 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002051 fHWBlendState.fConstColor = blendConst;
2052 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002053 }
2054 }
2055 }
2056}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002057namespace {
2058
2059unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002060 switch (filter) {
2061 case GrSamplerState::kBilinear_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002062 return GR_GL_LINEAR;
2063 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002064 return GR_GL_NEAREST;
2065 default:
2066 GrAssert(!"Unknown filter type");
2067 return GR_GL_LINEAR;
2068 }
2069}
2070
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002071// get_swizzle is only called from this .cpp so it is OK to inline it here
2072inline const GrGLenum* get_swizzle(GrPixelConfig config,
2073 const GrSamplerState& sampler,
2074 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002075 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002076 if (glCaps.textureRedSupport()) {
2077 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2078 GR_GL_RED, GR_GL_RED };
2079 return gRedSmear;
2080 } else {
2081 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2082 GR_GL_ALPHA, GR_GL_ALPHA };
2083 return gAlphaSmear;
2084 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002085 } else if (sampler.swapsRAndB()) {
2086 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2087 GR_GL_RED, GR_GL_ALPHA };
2088 return gRedBlueSwap;
2089 } else {
2090 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2091 GR_GL_BLUE, GR_GL_ALPHA };
2092 return gStraight;
2093 }
2094}
2095
2096void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002097 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2098 GR_GL_TEXTURE_SWIZZLE_RGBA,
2099 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002100}
2101}
2102
bsalomon@google.com4c883782012-06-04 19:05:11 +00002103void GrGpuGL::flushBoundTextureAndParams(int stage) {
2104 GrDrawState* drawState = this->drawState();
2105
2106 GrGLTexture* nextTexture =
2107 static_cast<GrGLTexture*>(drawState->getTexture(stage));
2108
2109 // true for now, but maybe not with GrEffect.
2110 GrAssert(NULL != nextTexture);
2111 // if we created a rt/tex and rendered to it without using a
2112 // texture and now we're texturing from the rt it will still be
2113 // the last bound texture, but it needs resolving. So keep this
2114 // out of the "last != next" check.
2115 GrGLRenderTarget* texRT =
2116 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2117 if (NULL != texRT) {
2118 this->onResolveRenderTarget(texRT);
2119 }
2120
2121 if (fHWBoundTextures[stage] != nextTexture) {
2122 this->setTextureUnit(stage);
2123 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002124 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2125 fHWBoundTextures[stage] = nextTexture;
2126 }
2127
2128 const GrSamplerState& sampler = drawState->getSampler(stage);
2129 ResetTimestamp timestamp;
2130 const GrGLTexture::TexParams& oldTexParams =
2131 nextTexture->getCachedTexParams(&timestamp);
2132 bool setAll = timestamp < this->getResetTimestamp();
2133 GrGLTexture::TexParams newTexParams;
2134
2135 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
2136
2137 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
2138 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2139 newTexParams.fWrapT = wraps[sampler.getWrapY()];
2140 memcpy(newTexParams.fSwizzleRGBA,
2141 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
2142 sizeof(newTexParams.fSwizzleRGBA));
2143 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2144 this->setTextureUnit(stage);
2145 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2146 GR_GL_TEXTURE_MAG_FILTER,
2147 newTexParams.fFilter));
2148 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2149 GR_GL_TEXTURE_MIN_FILTER,
2150 newTexParams.fFilter));
2151 }
2152 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2153 this->setTextureUnit(stage);
2154 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2155 GR_GL_TEXTURE_WRAP_S,
2156 newTexParams.fWrapS));
2157 }
2158 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2159 this->setTextureUnit(stage);
2160 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2161 GR_GL_TEXTURE_WRAP_T,
2162 newTexParams.fWrapT));
2163 }
2164 if (this->glCaps().textureSwizzleSupport() &&
2165 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2166 oldTexParams.fSwizzleRGBA,
2167 sizeof(newTexParams.fSwizzleRGBA)))) {
2168 this->setTextureUnit(stage);
2169 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2170 this->glInterface());
2171 }
2172 nextTexture->setCachedTexParams(newTexParams,
2173 this->getResetTimestamp());
2174}
2175
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002176void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002177
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002178 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002179
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002180 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002181 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002182 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002183 fHWDitherEnabled = kYes_TriState;
2184 }
2185 } else {
2186 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002187 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002188 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002189 }
2190 }
2191
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002192 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002193 if (kNo_TriState != fHWWriteToColor) {
2194 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2195 GR_GL_FALSE, GR_GL_FALSE));
2196 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002197 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002198 } else {
2199 if (kYes_TriState != fHWWriteToColor) {
2200 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2201 fHWWriteToColor = kYes_TriState;
2202 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002203 }
2204
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002205 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002206 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002207 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002208 GL_CALL(Enable(GR_GL_CULL_FACE));
2209 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002210 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002211 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002212 GL_CALL(Enable(GR_GL_CULL_FACE));
2213 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002214 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002215 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002216 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 break;
2218 default:
2219 GrCrash("Unknown draw face.");
2220 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002221 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002222 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002223}
2224
2225void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002226 if (fHWGeometryState.fVertexBuffer != buffer) {
2227 fHWGeometryState.fArrayPtrsDirty = true;
2228 fHWGeometryState.fVertexBuffer = buffer;
2229 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002230}
2231
2232void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002233 if (fHWGeometryState.fVertexBuffer == buffer) {
2234 // deleting bound buffer does implied bind to 0
2235 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002236 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002237 }
2238}
2239
2240void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002241 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002242}
2243
2244void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002245 if (fHWGeometryState.fIndexBuffer == buffer) {
2246 // deleting bound buffer does implied bind to 0
2247 fHWGeometryState.fIndexBuffer = NULL;
2248 }
2249}
2250
reed@google.comac10a2d2010-12-22 21:39:39 +00002251void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2252 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002253 GrDrawState* drawState = this->drawState();
2254 if (drawState->getRenderTarget() == renderTarget) {
2255 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002256 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002257 if (fHWBoundRenderTarget == renderTarget) {
2258 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002260}
2261
2262void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002263 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002264 GrDrawState* drawState = this->drawState();
2265 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002266 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002267 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002268 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002269 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002270 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002271 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002272 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002273}
2274
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002275bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2276 bool getSizedInternalFormat,
2277 GrGLenum* internalFormat,
2278 GrGLenum* externalFormat,
2279 GrGLenum* externalType) {
2280 GrGLenum dontCare;
2281 if (NULL == internalFormat) {
2282 internalFormat = &dontCare;
2283 }
2284 if (NULL == externalFormat) {
2285 externalFormat = &dontCare;
2286 }
2287 if (NULL == externalType) {
2288 externalType = &dontCare;
2289 }
2290
reed@google.comac10a2d2010-12-22 21:39:39 +00002291 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002292 case kRGBA_8888_PM_GrPixelConfig:
2293 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002294 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002295 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002296 if (getSizedInternalFormat) {
2297 *internalFormat = GR_GL_RGBA8;
2298 } else {
2299 *internalFormat = GR_GL_RGBA;
2300 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002301 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002302 break;
2303 case kBGRA_8888_PM_GrPixelConfig:
2304 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002305 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002306 return false;
2307 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002308 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002309 if (getSizedInternalFormat) {
2310 *internalFormat = GR_GL_BGRA8;
2311 } else {
2312 *internalFormat = GR_GL_BGRA;
2313 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002314 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002315 if (getSizedInternalFormat) {
2316 *internalFormat = GR_GL_RGBA8;
2317 } else {
2318 *internalFormat = GR_GL_RGBA;
2319 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002320 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002321 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002322 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002323 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002324 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002325 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002326 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002327 if (getSizedInternalFormat) {
2328 if (this->glBinding() == kDesktop_GrGLBinding) {
2329 return false;
2330 } else {
2331 *internalFormat = GR_GL_RGB565;
2332 }
2333 } else {
2334 *internalFormat = GR_GL_RGB;
2335 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002336 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002337 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002338 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002339 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002340 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002341 if (getSizedInternalFormat) {
2342 *internalFormat = GR_GL_RGBA4;
2343 } else {
2344 *internalFormat = GR_GL_RGBA;
2345 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002346 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002347 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002348 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002349 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002350 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002351 // glCompressedTexImage doesn't take external params
2352 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002353 // no sized/unsized internal format distinction here
2354 *internalFormat = GR_GL_PALETTE8_RGBA8;
2355 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002356 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002357 } else {
2358 return false;
2359 }
2360 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002361 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002362 if (this->glCaps().textureRedSupport()) {
2363 *internalFormat = GR_GL_RED;
2364 *externalFormat = GR_GL_RED;
2365 if (getSizedInternalFormat) {
2366 *internalFormat = GR_GL_R8;
2367 } else {
2368 *internalFormat = GR_GL_RED;
2369 }
2370 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002371 } else {
2372 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002373 *externalFormat = GR_GL_ALPHA;
2374 if (getSizedInternalFormat) {
2375 *internalFormat = GR_GL_ALPHA8;
2376 } else {
2377 *internalFormat = GR_GL_ALPHA;
2378 }
2379 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002380 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002381 break;
2382 default:
2383 return false;
2384 }
2385 return true;
2386}
2387
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002388void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002389 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002390 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002391 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002392 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002393 }
2394}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002395
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002396void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002397 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002398 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002399 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002400 }
2401}
2402
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002403void GrGpuGL::setBuffers(bool indexed,
2404 int* extraVertexOffset,
2405 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002406
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002407 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002408
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002409 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2410
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002411 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002412 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002413 case kBuffer_GeometrySrcType:
2414 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002415 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002416 break;
2417 case kArray_GeometrySrcType:
2418 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002419 this->finalizeReservedVertices();
2420 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2421 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002422 break;
2423 default:
2424 vbuf = NULL; // suppress warning
2425 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002426 }
2427
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002428 GrAssert(NULL != vbuf);
2429 GrAssert(!vbuf->isLocked());
2430 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002431 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002432 fHWGeometryState.fArrayPtrsDirty = true;
2433 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002434 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002435
2436 if (indexed) {
2437 GrAssert(NULL != extraIndexOffset);
2438
2439 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002440 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002441 case kBuffer_GeometrySrcType:
2442 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002443 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002444 break;
2445 case kArray_GeometrySrcType:
2446 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002447 this->finalizeReservedIndices();
2448 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2449 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002450 break;
2451 default:
2452 ibuf = NULL; // suppress warning
2453 GrCrash("Unknown geometry src type!");
2454 }
2455
2456 GrAssert(NULL != ibuf);
2457 GrAssert(!ibuf->isLocked());
2458 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002459 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460 fHWGeometryState.fIndexBuffer = ibuf;
2461 }
2462 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002463}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002464