blob: bc35f5004163ff63db3d14997bb0c1b044e48802 [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.comc811ea32012-05-21 15:33:09 +0000490 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000491 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000492
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000493 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000494 // set to true to force disableScissor to make a GL call.
495 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000496 this->disableScissor();
497
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000498 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000500 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000501 fHWStencilClip = false;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000502
503 // TODO: I believe this should actually go in GrGpu::onResetContext
504 // rather than here
505 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000506
507 fHWGeometryState.fIndexBuffer = NULL;
508 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000509
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000510 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000512 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000513
514 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000515
516 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000517 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000518 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
519 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000520 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000521 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
522 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000523 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000524 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
525 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000526 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000527 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
528 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000529}
530
bsalomon@google.come269f212011-11-07 13:29:52 +0000531GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000532 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000533 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000534 return NULL;
535 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000536
bsalomon@google.com99621082011-11-15 16:47:16 +0000537 glTexDesc.fWidth = desc.fWidth;
538 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000539 glTexDesc.fConfig = desc.fConfig;
540 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
541 glTexDesc.fOwnsID = false;
542 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
543
544 GrGLTexture* texture = NULL;
545 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
546 GrGLRenderTarget::Desc glRTDesc;
547 glRTDesc.fRTFBOID = 0;
548 glRTDesc.fTexFBOID = 0;
549 glRTDesc.fMSColorRenderbufferID = 0;
550 glRTDesc.fOwnIDs = true;
551 glRTDesc.fConfig = desc.fConfig;
552 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000553 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
554 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000555 glTexDesc.fTextureID,
556 &glRTDesc)) {
557 return NULL;
558 }
559 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
560 } else {
561 texture = new GrGLTexture(this, glTexDesc);
562 }
563 if (NULL == texture) {
564 return NULL;
565 }
566
567 this->setSpareTextureUnit();
568 return texture;
569}
570
571GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
572 GrGLRenderTarget::Desc glDesc;
573 glDesc.fConfig = desc.fConfig;
574 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
575 glDesc.fMSColorRenderbufferID = 0;
576 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
577 glDesc.fSampleCnt = desc.fSampleCnt;
578 glDesc.fOwnIDs = false;
579 GrGLIRect viewport;
580 viewport.fLeft = 0;
581 viewport.fBottom = 0;
582 viewport.fWidth = desc.fWidth;
583 viewport.fHeight = desc.fHeight;
584
585 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
586 if (desc.fStencilBits) {
587 GrGLStencilBuffer::Format format;
588 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
589 format.fPacked = false;
590 format.fStencilBits = desc.fStencilBits;
591 format.fTotalBits = desc.fStencilBits;
592 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
593 0,
594 desc.fWidth,
595 desc.fHeight,
596 desc.fSampleCnt,
597 format);
598 tgt->setStencilBuffer(sb);
599 sb->unref();
600 }
601 return tgt;
602}
603
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000604////////////////////////////////////////////////////////////////////////////////
605
bsalomon@google.com6f379512011-11-16 20:36:03 +0000606void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
607 int left, int top, int width, int height,
608 GrPixelConfig config, const void* buffer,
609 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000610 if (NULL == buffer) {
611 return;
612 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000613 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
614
bsalomon@google.com6f379512011-11-16 20:36:03 +0000615 this->setSpareTextureUnit();
616 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
617 GrGLTexture::Desc desc;
618 desc.fConfig = glTex->config();
619 desc.fWidth = glTex->width();
620 desc.fHeight = glTex->height();
621 desc.fOrientation = glTex->orientation();
622 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000623
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000624 this->uploadTexData(desc, false,
625 left, top, width, height,
626 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000627}
628
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000629namespace {
630bool adjust_pixel_ops_params(int surfaceWidth,
631 int surfaceHeight,
632 size_t bpp,
633 int* left, int* top, int* width, int* height,
634 const void** data,
635 size_t* rowBytes) {
636 if (!*rowBytes) {
637 *rowBytes = *width * bpp;
638 }
639
640 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
641 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
642
643 if (!subRect.intersect(bounds)) {
644 return false;
645 }
646 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
647 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
648
649 *left = subRect.fLeft;
650 *top = subRect.fTop;
651 *width = subRect.width();
652 *height = subRect.height();
653 return true;
654}
655}
656
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000657bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
658 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000659 int left, int top, int width, int height,
660 GrPixelConfig dataConfig,
661 const void* data,
662 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000663 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000664
665 size_t bpp = GrBytesPerPixel(dataConfig);
666 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
667 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000668 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000669 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000670 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000671
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000672 // in case we need a temporary, trimmed copy of the src pixels
673 SkAutoSMalloc<128 * 128> tempStorage;
674
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000675 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000676 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000677 if (useTexStorage) {
678 if (kDesktop_GrGLBinding == this->glBinding()) {
679 // 565 is not a sized internal format on desktop GL. So on desktop
680 // with 565 we always use an unsized internal format to let the
681 // system pick the best sized format to convert the 565 data to.
682 // Since glTexStorage only allows sized internal formats we will
683 // instead fallback to glTexImage2D.
684 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
685 } else {
686 // ES doesn't allow paletted textures to be used with tex storage
687 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
688 }
689 }
690
691 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000692 GrGLenum externalFormat;
693 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000694 // glTexStorage requires sized internal formats on both desktop and ES. ES
695 // glTexImage requires an unsized format.
696 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
697 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000698 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000699 }
700
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000701 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000702 // paletted textures cannot be updated
703 return false;
704 }
705
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000706 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000707 * check whether to allocate a temporary buffer for flipping y or
708 * because our srcData has extra bytes past each row. If so, we need
709 * to trim those off here, since GL ES may not let us specify
710 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000711 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000712 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000713 bool swFlipY = false;
714 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000715 if (NULL != data) {
716 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000717 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000718 glFlipY = true;
719 } else {
720 swFlipY = true;
721 }
722 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000723 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000724 // can't use this for flipping, only non-neg values allowed. :(
725 if (rowBytes != trimRowBytes) {
726 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
727 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
728 restoreGLRowLength = true;
729 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000730 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000731 if (trimRowBytes != rowBytes || swFlipY) {
732 // copy data into our new storage, skipping the trailing bytes
733 size_t trimSize = height * trimRowBytes;
734 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000735 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000736 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000737 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000738 char* dst = (char*)tempStorage.reset(trimSize);
739 for (int y = 0; y < height; y++) {
740 memcpy(dst, src, trimRowBytes);
741 if (swFlipY) {
742 src -= rowBytes;
743 } else {
744 src += rowBytes;
745 }
746 dst += trimRowBytes;
747 }
748 // now point data to our copied version
749 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000750 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000751 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000752 if (glFlipY) {
753 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
754 }
755 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000756 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000757 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000758 if (isNewTexture &&
759 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000760 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000761 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000762 if (useTexStorage) {
763 // We never resize or change formats of textures. We don't use
764 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000765 GL_ALLOC_CALL(this->glInterface(),
766 TexStorage2D(GR_GL_TEXTURE_2D,
767 1, // levels
768 internalFormat,
769 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000770 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000771 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
772 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
773 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000774 GL_ALLOC_CALL(this->glInterface(),
775 CompressedTexImage2D(GR_GL_TEXTURE_2D,
776 0, // level
777 internalFormat,
778 desc.fWidth, desc.fHeight,
779 0, // border
780 imageSize,
781 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000782 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000783 GL_ALLOC_CALL(this->glInterface(),
784 TexImage2D(GR_GL_TEXTURE_2D,
785 0, // level
786 internalFormat,
787 desc.fWidth, desc.fHeight,
788 0, // border
789 externalFormat, externalType,
790 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000791 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000792 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000793 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000794 if (error != GR_GL_NO_ERROR) {
795 succeeded = false;
796 } else {
797 // if we have data and we used TexStorage to create the texture, we
798 // now upload with TexSubImage.
799 if (NULL != data && useTexStorage) {
800 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
801 0, // level
802 left, top,
803 width, height,
804 externalFormat, externalType,
805 data));
806 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000807 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000808 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000809 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000810 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000811 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000812 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
813 0, // level
814 left, top,
815 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000816 externalFormat, externalType, data));
817 }
818
819 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000820 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000821 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000822 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000823 if (glFlipY) {
824 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
825 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000826 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000827}
828
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000829namespace {
830bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
831 int sampleCount,
832 GrGLenum format,
833 int width, int height) {
834 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
835 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
836 bool created = false;
837 if (GrGLCaps::kNVDesktop_CoverageAAType ==
838 ctxInfo.caps().coverageAAType()) {
839 const GrGLCaps::MSAACoverageMode& mode =
840 ctxInfo.caps().getMSAACoverageMode(sampleCount);
841 GL_ALLOC_CALL(ctxInfo.interface(),
842 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
843 mode.fCoverageSampleCnt,
844 mode.fColorSampleCnt,
845 format,
846 width, height));
847 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
848 }
849 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000850 // glRBMS will fail if requested samples is > max samples.
851 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000852 GL_ALLOC_CALL(ctxInfo.interface(),
853 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
854 sampleCount,
855 format,
856 width, height));
857 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
858 }
859 return created;
860}
861}
862
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000863bool GrGpuGL::createRenderTargetObjects(int width, int height,
864 GrGLuint texID,
865 GrGLRenderTarget::Desc* desc) {
866 desc->fMSColorRenderbufferID = 0;
867 desc->fRTFBOID = 0;
868 desc->fTexFBOID = 0;
869 desc->fOwnIDs = true;
870
871 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000872
bsalomon@google.comab15d612011-08-09 12:57:56 +0000873 GrGLenum msColorFormat = 0; // suppress warning
874
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000875 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876 if (!desc->fTexFBOID) {
877 goto FAILED;
878 }
879
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880
881 // If we are using multisampling we will create two FBOS. We render
882 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000883 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000884 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000885 goto FAILED;
886 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000887 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
888 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000889 if (!desc->fRTFBOID ||
890 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000891 !this->configToGLFormats(desc->fConfig,
892 // GLES requires sized internal formats
893 kES2_GrGLBinding == this->glBinding(),
894 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000895 goto FAILED;
896 }
897 } else {
898 desc->fRTFBOID = desc->fTexFBOID;
899 }
900
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000901 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000902 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000903 if (desc->fRTFBOID != desc->fTexFBOID) {
904 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000905 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000906 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000907 if (!renderbuffer_storage_msaa(fGLContextInfo,
908 desc->fSampleCnt,
909 msColorFormat,
910 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000911 goto FAILED;
912 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000913 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
914 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915 GR_GL_COLOR_ATTACHMENT0,
916 GR_GL_RENDERBUFFER,
917 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000918 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000919 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
920 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
921 goto FAILED;
922 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000923 fGLContextInfo.caps().markConfigAsValidColorAttachment(
924 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000925 }
926 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000929 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
930 GR_GL_COLOR_ATTACHMENT0,
931 GR_GL_TEXTURE_2D,
932 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000933 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000934 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
935 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
936 goto FAILED;
937 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000938 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 }
940
941 return true;
942
943FAILED:
944 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000945 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000946 }
947 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000948 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000949 }
950 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000951 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000952 }
953 return false;
954}
955
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000956// good to set a break-point here to know when createTexture fails
957static GrTexture* return_null_texture() {
958// GrAssert(!"null texture");
959 return NULL;
960}
961
962#if GR_DEBUG
963static size_t as_size_t(int x) {
964 return x;
965}
966#endif
967
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000968GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000969 const void* srcData,
970 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000971
972#if GR_COLLECT_STATS
973 ++fStats.fTextureCreateCnt;
974#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000975
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000976 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000977 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000978
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000979 // Attempt to catch un- or wrongly initialized sample counts;
980 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
981
bsalomon@google.com99621082011-11-15 16:47:16 +0000982 glTexDesc.fWidth = desc.fWidth;
983 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000984 glTexDesc.fConfig = desc.fConfig;
985 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000986
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000987 glRTDesc.fMSColorRenderbufferID = 0;
988 glRTDesc.fRTFBOID = 0;
989 glRTDesc.fTexFBOID = 0;
990 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000991 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000992
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000993 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000994
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000995 const Caps& caps = this->getCaps();
996
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000997 // We keep GrRenderTargets in GL's normal orientation so that they
998 // can be drawn to by the outside world without the client having
999 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001000 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001001 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001002
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001003 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001004 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001005 desc.fSampleCnt) {
1006 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001007 }
1008
reed@google.comac10a2d2010-12-22 21:39:39 +00001009 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001010 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1011 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001012 return return_null_texture();
1013 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001014 }
1015
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001017 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001018 // provides a hint about how this texture will be used
1019 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1020 GR_GL_TEXTURE_USAGE,
1021 GR_GL_FRAMEBUFFER_ATTACHMENT));
1022 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001023 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001024 return return_null_texture();
1025 }
1026
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001027 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001028 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001029
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001030 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1031 // drivers have a bug where an FBO won't be complete if it includes a
1032 // texture that is not mipmap complete (considering the filter in use).
1033 GrGLTexture::TexParams initialTexParams;
1034 // we only set a subset here so invalidate first
1035 initialTexParams.invalidate();
1036 initialTexParams.fFilter = GR_GL_NEAREST;
1037 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1038 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001039 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1040 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001041 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001042 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1043 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001044 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001045 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1046 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001047 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1049 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001050 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001051 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1052 glTexDesc.fWidth, glTexDesc.fHeight,
1053 desc.fConfig, srcData, rowBytes)) {
1054 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1055 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001056 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001057
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001058 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 if (renderTarget) {
1060#if GR_COLLECT_STATS
1061 ++fStats.fRenderTargetCreateCnt;
1062#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001063 // unbind the texture from the texture unit before binding it to the frame buffer
1064 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1065
bsalomon@google.com99621082011-11-15 16:47:16 +00001066 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1067 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001068 glTexDesc.fTextureID,
1069 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001070 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 return return_null_texture();
1072 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001073 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001075 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001076 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001077 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001078#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001079 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1080 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001081#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 return tex;
1083}
1084
1085namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001086
1087const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1088
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001089void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1090 GrGLuint rb,
1091 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001092 // we shouldn't ever know one size and not the other
1093 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1094 (kUnknownBitCount == format->fTotalBits));
1095 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001096 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1098 (GrGLint*)&format->fStencilBits);
1099 if (format->fPacked) {
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_DEPTH_SIZE,
1102 (GrGLint*)&format->fTotalBits);
1103 format->fTotalBits += format->fStencilBits;
1104 } else {
1105 format->fTotalBits = format->fStencilBits;
1106 }
1107 }
1108}
1109}
1110
1111bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1112 int width, int height) {
1113
1114 // All internally created RTs are also textures. We don't create
1115 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1116 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001117 GrAssert(width >= rt->width());
1118 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119
1120 int samples = rt->numSamples();
1121 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123 if (!sbID) {
1124 return false;
1125 }
1126
1127 GrGLStencilBuffer* sb = NULL;
1128
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001129 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 // we start with the last stencil format that succeeded in hopes
1133 // that we won't go through this loop more than once after the
1134 // first (painful) stencil creation.
1135 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001136 const GrGLCaps::StencilFormat& sFmt =
1137 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001138 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001139 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001140 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001141 bool created;
1142 if (samples > 0) {
1143 created = renderbuffer_storage_msaa(fGLContextInfo,
1144 samples,
1145 sFmt.fInternalFormat,
1146 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001148 GL_ALLOC_CALL(this->glInterface(),
1149 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001150 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001151 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001152 created =
1153 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001155 if (created) {
1156 // After sized formats we attempt an unsized format and take
1157 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001158 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001159 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001160 sb = new GrGLStencilBuffer(this, sbID, width, height,
1161 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1163 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001164 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 return true;
1167 }
1168 sb->abandon(); // otherwise we lose sbID
1169 sb->unref();
1170 }
1171 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001172 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001173 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174}
1175
1176bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1177 GrRenderTarget* rt) {
1178 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1179
1180 GrGLuint fbo = glrt->renderFBOID();
1181
1182 if (NULL == sb) {
1183 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001184 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 GR_GL_STENCIL_ATTACHMENT,
1186 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001187 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 GR_GL_DEPTH_ATTACHMENT,
1189 GR_GL_RENDERBUFFER, 0));
1190#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001191 GrGLenum status;
1192 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1194#endif
1195 }
1196 return true;
1197 } else {
1198 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1199 GrGLuint rb = glsb->renderbufferID();
1200
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001201 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1203 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 GR_GL_STENCIL_ATTACHMENT,
1205 GR_GL_RENDERBUFFER, rb));
1206 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001207 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 GR_GL_DEPTH_ATTACHMENT,
1209 GR_GL_RENDERBUFFER, rb));
1210 } else {
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, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001214 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001216 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001217 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001218 glsb->format())) {
1219 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1220 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001222 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001224 if (glsb->format().fPacked) {
1225 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1226 GR_GL_DEPTH_ATTACHMENT,
1227 GR_GL_RENDERBUFFER, 0));
1228 }
1229 return false;
1230 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001231 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001232 rt->config(),
1233 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001236 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001237 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001238}
1239
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001240////////////////////////////////////////////////////////////////////////////////
1241
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001242GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001243 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001244 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001245 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001246 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001247 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001248 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001250 GL_ALLOC_CALL(this->glInterface(),
1251 BufferData(GR_GL_ARRAY_BUFFER,
1252 size,
1253 NULL, // data ptr
1254 dynamic ? GR_GL_DYNAMIC_DRAW :
1255 GR_GL_STATIC_DRAW));
1256 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001257 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 // deleting bound buffer does implicit bind to 0
1259 fHWGeometryState.fVertexBuffer = NULL;
1260 return NULL;
1261 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001262 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 size, dynamic);
1264 fHWGeometryState.fVertexBuffer = vertexBuffer;
1265 return vertexBuffer;
1266 }
1267 return NULL;
1268}
1269
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001270GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001271 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001274 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001275 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001276 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001277 GL_ALLOC_CALL(this->glInterface(),
1278 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1279 size,
1280 NULL, // data ptr
1281 dynamic ? GR_GL_DYNAMIC_DRAW :
1282 GR_GL_STATIC_DRAW));
1283 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001284 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 // deleting bound buffer does implicit bind to 0
1286 fHWGeometryState.fIndexBuffer = NULL;
1287 return NULL;
1288 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001289 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 size, dynamic);
1291 fHWGeometryState.fIndexBuffer = indexBuffer;
1292 return indexBuffer;
1293 }
1294 return NULL;
1295}
1296
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001297void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001298 const GrDrawState& drawState = this->getDrawState();
1299 const GrGLRenderTarget* rt =
1300 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1301
1302 GrAssert(NULL != rt);
1303 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001304
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001305 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001306 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1307 rect.width(), rect.height());
1308 if (scissor.contains(vp)) {
1309 disableScissor();
1310 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 }
1312
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001313 if (fHWBounds.fScissorRect != scissor) {
1314 scissor.pushToGLScissor(this->glInterface());
1315 fHWBounds.fScissorRect = scissor;
1316 }
1317 if (!fHWBounds.fScissorEnabled) {
1318 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1319 fHWBounds.fScissorEnabled = true;
1320 }
1321}
1322
1323void GrGpuGL::disableScissor() {
1324 if (fHWBounds.fScissorEnabled) {
1325 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1326 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 }
1328}
1329
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001330void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001331 const GrDrawState& drawState = this->getDrawState();
1332 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001333 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001334 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001335
bsalomon@google.com74b98712011-11-11 19:46:16 +00001336 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001337 if (NULL != rect) {
1338 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001339 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001340 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001341 if (clippedRect.intersect(rtRect)) {
1342 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001343 } else {
1344 return;
1345 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001347 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001348 if (NULL != rect)
1349 this->enableScissoring(*rect);
1350 else
1351 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001352
1353 GrGLfloat r, g, b, a;
1354 static const GrGLfloat scale255 = 1.f / 255.f;
1355 a = GrColorUnpackA(color) * scale255;
1356 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001357 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001358 scaleRGB *= a;
1359 }
1360 r = GrColorUnpackR(color) * scaleRGB;
1361 g = GrColorUnpackG(color) * scaleRGB;
1362 b = GrColorUnpackB(color) * scaleRGB;
1363
1364 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001365 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001366 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001367 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001368}
1369
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001370void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001371 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001372 return;
1373 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001374
1375 this->flushRenderTarget(&GrIRect::EmptyIRect());
1376
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001377 this->disableScissor();
1378
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001379 GL_CALL(StencilMask(0xffffffff));
1380 GL_CALL(ClearStencil(0));
1381 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001382 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001383}
1384
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001385void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001386 const GrDrawState& drawState = this->getDrawState();
1387 const GrRenderTarget* rt = drawState.getRenderTarget();
1388 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001389
1390 // this should only be called internally when we know we have a
1391 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001392 GrAssert(NULL != rt->getStencilBuffer());
1393 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001394#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001395 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001396 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001397#else
1398 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001399 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001400 // turned into draws. Our contract on GrDrawTarget says that
1401 // changing the clip between stencil passes may or may not
1402 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001403 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001404#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001405 GrGLint value;
1406 if (insideClip) {
1407 value = (1 << (stencilBitCount - 1));
1408 } else {
1409 value = 0;
1410 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001411 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001412 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001413 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001414 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001415 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001416 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001417}
1418
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001419void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001420 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001421}
1422
bsalomon@google.comc4364992011-11-07 15:54:49 +00001423bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1424 int left, int top,
1425 int width, int height,
1426 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001427 size_t rowBytes) const {
1428 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001429 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001430 return false;
1431 }
1432
1433 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001434 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001435 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001436 return true;
1437 }
1438 // If we have to do memcpys to handle rowBytes then y-flip is free
1439 // Note the rowBytes might be tight to the passed in data, but if data
1440 // gets clipped in x to the target the rowBytes will no longer be tight.
1441 if (left >= 0 && (left + width) < renderTarget->width()) {
1442 return 0 == rowBytes ||
1443 GrBytesPerPixel(config) * width == rowBytes;
1444 } else {
1445 return false;
1446 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001447}
1448
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001449bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001450 int left, int top,
1451 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001452 GrPixelConfig config,
1453 void* buffer,
1454 size_t rowBytes,
1455 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001456 GrGLenum format;
1457 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001458 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001460 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001461 size_t bpp = GrBytesPerPixel(config);
1462 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1463 &left, &top, &width, &height,
1464 const_cast<const void**>(&buffer),
1465 &rowBytes)) {
1466 return false;
1467 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001468
bsalomon@google.comc6980972011-11-02 19:57:21 +00001469 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001470 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001471 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001472 switch (tgt->getResolveType()) {
1473 case GrGLRenderTarget::kCantResolve_ResolveType:
1474 return false;
1475 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001476 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001477 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001478 break;
1479 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001480 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001481 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001482 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1483 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001484 break;
1485 default:
1486 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 }
1488
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001489 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001490
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001491 // the read rect is viewport-relative
1492 GrGLIRect readRect;
1493 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001494
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001495 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001496 if (0 == rowBytes) {
1497 rowBytes = tightRowBytes;
1498 }
1499 size_t readDstRowBytes = tightRowBytes;
1500 void* readDst = buffer;
1501
1502 // determine if GL can read using the passed rowBytes or if we need
1503 // a scratch buffer.
1504 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1505 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001506 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001507 GrAssert(!(rowBytes % sizeof(GrColor)));
1508 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1509 readDstRowBytes = rowBytes;
1510 } else {
1511 scratch.reset(tightRowBytes * height);
1512 readDst = scratch.get();
1513 }
1514 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001515 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001516 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1517 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001518 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1519 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001520 format, type, readDst));
1521 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001522 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001523 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1524 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001525 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001526 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1527 invertY = true;
1528 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001529
1530 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001531 // API presents top-to-bottom. We must preserve the padding contents. Note
1532 // that the above readPixels did not overwrite the padding.
1533 if (readDst == buffer) {
1534 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001535 if (!invertY) {
1536 scratch.reset(tightRowBytes);
1537 void* tmpRow = scratch.get();
1538 // flip y in-place by rows
1539 const int halfY = height >> 1;
1540 char* top = reinterpret_cast<char*>(buffer);
1541 char* bottom = top + (height - 1) * rowBytes;
1542 for (int y = 0; y < halfY; y++) {
1543 memcpy(tmpRow, top, tightRowBytes);
1544 memcpy(top, bottom, tightRowBytes);
1545 memcpy(bottom, tmpRow, tightRowBytes);
1546 top += rowBytes;
1547 bottom -= rowBytes;
1548 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001549 }
1550 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001551 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001552 // copy from readDst to buffer while flipping y
1553 const int halfY = height >> 1;
1554 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001555 char* dst = reinterpret_cast<char*>(buffer);
1556 if (!invertY) {
1557 dst += (height-1) * rowBytes;
1558 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001559 for (int y = 0; y < height; y++) {
1560 memcpy(dst, src, tightRowBytes);
1561 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001562 if (invertY) {
1563 dst += rowBytes;
1564 } else {
1565 dst -= rowBytes;
1566 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001567 }
1568 }
1569 return true;
1570}
1571
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001572void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001573
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001574 GrGLRenderTarget* rt =
1575 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1576 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001577
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001578 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001579 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001580 #if GR_COLLECT_STATS
1581 ++fStats.fRenderTargetChngCnt;
1582 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001583 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001584 GrGLenum status;
1585 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001586 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001587 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 }
1589 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001590 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001591 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001592 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001593 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001594 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001595 fHWBounds.fViewportRect = vp;
1596 }
1597 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001598 if (NULL == bound || !bound->isEmpty()) {
1599 rt->flagAsNeedingResolve(bound);
1600 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001601}
1602
twiz@google.com0f31ca72011-03-18 17:38:11 +00001603GrGLenum gPrimitiveType2GLMode[] = {
1604 GR_GL_TRIANGLES,
1605 GR_GL_TRIANGLE_STRIP,
1606 GR_GL_TRIANGLE_FAN,
1607 GR_GL_POINTS,
1608 GR_GL_LINES,
1609 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001610};
1611
bsalomon@google.comd302f142011-03-03 13:54:13 +00001612#define SWAP_PER_DRAW 0
1613
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001614#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001615 #if GR_MAC_BUILD
1616 #include <AGL/agl.h>
1617 #elif GR_WIN32_BUILD
1618 void SwapBuf() {
1619 DWORD procID = GetCurrentProcessId();
1620 HWND hwnd = GetTopWindow(GetDesktopWindow());
1621 while(hwnd) {
1622 DWORD wndProcID = 0;
1623 GetWindowThreadProcessId(hwnd, &wndProcID);
1624 if(wndProcID == procID) {
1625 SwapBuffers(GetDC(hwnd));
1626 }
1627 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1628 }
1629 }
1630 #endif
1631#endif
1632
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001633void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1634 uint32_t startVertex,
1635 uint32_t startIndex,
1636 uint32_t vertexCount,
1637 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001638 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1639
twiz@google.com0f31ca72011-03-18 17:38:11 +00001640 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001641
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001642 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1643 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1644
1645 // our setupGeometry better have adjusted this to zero since
1646 // DrawElements always draws from the begining of the arrays for idx 0.
1647 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001648
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001649 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1650 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001651#if SWAP_PER_DRAW
1652 glFlush();
1653 #if GR_MAC_BUILD
1654 aglSwapBuffers(aglGetCurrentContext());
1655 int set_a_break_pt_here = 9;
1656 aglSwapBuffers(aglGetCurrentContext());
1657 #elif GR_WIN32_BUILD
1658 SwapBuf();
1659 int set_a_break_pt_here = 9;
1660 SwapBuf();
1661 #endif
1662#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001663}
1664
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001665void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1666 uint32_t startVertex,
1667 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001668 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1669
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001670 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1671
1672 // our setupGeometry better have adjusted this to zero.
1673 // DrawElements doesn't take an offset so we always adjus the startVertex.
1674 GrAssert(0 == startVertex);
1675
1676 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1677 // account for startVertex in the DrawElements case. So we always
1678 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001679 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001680#if SWAP_PER_DRAW
1681 glFlush();
1682 #if GR_MAC_BUILD
1683 aglSwapBuffers(aglGetCurrentContext());
1684 int set_a_break_pt_here = 9;
1685 aglSwapBuffers(aglGetCurrentContext());
1686 #elif GR_WIN32_BUILD
1687 SwapBuf();
1688 int set_a_break_pt_here = 9;
1689 SwapBuf();
1690 #endif
1691#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001692}
1693
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001694void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1695
1696 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001697
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001698 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001699 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001700 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001701 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1702 rt->renderFBOID()));
1703 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1704 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001705 #if GR_COLLECT_STATS
1706 ++fStats.fRenderTargetChngCnt;
1707 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001708 // make sure we go through flushRenderTarget() since we've modified
1709 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001710 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001711 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001712 const GrIRect dirtyRect = rt->getResolveRect();
1713 GrGLIRect r;
1714 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1715 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001716
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001717 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001718 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001719#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001720 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1721 GL_CALL(Scissor(r.fLeft, r.fBottom,
1722 r.fWidth, r.fHeight));
1723 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001724 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001725 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001726#else
1727 this->enableScissoring(dirtyRect);
1728 GL_CALL(ResolveMultisampleFramebuffer());
1729#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001731 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001732 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001733 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1734 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001735 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001736 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001737 int right = r.fLeft + r.fWidth;
1738 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001739 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1740 r.fLeft, r.fBottom, right, top,
1741 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001743 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001744 }
1745}
1746
twiz@google.com0f31ca72011-03-18 17:38:11 +00001747static const GrGLenum grToGLStencilFunc[] = {
1748 GR_GL_ALWAYS, // kAlways_StencilFunc
1749 GR_GL_NEVER, // kNever_StencilFunc
1750 GR_GL_GREATER, // kGreater_StencilFunc
1751 GR_GL_GEQUAL, // kGEqual_StencilFunc
1752 GR_GL_LESS, // kLess_StencilFunc
1753 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1754 GR_GL_EQUAL, // kEqual_StencilFunc,
1755 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001756};
1757GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1758GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1759GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1760GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1761GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1762GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1763GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1764GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1765GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1766
twiz@google.com0f31ca72011-03-18 17:38:11 +00001767static const GrGLenum grToGLStencilOp[] = {
1768 GR_GL_KEEP, // kKeep_StencilOp
1769 GR_GL_REPLACE, // kReplace_StencilOp
1770 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1771 GR_GL_INCR, // kIncClamp_StencilOp
1772 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1773 GR_GL_DECR, // kDecClamp_StencilOp
1774 GR_GL_ZERO, // kZero_StencilOp
1775 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001776};
1777GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1778GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1779GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1780GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1781GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1782GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1783GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1784GR_STATIC_ASSERT(6 == kZero_StencilOp);
1785GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1786
reed@google.comac10a2d2010-12-22 21:39:39 +00001787void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001788 const GrDrawState& drawState = this->getDrawState();
1789
1790 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001791
1792 // use stencil for clipping if clipping is enabled and the clip
1793 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001794 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001795 bool drawClipToStencil =
1796 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001797 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1798 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001799 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1800 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001801
1802 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001803
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001804 // we can't simultaneously perform stencil-clipping and
1805 // modify the stencil clip
1806 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001807
bsalomon@google.comd302f142011-03-03 13:54:13 +00001808 if (settings->isDisabled()) {
1809 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001810 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001811 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001812 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001813
1814 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001815 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001816 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001817 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001818 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001819 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001820 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1821 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1822 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1823 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1824 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1825 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1826 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1827 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828 }
1829 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001830 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001831 GrStencilBuffer* stencilBuffer =
1832 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001833 if (NULL != stencilBuffer) {
1834 stencilBits = stencilBuffer->bits();
1835 }
1836 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001837 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001838
1839 GrGLuint clipStencilMask = 0;
1840 GrGLuint userStencilMask = ~0;
1841 if (stencilBits > 0) {
1842 clipStencilMask = 1 << (stencilBits - 1);
1843 userStencilMask = clipStencilMask - 1;
1844 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001845
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001846 unsigned int frontRef = settings->frontFuncRef();
1847 unsigned int frontMask = settings->frontFuncMask();
1848 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001849 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001850
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001851 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001852 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1853 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001854 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001855 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001856 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001857
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001858 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859 stencilClip,
1860 clipStencilMask,
1861 userStencilMask,
1862 &frontRef,
1863 &frontMask);
1864 frontWriteMask &= userStencilMask;
1865 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001866 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001867 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001868 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001869 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001870 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001871 settings->backFailOp() < 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->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001874 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001875 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001876
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001877 unsigned int backRef = settings->backFuncRef();
1878 unsigned int backMask = settings->backFuncMask();
1879 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001880
1881
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001882 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001883 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1884 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001885 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001886 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001887 stencilClip, settings->backFunc())];
1888 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001889 stencilClip,
1890 clipStencilMask,
1891 userStencilMask,
1892 &backRef,
1893 &backMask);
1894 backWriteMask &= userStencilMask;
1895 }
1896
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001897 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1898 frontRef, frontMask));
1899 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1900 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1901 backRef, backMask));
1902 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1903 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001904 grToGLStencilOp[settings->frontFailOp()],
1905 grToGLStencilOp[settings->frontPassOp()],
1906 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001907
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001908 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001909 grToGLStencilOp[settings->backFailOp()],
1910 grToGLStencilOp[settings->backPassOp()],
1911 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001912 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001913 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1914 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001915 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1916 grToGLStencilOp[settings->frontPassOp()],
1917 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001918 }
1919 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001920 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001921 fHWStencilClip = stencilClip;
1922 }
1923}
1924
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001925void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001926 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001927 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001928 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1929 // smooth lines.
1930
1931 // we prefer smooth lines over multisampled lines
1932 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001933 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001934 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001935 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001936 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001937 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001938 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001939 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001940 fHWAAState.fSmoothLineEnabled = false;
1941 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001942 if (rt->isMultisampled() &&
1943 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001944 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001945 fHWAAState.fMSAAEnabled = false;
1946 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001947 } else if (rt->isMultisampled() &&
1948 this->getDrawState().isHWAntialiasState() !=
1949 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001950 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001951 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001952 fHWAAState.fMSAAEnabled = false;
1953 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001954 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001955 fHWAAState.fMSAAEnabled = true;
1956 }
1957 }
1958 }
1959}
1960
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001961void GrGpuGL::flushBlend(GrPrimitiveType type,
1962 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001963 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001964 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001965 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001966 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001967 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001968 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001969 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
1970 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001971 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1972 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001973 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
1974 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001975 }
1976 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001977 // any optimization to disable blending should
1978 // have already been applied and tweaked the coeffs
1979 // to (1, 0).
1980 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1981 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001982 if (blendOff) {
1983 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001984 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001985 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001986 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001987 } else {
1988 if (kYes_TriState != fHWBlendState.fEnabled) {
1989 GL_CALL(Enable(GR_GL_BLEND));
1990 fHWBlendState.fEnabled = kYes_TriState;
1991 }
1992 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1993 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001994 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1995 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001996 fHWBlendState.fSrcCoeff = srcCoeff;
1997 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001998 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001999 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002000 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2001 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002002 (!fHWBlendState.fConstColorValid ||
2003 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002004
2005 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002006 GrColorUnpackR(blendConst) / 255.f,
2007 GrColorUnpackG(blendConst) / 255.f,
2008 GrColorUnpackB(blendConst) / 255.f,
2009 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002010 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002011 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002012 fHWBlendState.fConstColor = blendConst;
2013 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002014 }
2015 }
2016 }
2017}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002018namespace {
2019
2020unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002021 switch (filter) {
2022 case GrSamplerState::kBilinear_Filter:
2023 case GrSamplerState::k4x4Downsample_Filter:
2024 return GR_GL_LINEAR;
2025 case GrSamplerState::kNearest_Filter:
2026 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002027 case GrSamplerState::kErode_Filter:
2028 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002029 return GR_GL_NEAREST;
2030 default:
2031 GrAssert(!"Unknown filter type");
2032 return GR_GL_LINEAR;
2033 }
2034}
2035
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002036// get_swizzle is only called from this .cpp so it is OK to inline it here
2037inline const GrGLenum* get_swizzle(GrPixelConfig config,
2038 const GrSamplerState& sampler,
2039 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002040 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002041 if (glCaps.textureRedSupport()) {
2042 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2043 GR_GL_RED, GR_GL_RED };
2044 return gRedSmear;
2045 } else {
2046 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2047 GR_GL_ALPHA, GR_GL_ALPHA };
2048 return gAlphaSmear;
2049 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002050 } else if (sampler.swapsRAndB()) {
2051 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2052 GR_GL_RED, GR_GL_ALPHA };
2053 return gRedBlueSwap;
2054 } else {
2055 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2056 GR_GL_BLUE, GR_GL_ALPHA };
2057 return gStraight;
2058 }
2059}
2060
2061void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2062 // should add texparameteri to interface to make 1 instead of 4 calls here
2063 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2064 GR_GL_TEXTURE_SWIZZLE_R,
2065 swizzle[0]));
2066 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2067 GR_GL_TEXTURE_SWIZZLE_G,
2068 swizzle[1]));
2069 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2070 GR_GL_TEXTURE_SWIZZLE_B,
2071 swizzle[2]));
2072 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2073 GR_GL_TEXTURE_SWIZZLE_A,
2074 swizzle[3]));
2075}
2076}
2077
bsalomon@google.comffca4002011-02-22 20:34:01 +00002078bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002079
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002080 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002081 // GrGpu::setupClipAndFlushState should have already checked this
2082 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002083 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002084
tomhudson@google.com93813632011-10-27 20:21:16 +00002085 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002086 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002087 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002088 GrGLTexture* nextTexture =
2089 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002090
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002091 // true for now, but maybe not with GrEffect.
2092 GrAssert(NULL != nextTexture);
2093 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002094 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002095 // the last bound texture, but it needs resolving. So keep this
2096 // out of the "last != next" check.
2097 GrGLRenderTarget* texRT =
2098 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2099 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002100 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002101 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002102
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002103 if (fHWBoundTextures[s] != nextTexture) {
2104 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002105 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002106 #if GR_COLLECT_STATS
2107 ++fStats.fTextureChngCnt;
2108 #endif
2109 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002110 fHWBoundTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002111 // The texture matrix has to compensate for texture width/height
2112 // and NPOT-embedded-in-POT
2113 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002114 }
2115
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002116 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002117 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002118 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002119 nextTexture->getCachedTexParams(&timestamp);
2120 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002121 GrGLTexture::TexParams newTexParams;
2122
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002123 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002124
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002125 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002126 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2127 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002128 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002129 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002130 sizeof(newTexParams.fSwizzleRGBA));
2131 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002132 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002133 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002134 GR_GL_TEXTURE_MAG_FILTER,
2135 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002136 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002137 GR_GL_TEXTURE_MIN_FILTER,
2138 newTexParams.fFilter));
2139 }
2140 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002141 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002142 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002143 GR_GL_TEXTURE_WRAP_S,
2144 newTexParams.fWrapS));
2145 }
2146 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002147 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002148 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002149 GR_GL_TEXTURE_WRAP_T,
2150 newTexParams.fWrapT));
2151 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002152 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002153 (setAll ||
2154 memcmp(newTexParams.fSwizzleRGBA,
2155 oldTexParams.fSwizzleRGBA,
2156 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002157 this->setTextureUnit(s);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002158 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2159 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002160 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002161 nextTexture->setCachedTexParams(newTexParams,
2162 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002163 }
2164 }
2165
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002166 GrIRect* rect = NULL;
2167 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002168 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002169 fClip.hasConservativeBounds()) {
2170 fClip.getConservativeBounds().roundOut(&clipBounds);
2171 rect = &clipBounds;
2172 }
2173 this->flushRenderTarget(rect);
2174 this->flushAAState(type);
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002175
2176 if (drawState->isDitherState()) {
2177 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002178 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002179 fHWDitherEnabled = kYes_TriState;
2180 }
2181 } else {
2182 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002183 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002184 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002185 }
2186 }
2187
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002188 if (drawState->isColorWriteDisabled()) {
2189 if (kNo_TriState != fHWWriteToColor) {
2190 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2191 GR_GL_FALSE, GR_GL_FALSE));
2192 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002193 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002194 } else {
2195 if (kYes_TriState != fHWWriteToColor) {
2196 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2197 fHWWriteToColor = kYes_TriState;
2198 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002199 }
2200
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002201 if (fHWDrawFace != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002202 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002203 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002204 GL_CALL(Enable(GR_GL_CULL_FACE));
2205 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002206 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002207 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002208 GL_CALL(Enable(GR_GL_CULL_FACE));
2209 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002210 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002211 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002212 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002213 break;
2214 default:
2215 GrCrash("Unknown draw face.");
2216 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002217 fHWDrawFace = drawState->getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002218 }
2219
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002220#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002221 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002222 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002223 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002224 NULL == drawState->getRenderTarget() ||
2225 NULL == drawState->getTexture(s) ||
2226 drawState->getTexture(s)->asRenderTarget() !=
2227 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002228 }
2229#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002230
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002231 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002232
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002233 // This copy must happen after flushStencil() is called. flushStencil()
2234 // relies on detecting when the kModifyStencilClip_StateBit state has
2235 // changed since the last draw.
2236 fHWDrawState.copyStateFlags(*drawState);
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002237
2238 // TODO: may no longer need this
robertphillips@google.com1942c052012-05-03 17:58:27 +00002239 // only GrInOrderDrawBuffer ever needs to ref/unref the textures
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002240 fHWDrawState.disableBehavior(GrDrawState::kTexturesNeedRef_BehaviorBit);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002241 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002242}
2243
2244void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002245 if (fHWGeometryState.fVertexBuffer != buffer) {
2246 fHWGeometryState.fArrayPtrsDirty = true;
2247 fHWGeometryState.fVertexBuffer = buffer;
2248 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002249}
2250
2251void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002252 if (fHWGeometryState.fVertexBuffer == buffer) {
2253 // deleting bound buffer does implied bind to 0
2254 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002255 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002256 }
2257}
2258
2259void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002260 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002261}
2262
2263void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002264 if (fHWGeometryState.fIndexBuffer == buffer) {
2265 // deleting bound buffer does implied bind to 0
2266 fHWGeometryState.fIndexBuffer = NULL;
2267 }
2268}
2269
reed@google.comac10a2d2010-12-22 21:39:39 +00002270void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2271 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002272 GrDrawState* drawState = this->drawState();
2273 if (drawState->getRenderTarget() == renderTarget) {
2274 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002275 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002276 if (fHWBoundRenderTarget == renderTarget) {
2277 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002278 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002279}
2280
2281void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002282 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002283 GrDrawState* drawState = this->drawState();
2284 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002285 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002286 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002287 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002288 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002289 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002290 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002291 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002292}
2293
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002294bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2295 bool getSizedInternalFormat,
2296 GrGLenum* internalFormat,
2297 GrGLenum* externalFormat,
2298 GrGLenum* externalType) {
2299 GrGLenum dontCare;
2300 if (NULL == internalFormat) {
2301 internalFormat = &dontCare;
2302 }
2303 if (NULL == externalFormat) {
2304 externalFormat = &dontCare;
2305 }
2306 if (NULL == externalType) {
2307 externalType = &dontCare;
2308 }
2309
reed@google.comac10a2d2010-12-22 21:39:39 +00002310 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002311 case kRGBA_8888_PM_GrPixelConfig:
2312 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002313 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002314 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002315 if (getSizedInternalFormat) {
2316 *internalFormat = GR_GL_RGBA8;
2317 } else {
2318 *internalFormat = GR_GL_RGBA;
2319 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002320 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002321 break;
2322 case kBGRA_8888_PM_GrPixelConfig:
2323 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002324 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002325 return false;
2326 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002327 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002328 if (getSizedInternalFormat) {
2329 *internalFormat = GR_GL_BGRA8;
2330 } else {
2331 *internalFormat = GR_GL_BGRA;
2332 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002333 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002334 if (getSizedInternalFormat) {
2335 *internalFormat = GR_GL_RGBA8;
2336 } else {
2337 *internalFormat = GR_GL_RGBA;
2338 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002339 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002340 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002341 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002342 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002343 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002344 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002345 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002346 if (getSizedInternalFormat) {
2347 if (this->glBinding() == kDesktop_GrGLBinding) {
2348 return false;
2349 } else {
2350 *internalFormat = GR_GL_RGB565;
2351 }
2352 } else {
2353 *internalFormat = GR_GL_RGB;
2354 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002355 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002356 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002357 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002358 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002359 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002360 if (getSizedInternalFormat) {
2361 *internalFormat = GR_GL_RGBA4;
2362 } else {
2363 *internalFormat = GR_GL_RGBA;
2364 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002365 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002366 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002367 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002368 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002369 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002370 // glCompressedTexImage doesn't take external params
2371 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002372 // no sized/unsized internal format distinction here
2373 *internalFormat = GR_GL_PALETTE8_RGBA8;
2374 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002375 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002376 } else {
2377 return false;
2378 }
2379 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002380 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002381 if (this->glCaps().textureRedSupport()) {
2382 *internalFormat = GR_GL_RED;
2383 *externalFormat = GR_GL_RED;
2384 if (getSizedInternalFormat) {
2385 *internalFormat = GR_GL_R8;
2386 } else {
2387 *internalFormat = GR_GL_RED;
2388 }
2389 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002390 } else {
2391 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002392 *externalFormat = GR_GL_ALPHA;
2393 if (getSizedInternalFormat) {
2394 *internalFormat = GR_GL_ALPHA8;
2395 } else {
2396 *internalFormat = GR_GL_ALPHA;
2397 }
2398 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002399 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002400 break;
2401 default:
2402 return false;
2403 }
2404 return true;
2405}
2406
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002407void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002408 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002409 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002410 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002411 fActiveTextureUnitIdx = unit;
2412 }
2413}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002414
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002415void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002416 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002417 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002418 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2419 }
2420}
2421
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002422void GrGpuGL::resetDirtyFlags() {
2423 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2424}
2425
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002426void GrGpuGL::setBuffers(bool indexed,
2427 int* extraVertexOffset,
2428 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002429
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002430 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002431
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002432 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2433
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002435 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002436 case kBuffer_GeometrySrcType:
2437 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002438 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439 break;
2440 case kArray_GeometrySrcType:
2441 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002442 this->finalizeReservedVertices();
2443 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2444 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 break;
2446 default:
2447 vbuf = NULL; // suppress warning
2448 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002449 }
2450
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002451 GrAssert(NULL != vbuf);
2452 GrAssert(!vbuf->isLocked());
2453 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002454 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002455 fHWGeometryState.fArrayPtrsDirty = true;
2456 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002457 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002458
2459 if (indexed) {
2460 GrAssert(NULL != extraIndexOffset);
2461
2462 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002463 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002464 case kBuffer_GeometrySrcType:
2465 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002466 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002467 break;
2468 case kArray_GeometrySrcType:
2469 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002470 this->finalizeReservedIndices();
2471 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2472 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002473 break;
2474 default:
2475 ibuf = NULL; // suppress warning
2476 GrCrash("Unknown geometry src type!");
2477 }
2478
2479 GrAssert(NULL != ibuf);
2480 GrAssert(!ibuf->isLocked());
2481 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002482 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002483 fHWGeometryState.fIndexBuffer = ibuf;
2484 }
2485 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002486}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002487