blob: 975b2525761726cb45ef6355ab75933b76c2ee30 [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()) {
467 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
468 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
469 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000470 fHWAAState.fMSAAEnabled = false;
471 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000472 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000473
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000474 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000475 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000476
reed@google.comac10a2d2010-12-22 21:39:39 +0000477 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000478 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000479
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000480 // invalid
481 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000482
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000483 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000484
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000485 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000486
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000487 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000488
tomhudson@google.com93813632011-10-27 20:21:16 +0000489 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000490 fHWDrawState.setTexture(s, NULL);
491 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
492 -GR_ScalarMax,
493 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000494 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000495 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000496 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000497
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000498 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000499 // set to true to force disableScissor to make a GL call.
500 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000501 this->disableScissor();
502
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000503 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000504
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000505 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 fHWStencilClip = false;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000507
508 // TODO: I believe this should actually go in GrGpu::onResetContext
509 // rather than here
510 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
512 fHWGeometryState.fIndexBuffer = NULL;
513 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000514
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000515 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000516
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000517 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000518 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000519
520 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000521 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000522 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
523 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000524 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000525 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
526 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000527 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000528 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
529 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000530 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000531 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
532 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000533}
534
bsalomon@google.come269f212011-11-07 13:29:52 +0000535GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000536 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000537 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000538 return NULL;
539 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000540
bsalomon@google.com99621082011-11-15 16:47:16 +0000541 glTexDesc.fWidth = desc.fWidth;
542 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000543 glTexDesc.fConfig = desc.fConfig;
544 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
545 glTexDesc.fOwnsID = false;
546 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
547
548 GrGLTexture* texture = NULL;
549 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
550 GrGLRenderTarget::Desc glRTDesc;
551 glRTDesc.fRTFBOID = 0;
552 glRTDesc.fTexFBOID = 0;
553 glRTDesc.fMSColorRenderbufferID = 0;
554 glRTDesc.fOwnIDs = true;
555 glRTDesc.fConfig = desc.fConfig;
556 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000557 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
558 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000559 glTexDesc.fTextureID,
560 &glRTDesc)) {
561 return NULL;
562 }
563 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
564 } else {
565 texture = new GrGLTexture(this, glTexDesc);
566 }
567 if (NULL == texture) {
568 return NULL;
569 }
570
571 this->setSpareTextureUnit();
572 return texture;
573}
574
575GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
576 GrGLRenderTarget::Desc glDesc;
577 glDesc.fConfig = desc.fConfig;
578 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
579 glDesc.fMSColorRenderbufferID = 0;
580 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
581 glDesc.fSampleCnt = desc.fSampleCnt;
582 glDesc.fOwnIDs = false;
583 GrGLIRect viewport;
584 viewport.fLeft = 0;
585 viewport.fBottom = 0;
586 viewport.fWidth = desc.fWidth;
587 viewport.fHeight = desc.fHeight;
588
589 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
590 if (desc.fStencilBits) {
591 GrGLStencilBuffer::Format format;
592 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
593 format.fPacked = false;
594 format.fStencilBits = desc.fStencilBits;
595 format.fTotalBits = desc.fStencilBits;
596 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
597 0,
598 desc.fWidth,
599 desc.fHeight,
600 desc.fSampleCnt,
601 format);
602 tgt->setStencilBuffer(sb);
603 sb->unref();
604 }
605 return tgt;
606}
607
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000608////////////////////////////////////////////////////////////////////////////////
609
bsalomon@google.com6f379512011-11-16 20:36:03 +0000610void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
611 int left, int top, int width, int height,
612 GrPixelConfig config, const void* buffer,
613 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000614 if (NULL == buffer) {
615 return;
616 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000617 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
618
bsalomon@google.com6f379512011-11-16 20:36:03 +0000619 this->setSpareTextureUnit();
620 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
621 GrGLTexture::Desc desc;
622 desc.fConfig = glTex->config();
623 desc.fWidth = glTex->width();
624 desc.fHeight = glTex->height();
625 desc.fOrientation = glTex->orientation();
626 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000627
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000628 this->uploadTexData(desc, false,
629 left, top, width, height,
630 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000631}
632
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000633namespace {
634bool adjust_pixel_ops_params(int surfaceWidth,
635 int surfaceHeight,
636 size_t bpp,
637 int* left, int* top, int* width, int* height,
638 const void** data,
639 size_t* rowBytes) {
640 if (!*rowBytes) {
641 *rowBytes = *width * bpp;
642 }
643
644 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
645 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
646
647 if (!subRect.intersect(bounds)) {
648 return false;
649 }
650 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
651 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
652
653 *left = subRect.fLeft;
654 *top = subRect.fTop;
655 *width = subRect.width();
656 *height = subRect.height();
657 return true;
658}
659}
660
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000661bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
662 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000663 int left, int top, int width, int height,
664 GrPixelConfig dataConfig,
665 const void* data,
666 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000667 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000668
669 size_t bpp = GrBytesPerPixel(dataConfig);
670 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
671 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000672 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000673 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000674 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000675
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000676 // in case we need a temporary, trimmed copy of the src pixels
677 SkAutoSMalloc<128 * 128> tempStorage;
678
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000679 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000680 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000681 if (useTexStorage) {
682 if (kDesktop_GrGLBinding == this->glBinding()) {
683 // 565 is not a sized internal format on desktop GL. So on desktop
684 // with 565 we always use an unsized internal format to let the
685 // system pick the best sized format to convert the 565 data to.
686 // Since glTexStorage only allows sized internal formats we will
687 // instead fallback to glTexImage2D.
688 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
689 } else {
690 // ES doesn't allow paletted textures to be used with tex storage
691 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
692 }
693 }
694
695 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000696 GrGLenum externalFormat;
697 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000698 // glTexStorage requires sized internal formats on both desktop and ES. ES
699 // glTexImage requires an unsized format.
700 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
701 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000702 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000703 }
704
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000705 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000706 // paletted textures cannot be updated
707 return false;
708 }
709
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000710 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000711 * check whether to allocate a temporary buffer for flipping y or
712 * because our srcData has extra bytes past each row. If so, we need
713 * to trim those off here, since GL ES may not let us specify
714 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000715 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000716 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000717 bool swFlipY = false;
718 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000719 if (NULL != data) {
720 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000721 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000722 glFlipY = true;
723 } else {
724 swFlipY = true;
725 }
726 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000727 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000728 // can't use this for flipping, only non-neg values allowed. :(
729 if (rowBytes != trimRowBytes) {
730 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
731 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
732 restoreGLRowLength = true;
733 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000734 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000735 if (trimRowBytes != rowBytes || swFlipY) {
736 // copy data into our new storage, skipping the trailing bytes
737 size_t trimSize = height * trimRowBytes;
738 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000739 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000740 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000741 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000742 char* dst = (char*)tempStorage.reset(trimSize);
743 for (int y = 0; y < height; y++) {
744 memcpy(dst, src, trimRowBytes);
745 if (swFlipY) {
746 src -= rowBytes;
747 } else {
748 src += rowBytes;
749 }
750 dst += trimRowBytes;
751 }
752 // now point data to our copied version
753 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000754 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000755 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000756 if (glFlipY) {
757 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
758 }
759 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000760 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000761 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000762 if (isNewTexture &&
763 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000764 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000765 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000766 if (useTexStorage) {
767 // We never resize or change formats of textures. We don't use
768 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000769 GL_ALLOC_CALL(this->glInterface(),
770 TexStorage2D(GR_GL_TEXTURE_2D,
771 1, // levels
772 internalFormat,
773 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000774 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000775 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
776 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
777 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000778 GL_ALLOC_CALL(this->glInterface(),
779 CompressedTexImage2D(GR_GL_TEXTURE_2D,
780 0, // level
781 internalFormat,
782 desc.fWidth, desc.fHeight,
783 0, // border
784 imageSize,
785 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000786 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000787 GL_ALLOC_CALL(this->glInterface(),
788 TexImage2D(GR_GL_TEXTURE_2D,
789 0, // level
790 internalFormat,
791 desc.fWidth, desc.fHeight,
792 0, // border
793 externalFormat, externalType,
794 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000795 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000796 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000797 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000798 if (error != GR_GL_NO_ERROR) {
799 succeeded = false;
800 } else {
801 // if we have data and we used TexStorage to create the texture, we
802 // now upload with TexSubImage.
803 if (NULL != data && useTexStorage) {
804 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
805 0, // level
806 left, top,
807 width, height,
808 externalFormat, externalType,
809 data));
810 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000811 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000812 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000813 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000814 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000816 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
817 0, // level
818 left, top,
819 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000820 externalFormat, externalType, data));
821 }
822
823 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000824 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000825 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000826 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000827 if (glFlipY) {
828 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
829 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000830 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000831}
832
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000833namespace {
834bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
835 int sampleCount,
836 GrGLenum format,
837 int width, int height) {
838 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
839 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
840 bool created = false;
841 if (GrGLCaps::kNVDesktop_CoverageAAType ==
842 ctxInfo.caps().coverageAAType()) {
843 const GrGLCaps::MSAACoverageMode& mode =
844 ctxInfo.caps().getMSAACoverageMode(sampleCount);
845 GL_ALLOC_CALL(ctxInfo.interface(),
846 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
847 mode.fCoverageSampleCnt,
848 mode.fColorSampleCnt,
849 format,
850 width, height));
851 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
852 }
853 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000854 // glRBMS will fail if requested samples is > max samples.
855 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000856 GL_ALLOC_CALL(ctxInfo.interface(),
857 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
858 sampleCount,
859 format,
860 width, height));
861 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
862 }
863 return created;
864}
865}
866
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000867bool GrGpuGL::createRenderTargetObjects(int width, int height,
868 GrGLuint texID,
869 GrGLRenderTarget::Desc* desc) {
870 desc->fMSColorRenderbufferID = 0;
871 desc->fRTFBOID = 0;
872 desc->fTexFBOID = 0;
873 desc->fOwnIDs = true;
874
875 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876
bsalomon@google.comab15d612011-08-09 12:57:56 +0000877 GrGLenum msColorFormat = 0; // suppress warning
878
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000879 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880 if (!desc->fTexFBOID) {
881 goto FAILED;
882 }
883
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000884
885 // If we are using multisampling we will create two FBOS. We render
886 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000887 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000888 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000889 goto FAILED;
890 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
892 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893 if (!desc->fRTFBOID ||
894 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000895 !this->configToGLFormats(desc->fConfig,
896 // GLES requires sized internal formats
897 kES2_GrGLBinding == this->glBinding(),
898 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 goto FAILED;
900 }
901 } else {
902 desc->fRTFBOID = desc->fTexFBOID;
903 }
904
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000905 // below here we may bind the FBO
906 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907 if (desc->fRTFBOID != desc->fTexFBOID) {
908 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000911 if (!renderbuffer_storage_msaa(fGLContextInfo,
912 desc->fSampleCnt,
913 msColorFormat,
914 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915 goto FAILED;
916 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000917 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
918 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919 GR_GL_COLOR_ATTACHMENT0,
920 GR_GL_RENDERBUFFER,
921 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000922 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000923 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
924 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
925 goto FAILED;
926 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000927 fGLContextInfo.caps().markConfigAsValidColorAttachment(
928 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 }
930 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000931 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000933 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
934 GR_GL_COLOR_ATTACHMENT0,
935 GR_GL_TEXTURE_2D,
936 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000937 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000938 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
939 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
940 goto FAILED;
941 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000942 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943 }
944
945 return true;
946
947FAILED:
948 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000949 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950 }
951 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000952 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000953 }
954 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000955 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000956 }
957 return false;
958}
959
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000960// good to set a break-point here to know when createTexture fails
961static GrTexture* return_null_texture() {
962// GrAssert(!"null texture");
963 return NULL;
964}
965
966#if GR_DEBUG
967static size_t as_size_t(int x) {
968 return x;
969}
970#endif
971
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000972GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000973 const void* srcData,
974 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000975
976#if GR_COLLECT_STATS
977 ++fStats.fTextureCreateCnt;
978#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000979
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000980 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000981 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000982
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000983 // Attempt to catch un- or wrongly initialized sample counts;
984 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
985
bsalomon@google.com99621082011-11-15 16:47:16 +0000986 glTexDesc.fWidth = desc.fWidth;
987 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000988 glTexDesc.fConfig = desc.fConfig;
989 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000990
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000991 glRTDesc.fMSColorRenderbufferID = 0;
992 glRTDesc.fRTFBOID = 0;
993 glRTDesc.fTexFBOID = 0;
994 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000995 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000996
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000997 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000998
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000999 const Caps& caps = this->getCaps();
1000
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001001 // We keep GrRenderTargets in GL's normal orientation so that they
1002 // can be drawn to by the outside world without the client having
1003 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001004 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001005 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001006
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001007 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001008 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001009 desc.fSampleCnt) {
1010 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001011 }
1012
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001014 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1015 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001016 return return_null_texture();
1017 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001018 }
1019
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001020 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001021 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001022 // provides a hint about how this texture will be used
1023 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1024 GR_GL_TEXTURE_USAGE,
1025 GR_GL_FRAMEBUFFER_ATTACHMENT));
1026 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001027 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001028 return return_null_texture();
1029 }
1030
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001031 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001032 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001033
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001034 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1035 // drivers have a bug where an FBO won't be complete if it includes a
1036 // texture that is not mipmap complete (considering the filter in use).
1037 GrGLTexture::TexParams initialTexParams;
1038 // we only set a subset here so invalidate first
1039 initialTexParams.invalidate();
1040 initialTexParams.fFilter = GR_GL_NEAREST;
1041 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1042 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001043 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1044 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001045 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1047 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001048 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001049 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1050 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001051 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001052 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1053 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001054 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001055 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1056 glTexDesc.fWidth, glTexDesc.fHeight,
1057 desc.fConfig, srcData, rowBytes)) {
1058 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1059 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001060 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001061
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 if (renderTarget) {
1064#if GR_COLLECT_STATS
1065 ++fStats.fRenderTargetCreateCnt;
1066#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001067 // unbind the texture from the texture unit before binding it to the frame buffer
1068 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1069
bsalomon@google.com99621082011-11-15 16:47:16 +00001070 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1071 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001072 glTexDesc.fTextureID,
1073 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001074 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 return return_null_texture();
1076 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001077 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001079 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001081 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001082#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001083 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1084 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001085#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 return tex;
1087}
1088
1089namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001090
1091const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1092
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001093void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1094 GrGLuint rb,
1095 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001096 // we shouldn't ever know one size and not the other
1097 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1098 (kUnknownBitCount == format->fTotalBits));
1099 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001100 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1102 (GrGLint*)&format->fStencilBits);
1103 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001104 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1106 (GrGLint*)&format->fTotalBits);
1107 format->fTotalBits += format->fStencilBits;
1108 } else {
1109 format->fTotalBits = format->fStencilBits;
1110 }
1111 }
1112}
1113}
1114
1115bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1116 int width, int height) {
1117
1118 // All internally created RTs are also textures. We don't create
1119 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1120 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001121 GrAssert(width >= rt->width());
1122 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123
1124 int samples = rt->numSamples();
1125 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001126 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 if (!sbID) {
1128 return false;
1129 }
1130
1131 GrGLStencilBuffer* sb = NULL;
1132
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001133 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 // we start with the last stencil format that succeeded in hopes
1137 // that we won't go through this loop more than once after the
1138 // first (painful) stencil creation.
1139 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001140 const GrGLCaps::StencilFormat& sFmt =
1141 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001142 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001143 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001145 bool created;
1146 if (samples > 0) {
1147 created = renderbuffer_storage_msaa(fGLContextInfo,
1148 samples,
1149 sFmt.fInternalFormat,
1150 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001152 GL_ALLOC_CALL(this->glInterface(),
1153 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001154 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001155 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001156 created =
1157 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001159 if (created) {
1160 // After sized formats we attempt an unsized format and take
1161 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001162 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001163 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001164 sb = new GrGLStencilBuffer(this, sbID, width, height,
1165 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1167 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001168 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 return true;
1171 }
1172 sb->abandon(); // otherwise we lose sbID
1173 sb->unref();
1174 }
1175 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001176 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001177 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178}
1179
1180bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1181 GrRenderTarget* rt) {
1182 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1183
1184 GrGLuint fbo = glrt->renderFBOID();
1185
1186 if (NULL == sb) {
1187 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001188 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 GR_GL_STENCIL_ATTACHMENT,
1190 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 GR_GL_DEPTH_ATTACHMENT,
1193 GR_GL_RENDERBUFFER, 0));
1194#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001195 GrGLenum status;
1196 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1198#endif
1199 }
1200 return true;
1201 } else {
1202 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1203 GrGLuint rb = glsb->renderbufferID();
1204
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001205 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1207 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 GR_GL_STENCIL_ATTACHMENT,
1209 GR_GL_RENDERBUFFER, rb));
1210 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001211 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 GR_GL_DEPTH_ATTACHMENT,
1213 GR_GL_RENDERBUFFER, rb));
1214 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001215 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 GR_GL_DEPTH_ATTACHMENT,
1217 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001220 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001221 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001222 glsb->format())) {
1223 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1224 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001226 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001228 if (glsb->format().fPacked) {
1229 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1230 GR_GL_DEPTH_ATTACHMENT,
1231 GR_GL_RENDERBUFFER, 0));
1232 }
1233 return false;
1234 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001235 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001236 rt->config(),
1237 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001240 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001242}
1243
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001244////////////////////////////////////////////////////////////////////////////////
1245
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001246GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001247 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001251 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001252 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001254 GL_ALLOC_CALL(this->glInterface(),
1255 BufferData(GR_GL_ARRAY_BUFFER,
1256 size,
1257 NULL, // data ptr
1258 dynamic ? GR_GL_DYNAMIC_DRAW :
1259 GR_GL_STATIC_DRAW));
1260 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 // deleting bound buffer does implicit bind to 0
1263 fHWGeometryState.fVertexBuffer = NULL;
1264 return NULL;
1265 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001266 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 size, dynamic);
1268 fHWGeometryState.fVertexBuffer = vertexBuffer;
1269 return vertexBuffer;
1270 }
1271 return NULL;
1272}
1273
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001274GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001275 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001276 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001278 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001279 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001281 GL_ALLOC_CALL(this->glInterface(),
1282 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1283 size,
1284 NULL, // data ptr
1285 dynamic ? GR_GL_DYNAMIC_DRAW :
1286 GR_GL_STATIC_DRAW));
1287 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 // deleting bound buffer does implicit bind to 0
1290 fHWGeometryState.fIndexBuffer = NULL;
1291 return NULL;
1292 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001293 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 size, dynamic);
1295 fHWGeometryState.fIndexBuffer = indexBuffer;
1296 return indexBuffer;
1297 }
1298 return NULL;
1299}
1300
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001301void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001302 const GrDrawState& drawState = this->getDrawState();
1303 const GrGLRenderTarget* rt =
1304 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1305
1306 GrAssert(NULL != rt);
1307 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001308
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001309 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001310 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1311 rect.width(), rect.height());
1312 if (scissor.contains(vp)) {
1313 disableScissor();
1314 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 }
1316
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001317 if (fHWBounds.fScissorRect != scissor) {
1318 scissor.pushToGLScissor(this->glInterface());
1319 fHWBounds.fScissorRect = scissor;
1320 }
1321 if (!fHWBounds.fScissorEnabled) {
1322 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1323 fHWBounds.fScissorEnabled = true;
1324 }
1325}
1326
1327void GrGpuGL::disableScissor() {
1328 if (fHWBounds.fScissorEnabled) {
1329 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1330 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 }
1332}
1333
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001334void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001335 const GrDrawState& drawState = this->getDrawState();
1336 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001337 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001338 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001339
bsalomon@google.com74b98712011-11-11 19:46:16 +00001340 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001341 if (NULL != rect) {
1342 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001343 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001344 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001345 if (clippedRect.intersect(rtRect)) {
1346 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001347 } else {
1348 return;
1349 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001351 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001352 if (NULL != rect)
1353 this->enableScissoring(*rect);
1354 else
1355 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001356
1357 GrGLfloat r, g, b, a;
1358 static const GrGLfloat scale255 = 1.f / 255.f;
1359 a = GrColorUnpackA(color) * scale255;
1360 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001361 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001362 scaleRGB *= a;
1363 }
1364 r = GrColorUnpackR(color) * scaleRGB;
1365 g = GrColorUnpackG(color) * scaleRGB;
1366 b = GrColorUnpackB(color) * scaleRGB;
1367
1368 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001369 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001370 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001371 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001372}
1373
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001374void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001375 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001376 return;
1377 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001378
1379 this->flushRenderTarget(&GrIRect::EmptyIRect());
1380
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001381 this->disableScissor();
1382
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001383 GL_CALL(StencilMask(0xffffffff));
1384 GL_CALL(ClearStencil(0));
1385 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001386 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001387}
1388
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001389void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001390 const GrDrawState& drawState = this->getDrawState();
1391 const GrRenderTarget* rt = drawState.getRenderTarget();
1392 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001393
1394 // this should only be called internally when we know we have a
1395 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001396 GrAssert(NULL != rt->getStencilBuffer());
1397 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001398#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001399 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001400 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001401#else
1402 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001403 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001404 // turned into draws. Our contract on GrDrawTarget says that
1405 // changing the clip between stencil passes may or may not
1406 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001407 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001408#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001409 GrGLint value;
1410 if (insideClip) {
1411 value = (1 << (stencilBitCount - 1));
1412 } else {
1413 value = 0;
1414 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001415 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001416 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001417 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001418 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001419 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001420 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001421}
1422
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001423void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001424 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001425}
1426
bsalomon@google.comc4364992011-11-07 15:54:49 +00001427bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1428 int left, int top,
1429 int width, int height,
1430 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001431 size_t rowBytes) const {
1432 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001433 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001434 return false;
1435 }
1436
1437 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001438 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001439 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001440 return true;
1441 }
1442 // If we have to do memcpys to handle rowBytes then y-flip is free
1443 // Note the rowBytes might be tight to the passed in data, but if data
1444 // gets clipped in x to the target the rowBytes will no longer be tight.
1445 if (left >= 0 && (left + width) < renderTarget->width()) {
1446 return 0 == rowBytes ||
1447 GrBytesPerPixel(config) * width == rowBytes;
1448 } else {
1449 return false;
1450 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001451}
1452
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001453bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001454 int left, int top,
1455 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001456 GrPixelConfig config,
1457 void* buffer,
1458 size_t rowBytes,
1459 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001460 GrGLenum format;
1461 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001462 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001464 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001465 size_t bpp = GrBytesPerPixel(config);
1466 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1467 &left, &top, &width, &height,
1468 const_cast<const void**>(&buffer),
1469 &rowBytes)) {
1470 return false;
1471 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001472
bsalomon@google.comc6980972011-11-02 19:57:21 +00001473 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001474 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001475 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001476 switch (tgt->getResolveType()) {
1477 case GrGLRenderTarget::kCantResolve_ResolveType:
1478 return false;
1479 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001480 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001481 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001482 break;
1483 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001484 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001485 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001486 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1487 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001488 break;
1489 default:
1490 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 }
1492
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001493 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001494
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001495 // the read rect is viewport-relative
1496 GrGLIRect readRect;
1497 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001499 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001500 if (0 == rowBytes) {
1501 rowBytes = tightRowBytes;
1502 }
1503 size_t readDstRowBytes = tightRowBytes;
1504 void* readDst = buffer;
1505
1506 // determine if GL can read using the passed rowBytes or if we need
1507 // a scratch buffer.
1508 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1509 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001510 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511 GrAssert(!(rowBytes % sizeof(GrColor)));
1512 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1513 readDstRowBytes = rowBytes;
1514 } else {
1515 scratch.reset(tightRowBytes * height);
1516 readDst = scratch.get();
1517 }
1518 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001519 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001520 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1521 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001522 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1523 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001524 format, type, readDst));
1525 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001526 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001527 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1528 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001529 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001530 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1531 invertY = true;
1532 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001533
1534 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001535 // API presents top-to-bottom. We must preserve the padding contents. Note
1536 // that the above readPixels did not overwrite the padding.
1537 if (readDst == buffer) {
1538 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001539 if (!invertY) {
1540 scratch.reset(tightRowBytes);
1541 void* tmpRow = scratch.get();
1542 // flip y in-place by rows
1543 const int halfY = height >> 1;
1544 char* top = reinterpret_cast<char*>(buffer);
1545 char* bottom = top + (height - 1) * rowBytes;
1546 for (int y = 0; y < halfY; y++) {
1547 memcpy(tmpRow, top, tightRowBytes);
1548 memcpy(top, bottom, tightRowBytes);
1549 memcpy(bottom, tmpRow, tightRowBytes);
1550 top += rowBytes;
1551 bottom -= rowBytes;
1552 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001553 }
1554 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001555 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001556 // copy from readDst to buffer while flipping y
1557 const int halfY = height >> 1;
1558 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001559 char* dst = reinterpret_cast<char*>(buffer);
1560 if (!invertY) {
1561 dst += (height-1) * rowBytes;
1562 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001563 for (int y = 0; y < height; y++) {
1564 memcpy(dst, src, tightRowBytes);
1565 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001566 if (invertY) {
1567 dst += rowBytes;
1568 } else {
1569 dst -= rowBytes;
1570 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001571 }
1572 }
1573 return true;
1574}
1575
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001576void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001577
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001578 GrGLRenderTarget* rt =
1579 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1580 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001581
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001582 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001583 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001584 #if GR_COLLECT_STATS
1585 ++fStats.fRenderTargetChngCnt;
1586 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001587 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001588 GrGLenum status;
1589 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001590 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001591 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001592 }
1593 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001594 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001595 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001596 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001597 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001598 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001599 fHWBounds.fViewportRect = vp;
1600 }
1601 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001602 if (NULL == bound || !bound->isEmpty()) {
1603 rt->flagAsNeedingResolve(bound);
1604 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001605}
1606
twiz@google.com0f31ca72011-03-18 17:38:11 +00001607GrGLenum gPrimitiveType2GLMode[] = {
1608 GR_GL_TRIANGLES,
1609 GR_GL_TRIANGLE_STRIP,
1610 GR_GL_TRIANGLE_FAN,
1611 GR_GL_POINTS,
1612 GR_GL_LINES,
1613 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001614};
1615
bsalomon@google.comd302f142011-03-03 13:54:13 +00001616#define SWAP_PER_DRAW 0
1617
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001618#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001619 #if GR_MAC_BUILD
1620 #include <AGL/agl.h>
1621 #elif GR_WIN32_BUILD
1622 void SwapBuf() {
1623 DWORD procID = GetCurrentProcessId();
1624 HWND hwnd = GetTopWindow(GetDesktopWindow());
1625 while(hwnd) {
1626 DWORD wndProcID = 0;
1627 GetWindowThreadProcessId(hwnd, &wndProcID);
1628 if(wndProcID == procID) {
1629 SwapBuffers(GetDC(hwnd));
1630 }
1631 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1632 }
1633 }
1634 #endif
1635#endif
1636
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001637void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1638 uint32_t startVertex,
1639 uint32_t startIndex,
1640 uint32_t vertexCount,
1641 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1643
twiz@google.com0f31ca72011-03-18 17:38:11 +00001644 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001645
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001646 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1647 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1648
1649 // our setupGeometry better have adjusted this to zero since
1650 // DrawElements always draws from the begining of the arrays for idx 0.
1651 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001652
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001653 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1654 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001655#if SWAP_PER_DRAW
1656 glFlush();
1657 #if GR_MAC_BUILD
1658 aglSwapBuffers(aglGetCurrentContext());
1659 int set_a_break_pt_here = 9;
1660 aglSwapBuffers(aglGetCurrentContext());
1661 #elif GR_WIN32_BUILD
1662 SwapBuf();
1663 int set_a_break_pt_here = 9;
1664 SwapBuf();
1665 #endif
1666#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001667}
1668
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001669void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1670 uint32_t startVertex,
1671 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001672 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1673
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001674 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1675
1676 // our setupGeometry better have adjusted this to zero.
1677 // DrawElements doesn't take an offset so we always adjus the startVertex.
1678 GrAssert(0 == startVertex);
1679
1680 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1681 // account for startVertex in the DrawElements case. So we always
1682 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001683 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001684#if SWAP_PER_DRAW
1685 glFlush();
1686 #if GR_MAC_BUILD
1687 aglSwapBuffers(aglGetCurrentContext());
1688 int set_a_break_pt_here = 9;
1689 aglSwapBuffers(aglGetCurrentContext());
1690 #elif GR_WIN32_BUILD
1691 SwapBuf();
1692 int set_a_break_pt_here = 9;
1693 SwapBuf();
1694 #endif
1695#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001696}
1697
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001698void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1699
1700 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001701
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001702 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001703 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001704 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001705 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1706 rt->renderFBOID()));
1707 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1708 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001709 #if GR_COLLECT_STATS
1710 ++fStats.fRenderTargetChngCnt;
1711 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001712 // make sure we go through flushRenderTarget() since we've modified
1713 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001714 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001715 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001716 const GrIRect dirtyRect = rt->getResolveRect();
1717 GrGLIRect r;
1718 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1719 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001720
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001721 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001722 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001723#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001724 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1725 GL_CALL(Scissor(r.fLeft, r.fBottom,
1726 r.fWidth, r.fHeight));
1727 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001728 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001729 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001730#else
1731 this->enableScissoring(dirtyRect);
1732 GL_CALL(ResolveMultisampleFramebuffer());
1733#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001735 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001736 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001737 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1738 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001739 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001740 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001741 int right = r.fLeft + r.fWidth;
1742 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001743 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1744 r.fLeft, r.fBottom, right, top,
1745 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001746 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001747 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001748 }
1749}
1750
twiz@google.com0f31ca72011-03-18 17:38:11 +00001751static const GrGLenum grToGLStencilFunc[] = {
1752 GR_GL_ALWAYS, // kAlways_StencilFunc
1753 GR_GL_NEVER, // kNever_StencilFunc
1754 GR_GL_GREATER, // kGreater_StencilFunc
1755 GR_GL_GEQUAL, // kGEqual_StencilFunc
1756 GR_GL_LESS, // kLess_StencilFunc
1757 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1758 GR_GL_EQUAL, // kEqual_StencilFunc,
1759 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001760};
1761GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1762GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1763GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1764GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1765GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1766GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1767GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1768GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1769GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1770
twiz@google.com0f31ca72011-03-18 17:38:11 +00001771static const GrGLenum grToGLStencilOp[] = {
1772 GR_GL_KEEP, // kKeep_StencilOp
1773 GR_GL_REPLACE, // kReplace_StencilOp
1774 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1775 GR_GL_INCR, // kIncClamp_StencilOp
1776 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1777 GR_GL_DECR, // kDecClamp_StencilOp
1778 GR_GL_ZERO, // kZero_StencilOp
1779 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001780};
1781GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1782GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1783GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1784GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1785GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1786GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1787GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1788GR_STATIC_ASSERT(6 == kZero_StencilOp);
1789GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1790
reed@google.comac10a2d2010-12-22 21:39:39 +00001791void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001792 const GrDrawState& drawState = this->getDrawState();
1793
1794 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001795
1796 // use stencil for clipping if clipping is enabled and the clip
1797 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001798 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001799 bool drawClipToStencil =
1800 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001801 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1802 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001803 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1804 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001805
1806 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001807
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001808 // we can't simultaneously perform stencil-clipping and
1809 // modify the stencil clip
1810 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001811
bsalomon@google.comd302f142011-03-03 13:54:13 +00001812 if (settings->isDisabled()) {
1813 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001814 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001815 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001816 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001817
1818 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001819 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001820 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001821 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001823 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001824 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1825 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1826 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1827 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1828 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1829 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1830 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1831 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001832 }
1833 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001834 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001835 GrStencilBuffer* stencilBuffer =
1836 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001837 if (NULL != stencilBuffer) {
1838 stencilBits = stencilBuffer->bits();
1839 }
1840 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001841 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001842
1843 GrGLuint clipStencilMask = 0;
1844 GrGLuint userStencilMask = ~0;
1845 if (stencilBits > 0) {
1846 clipStencilMask = 1 << (stencilBits - 1);
1847 userStencilMask = clipStencilMask - 1;
1848 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001849
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001850 unsigned int frontRef = settings->frontFuncRef();
1851 unsigned int frontMask = settings->frontFuncMask();
1852 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001853 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001854
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001855 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001856 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1857 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001859 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001860 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001862 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863 stencilClip,
1864 clipStencilMask,
1865 userStencilMask,
1866 &frontRef,
1867 &frontMask);
1868 frontWriteMask &= userStencilMask;
1869 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001870 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001871 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001872 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001873 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001874 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001875 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001876 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001877 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001878 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001879 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001880
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001881 unsigned int backRef = settings->backFuncRef();
1882 unsigned int backMask = settings->backFuncMask();
1883 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001884
1885
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001886 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001887 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1888 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001889 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001890 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001891 stencilClip, settings->backFunc())];
1892 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001893 stencilClip,
1894 clipStencilMask,
1895 userStencilMask,
1896 &backRef,
1897 &backMask);
1898 backWriteMask &= userStencilMask;
1899 }
1900
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001901 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1902 frontRef, frontMask));
1903 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1904 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1905 backRef, backMask));
1906 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1907 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001908 grToGLStencilOp[settings->frontFailOp()],
1909 grToGLStencilOp[settings->frontPassOp()],
1910 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001911
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001912 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001913 grToGLStencilOp[settings->backFailOp()],
1914 grToGLStencilOp[settings->backPassOp()],
1915 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001916 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001917 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1918 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001919 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1920 grToGLStencilOp[settings->frontPassOp()],
1921 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922 }
1923 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001924 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001925 fHWStencilClip = stencilClip;
1926 }
1927}
1928
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001929void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001930 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001931 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001932 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1933 // smooth lines.
1934
1935 // we prefer smooth lines over multisampled lines
1936 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001937 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001938 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001939 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001940 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001941 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001942 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001943 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001944 fHWAAState.fSmoothLineEnabled = false;
1945 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001946 if (rt->isMultisampled() &&
1947 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001948 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001949 fHWAAState.fMSAAEnabled = false;
1950 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001951 } else if (rt->isMultisampled() &&
1952 this->getDrawState().isHWAntialiasState() !=
1953 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001954 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001955 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956 fHWAAState.fMSAAEnabled = false;
1957 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 fHWAAState.fMSAAEnabled = true;
1960 }
1961 }
1962 }
1963}
1964
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001965void GrGpuGL::flushBlend(GrPrimitiveType type,
1966 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001967 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001968 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001969 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001970 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001971 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001972 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001973 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
1974 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001975 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1976 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001977 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
1978 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001979 }
1980 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001981 // any optimization to disable blending should
1982 // have already been applied and tweaked the coeffs
1983 // to (1, 0).
1984 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1985 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001986 if (blendOff) {
1987 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001988 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001989 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001990 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001991 } else {
1992 if (kYes_TriState != fHWBlendState.fEnabled) {
1993 GL_CALL(Enable(GR_GL_BLEND));
1994 fHWBlendState.fEnabled = kYes_TriState;
1995 }
1996 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1997 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001998 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1999 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002000 fHWBlendState.fSrcCoeff = srcCoeff;
2001 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002002 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002003 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002004 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2005 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002006 (!fHWBlendState.fConstColorValid ||
2007 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002008
2009 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002010 GrColorUnpackR(blendConst) / 255.f,
2011 GrColorUnpackG(blendConst) / 255.f,
2012 GrColorUnpackB(blendConst) / 255.f,
2013 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002014 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002015 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002016 fHWBlendState.fConstColor = blendConst;
2017 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002018 }
2019 }
2020 }
2021}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002022namespace {
2023
2024unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002025 switch (filter) {
2026 case GrSamplerState::kBilinear_Filter:
2027 case GrSamplerState::k4x4Downsample_Filter:
2028 return GR_GL_LINEAR;
2029 case GrSamplerState::kNearest_Filter:
2030 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002031 case GrSamplerState::kErode_Filter:
2032 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002033 return GR_GL_NEAREST;
2034 default:
2035 GrAssert(!"Unknown filter type");
2036 return GR_GL_LINEAR;
2037 }
2038}
2039
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002040// get_swizzle is only called from this .cpp so it is OK to inline it here
2041inline const GrGLenum* get_swizzle(GrPixelConfig config,
2042 const GrSamplerState& sampler,
2043 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002044 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002045 if (glCaps.textureRedSupport()) {
2046 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2047 GR_GL_RED, GR_GL_RED };
2048 return gRedSmear;
2049 } else {
2050 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2051 GR_GL_ALPHA, GR_GL_ALPHA };
2052 return gAlphaSmear;
2053 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002054 } else if (sampler.swapsRAndB()) {
2055 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2056 GR_GL_RED, GR_GL_ALPHA };
2057 return gRedBlueSwap;
2058 } else {
2059 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2060 GR_GL_BLUE, GR_GL_ALPHA };
2061 return gStraight;
2062 }
2063}
2064
2065void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2066 // should add texparameteri to interface to make 1 instead of 4 calls here
2067 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2068 GR_GL_TEXTURE_SWIZZLE_R,
2069 swizzle[0]));
2070 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2071 GR_GL_TEXTURE_SWIZZLE_G,
2072 swizzle[1]));
2073 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2074 GR_GL_TEXTURE_SWIZZLE_B,
2075 swizzle[2]));
2076 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2077 GR_GL_TEXTURE_SWIZZLE_A,
2078 swizzle[3]));
2079}
2080}
2081
bsalomon@google.comffca4002011-02-22 20:34:01 +00002082bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002083
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002084 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002085 // GrGpu::setupClipAndFlushState should have already checked this
2086 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002087 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002088
tomhudson@google.com93813632011-10-27 20:21:16 +00002089 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002090 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002091 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002092 GrGLTexture* nextTexture =
2093 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002094
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002095 // true for now, but maybe not with GrEffect.
2096 GrAssert(NULL != nextTexture);
2097 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002098 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002099 // the last bound texture, but it needs resolving. So keep this
2100 // out of the "last != next" check.
2101 GrGLRenderTarget* texRT =
2102 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2103 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002104 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002105 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002106
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002107 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002108 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002109 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002110 #if GR_COLLECT_STATS
2111 ++fStats.fTextureChngCnt;
2112 #endif
2113 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002114 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002115 // The texture matrix has to compensate for texture width/height
2116 // and NPOT-embedded-in-POT
2117 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002118 }
2119
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002120 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002121 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002122 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002123 nextTexture->getCachedTexParams(&timestamp);
2124 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002125 GrGLTexture::TexParams newTexParams;
2126
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002127 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002128
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002129 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002130 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2131 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002132 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002133 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002134 sizeof(newTexParams.fSwizzleRGBA));
2135 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002136 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002137 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002138 GR_GL_TEXTURE_MAG_FILTER,
2139 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002140 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002141 GR_GL_TEXTURE_MIN_FILTER,
2142 newTexParams.fFilter));
2143 }
2144 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2145 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002146 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002147 GR_GL_TEXTURE_WRAP_S,
2148 newTexParams.fWrapS));
2149 }
2150 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2151 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002152 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002153 GR_GL_TEXTURE_WRAP_T,
2154 newTexParams.fWrapT));
2155 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002156 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002157 (setAll ||
2158 memcmp(newTexParams.fSwizzleRGBA,
2159 oldTexParams.fSwizzleRGBA,
2160 sizeof(newTexParams.fSwizzleRGBA)))) {
2161 setTextureUnit(s);
2162 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2163 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002164 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002165 nextTexture->setCachedTexParams(newTexParams,
2166 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002167 }
2168 }
2169
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002170 GrIRect* rect = NULL;
2171 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002172 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002173 fClip.hasConservativeBounds()) {
2174 fClip.getConservativeBounds().roundOut(&clipBounds);
2175 rect = &clipBounds;
2176 }
2177 this->flushRenderTarget(rect);
2178 this->flushAAState(type);
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002179
2180 if (drawState->isDitherState()) {
2181 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002182 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002183 fHWDitherEnabled = kYes_TriState;
2184 }
2185 } else {
2186 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002187 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002188 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002189 }
2190 }
2191
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002192 if (drawState->isColorWriteDisabled()) {
2193 if (kNo_TriState != fHWWriteToColor) {
2194 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2195 GR_GL_FALSE, GR_GL_FALSE));
2196 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002197 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002198 } else {
2199 if (kYes_TriState != fHWWriteToColor) {
2200 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2201 fHWWriteToColor = kYes_TriState;
2202 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002203 }
2204
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002205 if (fHWDrawFace != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002206 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002207 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002208 GL_CALL(Enable(GR_GL_CULL_FACE));
2209 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002210 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002211 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002212 GL_CALL(Enable(GR_GL_CULL_FACE));
2213 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002214 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002215 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002216 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 break;
2218 default:
2219 GrCrash("Unknown draw face.");
2220 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002221 fHWDrawFace = drawState->getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002222 }
2223
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002224#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002225 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002226 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002227 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002228 NULL == drawState->getRenderTarget() ||
2229 NULL == drawState->getTexture(s) ||
2230 drawState->getTexture(s)->asRenderTarget() !=
2231 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002232 }
2233#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002234
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002235 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002236
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002237 // This copy must happen after flushStencil() is called. flushStencil()
2238 // relies on detecting when the kModifyStencilClip_StateBit state has
2239 // changed since the last draw.
2240 fHWDrawState.copyStateFlags(*drawState);
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002241
2242 // TODO: may no longer need this
robertphillips@google.com1942c052012-05-03 17:58:27 +00002243 // only GrInOrderDrawBuffer ever needs to ref/unref the textures
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002244 fHWDrawState.disableBehavior(GrDrawState::kTexturesNeedRef_BehaviorBit);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002245 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002246}
2247
2248void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002249 if (fHWGeometryState.fVertexBuffer != buffer) {
2250 fHWGeometryState.fArrayPtrsDirty = true;
2251 fHWGeometryState.fVertexBuffer = buffer;
2252 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002253}
2254
2255void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002256 if (fHWGeometryState.fVertexBuffer == buffer) {
2257 // deleting bound buffer does implied bind to 0
2258 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002259 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002260 }
2261}
2262
2263void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002264 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002265}
2266
2267void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002268 if (fHWGeometryState.fIndexBuffer == buffer) {
2269 // deleting bound buffer does implied bind to 0
2270 fHWGeometryState.fIndexBuffer = NULL;
2271 }
2272}
2273
reed@google.comac10a2d2010-12-22 21:39:39 +00002274void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2275 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002276 GrDrawState* drawState = this->drawState();
2277 if (drawState->getRenderTarget() == renderTarget) {
2278 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002279 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002280 if (fHWDrawState.getRenderTarget() == renderTarget) {
2281 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002282 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002283}
2284
2285void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002286 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002287 GrDrawState* drawState = this->drawState();
2288 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002289 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002290 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002291 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002292 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002293 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002294 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002296}
2297
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002298bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2299 bool getSizedInternalFormat,
2300 GrGLenum* internalFormat,
2301 GrGLenum* externalFormat,
2302 GrGLenum* externalType) {
2303 GrGLenum dontCare;
2304 if (NULL == internalFormat) {
2305 internalFormat = &dontCare;
2306 }
2307 if (NULL == externalFormat) {
2308 externalFormat = &dontCare;
2309 }
2310 if (NULL == externalType) {
2311 externalType = &dontCare;
2312 }
2313
reed@google.comac10a2d2010-12-22 21:39:39 +00002314 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002315 case kRGBA_8888_PM_GrPixelConfig:
2316 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002317 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002318 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002319 if (getSizedInternalFormat) {
2320 *internalFormat = GR_GL_RGBA8;
2321 } else {
2322 *internalFormat = GR_GL_RGBA;
2323 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002324 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002325 break;
2326 case kBGRA_8888_PM_GrPixelConfig:
2327 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002328 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002329 return false;
2330 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002331 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002332 if (getSizedInternalFormat) {
2333 *internalFormat = GR_GL_BGRA8;
2334 } else {
2335 *internalFormat = GR_GL_BGRA;
2336 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002337 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002338 if (getSizedInternalFormat) {
2339 *internalFormat = GR_GL_RGBA8;
2340 } else {
2341 *internalFormat = GR_GL_RGBA;
2342 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002343 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002344 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002345 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002346 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002347 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002348 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002349 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002350 if (getSizedInternalFormat) {
2351 if (this->glBinding() == kDesktop_GrGLBinding) {
2352 return false;
2353 } else {
2354 *internalFormat = GR_GL_RGB565;
2355 }
2356 } else {
2357 *internalFormat = GR_GL_RGB;
2358 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002359 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002360 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002361 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002362 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002363 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002364 if (getSizedInternalFormat) {
2365 *internalFormat = GR_GL_RGBA4;
2366 } else {
2367 *internalFormat = GR_GL_RGBA;
2368 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002369 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002370 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002371 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002372 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002373 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002374 // glCompressedTexImage doesn't take external params
2375 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002376 // no sized/unsized internal format distinction here
2377 *internalFormat = GR_GL_PALETTE8_RGBA8;
2378 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002379 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002380 } else {
2381 return false;
2382 }
2383 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002384 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002385 if (this->glCaps().textureRedSupport()) {
2386 *internalFormat = GR_GL_RED;
2387 *externalFormat = GR_GL_RED;
2388 if (getSizedInternalFormat) {
2389 *internalFormat = GR_GL_R8;
2390 } else {
2391 *internalFormat = GR_GL_RED;
2392 }
2393 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002394 } else {
2395 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002396 *externalFormat = GR_GL_ALPHA;
2397 if (getSizedInternalFormat) {
2398 *internalFormat = GR_GL_ALPHA8;
2399 } else {
2400 *internalFormat = GR_GL_ALPHA;
2401 }
2402 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002403 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002404 break;
2405 default:
2406 return false;
2407 }
2408 return true;
2409}
2410
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002411void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002412 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002413 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002414 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002415 fActiveTextureUnitIdx = unit;
2416 }
2417}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002418
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002419void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002420 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002421 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002422 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2423 }
2424}
2425
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002426void GrGpuGL::resetDirtyFlags() {
2427 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2428}
2429
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002430void GrGpuGL::setBuffers(bool indexed,
2431 int* extraVertexOffset,
2432 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002433
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002435
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002436 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2437
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002438 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002439 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002440 case kBuffer_GeometrySrcType:
2441 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002442 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443 break;
2444 case kArray_GeometrySrcType:
2445 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002446 this->finalizeReservedVertices();
2447 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2448 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002449 break;
2450 default:
2451 vbuf = NULL; // suppress warning
2452 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002453 }
2454
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002455 GrAssert(NULL != vbuf);
2456 GrAssert(!vbuf->isLocked());
2457 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002458 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459 fHWGeometryState.fArrayPtrsDirty = true;
2460 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002461 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462
2463 if (indexed) {
2464 GrAssert(NULL != extraIndexOffset);
2465
2466 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002467 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002468 case kBuffer_GeometrySrcType:
2469 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002470 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002471 break;
2472 case kArray_GeometrySrcType:
2473 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002474 this->finalizeReservedIndices();
2475 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2476 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002477 break;
2478 default:
2479 ibuf = NULL; // suppress warning
2480 GrCrash("Unknown geometry src type!");
2481 }
2482
2483 GrAssert(NULL != ibuf);
2484 GrAssert(!ibuf->isLocked());
2485 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002486 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002487 fHWGeometryState.fIndexBuffer = ibuf;
2488 }
2489 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002490}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002491