blob: dd65394a33cea722412a5373cbf0dcdff65b540d [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
511 // TODO: I believe this should actually go in GrGpu::onResetContext
512 // rather than here
513 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000514
515 fHWGeometryState.fIndexBuffer = NULL;
516 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000517
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000518 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000519
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000520 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000521
522 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000523 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000524 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
525 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000526 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000527 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
528 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000529 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000530 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
531 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000532 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000533 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
534 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000535
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000536 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000537
538 // Third party GL code may have left vertex attributes enabled. Some GL
539 // implementations (osmesa) may read vetex attributes that are not required
540 // by the current shader. Therefore, we have to ensure that only the
541 // attributes we require for the current draw are enabled or we may cause an
542 // invalid read.
543
544 // Disable all vertex layout bits so that next flush will assume all
545 // optional vertex attributes are disabled.
546 fHWGeometryState.fVertexLayout = 0;
547
548 // We always use the this attribute and assume it is always enabled.
549 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
550 GL_CALL(EnableVertexAttribArray(posAttrIdx));
551 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000552 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000553 if (va != posAttrIdx) {
554 GL_CALL(DisableVertexAttribArray(va));
555 }
556 }
557
558 fHWProgramID = 0;
559 fHWConstAttribColor = GrColor_ILLEGAL;
560 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000561}
562
bsalomon@google.come269f212011-11-07 13:29:52 +0000563GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000564 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000565 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000566 return NULL;
567 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000568
robertphillips@google.com32716282012-06-04 12:48:45 +0000569 // next line relies on PlatformTextureDesc's flags matching GrTexture's
570 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000571 glTexDesc.fWidth = desc.fWidth;
572 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000573 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000574 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000575 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
576 glTexDesc.fOwnsID = false;
577 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
578
579 GrGLTexture* texture = NULL;
580 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
581 GrGLRenderTarget::Desc glRTDesc;
582 glRTDesc.fRTFBOID = 0;
583 glRTDesc.fTexFBOID = 0;
584 glRTDesc.fMSColorRenderbufferID = 0;
585 glRTDesc.fOwnIDs = true;
586 glRTDesc.fConfig = desc.fConfig;
587 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000588 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
589 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000590 glTexDesc.fTextureID,
591 &glRTDesc)) {
592 return NULL;
593 }
594 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
595 } else {
596 texture = new GrGLTexture(this, glTexDesc);
597 }
598 if (NULL == texture) {
599 return NULL;
600 }
601
602 this->setSpareTextureUnit();
603 return texture;
604}
605
606GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
607 GrGLRenderTarget::Desc glDesc;
608 glDesc.fConfig = desc.fConfig;
609 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
610 glDesc.fMSColorRenderbufferID = 0;
611 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
612 glDesc.fSampleCnt = desc.fSampleCnt;
613 glDesc.fOwnIDs = false;
614 GrGLIRect viewport;
615 viewport.fLeft = 0;
616 viewport.fBottom = 0;
617 viewport.fWidth = desc.fWidth;
618 viewport.fHeight = desc.fHeight;
619
620 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
621 if (desc.fStencilBits) {
622 GrGLStencilBuffer::Format format;
623 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
624 format.fPacked = false;
625 format.fStencilBits = desc.fStencilBits;
626 format.fTotalBits = desc.fStencilBits;
627 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
628 0,
629 desc.fWidth,
630 desc.fHeight,
631 desc.fSampleCnt,
632 format);
633 tgt->setStencilBuffer(sb);
634 sb->unref();
635 }
636 return tgt;
637}
638
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000639////////////////////////////////////////////////////////////////////////////////
640
bsalomon@google.com6f379512011-11-16 20:36:03 +0000641void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
642 int left, int top, int width, int height,
643 GrPixelConfig config, const void* buffer,
644 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000645 if (NULL == buffer) {
646 return;
647 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000648 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
649
bsalomon@google.com6f379512011-11-16 20:36:03 +0000650 this->setSpareTextureUnit();
651 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
652 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000653 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000654 desc.fWidth = glTex->width();
655 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000656 desc.fConfig = glTex->config();
657 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000658 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000659 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000660
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000661 this->uploadTexData(desc, false,
662 left, top, width, height,
663 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000664}
665
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000666namespace {
667bool adjust_pixel_ops_params(int surfaceWidth,
668 int surfaceHeight,
669 size_t bpp,
670 int* left, int* top, int* width, int* height,
671 const void** data,
672 size_t* rowBytes) {
673 if (!*rowBytes) {
674 *rowBytes = *width * bpp;
675 }
676
677 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
678 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
679
680 if (!subRect.intersect(bounds)) {
681 return false;
682 }
683 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
684 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
685
686 *left = subRect.fLeft;
687 *top = subRect.fTop;
688 *width = subRect.width();
689 *height = subRect.height();
690 return true;
691}
692}
693
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000694bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
695 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000696 int left, int top, int width, int height,
697 GrPixelConfig dataConfig,
698 const void* data,
699 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000700 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000701
702 size_t bpp = GrBytesPerPixel(dataConfig);
703 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
704 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000705 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000706 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000707 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000708
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000709 // in case we need a temporary, trimmed copy of the src pixels
710 SkAutoSMalloc<128 * 128> tempStorage;
711
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000712 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000713 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000714 if (useTexStorage) {
715 if (kDesktop_GrGLBinding == this->glBinding()) {
716 // 565 is not a sized internal format on desktop GL. So on desktop
717 // with 565 we always use an unsized internal format to let the
718 // system pick the best sized format to convert the 565 data to.
719 // Since glTexStorage only allows sized internal formats we will
720 // instead fallback to glTexImage2D.
721 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
722 } else {
723 // ES doesn't allow paletted textures to be used with tex storage
724 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
725 }
726 }
727
728 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000729 GrGLenum externalFormat;
730 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000731 // glTexStorage requires sized internal formats on both desktop and ES. ES
732 // glTexImage requires an unsized format.
733 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
734 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000735 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000736 }
737
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000738 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000739 // paletted textures cannot be updated
740 return false;
741 }
742
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000743 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000744 * check whether to allocate a temporary buffer for flipping y or
745 * because our srcData has extra bytes past each row. If so, we need
746 * to trim those off here, since GL ES may not let us specify
747 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000748 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000749 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000750 bool swFlipY = false;
751 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000752 if (NULL != data) {
753 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000754 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000755 glFlipY = true;
756 } else {
757 swFlipY = true;
758 }
759 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000760 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000761 // can't use this for flipping, only non-neg values allowed. :(
762 if (rowBytes != trimRowBytes) {
763 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
764 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
765 restoreGLRowLength = true;
766 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000767 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000768 if (trimRowBytes != rowBytes || swFlipY) {
769 // copy data into our new storage, skipping the trailing bytes
770 size_t trimSize = height * trimRowBytes;
771 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000772 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000773 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000774 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000775 char* dst = (char*)tempStorage.reset(trimSize);
776 for (int y = 0; y < height; y++) {
777 memcpy(dst, src, trimRowBytes);
778 if (swFlipY) {
779 src -= rowBytes;
780 } else {
781 src += rowBytes;
782 }
783 dst += trimRowBytes;
784 }
785 // now point data to our copied version
786 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000787 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000788 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000789 if (glFlipY) {
790 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
791 }
792 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000793 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000794 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000795 if (isNewTexture &&
796 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000797 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000798 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000799 if (useTexStorage) {
800 // We never resize or change formats of textures. We don't use
801 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000802 GL_ALLOC_CALL(this->glInterface(),
803 TexStorage2D(GR_GL_TEXTURE_2D,
804 1, // levels
805 internalFormat,
806 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000807 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000808 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
809 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
810 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000811 GL_ALLOC_CALL(this->glInterface(),
812 CompressedTexImage2D(GR_GL_TEXTURE_2D,
813 0, // level
814 internalFormat,
815 desc.fWidth, desc.fHeight,
816 0, // border
817 imageSize,
818 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000819 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000820 GL_ALLOC_CALL(this->glInterface(),
821 TexImage2D(GR_GL_TEXTURE_2D,
822 0, // level
823 internalFormat,
824 desc.fWidth, desc.fHeight,
825 0, // border
826 externalFormat, externalType,
827 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000828 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000829 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000830 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000831 if (error != GR_GL_NO_ERROR) {
832 succeeded = false;
833 } else {
834 // if we have data and we used TexStorage to create the texture, we
835 // now upload with TexSubImage.
836 if (NULL != data && useTexStorage) {
837 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
838 0, // level
839 left, top,
840 width, height,
841 externalFormat, externalType,
842 data));
843 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000844 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000845 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000846 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000847 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000848 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000849 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
850 0, // level
851 left, top,
852 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000853 externalFormat, externalType, data));
854 }
855
856 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000857 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000858 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000859 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000860 if (glFlipY) {
861 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
862 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000863 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000864}
865
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000866namespace {
867bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
868 int sampleCount,
869 GrGLenum format,
870 int width, int height) {
871 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
872 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
873 bool created = false;
874 if (GrGLCaps::kNVDesktop_CoverageAAType ==
875 ctxInfo.caps().coverageAAType()) {
876 const GrGLCaps::MSAACoverageMode& mode =
877 ctxInfo.caps().getMSAACoverageMode(sampleCount);
878 GL_ALLOC_CALL(ctxInfo.interface(),
879 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
880 mode.fCoverageSampleCnt,
881 mode.fColorSampleCnt,
882 format,
883 width, height));
884 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
885 }
886 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000887 // glRBMS will fail if requested samples is > max samples.
888 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000889 GL_ALLOC_CALL(ctxInfo.interface(),
890 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
891 sampleCount,
892 format,
893 width, height));
894 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
895 }
896 return created;
897}
898}
899
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000900bool GrGpuGL::createRenderTargetObjects(int width, int height,
901 GrGLuint texID,
902 GrGLRenderTarget::Desc* desc) {
903 desc->fMSColorRenderbufferID = 0;
904 desc->fRTFBOID = 0;
905 desc->fTexFBOID = 0;
906 desc->fOwnIDs = true;
907
908 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909
bsalomon@google.comab15d612011-08-09 12:57:56 +0000910 GrGLenum msColorFormat = 0; // suppress warning
911
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000912 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913 if (!desc->fTexFBOID) {
914 goto FAILED;
915 }
916
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917
918 // If we are using multisampling we will create two FBOS. We render
919 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000920 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000921 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000922 goto FAILED;
923 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000924 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
925 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000926 if (!desc->fRTFBOID ||
927 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000928 !this->configToGLFormats(desc->fConfig,
929 // GLES requires sized internal formats
930 kES2_GrGLBinding == this->glBinding(),
931 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932 goto FAILED;
933 }
934 } else {
935 desc->fRTFBOID = desc->fTexFBOID;
936 }
937
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000938 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000939 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940 if (desc->fRTFBOID != desc->fTexFBOID) {
941 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000944 if (!renderbuffer_storage_msaa(fGLContextInfo,
945 desc->fSampleCnt,
946 msColorFormat,
947 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948 goto FAILED;
949 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000950 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
951 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000952 GR_GL_COLOR_ATTACHMENT0,
953 GR_GL_RENDERBUFFER,
954 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000955 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000956 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
957 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
958 goto FAILED;
959 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000960 fGLContextInfo.caps().markConfigAsValidColorAttachment(
961 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000962 }
963 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000964 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000965
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000966 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
967 GR_GL_COLOR_ATTACHMENT0,
968 GR_GL_TEXTURE_2D,
969 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000970 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000971 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
972 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
973 goto FAILED;
974 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000975 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000976 }
977
978 return true;
979
980FAILED:
981 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000982 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000983 }
984 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000985 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000986 }
987 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000988 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000989 }
990 return false;
991}
992
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000993// good to set a break-point here to know when createTexture fails
994static GrTexture* return_null_texture() {
995// GrAssert(!"null texture");
996 return NULL;
997}
998
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000999#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001000static size_t as_size_t(int x) {
1001 return x;
1002}
1003#endif
1004
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001005GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001006 const void* srcData,
1007 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001008
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001009 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001010 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001011
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001012 // Attempt to catch un- or wrongly initialized sample counts;
1013 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1014
robertphillips@google.com32716282012-06-04 12:48:45 +00001015 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +00001016 glTexDesc.fWidth = desc.fWidth;
1017 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001018 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +00001019 glTexDesc.fSampleCnt = desc.fSampleCnt;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001020 glTexDesc.fClientCacheID = desc.fClientCacheID;
robertphillips@google.com32716282012-06-04 12:48:45 +00001021
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001022 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001023
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001024 glRTDesc.fMSColorRenderbufferID = 0;
1025 glRTDesc.fRTFBOID = 0;
1026 glRTDesc.fTexFBOID = 0;
1027 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001028 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001029
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001030 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001031
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001032 const Caps& caps = this->getCaps();
1033
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001034 // We keep GrRenderTargets in GL's normal orientation so that they
1035 // can be drawn to by the outside world without the client having
1036 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001037 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001038 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001039
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001040 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001041 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001042 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001043 //GrPrintf("MSAA RT requested but not supported on this platform.");
1044 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001045 }
1046
reed@google.comac10a2d2010-12-22 21:39:39 +00001047 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001048 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1049 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001050 return return_null_texture();
1051 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 }
1053
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001054 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001055 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001056 // provides a hint about how this texture will be used
1057 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1058 GR_GL_TEXTURE_USAGE,
1059 GR_GL_FRAMEBUFFER_ATTACHMENT));
1060 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001061 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001062 return return_null_texture();
1063 }
1064
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001065 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001066 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001067
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001068 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1069 // drivers have a bug where an FBO won't be complete if it includes a
1070 // texture that is not mipmap complete (considering the filter in use).
1071 GrGLTexture::TexParams initialTexParams;
1072 // we only set a subset here so invalidate first
1073 initialTexParams.invalidate();
1074 initialTexParams.fFilter = GR_GL_NEAREST;
1075 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1076 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001077 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1078 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001079 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001080 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1081 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001082 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001083 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1084 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001085 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001086 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1087 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001088 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001089 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1090 glTexDesc.fWidth, glTexDesc.fHeight,
1091 desc.fConfig, srcData, rowBytes)) {
1092 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1093 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001094 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001095
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001096 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001098 // unbind the texture from the texture unit before binding it to the frame buffer
1099 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1100
bsalomon@google.com99621082011-11-15 16:47:16 +00001101 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1102 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103 glTexDesc.fTextureID,
1104 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001105 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001106 return return_null_texture();
1107 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001108 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001110 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001111 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001112 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001113#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001114 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1115 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001116#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117 return tex;
1118}
1119
1120namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001121
1122const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1123
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001124void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1125 GrGLuint rb,
1126 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 // we shouldn't ever know one size and not the other
1128 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1129 (kUnknownBitCount == format->fTotalBits));
1130 if (kUnknownBitCount == format->fStencilBits) {
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_STENCIL_SIZE,
1133 (GrGLint*)&format->fStencilBits);
1134 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1137 (GrGLint*)&format->fTotalBits);
1138 format->fTotalBits += format->fStencilBits;
1139 } else {
1140 format->fTotalBits = format->fStencilBits;
1141 }
1142 }
1143}
1144}
1145
1146bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1147 int width, int height) {
1148
1149 // All internally created RTs are also textures. We don't create
1150 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1151 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001152 GrAssert(width >= rt->width());
1153 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154
1155 int samples = rt->numSamples();
1156 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 if (!sbID) {
1159 return false;
1160 }
1161
1162 GrGLStencilBuffer* sb = NULL;
1163
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001164 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001166 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001167 // we start with the last stencil format that succeeded in hopes
1168 // that we won't go through this loop more than once after the
1169 // first (painful) stencil creation.
1170 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001171 const GrGLCaps::StencilFormat& sFmt =
1172 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001173 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001174 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001176 bool created;
1177 if (samples > 0) {
1178 created = renderbuffer_storage_msaa(fGLContextInfo,
1179 samples,
1180 sFmt.fInternalFormat,
1181 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001183 GL_ALLOC_CALL(this->glInterface(),
1184 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001185 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001186 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001187 created =
1188 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001190 if (created) {
1191 // After sized formats we attempt an unsized format and take
1192 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001193 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001195 sb = new GrGLStencilBuffer(this, sbID, width, height,
1196 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1198 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001199 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 return true;
1202 }
1203 sb->abandon(); // otherwise we lose sbID
1204 sb->unref();
1205 }
1206 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001207 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001208 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209}
1210
1211bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1212 GrRenderTarget* rt) {
1213 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1214
1215 GrGLuint fbo = glrt->renderFBOID();
1216
1217 if (NULL == sb) {
1218 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 GR_GL_STENCIL_ATTACHMENT,
1221 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001222 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 GR_GL_DEPTH_ATTACHMENT,
1224 GR_GL_RENDERBUFFER, 0));
1225#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001226 GrGLenum status;
1227 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1229#endif
1230 }
1231 return true;
1232 } else {
1233 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1234 GrGLuint rb = glsb->renderbufferID();
1235
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001236 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1238 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 GR_GL_STENCIL_ATTACHMENT,
1240 GR_GL_RENDERBUFFER, rb));
1241 if (glsb->format().fPacked) {
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, rb));
1245 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001246 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247 GR_GL_DEPTH_ATTACHMENT,
1248 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001251 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001252 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001253 glsb->format())) {
1254 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1255 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001256 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001257 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001258 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001259 if (glsb->format().fPacked) {
1260 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1261 GR_GL_DEPTH_ATTACHMENT,
1262 GR_GL_RENDERBUFFER, 0));
1263 }
1264 return false;
1265 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001266 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001267 rt->config(),
1268 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001269 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001270 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001271 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001273}
1274
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001275////////////////////////////////////////////////////////////////////////////////
1276
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001277GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001278 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001279 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001281 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001282 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001283 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001285 GL_ALLOC_CALL(this->glInterface(),
1286 BufferData(GR_GL_ARRAY_BUFFER,
1287 size,
1288 NULL, // data ptr
1289 dynamic ? GR_GL_DYNAMIC_DRAW :
1290 GR_GL_STATIC_DRAW));
1291 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001292 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 // deleting bound buffer does implicit bind to 0
1294 fHWGeometryState.fVertexBuffer = NULL;
1295 return NULL;
1296 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001297 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 size, dynamic);
1299 fHWGeometryState.fVertexBuffer = vertexBuffer;
1300 return vertexBuffer;
1301 }
1302 return NULL;
1303}
1304
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001305GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001306 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001307 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001308 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001309 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001310 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001312 GL_ALLOC_CALL(this->glInterface(),
1313 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1314 size,
1315 NULL, // data ptr
1316 dynamic ? GR_GL_DYNAMIC_DRAW :
1317 GR_GL_STATIC_DRAW));
1318 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001319 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 // deleting bound buffer does implicit bind to 0
1321 fHWGeometryState.fIndexBuffer = NULL;
1322 return NULL;
1323 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001324 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 size, dynamic);
1326 fHWGeometryState.fIndexBuffer = indexBuffer;
1327 return indexBuffer;
1328 }
1329 return NULL;
1330}
1331
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001332GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
1333 GrPath* path = NULL;
1334 if (fCaps.fPathStencilingSupport) {
1335 path = new GrGLPath(this, inPath);
1336 }
1337 return path;
1338}
1339
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001340void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001341 const GrDrawState& drawState = this->getDrawState();
1342 const GrGLRenderTarget* rt =
1343 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1344
1345 GrAssert(NULL != rt);
1346 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001347
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001348 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001349 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1350 rect.width(), rect.height());
1351 if (scissor.contains(vp)) {
1352 disableScissor();
1353 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 }
1355
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001356 if (fHWBounds.fScissorRect != scissor) {
1357 scissor.pushToGLScissor(this->glInterface());
1358 fHWBounds.fScissorRect = scissor;
1359 }
1360 if (!fHWBounds.fScissorEnabled) {
1361 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1362 fHWBounds.fScissorEnabled = true;
1363 }
1364}
1365
1366void GrGpuGL::disableScissor() {
1367 if (fHWBounds.fScissorEnabled) {
1368 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1369 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 }
1371}
1372
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001373void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001374 const GrDrawState& drawState = this->getDrawState();
1375 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001376 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001377 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001378
bsalomon@google.com74b98712011-11-11 19:46:16 +00001379 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001380 if (NULL != rect) {
1381 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001382 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001383 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001384 if (clippedRect.intersect(rtRect)) {
1385 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001386 } else {
1387 return;
1388 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001389 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001390 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001391 if (NULL != rect)
1392 this->enableScissoring(*rect);
1393 else
1394 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001395
1396 GrGLfloat r, g, b, a;
1397 static const GrGLfloat scale255 = 1.f / 255.f;
1398 a = GrColorUnpackA(color) * scale255;
1399 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001400 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001401 scaleRGB *= a;
1402 }
1403 r = GrColorUnpackR(color) * scaleRGB;
1404 g = GrColorUnpackG(color) * scaleRGB;
1405 b = GrColorUnpackB(color) * scaleRGB;
1406
1407 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001408 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001409 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001410 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001411}
1412
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001413void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001414 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001415 return;
1416 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001417
1418 this->flushRenderTarget(&GrIRect::EmptyIRect());
1419
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001420 this->disableScissor();
1421
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001422 GL_CALL(StencilMask(0xffffffff));
1423 GL_CALL(ClearStencil(0));
1424 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001425 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001426}
1427
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001428void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001429 const GrDrawState& drawState = this->getDrawState();
1430 const GrRenderTarget* rt = drawState.getRenderTarget();
1431 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001432
1433 // this should only be called internally when we know we have a
1434 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001435 GrAssert(NULL != rt->getStencilBuffer());
1436 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001437#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001438 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001439 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001440#else
1441 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001442 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001443 // turned into draws. Our contract on GrDrawTarget says that
1444 // changing the clip between stencil passes may or may not
1445 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001446 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001447#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001448 GrGLint value;
1449 if (insideClip) {
1450 value = (1 << (stencilBitCount - 1));
1451 } else {
1452 value = 0;
1453 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001454 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001455 this->enableScissoring(rect);
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001456 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001457 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001458 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001459 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001460}
1461
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001462void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001463 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001464}
1465
bsalomon@google.comc4364992011-11-07 15:54:49 +00001466bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1467 int left, int top,
1468 int width, int height,
1469 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001470 size_t rowBytes) const {
1471 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001472 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001473 return false;
1474 }
1475
1476 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001477 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001478 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001479 return true;
1480 }
1481 // If we have to do memcpys to handle rowBytes then y-flip is free
1482 // Note the rowBytes might be tight to the passed in data, but if data
1483 // gets clipped in x to the target the rowBytes will no longer be tight.
1484 if (left >= 0 && (left + width) < renderTarget->width()) {
1485 return 0 == rowBytes ||
1486 GrBytesPerPixel(config) * width == rowBytes;
1487 } else {
1488 return false;
1489 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001490}
1491
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001492bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001493 int left, int top,
1494 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001495 GrPixelConfig config,
1496 void* buffer,
1497 size_t rowBytes,
1498 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001499 GrGLenum format;
1500 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001501 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001503 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001504 size_t bpp = GrBytesPerPixel(config);
1505 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1506 &left, &top, &width, &height,
1507 const_cast<const void**>(&buffer),
1508 &rowBytes)) {
1509 return false;
1510 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001511
bsalomon@google.comc6980972011-11-02 19:57:21 +00001512 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001513 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001514 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001515 switch (tgt->getResolveType()) {
1516 case GrGLRenderTarget::kCantResolve_ResolveType:
1517 return false;
1518 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001519 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001520 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001521 break;
1522 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001523 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001524 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001525 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1526 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001527 break;
1528 default:
1529 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001530 }
1531
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001532 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001533
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001534 // the read rect is viewport-relative
1535 GrGLIRect readRect;
1536 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001538 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 if (0 == rowBytes) {
1540 rowBytes = tightRowBytes;
1541 }
1542 size_t readDstRowBytes = tightRowBytes;
1543 void* readDst = buffer;
1544
1545 // determine if GL can read using the passed rowBytes or if we need
1546 // a scratch buffer.
1547 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1548 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001549 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001550 GrAssert(!(rowBytes % sizeof(GrColor)));
1551 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1552 readDstRowBytes = rowBytes;
1553 } else {
1554 scratch.reset(tightRowBytes * height);
1555 readDst = scratch.get();
1556 }
1557 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001558 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001559 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1560 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001561 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1562 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001563 format, type, readDst));
1564 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001565 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001566 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1567 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001568 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001569 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1570 invertY = true;
1571 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001572
1573 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001574 // API presents top-to-bottom. We must preserve the padding contents. Note
1575 // that the above readPixels did not overwrite the padding.
1576 if (readDst == buffer) {
1577 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001578 if (!invertY) {
1579 scratch.reset(tightRowBytes);
1580 void* tmpRow = scratch.get();
1581 // flip y in-place by rows
1582 const int halfY = height >> 1;
1583 char* top = reinterpret_cast<char*>(buffer);
1584 char* bottom = top + (height - 1) * rowBytes;
1585 for (int y = 0; y < halfY; y++) {
1586 memcpy(tmpRow, top, tightRowBytes);
1587 memcpy(top, bottom, tightRowBytes);
1588 memcpy(bottom, tmpRow, tightRowBytes);
1589 top += rowBytes;
1590 bottom -= rowBytes;
1591 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001592 }
1593 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001594 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001595 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001596 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001597 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001598 char* dst = reinterpret_cast<char*>(buffer);
1599 if (!invertY) {
1600 dst += (height-1) * rowBytes;
1601 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001602 for (int y = 0; y < height; y++) {
1603 memcpy(dst, src, tightRowBytes);
1604 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001605 if (invertY) {
1606 dst += rowBytes;
1607 } else {
1608 dst -= rowBytes;
1609 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 }
1611 }
1612 return true;
1613}
1614
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001615void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001616
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001617 GrGLRenderTarget* rt =
1618 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1619 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001620
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001621 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001622 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001624 GrGLenum status;
1625 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001626 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001627 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 }
1629 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001630 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001631 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001632 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001633 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001634 fHWBounds.fViewportRect = vp;
1635 }
1636 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001637 if (NULL == bound || !bound->isEmpty()) {
1638 rt->flagAsNeedingResolve(bound);
1639 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001640}
1641
twiz@google.com0f31ca72011-03-18 17:38:11 +00001642GrGLenum gPrimitiveType2GLMode[] = {
1643 GR_GL_TRIANGLES,
1644 GR_GL_TRIANGLE_STRIP,
1645 GR_GL_TRIANGLE_FAN,
1646 GR_GL_POINTS,
1647 GR_GL_LINES,
1648 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001649};
1650
bsalomon@google.comd302f142011-03-03 13:54:13 +00001651#define SWAP_PER_DRAW 0
1652
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001653#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001654 #if GR_MAC_BUILD
1655 #include <AGL/agl.h>
1656 #elif GR_WIN32_BUILD
1657 void SwapBuf() {
1658 DWORD procID = GetCurrentProcessId();
1659 HWND hwnd = GetTopWindow(GetDesktopWindow());
1660 while(hwnd) {
1661 DWORD wndProcID = 0;
1662 GetWindowThreadProcessId(hwnd, &wndProcID);
1663 if(wndProcID == procID) {
1664 SwapBuffers(GetDC(hwnd));
1665 }
1666 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1667 }
1668 }
1669 #endif
1670#endif
1671
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001672void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1673 uint32_t startVertex,
1674 uint32_t startIndex,
1675 uint32_t vertexCount,
1676 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001677 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1678
twiz@google.com0f31ca72011-03-18 17:38:11 +00001679 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001680
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001681 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1682 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1683
1684 // our setupGeometry better have adjusted this to zero since
1685 // DrawElements always draws from the begining of the arrays for idx 0.
1686 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001687
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001688 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1689 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001690#if SWAP_PER_DRAW
1691 glFlush();
1692 #if GR_MAC_BUILD
1693 aglSwapBuffers(aglGetCurrentContext());
1694 int set_a_break_pt_here = 9;
1695 aglSwapBuffers(aglGetCurrentContext());
1696 #elif GR_WIN32_BUILD
1697 SwapBuf();
1698 int set_a_break_pt_here = 9;
1699 SwapBuf();
1700 #endif
1701#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001702}
1703
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001704void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1705 uint32_t startVertex,
1706 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001707 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1708
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001709 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1710
1711 // our setupGeometry better have adjusted this to zero.
1712 // DrawElements doesn't take an offset so we always adjus the startVertex.
1713 GrAssert(0 == startVertex);
1714
1715 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1716 // account for startVertex in the DrawElements case. So we always
1717 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001718 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001719#if SWAP_PER_DRAW
1720 glFlush();
1721 #if GR_MAC_BUILD
1722 aglSwapBuffers(aglGetCurrentContext());
1723 int set_a_break_pt_here = 9;
1724 aglSwapBuffers(aglGetCurrentContext());
1725 #elif GR_WIN32_BUILD
1726 SwapBuf();
1727 int set_a_break_pt_here = 9;
1728 SwapBuf();
1729 #endif
1730#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001731}
1732
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001733void GrGpuGL::onGpuStencilPath(const GrPath&, GrPathFill) {
1734 GrCrash("Not implemented yet. Should not get here.");
1735}
1736
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001737void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1738
1739 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001740
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001741 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001742 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001743 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001744 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1745 rt->renderFBOID()));
1746 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1747 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001748 // make sure we go through flushRenderTarget() since we've modified
1749 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001750 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001751 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001752 const GrIRect dirtyRect = rt->getResolveRect();
1753 GrGLIRect r;
1754 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1755 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001756
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001757 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001758 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001759#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001760 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1761 GL_CALL(Scissor(r.fLeft, r.fBottom,
1762 r.fWidth, r.fHeight));
1763 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001764 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001766#else
1767 this->enableScissoring(dirtyRect);
1768 GL_CALL(ResolveMultisampleFramebuffer());
1769#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001771 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001772 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001773 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1774 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001775 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001776 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001777 int right = r.fLeft + r.fWidth;
1778 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001779 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1780 r.fLeft, r.fBottom, right, top,
1781 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001782 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001783 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001784 }
1785}
1786
bsalomon@google.com411dad02012-06-05 20:24:20 +00001787namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001788
bsalomon@google.com411dad02012-06-05 20:24:20 +00001789GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1790 static const GrGLenum gTable[] = {
1791 GR_GL_ALWAYS, // kAlways_StencilFunc
1792 GR_GL_NEVER, // kNever_StencilFunc
1793 GR_GL_GREATER, // kGreater_StencilFunc
1794 GR_GL_GEQUAL, // kGEqual_StencilFunc
1795 GR_GL_LESS, // kLess_StencilFunc
1796 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1797 GR_GL_EQUAL, // kEqual_StencilFunc,
1798 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1799 };
1800 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1801 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1802 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1803 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1804 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1805 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1806 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1807 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1808 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1809 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1810
1811 return gTable[basicFunc];
1812}
1813
1814GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1815 static const GrGLenum gTable[] = {
1816 GR_GL_KEEP, // kKeep_StencilOp
1817 GR_GL_REPLACE, // kReplace_StencilOp
1818 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1819 GR_GL_INCR, // kIncClamp_StencilOp
1820 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1821 GR_GL_DECR, // kDecClamp_StencilOp
1822 GR_GL_ZERO, // kZero_StencilOp
1823 GR_GL_INVERT, // kInvert_StencilOp
1824 };
1825 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1826 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1827 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1828 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1829 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1830 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1831 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1832 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1833 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1834 GrAssert((unsigned) op < kStencilOpCount);
1835 return gTable[op];
1836}
1837
1838void set_gl_stencil(const GrGLInterface* gl,
1839 GrGLenum glFace,
1840 GrStencilFunc func,
1841 GrStencilOp failOp,
1842 GrStencilOp passOp,
1843 unsigned int ref,
1844 unsigned int mask,
1845 unsigned int writeMask) {
1846 GrGLenum glFunc = gr_to_gl_stencil_func(func);
1847 GrGLenum glFailOp = gr_to_gl_stencil_op(failOp);
1848 GrGLenum glPassOp = gr_to_gl_stencil_op(passOp);
1849
1850 if (GR_GL_FRONT_AND_BACK == glFace) {
1851 // we call the combined func just in case separate stencil is not
1852 // supported.
1853 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1854 GR_GL_CALL(gl, StencilMask(writeMask));
1855 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1856 } else {
1857 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1858 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1859 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1860 }
1861}
1862}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863
reed@google.comac10a2d2010-12-22 21:39:39 +00001864void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001865 const GrDrawState& drawState = this->getDrawState();
1866
reed@google.comac10a2d2010-12-22 21:39:39 +00001867 // use stencil for clipping if clipping is enabled and the clip
1868 // has been written into the stencil.
bsalomon@google.com411dad02012-06-05 20:24:20 +00001869 GrClipMaskManager::StencilClipMode clipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001870 if (fClipMaskManager.isClipInStencil() &&
1871 drawState.isClipState()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001872 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001873 // We can't be modifying the clip and respecting it at the same time.
1874 GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1875 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001876 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001877 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001878 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001879 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001880
bsalomon@google.com411dad02012-06-05 20:24:20 +00001881 // The caller may not be using the stencil buffer but we may need to enable
1882 // it in order to respect a stencil clip.
1883 const GrStencilSettings* settings = &drawState.getStencil();
1884 if (settings->isDisabled() &&
1885 GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1886 settings = GetClipStencilSettings();
1887 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001888
bsalomon@google.com411dad02012-06-05 20:24:20 +00001889 // TODO: dynamically attach a stencil buffer
1890 int stencilBits = 0;
1891 GrStencilBuffer* stencilBuffer =
1892 drawState.getRenderTarget()->getStencilBuffer();
1893 if (NULL != stencilBuffer) {
1894 stencilBits = stencilBuffer->bits();
1895 }
1896 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897
bsalomon@google.com411dad02012-06-05 20:24:20 +00001898 bool updateStencilSettings = stencilBits > 0 &&
1899 ((fHWStencilSettings != *settings) ||
1900 (fHWStencilClipMode != clipMode));
1901 if (updateStencilSettings) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001902 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001903 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001904 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001905 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001906 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001907 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001908 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1909 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1910 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1911 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1912 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1913 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1914 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1915 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001916 }
1917 #endif
bsalomon@google.comd302f142011-03-03 13:54:13 +00001918
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001919 unsigned int frontRef = settings->frontFuncRef();
1920 unsigned int frontMask = settings->frontFuncMask();
1921 unsigned int frontWriteMask = settings->frontWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922
bsalomon@google.com411dad02012-06-05 20:24:20 +00001923 GrStencilFunc frontFunc =
1924 fClipMaskManager.adjustStencilParams(settings->frontFunc(),
1925 clipMode,
1926 stencilBits,
1927 &frontRef,
1928 &frontMask,
1929 &frontWriteMask);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001930 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001931 unsigned int backRef = settings->backFuncRef();
1932 unsigned int backMask = settings->backFuncMask();
1933 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001934
bsalomon@google.com411dad02012-06-05 20:24:20 +00001935 GrStencilFunc backFunc =
1936 fClipMaskManager.adjustStencilParams(settings->frontFunc(),
1937 clipMode,
1938 stencilBits,
1939 &backRef,
1940 &backMask,
1941 &backWriteMask);
1942 set_gl_stencil(this->glInterface(),
1943 GR_GL_FRONT,
1944 frontFunc,
1945 settings->frontFailOp(),
1946 settings->frontPassOp(),
1947 frontRef,
1948 frontMask,
1949 frontWriteMask);
1950 set_gl_stencil(this->glInterface(),
1951 GR_GL_BACK,
1952 backFunc,
1953 settings->backFailOp(),
1954 settings->backPassOp(),
1955 backRef,
1956 backMask,
1957 backWriteMask);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001958 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001959 set_gl_stencil(this->glInterface(),
1960 GR_GL_FRONT_AND_BACK,
1961 frontFunc,
1962 settings->frontFailOp(),
1963 settings->frontPassOp(),
1964 frontRef,
1965 frontMask,
1966 frontWriteMask);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001967 }
1968 }
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001969 fHWStencilSettings = *settings;
1970 fHWStencilClipMode = clipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001971 }
1972}
1973
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001974void GrGpuGL::flushAAState(bool isLines) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001975 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001976 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001977 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1978 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001979 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001980 bool smoothLines = false;
1981
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001982 if (isLines) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001983 smoothLines = this->willUseHWAALines();
1984 if (smoothLines) {
1985 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1986 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1987 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1988 // must disable msaa to use line smoothing
1989 if (rt->isMultisampled() &&
1990 kNo_TriState != fHWAAState.fMSAAEnabled) {
1991 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1992 fHWAAState.fMSAAEnabled = kNo_TriState;
1993 }
1994 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001995 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001996 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1997 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1998 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1999 }
2000 }
2001 }
2002 if (!smoothLines && rt->isMultisampled()) {
2003 if (this->getDrawState().isHWAntialiasState()) {
2004 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2005 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2006 fHWAAState.fMSAAEnabled = kYes_TriState;
2007 }
2008 } else {
2009 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2010 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2011 fHWAAState.fMSAAEnabled = kNo_TriState;
2012 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002013 }
2014 }
2015 }
2016}
2017
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002018void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002019 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002020 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002021 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002022 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002023 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002024 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002025 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002026 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2027 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2028 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2029 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2030 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2031 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002032 }
2033 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002034 // any optimization to disable blending should
2035 // have already been applied and tweaked the coeffs
2036 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002037 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2038 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002039 if (blendOff) {
2040 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002041 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002042 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002043 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002044 } else {
2045 if (kYes_TriState != fHWBlendState.fEnabled) {
2046 GL_CALL(Enable(GR_GL_BLEND));
2047 fHWBlendState.fEnabled = kYes_TriState;
2048 }
2049 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2050 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002051 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2052 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002053 fHWBlendState.fSrcCoeff = srcCoeff;
2054 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002055 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002056 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002057 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2058 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002059 (!fHWBlendState.fConstColorValid ||
2060 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002061
2062 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002063 GrColorUnpackR(blendConst) / 255.f,
2064 GrColorUnpackG(blendConst) / 255.f,
2065 GrColorUnpackB(blendConst) / 255.f,
2066 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002067 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002068 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002069 fHWBlendState.fConstColor = blendConst;
2070 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002071 }
2072 }
2073 }
2074}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002075namespace {
2076
2077unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002078 switch (filter) {
2079 case GrSamplerState::kBilinear_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002080 return GR_GL_LINEAR;
2081 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002082 return GR_GL_NEAREST;
2083 default:
2084 GrAssert(!"Unknown filter type");
2085 return GR_GL_LINEAR;
2086 }
2087}
2088
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002089// get_swizzle is only called from this .cpp so it is OK to inline it here
2090inline const GrGLenum* get_swizzle(GrPixelConfig config,
2091 const GrSamplerState& sampler,
2092 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002093 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002094 if (glCaps.textureRedSupport()) {
2095 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2096 GR_GL_RED, GR_GL_RED };
2097 return gRedSmear;
2098 } else {
2099 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2100 GR_GL_ALPHA, GR_GL_ALPHA };
2101 return gAlphaSmear;
2102 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002103 } else if (sampler.swapsRAndB()) {
2104 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2105 GR_GL_RED, GR_GL_ALPHA };
2106 return gRedBlueSwap;
2107 } else {
2108 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2109 GR_GL_BLUE, GR_GL_ALPHA };
2110 return gStraight;
2111 }
2112}
2113
2114void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002115 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2116 GR_GL_TEXTURE_SWIZZLE_RGBA,
2117 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002118}
2119}
2120
bsalomon@google.com4c883782012-06-04 19:05:11 +00002121void GrGpuGL::flushBoundTextureAndParams(int stage) {
2122 GrDrawState* drawState = this->drawState();
2123
2124 GrGLTexture* nextTexture =
2125 static_cast<GrGLTexture*>(drawState->getTexture(stage));
2126
2127 // true for now, but maybe not with GrEffect.
2128 GrAssert(NULL != nextTexture);
2129 // if we created a rt/tex and rendered to it without using a
2130 // texture and now we're texturing from the rt it will still be
2131 // the last bound texture, but it needs resolving. So keep this
2132 // out of the "last != next" check.
2133 GrGLRenderTarget* texRT =
2134 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2135 if (NULL != texRT) {
2136 this->onResolveRenderTarget(texRT);
2137 }
2138
2139 if (fHWBoundTextures[stage] != nextTexture) {
2140 this->setTextureUnit(stage);
2141 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002142 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2143 fHWBoundTextures[stage] = nextTexture;
2144 }
2145
2146 const GrSamplerState& sampler = drawState->getSampler(stage);
2147 ResetTimestamp timestamp;
2148 const GrGLTexture::TexParams& oldTexParams =
2149 nextTexture->getCachedTexParams(&timestamp);
2150 bool setAll = timestamp < this->getResetTimestamp();
2151 GrGLTexture::TexParams newTexParams;
2152
2153 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
2154
2155 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
2156 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2157 newTexParams.fWrapT = wraps[sampler.getWrapY()];
2158 memcpy(newTexParams.fSwizzleRGBA,
2159 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
2160 sizeof(newTexParams.fSwizzleRGBA));
2161 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2162 this->setTextureUnit(stage);
2163 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2164 GR_GL_TEXTURE_MAG_FILTER,
2165 newTexParams.fFilter));
2166 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2167 GR_GL_TEXTURE_MIN_FILTER,
2168 newTexParams.fFilter));
2169 }
2170 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2171 this->setTextureUnit(stage);
2172 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2173 GR_GL_TEXTURE_WRAP_S,
2174 newTexParams.fWrapS));
2175 }
2176 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2177 this->setTextureUnit(stage);
2178 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2179 GR_GL_TEXTURE_WRAP_T,
2180 newTexParams.fWrapT));
2181 }
2182 if (this->glCaps().textureSwizzleSupport() &&
2183 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2184 oldTexParams.fSwizzleRGBA,
2185 sizeof(newTexParams.fSwizzleRGBA)))) {
2186 this->setTextureUnit(stage);
2187 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2188 this->glInterface());
2189 }
2190 nextTexture->setCachedTexParams(newTexParams,
2191 this->getResetTimestamp());
2192}
2193
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002194void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002195
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002196 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002197
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002198 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002199 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002200 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002201 fHWDitherEnabled = kYes_TriState;
2202 }
2203 } else {
2204 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002205 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002206 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002207 }
2208 }
2209
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002210 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002211 if (kNo_TriState != fHWWriteToColor) {
2212 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2213 GR_GL_FALSE, GR_GL_FALSE));
2214 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002215 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002216 } else {
2217 if (kYes_TriState != fHWWriteToColor) {
2218 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2219 fHWWriteToColor = kYes_TriState;
2220 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002221 }
2222
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002223 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002224 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002225 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002226 GL_CALL(Enable(GR_GL_CULL_FACE));
2227 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002228 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002229 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002230 GL_CALL(Enable(GR_GL_CULL_FACE));
2231 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002232 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002233 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002234 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002235 break;
2236 default:
2237 GrCrash("Unknown draw face.");
2238 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002239 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002240 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002241}
2242
2243void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002244 if (fHWGeometryState.fVertexBuffer != buffer) {
2245 fHWGeometryState.fArrayPtrsDirty = true;
2246 fHWGeometryState.fVertexBuffer = buffer;
2247 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002248}
2249
2250void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 if (fHWGeometryState.fVertexBuffer == buffer) {
2252 // deleting bound buffer does implied bind to 0
2253 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002254 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 }
2256}
2257
2258void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002259 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002260}
2261
2262void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002263 if (fHWGeometryState.fIndexBuffer == buffer) {
2264 // deleting bound buffer does implied bind to 0
2265 fHWGeometryState.fIndexBuffer = NULL;
2266 }
2267}
2268
reed@google.comac10a2d2010-12-22 21:39:39 +00002269void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2270 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002271 GrDrawState* drawState = this->drawState();
2272 if (drawState->getRenderTarget() == renderTarget) {
2273 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002274 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002275 if (fHWBoundRenderTarget == renderTarget) {
2276 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002277 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002278}
2279
2280void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002281 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002282 GrDrawState* drawState = this->drawState();
2283 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002284 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002285 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002286 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002287 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002288 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002289 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002291}
2292
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002293bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2294 bool getSizedInternalFormat,
2295 GrGLenum* internalFormat,
2296 GrGLenum* externalFormat,
2297 GrGLenum* externalType) {
2298 GrGLenum dontCare;
2299 if (NULL == internalFormat) {
2300 internalFormat = &dontCare;
2301 }
2302 if (NULL == externalFormat) {
2303 externalFormat = &dontCare;
2304 }
2305 if (NULL == externalType) {
2306 externalType = &dontCare;
2307 }
2308
reed@google.comac10a2d2010-12-22 21:39:39 +00002309 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002310 case kRGBA_8888_PM_GrPixelConfig:
2311 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002312 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002313 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002314 if (getSizedInternalFormat) {
2315 *internalFormat = GR_GL_RGBA8;
2316 } else {
2317 *internalFormat = GR_GL_RGBA;
2318 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002319 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002320 break;
2321 case kBGRA_8888_PM_GrPixelConfig:
2322 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002323 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002324 return false;
2325 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002326 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002327 if (getSizedInternalFormat) {
2328 *internalFormat = GR_GL_BGRA8;
2329 } else {
2330 *internalFormat = GR_GL_BGRA;
2331 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002332 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002333 if (getSizedInternalFormat) {
2334 *internalFormat = GR_GL_RGBA8;
2335 } else {
2336 *internalFormat = GR_GL_RGBA;
2337 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002338 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002339 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002340 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002341 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002342 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002343 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002344 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002345 if (getSizedInternalFormat) {
2346 if (this->glBinding() == kDesktop_GrGLBinding) {
2347 return false;
2348 } else {
2349 *internalFormat = GR_GL_RGB565;
2350 }
2351 } else {
2352 *internalFormat = GR_GL_RGB;
2353 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002354 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002355 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002356 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002357 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002358 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002359 if (getSizedInternalFormat) {
2360 *internalFormat = GR_GL_RGBA4;
2361 } else {
2362 *internalFormat = GR_GL_RGBA;
2363 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002364 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002365 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002366 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002367 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002368 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002369 // glCompressedTexImage doesn't take external params
2370 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002371 // no sized/unsized internal format distinction here
2372 *internalFormat = GR_GL_PALETTE8_RGBA8;
2373 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002374 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002375 } else {
2376 return false;
2377 }
2378 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002379 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002380 if (this->glCaps().textureRedSupport()) {
2381 *internalFormat = GR_GL_RED;
2382 *externalFormat = GR_GL_RED;
2383 if (getSizedInternalFormat) {
2384 *internalFormat = GR_GL_R8;
2385 } else {
2386 *internalFormat = GR_GL_RED;
2387 }
2388 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002389 } else {
2390 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002391 *externalFormat = GR_GL_ALPHA;
2392 if (getSizedInternalFormat) {
2393 *internalFormat = GR_GL_ALPHA8;
2394 } else {
2395 *internalFormat = GR_GL_ALPHA;
2396 }
2397 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002398 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002399 break;
2400 default:
2401 return false;
2402 }
2403 return true;
2404}
2405
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002406void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002407 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002408 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002409 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002410 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002411 }
2412}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002413
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002414void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002415 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002416 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002417 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002418 }
2419}
2420
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002421void GrGpuGL::setBuffers(bool indexed,
2422 int* extraVertexOffset,
2423 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002424
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002425 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002426
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002427 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2428
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002429 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002430 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002431 case kBuffer_GeometrySrcType:
2432 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002433 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434 break;
2435 case kArray_GeometrySrcType:
2436 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002437 this->finalizeReservedVertices();
2438 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2439 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002440 break;
2441 default:
2442 vbuf = NULL; // suppress warning
2443 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002444 }
2445
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002446 GrAssert(NULL != vbuf);
2447 GrAssert(!vbuf->isLocked());
2448 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002449 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002450 fHWGeometryState.fArrayPtrsDirty = true;
2451 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002452 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002453
2454 if (indexed) {
2455 GrAssert(NULL != extraIndexOffset);
2456
2457 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002458 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459 case kBuffer_GeometrySrcType:
2460 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002461 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462 break;
2463 case kArray_GeometrySrcType:
2464 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002465 this->finalizeReservedIndices();
2466 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2467 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002468 break;
2469 default:
2470 ibuf = NULL; // suppress warning
2471 GrCrash("Unknown geometry src type!");
2472 }
2473
2474 GrAssert(NULL != ibuf);
2475 GrAssert(!ibuf->isLocked());
2476 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002477 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002478 fHWGeometryState.fIndexBuffer = ibuf;
2479 }
2480 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002481}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002482