blob: 88170682078d5966db59a553cbcbd3cc379000e2 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000011#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000012#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
twiz@google.com0f31ca72011-03-18 17:38:11 +000014static const GrGLuint GR_MAX_GLUINT = ~0;
15static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com0b77d682011-08-19 13:28:54 +000017#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000018#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019
bsalomon@google.com316f99232011-01-13 21:28:12 +000020// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000022static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000026#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
27 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
30#else
31 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
34#endif
35
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000036
37///////////////////////////////////////////////////////////////////////////////
38
twiz@google.com0f31ca72011-03-18 17:38:11 +000039static const GrGLenum gXfermodeCoeff2Blend[] = {
40 GR_GL_ZERO,
41 GR_GL_ONE,
42 GR_GL_SRC_COLOR,
43 GR_GL_ONE_MINUS_SRC_COLOR,
44 GR_GL_DST_COLOR,
45 GR_GL_ONE_MINUS_DST_COLOR,
46 GR_GL_SRC_ALPHA,
47 GR_GL_ONE_MINUS_SRC_ALPHA,
48 GR_GL_DST_ALPHA,
49 GR_GL_ONE_MINUS_DST_ALPHA,
50 GR_GL_CONSTANT_COLOR,
51 GR_GL_ONE_MINUS_CONSTANT_COLOR,
52 GR_GL_CONSTANT_ALPHA,
53 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000054
55 // extended blend coeffs
56 GR_GL_SRC1_COLOR,
57 GR_GL_ONE_MINUS_SRC1_COLOR,
58 GR_GL_SRC1_ALPHA,
59 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000060};
61
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000063 static const bool gCoeffReferencesBlendConst[] = {
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 true,
75 true,
76 true,
77 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000078
79 // extended blend coeffs
80 false,
81 false,
82 false,
83 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000084 };
85 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000086 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
87
88 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
89 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
90 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
91 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
92 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
93 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
94 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
95 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
96 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
97 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
98 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
99 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
100 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
101 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
102
103 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
104 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
105 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
106 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
107
108 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
109 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000110}
111
reed@google.comac10a2d2010-12-22 21:39:39 +0000112///////////////////////////////////////////////////////////////////////////////
113
bsalomon@google.comd302f142011-03-03 13:54:13 +0000114void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
115 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000116 GrMatrix* matrix) {
117 GrAssert(NULL != texture);
118 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000119 GrGLTexture::Orientation orientation = texture->orientation();
120 if (GrGLTexture::kBottomUp_Orientation == orientation) {
121 GrMatrix invY;
122 invY.setAll(GR_Scalar1, 0, 0,
123 0, -GR_Scalar1, GR_Scalar1,
124 0, 0, GrMatrix::I()[8]);
125 matrix->postConcat(invY);
126 } else {
127 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
128 }
129}
130
bsalomon@google.comd302f142011-03-03 13:54:13 +0000131bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000132 const GrSamplerState& sampler) {
133 GrAssert(NULL != texture);
134 if (!sampler.getMatrix().isIdentity()) {
135 return false;
136 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000137 GrGLTexture::Orientation orientation = texture->orientation();
138 if (GrGLTexture::kBottomUp_Orientation == orientation) {
139 return false;
140 } else {
141 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
142 }
143 return true;
144}
145
146///////////////////////////////////////////////////////////////////////////////
147
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000148static bool gPrintStartupSpew;
149
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000150static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000151
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000152 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000153
twiz@google.com0f31ca72011-03-18 17:38:11 +0000154 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000155 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
156 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000157 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000158 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
159 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000160 // some implementations require texture to be mip-map complete before
161 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000162 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000163 GR_GL_TEXTURE_MIN_FILTER,
164 GR_GL_NEAREST));
165 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
166 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
167 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
168 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
169 GR_GL_COLOR_ATTACHMENT0,
170 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000171 GrGLenum status;
172 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000173 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
174 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000175
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000176 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000177}
178
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000179GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
180
181 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000182
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000183 fillInConfigRenderableTable();
184
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000185 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000186
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000187 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000188
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000189 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000190 const GrGLubyte* ext;
191 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000192 const GrGLubyte* vendor;
193 const GrGLubyte* renderer;
194 const GrGLubyte* version;
195 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
196 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
197 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000198 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
199 this);
200 GrPrintf("------ VENDOR %s\n", vendor);
201 GrPrintf("------ RENDERER %s\n", renderer);
202 GrPrintf("------ VERSION %s\n", version);
203 GrPrintf("------ EXTENSIONS\n %s \n", ext);
204 }
205
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000206 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000207
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000208 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000209
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000210 fProgramData = NULL;
211 fProgramCache = new ProgramCache(this->glContextInfo());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000212
213#if 0
214 this->programUnitTest();
215#endif
216
bsalomon@google.comfe676522011-06-17 18:12:21 +0000217 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000218 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000219}
220
221GrGpuGL::~GrGpuGL() {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000222 if (fProgramData && 0 != fHWProgramID) {
223 // detach the current program so there is no confusion on OpenGL's part
224 // that we want it to be deleted
225 GrAssert(fHWProgramID == fProgramData->fProgramID);
226 GL_CALL(UseProgram(0));
227 }
228
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000229 delete fProgramCache;
230 fProgramCache = NULL;
231 fProgramData = NULL;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000232
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000233 // This must be called by before the GrDrawTarget destructor
234 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000235 // This subclass must do this before the base class destructor runs
236 // since we will unref the GrGLInterface.
237 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000238}
239
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000240///////////////////////////////////////////////////////////////////////////////
241
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000242void GrGpuGL::initCaps() {
243 GrGLint maxTextureUnits;
244 // check FS and fixed-function texture unit limits
245 // we only use textures in the fragment stage currently.
246 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000247 const GrGLInterface* gl = this->glInterface();
248 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000249 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250
251 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000252 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000254 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000255 for (int i = 0; i < numFormats; ++i) {
256 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
257 fCaps.f8BitPaletteSupport = true;
258 break;
259 }
260 }
261
262 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000263 // we could also look for GL_ATI_separate_stencil extension or
264 // GL_EXT_stencil_two_side but they use different function signatures
265 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000266 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000267 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000268 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000269 this->hasExtension("GL_EXT_stencil_wrap");
270 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000271 // ES 2 has two sided stencil and stencil wrap
272 fCaps.fTwoSidedStencilSupport = true;
273 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000274 }
275
276 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000277 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
278 // extension includes glMapBuffer.
279 } else {
280 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
281 }
282
283 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000284 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000285 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
286 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000287 } else {
288 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000289 }
290 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000291 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000292 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000293 }
294
295 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
296
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000297 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
298 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000299 // Our render targets are always created with textures as the color
300 // attachment, hence this min:
301 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
302
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000303 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000304
305 // Enable supported shader-related caps
306 if (kDesktop_GrGLBinding == this->glBinding()) {
307 fCaps.fDualSourceBlendingSupport =
308 this->glVersion() >= GR_GL_VER(3,3) ||
309 this->hasExtension("GL_ARB_blend_func_extended");
310 fCaps.fShaderDerivativeSupport = true;
311 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
312 fCaps.fGeometryShaderSupport =
313 this->glVersion() >= GR_GL_VER(3,2) &&
314 this->glslGeneration() >= k150_GrGLSLGeneration;
315 } else {
316 fCaps.fShaderDerivativeSupport =
317 this->hasExtension("GL_OES_standard_derivatives");
318 }
319
320 GR_GL_GetIntegerv(this->glInterface(),
321 GR_GL_MAX_VERTEX_ATTRIBS,
322 &fMaxVertexAttribs);
323
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000324}
325
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000326void GrGpuGL::fillInConfigRenderableTable() {
327
328 // OpenGL < 3.0
329 // no support for render targets unless the GL_ARB_framebuffer_object
330 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
331 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
332 // probably don't get R8 in this case.
333
334 // OpenGL 3.0
335 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
336 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
337
338 // >= OpenGL 3.1
339 // base color renderable: RED, RG, RGB, and RGBA
340 // sized derivatives: R8, RGBA4, RGBA8
341 // if the GL_ARB_compatibility extension is supported then we get back
342 // support for GL_ALPHA and ALPHA8
343
344 // GL_EXT_bgra adds BGRA render targets to any version
345
346 // ES 2.0
347 // color renderable: RGBA4, RGB5_A1, RGB565
348 // GL_EXT_texture_rg adds support for R8 as a color render target
349 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
350 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
351 // added BGRA support
352
353 if (kDesktop_GrGLBinding == this->glBinding()) {
354 // Post 3.0 we will get R8
355 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
356 if (this->glVersion() >= GR_GL_VER(3,0) ||
357 this->hasExtension("GL_ARB_framebuffer_object")) {
358 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
359 }
360 } else {
361 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000362 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
363 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000364 }
365
366 if (kDesktop_GrGLBinding != this->glBinding()) {
367 // only available in ES
368 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
369 }
370
371 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
372 // GL_EXT_framebuffer_object for FBO support. Both of these
373 // allow RGBA4 render targets so this is always supported.
374 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
375
376 if (this->glCaps().rgba8RenderbufferSupport()) {
377 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
378 }
379
380 if (this->glCaps().bgraFormatSupport()) {
381 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
382 }
383
384 // the un-premultiplied formats just inherit the premultiplied setting
385 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
386 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
387 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
388 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
389}
390
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000391bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
392 if (kUnknown_CanPreserveUnpremulRoundtrip ==
393 fCanPreserveUnpremulRoundtrip) {
394
395 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
396 uint32_t* srcData = data.get();
397 uint32_t* firstRead = data.get() + 256 * 256;
398 uint32_t* secondRead = data.get() + 2 * 256 * 256;
399
400 for (int y = 0; y < 256; ++y) {
401 for (int x = 0; x < 256; ++x) {
402 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
403 color[3] = y;
404 color[2] = x;
405 color[1] = x;
406 color[0] = x;
407 }
408 }
409
410 // We have broader support for read/write pixels on render targets
411 // than on textures.
412 GrTextureDesc dstDesc;
413 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
414 kNoStencil_GrTextureFlagBit;
415 dstDesc.fWidth = 256;
416 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000417 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000418 dstDesc.fSampleCnt = 0;
419
420 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
421 if (!dstTex.get()) {
422 return false;
423 }
424 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
425 GrAssert(NULL != rt);
426
427 bool failed = true;
428 static const UnpremulConversion gMethods[] = {
429 kUpOnWrite_DownOnRead_UnpremulConversion,
430 kDownOnWrite_UpOnRead_UnpremulConversion,
431 };
432
433 // pretend that we can do the roundtrip to avoid recursive calls to
434 // this function
435 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
436 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
437 fUnpremulConversion = gMethods[i];
438 rt->writePixels(0, 0,
439 256, 256,
440 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
441 rt->readPixels(0, 0,
442 256, 256,
443 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
444 rt->writePixels(0, 0,
445 256, 256,
446 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
447 rt->readPixels(0, 0,
448 256, 256,
449 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
450 failed = false;
451 for (int j = 0; j < 256 * 256; ++j) {
452 if (firstRead[j] != secondRead[j]) {
453 failed = true;
454 break;
455 }
456 }
457 }
458 fCanPreserveUnpremulRoundtrip = failed ?
459 kNo_CanPreserveUnpremulRoundtrip :
460 kYes_CanPreserveUnpremulRoundtrip;
461 }
462
463 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
464 return true;
465 } else {
466 return false;
467 }
468}
469
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000470GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000471 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
472 return GrPixelConfigSwapRAndB(config);
473 } else {
474 return config;
475 }
476}
477
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000478GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000479 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000480 return GrPixelConfigSwapRAndB(config);
481 } else {
482 return config;
483 }
484}
485
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000486bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
487 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
488}
489
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000490void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000491 if (gPrintStartupSpew && !fPrintedCaps) {
492 fPrintedCaps = true;
493 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000494 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000495 }
496
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000497 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000498 GL_CALL(Disable(GR_GL_DEPTH_TEST));
499 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000500
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000501 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
502 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000504 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000505 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000506 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000507 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
508 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
509 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
510 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
511 GL_CALL(Disable(GR_GL_COLOR_TABLE));
512 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
513 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
514 // Since ES doesn't support glPointSize at all we always use the VS to
515 // set the point size
516 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
517
518 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
519 // currently part of our gl interface. There are probably others as
520 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000521 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000522 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000523 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000524
reed@google.comac10a2d2010-12-22 21:39:39 +0000525 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000526 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000527
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000528 // invalid
529 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000531 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000532
tomhudson@google.com93813632011-10-27 20:21:16 +0000533 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000534 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000535 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000536
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000537 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000538 // set to true to force disableScissor to make a GL call.
539 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000540 this->disableScissor();
541
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000542 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000543
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000544 fHWStencilSettings.invalidate();
545 fHWStencilClipMode = kInvalid_StencilClipMode;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000546
547 // TODO: I believe this should actually go in GrGpu::onResetContext
548 // rather than here
549 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000550
551 fHWGeometryState.fIndexBuffer = NULL;
552 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000553
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000554 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000555
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000556 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000557
558 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000559 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000560 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
561 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000562 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000563 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
564 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000565 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000566 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
567 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000568 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000569 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
570 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000571
572 fHWGeometryState.fVertexOffset = ~0;
573
574 // Third party GL code may have left vertex attributes enabled. Some GL
575 // implementations (osmesa) may read vetex attributes that are not required
576 // by the current shader. Therefore, we have to ensure that only the
577 // attributes we require for the current draw are enabled or we may cause an
578 // invalid read.
579
580 // Disable all vertex layout bits so that next flush will assume all
581 // optional vertex attributes are disabled.
582 fHWGeometryState.fVertexLayout = 0;
583
584 // We always use the this attribute and assume it is always enabled.
585 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
586 GL_CALL(EnableVertexAttribArray(posAttrIdx));
587 // Disable all other vertex attributes.
588 for (int va = 0; va < fMaxVertexAttribs; ++va) {
589 if (va != posAttrIdx) {
590 GL_CALL(DisableVertexAttribArray(va));
591 }
592 }
593
594 fHWProgramID = 0;
595 fHWConstAttribColor = GrColor_ILLEGAL;
596 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000597}
598
bsalomon@google.come269f212011-11-07 13:29:52 +0000599GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000600 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000601 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000602 return NULL;
603 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000604
bsalomon@google.com99621082011-11-15 16:47:16 +0000605 glTexDesc.fWidth = desc.fWidth;
606 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000607 glTexDesc.fConfig = desc.fConfig;
608 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
609 glTexDesc.fOwnsID = false;
610 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
611
612 GrGLTexture* texture = NULL;
613 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
614 GrGLRenderTarget::Desc glRTDesc;
615 glRTDesc.fRTFBOID = 0;
616 glRTDesc.fTexFBOID = 0;
617 glRTDesc.fMSColorRenderbufferID = 0;
618 glRTDesc.fOwnIDs = true;
619 glRTDesc.fConfig = desc.fConfig;
620 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000621 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
622 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000623 glTexDesc.fTextureID,
624 &glRTDesc)) {
625 return NULL;
626 }
627 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
628 } else {
629 texture = new GrGLTexture(this, glTexDesc);
630 }
631 if (NULL == texture) {
632 return NULL;
633 }
634
635 this->setSpareTextureUnit();
636 return texture;
637}
638
639GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
640 GrGLRenderTarget::Desc glDesc;
641 glDesc.fConfig = desc.fConfig;
642 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
643 glDesc.fMSColorRenderbufferID = 0;
644 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
645 glDesc.fSampleCnt = desc.fSampleCnt;
646 glDesc.fOwnIDs = false;
647 GrGLIRect viewport;
648 viewport.fLeft = 0;
649 viewport.fBottom = 0;
650 viewport.fWidth = desc.fWidth;
651 viewport.fHeight = desc.fHeight;
652
653 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
654 if (desc.fStencilBits) {
655 GrGLStencilBuffer::Format format;
656 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
657 format.fPacked = false;
658 format.fStencilBits = desc.fStencilBits;
659 format.fTotalBits = desc.fStencilBits;
660 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
661 0,
662 desc.fWidth,
663 desc.fHeight,
664 desc.fSampleCnt,
665 format);
666 tgt->setStencilBuffer(sb);
667 sb->unref();
668 }
669 return tgt;
670}
671
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000672////////////////////////////////////////////////////////////////////////////////
673
bsalomon@google.com6f379512011-11-16 20:36:03 +0000674void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
675 int left, int top, int width, int height,
676 GrPixelConfig config, const void* buffer,
677 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000678 if (NULL == buffer) {
679 return;
680 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000681 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
682
bsalomon@google.com6f379512011-11-16 20:36:03 +0000683 this->setSpareTextureUnit();
684 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
685 GrGLTexture::Desc desc;
686 desc.fConfig = glTex->config();
687 desc.fWidth = glTex->width();
688 desc.fHeight = glTex->height();
689 desc.fOrientation = glTex->orientation();
690 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000691
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000692 this->uploadTexData(desc, false,
693 left, top, width, height,
694 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000695}
696
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000697namespace {
698bool adjust_pixel_ops_params(int surfaceWidth,
699 int surfaceHeight,
700 size_t bpp,
701 int* left, int* top, int* width, int* height,
702 const void** data,
703 size_t* rowBytes) {
704 if (!*rowBytes) {
705 *rowBytes = *width * bpp;
706 }
707
708 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
709 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
710
711 if (!subRect.intersect(bounds)) {
712 return false;
713 }
714 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
715 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
716
717 *left = subRect.fLeft;
718 *top = subRect.fTop;
719 *width = subRect.width();
720 *height = subRect.height();
721 return true;
722}
723}
724
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000725bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
726 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000727 int left, int top, int width, int height,
728 GrPixelConfig dataConfig,
729 const void* data,
730 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000731 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000732
733 size_t bpp = GrBytesPerPixel(dataConfig);
734 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
735 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000736 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000737 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000738 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000739
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000740 // in case we need a temporary, trimmed copy of the src pixels
741 SkAutoSMalloc<128 * 128> tempStorage;
742
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000743 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000744 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000745 if (useTexStorage) {
746 if (kDesktop_GrGLBinding == this->glBinding()) {
747 // 565 is not a sized internal format on desktop GL. So on desktop
748 // with 565 we always use an unsized internal format to let the
749 // system pick the best sized format to convert the 565 data to.
750 // Since glTexStorage only allows sized internal formats we will
751 // instead fallback to glTexImage2D.
752 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
753 } else {
754 // ES doesn't allow paletted textures to be used with tex storage
755 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
756 }
757 }
758
759 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000760 GrGLenum externalFormat;
761 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000762 // glTexStorage requires sized internal formats on both desktop and ES. ES
763 // glTexImage requires an unsized format.
764 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
765 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000766 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000767 }
768
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000769 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000770 // paletted textures cannot be updated
771 return false;
772 }
773
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000774 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000775 * check whether to allocate a temporary buffer for flipping y or
776 * because our srcData has extra bytes past each row. If so, we need
777 * to trim those off here, since GL ES may not let us specify
778 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000779 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000780 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000781 bool swFlipY = false;
782 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000783 if (NULL != data) {
784 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000785 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000786 glFlipY = true;
787 } else {
788 swFlipY = true;
789 }
790 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000791 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000792 // can't use this for flipping, only non-neg values allowed. :(
793 if (rowBytes != trimRowBytes) {
794 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
795 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
796 restoreGLRowLength = true;
797 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000798 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000799 if (trimRowBytes != rowBytes || swFlipY) {
800 // copy data into our new storage, skipping the trailing bytes
801 size_t trimSize = height * trimRowBytes;
802 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000803 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000804 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000805 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000806 char* dst = (char*)tempStorage.reset(trimSize);
807 for (int y = 0; y < height; y++) {
808 memcpy(dst, src, trimRowBytes);
809 if (swFlipY) {
810 src -= rowBytes;
811 } else {
812 src += rowBytes;
813 }
814 dst += trimRowBytes;
815 }
816 // now point data to our copied version
817 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000818 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000819 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000820 if (glFlipY) {
821 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
822 }
823 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000824 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000825 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000826 if (isNewTexture &&
827 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000828 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000829 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000830 if (useTexStorage) {
831 // We never resize or change formats of textures. We don't use
832 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000833 GL_ALLOC_CALL(this->glInterface(),
834 TexStorage2D(GR_GL_TEXTURE_2D,
835 1, // levels
836 internalFormat,
837 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000838 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000839 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
840 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
841 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000842 GL_ALLOC_CALL(this->glInterface(),
843 CompressedTexImage2D(GR_GL_TEXTURE_2D,
844 0, // level
845 internalFormat,
846 desc.fWidth, desc.fHeight,
847 0, // border
848 imageSize,
849 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000850 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000851 GL_ALLOC_CALL(this->glInterface(),
852 TexImage2D(GR_GL_TEXTURE_2D,
853 0, // level
854 internalFormat,
855 desc.fWidth, desc.fHeight,
856 0, // border
857 externalFormat, externalType,
858 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000859 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000860 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000861 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000862 if (error != GR_GL_NO_ERROR) {
863 succeeded = false;
864 } else {
865 // if we have data and we used TexStorage to create the texture, we
866 // now upload with TexSubImage.
867 if (NULL != data && useTexStorage) {
868 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
869 0, // level
870 left, top,
871 width, height,
872 externalFormat, externalType,
873 data));
874 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000875 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000876 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000877 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000878 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000879 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000880 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
881 0, // level
882 left, top,
883 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000884 externalFormat, externalType, data));
885 }
886
887 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000888 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000889 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000890 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000891 if (glFlipY) {
892 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
893 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000894 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000895}
896
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000897namespace {
898bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
899 int sampleCount,
900 GrGLenum format,
901 int width, int height) {
902 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
903 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
904 bool created = false;
905 if (GrGLCaps::kNVDesktop_CoverageAAType ==
906 ctxInfo.caps().coverageAAType()) {
907 const GrGLCaps::MSAACoverageMode& mode =
908 ctxInfo.caps().getMSAACoverageMode(sampleCount);
909 GL_ALLOC_CALL(ctxInfo.interface(),
910 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
911 mode.fCoverageSampleCnt,
912 mode.fColorSampleCnt,
913 format,
914 width, height));
915 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
916 }
917 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000918 // glRBMS will fail if requested samples is > max samples.
919 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000920 GL_ALLOC_CALL(ctxInfo.interface(),
921 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
922 sampleCount,
923 format,
924 width, height));
925 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
926 }
927 return created;
928}
929}
930
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000931bool GrGpuGL::createRenderTargetObjects(int width, int height,
932 GrGLuint texID,
933 GrGLRenderTarget::Desc* desc) {
934 desc->fMSColorRenderbufferID = 0;
935 desc->fRTFBOID = 0;
936 desc->fTexFBOID = 0;
937 desc->fOwnIDs = true;
938
939 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940
bsalomon@google.comab15d612011-08-09 12:57:56 +0000941 GrGLenum msColorFormat = 0; // suppress warning
942
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000943 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000944 if (!desc->fTexFBOID) {
945 goto FAILED;
946 }
947
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948
949 // If we are using multisampling we will create two FBOS. We render
950 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000951 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000952 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000953 goto FAILED;
954 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000955 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
956 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000957 if (!desc->fRTFBOID ||
958 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000959 !this->configToGLFormats(desc->fConfig,
960 // GLES requires sized internal formats
961 kES2_GrGLBinding == this->glBinding(),
962 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000963 goto FAILED;
964 }
965 } else {
966 desc->fRTFBOID = desc->fTexFBOID;
967 }
968
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000969 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000970 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 if (desc->fRTFBOID != desc->fTexFBOID) {
972 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000973 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000974 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000975 if (!renderbuffer_storage_msaa(fGLContextInfo,
976 desc->fSampleCnt,
977 msColorFormat,
978 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000979 goto FAILED;
980 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000981 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
982 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000983 GR_GL_COLOR_ATTACHMENT0,
984 GR_GL_RENDERBUFFER,
985 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000986 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000987 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
988 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
989 goto FAILED;
990 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000991 fGLContextInfo.caps().markConfigAsValidColorAttachment(
992 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000993 }
994 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000995 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000996
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000997 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
998 GR_GL_COLOR_ATTACHMENT0,
999 GR_GL_TEXTURE_2D,
1000 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001001 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001002 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1003 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1004 goto FAILED;
1005 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001006 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007 }
1008
1009 return true;
1010
1011FAILED:
1012 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001013 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001014 }
1015 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001017 }
1018 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001019 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001020 }
1021 return false;
1022}
1023
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001024// good to set a break-point here to know when createTexture fails
1025static GrTexture* return_null_texture() {
1026// GrAssert(!"null texture");
1027 return NULL;
1028}
1029
1030#if GR_DEBUG
1031static size_t as_size_t(int x) {
1032 return x;
1033}
1034#endif
1035
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001036GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001037 const void* srcData,
1038 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001039
1040#if GR_COLLECT_STATS
1041 ++fStats.fTextureCreateCnt;
1042#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001043
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001044 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001045 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001046
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001047 // Attempt to catch un- or wrongly initialized sample counts;
1048 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1049
bsalomon@google.com99621082011-11-15 16:47:16 +00001050 glTexDesc.fWidth = desc.fWidth;
1051 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001052 glTexDesc.fConfig = desc.fConfig;
1053 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001054
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001055 glRTDesc.fMSColorRenderbufferID = 0;
1056 glRTDesc.fRTFBOID = 0;
1057 glRTDesc.fTexFBOID = 0;
1058 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001059 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001060
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001061 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001062
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001063 const Caps& caps = this->getCaps();
1064
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001065 // We keep GrRenderTargets in GL's normal orientation so that they
1066 // can be drawn to by the outside world without the client having
1067 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001068 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001069 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001070
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001071 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001072 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001073 desc.fSampleCnt) {
1074 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 }
1076
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001078 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1079 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001080 return return_null_texture();
1081 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001082 }
1083
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001084 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001085 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001086 // provides a hint about how this texture will be used
1087 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1088 GR_GL_TEXTURE_USAGE,
1089 GR_GL_FRAMEBUFFER_ATTACHMENT));
1090 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001091 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001092 return return_null_texture();
1093 }
1094
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001095 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001096 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001097
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001098 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1099 // drivers have a bug where an FBO won't be complete if it includes a
1100 // texture that is not mipmap complete (considering the filter in use).
1101 GrGLTexture::TexParams initialTexParams;
1102 // we only set a subset here so invalidate first
1103 initialTexParams.invalidate();
1104 initialTexParams.fFilter = GR_GL_NEAREST;
1105 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1106 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1108 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001109 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001110 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1111 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001112 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1114 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001115 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001116 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1117 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001118 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001119 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1120 glTexDesc.fWidth, glTexDesc.fHeight,
1121 desc.fConfig, srcData, rowBytes)) {
1122 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1123 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001124 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001125
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001126 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001127 if (renderTarget) {
1128#if GR_COLLECT_STATS
1129 ++fStats.fRenderTargetCreateCnt;
1130#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001131 // unbind the texture from the texture unit before binding it to the frame buffer
1132 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1133
bsalomon@google.com99621082011-11-15 16:47:16 +00001134 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1135 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 glTexDesc.fTextureID,
1137 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001138 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 return return_null_texture();
1140 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001141 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001143 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001145 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001146#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001147 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1148 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001149#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 return tex;
1151}
1152
1153namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001154
1155const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1156
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1158 GrGLuint rb,
1159 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001160 // we shouldn't ever know one size and not the other
1161 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1162 (kUnknownBitCount == format->fTotalBits));
1163 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1166 (GrGLint*)&format->fStencilBits);
1167 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001168 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1170 (GrGLint*)&format->fTotalBits);
1171 format->fTotalBits += format->fStencilBits;
1172 } else {
1173 format->fTotalBits = format->fStencilBits;
1174 }
1175 }
1176}
1177}
1178
1179bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1180 int width, int height) {
1181
1182 // All internally created RTs are also textures. We don't create
1183 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1184 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001185 GrAssert(width >= rt->width());
1186 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187
1188 int samples = rt->numSamples();
1189 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001190 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 if (!sbID) {
1192 return false;
1193 }
1194
1195 GrGLStencilBuffer* sb = NULL;
1196
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001197 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 // we start with the last stencil format that succeeded in hopes
1201 // that we won't go through this loop more than once after the
1202 // first (painful) stencil creation.
1203 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001204 const GrGLCaps::StencilFormat& sFmt =
1205 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001206 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001207 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001209 bool created;
1210 if (samples > 0) {
1211 created = renderbuffer_storage_msaa(fGLContextInfo,
1212 samples,
1213 sFmt.fInternalFormat,
1214 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001216 GL_ALLOC_CALL(this->glInterface(),
1217 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001218 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001219 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001220 created =
1221 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001223 if (created) {
1224 // After sized formats we attempt an unsized format and take
1225 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001226 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001228 sb = new GrGLStencilBuffer(this, sbID, width, height,
1229 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001230 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1231 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001232 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 return true;
1235 }
1236 sb->abandon(); // otherwise we lose sbID
1237 sb->unref();
1238 }
1239 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001241 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242}
1243
1244bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1245 GrRenderTarget* rt) {
1246 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1247
1248 GrGLuint fbo = glrt->renderFBOID();
1249
1250 if (NULL == sb) {
1251 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001252 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001253 GR_GL_STENCIL_ATTACHMENT,
1254 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001255 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001256 GR_GL_DEPTH_ATTACHMENT,
1257 GR_GL_RENDERBUFFER, 0));
1258#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001259 GrGLenum status;
1260 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001261 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1262#endif
1263 }
1264 return true;
1265 } else {
1266 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1267 GrGLuint rb = glsb->renderbufferID();
1268
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001269 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001270 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1271 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001272 GR_GL_STENCIL_ATTACHMENT,
1273 GR_GL_RENDERBUFFER, rb));
1274 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001275 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001276 GR_GL_DEPTH_ATTACHMENT,
1277 GR_GL_RENDERBUFFER, rb));
1278 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001279 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001280 GR_GL_DEPTH_ATTACHMENT,
1281 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001283
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001284 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001285 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001286 glsb->format())) {
1287 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1288 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001290 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001291 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001292 if (glsb->format().fPacked) {
1293 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1294 GR_GL_DEPTH_ATTACHMENT,
1295 GR_GL_RENDERBUFFER, 0));
1296 }
1297 return false;
1298 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001299 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001300 rt->config(),
1301 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001302 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001303 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001304 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001305 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001306}
1307
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001308////////////////////////////////////////////////////////////////////////////////
1309
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001310GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001311 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001312 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001314 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001315 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001316 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001318 GL_ALLOC_CALL(this->glInterface(),
1319 BufferData(GR_GL_ARRAY_BUFFER,
1320 size,
1321 NULL, // data ptr
1322 dynamic ? GR_GL_DYNAMIC_DRAW :
1323 GR_GL_STATIC_DRAW));
1324 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001325 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 // deleting bound buffer does implicit bind to 0
1327 fHWGeometryState.fVertexBuffer = NULL;
1328 return NULL;
1329 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001330 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 size, dynamic);
1332 fHWGeometryState.fVertexBuffer = vertexBuffer;
1333 return vertexBuffer;
1334 }
1335 return NULL;
1336}
1337
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001338GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001339 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001340 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001342 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001343 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001345 GL_ALLOC_CALL(this->glInterface(),
1346 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1347 size,
1348 NULL, // data ptr
1349 dynamic ? GR_GL_DYNAMIC_DRAW :
1350 GR_GL_STATIC_DRAW));
1351 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001352 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 // deleting bound buffer does implicit bind to 0
1354 fHWGeometryState.fIndexBuffer = NULL;
1355 return NULL;
1356 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001357 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 size, dynamic);
1359 fHWGeometryState.fIndexBuffer = indexBuffer;
1360 return indexBuffer;
1361 }
1362 return NULL;
1363}
1364
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001365void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001366 const GrDrawState& drawState = this->getDrawState();
1367 const GrGLRenderTarget* rt =
1368 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1369
1370 GrAssert(NULL != rt);
1371 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001372
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001373 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001374 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1375 rect.width(), rect.height());
1376 if (scissor.contains(vp)) {
1377 disableScissor();
1378 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 }
1380
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001381 if (fHWBounds.fScissorRect != scissor) {
1382 scissor.pushToGLScissor(this->glInterface());
1383 fHWBounds.fScissorRect = scissor;
1384 }
1385 if (!fHWBounds.fScissorEnabled) {
1386 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1387 fHWBounds.fScissorEnabled = true;
1388 }
1389}
1390
1391void GrGpuGL::disableScissor() {
1392 if (fHWBounds.fScissorEnabled) {
1393 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1394 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001395 }
1396}
1397
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001398void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001399 const GrDrawState& drawState = this->getDrawState();
1400 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001401 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001402 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001403
bsalomon@google.com74b98712011-11-11 19:46:16 +00001404 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001405 if (NULL != rect) {
1406 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001407 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001408 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001409 if (clippedRect.intersect(rtRect)) {
1410 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001411 } else {
1412 return;
1413 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001414 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001415 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001416 if (NULL != rect)
1417 this->enableScissoring(*rect);
1418 else
1419 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001420
1421 GrGLfloat r, g, b, a;
1422 static const GrGLfloat scale255 = 1.f / 255.f;
1423 a = GrColorUnpackA(color) * scale255;
1424 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001425 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001426 scaleRGB *= a;
1427 }
1428 r = GrColorUnpackR(color) * scaleRGB;
1429 g = GrColorUnpackG(color) * scaleRGB;
1430 b = GrColorUnpackB(color) * scaleRGB;
1431
1432 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001433 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001434 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001435 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001436}
1437
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001438void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001439 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001440 return;
1441 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001442
1443 this->flushRenderTarget(&GrIRect::EmptyIRect());
1444
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001445 this->disableScissor();
1446
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001447 GL_CALL(StencilMask(0xffffffff));
1448 GL_CALL(ClearStencil(0));
1449 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001450 fHWStencilSettings.invalidate();
1451 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001452}
1453
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001454void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001455 const GrDrawState& drawState = this->getDrawState();
1456 const GrRenderTarget* rt = drawState.getRenderTarget();
1457 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001458
1459 // this should only be called internally when we know we have a
1460 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001461 GrAssert(NULL != rt->getStencilBuffer());
1462 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001463#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001464 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001465 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001466#else
1467 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001468 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001469 // turned into draws. Our contract on GrDrawTarget says that
1470 // changing the clip between stencil passes may or may not
1471 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001472 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001473#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001474 GrGLint value;
1475 if (insideClip) {
1476 value = (1 << (stencilBitCount - 1));
1477 } else {
1478 value = 0;
1479 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001480 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001481 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001482 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001483 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001484 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001485 fHWStencilSettings.invalidate();
1486 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001487}
1488
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001489void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001490 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001491}
1492
bsalomon@google.comc4364992011-11-07 15:54:49 +00001493bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1494 int left, int top,
1495 int width, int height,
1496 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001497 size_t rowBytes) const {
1498 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001499 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001500 return false;
1501 }
1502
1503 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001504 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001505 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001506 return true;
1507 }
1508 // If we have to do memcpys to handle rowBytes then y-flip is free
1509 // Note the rowBytes might be tight to the passed in data, but if data
1510 // gets clipped in x to the target the rowBytes will no longer be tight.
1511 if (left >= 0 && (left + width) < renderTarget->width()) {
1512 return 0 == rowBytes ||
1513 GrBytesPerPixel(config) * width == rowBytes;
1514 } else {
1515 return false;
1516 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001517}
1518
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001519bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001520 int left, int top,
1521 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001522 GrPixelConfig config,
1523 void* buffer,
1524 size_t rowBytes,
1525 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001526 GrGLenum format;
1527 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001528 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001529 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001530 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001531 size_t bpp = GrBytesPerPixel(config);
1532 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1533 &left, &top, &width, &height,
1534 const_cast<const void**>(&buffer),
1535 &rowBytes)) {
1536 return false;
1537 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001538
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001540 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001541 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001542 switch (tgt->getResolveType()) {
1543 case GrGLRenderTarget::kCantResolve_ResolveType:
1544 return false;
1545 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001546 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001547 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001548 break;
1549 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001550 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001551 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001552 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1553 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001554 break;
1555 default:
1556 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001557 }
1558
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001559 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001560
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001561 // the read rect is viewport-relative
1562 GrGLIRect readRect;
1563 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001564
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001565 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001566 if (0 == rowBytes) {
1567 rowBytes = tightRowBytes;
1568 }
1569 size_t readDstRowBytes = tightRowBytes;
1570 void* readDst = buffer;
1571
1572 // determine if GL can read using the passed rowBytes or if we need
1573 // a scratch buffer.
1574 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1575 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001576 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001577 GrAssert(!(rowBytes % sizeof(GrColor)));
1578 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1579 readDstRowBytes = rowBytes;
1580 } else {
1581 scratch.reset(tightRowBytes * height);
1582 readDst = scratch.get();
1583 }
1584 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001585 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001586 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1587 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001588 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1589 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001590 format, type, readDst));
1591 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001592 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001593 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1594 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001595 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001596 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1597 invertY = true;
1598 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001599
1600 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001601 // API presents top-to-bottom. We must preserve the padding contents. Note
1602 // that the above readPixels did not overwrite the padding.
1603 if (readDst == buffer) {
1604 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001605 if (!invertY) {
1606 scratch.reset(tightRowBytes);
1607 void* tmpRow = scratch.get();
1608 // flip y in-place by rows
1609 const int halfY = height >> 1;
1610 char* top = reinterpret_cast<char*>(buffer);
1611 char* bottom = top + (height - 1) * rowBytes;
1612 for (int y = 0; y < halfY; y++) {
1613 memcpy(tmpRow, top, tightRowBytes);
1614 memcpy(top, bottom, tightRowBytes);
1615 memcpy(bottom, tmpRow, tightRowBytes);
1616 top += rowBytes;
1617 bottom -= rowBytes;
1618 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001619 }
1620 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001621 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001622 // copy from readDst to buffer while flipping y
1623 const int halfY = height >> 1;
1624 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001625 char* dst = reinterpret_cast<char*>(buffer);
1626 if (!invertY) {
1627 dst += (height-1) * rowBytes;
1628 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001629 for (int y = 0; y < height; y++) {
1630 memcpy(dst, src, tightRowBytes);
1631 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001632 if (invertY) {
1633 dst += rowBytes;
1634 } else {
1635 dst -= rowBytes;
1636 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001637 }
1638 }
1639 return true;
1640}
1641
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001642void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001643
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001644 GrGLRenderTarget* rt =
1645 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1646 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001647
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001648 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001649 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001650 #if GR_COLLECT_STATS
1651 ++fStats.fRenderTargetChngCnt;
1652 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001654 GrGLenum status;
1655 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001656 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001657 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001658 }
1659 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001660 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001661 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001662 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001663 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001664 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001665 fHWBounds.fViewportRect = vp;
1666 }
1667 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001668 if (NULL == bound || !bound->isEmpty()) {
1669 rt->flagAsNeedingResolve(bound);
1670 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001671}
1672
twiz@google.com0f31ca72011-03-18 17:38:11 +00001673GrGLenum gPrimitiveType2GLMode[] = {
1674 GR_GL_TRIANGLES,
1675 GR_GL_TRIANGLE_STRIP,
1676 GR_GL_TRIANGLE_FAN,
1677 GR_GL_POINTS,
1678 GR_GL_LINES,
1679 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001680};
1681
bsalomon@google.comd302f142011-03-03 13:54:13 +00001682#define SWAP_PER_DRAW 0
1683
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001684#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001685 #if GR_MAC_BUILD
1686 #include <AGL/agl.h>
1687 #elif GR_WIN32_BUILD
1688 void SwapBuf() {
1689 DWORD procID = GetCurrentProcessId();
1690 HWND hwnd = GetTopWindow(GetDesktopWindow());
1691 while(hwnd) {
1692 DWORD wndProcID = 0;
1693 GetWindowThreadProcessId(hwnd, &wndProcID);
1694 if(wndProcID == procID) {
1695 SwapBuffers(GetDC(hwnd));
1696 }
1697 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1698 }
1699 }
1700 #endif
1701#endif
1702
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001703void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1704 uint32_t startVertex,
1705 uint32_t startIndex,
1706 uint32_t vertexCount,
1707 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001708 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1709
twiz@google.com0f31ca72011-03-18 17:38:11 +00001710 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001711
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001712 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1713 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1714
1715 // our setupGeometry better have adjusted this to zero since
1716 // DrawElements always draws from the begining of the arrays for idx 0.
1717 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001718
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001719 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1720 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001721#if SWAP_PER_DRAW
1722 glFlush();
1723 #if GR_MAC_BUILD
1724 aglSwapBuffers(aglGetCurrentContext());
1725 int set_a_break_pt_here = 9;
1726 aglSwapBuffers(aglGetCurrentContext());
1727 #elif GR_WIN32_BUILD
1728 SwapBuf();
1729 int set_a_break_pt_here = 9;
1730 SwapBuf();
1731 #endif
1732#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001733}
1734
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001735void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1736 uint32_t startVertex,
1737 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001738 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1739
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001740 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1741
1742 // our setupGeometry better have adjusted this to zero.
1743 // DrawElements doesn't take an offset so we always adjus the startVertex.
1744 GrAssert(0 == startVertex);
1745
1746 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1747 // account for startVertex in the DrawElements case. So we always
1748 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001749 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001750#if SWAP_PER_DRAW
1751 glFlush();
1752 #if GR_MAC_BUILD
1753 aglSwapBuffers(aglGetCurrentContext());
1754 int set_a_break_pt_here = 9;
1755 aglSwapBuffers(aglGetCurrentContext());
1756 #elif GR_WIN32_BUILD
1757 SwapBuf();
1758 int set_a_break_pt_here = 9;
1759 SwapBuf();
1760 #endif
1761#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001762}
1763
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001764void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1765
1766 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001767
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001768 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001769 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001771 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1772 rt->renderFBOID()));
1773 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1774 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001775 #if GR_COLLECT_STATS
1776 ++fStats.fRenderTargetChngCnt;
1777 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001778 // make sure we go through flushRenderTarget() since we've modified
1779 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001780 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001781 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001782 const GrIRect dirtyRect = rt->getResolveRect();
1783 GrGLIRect r;
1784 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1785 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001786
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001787 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001788 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001789#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001790 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1791 GL_CALL(Scissor(r.fLeft, r.fBottom,
1792 r.fWidth, r.fHeight));
1793 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001794 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001795 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001796#else
1797 this->enableScissoring(dirtyRect);
1798 GL_CALL(ResolveMultisampleFramebuffer());
1799#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001800 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001801 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001802 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001803 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1804 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001805 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001806 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001807 int right = r.fLeft + r.fWidth;
1808 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001809 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1810 r.fLeft, r.fBottom, right, top,
1811 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001812 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001813 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001814 }
1815}
1816
twiz@google.com0f31ca72011-03-18 17:38:11 +00001817static const GrGLenum grToGLStencilFunc[] = {
1818 GR_GL_ALWAYS, // kAlways_StencilFunc
1819 GR_GL_NEVER, // kNever_StencilFunc
1820 GR_GL_GREATER, // kGreater_StencilFunc
1821 GR_GL_GEQUAL, // kGEqual_StencilFunc
1822 GR_GL_LESS, // kLess_StencilFunc
1823 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1824 GR_GL_EQUAL, // kEqual_StencilFunc,
1825 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001826};
1827GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1828GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1829GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1830GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1831GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1832GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1833GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1834GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1835GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1836
twiz@google.com0f31ca72011-03-18 17:38:11 +00001837static const GrGLenum grToGLStencilOp[] = {
1838 GR_GL_KEEP, // kKeep_StencilOp
1839 GR_GL_REPLACE, // kReplace_StencilOp
1840 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1841 GR_GL_INCR, // kIncClamp_StencilOp
1842 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1843 GR_GL_DECR, // kDecClamp_StencilOp
1844 GR_GL_ZERO, // kZero_StencilOp
1845 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001846};
1847GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1848GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1849GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1850GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1851GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1852GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1853GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1854GR_STATIC_ASSERT(6 == kZero_StencilOp);
1855GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1856
reed@google.comac10a2d2010-12-22 21:39:39 +00001857void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001858 const GrDrawState& drawState = this->getDrawState();
1859
1860 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001861
1862 // use stencil for clipping if clipping is enabled and the clip
1863 // has been written into the stencil.
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001864 StencilClipMode clipMode;
1865 if (fClipMaskManager.isClipInStencil() &&
1866 drawState.isClipState()) {
1867 clipMode = kUseClip_StencilClipMode;
1868 // We can't be modifying the clip and respecting it at the same time.
1869 GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1870 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
1871 clipMode = kModifyClip_StencilClipMode;
1872 } else {
1873 clipMode = kIgnoreClip_StencilClipMode;
1874 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001875
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001876 bool stencilChange = (fHWStencilSettings != *settings) ||
1877 (fHWStencilClipMode != clipMode);
reed@google.comac10a2d2010-12-22 21:39:39 +00001878 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001879
bsalomon@google.comd302f142011-03-03 13:54:13 +00001880 if (settings->isDisabled()) {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001881 if (kUseClip_StencilClipMode == clipMode) {
digit@google.com9b482c42012-02-16 22:03:26 +00001882 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001884 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001885
1886 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001887 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001888 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001889 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001890 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001891 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001892 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1893 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1894 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1895 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1896 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1897 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1898 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1899 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900 }
1901 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001902 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001903 GrStencilBuffer* stencilBuffer =
1904 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001905 if (NULL != stencilBuffer) {
1906 stencilBits = stencilBuffer->bits();
1907 }
1908 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001909 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001910
1911 GrGLuint clipStencilMask = 0;
1912 GrGLuint userStencilMask = ~0;
1913 if (stencilBits > 0) {
1914 clipStencilMask = 1 << (stencilBits - 1);
1915 userStencilMask = clipStencilMask - 1;
1916 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001917
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001918 unsigned int frontRef = settings->frontFuncRef();
1919 unsigned int frontMask = settings->frontFuncMask();
1920 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001921 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001923 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001924 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1925 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001926 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001927 bool useClip = kUseClip_StencilClipMode == clipMode;
1928 frontFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1929 settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001931 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001932 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001933 clipStencilMask,
1934 userStencilMask,
1935 &frontRef,
1936 &frontMask);
1937 frontWriteMask &= userStencilMask;
1938 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001939 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001940 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001941 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001942 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001943 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001944 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001945 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001946 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001947 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001948 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001949
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001950 unsigned int backRef = settings->backFuncRef();
1951 unsigned int backMask = settings->backFuncMask();
1952 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001953
1954
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001955 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001956 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1957 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001958 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001959 bool useClip = kUseClip_StencilClipMode == clipMode;
1960 backFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1961 settings->backFunc())];
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001962 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001963 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001964 clipStencilMask,
1965 userStencilMask,
1966 &backRef,
1967 &backMask);
1968 backWriteMask &= userStencilMask;
1969 }
1970
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001971 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1972 frontRef, frontMask));
1973 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1974 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1975 backRef, backMask));
1976 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1977 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001978 grToGLStencilOp[settings->frontFailOp()],
1979 grToGLStencilOp[settings->frontPassOp()],
1980 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001981
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001982 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001983 grToGLStencilOp[settings->backFailOp()],
1984 grToGLStencilOp[settings->backPassOp()],
1985 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001986 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001987 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1988 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001989 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1990 grToGLStencilOp[settings->frontPassOp()],
1991 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001992 }
1993 }
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001994 fHWStencilSettings = *settings;
1995 fHWStencilClipMode = clipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001996 }
1997}
1998
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001999void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002000 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002001 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002002 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
2003 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002004 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002005 bool smoothLines = false;
2006
bsalomon@google.com0650e812011-04-08 18:07:53 +00002007 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002008 smoothLines = this->willUseHWAALines();
2009 if (smoothLines) {
2010 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
2011 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
2012 fHWAAState.fSmoothLineEnabled = kYes_TriState;
2013 // must disable msaa to use line smoothing
2014 if (rt->isMultisampled() &&
2015 kNo_TriState != fHWAAState.fMSAAEnabled) {
2016 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2017 fHWAAState.fMSAAEnabled = kNo_TriState;
2018 }
2019 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002020 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002021 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
2022 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
2023 fHWAAState.fSmoothLineEnabled = kNo_TriState;
2024 }
2025 }
2026 }
2027 if (!smoothLines && rt->isMultisampled()) {
2028 if (this->getDrawState().isHWAntialiasState()) {
2029 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2030 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2031 fHWAAState.fMSAAEnabled = kYes_TriState;
2032 }
2033 } else {
2034 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2035 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2036 fHWAAState.fMSAAEnabled = kNo_TriState;
2037 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002038 }
2039 }
2040 }
2041}
2042
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002043void GrGpuGL::flushBlend(GrPrimitiveType type,
2044 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002045 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002046 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002047 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002048 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002049 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002050 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002051 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
2052 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002053 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2054 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002055 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
2056 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002057 }
2058 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002059 // any optimization to disable blending should
2060 // have already been applied and tweaked the coeffs
2061 // to (1, 0).
2062 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2063 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002064 if (blendOff) {
2065 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002066 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002067 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002068 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002069 } else {
2070 if (kYes_TriState != fHWBlendState.fEnabled) {
2071 GL_CALL(Enable(GR_GL_BLEND));
2072 fHWBlendState.fEnabled = kYes_TriState;
2073 }
2074 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2075 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002076 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2077 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002078 fHWBlendState.fSrcCoeff = srcCoeff;
2079 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002080 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002081 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002082 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2083 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002084 (!fHWBlendState.fConstColorValid ||
2085 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002086
2087 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002088 GrColorUnpackR(blendConst) / 255.f,
2089 GrColorUnpackG(blendConst) / 255.f,
2090 GrColorUnpackB(blendConst) / 255.f,
2091 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002092 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002093 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002094 fHWBlendState.fConstColor = blendConst;
2095 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002096 }
2097 }
2098 }
2099}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002100namespace {
2101
2102unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002103 switch (filter) {
2104 case GrSamplerState::kBilinear_Filter:
2105 case GrSamplerState::k4x4Downsample_Filter:
2106 return GR_GL_LINEAR;
2107 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002108 return GR_GL_NEAREST;
2109 default:
2110 GrAssert(!"Unknown filter type");
2111 return GR_GL_LINEAR;
2112 }
2113}
2114
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002115// get_swizzle is only called from this .cpp so it is OK to inline it here
2116inline const GrGLenum* get_swizzle(GrPixelConfig config,
2117 const GrSamplerState& sampler,
2118 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002119 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002120 if (glCaps.textureRedSupport()) {
2121 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2122 GR_GL_RED, GR_GL_RED };
2123 return gRedSmear;
2124 } else {
2125 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2126 GR_GL_ALPHA, GR_GL_ALPHA };
2127 return gAlphaSmear;
2128 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002129 } else if (sampler.swapsRAndB()) {
2130 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2131 GR_GL_RED, GR_GL_ALPHA };
2132 return gRedBlueSwap;
2133 } else {
2134 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2135 GR_GL_BLUE, GR_GL_ALPHA };
2136 return gStraight;
2137 }
2138}
2139
2140void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002141 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2142 GR_GL_TEXTURE_SWIZZLE_RGBA,
2143 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002144}
2145}
2146
bsalomon@google.comffca4002011-02-22 20:34:01 +00002147bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002148
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002149 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002150 // GrGpu::setupClipAndFlushState should have already checked this
2151 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002152 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002153
tomhudson@google.com93813632011-10-27 20:21:16 +00002154 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002155 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002156 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002157 GrGLTexture* nextTexture =
2158 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002159
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002160 // true for now, but maybe not with GrEffect.
2161 GrAssert(NULL != nextTexture);
2162 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002163 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002164 // the last bound texture, but it needs resolving. So keep this
2165 // out of the "last != next" check.
2166 GrGLRenderTarget* texRT =
2167 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2168 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002169 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002170 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002171
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002172 if (fHWBoundTextures[s] != nextTexture) {
2173 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002174 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002175 #if GR_COLLECT_STATS
2176 ++fStats.fTextureChngCnt;
2177 #endif
2178 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002179 fHWBoundTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002180 // The texture matrix has to compensate for texture width/height
2181 // and NPOT-embedded-in-POT
2182 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002183 }
2184
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002185 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002186 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002187 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002188 nextTexture->getCachedTexParams(&timestamp);
2189 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002190 GrGLTexture::TexParams newTexParams;
2191
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002192 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002193
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002194 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002195 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2196 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002197 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002198 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002199 sizeof(newTexParams.fSwizzleRGBA));
2200 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002201 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002202 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002203 GR_GL_TEXTURE_MAG_FILTER,
2204 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002205 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002206 GR_GL_TEXTURE_MIN_FILTER,
2207 newTexParams.fFilter));
2208 }
2209 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002210 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002211 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002212 GR_GL_TEXTURE_WRAP_S,
2213 newTexParams.fWrapS));
2214 }
2215 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002216 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002217 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002218 GR_GL_TEXTURE_WRAP_T,
2219 newTexParams.fWrapT));
2220 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002221 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002222 (setAll ||
2223 memcmp(newTexParams.fSwizzleRGBA,
2224 oldTexParams.fSwizzleRGBA,
2225 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002226 this->setTextureUnit(s);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002227 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2228 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002229 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002230 nextTexture->setCachedTexParams(newTexParams,
2231 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002232 }
2233 }
2234
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002235 GrIRect* rect = NULL;
2236 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002237 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002238 fClip.hasConservativeBounds()) {
2239 fClip.getConservativeBounds().roundOut(&clipBounds);
2240 rect = &clipBounds;
2241 }
2242 this->flushRenderTarget(rect);
2243 this->flushAAState(type);
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002244
2245 if (drawState->isDitherState()) {
2246 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002247 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002248 fHWDitherEnabled = kYes_TriState;
2249 }
2250 } else {
2251 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002252 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002253 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002254 }
2255 }
2256
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002257 if (drawState->isColorWriteDisabled()) {
2258 if (kNo_TriState != fHWWriteToColor) {
2259 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2260 GR_GL_FALSE, GR_GL_FALSE));
2261 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002262 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002263 } else {
2264 if (kYes_TriState != fHWWriteToColor) {
2265 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2266 fHWWriteToColor = kYes_TriState;
2267 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002268 }
2269
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002270 if (fHWDrawFace != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002271 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002272 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002273 GL_CALL(Enable(GR_GL_CULL_FACE));
2274 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002275 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002276 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002277 GL_CALL(Enable(GR_GL_CULL_FACE));
2278 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002279 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002280 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002281 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002282 break;
2283 default:
2284 GrCrash("Unknown draw face.");
2285 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002286 fHWDrawFace = drawState->getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002287 }
2288
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002289#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002291 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002292 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002293 NULL == drawState->getRenderTarget() ||
2294 NULL == drawState->getTexture(s) ||
2295 drawState->getTexture(s)->asRenderTarget() !=
2296 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002297 }
2298#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002299
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002300 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002301
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002302 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002303}
2304
2305void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002306 if (fHWGeometryState.fVertexBuffer != buffer) {
2307 fHWGeometryState.fArrayPtrsDirty = true;
2308 fHWGeometryState.fVertexBuffer = buffer;
2309 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002310}
2311
2312void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002313 if (fHWGeometryState.fVertexBuffer == buffer) {
2314 // deleting bound buffer does implied bind to 0
2315 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002316 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002317 }
2318}
2319
2320void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002321 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002322}
2323
2324void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002325 if (fHWGeometryState.fIndexBuffer == buffer) {
2326 // deleting bound buffer does implied bind to 0
2327 fHWGeometryState.fIndexBuffer = NULL;
2328 }
2329}
2330
reed@google.comac10a2d2010-12-22 21:39:39 +00002331void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2332 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002333 GrDrawState* drawState = this->drawState();
2334 if (drawState->getRenderTarget() == renderTarget) {
2335 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002336 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002337 if (fHWBoundRenderTarget == renderTarget) {
2338 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002339 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002340}
2341
2342void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002343 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002344 GrDrawState* drawState = this->drawState();
2345 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002346 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002347 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002348 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002349 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002350 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002351 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002352 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002353}
2354
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002355bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2356 bool getSizedInternalFormat,
2357 GrGLenum* internalFormat,
2358 GrGLenum* externalFormat,
2359 GrGLenum* externalType) {
2360 GrGLenum dontCare;
2361 if (NULL == internalFormat) {
2362 internalFormat = &dontCare;
2363 }
2364 if (NULL == externalFormat) {
2365 externalFormat = &dontCare;
2366 }
2367 if (NULL == externalType) {
2368 externalType = &dontCare;
2369 }
2370
reed@google.comac10a2d2010-12-22 21:39:39 +00002371 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002372 case kRGBA_8888_PM_GrPixelConfig:
2373 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002374 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002375 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002376 if (getSizedInternalFormat) {
2377 *internalFormat = GR_GL_RGBA8;
2378 } else {
2379 *internalFormat = GR_GL_RGBA;
2380 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002381 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002382 break;
2383 case kBGRA_8888_PM_GrPixelConfig:
2384 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002385 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002386 return false;
2387 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002388 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002389 if (getSizedInternalFormat) {
2390 *internalFormat = GR_GL_BGRA8;
2391 } else {
2392 *internalFormat = GR_GL_BGRA;
2393 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002394 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002395 if (getSizedInternalFormat) {
2396 *internalFormat = GR_GL_RGBA8;
2397 } else {
2398 *internalFormat = GR_GL_RGBA;
2399 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002400 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002401 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002402 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002403 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002404 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002405 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002406 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002407 if (getSizedInternalFormat) {
2408 if (this->glBinding() == kDesktop_GrGLBinding) {
2409 return false;
2410 } else {
2411 *internalFormat = GR_GL_RGB565;
2412 }
2413 } else {
2414 *internalFormat = GR_GL_RGB;
2415 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002416 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002417 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002418 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002419 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002420 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002421 if (getSizedInternalFormat) {
2422 *internalFormat = GR_GL_RGBA4;
2423 } else {
2424 *internalFormat = GR_GL_RGBA;
2425 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002426 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002427 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002428 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002429 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002430 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002431 // glCompressedTexImage doesn't take external params
2432 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002433 // no sized/unsized internal format distinction here
2434 *internalFormat = GR_GL_PALETTE8_RGBA8;
2435 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002436 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002437 } else {
2438 return false;
2439 }
2440 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002441 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002442 if (this->glCaps().textureRedSupport()) {
2443 *internalFormat = GR_GL_RED;
2444 *externalFormat = GR_GL_RED;
2445 if (getSizedInternalFormat) {
2446 *internalFormat = GR_GL_R8;
2447 } else {
2448 *internalFormat = GR_GL_RED;
2449 }
2450 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002451 } else {
2452 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002453 *externalFormat = GR_GL_ALPHA;
2454 if (getSizedInternalFormat) {
2455 *internalFormat = GR_GL_ALPHA8;
2456 } else {
2457 *internalFormat = GR_GL_ALPHA;
2458 }
2459 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002460 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002461 break;
2462 default:
2463 return false;
2464 }
2465 return true;
2466}
2467
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002468void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002469 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002470 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002471 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002472 fActiveTextureUnitIdx = unit;
2473 }
2474}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002475
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002476void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002477 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002478 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002479 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2480 }
2481}
2482
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002483void GrGpuGL::resetDirtyFlags() {
2484 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2485}
2486
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002487void GrGpuGL::setBuffers(bool indexed,
2488 int* extraVertexOffset,
2489 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002490
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002491 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002492
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002493 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2494
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002495 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002496 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002497 case kBuffer_GeometrySrcType:
2498 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002499 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002500 break;
2501 case kArray_GeometrySrcType:
2502 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002503 this->finalizeReservedVertices();
2504 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2505 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002506 break;
2507 default:
2508 vbuf = NULL; // suppress warning
2509 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002510 }
2511
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002512 GrAssert(NULL != vbuf);
2513 GrAssert(!vbuf->isLocked());
2514 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002515 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002516 fHWGeometryState.fArrayPtrsDirty = true;
2517 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002518 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002519
2520 if (indexed) {
2521 GrAssert(NULL != extraIndexOffset);
2522
2523 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002524 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002525 case kBuffer_GeometrySrcType:
2526 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002527 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002528 break;
2529 case kArray_GeometrySrcType:
2530 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002531 this->finalizeReservedIndices();
2532 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2533 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002534 break;
2535 default:
2536 ibuf = NULL; // suppress warning
2537 GrCrash("Unknown geometry src type!");
2538 }
2539
2540 GrAssert(NULL != ibuf);
2541 GrAssert(!ibuf->isLocked());
2542 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002543 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002544 fHWGeometryState.fIndexBuffer = ibuf;
2545 }
2546 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002547}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002548