blob: 6549e050f1de39fb5978822fc7950d94118d0010 [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));
471 GL_CALL(Disable(GR_GL_COLOR_TABLE));
472 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
473 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
474 // Since ES doesn't support glPointSize at all we always use the VS to
475 // set the point size
476 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
477
478 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
479 // currently part of our gl interface. There are probably others as
480 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000481 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000482 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000483 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000484
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000486 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000487
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000488 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000489 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000490
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000491 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000492
tomhudson@google.com93813632011-10-27 20:21:16 +0000493 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000494 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000495 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000496
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000497 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000498 // set to true to force disableScissor to make a GL call.
499 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000500 this->disableScissor();
501
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000502 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000504 fHWStencilSettings.invalidate();
bsalomon@google.com411dad02012-06-05 20:24:20 +0000505 // This is arbitrary. The above invalidate ensures a full setup of the
506 // stencil on the next draw.
507 fHWStencilClipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000508
509 // TODO: I believe this should actually go in GrGpu::onResetContext
510 // rather than here
511 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000512
513 fHWGeometryState.fIndexBuffer = NULL;
514 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000515
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000516 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000517
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000518 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000519
520 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000521 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000522 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
523 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000524 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000525 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
526 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000527 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000528 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
529 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000530 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000531 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
532 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000533
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000534 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000535
536 // Third party GL code may have left vertex attributes enabled. Some GL
537 // implementations (osmesa) may read vetex attributes that are not required
538 // by the current shader. Therefore, we have to ensure that only the
539 // attributes we require for the current draw are enabled or we may cause an
540 // invalid read.
541
542 // Disable all vertex layout bits so that next flush will assume all
543 // optional vertex attributes are disabled.
544 fHWGeometryState.fVertexLayout = 0;
545
546 // We always use the this attribute and assume it is always enabled.
547 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
548 GL_CALL(EnableVertexAttribArray(posAttrIdx));
549 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000550 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000551 if (va != posAttrIdx) {
552 GL_CALL(DisableVertexAttribArray(va));
553 }
554 }
555
556 fHWProgramID = 0;
557 fHWConstAttribColor = GrColor_ILLEGAL;
558 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000559}
560
bsalomon@google.come269f212011-11-07 13:29:52 +0000561GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000562 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000563 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000564 return NULL;
565 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000566
robertphillips@google.com32716282012-06-04 12:48:45 +0000567 // next line relies on PlatformTextureDesc's flags matching GrTexture's
568 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000569 glTexDesc.fWidth = desc.fWidth;
570 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000571 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000572 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000573 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
574 glTexDesc.fOwnsID = false;
575 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
576
577 GrGLTexture* texture = NULL;
578 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
579 GrGLRenderTarget::Desc glRTDesc;
580 glRTDesc.fRTFBOID = 0;
581 glRTDesc.fTexFBOID = 0;
582 glRTDesc.fMSColorRenderbufferID = 0;
583 glRTDesc.fOwnIDs = true;
584 glRTDesc.fConfig = desc.fConfig;
585 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000586 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
587 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000588 glTexDesc.fTextureID,
589 &glRTDesc)) {
590 return NULL;
591 }
592 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
593 } else {
594 texture = new GrGLTexture(this, glTexDesc);
595 }
596 if (NULL == texture) {
597 return NULL;
598 }
599
600 this->setSpareTextureUnit();
601 return texture;
602}
603
604GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
605 GrGLRenderTarget::Desc glDesc;
606 glDesc.fConfig = desc.fConfig;
607 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
608 glDesc.fMSColorRenderbufferID = 0;
609 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
610 glDesc.fSampleCnt = desc.fSampleCnt;
611 glDesc.fOwnIDs = false;
612 GrGLIRect viewport;
613 viewport.fLeft = 0;
614 viewport.fBottom = 0;
615 viewport.fWidth = desc.fWidth;
616 viewport.fHeight = desc.fHeight;
617
618 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
619 if (desc.fStencilBits) {
620 GrGLStencilBuffer::Format format;
621 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
622 format.fPacked = false;
623 format.fStencilBits = desc.fStencilBits;
624 format.fTotalBits = desc.fStencilBits;
625 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
626 0,
627 desc.fWidth,
628 desc.fHeight,
629 desc.fSampleCnt,
630 format);
631 tgt->setStencilBuffer(sb);
632 sb->unref();
633 }
634 return tgt;
635}
636
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000637////////////////////////////////////////////////////////////////////////////////
638
bsalomon@google.com6f379512011-11-16 20:36:03 +0000639void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
640 int left, int top, int width, int height,
641 GrPixelConfig config, const void* buffer,
642 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000643 if (NULL == buffer) {
644 return;
645 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000646 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
647
bsalomon@google.com6f379512011-11-16 20:36:03 +0000648 this->setSpareTextureUnit();
649 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
650 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000651 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000652 desc.fWidth = glTex->width();
653 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000654 desc.fConfig = glTex->config();
655 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000656 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000657 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000658
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000659 this->uploadTexData(desc, false,
660 left, top, width, height,
661 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000662}
663
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000664namespace {
665bool adjust_pixel_ops_params(int surfaceWidth,
666 int surfaceHeight,
667 size_t bpp,
668 int* left, int* top, int* width, int* height,
669 const void** data,
670 size_t* rowBytes) {
671 if (!*rowBytes) {
672 *rowBytes = *width * bpp;
673 }
674
675 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
676 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
677
678 if (!subRect.intersect(bounds)) {
679 return false;
680 }
681 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
682 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
683
684 *left = subRect.fLeft;
685 *top = subRect.fTop;
686 *width = subRect.width();
687 *height = subRect.height();
688 return true;
689}
690}
691
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000692bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
693 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000694 int left, int top, int width, int height,
695 GrPixelConfig dataConfig,
696 const void* data,
697 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000698 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000699
700 size_t bpp = GrBytesPerPixel(dataConfig);
701 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
702 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000703 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000704 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000705 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000706
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000707 // in case we need a temporary, trimmed copy of the src pixels
708 SkAutoSMalloc<128 * 128> tempStorage;
709
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000710 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000711 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000712 if (useTexStorage) {
713 if (kDesktop_GrGLBinding == this->glBinding()) {
714 // 565 is not a sized internal format on desktop GL. So on desktop
715 // with 565 we always use an unsized internal format to let the
716 // system pick the best sized format to convert the 565 data to.
717 // Since glTexStorage only allows sized internal formats we will
718 // instead fallback to glTexImage2D.
719 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
720 } else {
721 // ES doesn't allow paletted textures to be used with tex storage
722 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
723 }
724 }
725
726 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000727 GrGLenum externalFormat;
728 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000729 // glTexStorage requires sized internal formats on both desktop and ES. ES
730 // glTexImage requires an unsized format.
731 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
732 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000733 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000734 }
735
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000736 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000737 // paletted textures cannot be updated
738 return false;
739 }
740
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000741 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000742 * check whether to allocate a temporary buffer for flipping y or
743 * because our srcData has extra bytes past each row. If so, we need
744 * to trim those off here, since GL ES may not let us specify
745 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000746 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000747 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000748 bool swFlipY = false;
749 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000750 if (NULL != data) {
751 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000752 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000753 glFlipY = true;
754 } else {
755 swFlipY = true;
756 }
757 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000758 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000759 // can't use this for flipping, only non-neg values allowed. :(
760 if (rowBytes != trimRowBytes) {
761 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
762 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
763 restoreGLRowLength = true;
764 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000765 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000766 if (trimRowBytes != rowBytes || swFlipY) {
767 // copy data into our new storage, skipping the trailing bytes
768 size_t trimSize = height * trimRowBytes;
769 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000770 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000771 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000772 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000773 char* dst = (char*)tempStorage.reset(trimSize);
774 for (int y = 0; y < height; y++) {
775 memcpy(dst, src, trimRowBytes);
776 if (swFlipY) {
777 src -= rowBytes;
778 } else {
779 src += rowBytes;
780 }
781 dst += trimRowBytes;
782 }
783 // now point data to our copied version
784 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000785 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000786 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000787 if (glFlipY) {
788 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
789 }
790 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000791 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000792 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000793 if (isNewTexture &&
794 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000795 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000796 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000797 if (useTexStorage) {
798 // We never resize or change formats of textures. We don't use
799 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000800 GL_ALLOC_CALL(this->glInterface(),
801 TexStorage2D(GR_GL_TEXTURE_2D,
802 1, // levels
803 internalFormat,
804 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000805 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000806 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
807 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
808 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000809 GL_ALLOC_CALL(this->glInterface(),
810 CompressedTexImage2D(GR_GL_TEXTURE_2D,
811 0, // level
812 internalFormat,
813 desc.fWidth, desc.fHeight,
814 0, // border
815 imageSize,
816 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000817 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000818 GL_ALLOC_CALL(this->glInterface(),
819 TexImage2D(GR_GL_TEXTURE_2D,
820 0, // level
821 internalFormat,
822 desc.fWidth, desc.fHeight,
823 0, // border
824 externalFormat, externalType,
825 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000826 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000827 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000828 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000829 if (error != GR_GL_NO_ERROR) {
830 succeeded = false;
831 } else {
832 // if we have data and we used TexStorage to create the texture, we
833 // now upload with TexSubImage.
834 if (NULL != data && useTexStorage) {
835 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
836 0, // level
837 left, top,
838 width, height,
839 externalFormat, externalType,
840 data));
841 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000842 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000843 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000844 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000845 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000847 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
848 0, // level
849 left, top,
850 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000851 externalFormat, externalType, data));
852 }
853
854 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000855 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000856 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000857 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000858 if (glFlipY) {
859 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
860 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000861 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000862}
863
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000864namespace {
865bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
866 int sampleCount,
867 GrGLenum format,
868 int width, int height) {
869 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
870 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
871 bool created = false;
872 if (GrGLCaps::kNVDesktop_CoverageAAType ==
873 ctxInfo.caps().coverageAAType()) {
874 const GrGLCaps::MSAACoverageMode& mode =
875 ctxInfo.caps().getMSAACoverageMode(sampleCount);
876 GL_ALLOC_CALL(ctxInfo.interface(),
877 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
878 mode.fCoverageSampleCnt,
879 mode.fColorSampleCnt,
880 format,
881 width, height));
882 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
883 }
884 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000885 // glRBMS will fail if requested samples is > max samples.
886 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000887 GL_ALLOC_CALL(ctxInfo.interface(),
888 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
889 sampleCount,
890 format,
891 width, height));
892 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
893 }
894 return created;
895}
896}
897
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000898bool GrGpuGL::createRenderTargetObjects(int width, int height,
899 GrGLuint texID,
900 GrGLRenderTarget::Desc* desc) {
901 desc->fMSColorRenderbufferID = 0;
902 desc->fRTFBOID = 0;
903 desc->fTexFBOID = 0;
904 desc->fOwnIDs = true;
905
906 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907
bsalomon@google.comab15d612011-08-09 12:57:56 +0000908 GrGLenum msColorFormat = 0; // suppress warning
909
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000910 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000911 if (!desc->fTexFBOID) {
912 goto FAILED;
913 }
914
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915
916 // If we are using multisampling we will create two FBOS. We render
917 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000918 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000919 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000920 goto FAILED;
921 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
923 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000924 if (!desc->fRTFBOID ||
925 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000926 !this->configToGLFormats(desc->fConfig,
927 // GLES requires sized internal formats
928 kES2_GrGLBinding == this->glBinding(),
929 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000930 goto FAILED;
931 }
932 } else {
933 desc->fRTFBOID = desc->fTexFBOID;
934 }
935
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000936 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000937 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000938 if (desc->fRTFBOID != desc->fTexFBOID) {
939 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000940 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000941 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000942 if (!renderbuffer_storage_msaa(fGLContextInfo,
943 desc->fSampleCnt,
944 msColorFormat,
945 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000946 goto FAILED;
947 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000948 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
949 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950 GR_GL_COLOR_ATTACHMENT0,
951 GR_GL_RENDERBUFFER,
952 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000953 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000954 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
955 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
956 goto FAILED;
957 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000958 fGLContextInfo.caps().markConfigAsValidColorAttachment(
959 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000960 }
961 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000962 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000963
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000964 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
965 GR_GL_COLOR_ATTACHMENT0,
966 GR_GL_TEXTURE_2D,
967 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000968 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000969 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
970 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
971 goto FAILED;
972 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000973 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000974 }
975
976 return true;
977
978FAILED:
979 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000980 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000981 }
982 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000983 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000984 }
985 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000986 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000987 }
988 return false;
989}
990
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000991// good to set a break-point here to know when createTexture fails
992static GrTexture* return_null_texture() {
993// GrAssert(!"null texture");
994 return NULL;
995}
996
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000997#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000998static size_t as_size_t(int x) {
999 return x;
1000}
1001#endif
1002
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001003GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001004 const void* srcData,
1005 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001006
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001007 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001008 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001009
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001010 // Attempt to catch un- or wrongly initialized sample counts;
1011 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1012
robertphillips@google.com32716282012-06-04 12:48:45 +00001013 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +00001014 glTexDesc.fWidth = desc.fWidth;
1015 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001016 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +00001017 glTexDesc.fSampleCnt = desc.fSampleCnt;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001018 glTexDesc.fClientCacheID = desc.fClientCacheID;
robertphillips@google.com32716282012-06-04 12:48:45 +00001019
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001020 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001021
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001022 glRTDesc.fMSColorRenderbufferID = 0;
1023 glRTDesc.fRTFBOID = 0;
1024 glRTDesc.fTexFBOID = 0;
1025 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001026 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001027
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001028 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001029
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001030 const Caps& caps = this->getCaps();
1031
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001032 // We keep GrRenderTargets in GL's normal orientation so that they
1033 // can be drawn to by the outside world without the client having
1034 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001035 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001036 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001037
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001038 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001039 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001040 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +00001041 //GrPrintf("MSAA RT requested but not supported on this platform.");
1042 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +00001043 }
1044
reed@google.comac10a2d2010-12-22 21:39:39 +00001045 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001046 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1047 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001048 return return_null_texture();
1049 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001050 }
1051
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001052 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001053 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001054 // provides a hint about how this texture will be used
1055 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1056 GR_GL_TEXTURE_USAGE,
1057 GR_GL_FRAMEBUFFER_ATTACHMENT));
1058 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001059 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001060 return return_null_texture();
1061 }
1062
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001063 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001064 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001065
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001066 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1067 // drivers have a bug where an FBO won't be complete if it includes a
1068 // texture that is not mipmap complete (considering the filter in use).
1069 GrGLTexture::TexParams initialTexParams;
1070 // we only set a subset here so invalidate first
1071 initialTexParams.invalidate();
1072 initialTexParams.fFilter = GR_GL_NEAREST;
1073 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1074 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001075 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1076 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001077 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1079 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001080 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001081 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1082 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001083 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001084 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1085 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001086 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001087 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1088 glTexDesc.fWidth, glTexDesc.fHeight,
1089 desc.fConfig, srcData, rowBytes)) {
1090 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1091 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001092 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001093
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001094 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001096 // unbind the texture from the texture unit before binding it to the frame buffer
1097 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1098
bsalomon@google.com99621082011-11-15 16:47:16 +00001099 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1100 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 glTexDesc.fTextureID,
1102 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001103 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001104 return return_null_texture();
1105 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001106 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001107 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001108 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001109 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001110 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001111#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001112 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1113 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001114#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 return tex;
1116}
1117
1118namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001119
1120const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1121
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1123 GrGLuint rb,
1124 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 // we shouldn't ever know one size and not the other
1126 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1127 (kUnknownBitCount == format->fTotalBits));
1128 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001129 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1131 (GrGLint*)&format->fStencilBits);
1132 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001133 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1135 (GrGLint*)&format->fTotalBits);
1136 format->fTotalBits += format->fStencilBits;
1137 } else {
1138 format->fTotalBits = format->fStencilBits;
1139 }
1140 }
1141}
1142}
1143
1144bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1145 int width, int height) {
1146
1147 // All internally created RTs are also textures. We don't create
1148 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1149 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001150 GrAssert(width >= rt->width());
1151 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152
1153 int samples = rt->numSamples();
1154 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001155 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 if (!sbID) {
1157 return false;
1158 }
1159
1160 GrGLStencilBuffer* sb = NULL;
1161
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001162 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 // we start with the last stencil format that succeeded in hopes
1166 // that we won't go through this loop more than once after the
1167 // first (painful) stencil creation.
1168 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001169 const GrGLCaps::StencilFormat& sFmt =
1170 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001171 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001172 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001174 bool created;
1175 if (samples > 0) {
1176 created = renderbuffer_storage_msaa(fGLContextInfo,
1177 samples,
1178 sFmt.fInternalFormat,
1179 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001181 GL_ALLOC_CALL(this->glInterface(),
1182 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001183 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001184 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001185 created =
1186 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001188 if (created) {
1189 // After sized formats we attempt an unsized format and take
1190 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001191 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001193 sb = new GrGLStencilBuffer(this, sbID, width, height,
1194 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1196 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001197 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001199 return true;
1200 }
1201 sb->abandon(); // otherwise we lose sbID
1202 sb->unref();
1203 }
1204 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001205 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001206 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207}
1208
1209bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1210 GrRenderTarget* rt) {
1211 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1212
1213 GrGLuint fbo = glrt->renderFBOID();
1214
1215 if (NULL == sb) {
1216 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 GR_GL_STENCIL_ATTACHMENT,
1219 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 GR_GL_DEPTH_ATTACHMENT,
1222 GR_GL_RENDERBUFFER, 0));
1223#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001224 GrGLenum status;
1225 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1227#endif
1228 }
1229 return true;
1230 } else {
1231 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1232 GrGLuint rb = glsb->renderbufferID();
1233
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001234 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001235 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1236 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237 GR_GL_STENCIL_ATTACHMENT,
1238 GR_GL_RENDERBUFFER, rb));
1239 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 GR_GL_DEPTH_ATTACHMENT,
1242 GR_GL_RENDERBUFFER, rb));
1243 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001244 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 GR_GL_DEPTH_ATTACHMENT,
1246 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001247 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001249 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001250 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001251 glsb->format())) {
1252 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1253 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001254 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001255 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001256 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001257 if (glsb->format().fPacked) {
1258 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1259 GR_GL_DEPTH_ATTACHMENT,
1260 GR_GL_RENDERBUFFER, 0));
1261 }
1262 return false;
1263 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001264 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001265 rt->config(),
1266 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001267 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001269 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001270 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001271}
1272
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001273////////////////////////////////////////////////////////////////////////////////
1274
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001275GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001276 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001277 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001279 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001280 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001281 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001283 GL_ALLOC_CALL(this->glInterface(),
1284 BufferData(GR_GL_ARRAY_BUFFER,
1285 size,
1286 NULL, // data ptr
1287 dynamic ? GR_GL_DYNAMIC_DRAW :
1288 GR_GL_STATIC_DRAW));
1289 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001290 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 // deleting bound buffer does implicit bind to 0
1292 fHWGeometryState.fVertexBuffer = NULL;
1293 return NULL;
1294 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001295 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 size, dynamic);
1297 fHWGeometryState.fVertexBuffer = vertexBuffer;
1298 return vertexBuffer;
1299 }
1300 return NULL;
1301}
1302
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001303GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001304 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001305 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001306 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001307 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001308 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001310 GL_ALLOC_CALL(this->glInterface(),
1311 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1312 size,
1313 NULL, // data ptr
1314 dynamic ? GR_GL_DYNAMIC_DRAW :
1315 GR_GL_STATIC_DRAW));
1316 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001317 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001318 // deleting bound buffer does implicit bind to 0
1319 fHWGeometryState.fIndexBuffer = NULL;
1320 return NULL;
1321 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001322 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001323 size, dynamic);
1324 fHWGeometryState.fIndexBuffer = indexBuffer;
1325 return indexBuffer;
1326 }
1327 return NULL;
1328}
1329
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001330GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
1331 GrPath* path = NULL;
1332 if (fCaps.fPathStencilingSupport) {
1333 path = new GrGLPath(this, inPath);
1334 }
1335 return path;
1336}
1337
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001338void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001339 const GrDrawState& drawState = this->getDrawState();
1340 const GrGLRenderTarget* rt =
1341 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1342
1343 GrAssert(NULL != rt);
1344 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001345
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001346 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001347 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1348 rect.width(), rect.height());
1349 if (scissor.contains(vp)) {
1350 disableScissor();
1351 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 }
1353
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001354 if (fHWBounds.fScissorRect != scissor) {
1355 scissor.pushToGLScissor(this->glInterface());
1356 fHWBounds.fScissorRect = scissor;
1357 }
1358 if (!fHWBounds.fScissorEnabled) {
1359 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1360 fHWBounds.fScissorEnabled = true;
1361 }
1362}
1363
1364void GrGpuGL::disableScissor() {
1365 if (fHWBounds.fScissorEnabled) {
1366 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1367 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001368 }
1369}
1370
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001371void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001372 const GrDrawState& drawState = this->getDrawState();
1373 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001374 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001375 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001376
bsalomon@google.com74b98712011-11-11 19:46:16 +00001377 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001378 if (NULL != rect) {
1379 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001380 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001381 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001382 if (clippedRect.intersect(rtRect)) {
1383 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001384 } else {
1385 return;
1386 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001387 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001388 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001389 if (NULL != rect)
1390 this->enableScissoring(*rect);
1391 else
1392 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001393
1394 GrGLfloat r, g, b, a;
1395 static const GrGLfloat scale255 = 1.f / 255.f;
1396 a = GrColorUnpackA(color) * scale255;
1397 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001398 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001399 scaleRGB *= a;
1400 }
1401 r = GrColorUnpackR(color) * scaleRGB;
1402 g = GrColorUnpackG(color) * scaleRGB;
1403 b = GrColorUnpackB(color) * scaleRGB;
1404
1405 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001406 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001407 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001408 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001409}
1410
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001411void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001412 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001413 return;
1414 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001415
1416 this->flushRenderTarget(&GrIRect::EmptyIRect());
1417
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001418 this->disableScissor();
1419
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001420 GL_CALL(StencilMask(0xffffffff));
1421 GL_CALL(ClearStencil(0));
1422 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001423 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001424}
1425
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001426void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001427 const GrDrawState& drawState = this->getDrawState();
1428 const GrRenderTarget* rt = drawState.getRenderTarget();
1429 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001430
1431 // this should only be called internally when we know we have a
1432 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001433 GrAssert(NULL != rt->getStencilBuffer());
1434 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001435#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001436 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001437 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001438#else
1439 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001440 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001441 // turned into draws. Our contract on GrDrawTarget says that
1442 // changing the clip between stencil passes may or may not
1443 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001444 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001445#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001446 GrGLint value;
1447 if (insideClip) {
1448 value = (1 << (stencilBitCount - 1));
1449 } else {
1450 value = 0;
1451 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001452 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001453 this->enableScissoring(rect);
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001454 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001455 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001456 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001457 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001458}
1459
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001460void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001461 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001462}
1463
bsalomon@google.comc4364992011-11-07 15:54:49 +00001464bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1465 int left, int top,
1466 int width, int height,
1467 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001468 size_t rowBytes) const {
1469 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001470 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001471 return false;
1472 }
1473
1474 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001475 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001476 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001477 return true;
1478 }
1479 // If we have to do memcpys to handle rowBytes then y-flip is free
1480 // Note the rowBytes might be tight to the passed in data, but if data
1481 // gets clipped in x to the target the rowBytes will no longer be tight.
1482 if (left >= 0 && (left + width) < renderTarget->width()) {
1483 return 0 == rowBytes ||
1484 GrBytesPerPixel(config) * width == rowBytes;
1485 } else {
1486 return false;
1487 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001488}
1489
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001490bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001491 int left, int top,
1492 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001493 GrPixelConfig config,
1494 void* buffer,
1495 size_t rowBytes,
1496 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001497 GrGLenum format;
1498 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001499 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001501 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001502 size_t bpp = GrBytesPerPixel(config);
1503 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1504 &left, &top, &width, &height,
1505 const_cast<const void**>(&buffer),
1506 &rowBytes)) {
1507 return false;
1508 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001509
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001511 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001512 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001513 switch (tgt->getResolveType()) {
1514 case GrGLRenderTarget::kCantResolve_ResolveType:
1515 return false;
1516 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001517 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001518 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001519 break;
1520 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001521 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001522 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001523 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1524 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001525 break;
1526 default:
1527 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001528 }
1529
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001530 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001531
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001532 // the read rect is viewport-relative
1533 GrGLIRect readRect;
1534 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001535
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001536 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537 if (0 == rowBytes) {
1538 rowBytes = tightRowBytes;
1539 }
1540 size_t readDstRowBytes = tightRowBytes;
1541 void* readDst = buffer;
1542
1543 // determine if GL can read using the passed rowBytes or if we need
1544 // a scratch buffer.
1545 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1546 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001547 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001548 GrAssert(!(rowBytes % sizeof(GrColor)));
1549 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1550 readDstRowBytes = rowBytes;
1551 } else {
1552 scratch.reset(tightRowBytes * height);
1553 readDst = scratch.get();
1554 }
1555 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001556 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001557 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1558 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001559 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1560 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001561 format, type, readDst));
1562 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001563 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001564 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1565 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001566 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001567 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1568 invertY = true;
1569 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001570
1571 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001572 // API presents top-to-bottom. We must preserve the padding contents. Note
1573 // that the above readPixels did not overwrite the padding.
1574 if (readDst == buffer) {
1575 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001576 if (!invertY) {
1577 scratch.reset(tightRowBytes);
1578 void* tmpRow = scratch.get();
1579 // flip y in-place by rows
1580 const int halfY = height >> 1;
1581 char* top = reinterpret_cast<char*>(buffer);
1582 char* bottom = top + (height - 1) * rowBytes;
1583 for (int y = 0; y < halfY; y++) {
1584 memcpy(tmpRow, top, tightRowBytes);
1585 memcpy(top, bottom, tightRowBytes);
1586 memcpy(bottom, tmpRow, tightRowBytes);
1587 top += rowBytes;
1588 bottom -= rowBytes;
1589 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001590 }
1591 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001592 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001593 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001594 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001595 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001596 char* dst = reinterpret_cast<char*>(buffer);
1597 if (!invertY) {
1598 dst += (height-1) * rowBytes;
1599 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001600 for (int y = 0; y < height; y++) {
1601 memcpy(dst, src, tightRowBytes);
1602 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001603 if (invertY) {
1604 dst += rowBytes;
1605 } else {
1606 dst -= rowBytes;
1607 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001608 }
1609 }
1610 return true;
1611}
1612
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001613void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001614
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001615 GrGLRenderTarget* rt =
1616 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1617 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001618
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001619 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001620 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001621 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001622 GrGLenum status;
1623 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001624 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001625 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001626 }
1627 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001628 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001629 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001630 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001631 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001632 fHWBounds.fViewportRect = vp;
1633 }
1634 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001635 if (NULL == bound || !bound->isEmpty()) {
1636 rt->flagAsNeedingResolve(bound);
1637 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001638}
1639
twiz@google.com0f31ca72011-03-18 17:38:11 +00001640GrGLenum gPrimitiveType2GLMode[] = {
1641 GR_GL_TRIANGLES,
1642 GR_GL_TRIANGLE_STRIP,
1643 GR_GL_TRIANGLE_FAN,
1644 GR_GL_POINTS,
1645 GR_GL_LINES,
1646 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001647};
1648
bsalomon@google.comd302f142011-03-03 13:54:13 +00001649#define SWAP_PER_DRAW 0
1650
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001651#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001652 #if GR_MAC_BUILD
1653 #include <AGL/agl.h>
1654 #elif GR_WIN32_BUILD
1655 void SwapBuf() {
1656 DWORD procID = GetCurrentProcessId();
1657 HWND hwnd = GetTopWindow(GetDesktopWindow());
1658 while(hwnd) {
1659 DWORD wndProcID = 0;
1660 GetWindowThreadProcessId(hwnd, &wndProcID);
1661 if(wndProcID == procID) {
1662 SwapBuffers(GetDC(hwnd));
1663 }
1664 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1665 }
1666 }
1667 #endif
1668#endif
1669
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001670void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1671 uint32_t startVertex,
1672 uint32_t startIndex,
1673 uint32_t vertexCount,
1674 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001675 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1676
twiz@google.com0f31ca72011-03-18 17:38:11 +00001677 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001678
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001679 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1680 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1681
1682 // our setupGeometry better have adjusted this to zero since
1683 // DrawElements always draws from the begining of the arrays for idx 0.
1684 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001685
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001686 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1687 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001688#if SWAP_PER_DRAW
1689 glFlush();
1690 #if GR_MAC_BUILD
1691 aglSwapBuffers(aglGetCurrentContext());
1692 int set_a_break_pt_here = 9;
1693 aglSwapBuffers(aglGetCurrentContext());
1694 #elif GR_WIN32_BUILD
1695 SwapBuf();
1696 int set_a_break_pt_here = 9;
1697 SwapBuf();
1698 #endif
1699#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001700}
1701
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001702void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1703 uint32_t startVertex,
1704 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001705 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1706
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001707 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1708
1709 // our setupGeometry better have adjusted this to zero.
1710 // DrawElements doesn't take an offset so we always adjus the startVertex.
1711 GrAssert(0 == startVertex);
1712
1713 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1714 // account for startVertex in the DrawElements case. So we always
1715 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001716 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001717#if SWAP_PER_DRAW
1718 glFlush();
1719 #if GR_MAC_BUILD
1720 aglSwapBuffers(aglGetCurrentContext());
1721 int set_a_break_pt_here = 9;
1722 aglSwapBuffers(aglGetCurrentContext());
1723 #elif GR_WIN32_BUILD
1724 SwapBuf();
1725 int set_a_break_pt_here = 9;
1726 SwapBuf();
1727 #endif
1728#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001729}
1730
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001731void GrGpuGL::onGpuStencilPath(const GrPath&, GrPathFill) {
1732 GrCrash("Not implemented yet. Should not get here.");
1733}
1734
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001735void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1736
1737 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001738
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001739 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001740 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001741 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001742 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1743 rt->renderFBOID()));
1744 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1745 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001746 // make sure we go through flushRenderTarget() since we've modified
1747 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001748 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001749 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001750 const GrIRect dirtyRect = rt->getResolveRect();
1751 GrGLIRect r;
1752 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1753 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001754
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001755 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001756 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001757#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001758 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1759 GL_CALL(Scissor(r.fLeft, r.fBottom,
1760 r.fWidth, r.fHeight));
1761 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001762 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001764#else
1765 this->enableScissoring(dirtyRect);
1766 GL_CALL(ResolveMultisampleFramebuffer());
1767#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001769 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001770 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001771 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1772 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001773 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001774 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001775 int right = r.fLeft + r.fWidth;
1776 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001777 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1778 r.fLeft, r.fBottom, right, top,
1779 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001781 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001782 }
1783}
1784
bsalomon@google.com411dad02012-06-05 20:24:20 +00001785namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001786
bsalomon@google.com411dad02012-06-05 20:24:20 +00001787GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1788 static const GrGLenum gTable[] = {
1789 GR_GL_ALWAYS, // kAlways_StencilFunc
1790 GR_GL_NEVER, // kNever_StencilFunc
1791 GR_GL_GREATER, // kGreater_StencilFunc
1792 GR_GL_GEQUAL, // kGEqual_StencilFunc
1793 GR_GL_LESS, // kLess_StencilFunc
1794 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1795 GR_GL_EQUAL, // kEqual_StencilFunc,
1796 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1797 };
1798 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1799 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1800 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1801 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1802 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1803 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1804 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1805 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1806 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1807 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1808
1809 return gTable[basicFunc];
1810}
1811
1812GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1813 static const GrGLenum gTable[] = {
1814 GR_GL_KEEP, // kKeep_StencilOp
1815 GR_GL_REPLACE, // kReplace_StencilOp
1816 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1817 GR_GL_INCR, // kIncClamp_StencilOp
1818 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1819 GR_GL_DECR, // kDecClamp_StencilOp
1820 GR_GL_ZERO, // kZero_StencilOp
1821 GR_GL_INVERT, // kInvert_StencilOp
1822 };
1823 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1824 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1825 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1826 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1827 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1828 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1829 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1830 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1831 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1832 GrAssert((unsigned) op < kStencilOpCount);
1833 return gTable[op];
1834}
1835
1836void set_gl_stencil(const GrGLInterface* gl,
1837 GrGLenum glFace,
1838 GrStencilFunc func,
1839 GrStencilOp failOp,
1840 GrStencilOp passOp,
1841 unsigned int ref,
1842 unsigned int mask,
1843 unsigned int writeMask) {
1844 GrGLenum glFunc = gr_to_gl_stencil_func(func);
1845 GrGLenum glFailOp = gr_to_gl_stencil_op(failOp);
1846 GrGLenum glPassOp = gr_to_gl_stencil_op(passOp);
1847
1848 if (GR_GL_FRONT_AND_BACK == glFace) {
1849 // we call the combined func just in case separate stencil is not
1850 // supported.
1851 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1852 GR_GL_CALL(gl, StencilMask(writeMask));
1853 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1854 } else {
1855 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1856 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1857 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1858 }
1859}
1860}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861
reed@google.comac10a2d2010-12-22 21:39:39 +00001862void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001863 const GrDrawState& drawState = this->getDrawState();
1864
reed@google.comac10a2d2010-12-22 21:39:39 +00001865 // use stencil for clipping if clipping is enabled and the clip
1866 // has been written into the stencil.
bsalomon@google.com411dad02012-06-05 20:24:20 +00001867 GrClipMaskManager::StencilClipMode clipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001868 if (fClipMaskManager.isClipInStencil() &&
1869 drawState.isClipState()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001870 clipMode = GrClipMaskManager::kRespectClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001871 // We can't be modifying the clip and respecting it at the same time.
1872 GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1873 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001874 clipMode = GrClipMaskManager::kModifyClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001875 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001876 clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode;
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001877 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001878
bsalomon@google.com411dad02012-06-05 20:24:20 +00001879 // The caller may not be using the stencil buffer but we may need to enable
1880 // it in order to respect a stencil clip.
1881 const GrStencilSettings* settings = &drawState.getStencil();
1882 if (settings->isDisabled() &&
1883 GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) {
1884 settings = GetClipStencilSettings();
1885 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001886
bsalomon@google.com411dad02012-06-05 20:24:20 +00001887 // TODO: dynamically attach a stencil buffer
1888 int stencilBits = 0;
1889 GrStencilBuffer* stencilBuffer =
1890 drawState.getRenderTarget()->getStencilBuffer();
1891 if (NULL != stencilBuffer) {
1892 stencilBits = stencilBuffer->bits();
1893 }
1894 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.comd302f142011-03-03 13:54:13 +00001895
bsalomon@google.com411dad02012-06-05 20:24:20 +00001896 bool updateStencilSettings = stencilBits > 0 &&
1897 ((fHWStencilSettings != *settings) ||
1898 (fHWStencilClipMode != clipMode));
1899 if (updateStencilSettings) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001901 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001902 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001903 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001904 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001905 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001906 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1907 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1908 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1909 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1910 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1911 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1912 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1913 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001914 }
1915 #endif
bsalomon@google.comd302f142011-03-03 13:54:13 +00001916
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001917 unsigned int frontRef = settings->frontFuncRef();
1918 unsigned int frontMask = settings->frontFuncMask();
1919 unsigned int frontWriteMask = settings->frontWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001920
bsalomon@google.com411dad02012-06-05 20:24:20 +00001921 GrStencilFunc frontFunc =
1922 fClipMaskManager.adjustStencilParams(settings->frontFunc(),
1923 clipMode,
1924 stencilBits,
1925 &frontRef,
1926 &frontMask,
1927 &frontWriteMask);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001928 if (this->getCaps().fTwoSidedStencilSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001929 unsigned int backRef = settings->backFuncRef();
1930 unsigned int backMask = settings->backFuncMask();
1931 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001932
bsalomon@google.com411dad02012-06-05 20:24:20 +00001933 GrStencilFunc backFunc =
1934 fClipMaskManager.adjustStencilParams(settings->frontFunc(),
1935 clipMode,
1936 stencilBits,
1937 &backRef,
1938 &backMask,
1939 &backWriteMask);
1940 set_gl_stencil(this->glInterface(),
1941 GR_GL_FRONT,
1942 frontFunc,
1943 settings->frontFailOp(),
1944 settings->frontPassOp(),
1945 frontRef,
1946 frontMask,
1947 frontWriteMask);
1948 set_gl_stencil(this->glInterface(),
1949 GR_GL_BACK,
1950 backFunc,
1951 settings->backFailOp(),
1952 settings->backPassOp(),
1953 backRef,
1954 backMask,
1955 backWriteMask);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001956 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001957 set_gl_stencil(this->glInterface(),
1958 GR_GL_FRONT_AND_BACK,
1959 frontFunc,
1960 settings->frontFailOp(),
1961 settings->frontPassOp(),
1962 frontRef,
1963 frontMask,
1964 frontWriteMask);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001965 }
1966 }
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001967 fHWStencilSettings = *settings;
1968 fHWStencilClipMode = clipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001969 }
1970}
1971
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001972void GrGpuGL::flushAAState(bool isLines) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001973 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001974 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001975 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1976 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001977 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001978 bool smoothLines = false;
1979
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001980 if (isLines) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001981 smoothLines = this->willUseHWAALines();
1982 if (smoothLines) {
1983 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1984 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1985 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1986 // must disable msaa to use line smoothing
1987 if (rt->isMultisampled() &&
1988 kNo_TriState != fHWAAState.fMSAAEnabled) {
1989 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1990 fHWAAState.fMSAAEnabled = kNo_TriState;
1991 }
1992 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001993 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001994 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1995 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1996 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1997 }
1998 }
1999 }
2000 if (!smoothLines && rt->isMultisampled()) {
2001 if (this->getDrawState().isHWAntialiasState()) {
2002 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2003 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2004 fHWAAState.fMSAAEnabled = kYes_TriState;
2005 }
2006 } else {
2007 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2008 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2009 fHWAAState.fMSAAEnabled = kNo_TriState;
2010 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002011 }
2012 }
2013 }
2014}
2015
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002016void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002017 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002018 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002019 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002020 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002021 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002022 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002023 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002024 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2025 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2026 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2027 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2028 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2029 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002030 }
2031 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002032 // any optimization to disable blending should
2033 // have already been applied and tweaked the coeffs
2034 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002035 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2036 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002037 if (blendOff) {
2038 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002039 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002040 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002041 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002042 } else {
2043 if (kYes_TriState != fHWBlendState.fEnabled) {
2044 GL_CALL(Enable(GR_GL_BLEND));
2045 fHWBlendState.fEnabled = kYes_TriState;
2046 }
2047 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2048 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002049 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2050 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002051 fHWBlendState.fSrcCoeff = srcCoeff;
2052 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002053 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002054 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002055 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2056 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002057 (!fHWBlendState.fConstColorValid ||
2058 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002059
2060 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002061 GrColorUnpackR(blendConst) / 255.f,
2062 GrColorUnpackG(blendConst) / 255.f,
2063 GrColorUnpackB(blendConst) / 255.f,
2064 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002065 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002066 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002067 fHWBlendState.fConstColor = blendConst;
2068 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002069 }
2070 }
2071 }
2072}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002073namespace {
2074
2075unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002076 switch (filter) {
2077 case GrSamplerState::kBilinear_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002078 return GR_GL_LINEAR;
2079 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002080 return GR_GL_NEAREST;
2081 default:
2082 GrAssert(!"Unknown filter type");
2083 return GR_GL_LINEAR;
2084 }
2085}
2086
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002087// get_swizzle is only called from this .cpp so it is OK to inline it here
2088inline const GrGLenum* get_swizzle(GrPixelConfig config,
2089 const GrSamplerState& sampler,
2090 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002091 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002092 if (glCaps.textureRedSupport()) {
2093 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2094 GR_GL_RED, GR_GL_RED };
2095 return gRedSmear;
2096 } else {
2097 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2098 GR_GL_ALPHA, GR_GL_ALPHA };
2099 return gAlphaSmear;
2100 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002101 } else if (sampler.swapsRAndB()) {
2102 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2103 GR_GL_RED, GR_GL_ALPHA };
2104 return gRedBlueSwap;
2105 } else {
2106 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2107 GR_GL_BLUE, GR_GL_ALPHA };
2108 return gStraight;
2109 }
2110}
2111
2112void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002113 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2114 GR_GL_TEXTURE_SWIZZLE_RGBA,
2115 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002116}
2117}
2118
bsalomon@google.com4c883782012-06-04 19:05:11 +00002119void GrGpuGL::flushBoundTextureAndParams(int stage) {
2120 GrDrawState* drawState = this->drawState();
2121
2122 GrGLTexture* nextTexture =
2123 static_cast<GrGLTexture*>(drawState->getTexture(stage));
2124
2125 // true for now, but maybe not with GrEffect.
2126 GrAssert(NULL != nextTexture);
2127 // if we created a rt/tex and rendered to it without using a
2128 // texture and now we're texturing from the rt it will still be
2129 // the last bound texture, but it needs resolving. So keep this
2130 // out of the "last != next" check.
2131 GrGLRenderTarget* texRT =
2132 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2133 if (NULL != texRT) {
2134 this->onResolveRenderTarget(texRT);
2135 }
2136
2137 if (fHWBoundTextures[stage] != nextTexture) {
2138 this->setTextureUnit(stage);
2139 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002140 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2141 fHWBoundTextures[stage] = nextTexture;
2142 }
2143
2144 const GrSamplerState& sampler = drawState->getSampler(stage);
2145 ResetTimestamp timestamp;
2146 const GrGLTexture::TexParams& oldTexParams =
2147 nextTexture->getCachedTexParams(&timestamp);
2148 bool setAll = timestamp < this->getResetTimestamp();
2149 GrGLTexture::TexParams newTexParams;
2150
2151 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
2152
2153 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
2154 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2155 newTexParams.fWrapT = wraps[sampler.getWrapY()];
2156 memcpy(newTexParams.fSwizzleRGBA,
2157 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
2158 sizeof(newTexParams.fSwizzleRGBA));
2159 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2160 this->setTextureUnit(stage);
2161 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2162 GR_GL_TEXTURE_MAG_FILTER,
2163 newTexParams.fFilter));
2164 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2165 GR_GL_TEXTURE_MIN_FILTER,
2166 newTexParams.fFilter));
2167 }
2168 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2169 this->setTextureUnit(stage);
2170 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2171 GR_GL_TEXTURE_WRAP_S,
2172 newTexParams.fWrapS));
2173 }
2174 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2175 this->setTextureUnit(stage);
2176 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2177 GR_GL_TEXTURE_WRAP_T,
2178 newTexParams.fWrapT));
2179 }
2180 if (this->glCaps().textureSwizzleSupport() &&
2181 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2182 oldTexParams.fSwizzleRGBA,
2183 sizeof(newTexParams.fSwizzleRGBA)))) {
2184 this->setTextureUnit(stage);
2185 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2186 this->glInterface());
2187 }
2188 nextTexture->setCachedTexParams(newTexParams,
2189 this->getResetTimestamp());
2190}
2191
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002192void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002193
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002194 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002195
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002196 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002197 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002198 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002199 fHWDitherEnabled = kYes_TriState;
2200 }
2201 } else {
2202 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002203 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002204 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002205 }
2206 }
2207
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002208 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002209 if (kNo_TriState != fHWWriteToColor) {
2210 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2211 GR_GL_FALSE, GR_GL_FALSE));
2212 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002213 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002214 } else {
2215 if (kYes_TriState != fHWWriteToColor) {
2216 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2217 fHWWriteToColor = kYes_TriState;
2218 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002219 }
2220
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002221 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002222 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002223 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002224 GL_CALL(Enable(GR_GL_CULL_FACE));
2225 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002226 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002227 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002228 GL_CALL(Enable(GR_GL_CULL_FACE));
2229 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002230 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002231 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002232 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002233 break;
2234 default:
2235 GrCrash("Unknown draw face.");
2236 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002237 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002238 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002239}
2240
2241void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002242 if (fHWGeometryState.fVertexBuffer != buffer) {
2243 fHWGeometryState.fArrayPtrsDirty = true;
2244 fHWGeometryState.fVertexBuffer = buffer;
2245 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002246}
2247
2248void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002249 if (fHWGeometryState.fVertexBuffer == buffer) {
2250 // deleting bound buffer does implied bind to 0
2251 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002252 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002253 }
2254}
2255
2256void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002257 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002258}
2259
2260void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002261 if (fHWGeometryState.fIndexBuffer == buffer) {
2262 // deleting bound buffer does implied bind to 0
2263 fHWGeometryState.fIndexBuffer = NULL;
2264 }
2265}
2266
reed@google.comac10a2d2010-12-22 21:39:39 +00002267void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2268 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002269 GrDrawState* drawState = this->drawState();
2270 if (drawState->getRenderTarget() == renderTarget) {
2271 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002272 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002273 if (fHWBoundRenderTarget == renderTarget) {
2274 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002275 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002276}
2277
2278void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002279 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002280 GrDrawState* drawState = this->drawState();
2281 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002282 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002283 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002284 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002285 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002286 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002287 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002289}
2290
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002291bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2292 bool getSizedInternalFormat,
2293 GrGLenum* internalFormat,
2294 GrGLenum* externalFormat,
2295 GrGLenum* externalType) {
2296 GrGLenum dontCare;
2297 if (NULL == internalFormat) {
2298 internalFormat = &dontCare;
2299 }
2300 if (NULL == externalFormat) {
2301 externalFormat = &dontCare;
2302 }
2303 if (NULL == externalType) {
2304 externalType = &dontCare;
2305 }
2306
reed@google.comac10a2d2010-12-22 21:39:39 +00002307 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002308 case kRGBA_8888_PM_GrPixelConfig:
2309 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002310 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002311 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002312 if (getSizedInternalFormat) {
2313 *internalFormat = GR_GL_RGBA8;
2314 } else {
2315 *internalFormat = GR_GL_RGBA;
2316 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002317 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002318 break;
2319 case kBGRA_8888_PM_GrPixelConfig:
2320 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002321 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002322 return false;
2323 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002324 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002325 if (getSizedInternalFormat) {
2326 *internalFormat = GR_GL_BGRA8;
2327 } else {
2328 *internalFormat = GR_GL_BGRA;
2329 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002330 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002331 if (getSizedInternalFormat) {
2332 *internalFormat = GR_GL_RGBA8;
2333 } else {
2334 *internalFormat = GR_GL_RGBA;
2335 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002336 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002337 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002338 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002339 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002340 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002341 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002342 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002343 if (getSizedInternalFormat) {
2344 if (this->glBinding() == kDesktop_GrGLBinding) {
2345 return false;
2346 } else {
2347 *internalFormat = GR_GL_RGB565;
2348 }
2349 } else {
2350 *internalFormat = GR_GL_RGB;
2351 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002352 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002353 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002354 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002355 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002356 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002357 if (getSizedInternalFormat) {
2358 *internalFormat = GR_GL_RGBA4;
2359 } else {
2360 *internalFormat = GR_GL_RGBA;
2361 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002362 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002363 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002364 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002365 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002366 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002367 // glCompressedTexImage doesn't take external params
2368 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002369 // no sized/unsized internal format distinction here
2370 *internalFormat = GR_GL_PALETTE8_RGBA8;
2371 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002372 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002373 } else {
2374 return false;
2375 }
2376 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002377 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002378 if (this->glCaps().textureRedSupport()) {
2379 *internalFormat = GR_GL_RED;
2380 *externalFormat = GR_GL_RED;
2381 if (getSizedInternalFormat) {
2382 *internalFormat = GR_GL_R8;
2383 } else {
2384 *internalFormat = GR_GL_RED;
2385 }
2386 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002387 } else {
2388 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002389 *externalFormat = GR_GL_ALPHA;
2390 if (getSizedInternalFormat) {
2391 *internalFormat = GR_GL_ALPHA8;
2392 } else {
2393 *internalFormat = GR_GL_ALPHA;
2394 }
2395 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002396 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002397 break;
2398 default:
2399 return false;
2400 }
2401 return true;
2402}
2403
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002404void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002405 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002406 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002407 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002408 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002409 }
2410}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002411
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002412void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002413 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002414 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002415 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002416 }
2417}
2418
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002419void GrGpuGL::setBuffers(bool indexed,
2420 int* extraVertexOffset,
2421 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002422
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002423 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002424
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002425 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2426
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002427 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002428 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002429 case kBuffer_GeometrySrcType:
2430 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002431 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002432 break;
2433 case kArray_GeometrySrcType:
2434 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002435 this->finalizeReservedVertices();
2436 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2437 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002438 break;
2439 default:
2440 vbuf = NULL; // suppress warning
2441 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002442 }
2443
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002444 GrAssert(NULL != vbuf);
2445 GrAssert(!vbuf->isLocked());
2446 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002447 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002448 fHWGeometryState.fArrayPtrsDirty = true;
2449 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002450 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002451
2452 if (indexed) {
2453 GrAssert(NULL != extraIndexOffset);
2454
2455 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002456 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002457 case kBuffer_GeometrySrcType:
2458 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002459 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460 break;
2461 case kArray_GeometrySrcType:
2462 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002463 this->finalizeReservedIndices();
2464 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2465 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002466 break;
2467 default:
2468 ibuf = NULL; // suppress warning
2469 GrCrash("Unknown geometry src type!");
2470 }
2471
2472 GrAssert(NULL != ibuf);
2473 GrAssert(!ibuf->isLocked());
2474 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002475 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002476 fHWGeometryState.fIndexBuffer = ibuf;
2477 }
2478 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002479}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002480