blob: 02ff78e5ec25e6a68b9ccb66fe22e1dce919d4bc [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.com8f9cbd62011-12-09 15:55:34 +0000486 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000487
reed@google.comac10a2d2010-12-22 21:39:39 +0000488 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000489 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000490
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000491 // invalid
492 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000494 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000495
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000496 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000497
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000498 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000499
tomhudson@google.com93813632011-10-27 20:21:16 +0000500 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000501 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000502 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000503
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000504 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000505 // set to true to force disableScissor to make a GL call.
506 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000507 this->disableScissor();
508
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000509 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000510
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000511 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 fHWStencilClip = false;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000513
514 // TODO: I believe this should actually go in GrGpu::onResetContext
515 // rather than here
516 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000517
518 fHWGeometryState.fIndexBuffer = NULL;
519 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000520
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000521 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000522
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000523 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000524
525 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000526 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000527 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
528 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000529 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000530 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
531 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000532 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000533 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
534 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000535 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000536 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
537 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000538}
539
bsalomon@google.come269f212011-11-07 13:29:52 +0000540GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000541 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000542 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000543 return NULL;
544 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000545
bsalomon@google.com99621082011-11-15 16:47:16 +0000546 glTexDesc.fWidth = desc.fWidth;
547 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000548 glTexDesc.fConfig = desc.fConfig;
549 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
550 glTexDesc.fOwnsID = false;
551 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
552
553 GrGLTexture* texture = NULL;
554 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
555 GrGLRenderTarget::Desc glRTDesc;
556 glRTDesc.fRTFBOID = 0;
557 glRTDesc.fTexFBOID = 0;
558 glRTDesc.fMSColorRenderbufferID = 0;
559 glRTDesc.fOwnIDs = true;
560 glRTDesc.fConfig = desc.fConfig;
561 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000562 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
563 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000564 glTexDesc.fTextureID,
565 &glRTDesc)) {
566 return NULL;
567 }
568 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
569 } else {
570 texture = new GrGLTexture(this, glTexDesc);
571 }
572 if (NULL == texture) {
573 return NULL;
574 }
575
576 this->setSpareTextureUnit();
577 return texture;
578}
579
580GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
581 GrGLRenderTarget::Desc glDesc;
582 glDesc.fConfig = desc.fConfig;
583 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
584 glDesc.fMSColorRenderbufferID = 0;
585 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
586 glDesc.fSampleCnt = desc.fSampleCnt;
587 glDesc.fOwnIDs = false;
588 GrGLIRect viewport;
589 viewport.fLeft = 0;
590 viewport.fBottom = 0;
591 viewport.fWidth = desc.fWidth;
592 viewport.fHeight = desc.fHeight;
593
594 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
595 if (desc.fStencilBits) {
596 GrGLStencilBuffer::Format format;
597 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
598 format.fPacked = false;
599 format.fStencilBits = desc.fStencilBits;
600 format.fTotalBits = desc.fStencilBits;
601 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
602 0,
603 desc.fWidth,
604 desc.fHeight,
605 desc.fSampleCnt,
606 format);
607 tgt->setStencilBuffer(sb);
608 sb->unref();
609 }
610 return tgt;
611}
612
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000613////////////////////////////////////////////////////////////////////////////////
614
bsalomon@google.com6f379512011-11-16 20:36:03 +0000615void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
616 int left, int top, int width, int height,
617 GrPixelConfig config, const void* buffer,
618 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000619 if (NULL == buffer) {
620 return;
621 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000622 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
623
bsalomon@google.com6f379512011-11-16 20:36:03 +0000624 this->setSpareTextureUnit();
625 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
626 GrGLTexture::Desc desc;
627 desc.fConfig = glTex->config();
628 desc.fWidth = glTex->width();
629 desc.fHeight = glTex->height();
630 desc.fOrientation = glTex->orientation();
631 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000632
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000633 this->uploadTexData(desc, false,
634 left, top, width, height,
635 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000636}
637
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000638namespace {
639bool adjust_pixel_ops_params(int surfaceWidth,
640 int surfaceHeight,
641 size_t bpp,
642 int* left, int* top, int* width, int* height,
643 const void** data,
644 size_t* rowBytes) {
645 if (!*rowBytes) {
646 *rowBytes = *width * bpp;
647 }
648
649 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
650 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
651
652 if (!subRect.intersect(bounds)) {
653 return false;
654 }
655 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
656 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
657
658 *left = subRect.fLeft;
659 *top = subRect.fTop;
660 *width = subRect.width();
661 *height = subRect.height();
662 return true;
663}
664}
665
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000666bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
667 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000668 int left, int top, int width, int height,
669 GrPixelConfig dataConfig,
670 const void* data,
671 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000672 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000673
674 size_t bpp = GrBytesPerPixel(dataConfig);
675 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
676 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000677 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000678 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000679 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000680
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000681 // in case we need a temporary, trimmed copy of the src pixels
682 SkAutoSMalloc<128 * 128> tempStorage;
683
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000684 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000685 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000686 if (useTexStorage) {
687 if (kDesktop_GrGLBinding == this->glBinding()) {
688 // 565 is not a sized internal format on desktop GL. So on desktop
689 // with 565 we always use an unsized internal format to let the
690 // system pick the best sized format to convert the 565 data to.
691 // Since glTexStorage only allows sized internal formats we will
692 // instead fallback to glTexImage2D.
693 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
694 } else {
695 // ES doesn't allow paletted textures to be used with tex storage
696 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
697 }
698 }
699
700 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000701 GrGLenum externalFormat;
702 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000703 // glTexStorage requires sized internal formats on both desktop and ES. ES
704 // glTexImage requires an unsized format.
705 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
706 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000707 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000708 }
709
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000710 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000711 // paletted textures cannot be updated
712 return false;
713 }
714
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000715 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000716 * check whether to allocate a temporary buffer for flipping y or
717 * because our srcData has extra bytes past each row. If so, we need
718 * to trim those off here, since GL ES may not let us specify
719 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000720 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000721 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000722 bool swFlipY = false;
723 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000724 if (NULL != data) {
725 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000726 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000727 glFlipY = true;
728 } else {
729 swFlipY = true;
730 }
731 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000732 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000733 // can't use this for flipping, only non-neg values allowed. :(
734 if (rowBytes != trimRowBytes) {
735 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
736 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
737 restoreGLRowLength = true;
738 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000739 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000740 if (trimRowBytes != rowBytes || swFlipY) {
741 // copy data into our new storage, skipping the trailing bytes
742 size_t trimSize = height * trimRowBytes;
743 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000744 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000745 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000746 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000747 char* dst = (char*)tempStorage.reset(trimSize);
748 for (int y = 0; y < height; y++) {
749 memcpy(dst, src, trimRowBytes);
750 if (swFlipY) {
751 src -= rowBytes;
752 } else {
753 src += rowBytes;
754 }
755 dst += trimRowBytes;
756 }
757 // now point data to our copied version
758 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000759 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000760 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000761 if (glFlipY) {
762 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
763 }
764 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000765 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000766 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000767 if (isNewTexture &&
768 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000769 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000770 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000771 if (useTexStorage) {
772 // We never resize or change formats of textures. We don't use
773 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000774 GL_ALLOC_CALL(this->glInterface(),
775 TexStorage2D(GR_GL_TEXTURE_2D,
776 1, // levels
777 internalFormat,
778 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000779 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000780 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
781 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
782 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000783 GL_ALLOC_CALL(this->glInterface(),
784 CompressedTexImage2D(GR_GL_TEXTURE_2D,
785 0, // level
786 internalFormat,
787 desc.fWidth, desc.fHeight,
788 0, // border
789 imageSize,
790 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000791 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000792 GL_ALLOC_CALL(this->glInterface(),
793 TexImage2D(GR_GL_TEXTURE_2D,
794 0, // level
795 internalFormat,
796 desc.fWidth, desc.fHeight,
797 0, // border
798 externalFormat, externalType,
799 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000800 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000801 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000802 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000803 if (error != GR_GL_NO_ERROR) {
804 succeeded = false;
805 } else {
806 // if we have data and we used TexStorage to create the texture, we
807 // now upload with TexSubImage.
808 if (NULL != data && useTexStorage) {
809 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
810 0, // level
811 left, top,
812 width, height,
813 externalFormat, externalType,
814 data));
815 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000816 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000817 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000818 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000819 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000820 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000821 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
822 0, // level
823 left, top,
824 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000825 externalFormat, externalType, data));
826 }
827
828 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000829 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000830 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000831 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000832 if (glFlipY) {
833 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
834 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000835 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000836}
837
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000838namespace {
839bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
840 int sampleCount,
841 GrGLenum format,
842 int width, int height) {
843 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
844 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
845 bool created = false;
846 if (GrGLCaps::kNVDesktop_CoverageAAType ==
847 ctxInfo.caps().coverageAAType()) {
848 const GrGLCaps::MSAACoverageMode& mode =
849 ctxInfo.caps().getMSAACoverageMode(sampleCount);
850 GL_ALLOC_CALL(ctxInfo.interface(),
851 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
852 mode.fCoverageSampleCnt,
853 mode.fColorSampleCnt,
854 format,
855 width, height));
856 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
857 }
858 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000859 // glRBMS will fail if requested samples is > max samples.
860 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000861 GL_ALLOC_CALL(ctxInfo.interface(),
862 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
863 sampleCount,
864 format,
865 width, height));
866 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
867 }
868 return created;
869}
870}
871
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000872bool GrGpuGL::createRenderTargetObjects(int width, int height,
873 GrGLuint texID,
874 GrGLRenderTarget::Desc* desc) {
875 desc->fMSColorRenderbufferID = 0;
876 desc->fRTFBOID = 0;
877 desc->fTexFBOID = 0;
878 desc->fOwnIDs = true;
879
880 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000881
bsalomon@google.comab15d612011-08-09 12:57:56 +0000882 GrGLenum msColorFormat = 0; // suppress warning
883
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000884 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000885 if (!desc->fTexFBOID) {
886 goto FAILED;
887 }
888
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000889
890 // If we are using multisampling we will create two FBOS. We render
891 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000892 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000893 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000894 goto FAILED;
895 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000896 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
897 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000898 if (!desc->fRTFBOID ||
899 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000900 !this->configToGLFormats(desc->fConfig,
901 // GLES requires sized internal formats
902 kES2_GrGLBinding == this->glBinding(),
903 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904 goto FAILED;
905 }
906 } else {
907 desc->fRTFBOID = desc->fTexFBOID;
908 }
909
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000910 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000911 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 if (desc->fRTFBOID != desc->fTexFBOID) {
913 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000914 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000916 if (!renderbuffer_storage_msaa(fGLContextInfo,
917 desc->fSampleCnt,
918 msColorFormat,
919 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000920 goto FAILED;
921 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
923 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000924 GR_GL_COLOR_ATTACHMENT0,
925 GR_GL_RENDERBUFFER,
926 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000927 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000928 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
929 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
930 goto FAILED;
931 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000932 fGLContextInfo.caps().markConfigAsValidColorAttachment(
933 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000934 }
935 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000937
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000938 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
939 GR_GL_COLOR_ATTACHMENT0,
940 GR_GL_TEXTURE_2D,
941 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000942 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000943 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
944 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
945 goto FAILED;
946 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000947 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948 }
949
950 return true;
951
952FAILED:
953 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000954 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000955 }
956 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000957 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000958 }
959 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000960 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000961 }
962 return false;
963}
964
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000965// good to set a break-point here to know when createTexture fails
966static GrTexture* return_null_texture() {
967// GrAssert(!"null texture");
968 return NULL;
969}
970
971#if GR_DEBUG
972static size_t as_size_t(int x) {
973 return x;
974}
975#endif
976
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000977GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000978 const void* srcData,
979 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000980
981#if GR_COLLECT_STATS
982 ++fStats.fTextureCreateCnt;
983#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000984
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000985 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000986 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000987
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000988 // Attempt to catch un- or wrongly initialized sample counts;
989 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
990
bsalomon@google.com99621082011-11-15 16:47:16 +0000991 glTexDesc.fWidth = desc.fWidth;
992 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000993 glTexDesc.fConfig = desc.fConfig;
994 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000995
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000996 glRTDesc.fMSColorRenderbufferID = 0;
997 glRTDesc.fRTFBOID = 0;
998 glRTDesc.fTexFBOID = 0;
999 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001000 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001001
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001002 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001004 const Caps& caps = this->getCaps();
1005
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001006 // We keep GrRenderTargets in GL's normal orientation so that they
1007 // can be drawn to by the outside world without the client having
1008 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001009 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001010 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001011
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001012 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001013 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001014 desc.fSampleCnt) {
1015 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001016 }
1017
reed@google.comac10a2d2010-12-22 21:39:39 +00001018 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001019 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1020 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001021 return return_null_texture();
1022 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001023 }
1024
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001025 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001026 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001027 // provides a hint about how this texture will be used
1028 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1029 GR_GL_TEXTURE_USAGE,
1030 GR_GL_FRAMEBUFFER_ATTACHMENT));
1031 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001032 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001033 return return_null_texture();
1034 }
1035
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001036 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001037 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001038
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001039 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1040 // drivers have a bug where an FBO won't be complete if it includes a
1041 // texture that is not mipmap complete (considering the filter in use).
1042 GrGLTexture::TexParams initialTexParams;
1043 // we only set a subset here so invalidate first
1044 initialTexParams.invalidate();
1045 initialTexParams.fFilter = GR_GL_NEAREST;
1046 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1047 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1049 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001050 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001051 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1052 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001053 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001054 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1055 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001056 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001057 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1058 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001059 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001060 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1061 glTexDesc.fWidth, glTexDesc.fHeight,
1062 desc.fConfig, srcData, rowBytes)) {
1063 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1064 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001065 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001066
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001067 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001068 if (renderTarget) {
1069#if GR_COLLECT_STATS
1070 ++fStats.fRenderTargetCreateCnt;
1071#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001072 // unbind the texture from the texture unit before binding it to the frame buffer
1073 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1074
bsalomon@google.com99621082011-11-15 16:47:16 +00001075 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1076 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 glTexDesc.fTextureID,
1078 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 return return_null_texture();
1081 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001082 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001083 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001084 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001086 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001087#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001088 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1089 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001090#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 return tex;
1092}
1093
1094namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001095
1096const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1097
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001098void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1099 GrGLuint rb,
1100 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 // we shouldn't ever know one size and not the other
1102 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1103 (kUnknownBitCount == format->fTotalBits));
1104 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001105 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1107 (GrGLint*)&format->fStencilBits);
1108 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001109 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1111 (GrGLint*)&format->fTotalBits);
1112 format->fTotalBits += format->fStencilBits;
1113 } else {
1114 format->fTotalBits = format->fStencilBits;
1115 }
1116 }
1117}
1118}
1119
1120bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1121 int width, int height) {
1122
1123 // All internally created RTs are also textures. We don't create
1124 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1125 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001126 GrAssert(width >= rt->width());
1127 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128
1129 int samples = rt->numSamples();
1130 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 if (!sbID) {
1133 return false;
1134 }
1135
1136 GrGLStencilBuffer* sb = NULL;
1137
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001138 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001140 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141 // we start with the last stencil format that succeeded in hopes
1142 // that we won't go through this loop more than once after the
1143 // first (painful) stencil creation.
1144 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001145 const GrGLCaps::StencilFormat& sFmt =
1146 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001147 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001148 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001150 bool created;
1151 if (samples > 0) {
1152 created = renderbuffer_storage_msaa(fGLContextInfo,
1153 samples,
1154 sFmt.fInternalFormat,
1155 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001157 GL_ALLOC_CALL(this->glInterface(),
1158 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001159 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001160 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001161 created =
1162 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001164 if (created) {
1165 // After sized formats we attempt an unsized format and take
1166 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001167 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001168 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001169 sb = new GrGLStencilBuffer(this, sbID, width, height,
1170 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1172 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001173 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 return true;
1176 }
1177 sb->abandon(); // otherwise we lose sbID
1178 sb->unref();
1179 }
1180 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001181 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001182 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183}
1184
1185bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1186 GrRenderTarget* rt) {
1187 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1188
1189 GrGLuint fbo = glrt->renderFBOID();
1190
1191 if (NULL == sb) {
1192 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001193 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 GR_GL_STENCIL_ATTACHMENT,
1195 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001196 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 GR_GL_DEPTH_ATTACHMENT,
1198 GR_GL_RENDERBUFFER, 0));
1199#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001200 GrGLenum status;
1201 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1203#endif
1204 }
1205 return true;
1206 } else {
1207 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1208 GrGLuint rb = glsb->renderbufferID();
1209
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001210 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001211 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1212 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213 GR_GL_STENCIL_ATTACHMENT,
1214 GR_GL_RENDERBUFFER, rb));
1215 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 GR_GL_DEPTH_ATTACHMENT,
1218 GR_GL_RENDERBUFFER, rb));
1219 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 GR_GL_DEPTH_ATTACHMENT,
1222 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001223 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001225 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001226 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001227 glsb->format())) {
1228 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1229 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001230 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001231 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001233 if (glsb->format().fPacked) {
1234 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1235 GR_GL_DEPTH_ATTACHMENT,
1236 GR_GL_RENDERBUFFER, 0));
1237 }
1238 return false;
1239 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001240 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001241 rt->config(),
1242 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001245 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001247}
1248
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001249////////////////////////////////////////////////////////////////////////////////
1250
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001251GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001252 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001253 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001255 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001256 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001257 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001259 GL_ALLOC_CALL(this->glInterface(),
1260 BufferData(GR_GL_ARRAY_BUFFER,
1261 size,
1262 NULL, // data ptr
1263 dynamic ? GR_GL_DYNAMIC_DRAW :
1264 GR_GL_STATIC_DRAW));
1265 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001266 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 // deleting bound buffer does implicit bind to 0
1268 fHWGeometryState.fVertexBuffer = NULL;
1269 return NULL;
1270 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001271 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 size, dynamic);
1273 fHWGeometryState.fVertexBuffer = vertexBuffer;
1274 return vertexBuffer;
1275 }
1276 return NULL;
1277}
1278
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001279GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001280 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001281 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001283 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001284 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001286 GL_ALLOC_CALL(this->glInterface(),
1287 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1288 size,
1289 NULL, // data ptr
1290 dynamic ? GR_GL_DYNAMIC_DRAW :
1291 GR_GL_STATIC_DRAW));
1292 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 // deleting bound buffer does implicit bind to 0
1295 fHWGeometryState.fIndexBuffer = NULL;
1296 return NULL;
1297 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001298 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 size, dynamic);
1300 fHWGeometryState.fIndexBuffer = indexBuffer;
1301 return indexBuffer;
1302 }
1303 return NULL;
1304}
1305
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001306void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001307 const GrDrawState& drawState = this->getDrawState();
1308 const GrGLRenderTarget* rt =
1309 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1310
1311 GrAssert(NULL != rt);
1312 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001313
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001314 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001315 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1316 rect.width(), rect.height());
1317 if (scissor.contains(vp)) {
1318 disableScissor();
1319 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 }
1321
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001322 if (fHWBounds.fScissorRect != scissor) {
1323 scissor.pushToGLScissor(this->glInterface());
1324 fHWBounds.fScissorRect = scissor;
1325 }
1326 if (!fHWBounds.fScissorEnabled) {
1327 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1328 fHWBounds.fScissorEnabled = true;
1329 }
1330}
1331
1332void GrGpuGL::disableScissor() {
1333 if (fHWBounds.fScissorEnabled) {
1334 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1335 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 }
1337}
1338
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001339void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001340 const GrDrawState& drawState = this->getDrawState();
1341 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001342 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001343 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001344
bsalomon@google.com74b98712011-11-11 19:46:16 +00001345 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001346 if (NULL != rect) {
1347 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001348 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001349 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001350 if (clippedRect.intersect(rtRect)) {
1351 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001352 } else {
1353 return;
1354 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001355 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001356 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001357 if (NULL != rect)
1358 this->enableScissoring(*rect);
1359 else
1360 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001361
1362 GrGLfloat r, g, b, a;
1363 static const GrGLfloat scale255 = 1.f / 255.f;
1364 a = GrColorUnpackA(color) * scale255;
1365 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001366 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001367 scaleRGB *= a;
1368 }
1369 r = GrColorUnpackR(color) * scaleRGB;
1370 g = GrColorUnpackG(color) * scaleRGB;
1371 b = GrColorUnpackB(color) * scaleRGB;
1372
1373 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001374 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001375 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001376 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001377}
1378
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001379void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001380 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001381 return;
1382 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001383
1384 this->flushRenderTarget(&GrIRect::EmptyIRect());
1385
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001386 this->disableScissor();
1387
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001388 GL_CALL(StencilMask(0xffffffff));
1389 GL_CALL(ClearStencil(0));
1390 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001391 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001392}
1393
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001394void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001395 const GrDrawState& drawState = this->getDrawState();
1396 const GrRenderTarget* rt = drawState.getRenderTarget();
1397 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001398
1399 // this should only be called internally when we know we have a
1400 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001401 GrAssert(NULL != rt->getStencilBuffer());
1402 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001403#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001404 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001405 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001406#else
1407 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001408 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001409 // turned into draws. Our contract on GrDrawTarget says that
1410 // changing the clip between stencil passes may or may not
1411 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001412 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001413#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001414 GrGLint value;
1415 if (insideClip) {
1416 value = (1 << (stencilBitCount - 1));
1417 } else {
1418 value = 0;
1419 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001420 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001421 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001422 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001423 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001424 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001425 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001426}
1427
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001428void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001429 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001430}
1431
bsalomon@google.comc4364992011-11-07 15:54:49 +00001432bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1433 int left, int top,
1434 int width, int height,
1435 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001436 size_t rowBytes) const {
1437 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001438 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001439 return false;
1440 }
1441
1442 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001443 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001444 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001445 return true;
1446 }
1447 // If we have to do memcpys to handle rowBytes then y-flip is free
1448 // Note the rowBytes might be tight to the passed in data, but if data
1449 // gets clipped in x to the target the rowBytes will no longer be tight.
1450 if (left >= 0 && (left + width) < renderTarget->width()) {
1451 return 0 == rowBytes ||
1452 GrBytesPerPixel(config) * width == rowBytes;
1453 } else {
1454 return false;
1455 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001456}
1457
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001458bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001459 int left, int top,
1460 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001461 GrPixelConfig config,
1462 void* buffer,
1463 size_t rowBytes,
1464 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001465 GrGLenum format;
1466 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001467 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001468 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001469 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001470 size_t bpp = GrBytesPerPixel(config);
1471 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1472 &left, &top, &width, &height,
1473 const_cast<const void**>(&buffer),
1474 &rowBytes)) {
1475 return false;
1476 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001477
bsalomon@google.comc6980972011-11-02 19:57:21 +00001478 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001479 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001480 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001481 switch (tgt->getResolveType()) {
1482 case GrGLRenderTarget::kCantResolve_ResolveType:
1483 return false;
1484 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001485 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001486 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001487 break;
1488 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001489 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001490 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001491 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1492 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001493 break;
1494 default:
1495 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001496 }
1497
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001498 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001499
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001500 // the read rect is viewport-relative
1501 GrGLIRect readRect;
1502 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001503
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001504 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001505 if (0 == rowBytes) {
1506 rowBytes = tightRowBytes;
1507 }
1508 size_t readDstRowBytes = tightRowBytes;
1509 void* readDst = buffer;
1510
1511 // determine if GL can read using the passed rowBytes or if we need
1512 // a scratch buffer.
1513 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1514 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001515 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001516 GrAssert(!(rowBytes % sizeof(GrColor)));
1517 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1518 readDstRowBytes = rowBytes;
1519 } else {
1520 scratch.reset(tightRowBytes * height);
1521 readDst = scratch.get();
1522 }
1523 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001524 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001525 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1526 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001527 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1528 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001529 format, type, readDst));
1530 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001531 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001532 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1533 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001534 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001535 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1536 invertY = true;
1537 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001538
1539 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001540 // API presents top-to-bottom. We must preserve the padding contents. Note
1541 // that the above readPixels did not overwrite the padding.
1542 if (readDst == buffer) {
1543 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001544 if (!invertY) {
1545 scratch.reset(tightRowBytes);
1546 void* tmpRow = scratch.get();
1547 // flip y in-place by rows
1548 const int halfY = height >> 1;
1549 char* top = reinterpret_cast<char*>(buffer);
1550 char* bottom = top + (height - 1) * rowBytes;
1551 for (int y = 0; y < halfY; y++) {
1552 memcpy(tmpRow, top, tightRowBytes);
1553 memcpy(top, bottom, tightRowBytes);
1554 memcpy(bottom, tmpRow, tightRowBytes);
1555 top += rowBytes;
1556 bottom -= rowBytes;
1557 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001558 }
1559 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001560 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001561 // copy from readDst to buffer while flipping y
1562 const int halfY = height >> 1;
1563 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001564 char* dst = reinterpret_cast<char*>(buffer);
1565 if (!invertY) {
1566 dst += (height-1) * rowBytes;
1567 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001568 for (int y = 0; y < height; y++) {
1569 memcpy(dst, src, tightRowBytes);
1570 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001571 if (invertY) {
1572 dst += rowBytes;
1573 } else {
1574 dst -= rowBytes;
1575 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001576 }
1577 }
1578 return true;
1579}
1580
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001581void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001582
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001583 GrGLRenderTarget* rt =
1584 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1585 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001586
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001587 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001588 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001589 #if GR_COLLECT_STATS
1590 ++fStats.fRenderTargetChngCnt;
1591 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001592 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001593 GrGLenum status;
1594 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001595 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001596 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001597 }
1598 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001599 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001600 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001601 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001602 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001603 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 fHWBounds.fViewportRect = vp;
1605 }
1606 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001607 if (NULL == bound || !bound->isEmpty()) {
1608 rt->flagAsNeedingResolve(bound);
1609 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001610}
1611
twiz@google.com0f31ca72011-03-18 17:38:11 +00001612GrGLenum gPrimitiveType2GLMode[] = {
1613 GR_GL_TRIANGLES,
1614 GR_GL_TRIANGLE_STRIP,
1615 GR_GL_TRIANGLE_FAN,
1616 GR_GL_POINTS,
1617 GR_GL_LINES,
1618 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001619};
1620
bsalomon@google.comd302f142011-03-03 13:54:13 +00001621#define SWAP_PER_DRAW 0
1622
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001623#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001624 #if GR_MAC_BUILD
1625 #include <AGL/agl.h>
1626 #elif GR_WIN32_BUILD
1627 void SwapBuf() {
1628 DWORD procID = GetCurrentProcessId();
1629 HWND hwnd = GetTopWindow(GetDesktopWindow());
1630 while(hwnd) {
1631 DWORD wndProcID = 0;
1632 GetWindowThreadProcessId(hwnd, &wndProcID);
1633 if(wndProcID == procID) {
1634 SwapBuffers(GetDC(hwnd));
1635 }
1636 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1637 }
1638 }
1639 #endif
1640#endif
1641
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001642void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1643 uint32_t startVertex,
1644 uint32_t startIndex,
1645 uint32_t vertexCount,
1646 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1648
twiz@google.com0f31ca72011-03-18 17:38:11 +00001649 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001650
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001651 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1652 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1653
1654 // our setupGeometry better have adjusted this to zero since
1655 // DrawElements always draws from the begining of the arrays for idx 0.
1656 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001657
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001658 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1659 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001660#if SWAP_PER_DRAW
1661 glFlush();
1662 #if GR_MAC_BUILD
1663 aglSwapBuffers(aglGetCurrentContext());
1664 int set_a_break_pt_here = 9;
1665 aglSwapBuffers(aglGetCurrentContext());
1666 #elif GR_WIN32_BUILD
1667 SwapBuf();
1668 int set_a_break_pt_here = 9;
1669 SwapBuf();
1670 #endif
1671#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001672}
1673
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001674void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1675 uint32_t startVertex,
1676 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001677 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1678
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001679 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1680
1681 // our setupGeometry better have adjusted this to zero.
1682 // DrawElements doesn't take an offset so we always adjus the startVertex.
1683 GrAssert(0 == startVertex);
1684
1685 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1686 // account for startVertex in the DrawElements case. So we always
1687 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001688 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001689#if SWAP_PER_DRAW
1690 glFlush();
1691 #if GR_MAC_BUILD
1692 aglSwapBuffers(aglGetCurrentContext());
1693 int set_a_break_pt_here = 9;
1694 aglSwapBuffers(aglGetCurrentContext());
1695 #elif GR_WIN32_BUILD
1696 SwapBuf();
1697 int set_a_break_pt_here = 9;
1698 SwapBuf();
1699 #endif
1700#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001701}
1702
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001703void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1704
1705 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001706
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001707 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001708 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001709 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001710 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1711 rt->renderFBOID()));
1712 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1713 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 #if GR_COLLECT_STATS
1715 ++fStats.fRenderTargetChngCnt;
1716 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001717 // make sure we go through flushRenderTarget() since we've modified
1718 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001719 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001720 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001721 const GrIRect dirtyRect = rt->getResolveRect();
1722 GrGLIRect r;
1723 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1724 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001725
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001726 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001727 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001728#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001729 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1730 GL_CALL(Scissor(r.fLeft, r.fBottom,
1731 r.fWidth, r.fHeight));
1732 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001733 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001735#else
1736 this->enableScissoring(dirtyRect);
1737 GL_CALL(ResolveMultisampleFramebuffer());
1738#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001740 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001741 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001742 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1743 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001744 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001745 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001746 int right = r.fLeft + r.fWidth;
1747 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001748 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1749 r.fLeft, r.fBottom, right, top,
1750 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001751 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001752 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001753 }
1754}
1755
twiz@google.com0f31ca72011-03-18 17:38:11 +00001756static const GrGLenum grToGLStencilFunc[] = {
1757 GR_GL_ALWAYS, // kAlways_StencilFunc
1758 GR_GL_NEVER, // kNever_StencilFunc
1759 GR_GL_GREATER, // kGreater_StencilFunc
1760 GR_GL_GEQUAL, // kGEqual_StencilFunc
1761 GR_GL_LESS, // kLess_StencilFunc
1762 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1763 GR_GL_EQUAL, // kEqual_StencilFunc,
1764 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001765};
1766GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1767GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1768GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1769GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1770GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1771GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1772GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1773GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1774GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1775
twiz@google.com0f31ca72011-03-18 17:38:11 +00001776static const GrGLenum grToGLStencilOp[] = {
1777 GR_GL_KEEP, // kKeep_StencilOp
1778 GR_GL_REPLACE, // kReplace_StencilOp
1779 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1780 GR_GL_INCR, // kIncClamp_StencilOp
1781 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1782 GR_GL_DECR, // kDecClamp_StencilOp
1783 GR_GL_ZERO, // kZero_StencilOp
1784 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001785};
1786GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1787GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1788GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1789GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1790GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1791GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1792GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1793GR_STATIC_ASSERT(6 == kZero_StencilOp);
1794GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1795
reed@google.comac10a2d2010-12-22 21:39:39 +00001796void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001797 const GrDrawState& drawState = this->getDrawState();
1798
1799 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001800
1801 // use stencil for clipping if clipping is enabled and the clip
1802 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001803 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001804 bool drawClipToStencil =
1805 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001806 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1807 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001808 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1809 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001810
1811 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001812
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001813 // we can't simultaneously perform stencil-clipping and
1814 // modify the stencil clip
1815 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001816
bsalomon@google.comd302f142011-03-03 13:54:13 +00001817 if (settings->isDisabled()) {
1818 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001819 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001820 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001821 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822
1823 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001824 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001825 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001826 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001827 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001828 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001829 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1830 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1831 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1832 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1833 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1834 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1835 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1836 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001837 }
1838 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001839 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001840 GrStencilBuffer* stencilBuffer =
1841 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001842 if (NULL != stencilBuffer) {
1843 stencilBits = stencilBuffer->bits();
1844 }
1845 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001846 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001847
1848 GrGLuint clipStencilMask = 0;
1849 GrGLuint userStencilMask = ~0;
1850 if (stencilBits > 0) {
1851 clipStencilMask = 1 << (stencilBits - 1);
1852 userStencilMask = clipStencilMask - 1;
1853 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001854
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001855 unsigned int frontRef = settings->frontFuncRef();
1856 unsigned int frontMask = settings->frontFuncMask();
1857 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001858 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001860 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001861 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1862 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001864 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001865 stencilClip, 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.comd302f142011-03-03 13:54:13 +00001868 stencilClip,
1869 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.com8f9cbd62011-12-09 15:55:34 +00001891 if (drawClipToStencil) {
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 {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001895 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001896 stencilClip, settings->backFunc())];
1897 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001898 stencilClip,
1899 clipStencilMask,
1900 userStencilMask,
1901 &backRef,
1902 &backMask);
1903 backWriteMask &= userStencilMask;
1904 }
1905
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001906 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1907 frontRef, frontMask));
1908 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1909 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1910 backRef, backMask));
1911 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1912 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001913 grToGLStencilOp[settings->frontFailOp()],
1914 grToGLStencilOp[settings->frontPassOp()],
1915 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001916
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001917 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001918 grToGLStencilOp[settings->backFailOp()],
1919 grToGLStencilOp[settings->backPassOp()],
1920 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001921 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001922 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1923 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001924 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1925 grToGLStencilOp[settings->frontPassOp()],
1926 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001927 }
1928 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001929 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001930 fHWStencilClip = stencilClip;
1931 }
1932}
1933
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001934void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001935 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001936 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001937 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1938 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001939 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001940 bool smoothLines = false;
1941
bsalomon@google.com0650e812011-04-08 18:07:53 +00001942 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001943 smoothLines = this->willUseHWAALines();
1944 if (smoothLines) {
1945 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1946 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1947 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1948 // must disable msaa to use line smoothing
1949 if (rt->isMultisampled() &&
1950 kNo_TriState != fHWAAState.fMSAAEnabled) {
1951 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1952 fHWAAState.fMSAAEnabled = kNo_TriState;
1953 }
1954 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001955 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001956 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1957 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1958 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1959 }
1960 }
1961 }
1962 if (!smoothLines && rt->isMultisampled()) {
1963 if (this->getDrawState().isHWAntialiasState()) {
1964 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1965 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1966 fHWAAState.fMSAAEnabled = kYes_TriState;
1967 }
1968 } else {
1969 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1970 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1971 fHWAAState.fMSAAEnabled = kNo_TriState;
1972 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001973 }
1974 }
1975 }
1976}
1977
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001978void GrGpuGL::flushBlend(GrPrimitiveType type,
1979 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001980 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001981 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001982 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001983 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001984 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001985 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001986 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
1987 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001988 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1989 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001990 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
1991 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001992 }
1993 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001994 // any optimization to disable blending should
1995 // have already been applied and tweaked the coeffs
1996 // to (1, 0).
1997 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1998 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001999 if (blendOff) {
2000 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002001 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002002 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002003 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002004 } else {
2005 if (kYes_TriState != fHWBlendState.fEnabled) {
2006 GL_CALL(Enable(GR_GL_BLEND));
2007 fHWBlendState.fEnabled = kYes_TriState;
2008 }
2009 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2010 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002011 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2012 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002013 fHWBlendState.fSrcCoeff = srcCoeff;
2014 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002015 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002016 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002017 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2018 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002019 (!fHWBlendState.fConstColorValid ||
2020 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002021
2022 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002023 GrColorUnpackR(blendConst) / 255.f,
2024 GrColorUnpackG(blendConst) / 255.f,
2025 GrColorUnpackB(blendConst) / 255.f,
2026 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002027 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002028 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002029 fHWBlendState.fConstColor = blendConst;
2030 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002031 }
2032 }
2033 }
2034}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002035namespace {
2036
2037unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002038 switch (filter) {
2039 case GrSamplerState::kBilinear_Filter:
2040 case GrSamplerState::k4x4Downsample_Filter:
2041 return GR_GL_LINEAR;
2042 case GrSamplerState::kNearest_Filter:
2043 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002044 case GrSamplerState::kErode_Filter:
2045 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002046 return GR_GL_NEAREST;
2047 default:
2048 GrAssert(!"Unknown filter type");
2049 return GR_GL_LINEAR;
2050 }
2051}
2052
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002053// get_swizzle is only called from this .cpp so it is OK to inline it here
2054inline const GrGLenum* get_swizzle(GrPixelConfig config,
2055 const GrSamplerState& sampler,
2056 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002057 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002058 if (glCaps.textureRedSupport()) {
2059 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2060 GR_GL_RED, GR_GL_RED };
2061 return gRedSmear;
2062 } else {
2063 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2064 GR_GL_ALPHA, GR_GL_ALPHA };
2065 return gAlphaSmear;
2066 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002067 } else if (sampler.swapsRAndB()) {
2068 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2069 GR_GL_RED, GR_GL_ALPHA };
2070 return gRedBlueSwap;
2071 } else {
2072 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2073 GR_GL_BLUE, GR_GL_ALPHA };
2074 return gStraight;
2075 }
2076}
2077
2078void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2079 // should add texparameteri to interface to make 1 instead of 4 calls here
2080 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2081 GR_GL_TEXTURE_SWIZZLE_R,
2082 swizzle[0]));
2083 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2084 GR_GL_TEXTURE_SWIZZLE_G,
2085 swizzle[1]));
2086 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2087 GR_GL_TEXTURE_SWIZZLE_B,
2088 swizzle[2]));
2089 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2090 GR_GL_TEXTURE_SWIZZLE_A,
2091 swizzle[3]));
2092}
2093}
2094
bsalomon@google.comffca4002011-02-22 20:34:01 +00002095bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002096
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002097 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002098 // GrGpu::setupClipAndFlushState should have already checked this
2099 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002100 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002101
tomhudson@google.com93813632011-10-27 20:21:16 +00002102 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002103 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002104 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002105 GrGLTexture* nextTexture =
2106 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002107
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002108 // true for now, but maybe not with GrEffect.
2109 GrAssert(NULL != nextTexture);
2110 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002111 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002112 // the last bound texture, but it needs resolving. So keep this
2113 // out of the "last != next" check.
2114 GrGLRenderTarget* texRT =
2115 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2116 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002117 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002118 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002119
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002120 if (fHWBoundTextures[s] != nextTexture) {
2121 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002122 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002123 #if GR_COLLECT_STATS
2124 ++fStats.fTextureChngCnt;
2125 #endif
2126 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002127 fHWBoundTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002128 // The texture matrix has to compensate for texture width/height
2129 // and NPOT-embedded-in-POT
2130 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002131 }
2132
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002133 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002134 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002135 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002136 nextTexture->getCachedTexParams(&timestamp);
2137 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002138 GrGLTexture::TexParams newTexParams;
2139
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002140 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002141
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002142 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002143 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2144 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002145 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002146 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002147 sizeof(newTexParams.fSwizzleRGBA));
2148 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002149 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002150 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002151 GR_GL_TEXTURE_MAG_FILTER,
2152 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002154 GR_GL_TEXTURE_MIN_FILTER,
2155 newTexParams.fFilter));
2156 }
2157 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002158 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002159 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002160 GR_GL_TEXTURE_WRAP_S,
2161 newTexParams.fWrapS));
2162 }
2163 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002164 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002166 GR_GL_TEXTURE_WRAP_T,
2167 newTexParams.fWrapT));
2168 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002169 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002170 (setAll ||
2171 memcmp(newTexParams.fSwizzleRGBA,
2172 oldTexParams.fSwizzleRGBA,
2173 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002174 this->setTextureUnit(s);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002175 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2176 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002177 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002178 nextTexture->setCachedTexParams(newTexParams,
2179 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002180 }
2181 }
2182
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002183 GrIRect* rect = NULL;
2184 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002185 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002186 fClip.hasConservativeBounds()) {
2187 fClip.getConservativeBounds().roundOut(&clipBounds);
2188 rect = &clipBounds;
2189 }
2190 this->flushRenderTarget(rect);
2191 this->flushAAState(type);
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002192
2193 if (drawState->isDitherState()) {
2194 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002195 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002196 fHWDitherEnabled = kYes_TriState;
2197 }
2198 } else {
2199 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002200 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002201 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002202 }
2203 }
2204
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002205 if (drawState->isColorWriteDisabled()) {
2206 if (kNo_TriState != fHWWriteToColor) {
2207 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2208 GR_GL_FALSE, GR_GL_FALSE));
2209 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002210 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002211 } else {
2212 if (kYes_TriState != fHWWriteToColor) {
2213 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2214 fHWWriteToColor = kYes_TriState;
2215 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002216 }
2217
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002218 if (fHWDrawFace != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002219 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002220 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002221 GL_CALL(Enable(GR_GL_CULL_FACE));
2222 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002223 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002224 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002225 GL_CALL(Enable(GR_GL_CULL_FACE));
2226 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002227 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002228 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002229 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002230 break;
2231 default:
2232 GrCrash("Unknown draw face.");
2233 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002234 fHWDrawFace = drawState->getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002235 }
2236
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002237#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002238 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002239 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002240 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002241 NULL == drawState->getRenderTarget() ||
2242 NULL == drawState->getTexture(s) ||
2243 drawState->getTexture(s)->asRenderTarget() !=
2244 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002245 }
2246#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002247
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002248 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002249
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002250 // This copy must happen after flushStencil() is called. flushStencil()
2251 // relies on detecting when the kModifyStencilClip_StateBit state has
2252 // changed since the last draw.
2253 fHWDrawState.copyStateFlags(*drawState);
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002254
2255 // TODO: may no longer need this
robertphillips@google.com1942c052012-05-03 17:58:27 +00002256 // only GrInOrderDrawBuffer ever needs to ref/unref the textures
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002257 fHWDrawState.disableBehavior(GrDrawState::kTexturesNeedRef_BehaviorBit);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002258 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002259}
2260
2261void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002262 if (fHWGeometryState.fVertexBuffer != buffer) {
2263 fHWGeometryState.fArrayPtrsDirty = true;
2264 fHWGeometryState.fVertexBuffer = buffer;
2265 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002266}
2267
2268void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002269 if (fHWGeometryState.fVertexBuffer == buffer) {
2270 // deleting bound buffer does implied bind to 0
2271 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002272 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002273 }
2274}
2275
2276void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002277 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002278}
2279
2280void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 if (fHWGeometryState.fIndexBuffer == buffer) {
2282 // deleting bound buffer does implied bind to 0
2283 fHWGeometryState.fIndexBuffer = NULL;
2284 }
2285}
2286
reed@google.comac10a2d2010-12-22 21:39:39 +00002287void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2288 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002289 GrDrawState* drawState = this->drawState();
2290 if (drawState->getRenderTarget() == renderTarget) {
2291 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002292 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002293 if (fHWBoundRenderTarget == renderTarget) {
2294 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002296}
2297
2298void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002299 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002300 GrDrawState* drawState = this->drawState();
2301 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002302 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002303 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002304 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002305 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002306 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002307 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002308 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002309}
2310
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002311bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2312 bool getSizedInternalFormat,
2313 GrGLenum* internalFormat,
2314 GrGLenum* externalFormat,
2315 GrGLenum* externalType) {
2316 GrGLenum dontCare;
2317 if (NULL == internalFormat) {
2318 internalFormat = &dontCare;
2319 }
2320 if (NULL == externalFormat) {
2321 externalFormat = &dontCare;
2322 }
2323 if (NULL == externalType) {
2324 externalType = &dontCare;
2325 }
2326
reed@google.comac10a2d2010-12-22 21:39:39 +00002327 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002328 case kRGBA_8888_PM_GrPixelConfig:
2329 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002330 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002331 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002332 if (getSizedInternalFormat) {
2333 *internalFormat = GR_GL_RGBA8;
2334 } else {
2335 *internalFormat = GR_GL_RGBA;
2336 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002337 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002338 break;
2339 case kBGRA_8888_PM_GrPixelConfig:
2340 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002341 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002342 return false;
2343 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002344 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002345 if (getSizedInternalFormat) {
2346 *internalFormat = GR_GL_BGRA8;
2347 } else {
2348 *internalFormat = GR_GL_BGRA;
2349 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002350 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002351 if (getSizedInternalFormat) {
2352 *internalFormat = GR_GL_RGBA8;
2353 } else {
2354 *internalFormat = GR_GL_RGBA;
2355 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002356 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002357 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002358 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002359 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002360 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002361 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002362 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002363 if (getSizedInternalFormat) {
2364 if (this->glBinding() == kDesktop_GrGLBinding) {
2365 return false;
2366 } else {
2367 *internalFormat = GR_GL_RGB565;
2368 }
2369 } else {
2370 *internalFormat = GR_GL_RGB;
2371 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002372 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002373 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002374 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002375 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002376 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002377 if (getSizedInternalFormat) {
2378 *internalFormat = GR_GL_RGBA4;
2379 } else {
2380 *internalFormat = GR_GL_RGBA;
2381 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002382 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002383 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002384 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002385 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002386 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002387 // glCompressedTexImage doesn't take external params
2388 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002389 // no sized/unsized internal format distinction here
2390 *internalFormat = GR_GL_PALETTE8_RGBA8;
2391 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002392 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002393 } else {
2394 return false;
2395 }
2396 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002397 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002398 if (this->glCaps().textureRedSupport()) {
2399 *internalFormat = GR_GL_RED;
2400 *externalFormat = GR_GL_RED;
2401 if (getSizedInternalFormat) {
2402 *internalFormat = GR_GL_R8;
2403 } else {
2404 *internalFormat = GR_GL_RED;
2405 }
2406 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002407 } else {
2408 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002409 *externalFormat = GR_GL_ALPHA;
2410 if (getSizedInternalFormat) {
2411 *internalFormat = GR_GL_ALPHA8;
2412 } else {
2413 *internalFormat = GR_GL_ALPHA;
2414 }
2415 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002416 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002417 break;
2418 default:
2419 return false;
2420 }
2421 return true;
2422}
2423
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002424void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002425 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002426 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002427 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002428 fActiveTextureUnitIdx = unit;
2429 }
2430}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002431
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002432void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002433 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002434 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002435 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2436 }
2437}
2438
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002439void GrGpuGL::resetDirtyFlags() {
2440 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2441}
2442
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443void GrGpuGL::setBuffers(bool indexed,
2444 int* extraVertexOffset,
2445 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002446
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002447 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002448
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002449 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2450
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002451 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002452 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002453 case kBuffer_GeometrySrcType:
2454 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002455 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002456 break;
2457 case kArray_GeometrySrcType:
2458 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002459 this->finalizeReservedVertices();
2460 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2461 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462 break;
2463 default:
2464 vbuf = NULL; // suppress warning
2465 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002466 }
2467
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002468 GrAssert(NULL != vbuf);
2469 GrAssert(!vbuf->isLocked());
2470 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002471 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002472 fHWGeometryState.fArrayPtrsDirty = true;
2473 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002474 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002475
2476 if (indexed) {
2477 GrAssert(NULL != extraIndexOffset);
2478
2479 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002480 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002481 case kBuffer_GeometrySrcType:
2482 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002483 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002484 break;
2485 case kArray_GeometrySrcType:
2486 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002487 this->finalizeReservedIndices();
2488 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2489 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002490 break;
2491 default:
2492 ibuf = NULL; // suppress warning
2493 GrCrash("Unknown geometry src type!");
2494 }
2495
2496 GrAssert(NULL != ibuf);
2497 GrAssert(!ibuf->isLocked());
2498 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002499 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002500 fHWGeometryState.fIndexBuffer = ibuf;
2501 }
2502 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002503}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002504