blob: 62dcb4f21f905cbf93d83ea041cc9bbe62205a85 [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.comfe676522011-06-17 18:12:21 +0000210 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000211 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000212}
213
214GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000215 // This must be called by before the GrDrawTarget destructor
216 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000217 // This subclass must do this before the base class destructor runs
218 // since we will unref the GrGLInterface.
219 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000220}
221
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000222///////////////////////////////////////////////////////////////////////////////
223
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000224void GrGpuGL::initCaps() {
225 GrGLint maxTextureUnits;
226 // check FS and fixed-function texture unit limits
227 // we only use textures in the fragment stage currently.
228 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000229 const GrGLInterface* gl = this->glInterface();
230 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000231 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000232
233 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000234 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000236 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 for (int i = 0; i < numFormats; ++i) {
238 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
239 fCaps.f8BitPaletteSupport = true;
240 break;
241 }
242 }
243
244 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000245 // we could also look for GL_ATI_separate_stencil extension or
246 // GL_EXT_stencil_two_side but they use different function signatures
247 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000248 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000249 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000250 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 this->hasExtension("GL_EXT_stencil_wrap");
252 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000253 // ES 2 has two sided stencil and stencil wrap
254 fCaps.fTwoSidedStencilSupport = true;
255 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000256 }
257
258 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
260 // extension includes glMapBuffer.
261 } else {
262 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
263 }
264
265 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000266 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000267 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
268 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000269 } else {
270 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000271 }
272 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000273 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000274 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000275 }
276
277 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
278
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000279 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
280 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000281 // Our render targets are always created with textures as the color
282 // attachment, hence this min:
283 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
284
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000285 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000286}
287
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000288void GrGpuGL::fillInConfigRenderableTable() {
289
290 // OpenGL < 3.0
291 // no support for render targets unless the GL_ARB_framebuffer_object
292 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
293 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
294 // probably don't get R8 in this case.
295
296 // OpenGL 3.0
297 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
298 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
299
300 // >= OpenGL 3.1
301 // base color renderable: RED, RG, RGB, and RGBA
302 // sized derivatives: R8, RGBA4, RGBA8
303 // if the GL_ARB_compatibility extension is supported then we get back
304 // support for GL_ALPHA and ALPHA8
305
306 // GL_EXT_bgra adds BGRA render targets to any version
307
308 // ES 2.0
309 // color renderable: RGBA4, RGB5_A1, RGB565
310 // GL_EXT_texture_rg adds support for R8 as a color render target
311 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
312 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
313 // added BGRA support
314
315 if (kDesktop_GrGLBinding == this->glBinding()) {
316 // Post 3.0 we will get R8
317 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
318 if (this->glVersion() >= GR_GL_VER(3,0) ||
319 this->hasExtension("GL_ARB_framebuffer_object")) {
320 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
321 }
322 } else {
323 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000324 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
325 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000326 }
327
328 if (kDesktop_GrGLBinding != this->glBinding()) {
329 // only available in ES
330 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
331 }
332
333 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
334 // GL_EXT_framebuffer_object for FBO support. Both of these
335 // allow RGBA4 render targets so this is always supported.
336 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
337
338 if (this->glCaps().rgba8RenderbufferSupport()) {
339 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
340 }
341
342 if (this->glCaps().bgraFormatSupport()) {
343 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
344 }
345
346 // the un-premultiplied formats just inherit the premultiplied setting
347 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
348 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
349 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
350 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
351}
352
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000353bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
354 if (kUnknown_CanPreserveUnpremulRoundtrip ==
355 fCanPreserveUnpremulRoundtrip) {
356
357 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
358 uint32_t* srcData = data.get();
359 uint32_t* firstRead = data.get() + 256 * 256;
360 uint32_t* secondRead = data.get() + 2 * 256 * 256;
361
362 for (int y = 0; y < 256; ++y) {
363 for (int x = 0; x < 256; ++x) {
364 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
365 color[3] = y;
366 color[2] = x;
367 color[1] = x;
368 color[0] = x;
369 }
370 }
371
372 // We have broader support for read/write pixels on render targets
373 // than on textures.
374 GrTextureDesc dstDesc;
375 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
376 kNoStencil_GrTextureFlagBit;
377 dstDesc.fWidth = 256;
378 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000379 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000380 dstDesc.fSampleCnt = 0;
381
382 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
383 if (!dstTex.get()) {
384 return false;
385 }
386 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
387 GrAssert(NULL != rt);
388
389 bool failed = true;
390 static const UnpremulConversion gMethods[] = {
391 kUpOnWrite_DownOnRead_UnpremulConversion,
392 kDownOnWrite_UpOnRead_UnpremulConversion,
393 };
394
395 // pretend that we can do the roundtrip to avoid recursive calls to
396 // this function
397 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
398 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
399 fUnpremulConversion = gMethods[i];
400 rt->writePixels(0, 0,
401 256, 256,
402 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
403 rt->readPixels(0, 0,
404 256, 256,
405 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
406 rt->writePixels(0, 0,
407 256, 256,
408 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
409 rt->readPixels(0, 0,
410 256, 256,
411 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
412 failed = false;
413 for (int j = 0; j < 256 * 256; ++j) {
414 if (firstRead[j] != secondRead[j]) {
415 failed = true;
416 break;
417 }
418 }
419 }
420 fCanPreserveUnpremulRoundtrip = failed ?
421 kNo_CanPreserveUnpremulRoundtrip :
422 kYes_CanPreserveUnpremulRoundtrip;
423 }
424
425 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
426 return true;
427 } else {
428 return false;
429 }
430}
431
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000432GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000433 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
434 return GrPixelConfigSwapRAndB(config);
435 } else {
436 return config;
437 }
438}
439
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000440GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000441 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000442 return GrPixelConfigSwapRAndB(config);
443 } else {
444 return config;
445 }
446}
447
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000448bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
449 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
450}
451
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000452void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000453 if (gPrintStartupSpew && !fPrintedCaps) {
454 fPrintedCaps = true;
455 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000456 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000457 }
458
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000459 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000460 GL_CALL(Disable(GR_GL_DEPTH_TEST));
461 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000462
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000463 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
464 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000466 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000467 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000468 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000469 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
470 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
471 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
472 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
473 GL_CALL(Disable(GR_GL_COLOR_TABLE));
474 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
475 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
476 // Since ES doesn't support glPointSize at all we always use the VS to
477 // set the point size
478 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
479
480 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
481 // currently part of our gl interface. There are probably others as
482 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000483 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000484 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000485 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000486
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000488 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000489
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000490 // invalid
491 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000492
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000493 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000494
tomhudson@google.com93813632011-10-27 20:21:16 +0000495 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000496 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000497 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000498
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000499 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000500 // set to true to force disableScissor to make a GL call.
501 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000502 this->disableScissor();
503
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000504 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000505
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000506 fHWStencilSettings.invalidate();
507 fHWStencilClipMode = kInvalid_StencilClipMode;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000508
509 // TODO: I believe this should actually go in GrGpu::onResetContext
510 // rather than here
511 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000512
513 fHWGeometryState.fIndexBuffer = NULL;
514 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000515
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000516 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000517
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000518 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000519
520 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000521 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000522 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
523 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000524 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000525 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
526 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000527 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000528 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
529 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000530 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000531 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
532 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000533}
534
bsalomon@google.come269f212011-11-07 13:29:52 +0000535GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000536 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000537 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000538 return NULL;
539 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000540
bsalomon@google.com99621082011-11-15 16:47:16 +0000541 glTexDesc.fWidth = desc.fWidth;
542 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000543 glTexDesc.fConfig = desc.fConfig;
544 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
545 glTexDesc.fOwnsID = false;
546 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
547
548 GrGLTexture* texture = NULL;
549 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
550 GrGLRenderTarget::Desc glRTDesc;
551 glRTDesc.fRTFBOID = 0;
552 glRTDesc.fTexFBOID = 0;
553 glRTDesc.fMSColorRenderbufferID = 0;
554 glRTDesc.fOwnIDs = true;
555 glRTDesc.fConfig = desc.fConfig;
556 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000557 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
558 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000559 glTexDesc.fTextureID,
560 &glRTDesc)) {
561 return NULL;
562 }
563 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
564 } else {
565 texture = new GrGLTexture(this, glTexDesc);
566 }
567 if (NULL == texture) {
568 return NULL;
569 }
570
571 this->setSpareTextureUnit();
572 return texture;
573}
574
575GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
576 GrGLRenderTarget::Desc glDesc;
577 glDesc.fConfig = desc.fConfig;
578 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
579 glDesc.fMSColorRenderbufferID = 0;
580 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
581 glDesc.fSampleCnt = desc.fSampleCnt;
582 glDesc.fOwnIDs = false;
583 GrGLIRect viewport;
584 viewport.fLeft = 0;
585 viewport.fBottom = 0;
586 viewport.fWidth = desc.fWidth;
587 viewport.fHeight = desc.fHeight;
588
589 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
590 if (desc.fStencilBits) {
591 GrGLStencilBuffer::Format format;
592 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
593 format.fPacked = false;
594 format.fStencilBits = desc.fStencilBits;
595 format.fTotalBits = desc.fStencilBits;
596 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
597 0,
598 desc.fWidth,
599 desc.fHeight,
600 desc.fSampleCnt,
601 format);
602 tgt->setStencilBuffer(sb);
603 sb->unref();
604 }
605 return tgt;
606}
607
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000608////////////////////////////////////////////////////////////////////////////////
609
bsalomon@google.com6f379512011-11-16 20:36:03 +0000610void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
611 int left, int top, int width, int height,
612 GrPixelConfig config, const void* buffer,
613 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000614 if (NULL == buffer) {
615 return;
616 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000617 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
618
bsalomon@google.com6f379512011-11-16 20:36:03 +0000619 this->setSpareTextureUnit();
620 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
621 GrGLTexture::Desc desc;
622 desc.fConfig = glTex->config();
623 desc.fWidth = glTex->width();
624 desc.fHeight = glTex->height();
625 desc.fOrientation = glTex->orientation();
626 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000627
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000628 this->uploadTexData(desc, false,
629 left, top, width, height,
630 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000631}
632
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000633namespace {
634bool adjust_pixel_ops_params(int surfaceWidth,
635 int surfaceHeight,
636 size_t bpp,
637 int* left, int* top, int* width, int* height,
638 const void** data,
639 size_t* rowBytes) {
640 if (!*rowBytes) {
641 *rowBytes = *width * bpp;
642 }
643
644 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
645 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
646
647 if (!subRect.intersect(bounds)) {
648 return false;
649 }
650 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
651 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
652
653 *left = subRect.fLeft;
654 *top = subRect.fTop;
655 *width = subRect.width();
656 *height = subRect.height();
657 return true;
658}
659}
660
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000661bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
662 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000663 int left, int top, int width, int height,
664 GrPixelConfig dataConfig,
665 const void* data,
666 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000667 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000668
669 size_t bpp = GrBytesPerPixel(dataConfig);
670 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
671 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000672 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000673 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000674 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000675
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000676 // in case we need a temporary, trimmed copy of the src pixels
677 SkAutoSMalloc<128 * 128> tempStorage;
678
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000679 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000680 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000681 if (useTexStorage) {
682 if (kDesktop_GrGLBinding == this->glBinding()) {
683 // 565 is not a sized internal format on desktop GL. So on desktop
684 // with 565 we always use an unsized internal format to let the
685 // system pick the best sized format to convert the 565 data to.
686 // Since glTexStorage only allows sized internal formats we will
687 // instead fallback to glTexImage2D.
688 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
689 } else {
690 // ES doesn't allow paletted textures to be used with tex storage
691 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
692 }
693 }
694
695 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000696 GrGLenum externalFormat;
697 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000698 // glTexStorage requires sized internal formats on both desktop and ES. ES
699 // glTexImage requires an unsized format.
700 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
701 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000702 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000703 }
704
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000705 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000706 // paletted textures cannot be updated
707 return false;
708 }
709
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000710 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000711 * check whether to allocate a temporary buffer for flipping y or
712 * because our srcData has extra bytes past each row. If so, we need
713 * to trim those off here, since GL ES may not let us specify
714 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000715 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000716 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000717 bool swFlipY = false;
718 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000719 if (NULL != data) {
720 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000721 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000722 glFlipY = true;
723 } else {
724 swFlipY = true;
725 }
726 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000727 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000728 // can't use this for flipping, only non-neg values allowed. :(
729 if (rowBytes != trimRowBytes) {
730 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
731 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
732 restoreGLRowLength = true;
733 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000734 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000735 if (trimRowBytes != rowBytes || swFlipY) {
736 // copy data into our new storage, skipping the trailing bytes
737 size_t trimSize = height * trimRowBytes;
738 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000739 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000740 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000741 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000742 char* dst = (char*)tempStorage.reset(trimSize);
743 for (int y = 0; y < height; y++) {
744 memcpy(dst, src, trimRowBytes);
745 if (swFlipY) {
746 src -= rowBytes;
747 } else {
748 src += rowBytes;
749 }
750 dst += trimRowBytes;
751 }
752 // now point data to our copied version
753 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000754 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000755 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000756 if (glFlipY) {
757 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
758 }
759 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000760 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000761 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000762 if (isNewTexture &&
763 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000764 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000765 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000766 if (useTexStorage) {
767 // We never resize or change formats of textures. We don't use
768 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000769 GL_ALLOC_CALL(this->glInterface(),
770 TexStorage2D(GR_GL_TEXTURE_2D,
771 1, // levels
772 internalFormat,
773 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000774 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000775 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
776 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
777 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000778 GL_ALLOC_CALL(this->glInterface(),
779 CompressedTexImage2D(GR_GL_TEXTURE_2D,
780 0, // level
781 internalFormat,
782 desc.fWidth, desc.fHeight,
783 0, // border
784 imageSize,
785 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000786 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000787 GL_ALLOC_CALL(this->glInterface(),
788 TexImage2D(GR_GL_TEXTURE_2D,
789 0, // level
790 internalFormat,
791 desc.fWidth, desc.fHeight,
792 0, // border
793 externalFormat, externalType,
794 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000795 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000796 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000797 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000798 if (error != GR_GL_NO_ERROR) {
799 succeeded = false;
800 } else {
801 // if we have data and we used TexStorage to create the texture, we
802 // now upload with TexSubImage.
803 if (NULL != data && useTexStorage) {
804 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
805 0, // level
806 left, top,
807 width, height,
808 externalFormat, externalType,
809 data));
810 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000811 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000812 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000813 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000814 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000816 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
817 0, // level
818 left, top,
819 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000820 externalFormat, externalType, data));
821 }
822
823 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000824 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000825 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000826 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000827 if (glFlipY) {
828 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
829 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000830 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000831}
832
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000833namespace {
834bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
835 int sampleCount,
836 GrGLenum format,
837 int width, int height) {
838 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
839 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
840 bool created = false;
841 if (GrGLCaps::kNVDesktop_CoverageAAType ==
842 ctxInfo.caps().coverageAAType()) {
843 const GrGLCaps::MSAACoverageMode& mode =
844 ctxInfo.caps().getMSAACoverageMode(sampleCount);
845 GL_ALLOC_CALL(ctxInfo.interface(),
846 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
847 mode.fCoverageSampleCnt,
848 mode.fColorSampleCnt,
849 format,
850 width, height));
851 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
852 }
853 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000854 // glRBMS will fail if requested samples is > max samples.
855 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000856 GL_ALLOC_CALL(ctxInfo.interface(),
857 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
858 sampleCount,
859 format,
860 width, height));
861 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
862 }
863 return created;
864}
865}
866
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000867bool GrGpuGL::createRenderTargetObjects(int width, int height,
868 GrGLuint texID,
869 GrGLRenderTarget::Desc* desc) {
870 desc->fMSColorRenderbufferID = 0;
871 desc->fRTFBOID = 0;
872 desc->fTexFBOID = 0;
873 desc->fOwnIDs = true;
874
875 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876
bsalomon@google.comab15d612011-08-09 12:57:56 +0000877 GrGLenum msColorFormat = 0; // suppress warning
878
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000879 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880 if (!desc->fTexFBOID) {
881 goto FAILED;
882 }
883
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000884
885 // If we are using multisampling we will create two FBOS. We render
886 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000887 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000888 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000889 goto FAILED;
890 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
892 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893 if (!desc->fRTFBOID ||
894 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000895 !this->configToGLFormats(desc->fConfig,
896 // GLES requires sized internal formats
897 kES2_GrGLBinding == this->glBinding(),
898 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 goto FAILED;
900 }
901 } else {
902 desc->fRTFBOID = desc->fTexFBOID;
903 }
904
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000905 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000906 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907 if (desc->fRTFBOID != desc->fTexFBOID) {
908 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000911 if (!renderbuffer_storage_msaa(fGLContextInfo,
912 desc->fSampleCnt,
913 msColorFormat,
914 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915 goto FAILED;
916 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000917 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
918 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919 GR_GL_COLOR_ATTACHMENT0,
920 GR_GL_RENDERBUFFER,
921 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000922 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000923 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
924 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
925 goto FAILED;
926 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000927 fGLContextInfo.caps().markConfigAsValidColorAttachment(
928 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 }
930 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000931 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000933 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
934 GR_GL_COLOR_ATTACHMENT0,
935 GR_GL_TEXTURE_2D,
936 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000937 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000938 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
939 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
940 goto FAILED;
941 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000942 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943 }
944
945 return true;
946
947FAILED:
948 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000949 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950 }
951 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000952 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000953 }
954 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000955 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000956 }
957 return false;
958}
959
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000960// good to set a break-point here to know when createTexture fails
961static GrTexture* return_null_texture() {
962// GrAssert(!"null texture");
963 return NULL;
964}
965
966#if GR_DEBUG
967static size_t as_size_t(int x) {
968 return x;
969}
970#endif
971
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000972GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000973 const void* srcData,
974 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000975
976#if GR_COLLECT_STATS
977 ++fStats.fTextureCreateCnt;
978#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000979
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000980 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000981 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000982
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000983 // Attempt to catch un- or wrongly initialized sample counts;
984 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
985
bsalomon@google.com99621082011-11-15 16:47:16 +0000986 glTexDesc.fWidth = desc.fWidth;
987 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000988 glTexDesc.fConfig = desc.fConfig;
989 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000990
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000991 glRTDesc.fMSColorRenderbufferID = 0;
992 glRTDesc.fRTFBOID = 0;
993 glRTDesc.fTexFBOID = 0;
994 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000995 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000996
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000997 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000998
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000999 const Caps& caps = this->getCaps();
1000
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001001 // We keep GrRenderTargets in GL's normal orientation so that they
1002 // can be drawn to by the outside world without the client having
1003 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001004 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001005 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001006
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001007 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001008 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001009 desc.fSampleCnt) {
1010 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001011 }
1012
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001014 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1015 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001016 return return_null_texture();
1017 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001018 }
1019
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001020 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001021 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001022 // provides a hint about how this texture will be used
1023 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1024 GR_GL_TEXTURE_USAGE,
1025 GR_GL_FRAMEBUFFER_ATTACHMENT));
1026 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001027 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001028 return return_null_texture();
1029 }
1030
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001031 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001032 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001033
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001034 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1035 // drivers have a bug where an FBO won't be complete if it includes a
1036 // texture that is not mipmap complete (considering the filter in use).
1037 GrGLTexture::TexParams initialTexParams;
1038 // we only set a subset here so invalidate first
1039 initialTexParams.invalidate();
1040 initialTexParams.fFilter = GR_GL_NEAREST;
1041 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1042 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001043 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1044 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001045 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1047 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001048 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001049 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1050 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001051 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001052 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1053 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001054 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001055 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1056 glTexDesc.fWidth, glTexDesc.fHeight,
1057 desc.fConfig, srcData, rowBytes)) {
1058 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1059 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001060 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001061
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 if (renderTarget) {
1064#if GR_COLLECT_STATS
1065 ++fStats.fRenderTargetCreateCnt;
1066#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001067 // unbind the texture from the texture unit before binding it to the frame buffer
1068 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1069
bsalomon@google.com99621082011-11-15 16:47:16 +00001070 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1071 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001072 glTexDesc.fTextureID,
1073 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001074 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 return return_null_texture();
1076 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001077 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001079 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001081 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001082#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001083 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1084 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001085#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 return tex;
1087}
1088
1089namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001090
1091const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1092
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001093void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1094 GrGLuint rb,
1095 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001096 // we shouldn't ever know one size and not the other
1097 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1098 (kUnknownBitCount == format->fTotalBits));
1099 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001100 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1102 (GrGLint*)&format->fStencilBits);
1103 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001104 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1106 (GrGLint*)&format->fTotalBits);
1107 format->fTotalBits += format->fStencilBits;
1108 } else {
1109 format->fTotalBits = format->fStencilBits;
1110 }
1111 }
1112}
1113}
1114
1115bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1116 int width, int height) {
1117
1118 // All internally created RTs are also textures. We don't create
1119 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1120 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001121 GrAssert(width >= rt->width());
1122 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123
1124 int samples = rt->numSamples();
1125 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001126 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 if (!sbID) {
1128 return false;
1129 }
1130
1131 GrGLStencilBuffer* sb = NULL;
1132
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001133 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 // we start with the last stencil format that succeeded in hopes
1137 // that we won't go through this loop more than once after the
1138 // first (painful) stencil creation.
1139 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001140 const GrGLCaps::StencilFormat& sFmt =
1141 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001142 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001143 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001145 bool created;
1146 if (samples > 0) {
1147 created = renderbuffer_storage_msaa(fGLContextInfo,
1148 samples,
1149 sFmt.fInternalFormat,
1150 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001152 GL_ALLOC_CALL(this->glInterface(),
1153 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001154 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001155 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001156 created =
1157 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001159 if (created) {
1160 // After sized formats we attempt an unsized format and take
1161 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001162 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001163 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001164 sb = new GrGLStencilBuffer(this, sbID, width, height,
1165 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1167 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001168 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 return true;
1171 }
1172 sb->abandon(); // otherwise we lose sbID
1173 sb->unref();
1174 }
1175 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001176 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001177 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178}
1179
1180bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1181 GrRenderTarget* rt) {
1182 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1183
1184 GrGLuint fbo = glrt->renderFBOID();
1185
1186 if (NULL == sb) {
1187 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001188 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 GR_GL_STENCIL_ATTACHMENT,
1190 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 GR_GL_DEPTH_ATTACHMENT,
1193 GR_GL_RENDERBUFFER, 0));
1194#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001195 GrGLenum status;
1196 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1198#endif
1199 }
1200 return true;
1201 } else {
1202 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1203 GrGLuint rb = glsb->renderbufferID();
1204
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001205 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1207 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 GR_GL_STENCIL_ATTACHMENT,
1209 GR_GL_RENDERBUFFER, rb));
1210 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001211 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 GR_GL_DEPTH_ATTACHMENT,
1213 GR_GL_RENDERBUFFER, rb));
1214 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001215 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 GR_GL_DEPTH_ATTACHMENT,
1217 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001220 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001221 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001222 glsb->format())) {
1223 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1224 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001226 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001228 if (glsb->format().fPacked) {
1229 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1230 GR_GL_DEPTH_ATTACHMENT,
1231 GR_GL_RENDERBUFFER, 0));
1232 }
1233 return false;
1234 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001235 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001236 rt->config(),
1237 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001240 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001242}
1243
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001244////////////////////////////////////////////////////////////////////////////////
1245
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001246GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001247 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001251 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001252 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001254 GL_ALLOC_CALL(this->glInterface(),
1255 BufferData(GR_GL_ARRAY_BUFFER,
1256 size,
1257 NULL, // data ptr
1258 dynamic ? GR_GL_DYNAMIC_DRAW :
1259 GR_GL_STATIC_DRAW));
1260 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 // deleting bound buffer does implicit bind to 0
1263 fHWGeometryState.fVertexBuffer = NULL;
1264 return NULL;
1265 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001266 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 size, dynamic);
1268 fHWGeometryState.fVertexBuffer = vertexBuffer;
1269 return vertexBuffer;
1270 }
1271 return NULL;
1272}
1273
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001274GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001275 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001276 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001278 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001279 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001281 GL_ALLOC_CALL(this->glInterface(),
1282 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1283 size,
1284 NULL, // data ptr
1285 dynamic ? GR_GL_DYNAMIC_DRAW :
1286 GR_GL_STATIC_DRAW));
1287 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 // deleting bound buffer does implicit bind to 0
1290 fHWGeometryState.fIndexBuffer = NULL;
1291 return NULL;
1292 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001293 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 size, dynamic);
1295 fHWGeometryState.fIndexBuffer = indexBuffer;
1296 return indexBuffer;
1297 }
1298 return NULL;
1299}
1300
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001301void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001302 const GrDrawState& drawState = this->getDrawState();
1303 const GrGLRenderTarget* rt =
1304 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1305
1306 GrAssert(NULL != rt);
1307 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001308
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001309 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001310 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1311 rect.width(), rect.height());
1312 if (scissor.contains(vp)) {
1313 disableScissor();
1314 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 }
1316
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001317 if (fHWBounds.fScissorRect != scissor) {
1318 scissor.pushToGLScissor(this->glInterface());
1319 fHWBounds.fScissorRect = scissor;
1320 }
1321 if (!fHWBounds.fScissorEnabled) {
1322 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1323 fHWBounds.fScissorEnabled = true;
1324 }
1325}
1326
1327void GrGpuGL::disableScissor() {
1328 if (fHWBounds.fScissorEnabled) {
1329 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1330 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 }
1332}
1333
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001334void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001335 const GrDrawState& drawState = this->getDrawState();
1336 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001337 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001338 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001339
bsalomon@google.com74b98712011-11-11 19:46:16 +00001340 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001341 if (NULL != rect) {
1342 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001343 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001344 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001345 if (clippedRect.intersect(rtRect)) {
1346 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001347 } else {
1348 return;
1349 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001351 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001352 if (NULL != rect)
1353 this->enableScissoring(*rect);
1354 else
1355 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001356
1357 GrGLfloat r, g, b, a;
1358 static const GrGLfloat scale255 = 1.f / 255.f;
1359 a = GrColorUnpackA(color) * scale255;
1360 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001361 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001362 scaleRGB *= a;
1363 }
1364 r = GrColorUnpackR(color) * scaleRGB;
1365 g = GrColorUnpackG(color) * scaleRGB;
1366 b = GrColorUnpackB(color) * scaleRGB;
1367
1368 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001369 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001370 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001371 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001372}
1373
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001374void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001375 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001376 return;
1377 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001378
1379 this->flushRenderTarget(&GrIRect::EmptyIRect());
1380
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001381 this->disableScissor();
1382
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001383 GL_CALL(StencilMask(0xffffffff));
1384 GL_CALL(ClearStencil(0));
1385 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001386 fHWStencilSettings.invalidate();
1387 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001388}
1389
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001390void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001391 const GrDrawState& drawState = this->getDrawState();
1392 const GrRenderTarget* rt = drawState.getRenderTarget();
1393 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001394
1395 // this should only be called internally when we know we have a
1396 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001397 GrAssert(NULL != rt->getStencilBuffer());
1398 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001399#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001400 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001401 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001402#else
1403 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001404 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001405 // turned into draws. Our contract on GrDrawTarget says that
1406 // changing the clip between stencil passes may or may not
1407 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001408 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001409#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001410 GrGLint value;
1411 if (insideClip) {
1412 value = (1 << (stencilBitCount - 1));
1413 } else {
1414 value = 0;
1415 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001416 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001417 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001418 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001419 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001420 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001421 fHWStencilSettings.invalidate();
1422 fHWStencilClipMode = kInvalid_StencilClipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001423}
1424
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001425void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001426 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001427}
1428
bsalomon@google.comc4364992011-11-07 15:54:49 +00001429bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1430 int left, int top,
1431 int width, int height,
1432 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001433 size_t rowBytes) const {
1434 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001435 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001436 return false;
1437 }
1438
1439 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001440 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001441 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001442 return true;
1443 }
1444 // If we have to do memcpys to handle rowBytes then y-flip is free
1445 // Note the rowBytes might be tight to the passed in data, but if data
1446 // gets clipped in x to the target the rowBytes will no longer be tight.
1447 if (left >= 0 && (left + width) < renderTarget->width()) {
1448 return 0 == rowBytes ||
1449 GrBytesPerPixel(config) * width == rowBytes;
1450 } else {
1451 return false;
1452 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001453}
1454
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001455bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001456 int left, int top,
1457 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001458 GrPixelConfig config,
1459 void* buffer,
1460 size_t rowBytes,
1461 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001462 GrGLenum format;
1463 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001464 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001466 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001467 size_t bpp = GrBytesPerPixel(config);
1468 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1469 &left, &top, &width, &height,
1470 const_cast<const void**>(&buffer),
1471 &rowBytes)) {
1472 return false;
1473 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001474
bsalomon@google.comc6980972011-11-02 19:57:21 +00001475 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001476 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001477 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001478 switch (tgt->getResolveType()) {
1479 case GrGLRenderTarget::kCantResolve_ResolveType:
1480 return false;
1481 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001482 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001483 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001484 break;
1485 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001486 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001487 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001488 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1489 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001490 break;
1491 default:
1492 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001493 }
1494
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001495 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001496
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001497 // the read rect is viewport-relative
1498 GrGLIRect readRect;
1499 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001500
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001501 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001502 if (0 == rowBytes) {
1503 rowBytes = tightRowBytes;
1504 }
1505 size_t readDstRowBytes = tightRowBytes;
1506 void* readDst = buffer;
1507
1508 // determine if GL can read using the passed rowBytes or if we need
1509 // a scratch buffer.
1510 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1511 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001512 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001513 GrAssert(!(rowBytes % sizeof(GrColor)));
1514 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1515 readDstRowBytes = rowBytes;
1516 } else {
1517 scratch.reset(tightRowBytes * height);
1518 readDst = scratch.get();
1519 }
1520 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001521 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001522 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1523 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001524 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1525 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001526 format, type, readDst));
1527 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001528 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001529 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1530 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001531 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001532 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1533 invertY = true;
1534 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001535
1536 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537 // API presents top-to-bottom. We must preserve the padding contents. Note
1538 // that the above readPixels did not overwrite the padding.
1539 if (readDst == buffer) {
1540 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001541 if (!invertY) {
1542 scratch.reset(tightRowBytes);
1543 void* tmpRow = scratch.get();
1544 // flip y in-place by rows
1545 const int halfY = height >> 1;
1546 char* top = reinterpret_cast<char*>(buffer);
1547 char* bottom = top + (height - 1) * rowBytes;
1548 for (int y = 0; y < halfY; y++) {
1549 memcpy(tmpRow, top, tightRowBytes);
1550 memcpy(top, bottom, tightRowBytes);
1551 memcpy(bottom, tmpRow, tightRowBytes);
1552 top += rowBytes;
1553 bottom -= rowBytes;
1554 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001555 }
1556 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001557 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001558 // copy from readDst to buffer while flipping y
1559 const int halfY = height >> 1;
1560 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001561 char* dst = reinterpret_cast<char*>(buffer);
1562 if (!invertY) {
1563 dst += (height-1) * rowBytes;
1564 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001565 for (int y = 0; y < height; y++) {
1566 memcpy(dst, src, tightRowBytes);
1567 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001568 if (invertY) {
1569 dst += rowBytes;
1570 } else {
1571 dst -= rowBytes;
1572 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001573 }
1574 }
1575 return true;
1576}
1577
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001578void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001579
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001580 GrGLRenderTarget* rt =
1581 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1582 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001583
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001584 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001585 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001586 #if GR_COLLECT_STATS
1587 ++fStats.fRenderTargetChngCnt;
1588 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001589 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001590 GrGLenum status;
1591 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001592 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001593 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001594 }
1595 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001596 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001597 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001598 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001599 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001600 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001601 fHWBounds.fViewportRect = vp;
1602 }
1603 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001604 if (NULL == bound || !bound->isEmpty()) {
1605 rt->flagAsNeedingResolve(bound);
1606 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001607}
1608
twiz@google.com0f31ca72011-03-18 17:38:11 +00001609GrGLenum gPrimitiveType2GLMode[] = {
1610 GR_GL_TRIANGLES,
1611 GR_GL_TRIANGLE_STRIP,
1612 GR_GL_TRIANGLE_FAN,
1613 GR_GL_POINTS,
1614 GR_GL_LINES,
1615 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001616};
1617
bsalomon@google.comd302f142011-03-03 13:54:13 +00001618#define SWAP_PER_DRAW 0
1619
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001620#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001621 #if GR_MAC_BUILD
1622 #include <AGL/agl.h>
1623 #elif GR_WIN32_BUILD
1624 void SwapBuf() {
1625 DWORD procID = GetCurrentProcessId();
1626 HWND hwnd = GetTopWindow(GetDesktopWindow());
1627 while(hwnd) {
1628 DWORD wndProcID = 0;
1629 GetWindowThreadProcessId(hwnd, &wndProcID);
1630 if(wndProcID == procID) {
1631 SwapBuffers(GetDC(hwnd));
1632 }
1633 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1634 }
1635 }
1636 #endif
1637#endif
1638
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001639void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1640 uint32_t startVertex,
1641 uint32_t startIndex,
1642 uint32_t vertexCount,
1643 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1645
twiz@google.com0f31ca72011-03-18 17:38:11 +00001646 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001647
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001648 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1649 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1650
1651 // our setupGeometry better have adjusted this to zero since
1652 // DrawElements always draws from the begining of the arrays for idx 0.
1653 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001654
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001655 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1656 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001657#if SWAP_PER_DRAW
1658 glFlush();
1659 #if GR_MAC_BUILD
1660 aglSwapBuffers(aglGetCurrentContext());
1661 int set_a_break_pt_here = 9;
1662 aglSwapBuffers(aglGetCurrentContext());
1663 #elif GR_WIN32_BUILD
1664 SwapBuf();
1665 int set_a_break_pt_here = 9;
1666 SwapBuf();
1667 #endif
1668#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001669}
1670
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001671void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1672 uint32_t startVertex,
1673 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001674 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1675
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001676 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1677
1678 // our setupGeometry better have adjusted this to zero.
1679 // DrawElements doesn't take an offset so we always adjus the startVertex.
1680 GrAssert(0 == startVertex);
1681
1682 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1683 // account for startVertex in the DrawElements case. So we always
1684 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001685 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001686#if SWAP_PER_DRAW
1687 glFlush();
1688 #if GR_MAC_BUILD
1689 aglSwapBuffers(aglGetCurrentContext());
1690 int set_a_break_pt_here = 9;
1691 aglSwapBuffers(aglGetCurrentContext());
1692 #elif GR_WIN32_BUILD
1693 SwapBuf();
1694 int set_a_break_pt_here = 9;
1695 SwapBuf();
1696 #endif
1697#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001698}
1699
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001700void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1701
1702 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001703
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001704 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001705 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001706 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001707 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1708 rt->renderFBOID()));
1709 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1710 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001711 #if GR_COLLECT_STATS
1712 ++fStats.fRenderTargetChngCnt;
1713 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001714 // make sure we go through flushRenderTarget() since we've modified
1715 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001716 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001717 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001718 const GrIRect dirtyRect = rt->getResolveRect();
1719 GrGLIRect r;
1720 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1721 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001722
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001723 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001724 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001725#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001726 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1727 GL_CALL(Scissor(r.fLeft, r.fBottom,
1728 r.fWidth, r.fHeight));
1729 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001730 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001731 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001732#else
1733 this->enableScissoring(dirtyRect);
1734 GL_CALL(ResolveMultisampleFramebuffer());
1735#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001737 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001738 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001739 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1740 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001741 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001742 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001743 int right = r.fLeft + r.fWidth;
1744 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001745 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1746 r.fLeft, r.fBottom, right, top,
1747 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001748 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001749 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001750 }
1751}
1752
twiz@google.com0f31ca72011-03-18 17:38:11 +00001753static const GrGLenum grToGLStencilFunc[] = {
1754 GR_GL_ALWAYS, // kAlways_StencilFunc
1755 GR_GL_NEVER, // kNever_StencilFunc
1756 GR_GL_GREATER, // kGreater_StencilFunc
1757 GR_GL_GEQUAL, // kGEqual_StencilFunc
1758 GR_GL_LESS, // kLess_StencilFunc
1759 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1760 GR_GL_EQUAL, // kEqual_StencilFunc,
1761 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001762};
1763GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1764GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1765GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1766GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1767GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1768GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1769GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1770GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1771GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1772
twiz@google.com0f31ca72011-03-18 17:38:11 +00001773static const GrGLenum grToGLStencilOp[] = {
1774 GR_GL_KEEP, // kKeep_StencilOp
1775 GR_GL_REPLACE, // kReplace_StencilOp
1776 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1777 GR_GL_INCR, // kIncClamp_StencilOp
1778 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1779 GR_GL_DECR, // kDecClamp_StencilOp
1780 GR_GL_ZERO, // kZero_StencilOp
1781 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001782};
1783GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1784GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1785GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1786GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1787GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1788GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1789GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1790GR_STATIC_ASSERT(6 == kZero_StencilOp);
1791GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1792
reed@google.comac10a2d2010-12-22 21:39:39 +00001793void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001794 const GrDrawState& drawState = this->getDrawState();
1795
1796 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001797
1798 // use stencil for clipping if clipping is enabled and the clip
1799 // has been written into the stencil.
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001800 StencilClipMode clipMode;
1801 if (fClipMaskManager.isClipInStencil() &&
1802 drawState.isClipState()) {
1803 clipMode = kUseClip_StencilClipMode;
1804 // We can't be modifying the clip and respecting it at the same time.
1805 GrAssert(!drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
1806 } else if (drawState.isStateFlagEnabled(kModifyStencilClip_StateBit)) {
1807 clipMode = kModifyClip_StencilClipMode;
1808 } else {
1809 clipMode = kIgnoreClip_StencilClipMode;
1810 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001811
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001812 bool stencilChange = (fHWStencilSettings != *settings) ||
1813 (fHWStencilClipMode != clipMode);
reed@google.comac10a2d2010-12-22 21:39:39 +00001814 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001815
bsalomon@google.comd302f142011-03-03 13:54:13 +00001816 if (settings->isDisabled()) {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001817 if (kUseClip_StencilClipMode == clipMode) {
digit@google.com9b482c42012-02-16 22:03:26 +00001818 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001819 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001820 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001821
1822 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001823 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001824 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001825 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001826 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001827 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001828 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1829 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1830 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1831 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1832 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1833 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1834 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1835 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001836 }
1837 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001838 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001839 GrStencilBuffer* stencilBuffer =
1840 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001841 if (NULL != stencilBuffer) {
1842 stencilBits = stencilBuffer->bits();
1843 }
1844 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001845 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001846
1847 GrGLuint clipStencilMask = 0;
1848 GrGLuint userStencilMask = ~0;
1849 if (stencilBits > 0) {
1850 clipStencilMask = 1 << (stencilBits - 1);
1851 userStencilMask = clipStencilMask - 1;
1852 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001853
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001854 unsigned int frontRef = settings->frontFuncRef();
1855 unsigned int frontMask = settings->frontFuncMask();
1856 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001857 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001859 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001860 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1861 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001862 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001863 bool useClip = kUseClip_StencilClipMode == clipMode;
1864 frontFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1865 settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001867 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001868 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869 clipStencilMask,
1870 userStencilMask,
1871 &frontRef,
1872 &frontMask);
1873 frontWriteMask &= userStencilMask;
1874 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001875 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001876 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001877 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001878 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001879 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001880 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001881 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001882 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001883 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001884 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001885
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001886 unsigned int backRef = settings->backFuncRef();
1887 unsigned int backMask = settings->backFuncMask();
1888 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001889
1890
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001891 if (kModifyClip_StencilClipMode == clipMode) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001892 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1893 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001894 } else {
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001895 bool useClip = kUseClip_StencilClipMode == clipMode;
1896 backFunc = grToGLStencilFunc[ConvertStencilFunc(useClip,
1897 settings->backFunc())];
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001898 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001899 useClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900 clipStencilMask,
1901 userStencilMask,
1902 &backRef,
1903 &backMask);
1904 backWriteMask &= userStencilMask;
1905 }
1906
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001907 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1908 frontRef, frontMask));
1909 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1910 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1911 backRef, backMask));
1912 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1913 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001914 grToGLStencilOp[settings->frontFailOp()],
1915 grToGLStencilOp[settings->frontPassOp()],
1916 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001917
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001918 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001919 grToGLStencilOp[settings->backFailOp()],
1920 grToGLStencilOp[settings->backPassOp()],
1921 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001923 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1924 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001925 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1926 grToGLStencilOp[settings->frontPassOp()],
1927 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001928 }
1929 }
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001930 fHWStencilSettings = *settings;
1931 fHWStencilClipMode = clipMode;
reed@google.comac10a2d2010-12-22 21:39:39 +00001932 }
1933}
1934
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001935void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001936 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001938 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1939 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001940 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001941 bool smoothLines = false;
1942
bsalomon@google.com0650e812011-04-08 18:07:53 +00001943 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001944 smoothLines = this->willUseHWAALines();
1945 if (smoothLines) {
1946 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1947 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1948 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1949 // must disable msaa to use line smoothing
1950 if (rt->isMultisampled() &&
1951 kNo_TriState != fHWAAState.fMSAAEnabled) {
1952 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1953 fHWAAState.fMSAAEnabled = kNo_TriState;
1954 }
1955 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001957 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1958 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1959 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1960 }
1961 }
1962 }
1963 if (!smoothLines && rt->isMultisampled()) {
1964 if (this->getDrawState().isHWAntialiasState()) {
1965 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1966 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1967 fHWAAState.fMSAAEnabled = kYes_TriState;
1968 }
1969 } else {
1970 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1971 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1972 fHWAAState.fMSAAEnabled = kNo_TriState;
1973 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001974 }
1975 }
1976 }
1977}
1978
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001979void GrGpuGL::flushBlend(GrPrimitiveType type,
1980 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001981 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001982 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001983 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001984 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001985 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001986 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001987 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
1988 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001989 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1990 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001991 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
1992 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001993 }
1994 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001995 // any optimization to disable blending should
1996 // have already been applied and tweaked the coeffs
1997 // to (1, 0).
1998 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1999 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002000 if (blendOff) {
2001 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002002 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002003 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002004 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002005 } else {
2006 if (kYes_TriState != fHWBlendState.fEnabled) {
2007 GL_CALL(Enable(GR_GL_BLEND));
2008 fHWBlendState.fEnabled = kYes_TriState;
2009 }
2010 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2011 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002012 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2013 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002014 fHWBlendState.fSrcCoeff = srcCoeff;
2015 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002016 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002017 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002018 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2019 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002020 (!fHWBlendState.fConstColorValid ||
2021 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002022
2023 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002024 GrColorUnpackR(blendConst) / 255.f,
2025 GrColorUnpackG(blendConst) / 255.f,
2026 GrColorUnpackB(blendConst) / 255.f,
2027 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002028 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002029 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002030 fHWBlendState.fConstColor = blendConst;
2031 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002032 }
2033 }
2034 }
2035}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002036namespace {
2037
2038unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002039 switch (filter) {
2040 case GrSamplerState::kBilinear_Filter:
2041 case GrSamplerState::k4x4Downsample_Filter:
2042 return GR_GL_LINEAR;
2043 case GrSamplerState::kNearest_Filter:
2044 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002045 case GrSamplerState::kErode_Filter:
2046 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002047 return GR_GL_NEAREST;
2048 default:
2049 GrAssert(!"Unknown filter type");
2050 return GR_GL_LINEAR;
2051 }
2052}
2053
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002054// get_swizzle is only called from this .cpp so it is OK to inline it here
2055inline const GrGLenum* get_swizzle(GrPixelConfig config,
2056 const GrSamplerState& sampler,
2057 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002058 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002059 if (glCaps.textureRedSupport()) {
2060 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2061 GR_GL_RED, GR_GL_RED };
2062 return gRedSmear;
2063 } else {
2064 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2065 GR_GL_ALPHA, GR_GL_ALPHA };
2066 return gAlphaSmear;
2067 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002068 } else if (sampler.swapsRAndB()) {
2069 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2070 GR_GL_RED, GR_GL_ALPHA };
2071 return gRedBlueSwap;
2072 } else {
2073 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2074 GR_GL_BLUE, GR_GL_ALPHA };
2075 return gStraight;
2076 }
2077}
2078
2079void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2080 // should add texparameteri to interface to make 1 instead of 4 calls here
2081 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2082 GR_GL_TEXTURE_SWIZZLE_R,
2083 swizzle[0]));
2084 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2085 GR_GL_TEXTURE_SWIZZLE_G,
2086 swizzle[1]));
2087 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2088 GR_GL_TEXTURE_SWIZZLE_B,
2089 swizzle[2]));
2090 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2091 GR_GL_TEXTURE_SWIZZLE_A,
2092 swizzle[3]));
2093}
2094}
2095
bsalomon@google.comffca4002011-02-22 20:34:01 +00002096bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002097
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002098 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002099 // GrGpu::setupClipAndFlushState should have already checked this
2100 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002101 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002102
tomhudson@google.com93813632011-10-27 20:21:16 +00002103 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002104 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002105 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002106 GrGLTexture* nextTexture =
2107 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002108
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002109 // true for now, but maybe not with GrEffect.
2110 GrAssert(NULL != nextTexture);
2111 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002112 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002113 // the last bound texture, but it needs resolving. So keep this
2114 // out of the "last != next" check.
2115 GrGLRenderTarget* texRT =
2116 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2117 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002118 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002119 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002120
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002121 if (fHWBoundTextures[s] != nextTexture) {
2122 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002123 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002124 #if GR_COLLECT_STATS
2125 ++fStats.fTextureChngCnt;
2126 #endif
2127 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002128 fHWBoundTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002129 // The texture matrix has to compensate for texture width/height
2130 // and NPOT-embedded-in-POT
2131 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002132 }
2133
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002134 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002135 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002136 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002137 nextTexture->getCachedTexParams(&timestamp);
2138 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002139 GrGLTexture::TexParams newTexParams;
2140
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002141 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002142
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002143 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002144 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2145 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002146 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002147 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002148 sizeof(newTexParams.fSwizzleRGBA));
2149 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002150 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002151 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002152 GR_GL_TEXTURE_MAG_FILTER,
2153 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002154 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002155 GR_GL_TEXTURE_MIN_FILTER,
2156 newTexParams.fFilter));
2157 }
2158 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002159 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002160 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002161 GR_GL_TEXTURE_WRAP_S,
2162 newTexParams.fWrapS));
2163 }
2164 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002165 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002166 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002167 GR_GL_TEXTURE_WRAP_T,
2168 newTexParams.fWrapT));
2169 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002170 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002171 (setAll ||
2172 memcmp(newTexParams.fSwizzleRGBA,
2173 oldTexParams.fSwizzleRGBA,
2174 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002175 this->setTextureUnit(s);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002176 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2177 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002178 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002179 nextTexture->setCachedTexParams(newTexParams,
2180 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002181 }
2182 }
2183
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002184 GrIRect* rect = NULL;
2185 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002186 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002187 fClip.hasConservativeBounds()) {
2188 fClip.getConservativeBounds().roundOut(&clipBounds);
2189 rect = &clipBounds;
2190 }
2191 this->flushRenderTarget(rect);
2192 this->flushAAState(type);
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002193
2194 if (drawState->isDitherState()) {
2195 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002196 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002197 fHWDitherEnabled = kYes_TriState;
2198 }
2199 } else {
2200 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002201 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002202 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002203 }
2204 }
2205
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002206 if (drawState->isColorWriteDisabled()) {
2207 if (kNo_TriState != fHWWriteToColor) {
2208 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2209 GR_GL_FALSE, GR_GL_FALSE));
2210 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002211 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002212 } else {
2213 if (kYes_TriState != fHWWriteToColor) {
2214 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2215 fHWWriteToColor = kYes_TriState;
2216 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 }
2218
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002219 if (fHWDrawFace != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002220 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002221 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002222 GL_CALL(Enable(GR_GL_CULL_FACE));
2223 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002224 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002225 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002226 GL_CALL(Enable(GR_GL_CULL_FACE));
2227 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002228 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002229 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002230 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002231 break;
2232 default:
2233 GrCrash("Unknown draw face.");
2234 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002235 fHWDrawFace = drawState->getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002236 }
2237
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002238#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002239 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002240 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002241 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002242 NULL == drawState->getRenderTarget() ||
2243 NULL == drawState->getTexture(s) ||
2244 drawState->getTexture(s)->asRenderTarget() !=
2245 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002246 }
2247#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002248
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002249 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002250
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002251 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002252}
2253
2254void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002255 if (fHWGeometryState.fVertexBuffer != buffer) {
2256 fHWGeometryState.fArrayPtrsDirty = true;
2257 fHWGeometryState.fVertexBuffer = buffer;
2258 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002259}
2260
2261void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002262 if (fHWGeometryState.fVertexBuffer == buffer) {
2263 // deleting bound buffer does implied bind to 0
2264 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002265 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002266 }
2267}
2268
2269void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002270 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002271}
2272
2273void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002274 if (fHWGeometryState.fIndexBuffer == buffer) {
2275 // deleting bound buffer does implied bind to 0
2276 fHWGeometryState.fIndexBuffer = NULL;
2277 }
2278}
2279
reed@google.comac10a2d2010-12-22 21:39:39 +00002280void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2281 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002282 GrDrawState* drawState = this->drawState();
2283 if (drawState->getRenderTarget() == renderTarget) {
2284 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002285 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002286 if (fHWBoundRenderTarget == renderTarget) {
2287 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002289}
2290
2291void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002292 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002293 GrDrawState* drawState = this->drawState();
2294 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002295 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002296 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002297 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002298 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002299 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002300 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002301 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002302}
2303
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002304bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2305 bool getSizedInternalFormat,
2306 GrGLenum* internalFormat,
2307 GrGLenum* externalFormat,
2308 GrGLenum* externalType) {
2309 GrGLenum dontCare;
2310 if (NULL == internalFormat) {
2311 internalFormat = &dontCare;
2312 }
2313 if (NULL == externalFormat) {
2314 externalFormat = &dontCare;
2315 }
2316 if (NULL == externalType) {
2317 externalType = &dontCare;
2318 }
2319
reed@google.comac10a2d2010-12-22 21:39:39 +00002320 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002321 case kRGBA_8888_PM_GrPixelConfig:
2322 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002323 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002324 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002325 if (getSizedInternalFormat) {
2326 *internalFormat = GR_GL_RGBA8;
2327 } else {
2328 *internalFormat = GR_GL_RGBA;
2329 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002330 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002331 break;
2332 case kBGRA_8888_PM_GrPixelConfig:
2333 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002334 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002335 return false;
2336 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002337 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002338 if (getSizedInternalFormat) {
2339 *internalFormat = GR_GL_BGRA8;
2340 } else {
2341 *internalFormat = GR_GL_BGRA;
2342 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002343 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002344 if (getSizedInternalFormat) {
2345 *internalFormat = GR_GL_RGBA8;
2346 } else {
2347 *internalFormat = GR_GL_RGBA;
2348 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002349 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002350 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002351 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002352 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002353 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002354 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002355 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002356 if (getSizedInternalFormat) {
2357 if (this->glBinding() == kDesktop_GrGLBinding) {
2358 return false;
2359 } else {
2360 *internalFormat = GR_GL_RGB565;
2361 }
2362 } else {
2363 *internalFormat = GR_GL_RGB;
2364 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002365 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002366 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002367 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002368 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002369 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002370 if (getSizedInternalFormat) {
2371 *internalFormat = GR_GL_RGBA4;
2372 } else {
2373 *internalFormat = GR_GL_RGBA;
2374 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002375 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002376 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002377 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002378 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002379 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002380 // glCompressedTexImage doesn't take external params
2381 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002382 // no sized/unsized internal format distinction here
2383 *internalFormat = GR_GL_PALETTE8_RGBA8;
2384 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002385 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002386 } else {
2387 return false;
2388 }
2389 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002390 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002391 if (this->glCaps().textureRedSupport()) {
2392 *internalFormat = GR_GL_RED;
2393 *externalFormat = GR_GL_RED;
2394 if (getSizedInternalFormat) {
2395 *internalFormat = GR_GL_R8;
2396 } else {
2397 *internalFormat = GR_GL_RED;
2398 }
2399 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002400 } else {
2401 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002402 *externalFormat = GR_GL_ALPHA;
2403 if (getSizedInternalFormat) {
2404 *internalFormat = GR_GL_ALPHA8;
2405 } else {
2406 *internalFormat = GR_GL_ALPHA;
2407 }
2408 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002409 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002410 break;
2411 default:
2412 return false;
2413 }
2414 return true;
2415}
2416
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002417void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002418 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002419 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002420 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002421 fActiveTextureUnitIdx = unit;
2422 }
2423}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002424
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002425void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002426 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002427 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002428 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2429 }
2430}
2431
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002432void GrGpuGL::resetDirtyFlags() {
2433 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2434}
2435
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002436void GrGpuGL::setBuffers(bool indexed,
2437 int* extraVertexOffset,
2438 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002439
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002440 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002441
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002442 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2443
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002444 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002445 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002446 case kBuffer_GeometrySrcType:
2447 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002448 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002449 break;
2450 case kArray_GeometrySrcType:
2451 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002452 this->finalizeReservedVertices();
2453 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2454 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002455 break;
2456 default:
2457 vbuf = NULL; // suppress warning
2458 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002459 }
2460
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002461 GrAssert(NULL != vbuf);
2462 GrAssert(!vbuf->isLocked());
2463 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002464 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002465 fHWGeometryState.fArrayPtrsDirty = true;
2466 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002467 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002468
2469 if (indexed) {
2470 GrAssert(NULL != extraIndexOffset);
2471
2472 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002473 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002474 case kBuffer_GeometrySrcType:
2475 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002476 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002477 break;
2478 case kArray_GeometrySrcType:
2479 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002480 this->finalizeReservedIndices();
2481 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2482 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002483 break;
2484 default:
2485 ibuf = NULL; // suppress warning
2486 GrCrash("Unknown geometry src type!");
2487 }
2488
2489 GrAssert(NULL != ibuf);
2490 GrAssert(!ibuf->isLocked());
2491 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002492 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002493 fHWGeometryState.fIndexBuffer = ibuf;
2494 }
2495 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002496}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002497