blob: 29c7900c456a26c8354a7bf00c4e70d19fd1475d [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.com5739d2c2012-05-31 15:07:19 +0000210 this->createProgramCache();
211
212#if 0
213 this->programUnitTest();
214#endif
215
bsalomon@google.comfe676522011-06-17 18:12:21 +0000216 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000217 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000218}
219
220GrGpuGL::~GrGpuGL() {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000221 if (fProgramData && 0 != fHWProgramID) {
222 // detach the current program so there is no confusion on OpenGL's part
223 // that we want it to be deleted
224 GrAssert(fHWProgramID == fProgramData->fProgramID);
225 GL_CALL(UseProgram(0));
226 }
227
228 this->deleteProgramCache();
229
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000230 // This must be called by before the GrDrawTarget destructor
231 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000232 // This subclass must do this before the base class destructor runs
233 // since we will unref the GrGLInterface.
234 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000235}
236
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237///////////////////////////////////////////////////////////////////////////////
238
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000239void GrGpuGL::initCaps() {
240 GrGLint maxTextureUnits;
241 // check FS and fixed-function texture unit limits
242 // we only use textures in the fragment stage currently.
243 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000244 const GrGLInterface* gl = this->glInterface();
245 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000246 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000247
248 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000249 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000251 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000252 for (int i = 0; i < numFormats; ++i) {
253 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
254 fCaps.f8BitPaletteSupport = true;
255 break;
256 }
257 }
258
259 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000260 // we could also look for GL_ATI_separate_stencil extension or
261 // GL_EXT_stencil_two_side but they use different function signatures
262 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000263 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000264 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000265 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000266 this->hasExtension("GL_EXT_stencil_wrap");
267 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000268 // ES 2 has two sided stencil and stencil wrap
269 fCaps.fTwoSidedStencilSupport = true;
270 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000271 }
272
273 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000274 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
275 // extension includes glMapBuffer.
276 } else {
277 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
278 }
279
280 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000281 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000282 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
283 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000284 } else {
285 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000286 }
287 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000288 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000289 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000290 }
291
292 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
293
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000294 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
295 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000296 // Our render targets are always created with textures as the color
297 // attachment, hence this min:
298 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
299
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000300 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000301
302 // Enable supported shader-related caps
303 if (kDesktop_GrGLBinding == this->glBinding()) {
304 fCaps.fDualSourceBlendingSupport =
305 this->glVersion() >= GR_GL_VER(3,3) ||
306 this->hasExtension("GL_ARB_blend_func_extended");
307 fCaps.fShaderDerivativeSupport = true;
308 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
309 fCaps.fGeometryShaderSupport =
310 this->glVersion() >= GR_GL_VER(3,2) &&
311 this->glslGeneration() >= k150_GrGLSLGeneration;
312 } else {
313 fCaps.fShaderDerivativeSupport =
314 this->hasExtension("GL_OES_standard_derivatives");
315 }
316
317 GR_GL_GetIntegerv(this->glInterface(),
318 GR_GL_MAX_VERTEX_ATTRIBS,
319 &fMaxVertexAttribs);
320
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000321}
322
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000323void GrGpuGL::fillInConfigRenderableTable() {
324
325 // OpenGL < 3.0
326 // no support for render targets unless the GL_ARB_framebuffer_object
327 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
328 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
329 // probably don't get R8 in this case.
330
331 // OpenGL 3.0
332 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
333 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
334
335 // >= OpenGL 3.1
336 // base color renderable: RED, RG, RGB, and RGBA
337 // sized derivatives: R8, RGBA4, RGBA8
338 // if the GL_ARB_compatibility extension is supported then we get back
339 // support for GL_ALPHA and ALPHA8
340
341 // GL_EXT_bgra adds BGRA render targets to any version
342
343 // ES 2.0
344 // color renderable: RGBA4, RGB5_A1, RGB565
345 // GL_EXT_texture_rg adds support for R8 as a color render target
346 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
347 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
348 // added BGRA support
349
350 if (kDesktop_GrGLBinding == this->glBinding()) {
351 // Post 3.0 we will get R8
352 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
353 if (this->glVersion() >= GR_GL_VER(3,0) ||
354 this->hasExtension("GL_ARB_framebuffer_object")) {
355 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
356 }
357 } else {
358 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000359 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
360 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000361 }
362
363 if (kDesktop_GrGLBinding != this->glBinding()) {
364 // only available in ES
365 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
366 }
367
368 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
369 // GL_EXT_framebuffer_object for FBO support. Both of these
370 // allow RGBA4 render targets so this is always supported.
371 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
372
373 if (this->glCaps().rgba8RenderbufferSupport()) {
374 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
375 }
376
377 if (this->glCaps().bgraFormatSupport()) {
378 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
379 }
380
381 // the un-premultiplied formats just inherit the premultiplied setting
382 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
383 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
384 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
385 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
386}
387
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000388bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
389 if (kUnknown_CanPreserveUnpremulRoundtrip ==
390 fCanPreserveUnpremulRoundtrip) {
391
392 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
393 uint32_t* srcData = data.get();
394 uint32_t* firstRead = data.get() + 256 * 256;
395 uint32_t* secondRead = data.get() + 2 * 256 * 256;
396
397 for (int y = 0; y < 256; ++y) {
398 for (int x = 0; x < 256; ++x) {
399 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
400 color[3] = y;
401 color[2] = x;
402 color[1] = x;
403 color[0] = x;
404 }
405 }
406
407 // We have broader support for read/write pixels on render targets
408 // than on textures.
409 GrTextureDesc dstDesc;
410 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
411 kNoStencil_GrTextureFlagBit;
412 dstDesc.fWidth = 256;
413 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000414 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000415 dstDesc.fSampleCnt = 0;
416
417 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
418 if (!dstTex.get()) {
419 return false;
420 }
421 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
422 GrAssert(NULL != rt);
423
424 bool failed = true;
425 static const UnpremulConversion gMethods[] = {
426 kUpOnWrite_DownOnRead_UnpremulConversion,
427 kDownOnWrite_UpOnRead_UnpremulConversion,
428 };
429
430 // pretend that we can do the roundtrip to avoid recursive calls to
431 // this function
432 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
433 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
434 fUnpremulConversion = gMethods[i];
435 rt->writePixels(0, 0,
436 256, 256,
437 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
438 rt->readPixels(0, 0,
439 256, 256,
440 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
441 rt->writePixels(0, 0,
442 256, 256,
443 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
444 rt->readPixels(0, 0,
445 256, 256,
446 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
447 failed = false;
448 for (int j = 0; j < 256 * 256; ++j) {
449 if (firstRead[j] != secondRead[j]) {
450 failed = true;
451 break;
452 }
453 }
454 }
455 fCanPreserveUnpremulRoundtrip = failed ?
456 kNo_CanPreserveUnpremulRoundtrip :
457 kYes_CanPreserveUnpremulRoundtrip;
458 }
459
460 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
461 return true;
462 } else {
463 return false;
464 }
465}
466
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000467GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000468 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
469 return GrPixelConfigSwapRAndB(config);
470 } else {
471 return config;
472 }
473}
474
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000475GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000476 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000477 return GrPixelConfigSwapRAndB(config);
478 } else {
479 return config;
480 }
481}
482
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000483bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
484 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
485}
486
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000487void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000488 if (gPrintStartupSpew && !fPrintedCaps) {
489 fPrintedCaps = true;
490 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000491 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000492 }
493
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000494 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000495 GL_CALL(Disable(GR_GL_DEPTH_TEST));
496 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000497
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000498 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
499 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000501 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000502 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000503 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000504 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
505 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
506 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
507 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
508 GL_CALL(Disable(GR_GL_COLOR_TABLE));
509 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
510 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
511 // Since ES doesn't support glPointSize at all we always use the VS to
512 // set the point size
513 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
514
515 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
516 // currently part of our gl interface. There are probably others as
517 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000518 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000519 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000520 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000521
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000523 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000524
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000525 // invalid
526 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000527
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000528 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000529
tomhudson@google.com93813632011-10-27 20:21:16 +0000530 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000531 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000532 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000533
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000534 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000535 // set to true to force disableScissor to make a GL call.
536 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000537 this->disableScissor();
538
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000539 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000540
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000541 fHWStencilSettings.invalidate();
542 fHWStencilClipMode = kInvalid_StencilClipMode;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000543
544 // TODO: I believe this should actually go in GrGpu::onResetContext
545 // rather than here
546 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000547
548 fHWGeometryState.fIndexBuffer = NULL;
549 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000550
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000551 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000552
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000553 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000554
555 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000556 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000557 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
558 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000559 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000560 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
561 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000562 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000563 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
564 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000565 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000566 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
567 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000568
569 fHWGeometryState.fVertexOffset = ~0;
570
571 // Third party GL code may have left vertex attributes enabled. Some GL
572 // implementations (osmesa) may read vetex attributes that are not required
573 // by the current shader. Therefore, we have to ensure that only the
574 // attributes we require for the current draw are enabled or we may cause an
575 // invalid read.
576
577 // Disable all vertex layout bits so that next flush will assume all
578 // optional vertex attributes are disabled.
579 fHWGeometryState.fVertexLayout = 0;
580
581 // We always use the this attribute and assume it is always enabled.
582 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
583 GL_CALL(EnableVertexAttribArray(posAttrIdx));
584 // Disable all other vertex attributes.
585 for (int va = 0; va < fMaxVertexAttribs; ++va) {
586 if (va != posAttrIdx) {
587 GL_CALL(DisableVertexAttribArray(va));
588 }
589 }
590
591 fHWProgramID = 0;
592 fHWConstAttribColor = GrColor_ILLEGAL;
593 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000594}
595
bsalomon@google.come269f212011-11-07 13:29:52 +0000596GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000597 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000598 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000599 return NULL;
600 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000601
bsalomon@google.com99621082011-11-15 16:47:16 +0000602 glTexDesc.fWidth = desc.fWidth;
603 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000604 glTexDesc.fConfig = desc.fConfig;
605 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
606 glTexDesc.fOwnsID = false;
607 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
608
609 GrGLTexture* texture = NULL;
610 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
611 GrGLRenderTarget::Desc glRTDesc;
612 glRTDesc.fRTFBOID = 0;
613 glRTDesc.fTexFBOID = 0;
614 glRTDesc.fMSColorRenderbufferID = 0;
615 glRTDesc.fOwnIDs = true;
616 glRTDesc.fConfig = desc.fConfig;
617 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000618 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
619 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000620 glTexDesc.fTextureID,
621 &glRTDesc)) {
622 return NULL;
623 }
624 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
625 } else {
626 texture = new GrGLTexture(this, glTexDesc);
627 }
628 if (NULL == texture) {
629 return NULL;
630 }
631
632 this->setSpareTextureUnit();
633 return texture;
634}
635
636GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
637 GrGLRenderTarget::Desc glDesc;
638 glDesc.fConfig = desc.fConfig;
639 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
640 glDesc.fMSColorRenderbufferID = 0;
641 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
642 glDesc.fSampleCnt = desc.fSampleCnt;
643 glDesc.fOwnIDs = false;
644 GrGLIRect viewport;
645 viewport.fLeft = 0;
646 viewport.fBottom = 0;
647 viewport.fWidth = desc.fWidth;
648 viewport.fHeight = desc.fHeight;
649
650 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
651 if (desc.fStencilBits) {
652 GrGLStencilBuffer::Format format;
653 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
654 format.fPacked = false;
655 format.fStencilBits = desc.fStencilBits;
656 format.fTotalBits = desc.fStencilBits;
657 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
658 0,
659 desc.fWidth,
660 desc.fHeight,
661 desc.fSampleCnt,
662 format);
663 tgt->setStencilBuffer(sb);
664 sb->unref();
665 }
666 return tgt;
667}
668
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000669////////////////////////////////////////////////////////////////////////////////
670
bsalomon@google.com6f379512011-11-16 20:36:03 +0000671void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
672 int left, int top, int width, int height,
673 GrPixelConfig config, const void* buffer,
674 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000675 if (NULL == buffer) {
676 return;
677 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000678 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
679
bsalomon@google.com6f379512011-11-16 20:36:03 +0000680 this->setSpareTextureUnit();
681 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
682 GrGLTexture::Desc desc;
683 desc.fConfig = glTex->config();
684 desc.fWidth = glTex->width();
685 desc.fHeight = glTex->height();
686 desc.fOrientation = glTex->orientation();
687 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000688
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000689 this->uploadTexData(desc, false,
690 left, top, width, height,
691 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000692}
693
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000694namespace {
695bool adjust_pixel_ops_params(int surfaceWidth,
696 int surfaceHeight,
697 size_t bpp,
698 int* left, int* top, int* width, int* height,
699 const void** data,
700 size_t* rowBytes) {
701 if (!*rowBytes) {
702 *rowBytes = *width * bpp;
703 }
704
705 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
706 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
707
708 if (!subRect.intersect(bounds)) {
709 return false;
710 }
711 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
712 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
713
714 *left = subRect.fLeft;
715 *top = subRect.fTop;
716 *width = subRect.width();
717 *height = subRect.height();
718 return true;
719}
720}
721
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000722bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
723 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000724 int left, int top, int width, int height,
725 GrPixelConfig dataConfig,
726 const void* data,
727 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000728 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000729
730 size_t bpp = GrBytesPerPixel(dataConfig);
731 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
732 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000733 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000734 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000735 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000736
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000737 // in case we need a temporary, trimmed copy of the src pixels
738 SkAutoSMalloc<128 * 128> tempStorage;
739
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000740 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000741 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000742 if (useTexStorage) {
743 if (kDesktop_GrGLBinding == this->glBinding()) {
744 // 565 is not a sized internal format on desktop GL. So on desktop
745 // with 565 we always use an unsized internal format to let the
746 // system pick the best sized format to convert the 565 data to.
747 // Since glTexStorage only allows sized internal formats we will
748 // instead fallback to glTexImage2D.
749 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
750 } else {
751 // ES doesn't allow paletted textures to be used with tex storage
752 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
753 }
754 }
755
756 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000757 GrGLenum externalFormat;
758 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000759 // glTexStorage requires sized internal formats on both desktop and ES. ES
760 // glTexImage requires an unsized format.
761 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
762 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000763 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000764 }
765
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000766 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000767 // paletted textures cannot be updated
768 return false;
769 }
770
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000771 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000772 * check whether to allocate a temporary buffer for flipping y or
773 * because our srcData has extra bytes past each row. If so, we need
774 * to trim those off here, since GL ES may not let us specify
775 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000776 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000777 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000778 bool swFlipY = false;
779 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000780 if (NULL != data) {
781 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000782 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000783 glFlipY = true;
784 } else {
785 swFlipY = true;
786 }
787 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000788 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000789 // can't use this for flipping, only non-neg values allowed. :(
790 if (rowBytes != trimRowBytes) {
791 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
792 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
793 restoreGLRowLength = true;
794 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000795 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000796 if (trimRowBytes != rowBytes || swFlipY) {
797 // copy data into our new storage, skipping the trailing bytes
798 size_t trimSize = height * trimRowBytes;
799 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000800 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000801 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000802 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000803 char* dst = (char*)tempStorage.reset(trimSize);
804 for (int y = 0; y < height; y++) {
805 memcpy(dst, src, trimRowBytes);
806 if (swFlipY) {
807 src -= rowBytes;
808 } else {
809 src += rowBytes;
810 }
811 dst += trimRowBytes;
812 }
813 // now point data to our copied version
814 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000816 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000817 if (glFlipY) {
818 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
819 }
820 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000821 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000822 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000823 if (isNewTexture &&
824 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000825 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000826 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000827 if (useTexStorage) {
828 // We never resize or change formats of textures. We don't use
829 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000830 GL_ALLOC_CALL(this->glInterface(),
831 TexStorage2D(GR_GL_TEXTURE_2D,
832 1, // levels
833 internalFormat,
834 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000835 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000836 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
837 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
838 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000839 GL_ALLOC_CALL(this->glInterface(),
840 CompressedTexImage2D(GR_GL_TEXTURE_2D,
841 0, // level
842 internalFormat,
843 desc.fWidth, desc.fHeight,
844 0, // border
845 imageSize,
846 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000847 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000848 GL_ALLOC_CALL(this->glInterface(),
849 TexImage2D(GR_GL_TEXTURE_2D,
850 0, // level
851 internalFormat,
852 desc.fWidth, desc.fHeight,
853 0, // border
854 externalFormat, externalType,
855 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000856 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000857 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000858 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000859 if (error != GR_GL_NO_ERROR) {
860 succeeded = false;
861 } else {
862 // if we have data and we used TexStorage to create the texture, we
863 // now upload with TexSubImage.
864 if (NULL != data && useTexStorage) {
865 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
866 0, // level
867 left, top,
868 width, height,
869 externalFormat, externalType,
870 data));
871 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000872 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000873 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000874 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000875 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000876 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000877 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
878 0, // level
879 left, top,
880 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000881 externalFormat, externalType, data));
882 }
883
884 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000885 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000886 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000887 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000888 if (glFlipY) {
889 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
890 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000891 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000892}
893
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000894namespace {
895bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
896 int sampleCount,
897 GrGLenum format,
898 int width, int height) {
899 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
900 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
901 bool created = false;
902 if (GrGLCaps::kNVDesktop_CoverageAAType ==
903 ctxInfo.caps().coverageAAType()) {
904 const GrGLCaps::MSAACoverageMode& mode =
905 ctxInfo.caps().getMSAACoverageMode(sampleCount);
906 GL_ALLOC_CALL(ctxInfo.interface(),
907 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
908 mode.fCoverageSampleCnt,
909 mode.fColorSampleCnt,
910 format,
911 width, height));
912 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
913 }
914 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000915 // glRBMS will fail if requested samples is > max samples.
916 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000917 GL_ALLOC_CALL(ctxInfo.interface(),
918 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
919 sampleCount,
920 format,
921 width, height));
922 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
923 }
924 return created;
925}
926}
927
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928bool GrGpuGL::createRenderTargetObjects(int width, int height,
929 GrGLuint texID,
930 GrGLRenderTarget::Desc* desc) {
931 desc->fMSColorRenderbufferID = 0;
932 desc->fRTFBOID = 0;
933 desc->fTexFBOID = 0;
934 desc->fOwnIDs = true;
935
936 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000937
bsalomon@google.comab15d612011-08-09 12:57:56 +0000938 GrGLenum msColorFormat = 0; // suppress warning
939
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000940 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000941 if (!desc->fTexFBOID) {
942 goto FAILED;
943 }
944
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945
946 // If we are using multisampling we will create two FBOS. We render
947 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000948 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000949 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000950 goto FAILED;
951 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000952 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
953 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000954 if (!desc->fRTFBOID ||
955 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000956 !this->configToGLFormats(desc->fConfig,
957 // GLES requires sized internal formats
958 kES2_GrGLBinding == this->glBinding(),
959 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000960 goto FAILED;
961 }
962 } else {
963 desc->fRTFBOID = desc->fTexFBOID;
964 }
965
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000966 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000967 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000968 if (desc->fRTFBOID != desc->fTexFBOID) {
969 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000970 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000972 if (!renderbuffer_storage_msaa(fGLContextInfo,
973 desc->fSampleCnt,
974 msColorFormat,
975 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000976 goto FAILED;
977 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000978 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
979 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000980 GR_GL_COLOR_ATTACHMENT0,
981 GR_GL_RENDERBUFFER,
982 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000983 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000984 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
985 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
986 goto FAILED;
987 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000988 fGLContextInfo.caps().markConfigAsValidColorAttachment(
989 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000990 }
991 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000992 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000993
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000994 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
995 GR_GL_COLOR_ATTACHMENT0,
996 GR_GL_TEXTURE_2D,
997 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000998 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000999 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1000 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1001 goto FAILED;
1002 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001003 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001004 }
1005
1006 return true;
1007
1008FAILED:
1009 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001010 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001011 }
1012 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001013 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001014 }
1015 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001017 }
1018 return false;
1019}
1020
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001021// good to set a break-point here to know when createTexture fails
1022static GrTexture* return_null_texture() {
1023// GrAssert(!"null texture");
1024 return NULL;
1025}
1026
1027#if GR_DEBUG
1028static size_t as_size_t(int x) {
1029 return x;
1030}
1031#endif
1032
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001033GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001034 const void* srcData,
1035 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001036
1037#if GR_COLLECT_STATS
1038 ++fStats.fTextureCreateCnt;
1039#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001040
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001041 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001042 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001043
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001044 // Attempt to catch un- or wrongly initialized sample counts;
1045 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1046
bsalomon@google.com99621082011-11-15 16:47:16 +00001047 glTexDesc.fWidth = desc.fWidth;
1048 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001049 glTexDesc.fConfig = desc.fConfig;
1050 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001051
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001052 glRTDesc.fMSColorRenderbufferID = 0;
1053 glRTDesc.fRTFBOID = 0;
1054 glRTDesc.fTexFBOID = 0;
1055 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001056 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001057
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001058 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001059
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001060 const Caps& caps = this->getCaps();
1061
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001062 // We keep GrRenderTargets in GL's normal orientation so that they
1063 // can be drawn to by the outside world without the client having
1064 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001065 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001066 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001067
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001068 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001069 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001070 desc.fSampleCnt) {
1071 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 }
1073
reed@google.comac10a2d2010-12-22 21:39:39 +00001074 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001075 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1076 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001077 return return_null_texture();
1078 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001079 }
1080
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001081 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001082 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001083 // provides a hint about how this texture will be used
1084 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1085 GR_GL_TEXTURE_USAGE,
1086 GR_GL_FRAMEBUFFER_ATTACHMENT));
1087 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001088 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001089 return return_null_texture();
1090 }
1091
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001092 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001093 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001094
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001095 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1096 // drivers have a bug where an FBO won't be complete if it includes a
1097 // texture that is not mipmap complete (considering the filter in use).
1098 GrGLTexture::TexParams initialTexParams;
1099 // we only set a subset here so invalidate first
1100 initialTexParams.invalidate();
1101 initialTexParams.fFilter = GR_GL_NEAREST;
1102 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1103 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001104 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1105 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001106 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1108 GR_GL_TEXTURE_MIN_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_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001112 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1114 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001115 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001116 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1117 glTexDesc.fWidth, glTexDesc.fHeight,
1118 desc.fConfig, srcData, rowBytes)) {
1119 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1120 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001121 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001122
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001124 if (renderTarget) {
1125#if GR_COLLECT_STATS
1126 ++fStats.fRenderTargetCreateCnt;
1127#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001128 // unbind the texture from the texture unit before binding it to the frame buffer
1129 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1130
bsalomon@google.com99621082011-11-15 16:47:16 +00001131 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1132 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 glTexDesc.fTextureID,
1134 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001136 return return_null_texture();
1137 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001138 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001140 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001141 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001142 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001143#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001144 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1145 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001146#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 return tex;
1148}
1149
1150namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001151
1152const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1153
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001154void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1155 GrGLuint rb,
1156 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 // we shouldn't ever know one size and not the other
1158 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1159 (kUnknownBitCount == format->fTotalBits));
1160 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001161 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1163 (GrGLint*)&format->fStencilBits);
1164 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001165 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1167 (GrGLint*)&format->fTotalBits);
1168 format->fTotalBits += format->fStencilBits;
1169 } else {
1170 format->fTotalBits = format->fStencilBits;
1171 }
1172 }
1173}
1174}
1175
1176bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1177 int width, int height) {
1178
1179 // All internally created RTs are also textures. We don't create
1180 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1181 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001182 GrAssert(width >= rt->width());
1183 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184
1185 int samples = rt->numSamples();
1186 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001187 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 if (!sbID) {
1189 return false;
1190 }
1191
1192 GrGLStencilBuffer* sb = NULL;
1193
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001194 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001196 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 // we start with the last stencil format that succeeded in hopes
1198 // that we won't go through this loop more than once after the
1199 // first (painful) stencil creation.
1200 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001201 const GrGLCaps::StencilFormat& sFmt =
1202 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001203 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001204 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001206 bool created;
1207 if (samples > 0) {
1208 created = renderbuffer_storage_msaa(fGLContextInfo,
1209 samples,
1210 sFmt.fInternalFormat,
1211 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001213 GL_ALLOC_CALL(this->glInterface(),
1214 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001215 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001216 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001217 created =
1218 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001220 if (created) {
1221 // After sized formats we attempt an unsized format and take
1222 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001223 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001224 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001225 sb = new GrGLStencilBuffer(this, sbID, width, height,
1226 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1228 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001229 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001230 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231 return true;
1232 }
1233 sb->abandon(); // otherwise we lose sbID
1234 sb->unref();
1235 }
1236 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001238 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239}
1240
1241bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1242 GrRenderTarget* rt) {
1243 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1244
1245 GrGLuint fbo = glrt->renderFBOID();
1246
1247 if (NULL == sb) {
1248 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001249 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250 GR_GL_STENCIL_ATTACHMENT,
1251 GR_GL_RENDERBUFFER, 0));
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_DEPTH_ATTACHMENT,
1254 GR_GL_RENDERBUFFER, 0));
1255#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001256 GrGLenum status;
1257 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001258 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1259#endif
1260 }
1261 return true;
1262 } else {
1263 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1264 GrGLuint rb = glsb->renderbufferID();
1265
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001266 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001267 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1268 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001269 GR_GL_STENCIL_ATTACHMENT,
1270 GR_GL_RENDERBUFFER, rb));
1271 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001273 GR_GL_DEPTH_ATTACHMENT,
1274 GR_GL_RENDERBUFFER, rb));
1275 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001276 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001277 GR_GL_DEPTH_ATTACHMENT,
1278 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001280
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001281 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001282 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001283 glsb->format())) {
1284 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1285 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001286 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001287 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001288 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001289 if (glsb->format().fPacked) {
1290 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1291 GR_GL_DEPTH_ATTACHMENT,
1292 GR_GL_RENDERBUFFER, 0));
1293 }
1294 return false;
1295 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001296 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001297 rt->config(),
1298 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001299 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001300 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001301 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001303}
1304
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001305////////////////////////////////////////////////////////////////////////////////
1306
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001307GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001308 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001309 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001311 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001312 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001313 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001315 GL_ALLOC_CALL(this->glInterface(),
1316 BufferData(GR_GL_ARRAY_BUFFER,
1317 size,
1318 NULL, // data ptr
1319 dynamic ? GR_GL_DYNAMIC_DRAW :
1320 GR_GL_STATIC_DRAW));
1321 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001322 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001323 // deleting bound buffer does implicit bind to 0
1324 fHWGeometryState.fVertexBuffer = NULL;
1325 return NULL;
1326 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001327 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 size, dynamic);
1329 fHWGeometryState.fVertexBuffer = vertexBuffer;
1330 return vertexBuffer;
1331 }
1332 return NULL;
1333}
1334
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001335GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001336 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001337 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001339 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001340 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001342 GL_ALLOC_CALL(this->glInterface(),
1343 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1344 size,
1345 NULL, // data ptr
1346 dynamic ? GR_GL_DYNAMIC_DRAW :
1347 GR_GL_STATIC_DRAW));
1348 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001349 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 // deleting bound buffer does implicit bind to 0
1351 fHWGeometryState.fIndexBuffer = NULL;
1352 return NULL;
1353 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001354 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001355 size, dynamic);
1356 fHWGeometryState.fIndexBuffer = indexBuffer;
1357 return indexBuffer;
1358 }
1359 return NULL;
1360}
1361
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001362void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001363 const GrDrawState& drawState = this->getDrawState();
1364 const GrGLRenderTarget* rt =
1365 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1366
1367 GrAssert(NULL != rt);
1368 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001369
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001370 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001371 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1372 rect.width(), rect.height());
1373 if (scissor.contains(vp)) {
1374 disableScissor();
1375 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 }
1377
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001378 if (fHWBounds.fScissorRect != scissor) {
1379 scissor.pushToGLScissor(this->glInterface());
1380 fHWBounds.fScissorRect = scissor;
1381 }
1382 if (!fHWBounds.fScissorEnabled) {
1383 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1384 fHWBounds.fScissorEnabled = true;
1385 }
1386}
1387
1388void GrGpuGL::disableScissor() {
1389 if (fHWBounds.fScissorEnabled) {
1390 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1391 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001392 }
1393}
1394
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001395void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001396 const GrDrawState& drawState = this->getDrawState();
1397 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001398 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001399 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001400
bsalomon@google.com74b98712011-11-11 19:46:16 +00001401 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001402 if (NULL != rect) {
1403 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001404 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001405 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001406 if (clippedRect.intersect(rtRect)) {
1407 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001408 } else {
1409 return;
1410 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001412 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001413 if (NULL != rect)
1414 this->enableScissoring(*rect);
1415 else
1416 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001417
1418 GrGLfloat r, g, b, a;
1419 static const GrGLfloat scale255 = 1.f / 255.f;
1420 a = GrColorUnpackA(color) * scale255;
1421 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001422 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001423 scaleRGB *= a;
1424 }
1425 r = GrColorUnpackR(color) * scaleRGB;
1426 g = GrColorUnpackG(color) * scaleRGB;
1427 b = GrColorUnpackB(color) * scaleRGB;
1428
1429 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001430 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001431 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001432 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001433}
1434
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001435void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001436 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001437 return;
1438 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001439
1440 this->flushRenderTarget(&GrIRect::EmptyIRect());
1441
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001442 this->disableScissor();
1443
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001444 GL_CALL(StencilMask(0xffffffff));
1445 GL_CALL(ClearStencil(0));
1446 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001447 fHWStencilSettings.invalidate();
1448 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001449}
1450
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001451void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001452 const GrDrawState& drawState = this->getDrawState();
1453 const GrRenderTarget* rt = drawState.getRenderTarget();
1454 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001455
1456 // this should only be called internally when we know we have a
1457 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001458 GrAssert(NULL != rt->getStencilBuffer());
1459 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001460#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001461 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001462 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001463#else
1464 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001465 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001466 // turned into draws. Our contract on GrDrawTarget says that
1467 // changing the clip between stencil passes may or may not
1468 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001469 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001470#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001471 GrGLint value;
1472 if (insideClip) {
1473 value = (1 << (stencilBitCount - 1));
1474 } else {
1475 value = 0;
1476 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001477 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001478 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001479 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001480 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001481 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001482 fHWStencilSettings.invalidate();
1483 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001484}
1485
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001486void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001487 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001488}
1489
bsalomon@google.comc4364992011-11-07 15:54:49 +00001490bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1491 int left, int top,
1492 int width, int height,
1493 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001494 size_t rowBytes) const {
1495 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001496 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001497 return false;
1498 }
1499
1500 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001501 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001502 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001503 return true;
1504 }
1505 // If we have to do memcpys to handle rowBytes then y-flip is free
1506 // Note the rowBytes might be tight to the passed in data, but if data
1507 // gets clipped in x to the target the rowBytes will no longer be tight.
1508 if (left >= 0 && (left + width) < renderTarget->width()) {
1509 return 0 == rowBytes ||
1510 GrBytesPerPixel(config) * width == rowBytes;
1511 } else {
1512 return false;
1513 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001514}
1515
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001516bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001517 int left, int top,
1518 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001519 GrPixelConfig config,
1520 void* buffer,
1521 size_t rowBytes,
1522 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001523 GrGLenum format;
1524 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001525 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001526 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001527 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001528 size_t bpp = GrBytesPerPixel(config);
1529 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1530 &left, &top, &width, &height,
1531 const_cast<const void**>(&buffer),
1532 &rowBytes)) {
1533 return false;
1534 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001535
bsalomon@google.comc6980972011-11-02 19:57:21 +00001536 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001537 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001538 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001539 switch (tgt->getResolveType()) {
1540 case GrGLRenderTarget::kCantResolve_ResolveType:
1541 return false;
1542 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001543 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001544 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001545 break;
1546 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001547 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001548 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001549 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1550 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001551 break;
1552 default:
1553 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001554 }
1555
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001556 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001557
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001558 // the read rect is viewport-relative
1559 GrGLIRect readRect;
1560 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001561
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001562 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001563 if (0 == rowBytes) {
1564 rowBytes = tightRowBytes;
1565 }
1566 size_t readDstRowBytes = tightRowBytes;
1567 void* readDst = buffer;
1568
1569 // determine if GL can read using the passed rowBytes or if we need
1570 // a scratch buffer.
1571 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1572 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001573 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001574 GrAssert(!(rowBytes % sizeof(GrColor)));
1575 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1576 readDstRowBytes = rowBytes;
1577 } else {
1578 scratch.reset(tightRowBytes * height);
1579 readDst = scratch.get();
1580 }
1581 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001582 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001583 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1584 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001585 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1586 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001587 format, type, readDst));
1588 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001589 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001590 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1591 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001592 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001593 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1594 invertY = true;
1595 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001596
1597 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001598 // API presents top-to-bottom. We must preserve the padding contents. Note
1599 // that the above readPixels did not overwrite the padding.
1600 if (readDst == buffer) {
1601 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001602 if (!invertY) {
1603 scratch.reset(tightRowBytes);
1604 void* tmpRow = scratch.get();
1605 // flip y in-place by rows
1606 const int halfY = height >> 1;
1607 char* top = reinterpret_cast<char*>(buffer);
1608 char* bottom = top + (height - 1) * rowBytes;
1609 for (int y = 0; y < halfY; y++) {
1610 memcpy(tmpRow, top, tightRowBytes);
1611 memcpy(top, bottom, tightRowBytes);
1612 memcpy(bottom, tmpRow, tightRowBytes);
1613 top += rowBytes;
1614 bottom -= rowBytes;
1615 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001616 }
1617 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001618 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001619 // copy from readDst to buffer while flipping y
1620 const int halfY = height >> 1;
1621 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001622 char* dst = reinterpret_cast<char*>(buffer);
1623 if (!invertY) {
1624 dst += (height-1) * rowBytes;
1625 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001626 for (int y = 0; y < height; y++) {
1627 memcpy(dst, src, tightRowBytes);
1628 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001629 if (invertY) {
1630 dst += rowBytes;
1631 } else {
1632 dst -= rowBytes;
1633 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001634 }
1635 }
1636 return true;
1637}
1638
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001639void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001640
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001641 GrGLRenderTarget* rt =
1642 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1643 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001644
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001645 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001646 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 #if GR_COLLECT_STATS
1648 ++fStats.fRenderTargetChngCnt;
1649 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001650 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001651 GrGLenum status;
1652 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001653 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001654 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 }
1656 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001657 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001658 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001659 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001660 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001661 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001662 fHWBounds.fViewportRect = vp;
1663 }
1664 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001665 if (NULL == bound || !bound->isEmpty()) {
1666 rt->flagAsNeedingResolve(bound);
1667 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001668}
1669
twiz@google.com0f31ca72011-03-18 17:38:11 +00001670GrGLenum gPrimitiveType2GLMode[] = {
1671 GR_GL_TRIANGLES,
1672 GR_GL_TRIANGLE_STRIP,
1673 GR_GL_TRIANGLE_FAN,
1674 GR_GL_POINTS,
1675 GR_GL_LINES,
1676 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001677};
1678
bsalomon@google.comd302f142011-03-03 13:54:13 +00001679#define SWAP_PER_DRAW 0
1680
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001681#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001682 #if GR_MAC_BUILD
1683 #include <AGL/agl.h>
1684 #elif GR_WIN32_BUILD
1685 void SwapBuf() {
1686 DWORD procID = GetCurrentProcessId();
1687 HWND hwnd = GetTopWindow(GetDesktopWindow());
1688 while(hwnd) {
1689 DWORD wndProcID = 0;
1690 GetWindowThreadProcessId(hwnd, &wndProcID);
1691 if(wndProcID == procID) {
1692 SwapBuffers(GetDC(hwnd));
1693 }
1694 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1695 }
1696 }
1697 #endif
1698#endif
1699
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001700void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1701 uint32_t startVertex,
1702 uint32_t startIndex,
1703 uint32_t vertexCount,
1704 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001705 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1706
twiz@google.com0f31ca72011-03-18 17:38:11 +00001707 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001708
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001709 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1710 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1711
1712 // our setupGeometry better have adjusted this to zero since
1713 // DrawElements always draws from the begining of the arrays for idx 0.
1714 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001715
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001716 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1717 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001718#if SWAP_PER_DRAW
1719 glFlush();
1720 #if GR_MAC_BUILD
1721 aglSwapBuffers(aglGetCurrentContext());
1722 int set_a_break_pt_here = 9;
1723 aglSwapBuffers(aglGetCurrentContext());
1724 #elif GR_WIN32_BUILD
1725 SwapBuf();
1726 int set_a_break_pt_here = 9;
1727 SwapBuf();
1728 #endif
1729#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001730}
1731
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001732void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1733 uint32_t startVertex,
1734 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001735 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1736
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001737 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1738
1739 // our setupGeometry better have adjusted this to zero.
1740 // DrawElements doesn't take an offset so we always adjus the startVertex.
1741 GrAssert(0 == startVertex);
1742
1743 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1744 // account for startVertex in the DrawElements case. So we always
1745 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001746 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001747#if SWAP_PER_DRAW
1748 glFlush();
1749 #if GR_MAC_BUILD
1750 aglSwapBuffers(aglGetCurrentContext());
1751 int set_a_break_pt_here = 9;
1752 aglSwapBuffers(aglGetCurrentContext());
1753 #elif GR_WIN32_BUILD
1754 SwapBuf();
1755 int set_a_break_pt_here = 9;
1756 SwapBuf();
1757 #endif
1758#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001759}
1760
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001761void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1762
1763 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001764
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001765 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001766 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001767 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001768 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1769 rt->renderFBOID()));
1770 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1771 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001772 #if GR_COLLECT_STATS
1773 ++fStats.fRenderTargetChngCnt;
1774 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001775 // make sure we go through flushRenderTarget() since we've modified
1776 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001777 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001778 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001779 const GrIRect dirtyRect = rt->getResolveRect();
1780 GrGLIRect r;
1781 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1782 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001783
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001784 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001785 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001786#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001787 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1788 GL_CALL(Scissor(r.fLeft, r.fBottom,
1789 r.fWidth, r.fHeight));
1790 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001791 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001792 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001793#else
1794 this->enableScissoring(dirtyRect);
1795 GL_CALL(ResolveMultisampleFramebuffer());
1796#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001797 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001798 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001799 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001800 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1801 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001802 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001803 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001804 int right = r.fLeft + r.fWidth;
1805 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001806 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1807 r.fLeft, r.fBottom, right, top,
1808 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001809 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001810 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001811 }
1812}
1813
twiz@google.com0f31ca72011-03-18 17:38:11 +00001814static const GrGLenum grToGLStencilFunc[] = {
1815 GR_GL_ALWAYS, // kAlways_StencilFunc
1816 GR_GL_NEVER, // kNever_StencilFunc
1817 GR_GL_GREATER, // kGreater_StencilFunc
1818 GR_GL_GEQUAL, // kGEqual_StencilFunc
1819 GR_GL_LESS, // kLess_StencilFunc
1820 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1821 GR_GL_EQUAL, // kEqual_StencilFunc,
1822 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001823};
1824GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1825GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1826GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1827GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1828GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1829GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1830GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1831GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1832GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1833
twiz@google.com0f31ca72011-03-18 17:38:11 +00001834static const GrGLenum grToGLStencilOp[] = {
1835 GR_GL_KEEP, // kKeep_StencilOp
1836 GR_GL_REPLACE, // kReplace_StencilOp
1837 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1838 GR_GL_INCR, // kIncClamp_StencilOp
1839 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1840 GR_GL_DECR, // kDecClamp_StencilOp
1841 GR_GL_ZERO, // kZero_StencilOp
1842 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001843};
1844GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1845GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1846GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1847GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1848GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1849GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1850GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1851GR_STATIC_ASSERT(6 == kZero_StencilOp);
1852GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1853
reed@google.comac10a2d2010-12-22 21:39:39 +00001854void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001855 const GrDrawState& drawState = this->getDrawState();
1856
1857 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001858
1859 // use stencil for clipping if clipping is enabled and the clip
1860 // has been written into the stencil.
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001861 StencilClipMode clipMode;
1862 if (fClipMaskManager.isClipInStencil() &&
1863 drawState.isClipState()) {
1864 clipMode = kUseClip_StencilClipMode;
1865 // We can't be modifying the clip and respecting it at the same time.
1866 GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1867 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
1868 clipMode = kModifyClip_StencilClipMode;
1869 } else {
1870 clipMode = kIgnoreClip_StencilClipMode;
1871 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001872
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001873 bool stencilChange = (fHWStencilSettings != *settings) ||
1874 (fHWStencilClipMode != clipMode);
reed@google.comac10a2d2010-12-22 21:39:39 +00001875 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001876
bsalomon@google.comd302f142011-03-03 13:54:13 +00001877 if (settings->isDisabled()) {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001878 if (kUseClip_StencilClipMode == clipMode) {
digit@google.com9b482c42012-02-16 22:03:26 +00001879 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001880 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001881 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001882
1883 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001884 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001885 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001886 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001888 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001889 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1890 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1891 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1892 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1893 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1894 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1895 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1896 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897 }
1898 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001899 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001900 GrStencilBuffer* stencilBuffer =
1901 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001902 if (NULL != stencilBuffer) {
1903 stencilBits = stencilBuffer->bits();
1904 }
1905 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001906 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001907
1908 GrGLuint clipStencilMask = 0;
1909 GrGLuint userStencilMask = ~0;
1910 if (stencilBits > 0) {
1911 clipStencilMask = 1 << (stencilBits - 1);
1912 userStencilMask = clipStencilMask - 1;
1913 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001914
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001915 unsigned int frontRef = settings->frontFuncRef();
1916 unsigned int frontMask = settings->frontFuncMask();
1917 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001918 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001919
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001920 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001921 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1922 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001924 bool useClip = kUseClip_StencilClipMode == clipMode;
1925 frontFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1926 settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001927
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001928 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001929 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930 clipStencilMask,
1931 userStencilMask,
1932 &frontRef,
1933 &frontMask);
1934 frontWriteMask &= userStencilMask;
1935 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001936 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001937 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001938 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001939 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001940 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001941 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001942 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001943 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001944 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001945 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001946
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001947 unsigned int backRef = settings->backFuncRef();
1948 unsigned int backMask = settings->backFuncMask();
1949 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001950
1951
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001952 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001953 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1954 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001955 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001956 bool useClip = kUseClip_StencilClipMode == clipMode;
1957 backFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1958 settings->backFunc())];
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001959 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001960 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001961 clipStencilMask,
1962 userStencilMask,
1963 &backRef,
1964 &backMask);
1965 backWriteMask &= userStencilMask;
1966 }
1967
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001968 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1969 frontRef, frontMask));
1970 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1971 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1972 backRef, backMask));
1973 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1974 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001975 grToGLStencilOp[settings->frontFailOp()],
1976 grToGLStencilOp[settings->frontPassOp()],
1977 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001978
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001980 grToGLStencilOp[settings->backFailOp()],
1981 grToGLStencilOp[settings->backPassOp()],
1982 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001983 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001984 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1985 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001986 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1987 grToGLStencilOp[settings->frontPassOp()],
1988 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001989 }
1990 }
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001991 fHWStencilSettings = *settings;
1992 fHWStencilClipMode = clipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001993 }
1994}
1995
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001996void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001997 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001998 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001999 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
2000 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002001 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002002 bool smoothLines = false;
2003
bsalomon@google.com0650e812011-04-08 18:07:53 +00002004 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002005 smoothLines = this->willUseHWAALines();
2006 if (smoothLines) {
2007 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
2008 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
2009 fHWAAState.fSmoothLineEnabled = kYes_TriState;
2010 // must disable msaa to use line smoothing
2011 if (rt->isMultisampled() &&
2012 kNo_TriState != fHWAAState.fMSAAEnabled) {
2013 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2014 fHWAAState.fMSAAEnabled = kNo_TriState;
2015 }
2016 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002017 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002018 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
2019 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
2020 fHWAAState.fSmoothLineEnabled = kNo_TriState;
2021 }
2022 }
2023 }
2024 if (!smoothLines && rt->isMultisampled()) {
2025 if (this->getDrawState().isHWAntialiasState()) {
2026 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2027 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2028 fHWAAState.fMSAAEnabled = kYes_TriState;
2029 }
2030 } else {
2031 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2032 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2033 fHWAAState.fMSAAEnabled = kNo_TriState;
2034 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002035 }
2036 }
2037 }
2038}
2039
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002040void GrGpuGL::flushBlend(GrPrimitiveType type,
2041 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002042 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002043 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002044 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002045 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002046 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002047 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002048 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
2049 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002050 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2051 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002052 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
2053 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002054 }
2055 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002056 // any optimization to disable blending should
2057 // have already been applied and tweaked the coeffs
2058 // to (1, 0).
2059 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2060 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002061 if (blendOff) {
2062 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002063 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002064 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002065 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002066 } else {
2067 if (kYes_TriState != fHWBlendState.fEnabled) {
2068 GL_CALL(Enable(GR_GL_BLEND));
2069 fHWBlendState.fEnabled = kYes_TriState;
2070 }
2071 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2072 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002073 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2074 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002075 fHWBlendState.fSrcCoeff = srcCoeff;
2076 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002077 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002078 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002079 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2080 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002081 (!fHWBlendState.fConstColorValid ||
2082 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002083
2084 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002085 GrColorUnpackR(blendConst) / 255.f,
2086 GrColorUnpackG(blendConst) / 255.f,
2087 GrColorUnpackB(blendConst) / 255.f,
2088 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002089 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002090 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002091 fHWBlendState.fConstColor = blendConst;
2092 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002093 }
2094 }
2095 }
2096}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002097namespace {
2098
2099unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002100 switch (filter) {
2101 case GrSamplerState::kBilinear_Filter:
2102 case GrSamplerState::k4x4Downsample_Filter:
2103 return GR_GL_LINEAR;
2104 case GrSamplerState::kNearest_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002105 return GR_GL_NEAREST;
2106 default:
2107 GrAssert(!"Unknown filter type");
2108 return GR_GL_LINEAR;
2109 }
2110}
2111
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002112// get_swizzle is only called from this .cpp so it is OK to inline it here
2113inline const GrGLenum* get_swizzle(GrPixelConfig config,
2114 const GrSamplerState& sampler,
2115 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002116 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002117 if (glCaps.textureRedSupport()) {
2118 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2119 GR_GL_RED, GR_GL_RED };
2120 return gRedSmear;
2121 } else {
2122 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2123 GR_GL_ALPHA, GR_GL_ALPHA };
2124 return gAlphaSmear;
2125 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002126 } else if (sampler.swapsRAndB()) {
2127 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2128 GR_GL_RED, GR_GL_ALPHA };
2129 return gRedBlueSwap;
2130 } else {
2131 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2132 GR_GL_BLUE, GR_GL_ALPHA };
2133 return gStraight;
2134 }
2135}
2136
2137void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002138 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2139 GR_GL_TEXTURE_SWIZZLE_RGBA,
2140 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002141}
2142}
2143
bsalomon@google.comffca4002011-02-22 20:34:01 +00002144bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002145
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002146 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002147 // GrGpu::setupClipAndFlushState should have already checked this
2148 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002149 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002150
tomhudson@google.com93813632011-10-27 20:21:16 +00002151 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002152 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002153 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002154 GrGLTexture* nextTexture =
2155 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002156
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002157 // true for now, but maybe not with GrEffect.
2158 GrAssert(NULL != nextTexture);
2159 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002160 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002161 // the last bound texture, but it needs resolving. So keep this
2162 // out of the "last != next" check.
2163 GrGLRenderTarget* texRT =
2164 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2165 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002166 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002167 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002168
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002169 if (fHWBoundTextures[s] != nextTexture) {
2170 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002171 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002172 #if GR_COLLECT_STATS
2173 ++fStats.fTextureChngCnt;
2174 #endif
2175 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002176 fHWBoundTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002177 // The texture matrix has to compensate for texture width/height
2178 // and NPOT-embedded-in-POT
2179 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002180 }
2181
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002182 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002183 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002184 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002185 nextTexture->getCachedTexParams(&timestamp);
2186 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002187 GrGLTexture::TexParams newTexParams;
2188
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002189 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002190
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002191 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002192 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2193 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002194 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002195 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002196 sizeof(newTexParams.fSwizzleRGBA));
2197 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002198 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002199 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002200 GR_GL_TEXTURE_MAG_FILTER,
2201 newTexParams.fFilter));
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_MIN_FILTER,
2204 newTexParams.fFilter));
2205 }
2206 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002207 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002208 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002209 GR_GL_TEXTURE_WRAP_S,
2210 newTexParams.fWrapS));
2211 }
2212 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002213 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002214 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002215 GR_GL_TEXTURE_WRAP_T,
2216 newTexParams.fWrapT));
2217 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002218 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002219 (setAll ||
2220 memcmp(newTexParams.fSwizzleRGBA,
2221 oldTexParams.fSwizzleRGBA,
2222 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002223 this->setTextureUnit(s);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002224 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2225 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002226 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002227 nextTexture->setCachedTexParams(newTexParams,
2228 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002229 }
2230 }
2231
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002232 GrIRect* rect = NULL;
2233 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002234 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002235 fClip.hasConservativeBounds()) {
2236 fClip.getConservativeBounds().roundOut(&clipBounds);
2237 rect = &clipBounds;
2238 }
2239 this->flushRenderTarget(rect);
2240 this->flushAAState(type);
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002241
2242 if (drawState->isDitherState()) {
2243 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002244 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002245 fHWDitherEnabled = kYes_TriState;
2246 }
2247 } else {
2248 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002249 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002250 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 }
2252 }
2253
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002254 if (drawState->isColorWriteDisabled()) {
2255 if (kNo_TriState != fHWWriteToColor) {
2256 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2257 GR_GL_FALSE, GR_GL_FALSE));
2258 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002259 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002260 } else {
2261 if (kYes_TriState != fHWWriteToColor) {
2262 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2263 fHWWriteToColor = kYes_TriState;
2264 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002265 }
2266
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002267 if (fHWDrawFace != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002268 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002269 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002270 GL_CALL(Enable(GR_GL_CULL_FACE));
2271 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002272 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002273 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002274 GL_CALL(Enable(GR_GL_CULL_FACE));
2275 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002276 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002277 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002278 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002279 break;
2280 default:
2281 GrCrash("Unknown draw face.");
2282 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002283 fHWDrawFace = drawState->getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002284 }
2285
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002286#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002287 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002288 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002289 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002290 NULL == drawState->getRenderTarget() ||
2291 NULL == drawState->getTexture(s) ||
2292 drawState->getTexture(s)->asRenderTarget() !=
2293 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002294 }
2295#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002296
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002297 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002298
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002299 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002300}
2301
2302void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002303 if (fHWGeometryState.fVertexBuffer != buffer) {
2304 fHWGeometryState.fArrayPtrsDirty = true;
2305 fHWGeometryState.fVertexBuffer = buffer;
2306 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002307}
2308
2309void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002310 if (fHWGeometryState.fVertexBuffer == buffer) {
2311 // deleting bound buffer does implied bind to 0
2312 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002313 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002314 }
2315}
2316
2317void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002318 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002319}
2320
2321void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002322 if (fHWGeometryState.fIndexBuffer == buffer) {
2323 // deleting bound buffer does implied bind to 0
2324 fHWGeometryState.fIndexBuffer = NULL;
2325 }
2326}
2327
reed@google.comac10a2d2010-12-22 21:39:39 +00002328void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2329 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002330 GrDrawState* drawState = this->drawState();
2331 if (drawState->getRenderTarget() == renderTarget) {
2332 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002333 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002334 if (fHWBoundRenderTarget == renderTarget) {
2335 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002336 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002337}
2338
2339void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002340 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002341 GrDrawState* drawState = this->drawState();
2342 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002343 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002344 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002345 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002346 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002347 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002348 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002349 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002350}
2351
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002352bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2353 bool getSizedInternalFormat,
2354 GrGLenum* internalFormat,
2355 GrGLenum* externalFormat,
2356 GrGLenum* externalType) {
2357 GrGLenum dontCare;
2358 if (NULL == internalFormat) {
2359 internalFormat = &dontCare;
2360 }
2361 if (NULL == externalFormat) {
2362 externalFormat = &dontCare;
2363 }
2364 if (NULL == externalType) {
2365 externalType = &dontCare;
2366 }
2367
reed@google.comac10a2d2010-12-22 21:39:39 +00002368 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002369 case kRGBA_8888_PM_GrPixelConfig:
2370 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002371 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002372 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002373 if (getSizedInternalFormat) {
2374 *internalFormat = GR_GL_RGBA8;
2375 } else {
2376 *internalFormat = GR_GL_RGBA;
2377 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002378 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002379 break;
2380 case kBGRA_8888_PM_GrPixelConfig:
2381 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002382 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002383 return false;
2384 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002385 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002386 if (getSizedInternalFormat) {
2387 *internalFormat = GR_GL_BGRA8;
2388 } else {
2389 *internalFormat = GR_GL_BGRA;
2390 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002391 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002392 if (getSizedInternalFormat) {
2393 *internalFormat = GR_GL_RGBA8;
2394 } else {
2395 *internalFormat = GR_GL_RGBA;
2396 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002397 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002398 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002399 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002400 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002401 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002402 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002403 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002404 if (getSizedInternalFormat) {
2405 if (this->glBinding() == kDesktop_GrGLBinding) {
2406 return false;
2407 } else {
2408 *internalFormat = GR_GL_RGB565;
2409 }
2410 } else {
2411 *internalFormat = GR_GL_RGB;
2412 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002413 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002414 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002415 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002416 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002417 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002418 if (getSizedInternalFormat) {
2419 *internalFormat = GR_GL_RGBA4;
2420 } else {
2421 *internalFormat = GR_GL_RGBA;
2422 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002423 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002424 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002425 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002426 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002427 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002428 // glCompressedTexImage doesn't take external params
2429 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002430 // no sized/unsized internal format distinction here
2431 *internalFormat = GR_GL_PALETTE8_RGBA8;
2432 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002433 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002434 } else {
2435 return false;
2436 }
2437 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002438 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002439 if (this->glCaps().textureRedSupport()) {
2440 *internalFormat = GR_GL_RED;
2441 *externalFormat = GR_GL_RED;
2442 if (getSizedInternalFormat) {
2443 *internalFormat = GR_GL_R8;
2444 } else {
2445 *internalFormat = GR_GL_RED;
2446 }
2447 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002448 } else {
2449 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002450 *externalFormat = GR_GL_ALPHA;
2451 if (getSizedInternalFormat) {
2452 *internalFormat = GR_GL_ALPHA8;
2453 } else {
2454 *internalFormat = GR_GL_ALPHA;
2455 }
2456 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002457 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002458 break;
2459 default:
2460 return false;
2461 }
2462 return true;
2463}
2464
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002465void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002466 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002467 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002468 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002469 fActiveTextureUnitIdx = unit;
2470 }
2471}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002472
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002473void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002474 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002475 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002476 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2477 }
2478}
2479
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002480void GrGpuGL::resetDirtyFlags() {
2481 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2482}
2483
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002484void GrGpuGL::setBuffers(bool indexed,
2485 int* extraVertexOffset,
2486 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002487
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002488 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002489
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002490 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2491
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002492 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002493 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002494 case kBuffer_GeometrySrcType:
2495 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002496 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002497 break;
2498 case kArray_GeometrySrcType:
2499 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002500 this->finalizeReservedVertices();
2501 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2502 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002503 break;
2504 default:
2505 vbuf = NULL; // suppress warning
2506 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002507 }
2508
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002509 GrAssert(NULL != vbuf);
2510 GrAssert(!vbuf->isLocked());
2511 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002512 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002513 fHWGeometryState.fArrayPtrsDirty = true;
2514 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002515 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002516
2517 if (indexed) {
2518 GrAssert(NULL != extraIndexOffset);
2519
2520 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002521 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002522 case kBuffer_GeometrySrcType:
2523 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002524 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002525 break;
2526 case kArray_GeometrySrcType:
2527 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002528 this->finalizeReservedIndices();
2529 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2530 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002531 break;
2532 default:
2533 ibuf = NULL; // suppress warning
2534 GrCrash("Unknown geometry src type!");
2535 }
2536
2537 GrAssert(NULL != ibuf);
2538 GrAssert(!ibuf->isLocked());
2539 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002540 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002541 fHWGeometryState.fIndexBuffer = ibuf;
2542 }
2543 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002544}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002545