blob: 341675bc185f29fcd90fa0aac447661aeb2e9041 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000011#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000012#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
twiz@google.com0f31ca72011-03-18 17:38:11 +000014static const GrGLuint GR_MAX_GLUINT = ~0;
15static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com0b77d682011-08-19 13:28:54 +000017#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000018#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019
bsalomon@google.com316f99232011-01-13 21:28:12 +000020// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000022static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000026#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
27 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
30#else
31 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
34#endif
35
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000036
37///////////////////////////////////////////////////////////////////////////////
38
twiz@google.com0f31ca72011-03-18 17:38:11 +000039static const GrGLenum gXfermodeCoeff2Blend[] = {
40 GR_GL_ZERO,
41 GR_GL_ONE,
42 GR_GL_SRC_COLOR,
43 GR_GL_ONE_MINUS_SRC_COLOR,
44 GR_GL_DST_COLOR,
45 GR_GL_ONE_MINUS_DST_COLOR,
46 GR_GL_SRC_ALPHA,
47 GR_GL_ONE_MINUS_SRC_ALPHA,
48 GR_GL_DST_ALPHA,
49 GR_GL_ONE_MINUS_DST_ALPHA,
50 GR_GL_CONSTANT_COLOR,
51 GR_GL_ONE_MINUS_CONSTANT_COLOR,
52 GR_GL_CONSTANT_ALPHA,
53 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000054
55 // extended blend coeffs
56 GR_GL_SRC1_COLOR,
57 GR_GL_ONE_MINUS_SRC1_COLOR,
58 GR_GL_SRC1_ALPHA,
59 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000060};
61
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000063 static const bool gCoeffReferencesBlendConst[] = {
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 true,
75 true,
76 true,
77 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000078
79 // extended blend coeffs
80 false,
81 false,
82 false,
83 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000084 };
85 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000086 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
87
88 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
89 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
90 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
91 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
92 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
93 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
94 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
95 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
96 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
97 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
98 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
99 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
100 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
101 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
102
103 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
104 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
105 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
106 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
107
108 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
109 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000110}
111
reed@google.comac10a2d2010-12-22 21:39:39 +0000112///////////////////////////////////////////////////////////////////////////////
113
bsalomon@google.comd302f142011-03-03 13:54:13 +0000114void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
115 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000116 GrMatrix* matrix) {
117 GrAssert(NULL != texture);
118 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000119 GrGLTexture::Orientation orientation = texture->orientation();
120 if (GrGLTexture::kBottomUp_Orientation == orientation) {
121 GrMatrix invY;
122 invY.setAll(GR_Scalar1, 0, 0,
123 0, -GR_Scalar1, GR_Scalar1,
124 0, 0, GrMatrix::I()[8]);
125 matrix->postConcat(invY);
126 } else {
127 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
128 }
129}
130
bsalomon@google.comd302f142011-03-03 13:54:13 +0000131bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000132 const GrSamplerState& sampler) {
133 GrAssert(NULL != texture);
134 if (!sampler.getMatrix().isIdentity()) {
135 return false;
136 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000137 GrGLTexture::Orientation orientation = texture->orientation();
138 if (GrGLTexture::kBottomUp_Orientation == orientation) {
139 return false;
140 } else {
141 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
142 }
143 return true;
144}
145
146///////////////////////////////////////////////////////////////////////////////
147
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000148static bool gPrintStartupSpew;
149
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000150static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000151
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000152 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000153
twiz@google.com0f31ca72011-03-18 17:38:11 +0000154 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000155 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
156 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000157 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000158 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
159 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000160 // some implementations require texture to be mip-map complete before
161 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000162 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000163 GR_GL_TEXTURE_MIN_FILTER,
164 GR_GL_NEAREST));
165 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
166 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
167 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
168 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
169 GR_GL_COLOR_ATTACHMENT0,
170 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000171 GrGLenum status;
172 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000173 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
174 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000175
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000176 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000177}
178
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000179GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
180
181 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000182
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000183 fillInConfigRenderableTable();
184
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000185 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000186
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000187 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000188
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000189 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000190 const GrGLubyte* ext;
191 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000192 const GrGLubyte* vendor;
193 const GrGLubyte* renderer;
194 const GrGLubyte* version;
195 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
196 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
197 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000198 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
199 this);
200 GrPrintf("------ VENDOR %s\n", vendor);
201 GrPrintf("------ RENDERER %s\n", renderer);
202 GrPrintf("------ VERSION %s\n", version);
203 GrPrintf("------ EXTENSIONS\n %s \n", ext);
204 }
205
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000206 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000207
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000208 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000209
bsalomon@google.comfe676522011-06-17 18:12:21 +0000210 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000211 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000212}
213
214GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000215 // This must be called by before the GrDrawTarget destructor
216 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000217 // This subclass must do this before the base class destructor runs
218 // since we will unref the GrGLInterface.
219 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000220}
221
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000222///////////////////////////////////////////////////////////////////////////////
223
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000224void GrGpuGL::initCaps() {
225 GrGLint maxTextureUnits;
226 // check FS and fixed-function texture unit limits
227 // we only use textures in the fragment stage currently.
228 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000229 const GrGLInterface* gl = this->glInterface();
230 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000231 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000232
233 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000234 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000236 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 for (int i = 0; i < numFormats; ++i) {
238 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
239 fCaps.f8BitPaletteSupport = true;
240 break;
241 }
242 }
243
244 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000245 // we could also look for GL_ATI_separate_stencil extension or
246 // GL_EXT_stencil_two_side but they use different function signatures
247 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000248 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000249 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000250 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 this->hasExtension("GL_EXT_stencil_wrap");
252 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000253 // ES 2 has two sided stencil and stencil wrap
254 fCaps.fTwoSidedStencilSupport = true;
255 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000256 }
257
258 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
260 // extension includes glMapBuffer.
261 } else {
262 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
263 }
264
265 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000266 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000267 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
268 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000269 } else {
270 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000271 }
272 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000273 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000274 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000275 }
276
277 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
278
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000279 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
280 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000281 // Our render targets are always created with textures as the color
282 // attachment, hence this min:
283 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
284
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000285 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000286}
287
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000288void GrGpuGL::fillInConfigRenderableTable() {
289
290 // OpenGL < 3.0
291 // no support for render targets unless the GL_ARB_framebuffer_object
292 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
293 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
294 // probably don't get R8 in this case.
295
296 // OpenGL 3.0
297 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
298 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
299
300 // >= OpenGL 3.1
301 // base color renderable: RED, RG, RGB, and RGBA
302 // sized derivatives: R8, RGBA4, RGBA8
303 // if the GL_ARB_compatibility extension is supported then we get back
304 // support for GL_ALPHA and ALPHA8
305
306 // GL_EXT_bgra adds BGRA render targets to any version
307
308 // ES 2.0
309 // color renderable: RGBA4, RGB5_A1, RGB565
310 // GL_EXT_texture_rg adds support for R8 as a color render target
311 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
312 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
313 // added BGRA support
314
315 if (kDesktop_GrGLBinding == this->glBinding()) {
316 // Post 3.0 we will get R8
317 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
318 if (this->glVersion() >= GR_GL_VER(3,0) ||
319 this->hasExtension("GL_ARB_framebuffer_object")) {
320 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
321 }
322 } else {
323 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000324 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
325 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000326 }
327
328 if (kDesktop_GrGLBinding != this->glBinding()) {
329 // only available in ES
330 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
331 }
332
333 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
334 // GL_EXT_framebuffer_object for FBO support. Both of these
335 // allow RGBA4 render targets so this is always supported.
336 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
337
338 if (this->glCaps().rgba8RenderbufferSupport()) {
339 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
340 }
341
342 if (this->glCaps().bgraFormatSupport()) {
343 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
344 }
345
346 // the un-premultiplied formats just inherit the premultiplied setting
347 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
348 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
349 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
350 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
351}
352
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000353bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
354 if (kUnknown_CanPreserveUnpremulRoundtrip ==
355 fCanPreserveUnpremulRoundtrip) {
356
357 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
358 uint32_t* srcData = data.get();
359 uint32_t* firstRead = data.get() + 256 * 256;
360 uint32_t* secondRead = data.get() + 2 * 256 * 256;
361
362 for (int y = 0; y < 256; ++y) {
363 for (int x = 0; x < 256; ++x) {
364 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
365 color[3] = y;
366 color[2] = x;
367 color[1] = x;
368 color[0] = x;
369 }
370 }
371
372 // We have broader support for read/write pixels on render targets
373 // than on textures.
374 GrTextureDesc dstDesc;
375 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
376 kNoStencil_GrTextureFlagBit;
377 dstDesc.fWidth = 256;
378 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000379 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000380 dstDesc.fSampleCnt = 0;
381
382 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
383 if (!dstTex.get()) {
384 return false;
385 }
386 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
387 GrAssert(NULL != rt);
388
389 bool failed = true;
390 static const UnpremulConversion gMethods[] = {
391 kUpOnWrite_DownOnRead_UnpremulConversion,
392 kDownOnWrite_UpOnRead_UnpremulConversion,
393 };
394
395 // pretend that we can do the roundtrip to avoid recursive calls to
396 // this function
397 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
398 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
399 fUnpremulConversion = gMethods[i];
400 rt->writePixels(0, 0,
401 256, 256,
402 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
403 rt->readPixels(0, 0,
404 256, 256,
405 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
406 rt->writePixels(0, 0,
407 256, 256,
408 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
409 rt->readPixels(0, 0,
410 256, 256,
411 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
412 failed = false;
413 for (int j = 0; j < 256 * 256; ++j) {
414 if (firstRead[j] != secondRead[j]) {
415 failed = true;
416 break;
417 }
418 }
419 }
420 fCanPreserveUnpremulRoundtrip = failed ?
421 kNo_CanPreserveUnpremulRoundtrip :
422 kYes_CanPreserveUnpremulRoundtrip;
423 }
424
425 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
426 return true;
427 } else {
428 return false;
429 }
430}
431
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000432GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000433 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
434 return GrPixelConfigSwapRAndB(config);
435 } else {
436 return config;
437 }
438}
439
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000440GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000441 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000442 return GrPixelConfigSwapRAndB(config);
443 } else {
444 return config;
445 }
446}
447
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000448bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
449 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
450}
451
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000452void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000453 if (gPrintStartupSpew && !fPrintedCaps) {
454 fPrintedCaps = true;
455 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000456 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000457 }
458
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000459 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000460 GL_CALL(Disable(GR_GL_DEPTH_TEST));
461 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000462
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000463 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
464 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000466 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000467 // we never use point or polygon smoothing
468 // should we also disable polygon smoothing?
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000469 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000470 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000471 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000472 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000473 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000474
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000476 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000478 // invalid
479 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000480
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000481 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000482
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000483 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000484
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000485 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000486
tomhudson@google.com93813632011-10-27 20:21:16 +0000487 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000488 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000489 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000490
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000491 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000492 // set to true to force disableScissor to make a GL call.
493 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000494 this->disableScissor();
495
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000496 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000497
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000498 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000499 fHWStencilClip = false;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000500
501 // TODO: I believe this should actually go in GrGpu::onResetContext
502 // rather than here
503 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000504
505 fHWGeometryState.fIndexBuffer = NULL;
506 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000507
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000508 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000509
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000510 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000511
512 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000513
514 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000515 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000516 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
517 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000518 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000519 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
520 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000521 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000522 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
523 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000524 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000525 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
526 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000527}
528
bsalomon@google.come269f212011-11-07 13:29:52 +0000529GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000530 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000531 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000532 return NULL;
533 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000534
bsalomon@google.com99621082011-11-15 16:47:16 +0000535 glTexDesc.fWidth = desc.fWidth;
536 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000537 glTexDesc.fConfig = desc.fConfig;
538 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
539 glTexDesc.fOwnsID = false;
540 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
541
542 GrGLTexture* texture = NULL;
543 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
544 GrGLRenderTarget::Desc glRTDesc;
545 glRTDesc.fRTFBOID = 0;
546 glRTDesc.fTexFBOID = 0;
547 glRTDesc.fMSColorRenderbufferID = 0;
548 glRTDesc.fOwnIDs = true;
549 glRTDesc.fConfig = desc.fConfig;
550 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000551 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
552 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000553 glTexDesc.fTextureID,
554 &glRTDesc)) {
555 return NULL;
556 }
557 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
558 } else {
559 texture = new GrGLTexture(this, glTexDesc);
560 }
561 if (NULL == texture) {
562 return NULL;
563 }
564
565 this->setSpareTextureUnit();
566 return texture;
567}
568
569GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
570 GrGLRenderTarget::Desc glDesc;
571 glDesc.fConfig = desc.fConfig;
572 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
573 glDesc.fMSColorRenderbufferID = 0;
574 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
575 glDesc.fSampleCnt = desc.fSampleCnt;
576 glDesc.fOwnIDs = false;
577 GrGLIRect viewport;
578 viewport.fLeft = 0;
579 viewport.fBottom = 0;
580 viewport.fWidth = desc.fWidth;
581 viewport.fHeight = desc.fHeight;
582
583 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
584 if (desc.fStencilBits) {
585 GrGLStencilBuffer::Format format;
586 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
587 format.fPacked = false;
588 format.fStencilBits = desc.fStencilBits;
589 format.fTotalBits = desc.fStencilBits;
590 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
591 0,
592 desc.fWidth,
593 desc.fHeight,
594 desc.fSampleCnt,
595 format);
596 tgt->setStencilBuffer(sb);
597 sb->unref();
598 }
599 return tgt;
600}
601
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000602////////////////////////////////////////////////////////////////////////////////
603
bsalomon@google.com6f379512011-11-16 20:36:03 +0000604void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
605 int left, int top, int width, int height,
606 GrPixelConfig config, const void* buffer,
607 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000608 if (NULL == buffer) {
609 return;
610 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000611 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
612
bsalomon@google.com6f379512011-11-16 20:36:03 +0000613 this->setSpareTextureUnit();
614 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
615 GrGLTexture::Desc desc;
616 desc.fConfig = glTex->config();
617 desc.fWidth = glTex->width();
618 desc.fHeight = glTex->height();
619 desc.fOrientation = glTex->orientation();
620 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000621
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000622 this->uploadTexData(desc, false,
623 left, top, width, height,
624 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000625}
626
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000627namespace {
628bool adjust_pixel_ops_params(int surfaceWidth,
629 int surfaceHeight,
630 size_t bpp,
631 int* left, int* top, int* width, int* height,
632 const void** data,
633 size_t* rowBytes) {
634 if (!*rowBytes) {
635 *rowBytes = *width * bpp;
636 }
637
638 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
639 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
640
641 if (!subRect.intersect(bounds)) {
642 return false;
643 }
644 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
645 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
646
647 *left = subRect.fLeft;
648 *top = subRect.fTop;
649 *width = subRect.width();
650 *height = subRect.height();
651 return true;
652}
653}
654
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000655bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
656 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000657 int left, int top, int width, int height,
658 GrPixelConfig dataConfig,
659 const void* data,
660 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000661 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000662
663 size_t bpp = GrBytesPerPixel(dataConfig);
664 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
665 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000666 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000667 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000668 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000669
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000670 // in case we need a temporary, trimmed copy of the src pixels
671 SkAutoSMalloc<128 * 128> tempStorage;
672
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000673 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000674 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000675 if (useTexStorage) {
676 if (kDesktop_GrGLBinding == this->glBinding()) {
677 // 565 is not a sized internal format on desktop GL. So on desktop
678 // with 565 we always use an unsized internal format to let the
679 // system pick the best sized format to convert the 565 data to.
680 // Since glTexStorage only allows sized internal formats we will
681 // instead fallback to glTexImage2D.
682 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
683 } else {
684 // ES doesn't allow paletted textures to be used with tex storage
685 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
686 }
687 }
688
689 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000690 GrGLenum externalFormat;
691 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000692 // glTexStorage requires sized internal formats on both desktop and ES. ES
693 // glTexImage requires an unsized format.
694 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
695 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000696 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000697 }
698
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000699 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000700 // paletted textures cannot be updated
701 return false;
702 }
703
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000704 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000705 * check whether to allocate a temporary buffer for flipping y or
706 * because our srcData has extra bytes past each row. If so, we need
707 * to trim those off here, since GL ES may not let us specify
708 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000709 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000710 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000711 bool swFlipY = false;
712 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000713 if (NULL != data) {
714 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000715 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000716 glFlipY = true;
717 } else {
718 swFlipY = true;
719 }
720 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000721 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000722 // can't use this for flipping, only non-neg values allowed. :(
723 if (rowBytes != trimRowBytes) {
724 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
725 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
726 restoreGLRowLength = true;
727 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000728 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000729 if (trimRowBytes != rowBytes || swFlipY) {
730 // copy data into our new storage, skipping the trailing bytes
731 size_t trimSize = height * trimRowBytes;
732 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000733 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000734 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000735 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000736 char* dst = (char*)tempStorage.reset(trimSize);
737 for (int y = 0; y < height; y++) {
738 memcpy(dst, src, trimRowBytes);
739 if (swFlipY) {
740 src -= rowBytes;
741 } else {
742 src += rowBytes;
743 }
744 dst += trimRowBytes;
745 }
746 // now point data to our copied version
747 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000748 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000749 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000750 if (glFlipY) {
751 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
752 }
753 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000754 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000755 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000756 if (isNewTexture &&
757 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000758 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000759 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000760 if (useTexStorage) {
761 // We never resize or change formats of textures. We don't use
762 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000763 GL_ALLOC_CALL(this->glInterface(),
764 TexStorage2D(GR_GL_TEXTURE_2D,
765 1, // levels
766 internalFormat,
767 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000768 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000769 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
770 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
771 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000772 GL_ALLOC_CALL(this->glInterface(),
773 CompressedTexImage2D(GR_GL_TEXTURE_2D,
774 0, // level
775 internalFormat,
776 desc.fWidth, desc.fHeight,
777 0, // border
778 imageSize,
779 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000780 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000781 GL_ALLOC_CALL(this->glInterface(),
782 TexImage2D(GR_GL_TEXTURE_2D,
783 0, // level
784 internalFormat,
785 desc.fWidth, desc.fHeight,
786 0, // border
787 externalFormat, externalType,
788 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000789 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000790 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000791 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000792 if (error != GR_GL_NO_ERROR) {
793 succeeded = false;
794 } else {
795 // if we have data and we used TexStorage to create the texture, we
796 // now upload with TexSubImage.
797 if (NULL != data && useTexStorage) {
798 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
799 0, // level
800 left, top,
801 width, height,
802 externalFormat, externalType,
803 data));
804 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000805 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000806 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000807 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000808 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000809 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000810 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
811 0, // level
812 left, top,
813 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000814 externalFormat, externalType, data));
815 }
816
817 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000818 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000819 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000820 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000821 if (glFlipY) {
822 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
823 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000824 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000825}
826
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000827namespace {
828bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
829 int sampleCount,
830 GrGLenum format,
831 int width, int height) {
832 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
833 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
834 bool created = false;
835 if (GrGLCaps::kNVDesktop_CoverageAAType ==
836 ctxInfo.caps().coverageAAType()) {
837 const GrGLCaps::MSAACoverageMode& mode =
838 ctxInfo.caps().getMSAACoverageMode(sampleCount);
839 GL_ALLOC_CALL(ctxInfo.interface(),
840 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
841 mode.fCoverageSampleCnt,
842 mode.fColorSampleCnt,
843 format,
844 width, height));
845 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
846 }
847 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000848 // glRBMS will fail if requested samples is > max samples.
849 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000850 GL_ALLOC_CALL(ctxInfo.interface(),
851 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
852 sampleCount,
853 format,
854 width, height));
855 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
856 }
857 return created;
858}
859}
860
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000861bool GrGpuGL::createRenderTargetObjects(int width, int height,
862 GrGLuint texID,
863 GrGLRenderTarget::Desc* desc) {
864 desc->fMSColorRenderbufferID = 0;
865 desc->fRTFBOID = 0;
866 desc->fTexFBOID = 0;
867 desc->fOwnIDs = true;
868
869 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000870
bsalomon@google.comab15d612011-08-09 12:57:56 +0000871 GrGLenum msColorFormat = 0; // suppress warning
872
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000873 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874 if (!desc->fTexFBOID) {
875 goto FAILED;
876 }
877
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000878
879 // If we are using multisampling we will create two FBOS. We render
880 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000881 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000882 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000883 goto FAILED;
884 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000885 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
886 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000887 if (!desc->fRTFBOID ||
888 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000889 !this->configToGLFormats(desc->fConfig,
890 // GLES requires sized internal formats
891 kES2_GrGLBinding == this->glBinding(),
892 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893 goto FAILED;
894 }
895 } else {
896 desc->fRTFBOID = desc->fTexFBOID;
897 }
898
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000899 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000900 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000901 if (desc->fRTFBOID != desc->fTexFBOID) {
902 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000903 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000905 if (!renderbuffer_storage_msaa(fGLContextInfo,
906 desc->fSampleCnt,
907 msColorFormat,
908 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 goto FAILED;
910 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000911 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
912 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913 GR_GL_COLOR_ATTACHMENT0,
914 GR_GL_RENDERBUFFER,
915 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000916 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000917 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
918 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
919 goto FAILED;
920 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000921 fGLContextInfo.caps().markConfigAsValidColorAttachment(
922 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000923 }
924 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000925 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000926
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
928 GR_GL_COLOR_ATTACHMENT0,
929 GR_GL_TEXTURE_2D,
930 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000931 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000932 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
933 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
934 goto FAILED;
935 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000936 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000937 }
938
939 return true;
940
941FAILED:
942 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000943 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000944 }
945 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000946 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000947 }
948 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000949 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950 }
951 return false;
952}
953
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000954// good to set a break-point here to know when createTexture fails
955static GrTexture* return_null_texture() {
956// GrAssert(!"null texture");
957 return NULL;
958}
959
960#if GR_DEBUG
961static size_t as_size_t(int x) {
962 return x;
963}
964#endif
965
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000966GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000967 const void* srcData,
968 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000969
970#if GR_COLLECT_STATS
971 ++fStats.fTextureCreateCnt;
972#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000973
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000974 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000975 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000976
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000977 // Attempt to catch un- or wrongly initialized sample counts;
978 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
979
bsalomon@google.com99621082011-11-15 16:47:16 +0000980 glTexDesc.fWidth = desc.fWidth;
981 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000982 glTexDesc.fConfig = desc.fConfig;
983 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000984
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000985 glRTDesc.fMSColorRenderbufferID = 0;
986 glRTDesc.fRTFBOID = 0;
987 glRTDesc.fTexFBOID = 0;
988 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000989 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000990
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000991 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000992
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000993 const Caps& caps = this->getCaps();
994
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000995 // We keep GrRenderTargets in GL's normal orientation so that they
996 // can be drawn to by the outside world without the client having
997 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000998 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000999 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001000
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001001 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001002 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001003 desc.fSampleCnt) {
1004 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001005 }
1006
reed@google.comac10a2d2010-12-22 21:39:39 +00001007 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001008 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1009 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001010 return return_null_texture();
1011 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001012 }
1013
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001014 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001015 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001016 // provides a hint about how this texture will be used
1017 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1018 GR_GL_TEXTURE_USAGE,
1019 GR_GL_FRAMEBUFFER_ATTACHMENT));
1020 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001021 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001022 return return_null_texture();
1023 }
1024
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001025 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001026 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001027
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001028 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1029 // drivers have a bug where an FBO won't be complete if it includes a
1030 // texture that is not mipmap complete (considering the filter in use).
1031 GrGLTexture::TexParams initialTexParams;
1032 // we only set a subset here so invalidate first
1033 initialTexParams.invalidate();
1034 initialTexParams.fFilter = GR_GL_NEAREST;
1035 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1036 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001037 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1038 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001039 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001040 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1041 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001042 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001043 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1044 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001045 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1047 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001048 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001049 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1050 glTexDesc.fWidth, glTexDesc.fHeight,
1051 desc.fConfig, srcData, rowBytes)) {
1052 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1053 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001054 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001055
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001056 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 if (renderTarget) {
1058#if GR_COLLECT_STATS
1059 ++fStats.fRenderTargetCreateCnt;
1060#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001061 // unbind the texture from the texture unit before binding it to the frame buffer
1062 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1063
bsalomon@google.com99621082011-11-15 16:47:16 +00001064 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1065 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001066 glTexDesc.fTextureID,
1067 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001068 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 return return_null_texture();
1070 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001071 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001072 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001073 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001074 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001075 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001076#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001077 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1078 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001079#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001080 return tex;
1081}
1082
1083namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001084
1085const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1086
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001087void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1088 GrGLuint rb,
1089 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001090 // we shouldn't ever know one size and not the other
1091 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1092 (kUnknownBitCount == format->fTotalBits));
1093 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001094 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001095 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1096 (GrGLint*)&format->fStencilBits);
1097 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001098 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001099 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1100 (GrGLint*)&format->fTotalBits);
1101 format->fTotalBits += format->fStencilBits;
1102 } else {
1103 format->fTotalBits = format->fStencilBits;
1104 }
1105 }
1106}
1107}
1108
1109bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1110 int width, int height) {
1111
1112 // All internally created RTs are also textures. We don't create
1113 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1114 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001115 GrAssert(width >= rt->width());
1116 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117
1118 int samples = rt->numSamples();
1119 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 if (!sbID) {
1122 return false;
1123 }
1124
1125 GrGLStencilBuffer* sb = NULL;
1126
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001127 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001129 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 // we start with the last stencil format that succeeded in hopes
1131 // that we won't go through this loop more than once after the
1132 // first (painful) stencil creation.
1133 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001134 const GrGLCaps::StencilFormat& sFmt =
1135 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001136 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001137 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001139 bool created;
1140 if (samples > 0) {
1141 created = renderbuffer_storage_msaa(fGLContextInfo,
1142 samples,
1143 sFmt.fInternalFormat,
1144 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001146 GL_ALLOC_CALL(this->glInterface(),
1147 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001148 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001149 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001150 created =
1151 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001153 if (created) {
1154 // After sized formats we attempt an unsized format and take
1155 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001156 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001158 sb = new GrGLStencilBuffer(this, sbID, width, height,
1159 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001160 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1161 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001162 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001164 return true;
1165 }
1166 sb->abandon(); // otherwise we lose sbID
1167 sb->unref();
1168 }
1169 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001170 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001171 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172}
1173
1174bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1175 GrRenderTarget* rt) {
1176 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1177
1178 GrGLuint fbo = glrt->renderFBOID();
1179
1180 if (NULL == sb) {
1181 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001182 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 GR_GL_STENCIL_ATTACHMENT,
1184 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001185 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001186 GR_GL_DEPTH_ATTACHMENT,
1187 GR_GL_RENDERBUFFER, 0));
1188#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001189 GrGLenum status;
1190 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1192#endif
1193 }
1194 return true;
1195 } else {
1196 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1197 GrGLuint rb = glsb->renderbufferID();
1198
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001199 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001200 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1201 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 GR_GL_STENCIL_ATTACHMENT,
1203 GR_GL_RENDERBUFFER, rb));
1204 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001205 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206 GR_GL_DEPTH_ATTACHMENT,
1207 GR_GL_RENDERBUFFER, rb));
1208 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001209 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 GR_GL_DEPTH_ATTACHMENT,
1211 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001212 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001214 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001215 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001216 glsb->format())) {
1217 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1218 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001220 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001222 if (glsb->format().fPacked) {
1223 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1224 GR_GL_DEPTH_ATTACHMENT,
1225 GR_GL_RENDERBUFFER, 0));
1226 }
1227 return false;
1228 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001229 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001230 rt->config(),
1231 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001234 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001236}
1237
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001238////////////////////////////////////////////////////////////////////////////////
1239
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001240GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001241 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001242 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001243 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001244 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001245 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001246 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001247 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001248 GL_ALLOC_CALL(this->glInterface(),
1249 BufferData(GR_GL_ARRAY_BUFFER,
1250 size,
1251 NULL, // data ptr
1252 dynamic ? GR_GL_DYNAMIC_DRAW :
1253 GR_GL_STATIC_DRAW));
1254 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001255 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 // deleting bound buffer does implicit bind to 0
1257 fHWGeometryState.fVertexBuffer = NULL;
1258 return NULL;
1259 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001260 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 size, dynamic);
1262 fHWGeometryState.fVertexBuffer = vertexBuffer;
1263 return vertexBuffer;
1264 }
1265 return NULL;
1266}
1267
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001268GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001269 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001270 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001271 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001273 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001275 GL_ALLOC_CALL(this->glInterface(),
1276 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1277 size,
1278 NULL, // data ptr
1279 dynamic ? GR_GL_DYNAMIC_DRAW :
1280 GR_GL_STATIC_DRAW));
1281 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001282 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 // deleting bound buffer does implicit bind to 0
1284 fHWGeometryState.fIndexBuffer = NULL;
1285 return NULL;
1286 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001287 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001288 size, dynamic);
1289 fHWGeometryState.fIndexBuffer = indexBuffer;
1290 return indexBuffer;
1291 }
1292 return NULL;
1293}
1294
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001295void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001296 const GrDrawState& drawState = this->getDrawState();
1297 const GrGLRenderTarget* rt =
1298 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1299
1300 GrAssert(NULL != rt);
1301 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001302
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001303 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001304 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1305 rect.width(), rect.height());
1306 if (scissor.contains(vp)) {
1307 disableScissor();
1308 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 }
1310
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001311 if (fHWBounds.fScissorRect != scissor) {
1312 scissor.pushToGLScissor(this->glInterface());
1313 fHWBounds.fScissorRect = scissor;
1314 }
1315 if (!fHWBounds.fScissorEnabled) {
1316 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1317 fHWBounds.fScissorEnabled = true;
1318 }
1319}
1320
1321void GrGpuGL::disableScissor() {
1322 if (fHWBounds.fScissorEnabled) {
1323 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1324 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 }
1326}
1327
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001328void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001329 const GrDrawState& drawState = this->getDrawState();
1330 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001331 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001332 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001333
bsalomon@google.com74b98712011-11-11 19:46:16 +00001334 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001335 if (NULL != rect) {
1336 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001337 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001338 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001339 if (clippedRect.intersect(rtRect)) {
1340 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001341 } else {
1342 return;
1343 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001345 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001346 if (NULL != rect)
1347 this->enableScissoring(*rect);
1348 else
1349 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001350
1351 GrGLfloat r, g, b, a;
1352 static const GrGLfloat scale255 = 1.f / 255.f;
1353 a = GrColorUnpackA(color) * scale255;
1354 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001355 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001356 scaleRGB *= a;
1357 }
1358 r = GrColorUnpackR(color) * scaleRGB;
1359 g = GrColorUnpackG(color) * scaleRGB;
1360 b = GrColorUnpackB(color) * scaleRGB;
1361
1362 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001363 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001364 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001365 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001366}
1367
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001368void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001369 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001370 return;
1371 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001372
1373 this->flushRenderTarget(&GrIRect::EmptyIRect());
1374
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001375 this->disableScissor();
1376
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001377 GL_CALL(StencilMask(0xffffffff));
1378 GL_CALL(ClearStencil(0));
1379 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001380 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001381}
1382
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001383void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001384 const GrDrawState& drawState = this->getDrawState();
1385 const GrRenderTarget* rt = drawState.getRenderTarget();
1386 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001387
1388 // this should only be called internally when we know we have a
1389 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001390 GrAssert(NULL != rt->getStencilBuffer());
1391 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001392#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001394 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001395#else
1396 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001397 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001398 // turned into draws. Our contract on GrDrawTarget says that
1399 // changing the clip between stencil passes may or may not
1400 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001401 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001402#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001403 GrGLint value;
1404 if (insideClip) {
1405 value = (1 << (stencilBitCount - 1));
1406 } else {
1407 value = 0;
1408 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001409 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001410 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001411 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001412 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001413 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001414 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001415}
1416
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001417void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001418 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001419}
1420
bsalomon@google.comc4364992011-11-07 15:54:49 +00001421bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1422 int left, int top,
1423 int width, int height,
1424 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001425 size_t rowBytes) const {
1426 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001427 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001428 return false;
1429 }
1430
1431 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001432 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001433 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001434 return true;
1435 }
1436 // If we have to do memcpys to handle rowBytes then y-flip is free
1437 // Note the rowBytes might be tight to the passed in data, but if data
1438 // gets clipped in x to the target the rowBytes will no longer be tight.
1439 if (left >= 0 && (left + width) < renderTarget->width()) {
1440 return 0 == rowBytes ||
1441 GrBytesPerPixel(config) * width == rowBytes;
1442 } else {
1443 return false;
1444 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001445}
1446
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001447bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001448 int left, int top,
1449 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001450 GrPixelConfig config,
1451 void* buffer,
1452 size_t rowBytes,
1453 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001454 GrGLenum format;
1455 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001456 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001457 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001458 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001459 size_t bpp = GrBytesPerPixel(config);
1460 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1461 &left, &top, &width, &height,
1462 const_cast<const void**>(&buffer),
1463 &rowBytes)) {
1464 return false;
1465 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001466
bsalomon@google.comc6980972011-11-02 19:57:21 +00001467 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001468 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001469 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001470 switch (tgt->getResolveType()) {
1471 case GrGLRenderTarget::kCantResolve_ResolveType:
1472 return false;
1473 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001474 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001475 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001476 break;
1477 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001478 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001479 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001480 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1481 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001482 break;
1483 default:
1484 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001485 }
1486
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001487 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001488
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001489 // the read rect is viewport-relative
1490 GrGLIRect readRect;
1491 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001492
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001493 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001494 if (0 == rowBytes) {
1495 rowBytes = tightRowBytes;
1496 }
1497 size_t readDstRowBytes = tightRowBytes;
1498 void* readDst = buffer;
1499
1500 // determine if GL can read using the passed rowBytes or if we need
1501 // a scratch buffer.
1502 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1503 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001504 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001505 GrAssert(!(rowBytes % sizeof(GrColor)));
1506 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1507 readDstRowBytes = rowBytes;
1508 } else {
1509 scratch.reset(tightRowBytes * height);
1510 readDst = scratch.get();
1511 }
1512 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001513 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001514 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1515 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001516 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1517 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001518 format, type, readDst));
1519 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001520 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001521 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1522 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001523 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001524 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1525 invertY = true;
1526 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001527
1528 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001529 // API presents top-to-bottom. We must preserve the padding contents. Note
1530 // that the above readPixels did not overwrite the padding.
1531 if (readDst == buffer) {
1532 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001533 if (!invertY) {
1534 scratch.reset(tightRowBytes);
1535 void* tmpRow = scratch.get();
1536 // flip y in-place by rows
1537 const int halfY = height >> 1;
1538 char* top = reinterpret_cast<char*>(buffer);
1539 char* bottom = top + (height - 1) * rowBytes;
1540 for (int y = 0; y < halfY; y++) {
1541 memcpy(tmpRow, top, tightRowBytes);
1542 memcpy(top, bottom, tightRowBytes);
1543 memcpy(bottom, tmpRow, tightRowBytes);
1544 top += rowBytes;
1545 bottom -= rowBytes;
1546 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001547 }
1548 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001549 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001550 // copy from readDst to buffer while flipping y
1551 const int halfY = height >> 1;
1552 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001553 char* dst = reinterpret_cast<char*>(buffer);
1554 if (!invertY) {
1555 dst += (height-1) * rowBytes;
1556 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001557 for (int y = 0; y < height; y++) {
1558 memcpy(dst, src, tightRowBytes);
1559 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001560 if (invertY) {
1561 dst += rowBytes;
1562 } else {
1563 dst -= rowBytes;
1564 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001565 }
1566 }
1567 return true;
1568}
1569
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001570void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001571
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001572 GrGLRenderTarget* rt =
1573 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1574 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001575
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001576 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001577 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001578 #if GR_COLLECT_STATS
1579 ++fStats.fRenderTargetChngCnt;
1580 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001581 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001582 GrGLenum status;
1583 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001584 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001585 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001586 }
1587 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001588 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001589 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001590 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001591 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001592 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001593 fHWBounds.fViewportRect = vp;
1594 }
1595 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001596 if (NULL == bound || !bound->isEmpty()) {
1597 rt->flagAsNeedingResolve(bound);
1598 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001599}
1600
twiz@google.com0f31ca72011-03-18 17:38:11 +00001601GrGLenum gPrimitiveType2GLMode[] = {
1602 GR_GL_TRIANGLES,
1603 GR_GL_TRIANGLE_STRIP,
1604 GR_GL_TRIANGLE_FAN,
1605 GR_GL_POINTS,
1606 GR_GL_LINES,
1607 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001608};
1609
bsalomon@google.comd302f142011-03-03 13:54:13 +00001610#define SWAP_PER_DRAW 0
1611
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001612#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001613 #if GR_MAC_BUILD
1614 #include <AGL/agl.h>
1615 #elif GR_WIN32_BUILD
1616 void SwapBuf() {
1617 DWORD procID = GetCurrentProcessId();
1618 HWND hwnd = GetTopWindow(GetDesktopWindow());
1619 while(hwnd) {
1620 DWORD wndProcID = 0;
1621 GetWindowThreadProcessId(hwnd, &wndProcID);
1622 if(wndProcID == procID) {
1623 SwapBuffers(GetDC(hwnd));
1624 }
1625 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1626 }
1627 }
1628 #endif
1629#endif
1630
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001631void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1632 uint32_t startVertex,
1633 uint32_t startIndex,
1634 uint32_t vertexCount,
1635 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001636 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1637
twiz@google.com0f31ca72011-03-18 17:38:11 +00001638 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001639
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001640 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1641 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1642
1643 // our setupGeometry better have adjusted this to zero since
1644 // DrawElements always draws from the begining of the arrays for idx 0.
1645 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001646
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001647 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1648 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001649#if SWAP_PER_DRAW
1650 glFlush();
1651 #if GR_MAC_BUILD
1652 aglSwapBuffers(aglGetCurrentContext());
1653 int set_a_break_pt_here = 9;
1654 aglSwapBuffers(aglGetCurrentContext());
1655 #elif GR_WIN32_BUILD
1656 SwapBuf();
1657 int set_a_break_pt_here = 9;
1658 SwapBuf();
1659 #endif
1660#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001661}
1662
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001663void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1664 uint32_t startVertex,
1665 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001666 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1667
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001668 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1669
1670 // our setupGeometry better have adjusted this to zero.
1671 // DrawElements doesn't take an offset so we always adjus the startVertex.
1672 GrAssert(0 == startVertex);
1673
1674 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1675 // account for startVertex in the DrawElements case. So we always
1676 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001677 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001678#if SWAP_PER_DRAW
1679 glFlush();
1680 #if GR_MAC_BUILD
1681 aglSwapBuffers(aglGetCurrentContext());
1682 int set_a_break_pt_here = 9;
1683 aglSwapBuffers(aglGetCurrentContext());
1684 #elif GR_WIN32_BUILD
1685 SwapBuf();
1686 int set_a_break_pt_here = 9;
1687 SwapBuf();
1688 #endif
1689#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001690}
1691
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001692void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1693
1694 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001695
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001696 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001697 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001698 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001699 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1700 rt->renderFBOID()));
1701 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1702 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 #if GR_COLLECT_STATS
1704 ++fStats.fRenderTargetChngCnt;
1705 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001706 // make sure we go through flushRenderTarget() since we've modified
1707 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001708 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001709 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001710 const GrIRect dirtyRect = rt->getResolveRect();
1711 GrGLIRect r;
1712 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1713 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001714
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001715 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001716 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001717#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001718 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1719 GL_CALL(Scissor(r.fLeft, r.fBottom,
1720 r.fWidth, r.fHeight));
1721 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001722 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001723 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001724#else
1725 this->enableScissoring(dirtyRect);
1726 GL_CALL(ResolveMultisampleFramebuffer());
1727#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001728 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001729 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001730 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001731 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1732 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001733 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001734 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001735 int right = r.fLeft + r.fWidth;
1736 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001737 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1738 r.fLeft, r.fBottom, right, top,
1739 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001741 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 }
1743}
1744
twiz@google.com0f31ca72011-03-18 17:38:11 +00001745static const GrGLenum grToGLStencilFunc[] = {
1746 GR_GL_ALWAYS, // kAlways_StencilFunc
1747 GR_GL_NEVER, // kNever_StencilFunc
1748 GR_GL_GREATER, // kGreater_StencilFunc
1749 GR_GL_GEQUAL, // kGEqual_StencilFunc
1750 GR_GL_LESS, // kLess_StencilFunc
1751 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1752 GR_GL_EQUAL, // kEqual_StencilFunc,
1753 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001754};
1755GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1756GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1757GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1758GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1759GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1760GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1761GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1762GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1763GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1764
twiz@google.com0f31ca72011-03-18 17:38:11 +00001765static const GrGLenum grToGLStencilOp[] = {
1766 GR_GL_KEEP, // kKeep_StencilOp
1767 GR_GL_REPLACE, // kReplace_StencilOp
1768 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1769 GR_GL_INCR, // kIncClamp_StencilOp
1770 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1771 GR_GL_DECR, // kDecClamp_StencilOp
1772 GR_GL_ZERO, // kZero_StencilOp
1773 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001774};
1775GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1776GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1777GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1778GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1779GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1780GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1781GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1782GR_STATIC_ASSERT(6 == kZero_StencilOp);
1783GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1784
reed@google.comac10a2d2010-12-22 21:39:39 +00001785void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001786 const GrDrawState& drawState = this->getDrawState();
1787
1788 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001789
1790 // use stencil for clipping if clipping is enabled and the clip
1791 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001792 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001793 bool drawClipToStencil =
1794 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001795 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1796 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001797 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1798 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001799
1800 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001801
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001802 // we can't simultaneously perform stencil-clipping and
1803 // modify the stencil clip
1804 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001805
bsalomon@google.comd302f142011-03-03 13:54:13 +00001806 if (settings->isDisabled()) {
1807 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001808 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001809 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001810 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001811
1812 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001813 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001814 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001815 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001816 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001817 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001818 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1819 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1820 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1821 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1822 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1823 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1824 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1825 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001826 }
1827 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001828 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001829 GrStencilBuffer* stencilBuffer =
1830 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001831 if (NULL != stencilBuffer) {
1832 stencilBits = stencilBuffer->bits();
1833 }
1834 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001835 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001836
1837 GrGLuint clipStencilMask = 0;
1838 GrGLuint userStencilMask = ~0;
1839 if (stencilBits > 0) {
1840 clipStencilMask = 1 << (stencilBits - 1);
1841 userStencilMask = clipStencilMask - 1;
1842 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001843
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001844 unsigned int frontRef = settings->frontFuncRef();
1845 unsigned int frontMask = settings->frontFuncMask();
1846 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001847 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001848
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001849 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001850 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1851 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001852 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001853 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001854 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001855
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001856 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001857 stencilClip,
1858 clipStencilMask,
1859 userStencilMask,
1860 &frontRef,
1861 &frontMask);
1862 frontWriteMask &= userStencilMask;
1863 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001864 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001865 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001866 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001867 settings->frontPassOp() < 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->backFailOp() < 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->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001872 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001873 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001874
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001875 unsigned int backRef = settings->backFuncRef();
1876 unsigned int backMask = settings->backFuncMask();
1877 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001878
1879
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001880 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001881 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1882 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001884 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001885 stencilClip, settings->backFunc())];
1886 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887 stencilClip,
1888 clipStencilMask,
1889 userStencilMask,
1890 &backRef,
1891 &backMask);
1892 backWriteMask &= userStencilMask;
1893 }
1894
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001895 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1896 frontRef, frontMask));
1897 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1898 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1899 backRef, backMask));
1900 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1901 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001902 grToGLStencilOp[settings->frontFailOp()],
1903 grToGLStencilOp[settings->frontPassOp()],
1904 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001905
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001906 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001907 grToGLStencilOp[settings->backFailOp()],
1908 grToGLStencilOp[settings->backPassOp()],
1909 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001910 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001911 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1912 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001913 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1914 grToGLStencilOp[settings->frontPassOp()],
1915 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001916 }
1917 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001918 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001919 fHWStencilClip = stencilClip;
1920 }
1921}
1922
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001923void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001924 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001925 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001926 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1927 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001928 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001929 bool smoothLines = false;
1930
bsalomon@google.com0650e812011-04-08 18:07:53 +00001931 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001932 smoothLines = this->willUseHWAALines();
1933 if (smoothLines) {
1934 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1935 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1936 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1937 // must disable msaa to use line smoothing
1938 if (rt->isMultisampled() &&
1939 kNo_TriState != fHWAAState.fMSAAEnabled) {
1940 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1941 fHWAAState.fMSAAEnabled = kNo_TriState;
1942 }
1943 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001944 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001945 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1946 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1947 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1948 }
1949 }
1950 }
1951 if (!smoothLines && rt->isMultisampled()) {
1952 if (this->getDrawState().isHWAntialiasState()) {
1953 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1954 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1955 fHWAAState.fMSAAEnabled = kYes_TriState;
1956 }
1957 } else {
1958 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1959 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1960 fHWAAState.fMSAAEnabled = kNo_TriState;
1961 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001962 }
1963 }
1964 }
1965}
1966
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001967void GrGpuGL::flushBlend(GrPrimitiveType type,
1968 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001969 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001970 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001971 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001972 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001973 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001974 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001975 if (kSA_BlendCoeff != fHWBlendState.fSrcCoeff ||
1976 kISA_BlendCoeff != fHWBlendState.fDstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001977 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1978 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001979 fHWBlendState.fSrcCoeff = kSA_BlendCoeff;
1980 fHWBlendState.fDstCoeff = kISA_BlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001981 }
1982 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001983 // any optimization to disable blending should
1984 // have already been applied and tweaked the coeffs
1985 // to (1, 0).
1986 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1987 kZero_BlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001988 if (blendOff) {
1989 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001990 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001991 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001992 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001993 } else {
1994 if (kYes_TriState != fHWBlendState.fEnabled) {
1995 GL_CALL(Enable(GR_GL_BLEND));
1996 fHWBlendState.fEnabled = kYes_TriState;
1997 }
1998 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1999 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002000 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2001 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002002 fHWBlendState.fSrcCoeff = srcCoeff;
2003 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002004 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002005 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002006 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2007 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002008 (!fHWBlendState.fConstColorValid ||
2009 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002010
2011 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002012 GrColorUnpackR(blendConst) / 255.f,
2013 GrColorUnpackG(blendConst) / 255.f,
2014 GrColorUnpackB(blendConst) / 255.f,
2015 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002016 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002017 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002018 fHWBlendState.fConstColor = blendConst;
2019 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002020 }
2021 }
2022 }
2023}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002024namespace {
2025
2026unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002027 switch (filter) {
2028 case GrSamplerState::kBilinear_Filter:
2029 case GrSamplerState::k4x4Downsample_Filter:
2030 return GR_GL_LINEAR;
2031 case GrSamplerState::kNearest_Filter:
2032 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002033 case GrSamplerState::kErode_Filter:
2034 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002035 return GR_GL_NEAREST;
2036 default:
2037 GrAssert(!"Unknown filter type");
2038 return GR_GL_LINEAR;
2039 }
2040}
2041
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002042// get_swizzle is only called from this .cpp so it is OK to inline it here
2043inline const GrGLenum* get_swizzle(GrPixelConfig config,
2044 const GrSamplerState& sampler,
2045 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002046 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002047 if (glCaps.textureRedSupport()) {
2048 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2049 GR_GL_RED, GR_GL_RED };
2050 return gRedSmear;
2051 } else {
2052 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2053 GR_GL_ALPHA, GR_GL_ALPHA };
2054 return gAlphaSmear;
2055 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002056 } else if (sampler.swapsRAndB()) {
2057 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2058 GR_GL_RED, GR_GL_ALPHA };
2059 return gRedBlueSwap;
2060 } else {
2061 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2062 GR_GL_BLUE, GR_GL_ALPHA };
2063 return gStraight;
2064 }
2065}
2066
2067void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2068 // should add texparameteri to interface to make 1 instead of 4 calls here
2069 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2070 GR_GL_TEXTURE_SWIZZLE_R,
2071 swizzle[0]));
2072 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2073 GR_GL_TEXTURE_SWIZZLE_G,
2074 swizzle[1]));
2075 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2076 GR_GL_TEXTURE_SWIZZLE_B,
2077 swizzle[2]));
2078 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2079 GR_GL_TEXTURE_SWIZZLE_A,
2080 swizzle[3]));
2081}
2082}
2083
bsalomon@google.comffca4002011-02-22 20:34:01 +00002084bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002085
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002086 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002087 // GrGpu::setupClipAndFlushState should have already checked this
2088 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002089 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002090
tomhudson@google.com93813632011-10-27 20:21:16 +00002091 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002092 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002093 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002094 GrGLTexture* nextTexture =
2095 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002096
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002097 // true for now, but maybe not with GrEffect.
2098 GrAssert(NULL != nextTexture);
2099 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002100 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002101 // the last bound texture, but it needs resolving. So keep this
2102 // out of the "last != next" check.
2103 GrGLRenderTarget* texRT =
2104 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2105 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002106 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002107 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002108
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002109 if (fHWBoundTextures[s] != nextTexture) {
2110 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002111 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002112 #if GR_COLLECT_STATS
2113 ++fStats.fTextureChngCnt;
2114 #endif
2115 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002116 fHWBoundTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002117 // The texture matrix has to compensate for texture width/height
2118 // and NPOT-embedded-in-POT
2119 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002120 }
2121
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002122 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002123 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002124 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002125 nextTexture->getCachedTexParams(&timestamp);
2126 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002127 GrGLTexture::TexParams newTexParams;
2128
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002129 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002130
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002131 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002132 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2133 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002134 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002135 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002136 sizeof(newTexParams.fSwizzleRGBA));
2137 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002138 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002139 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002140 GR_GL_TEXTURE_MAG_FILTER,
2141 newTexParams.fFilter));
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_MIN_FILTER,
2144 newTexParams.fFilter));
2145 }
2146 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
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_S,
2150 newTexParams.fWrapS));
2151 }
2152 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002153 this->setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002154 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002155 GR_GL_TEXTURE_WRAP_T,
2156 newTexParams.fWrapT));
2157 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002158 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002159 (setAll ||
2160 memcmp(newTexParams.fSwizzleRGBA,
2161 oldTexParams.fSwizzleRGBA,
2162 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002163 this->setTextureUnit(s);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002164 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2165 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002166 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002167 nextTexture->setCachedTexParams(newTexParams,
2168 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002169 }
2170 }
2171
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002172 GrIRect* rect = NULL;
2173 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002174 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002175 fClip.hasConservativeBounds()) {
2176 fClip.getConservativeBounds().roundOut(&clipBounds);
2177 rect = &clipBounds;
2178 }
2179 this->flushRenderTarget(rect);
2180 this->flushAAState(type);
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002181
2182 if (drawState->isDitherState()) {
2183 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002184 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002185 fHWDitherEnabled = kYes_TriState;
2186 }
2187 } else {
2188 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002189 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002190 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002191 }
2192 }
2193
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002194 if (drawState->isColorWriteDisabled()) {
2195 if (kNo_TriState != fHWWriteToColor) {
2196 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2197 GR_GL_FALSE, GR_GL_FALSE));
2198 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002199 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002200 } else {
2201 if (kYes_TriState != fHWWriteToColor) {
2202 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2203 fHWWriteToColor = kYes_TriState;
2204 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002205 }
2206
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002207 if (fHWDrawFace != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002208 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002209 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002210 GL_CALL(Enable(GR_GL_CULL_FACE));
2211 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002212 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002213 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002214 GL_CALL(Enable(GR_GL_CULL_FACE));
2215 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002216 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002217 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002218 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002219 break;
2220 default:
2221 GrCrash("Unknown draw face.");
2222 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002223 fHWDrawFace = drawState->getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002224 }
2225
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002226#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002227 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002228 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002229 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002230 NULL == drawState->getRenderTarget() ||
2231 NULL == drawState->getTexture(s) ||
2232 drawState->getTexture(s)->asRenderTarget() !=
2233 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002234 }
2235#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002236
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002237 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002238
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002239 // This copy must happen after flushStencil() is called. flushStencil()
2240 // relies on detecting when the kModifyStencilClip_StateBit state has
2241 // changed since the last draw.
2242 fHWDrawState.copyStateFlags(*drawState);
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002243
2244 // TODO: may no longer need this
robertphillips@google.com1942c052012-05-03 17:58:27 +00002245 // only GrInOrderDrawBuffer ever needs to ref/unref the textures
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002246 fHWDrawState.disableBehavior(GrDrawState::kTexturesNeedRef_BehaviorBit);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002247 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002248}
2249
2250void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002251 if (fHWGeometryState.fVertexBuffer != buffer) {
2252 fHWGeometryState.fArrayPtrsDirty = true;
2253 fHWGeometryState.fVertexBuffer = buffer;
2254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002255}
2256
2257void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002258 if (fHWGeometryState.fVertexBuffer == buffer) {
2259 // deleting bound buffer does implied bind to 0
2260 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002261 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002262 }
2263}
2264
2265void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002266 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002267}
2268
2269void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002270 if (fHWGeometryState.fIndexBuffer == buffer) {
2271 // deleting bound buffer does implied bind to 0
2272 fHWGeometryState.fIndexBuffer = NULL;
2273 }
2274}
2275
reed@google.comac10a2d2010-12-22 21:39:39 +00002276void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2277 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002278 GrDrawState* drawState = this->drawState();
2279 if (drawState->getRenderTarget() == renderTarget) {
2280 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002282 if (fHWBoundRenderTarget == renderTarget) {
2283 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002284 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002285}
2286
2287void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002288 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002289 GrDrawState* drawState = this->drawState();
2290 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002291 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002292 }
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002293 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002294 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002295 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002296 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002297 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002298}
2299
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002300bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2301 bool getSizedInternalFormat,
2302 GrGLenum* internalFormat,
2303 GrGLenum* externalFormat,
2304 GrGLenum* externalType) {
2305 GrGLenum dontCare;
2306 if (NULL == internalFormat) {
2307 internalFormat = &dontCare;
2308 }
2309 if (NULL == externalFormat) {
2310 externalFormat = &dontCare;
2311 }
2312 if (NULL == externalType) {
2313 externalType = &dontCare;
2314 }
2315
reed@google.comac10a2d2010-12-22 21:39:39 +00002316 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002317 case kRGBA_8888_PM_GrPixelConfig:
2318 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002319 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002320 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002321 if (getSizedInternalFormat) {
2322 *internalFormat = GR_GL_RGBA8;
2323 } else {
2324 *internalFormat = GR_GL_RGBA;
2325 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002326 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002327 break;
2328 case kBGRA_8888_PM_GrPixelConfig:
2329 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002330 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002331 return false;
2332 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002333 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002334 if (getSizedInternalFormat) {
2335 *internalFormat = GR_GL_BGRA8;
2336 } else {
2337 *internalFormat = GR_GL_BGRA;
2338 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002339 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002340 if (getSizedInternalFormat) {
2341 *internalFormat = GR_GL_RGBA8;
2342 } else {
2343 *internalFormat = GR_GL_RGBA;
2344 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002345 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002346 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002347 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002348 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002349 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002350 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002351 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002352 if (getSizedInternalFormat) {
2353 if (this->glBinding() == kDesktop_GrGLBinding) {
2354 return false;
2355 } else {
2356 *internalFormat = GR_GL_RGB565;
2357 }
2358 } else {
2359 *internalFormat = GR_GL_RGB;
2360 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002361 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002362 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002363 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002364 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002365 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002366 if (getSizedInternalFormat) {
2367 *internalFormat = GR_GL_RGBA4;
2368 } else {
2369 *internalFormat = GR_GL_RGBA;
2370 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002371 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002372 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002373 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002374 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002375 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002376 // glCompressedTexImage doesn't take external params
2377 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002378 // no sized/unsized internal format distinction here
2379 *internalFormat = GR_GL_PALETTE8_RGBA8;
2380 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002381 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002382 } else {
2383 return false;
2384 }
2385 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002386 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002387 if (this->glCaps().textureRedSupport()) {
2388 *internalFormat = GR_GL_RED;
2389 *externalFormat = GR_GL_RED;
2390 if (getSizedInternalFormat) {
2391 *internalFormat = GR_GL_R8;
2392 } else {
2393 *internalFormat = GR_GL_RED;
2394 }
2395 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002396 } else {
2397 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002398 *externalFormat = GR_GL_ALPHA;
2399 if (getSizedInternalFormat) {
2400 *internalFormat = GR_GL_ALPHA8;
2401 } else {
2402 *internalFormat = GR_GL_ALPHA;
2403 }
2404 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002405 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002406 break;
2407 default:
2408 return false;
2409 }
2410 return true;
2411}
2412
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002413void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002414 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002415 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002416 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002417 fActiveTextureUnitIdx = unit;
2418 }
2419}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002420
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002421void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002422 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002423 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002424 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2425 }
2426}
2427
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002428void GrGpuGL::resetDirtyFlags() {
2429 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2430}
2431
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002432void GrGpuGL::setBuffers(bool indexed,
2433 int* extraVertexOffset,
2434 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002435
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002436 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002437
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002438 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2439
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002440 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002441 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002442 case kBuffer_GeometrySrcType:
2443 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002444 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 break;
2446 case kArray_GeometrySrcType:
2447 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002448 this->finalizeReservedVertices();
2449 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2450 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002451 break;
2452 default:
2453 vbuf = NULL; // suppress warning
2454 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002455 }
2456
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002457 GrAssert(NULL != vbuf);
2458 GrAssert(!vbuf->isLocked());
2459 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002460 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002461 fHWGeometryState.fArrayPtrsDirty = true;
2462 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002463 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002464
2465 if (indexed) {
2466 GrAssert(NULL != extraIndexOffset);
2467
2468 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002469 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002470 case kBuffer_GeometrySrcType:
2471 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002472 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002473 break;
2474 case kArray_GeometrySrcType:
2475 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002476 this->finalizeReservedIndices();
2477 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2478 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002479 break;
2480 default:
2481 ibuf = NULL; // suppress warning
2482 GrCrash("Unknown geometry src type!");
2483 }
2484
2485 GrAssert(NULL != ibuf);
2486 GrAssert(!ibuf->isLocked());
2487 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002488 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002489 fHWGeometryState.fIndexBuffer = ibuf;
2490 }
2491 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002492}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002493