blob: 5c1764cf2985e8deb4fc76f5e606b8a6d6f3e59d [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
caryclark@google.comcf6285b2012-06-06 12:09:01 +000015static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000016static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
31#else
32 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000037
38///////////////////////////////////////////////////////////////////////////////
39
twiz@google.com0f31ca72011-03-18 17:38:11 +000040static const GrGLenum gXfermodeCoeff2Blend[] = {
41 GR_GL_ZERO,
42 GR_GL_ONE,
43 GR_GL_SRC_COLOR,
44 GR_GL_ONE_MINUS_SRC_COLOR,
45 GR_GL_DST_COLOR,
46 GR_GL_ONE_MINUS_DST_COLOR,
47 GR_GL_SRC_ALPHA,
48 GR_GL_ONE_MINUS_SRC_ALPHA,
49 GR_GL_DST_ALPHA,
50 GR_GL_ONE_MINUS_DST_ALPHA,
51 GR_GL_CONSTANT_COLOR,
52 GR_GL_ONE_MINUS_CONSTANT_COLOR,
53 GR_GL_CONSTANT_ALPHA,
54 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000055
56 // extended blend coeffs
57 GR_GL_SRC1_COLOR,
58 GR_GL_ONE_MINUS_SRC1_COLOR,
59 GR_GL_SRC1_ALPHA,
60 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000061};
62
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000064 static const bool gCoeffReferencesBlendConst[] = {
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 true,
76 true,
77 true,
78 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000079
80 // extended blend coeffs
81 false,
82 false,
83 false,
84 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000085 };
86 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000087 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
88 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000089
bsalomon@google.com47059542012-06-06 20:51:20 +000090 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
91 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
92 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
93 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
94 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
95 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
97 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
98 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
99 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
101 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
103 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000104
bsalomon@google.com47059542012-06-06 20:51:20 +0000105 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
106 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
108 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000109
110 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000111 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
112 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000113}
114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115///////////////////////////////////////////////////////////////////////////////
116
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000117static bool gPrintStartupSpew;
118
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000119static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000120
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000121 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000122
twiz@google.com0f31ca72011-03-18 17:38:11 +0000123 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000124 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
125 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000126 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000127 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
128 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000129 // some implementations require texture to be mip-map complete before
130 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000131 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000132 GR_GL_TEXTURE_MIN_FILTER,
133 GR_GL_NEAREST));
134 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
135 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
136 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
137 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
138 GR_GL_COLOR_ATTACHMENT0,
139 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000140 GrGLenum status;
141 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000142 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
143 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000144
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000145 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000146}
147
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000148GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
149
150 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000151
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000152 fillInConfigRenderableTable();
153
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000154 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000155
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000156 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000157
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000158 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000159 const GrGLubyte* ext;
160 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000161 const GrGLubyte* vendor;
162 const GrGLubyte* renderer;
163 const GrGLubyte* version;
164 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
165 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
166 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000167 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
168 this);
169 GrPrintf("------ VENDOR %s\n", vendor);
170 GrPrintf("------ RENDERER %s\n", renderer);
171 GrPrintf("------ VERSION %s\n", version);
172 GrPrintf("------ EXTENSIONS\n %s \n", ext);
173 }
174
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000175 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000176
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000177 fProgramData = NULL;
178 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000179
bsalomon@google.comfe676522011-06-17 18:12:21 +0000180 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000181 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000182 if (false) { // avoid bit rot, suppress warning
183 fbo_test(this->glInterface(), 0, 0);
184 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000185}
186
187GrGpuGL::~GrGpuGL() {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000188 if (fProgramData && 0 != fHWProgramID) {
189 // detach the current program so there is no confusion on OpenGL's part
190 // that we want it to be deleted
191 GrAssert(fHWProgramID == fProgramData->fProgramID);
192 GL_CALL(UseProgram(0));
193 }
194
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000195 delete fProgramCache;
196 fProgramCache = NULL;
197 fProgramData = NULL;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000198
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000199 // This must be called by before the GrDrawTarget destructor
200 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000201 // This subclass must do this before the base class destructor runs
202 // since we will unref the GrGLInterface.
203 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000204}
205
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000206///////////////////////////////////////////////////////////////////////////////
207
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000208void GrGpuGL::initCaps() {
209 GrGLint maxTextureUnits;
210 // check FS and fixed-function texture unit limits
211 // we only use textures in the fragment stage currently.
212 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000213 const GrGLInterface* gl = this->glInterface();
214 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000215 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000216
217 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000218 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000219 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000220 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000221 for (int i = 0; i < numFormats; ++i) {
222 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
223 fCaps.f8BitPaletteSupport = true;
224 break;
225 }
226 }
227
228 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000229 // we could also look for GL_ATI_separate_stencil extension or
230 // GL_EXT_stencil_two_side but they use different function signatures
231 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000232 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000233 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000234 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 this->hasExtension("GL_EXT_stencil_wrap");
236 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000237 // ES 2 has two sided stencil and stencil wrap
238 fCaps.fTwoSidedStencilSupport = true;
239 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000240 }
241
242 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000243 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
244 // extension includes glMapBuffer.
245 } else {
246 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
247 }
248
249 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000250 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
252 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 } else {
254 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000255 }
256 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000257 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000258 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 }
260
261 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
262
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000263 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
264 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000265 // Our render targets are always created with textures as the color
266 // attachment, hence this min:
267 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
268
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000269 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000270
271 // Enable supported shader-related caps
272 if (kDesktop_GrGLBinding == this->glBinding()) {
273 fCaps.fDualSourceBlendingSupport =
274 this->glVersion() >= GR_GL_VER(3,3) ||
275 this->hasExtension("GL_ARB_blend_func_extended");
276 fCaps.fShaderDerivativeSupport = true;
277 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
278 fCaps.fGeometryShaderSupport =
279 this->glVersion() >= GR_GL_VER(3,2) &&
280 this->glslGeneration() >= k150_GrGLSLGeneration;
281 } else {
282 fCaps.fShaderDerivativeSupport =
283 this->hasExtension("GL_OES_standard_derivatives");
284 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000285}
286
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000287void GrGpuGL::fillInConfigRenderableTable() {
288
289 // OpenGL < 3.0
290 // no support for render targets unless the GL_ARB_framebuffer_object
291 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
292 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
293 // probably don't get R8 in this case.
294
295 // OpenGL 3.0
296 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
297 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
298
299 // >= OpenGL 3.1
300 // base color renderable: RED, RG, RGB, and RGBA
301 // sized derivatives: R8, RGBA4, RGBA8
302 // if the GL_ARB_compatibility extension is supported then we get back
303 // support for GL_ALPHA and ALPHA8
304
305 // GL_EXT_bgra adds BGRA render targets to any version
306
307 // ES 2.0
308 // color renderable: RGBA4, RGB5_A1, RGB565
309 // GL_EXT_texture_rg adds support for R8 as a color render target
310 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
311 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
312 // added BGRA support
313
314 if (kDesktop_GrGLBinding == this->glBinding()) {
315 // Post 3.0 we will get R8
316 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
317 if (this->glVersion() >= GR_GL_VER(3,0) ||
318 this->hasExtension("GL_ARB_framebuffer_object")) {
319 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
320 }
321 } else {
322 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000323 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
324 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000325 }
326
327 if (kDesktop_GrGLBinding != this->glBinding()) {
328 // only available in ES
329 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
330 }
331
332 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
333 // GL_EXT_framebuffer_object for FBO support. Both of these
334 // allow RGBA4 render targets so this is always supported.
335 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
336
337 if (this->glCaps().rgba8RenderbufferSupport()) {
338 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
339 }
340
341 if (this->glCaps().bgraFormatSupport()) {
342 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
343 }
344
345 // the un-premultiplied formats just inherit the premultiplied setting
346 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
347 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
348 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
349 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
350}
351
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000352bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
353 if (kUnknown_CanPreserveUnpremulRoundtrip ==
354 fCanPreserveUnpremulRoundtrip) {
355
356 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
357 uint32_t* srcData = data.get();
358 uint32_t* firstRead = data.get() + 256 * 256;
359 uint32_t* secondRead = data.get() + 2 * 256 * 256;
360
361 for (int y = 0; y < 256; ++y) {
362 for (int x = 0; x < 256; ++x) {
363 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
364 color[3] = y;
365 color[2] = x;
366 color[1] = x;
367 color[0] = x;
368 }
369 }
370
371 // We have broader support for read/write pixels on render targets
372 // than on textures.
373 GrTextureDesc dstDesc;
374 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
375 kNoStencil_GrTextureFlagBit;
376 dstDesc.fWidth = 256;
377 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000378 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000379
380 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
381 if (!dstTex.get()) {
382 return false;
383 }
384 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
385 GrAssert(NULL != rt);
386
387 bool failed = true;
388 static const UnpremulConversion gMethods[] = {
389 kUpOnWrite_DownOnRead_UnpremulConversion,
390 kDownOnWrite_UpOnRead_UnpremulConversion,
391 };
392
393 // pretend that we can do the roundtrip to avoid recursive calls to
394 // this function
395 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
396 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
397 fUnpremulConversion = gMethods[i];
398 rt->writePixels(0, 0,
399 256, 256,
400 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
401 rt->readPixels(0, 0,
402 256, 256,
403 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
404 rt->writePixels(0, 0,
405 256, 256,
406 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
407 rt->readPixels(0, 0,
408 256, 256,
409 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
410 failed = false;
411 for (int j = 0; j < 256 * 256; ++j) {
412 if (firstRead[j] != secondRead[j]) {
413 failed = true;
414 break;
415 }
416 }
417 }
418 fCanPreserveUnpremulRoundtrip = failed ?
419 kNo_CanPreserveUnpremulRoundtrip :
420 kYes_CanPreserveUnpremulRoundtrip;
421 }
422
423 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
424 return true;
425 } else {
426 return false;
427 }
428}
429
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000430GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000431 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
432 return GrPixelConfigSwapRAndB(config);
433 } else {
434 return config;
435 }
436}
437
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000438GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000439 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000440 return GrPixelConfigSwapRAndB(config);
441 } else {
442 return config;
443 }
444}
445
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000446bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
447 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
448}
449
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000450void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000451 if (gPrintStartupSpew && !fPrintedCaps) {
452 fPrintedCaps = true;
453 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000454 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000455 }
456
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000457 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000458 GL_CALL(Disable(GR_GL_DEPTH_TEST));
459 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000460
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000461 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
462 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000464 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000465 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000466 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000467 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
468 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
469 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
470 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000471 if (this->glCaps().imagingSupport()) {
472 GL_CALL(Disable(GR_GL_COLOR_TABLE));
473 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000474 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
475 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
476 // Since ES doesn't support glPointSize at all we always use the VS to
477 // set the point size
478 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
479
480 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
481 // currently part of our gl interface. There are probably others as
482 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000483 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000484 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000485 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000486
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000488 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000489
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000490 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000491 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000492
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000493 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000494
tomhudson@google.com93813632011-10-27 20:21:16 +0000495 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000496 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000497 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000498
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000499 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000500 // set to true to force disableScissor to make a GL call.
501 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000502 this->disableScissor();
503
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000504 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000505
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000506 fHWStencilSettings.invalidate();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000507 // This is arbitrary. The above invalidate ensures a full setup of the
508 // stencil on the next draw.
509 fHWStencilClipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000510
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 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;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001016 glTexDesc.fClientCacheID = desc.fClientCacheID;
robertphillips@google.com32716282012-06-04 12:48:45 +00001017
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001018 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001019
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001020 glRTDesc.fMSColorRenderbufferID = 0;
1021 glRTDesc.fRTFBOID = 0;
1022 glRTDesc.fTexFBOID = 0;
1023 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001024 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001025
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001026 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001027
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001028 const Caps& caps = this->getCaps();
1029
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001030 // We keep GrRenderTargets in GL's normal orientation so that they
1031 // can be drawn to by the outside world without the client having
1032 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001033 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001034 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001035
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001036 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001037 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001038 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001039 //GrPrintf("MSAA RT requested but not supported on this platform.");
1040 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 }
1042
reed@google.comac10a2d2010-12-22 21:39:39 +00001043 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001044 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1045 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001046 return return_null_texture();
1047 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001048 }
1049
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001050 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001051 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001052 // provides a hint about how this texture will be used
1053 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1054 GR_GL_TEXTURE_USAGE,
1055 GR_GL_FRAMEBUFFER_ATTACHMENT));
1056 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001057 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001058 return return_null_texture();
1059 }
1060
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001061 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001062 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001063
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001064 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1065 // drivers have a bug where an FBO won't be complete if it includes a
1066 // texture that is not mipmap complete (considering the filter in use).
1067 GrGLTexture::TexParams initialTexParams;
1068 // we only set a subset here so invalidate first
1069 initialTexParams.invalidate();
1070 initialTexParams.fFilter = GR_GL_NEAREST;
1071 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1072 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001073 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1074 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001075 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1077 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001078 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1080 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001081 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1083 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001084 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001085 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1086 glTexDesc.fWidth, glTexDesc.fHeight,
1087 desc.fConfig, srcData, rowBytes)) {
1088 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1089 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001090 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001091
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001092 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001094 // unbind the texture from the texture unit before binding it to the frame buffer
1095 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1096
bsalomon@google.com99621082011-11-15 16:47:16 +00001097 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1098 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001099 glTexDesc.fTextureID,
1100 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001101 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001102 return return_null_texture();
1103 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001104 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001106 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001107 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001108 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001109#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001110 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1111 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001112#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113 return tex;
1114}
1115
1116namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001117
1118const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1119
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1121 GrGLuint rb,
1122 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123 // we shouldn't ever know one size and not the other
1124 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1125 (kUnknownBitCount == format->fTotalBits));
1126 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1129 (GrGLint*)&format->fStencilBits);
1130 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1133 (GrGLint*)&format->fTotalBits);
1134 format->fTotalBits += format->fStencilBits;
1135 } else {
1136 format->fTotalBits = format->fStencilBits;
1137 }
1138 }
1139}
1140}
1141
1142bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1143 int width, int height) {
1144
1145 // All internally created RTs are also textures. We don't create
1146 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1147 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001148 GrAssert(width >= rt->width());
1149 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150
1151 int samples = rt->numSamples();
1152 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001153 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 if (!sbID) {
1155 return false;
1156 }
1157
1158 GrGLStencilBuffer* sb = NULL;
1159
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001160 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001162 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 // we start with the last stencil format that succeeded in hopes
1164 // that we won't go through this loop more than once after the
1165 // first (painful) stencil creation.
1166 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001167 const GrGLCaps::StencilFormat& sFmt =
1168 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001169 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001170 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001172 bool created;
1173 if (samples > 0) {
1174 created = renderbuffer_storage_msaa(fGLContextInfo,
1175 samples,
1176 sFmt.fInternalFormat,
1177 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001179 GL_ALLOC_CALL(this->glInterface(),
1180 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001181 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001182 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001183 created =
1184 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001186 if (created) {
1187 // After sized formats we attempt an unsized format and take
1188 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001189 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001190 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001191 sb = new GrGLStencilBuffer(this, sbID, width, height,
1192 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1194 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001195 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 return true;
1198 }
1199 sb->abandon(); // otherwise we lose sbID
1200 sb->unref();
1201 }
1202 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001203 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001204 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205}
1206
1207bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1208 GrRenderTarget* rt) {
1209 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1210
1211 GrGLuint fbo = glrt->renderFBOID();
1212
1213 if (NULL == sb) {
1214 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001215 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 GR_GL_STENCIL_ATTACHMENT,
1217 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001218 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219 GR_GL_DEPTH_ATTACHMENT,
1220 GR_GL_RENDERBUFFER, 0));
1221#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001222 GrGLenum status;
1223 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1225#endif
1226 }
1227 return true;
1228 } else {
1229 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1230 GrGLuint rb = glsb->renderbufferID();
1231
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001232 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001233 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1234 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 GR_GL_STENCIL_ATTACHMENT,
1236 GR_GL_RENDERBUFFER, rb));
1237 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001238 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 GR_GL_DEPTH_ATTACHMENT,
1240 GR_GL_RENDERBUFFER, rb));
1241 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001242 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 GR_GL_DEPTH_ATTACHMENT,
1244 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001245 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001246
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001247 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001248 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001249 glsb->format())) {
1250 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1251 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001252 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001253 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001254 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001255 if (glsb->format().fPacked) {
1256 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1257 GR_GL_DEPTH_ATTACHMENT,
1258 GR_GL_RENDERBUFFER, 0));
1259 }
1260 return false;
1261 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001262 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001263 rt->config(),
1264 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001265 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001266 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001267 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001269}
1270
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001271////////////////////////////////////////////////////////////////////////////////
1272
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001273GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001274 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001275 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001276 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001277 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001278 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001279 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001281 GL_ALLOC_CALL(this->glInterface(),
1282 BufferData(GR_GL_ARRAY_BUFFER,
1283 size,
1284 NULL, // data ptr
1285 dynamic ? GR_GL_DYNAMIC_DRAW :
1286 GR_GL_STATIC_DRAW));
1287 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 // deleting bound buffer does implicit bind to 0
1290 fHWGeometryState.fVertexBuffer = NULL;
1291 return NULL;
1292 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001293 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 size, dynamic);
1295 fHWGeometryState.fVertexBuffer = vertexBuffer;
1296 return vertexBuffer;
1297 }
1298 return NULL;
1299}
1300
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001301GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001302 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001303 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001305 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001306 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001308 GL_ALLOC_CALL(this->glInterface(),
1309 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1310 size,
1311 NULL, // data ptr
1312 dynamic ? GR_GL_DYNAMIC_DRAW :
1313 GR_GL_STATIC_DRAW));
1314 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001315 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 // deleting bound buffer does implicit bind to 0
1317 fHWGeometryState.fIndexBuffer = NULL;
1318 return NULL;
1319 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001320 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001321 size, dynamic);
1322 fHWGeometryState.fIndexBuffer = indexBuffer;
1323 return indexBuffer;
1324 }
1325 return NULL;
1326}
1327
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001328GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
1329 GrPath* path = NULL;
1330 if (fCaps.fPathStencilingSupport) {
1331 path = new GrGLPath(this, inPath);
1332 }
1333 return path;
1334}
1335
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001336void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001337 const GrDrawState& drawState = this->getDrawState();
1338 const GrGLRenderTarget* rt =
1339 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1340
1341 GrAssert(NULL != rt);
1342 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001343
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001344 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001345 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1346 rect.width(), rect.height());
1347 if (scissor.contains(vp)) {
1348 disableScissor();
1349 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 }
1351
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001352 if (fHWBounds.fScissorRect != scissor) {
1353 scissor.pushToGLScissor(this->glInterface());
1354 fHWBounds.fScissorRect = scissor;
1355 }
1356 if (!fHWBounds.fScissorEnabled) {
1357 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1358 fHWBounds.fScissorEnabled = true;
1359 }
1360}
1361
1362void GrGpuGL::disableScissor() {
1363 if (fHWBounds.fScissorEnabled) {
1364 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1365 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001366 }
1367}
1368
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001369void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001370 const GrDrawState& drawState = this->getDrawState();
1371 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001372 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001373 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001374
bsalomon@google.com74b98712011-11-11 19:46:16 +00001375 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001376 if (NULL != rect) {
1377 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001378 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001379 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001380 if (clippedRect.intersect(rtRect)) {
1381 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001382 } else {
1383 return;
1384 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001385 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001386 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001387 if (NULL != rect)
1388 this->enableScissoring(*rect);
1389 else
1390 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001391
1392 GrGLfloat r, g, b, a;
1393 static const GrGLfloat scale255 = 1.f / 255.f;
1394 a = GrColorUnpackA(color) * scale255;
1395 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001396 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001397 scaleRGB *= a;
1398 }
1399 r = GrColorUnpackR(color) * scaleRGB;
1400 g = GrColorUnpackG(color) * scaleRGB;
1401 b = GrColorUnpackB(color) * scaleRGB;
1402
1403 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001404 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001405 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001406 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001407}
1408
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001409void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001410 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001411 return;
1412 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001413
1414 this->flushRenderTarget(&GrIRect::EmptyIRect());
1415
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001416 this->disableScissor();
1417
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001418 GL_CALL(StencilMask(0xffffffff));
1419 GL_CALL(ClearStencil(0));
1420 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001421 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001422}
1423
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001424void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001425 const GrDrawState& drawState = this->getDrawState();
1426 const GrRenderTarget* rt = drawState.getRenderTarget();
1427 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001428
1429 // this should only be called internally when we know we have a
1430 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001431 GrAssert(NULL != rt->getStencilBuffer());
1432 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001433#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001434 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001435 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001436#else
1437 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001438 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001439 // turned into draws. Our contract on GrDrawTarget says that
1440 // changing the clip between stencil passes may or may not
1441 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001442 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001443#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001444 GrGLint value;
1445 if (insideClip) {
1446 value = (1 << (stencilBitCount - 1));
1447 } else {
1448 value = 0;
1449 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001450 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001451 this->enableScissoring(rect);
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001452 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001453 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001454 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001455 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001456}
1457
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001458void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001459 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001460}
1461
bsalomon@google.comc4364992011-11-07 15:54:49 +00001462bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1463 int left, int top,
1464 int width, int height,
1465 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001466 size_t rowBytes) const {
1467 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001468 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001469 return false;
1470 }
1471
1472 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001473 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001474 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001475 return true;
1476 }
1477 // If we have to do memcpys to handle rowBytes then y-flip is free
1478 // Note the rowBytes might be tight to the passed in data, but if data
1479 // gets clipped in x to the target the rowBytes will no longer be tight.
1480 if (left >= 0 && (left + width) < renderTarget->width()) {
1481 return 0 == rowBytes ||
1482 GrBytesPerPixel(config) * width == rowBytes;
1483 } else {
1484 return false;
1485 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001486}
1487
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001488bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001489 int left, int top,
1490 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001491 GrPixelConfig config,
1492 void* buffer,
1493 size_t rowBytes,
1494 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001495 GrGLenum format;
1496 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001497 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001498 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001499 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001500 size_t bpp = GrBytesPerPixel(config);
1501 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1502 &left, &top, &width, &height,
1503 const_cast<const void**>(&buffer),
1504 &rowBytes)) {
1505 return false;
1506 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001507
bsalomon@google.comc6980972011-11-02 19:57:21 +00001508 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001509 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001510 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001511 switch (tgt->getResolveType()) {
1512 case GrGLRenderTarget::kCantResolve_ResolveType:
1513 return false;
1514 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001515 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001516 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001517 break;
1518 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001519 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001520 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001521 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1522 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001523 break;
1524 default:
1525 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001526 }
1527
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001528 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001529
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001530 // the read rect is viewport-relative
1531 GrGLIRect readRect;
1532 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001533
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001534 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001535 if (0 == rowBytes) {
1536 rowBytes = tightRowBytes;
1537 }
1538 size_t readDstRowBytes = tightRowBytes;
1539 void* readDst = buffer;
1540
1541 // determine if GL can read using the passed rowBytes or if we need
1542 // a scratch buffer.
1543 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1544 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001545 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001546 GrAssert(!(rowBytes % sizeof(GrColor)));
1547 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1548 readDstRowBytes = rowBytes;
1549 } else {
1550 scratch.reset(tightRowBytes * height);
1551 readDst = scratch.get();
1552 }
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, 1));
1556 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001557 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1558 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001559 format, type, readDst));
1560 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001561 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001562 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1563 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001564 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001565 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1566 invertY = true;
1567 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001568
1569 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001570 // API presents top-to-bottom. We must preserve the padding contents. Note
1571 // that the above readPixels did not overwrite the padding.
1572 if (readDst == buffer) {
1573 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001574 if (!invertY) {
1575 scratch.reset(tightRowBytes);
1576 void* tmpRow = scratch.get();
1577 // flip y in-place by rows
1578 const int halfY = height >> 1;
1579 char* top = reinterpret_cast<char*>(buffer);
1580 char* bottom = top + (height - 1) * rowBytes;
1581 for (int y = 0; y < halfY; y++) {
1582 memcpy(tmpRow, top, tightRowBytes);
1583 memcpy(top, bottom, tightRowBytes);
1584 memcpy(bottom, tmpRow, tightRowBytes);
1585 top += rowBytes;
1586 bottom -= rowBytes;
1587 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001588 }
1589 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001590 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001591 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001592 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001593 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001594 char* dst = reinterpret_cast<char*>(buffer);
1595 if (!invertY) {
1596 dst += (height-1) * rowBytes;
1597 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001598 for (int y = 0; y < height; y++) {
1599 memcpy(dst, src, tightRowBytes);
1600 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001601 if (invertY) {
1602 dst += rowBytes;
1603 } else {
1604 dst -= rowBytes;
1605 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001606 }
1607 }
1608 return true;
1609}
1610
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001611void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001612
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001613 GrGLRenderTarget* rt =
1614 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1615 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001616
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001617 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001618 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001619 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001620 GrGLenum status;
1621 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001622 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001623 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001624 }
1625 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001626 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001627 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001628 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001629 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 fHWBounds.fViewportRect = vp;
1631 }
1632 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001633 if (NULL == bound || !bound->isEmpty()) {
1634 rt->flagAsNeedingResolve(bound);
1635 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001636}
1637
twiz@google.com0f31ca72011-03-18 17:38:11 +00001638GrGLenum gPrimitiveType2GLMode[] = {
1639 GR_GL_TRIANGLES,
1640 GR_GL_TRIANGLE_STRIP,
1641 GR_GL_TRIANGLE_FAN,
1642 GR_GL_POINTS,
1643 GR_GL_LINES,
1644 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001645};
1646
bsalomon@google.comd302f142011-03-03 13:54:13 +00001647#define SWAP_PER_DRAW 0
1648
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001649#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001650 #if GR_MAC_BUILD
1651 #include <AGL/agl.h>
1652 #elif GR_WIN32_BUILD
1653 void SwapBuf() {
1654 DWORD procID = GetCurrentProcessId();
1655 HWND hwnd = GetTopWindow(GetDesktopWindow());
1656 while(hwnd) {
1657 DWORD wndProcID = 0;
1658 GetWindowThreadProcessId(hwnd, &wndProcID);
1659 if(wndProcID == procID) {
1660 SwapBuffers(GetDC(hwnd));
1661 }
1662 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1663 }
1664 }
1665 #endif
1666#endif
1667
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001668void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1669 uint32_t startVertex,
1670 uint32_t startIndex,
1671 uint32_t vertexCount,
1672 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001673 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1674
twiz@google.com0f31ca72011-03-18 17:38:11 +00001675 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001676
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001677 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1678 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1679
1680 // our setupGeometry better have adjusted this to zero since
1681 // DrawElements always draws from the begining of the arrays for idx 0.
1682 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001683
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001684 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1685 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001686#if SWAP_PER_DRAW
1687 glFlush();
1688 #if GR_MAC_BUILD
1689 aglSwapBuffers(aglGetCurrentContext());
1690 int set_a_break_pt_here = 9;
1691 aglSwapBuffers(aglGetCurrentContext());
1692 #elif GR_WIN32_BUILD
1693 SwapBuf();
1694 int set_a_break_pt_here = 9;
1695 SwapBuf();
1696 #endif
1697#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001698}
1699
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001700void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1701 uint32_t startVertex,
1702 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1704
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001705 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1706
1707 // our setupGeometry better have adjusted this to zero.
1708 // DrawElements doesn't take an offset so we always adjus the startVertex.
1709 GrAssert(0 == startVertex);
1710
1711 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1712 // account for startVertex in the DrawElements case. So we always
1713 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001714 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001715#if SWAP_PER_DRAW
1716 glFlush();
1717 #if GR_MAC_BUILD
1718 aglSwapBuffers(aglGetCurrentContext());
1719 int set_a_break_pt_here = 9;
1720 aglSwapBuffers(aglGetCurrentContext());
1721 #elif GR_WIN32_BUILD
1722 SwapBuf();
1723 int set_a_break_pt_here = 9;
1724 SwapBuf();
1725 #endif
1726#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001727}
1728
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001729void GrGpuGL::onGpuStencilPath(const GrPath&, GrPathFill) {
1730 GrCrash("Not implemented yet. Should not get here.");
1731}
1732
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001733void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1734
1735 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001736
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001737 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001738 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001740 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1741 rt->renderFBOID()));
1742 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1743 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001744 // make sure we go through flushRenderTarget() since we've modified
1745 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001746 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001747 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001748 const GrIRect dirtyRect = rt->getResolveRect();
1749 GrGLIRect r;
1750 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1751 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001752
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001753 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001754 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001755#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001756 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1757 GL_CALL(Scissor(r.fLeft, r.fBottom,
1758 r.fWidth, r.fHeight));
1759 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001760 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001762#else
1763 this->enableScissoring(dirtyRect);
1764 GL_CALL(ResolveMultisampleFramebuffer());
1765#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001767 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001768 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001769 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1770 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001771 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001772 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001773 int right = r.fLeft + r.fWidth;
1774 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001775 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1776 r.fLeft, r.fBottom, right, top,
1777 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001778 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001779 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 }
1781}
1782
bsalomon@google.com411dad02012-06-05 20:24:20 +00001783namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001784
bsalomon@google.com411dad02012-06-05 20:24:20 +00001785GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1786 static const GrGLenum gTable[] = {
1787 GR_GL_ALWAYS, // kAlways_StencilFunc
1788 GR_GL_NEVER, // kNever_StencilFunc
1789 GR_GL_GREATER, // kGreater_StencilFunc
1790 GR_GL_GEQUAL, // kGEqual_StencilFunc
1791 GR_GL_LESS, // kLess_StencilFunc
1792 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1793 GR_GL_EQUAL, // kEqual_StencilFunc,
1794 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1795 };
1796 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1797 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1798 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1799 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1800 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1801 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1802 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1803 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1804 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1805 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1806
1807 return gTable[basicFunc];
1808}
1809
1810GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1811 static const GrGLenum gTable[] = {
1812 GR_GL_KEEP, // kKeep_StencilOp
1813 GR_GL_REPLACE, // kReplace_StencilOp
1814 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1815 GR_GL_INCR, // kIncClamp_StencilOp
1816 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1817 GR_GL_DECR, // kDecClamp_StencilOp
1818 GR_GL_ZERO, // kZero_StencilOp
1819 GR_GL_INVERT, // kInvert_StencilOp
1820 };
1821 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1822 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1823 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1824 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1825 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1826 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1827 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1828 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1829 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1830 GrAssert((unsigned) op < kStencilOpCount);
1831 return gTable[op];
1832}
1833
1834void set_gl_stencil(const GrGLInterface* gl,
1835 GrGLenum glFace,
1836 GrStencilFunc func,
1837 GrStencilOp failOp,
1838 GrStencilOp passOp,
1839 unsigned int ref,
1840 unsigned int mask,
1841 unsigned int writeMask) {
1842 GrGLenum glFunc = gr_to_gl_stencil_func(func);
1843 GrGLenum glFailOp = gr_to_gl_stencil_op(failOp);
1844 GrGLenum glPassOp = gr_to_gl_stencil_op(passOp);
1845
1846 if (GR_GL_FRONT_AND_BACK == glFace) {
1847 // we call the combined func just in case separate stencil is not
1848 // supported.
1849 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1850 GR_GL_CALL(gl, StencilMask(writeMask));
1851 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1852 } else {
1853 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1854 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1855 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1856 }
1857}
1858}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859
reed@google.comac10a2d2010-12-22 21:39:39 +00001860void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001861 const GrDrawState& drawState = this->getDrawState();
1862
reed@google.comac10a2d2010-12-22 21:39:39 +00001863 // use stencil for clipping if clipping is enabled and the clip
1864 // has been written into the stencil.
bsalomon@google.com411dad02012-06-05 20:24:20 +00001865 GrClipMaskManager::StencilClipMode clipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001866 if (fClipMaskManager.isClipInStencil() &&
1867 drawState.isClipState()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001868 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001869 // We can't be modifying the clip and respecting it at the same time.
1870 GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1871 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001872 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001873 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001874 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001875 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001876
bsalomon@google.com411dad02012-06-05 20:24:20 +00001877 // The caller may not be using the stencil buffer but we may need to enable
1878 // it in order to respect a stencil clip.
1879 const GrStencilSettings* settings = &drawState.getStencil();
1880 if (settings->isDisabled() &&
1881 GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1882 settings = GetClipStencilSettings();
1883 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001884
bsalomon@google.com411dad02012-06-05 20:24:20 +00001885 // TODO: dynamically attach a stencil buffer
1886 int stencilBits = 0;
1887 GrStencilBuffer* stencilBuffer =
1888 drawState.getRenderTarget()->getStencilBuffer();
1889 if (NULL != stencilBuffer) {
1890 stencilBits = stencilBuffer->bits();
1891 }
1892 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.comd302f142011-03-03 13:54:13 +00001893
bsalomon@google.com411dad02012-06-05 20:24:20 +00001894 bool updateStencilSettings = stencilBits > 0 &&
1895 ((fHWStencilSettings != *settings) ||
1896 (fHWStencilClipMode != clipMode));
1897 if (updateStencilSettings) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001898 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001899 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001901 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001902 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001903 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001904 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1905 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1906 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1907 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1908 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1909 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1910 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1911 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001912 }
1913 #endif
bsalomon@google.comd302f142011-03-03 13:54:13 +00001914
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001915 unsigned int frontRef = settings->frontFuncRef();
1916 unsigned int frontMask = settings->frontFuncMask();
1917 unsigned int frontWriteMask = settings->frontWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001918
bsalomon@google.com411dad02012-06-05 20:24:20 +00001919 GrStencilFunc frontFunc =
1920 fClipMaskManager.adjustStencilParams(settings->frontFunc(),
1921 clipMode,
1922 stencilBits,
1923 &frontRef,
1924 &frontMask,
1925 &frontWriteMask);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001926 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001927 unsigned int backRef = settings->backFuncRef();
1928 unsigned int backMask = settings->backFuncMask();
1929 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930
bsalomon@google.com411dad02012-06-05 20:24:20 +00001931 GrStencilFunc backFunc =
1932 fClipMaskManager.adjustStencilParams(settings->frontFunc(),
1933 clipMode,
1934 stencilBits,
1935 &backRef,
1936 &backMask,
1937 &backWriteMask);
1938 set_gl_stencil(this->glInterface(),
1939 GR_GL_FRONT,
1940 frontFunc,
1941 settings->frontFailOp(),
1942 settings->frontPassOp(),
1943 frontRef,
1944 frontMask,
1945 frontWriteMask);
1946 set_gl_stencil(this->glInterface(),
1947 GR_GL_BACK,
1948 backFunc,
1949 settings->backFailOp(),
1950 settings->backPassOp(),
1951 backRef,
1952 backMask,
1953 backWriteMask);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001954 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001955 set_gl_stencil(this->glInterface(),
1956 GR_GL_FRONT_AND_BACK,
1957 frontFunc,
1958 settings->frontFailOp(),
1959 settings->frontPassOp(),
1960 frontRef,
1961 frontMask,
1962 frontWriteMask);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001963 }
1964 }
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001965 fHWStencilSettings = *settings;
1966 fHWStencilClipMode = clipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001967 }
1968}
1969
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001970void GrGpuGL::flushAAState(bool isLines) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001971 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001972 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001973 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1974 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001975 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001976 bool smoothLines = false;
1977
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001978 if (isLines) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001979 smoothLines = this->willUseHWAALines();
1980 if (smoothLines) {
1981 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1982 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1983 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1984 // must disable msaa to use line smoothing
1985 if (rt->isMultisampled() &&
1986 kNo_TriState != fHWAAState.fMSAAEnabled) {
1987 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1988 fHWAAState.fMSAAEnabled = kNo_TriState;
1989 }
1990 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001991 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001992 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1993 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1994 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1995 }
1996 }
1997 }
1998 if (!smoothLines && rt->isMultisampled()) {
1999 if (this->getDrawState().isHWAntialiasState()) {
2000 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2001 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2002 fHWAAState.fMSAAEnabled = kYes_TriState;
2003 }
2004 } else {
2005 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2006 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2007 fHWAAState.fMSAAEnabled = kNo_TriState;
2008 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002009 }
2010 }
2011 }
2012}
2013
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002014void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002015 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002016 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002017 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002018 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002019 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002020 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002021 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002022 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2023 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2024 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2025 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2026 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2027 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002028 }
2029 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002030 // any optimization to disable blending should
2031 // have already been applied and tweaked the coeffs
2032 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002033 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2034 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002035 if (blendOff) {
2036 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002037 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002038 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002039 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002040 } else {
2041 if (kYes_TriState != fHWBlendState.fEnabled) {
2042 GL_CALL(Enable(GR_GL_BLEND));
2043 fHWBlendState.fEnabled = kYes_TriState;
2044 }
2045 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2046 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002047 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2048 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002049 fHWBlendState.fSrcCoeff = srcCoeff;
2050 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002051 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002052 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002053 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2054 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002055 (!fHWBlendState.fConstColorValid ||
2056 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002057
2058 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002059 GrColorUnpackR(blendConst) / 255.f,
2060 GrColorUnpackG(blendConst) / 255.f,
2061 GrColorUnpackB(blendConst) / 255.f,
2062 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002063 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002064 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002065 fHWBlendState.fConstColor = blendConst;
2066 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002067 }
2068 }
2069 }
2070}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002071namespace {
2072
2073unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002074 switch (filter) {
2075 case GrSamplerState::kBilinear_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002076 return GR_GL_LINEAR;
2077 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002078 return GR_GL_NEAREST;
2079 default:
2080 GrAssert(!"Unknown filter type");
2081 return GR_GL_LINEAR;
2082 }
2083}
2084
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002085// get_swizzle is only called from this .cpp so it is OK to inline it here
2086inline const GrGLenum* get_swizzle(GrPixelConfig config,
2087 const GrSamplerState& sampler,
2088 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002089 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002090 if (glCaps.textureRedSupport()) {
2091 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2092 GR_GL_RED, GR_GL_RED };
2093 return gRedSmear;
2094 } else {
2095 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2096 GR_GL_ALPHA, GR_GL_ALPHA };
2097 return gAlphaSmear;
2098 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002099 } else if (sampler.swapsRAndB()) {
2100 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2101 GR_GL_RED, GR_GL_ALPHA };
2102 return gRedBlueSwap;
2103 } else {
2104 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2105 GR_GL_BLUE, GR_GL_ALPHA };
2106 return gStraight;
2107 }
2108}
2109
2110void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002111 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2112 GR_GL_TEXTURE_SWIZZLE_RGBA,
2113 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002114}
2115}
2116
bsalomon@google.com4c883782012-06-04 19:05:11 +00002117void GrGpuGL::flushBoundTextureAndParams(int stage) {
2118 GrDrawState* drawState = this->drawState();
2119
2120 GrGLTexture* nextTexture =
2121 static_cast<GrGLTexture*>(drawState->getTexture(stage));
2122
2123 // true for now, but maybe not with GrEffect.
2124 GrAssert(NULL != nextTexture);
2125 // if we created a rt/tex and rendered to it without using a
2126 // texture and now we're texturing from the rt it will still be
2127 // the last bound texture, but it needs resolving. So keep this
2128 // out of the "last != next" check.
2129 GrGLRenderTarget* texRT =
2130 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2131 if (NULL != texRT) {
2132 this->onResolveRenderTarget(texRT);
2133 }
2134
2135 if (fHWBoundTextures[stage] != nextTexture) {
2136 this->setTextureUnit(stage);
2137 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002138 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2139 fHWBoundTextures[stage] = nextTexture;
2140 }
2141
2142 const GrSamplerState& sampler = drawState->getSampler(stage);
2143 ResetTimestamp timestamp;
2144 const GrGLTexture::TexParams& oldTexParams =
2145 nextTexture->getCachedTexParams(&timestamp);
2146 bool setAll = timestamp < this->getResetTimestamp();
2147 GrGLTexture::TexParams newTexParams;
2148
2149 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
2150
2151 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
2152 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2153 newTexParams.fWrapT = wraps[sampler.getWrapY()];
2154 memcpy(newTexParams.fSwizzleRGBA,
2155 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
2156 sizeof(newTexParams.fSwizzleRGBA));
2157 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2158 this->setTextureUnit(stage);
2159 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2160 GR_GL_TEXTURE_MAG_FILTER,
2161 newTexParams.fFilter));
2162 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2163 GR_GL_TEXTURE_MIN_FILTER,
2164 newTexParams.fFilter));
2165 }
2166 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2167 this->setTextureUnit(stage);
2168 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2169 GR_GL_TEXTURE_WRAP_S,
2170 newTexParams.fWrapS));
2171 }
2172 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2173 this->setTextureUnit(stage);
2174 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2175 GR_GL_TEXTURE_WRAP_T,
2176 newTexParams.fWrapT));
2177 }
2178 if (this->glCaps().textureSwizzleSupport() &&
2179 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2180 oldTexParams.fSwizzleRGBA,
2181 sizeof(newTexParams.fSwizzleRGBA)))) {
2182 this->setTextureUnit(stage);
2183 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2184 this->glInterface());
2185 }
2186 nextTexture->setCachedTexParams(newTexParams,
2187 this->getResetTimestamp());
2188}
2189
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002190void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002191
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002192 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002193
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002194 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002195 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002196 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002197 fHWDitherEnabled = kYes_TriState;
2198 }
2199 } else {
2200 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002201 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002202 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002203 }
2204 }
2205
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002206 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002207 if (kNo_TriState != fHWWriteToColor) {
2208 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2209 GR_GL_FALSE, GR_GL_FALSE));
2210 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002211 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002212 } else {
2213 if (kYes_TriState != fHWWriteToColor) {
2214 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2215 fHWWriteToColor = kYes_TriState;
2216 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 }
2218
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002219 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002220 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002221 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002222 GL_CALL(Enable(GR_GL_CULL_FACE));
2223 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002224 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002225 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002226 GL_CALL(Enable(GR_GL_CULL_FACE));
2227 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002228 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002229 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002230 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002231 break;
2232 default:
2233 GrCrash("Unknown draw face.");
2234 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002235 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002236 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002237}
2238
2239void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002240 if (fHWGeometryState.fVertexBuffer != buffer) {
2241 fHWGeometryState.fArrayPtrsDirty = true;
2242 fHWGeometryState.fVertexBuffer = buffer;
2243 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002244}
2245
2246void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002247 if (fHWGeometryState.fVertexBuffer == buffer) {
2248 // deleting bound buffer does implied bind to 0
2249 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002250 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 }
2252}
2253
2254void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002255 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002256}
2257
2258void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 if (fHWGeometryState.fIndexBuffer == buffer) {
2260 // deleting bound buffer does implied bind to 0
2261 fHWGeometryState.fIndexBuffer = NULL;
2262 }
2263}
2264
reed@google.comac10a2d2010-12-22 21:39:39 +00002265void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2266 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002267 GrDrawState* drawState = this->drawState();
2268 if (drawState->getRenderTarget() == renderTarget) {
2269 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002270 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002271 if (fHWBoundRenderTarget == renderTarget) {
2272 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002273 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002274}
2275
2276void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002277 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002278 GrDrawState* drawState = this->drawState();
2279 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002280 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002281 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002282 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002283 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002284 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002285 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002286 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002287}
2288
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002289bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2290 bool getSizedInternalFormat,
2291 GrGLenum* internalFormat,
2292 GrGLenum* externalFormat,
2293 GrGLenum* externalType) {
2294 GrGLenum dontCare;
2295 if (NULL == internalFormat) {
2296 internalFormat = &dontCare;
2297 }
2298 if (NULL == externalFormat) {
2299 externalFormat = &dontCare;
2300 }
2301 if (NULL == externalType) {
2302 externalType = &dontCare;
2303 }
2304
reed@google.comac10a2d2010-12-22 21:39:39 +00002305 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002306 case kRGBA_8888_PM_GrPixelConfig:
2307 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002308 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002309 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002310 if (getSizedInternalFormat) {
2311 *internalFormat = GR_GL_RGBA8;
2312 } else {
2313 *internalFormat = GR_GL_RGBA;
2314 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002315 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002316 break;
2317 case kBGRA_8888_PM_GrPixelConfig:
2318 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002319 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002320 return false;
2321 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002322 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002323 if (getSizedInternalFormat) {
2324 *internalFormat = GR_GL_BGRA8;
2325 } else {
2326 *internalFormat = GR_GL_BGRA;
2327 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002328 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002329 if (getSizedInternalFormat) {
2330 *internalFormat = GR_GL_RGBA8;
2331 } else {
2332 *internalFormat = GR_GL_RGBA;
2333 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002334 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002335 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002336 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002337 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002338 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002339 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002340 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002341 if (getSizedInternalFormat) {
2342 if (this->glBinding() == kDesktop_GrGLBinding) {
2343 return false;
2344 } else {
2345 *internalFormat = GR_GL_RGB565;
2346 }
2347 } else {
2348 *internalFormat = GR_GL_RGB;
2349 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002350 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002351 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002352 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002353 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002354 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002355 if (getSizedInternalFormat) {
2356 *internalFormat = GR_GL_RGBA4;
2357 } else {
2358 *internalFormat = GR_GL_RGBA;
2359 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002360 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002361 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002362 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002363 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002364 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002365 // glCompressedTexImage doesn't take external params
2366 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002367 // no sized/unsized internal format distinction here
2368 *internalFormat = GR_GL_PALETTE8_RGBA8;
2369 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002370 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002371 } else {
2372 return false;
2373 }
2374 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002375 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002376 if (this->glCaps().textureRedSupport()) {
2377 *internalFormat = GR_GL_RED;
2378 *externalFormat = GR_GL_RED;
2379 if (getSizedInternalFormat) {
2380 *internalFormat = GR_GL_R8;
2381 } else {
2382 *internalFormat = GR_GL_RED;
2383 }
2384 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002385 } else {
2386 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002387 *externalFormat = GR_GL_ALPHA;
2388 if (getSizedInternalFormat) {
2389 *internalFormat = GR_GL_ALPHA8;
2390 } else {
2391 *internalFormat = GR_GL_ALPHA;
2392 }
2393 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002394 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002395 break;
2396 default:
2397 return false;
2398 }
2399 return true;
2400}
2401
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002402void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002403 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002404 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002405 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002406 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002407 }
2408}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002409
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002410void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002411 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002412 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002413 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002414 }
2415}
2416
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002417void GrGpuGL::setBuffers(bool indexed,
2418 int* extraVertexOffset,
2419 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002420
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002421 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002422
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002423 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2424
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002425 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002426 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002427 case kBuffer_GeometrySrcType:
2428 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002429 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002430 break;
2431 case kArray_GeometrySrcType:
2432 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002433 this->finalizeReservedVertices();
2434 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2435 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002436 break;
2437 default:
2438 vbuf = NULL; // suppress warning
2439 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002440 }
2441
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002442 GrAssert(NULL != vbuf);
2443 GrAssert(!vbuf->isLocked());
2444 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002445 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002446 fHWGeometryState.fArrayPtrsDirty = true;
2447 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002448 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002449
2450 if (indexed) {
2451 GrAssert(NULL != extraIndexOffset);
2452
2453 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002454 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002455 case kBuffer_GeometrySrcType:
2456 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002457 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002458 break;
2459 case kArray_GeometrySrcType:
2460 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002461 this->finalizeReservedIndices();
2462 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2463 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002464 break;
2465 default:
2466 ibuf = NULL; // suppress warning
2467 GrCrash("Unknown geometry src type!");
2468 }
2469
2470 GrAssert(NULL != ibuf);
2471 GrAssert(!ibuf->isLocked());
2472 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002473 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002474 fHWGeometryState.fIndexBuffer = ibuf;
2475 }
2476 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002477}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002478