blob: 805d5134e86ecdb8dc339408cff79d666e6104e0 [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.com0b77d682011-08-19 13:28:54 +0000463 GL_CALL(Disable(GR_GL_CULL_FACE));
464 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000465 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000467 GL_CALL(Disable(GR_GL_DITHER));
468 if (kDesktop_GrGLBinding == this->glBinding()) {
469 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
470 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
471 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000472 fHWAAState.fMSAAEnabled = false;
473 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000474 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000475
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000476 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000477 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000478
reed@google.comac10a2d2010-12-22 21:39:39 +0000479 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000480 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000481
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000482 // invalid
483 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000484
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000485 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000486
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000487 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000488
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000489 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000490
tomhudson@google.com93813632011-10-27 20:21:16 +0000491 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000492 fHWDrawState.setTexture(s, NULL);
493 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
494 -GR_ScalarMax,
495 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000496 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000497 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000498 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000499
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000500 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000501 // set to true to force disableScissor to make a GL call.
502 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000503 this->disableScissor();
504
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000505 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000506
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000507 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 fHWStencilClip = false;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000509
510 // TODO: I believe this should actually go in GrGpu::onResetContext
511 // rather than here
512 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000513
514 fHWGeometryState.fIndexBuffer = NULL;
515 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000516
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000517 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000518
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000519 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000520 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000521
522 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000523 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000524 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
525 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000526 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000527 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
528 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000529 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000530 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
531 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000532 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000533 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
534 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000535}
536
bsalomon@google.come269f212011-11-07 13:29:52 +0000537GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000538 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000539 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000540 return NULL;
541 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000542
bsalomon@google.com99621082011-11-15 16:47:16 +0000543 glTexDesc.fWidth = desc.fWidth;
544 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000545 glTexDesc.fConfig = desc.fConfig;
546 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
547 glTexDesc.fOwnsID = false;
548 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
549
550 GrGLTexture* texture = NULL;
551 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
552 GrGLRenderTarget::Desc glRTDesc;
553 glRTDesc.fRTFBOID = 0;
554 glRTDesc.fTexFBOID = 0;
555 glRTDesc.fMSColorRenderbufferID = 0;
556 glRTDesc.fOwnIDs = true;
557 glRTDesc.fConfig = desc.fConfig;
558 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000559 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
560 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000561 glTexDesc.fTextureID,
562 &glRTDesc)) {
563 return NULL;
564 }
565 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
566 } else {
567 texture = new GrGLTexture(this, glTexDesc);
568 }
569 if (NULL == texture) {
570 return NULL;
571 }
572
573 this->setSpareTextureUnit();
574 return texture;
575}
576
577GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
578 GrGLRenderTarget::Desc glDesc;
579 glDesc.fConfig = desc.fConfig;
580 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
581 glDesc.fMSColorRenderbufferID = 0;
582 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
583 glDesc.fSampleCnt = desc.fSampleCnt;
584 glDesc.fOwnIDs = false;
585 GrGLIRect viewport;
586 viewport.fLeft = 0;
587 viewport.fBottom = 0;
588 viewport.fWidth = desc.fWidth;
589 viewport.fHeight = desc.fHeight;
590
591 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
592 if (desc.fStencilBits) {
593 GrGLStencilBuffer::Format format;
594 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
595 format.fPacked = false;
596 format.fStencilBits = desc.fStencilBits;
597 format.fTotalBits = desc.fStencilBits;
598 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
599 0,
600 desc.fWidth,
601 desc.fHeight,
602 desc.fSampleCnt,
603 format);
604 tgt->setStencilBuffer(sb);
605 sb->unref();
606 }
607 return tgt;
608}
609
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000610////////////////////////////////////////////////////////////////////////////////
611
bsalomon@google.com6f379512011-11-16 20:36:03 +0000612void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
613 int left, int top, int width, int height,
614 GrPixelConfig config, const void* buffer,
615 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000616 if (NULL == buffer) {
617 return;
618 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000619 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
620
bsalomon@google.com6f379512011-11-16 20:36:03 +0000621 this->setSpareTextureUnit();
622 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
623 GrGLTexture::Desc desc;
624 desc.fConfig = glTex->config();
625 desc.fWidth = glTex->width();
626 desc.fHeight = glTex->height();
627 desc.fOrientation = glTex->orientation();
628 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000629
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000630 this->uploadTexData(desc, false,
631 left, top, width, height,
632 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000633}
634
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000635namespace {
636bool adjust_pixel_ops_params(int surfaceWidth,
637 int surfaceHeight,
638 size_t bpp,
639 int* left, int* top, int* width, int* height,
640 const void** data,
641 size_t* rowBytes) {
642 if (!*rowBytes) {
643 *rowBytes = *width * bpp;
644 }
645
646 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
647 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
648
649 if (!subRect.intersect(bounds)) {
650 return false;
651 }
652 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
653 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
654
655 *left = subRect.fLeft;
656 *top = subRect.fTop;
657 *width = subRect.width();
658 *height = subRect.height();
659 return true;
660}
661}
662
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000663bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
664 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000665 int left, int top, int width, int height,
666 GrPixelConfig dataConfig,
667 const void* data,
668 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000669 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000670
671 size_t bpp = GrBytesPerPixel(dataConfig);
672 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
673 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000674 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000675 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000676 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000677
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000678 // in case we need a temporary, trimmed copy of the src pixels
679 SkAutoSMalloc<128 * 128> tempStorage;
680
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000681 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000682 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000683 if (useTexStorage) {
684 if (kDesktop_GrGLBinding == this->glBinding()) {
685 // 565 is not a sized internal format on desktop GL. So on desktop
686 // with 565 we always use an unsized internal format to let the
687 // system pick the best sized format to convert the 565 data to.
688 // Since glTexStorage only allows sized internal formats we will
689 // instead fallback to glTexImage2D.
690 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
691 } else {
692 // ES doesn't allow paletted textures to be used with tex storage
693 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
694 }
695 }
696
697 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000698 GrGLenum externalFormat;
699 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000700 // glTexStorage requires sized internal formats on both desktop and ES. ES
701 // glTexImage requires an unsized format.
702 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
703 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000704 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000705 }
706
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000707 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000708 // paletted textures cannot be updated
709 return false;
710 }
711
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000712 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000713 * check whether to allocate a temporary buffer for flipping y or
714 * because our srcData has extra bytes past each row. If so, we need
715 * to trim those off here, since GL ES may not let us specify
716 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000717 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000718 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000719 bool swFlipY = false;
720 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000721 if (NULL != data) {
722 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000723 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000724 glFlipY = true;
725 } else {
726 swFlipY = true;
727 }
728 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000729 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000730 // can't use this for flipping, only non-neg values allowed. :(
731 if (rowBytes != trimRowBytes) {
732 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
733 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
734 restoreGLRowLength = true;
735 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000736 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000737 if (trimRowBytes != rowBytes || swFlipY) {
738 // copy data into our new storage, skipping the trailing bytes
739 size_t trimSize = height * trimRowBytes;
740 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000741 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000742 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000743 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000744 char* dst = (char*)tempStorage.reset(trimSize);
745 for (int y = 0; y < height; y++) {
746 memcpy(dst, src, trimRowBytes);
747 if (swFlipY) {
748 src -= rowBytes;
749 } else {
750 src += rowBytes;
751 }
752 dst += trimRowBytes;
753 }
754 // now point data to our copied version
755 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000756 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000757 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000758 if (glFlipY) {
759 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
760 }
761 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000762 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000763 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000764 if (isNewTexture &&
765 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000766 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000767 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000768 if (useTexStorage) {
769 // We never resize or change formats of textures. We don't use
770 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000771 GL_ALLOC_CALL(this->glInterface(),
772 TexStorage2D(GR_GL_TEXTURE_2D,
773 1, // levels
774 internalFormat,
775 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000776 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000777 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
778 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
779 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000780 GL_ALLOC_CALL(this->glInterface(),
781 CompressedTexImage2D(GR_GL_TEXTURE_2D,
782 0, // level
783 internalFormat,
784 desc.fWidth, desc.fHeight,
785 0, // border
786 imageSize,
787 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000788 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000789 GL_ALLOC_CALL(this->glInterface(),
790 TexImage2D(GR_GL_TEXTURE_2D,
791 0, // level
792 internalFormat,
793 desc.fWidth, desc.fHeight,
794 0, // border
795 externalFormat, externalType,
796 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000797 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000798 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000799 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000800 if (error != GR_GL_NO_ERROR) {
801 succeeded = false;
802 } else {
803 // if we have data and we used TexStorage to create the texture, we
804 // now upload with TexSubImage.
805 if (NULL != data && useTexStorage) {
806 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
807 0, // level
808 left, top,
809 width, height,
810 externalFormat, externalType,
811 data));
812 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000813 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000814 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000815 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000816 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000817 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000818 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
819 0, // level
820 left, top,
821 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000822 externalFormat, externalType, data));
823 }
824
825 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000826 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000827 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000828 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000829 if (glFlipY) {
830 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
831 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000832 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000833}
834
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000835namespace {
836bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
837 int sampleCount,
838 GrGLenum format,
839 int width, int height) {
840 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
841 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
842 bool created = false;
843 if (GrGLCaps::kNVDesktop_CoverageAAType ==
844 ctxInfo.caps().coverageAAType()) {
845 const GrGLCaps::MSAACoverageMode& mode =
846 ctxInfo.caps().getMSAACoverageMode(sampleCount);
847 GL_ALLOC_CALL(ctxInfo.interface(),
848 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
849 mode.fCoverageSampleCnt,
850 mode.fColorSampleCnt,
851 format,
852 width, height));
853 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
854 }
855 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000856 // glRBMS will fail if requested samples is > max samples.
857 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000858 GL_ALLOC_CALL(ctxInfo.interface(),
859 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
860 sampleCount,
861 format,
862 width, height));
863 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
864 }
865 return created;
866}
867}
868
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000869bool GrGpuGL::createRenderTargetObjects(int width, int height,
870 GrGLuint texID,
871 GrGLRenderTarget::Desc* desc) {
872 desc->fMSColorRenderbufferID = 0;
873 desc->fRTFBOID = 0;
874 desc->fTexFBOID = 0;
875 desc->fOwnIDs = true;
876
877 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000878
bsalomon@google.comab15d612011-08-09 12:57:56 +0000879 GrGLenum msColorFormat = 0; // suppress warning
880
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000881 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000882 if (!desc->fTexFBOID) {
883 goto FAILED;
884 }
885
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000886
887 // If we are using multisampling we will create two FBOS. We render
888 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000889 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000890 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000891 goto FAILED;
892 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000893 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
894 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000895 if (!desc->fRTFBOID ||
896 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000897 !this->configToGLFormats(desc->fConfig,
898 // GLES requires sized internal formats
899 kES2_GrGLBinding == this->glBinding(),
900 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000901 goto FAILED;
902 }
903 } else {
904 desc->fRTFBOID = desc->fTexFBOID;
905 }
906
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000907 // below here we may bind the FBO
908 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 if (desc->fRTFBOID != desc->fTexFBOID) {
910 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000911 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000913 if (!renderbuffer_storage_msaa(fGLContextInfo,
914 desc->fSampleCnt,
915 msColorFormat,
916 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917 goto FAILED;
918 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
920 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921 GR_GL_COLOR_ATTACHMENT0,
922 GR_GL_RENDERBUFFER,
923 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000924 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000925 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
926 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
927 goto FAILED;
928 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000929 fGLContextInfo.caps().markConfigAsValidColorAttachment(
930 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000931 }
932 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000933 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000934
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
936 GR_GL_COLOR_ATTACHMENT0,
937 GR_GL_TEXTURE_2D,
938 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000939 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000940 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
941 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
942 goto FAILED;
943 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000944 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945 }
946
947 return true;
948
949FAILED:
950 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000951 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000952 }
953 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000954 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000955 }
956 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000957 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000958 }
959 return false;
960}
961
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000962// good to set a break-point here to know when createTexture fails
963static GrTexture* return_null_texture() {
964// GrAssert(!"null texture");
965 return NULL;
966}
967
968#if GR_DEBUG
969static size_t as_size_t(int x) {
970 return x;
971}
972#endif
973
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000974GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000975 const void* srcData,
976 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000977
978#if GR_COLLECT_STATS
979 ++fStats.fTextureCreateCnt;
980#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000981
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000982 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000983 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000984
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000985 // Attempt to catch un- or wrongly initialized sample counts;
986 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
987
bsalomon@google.com99621082011-11-15 16:47:16 +0000988 glTexDesc.fWidth = desc.fWidth;
989 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000990 glTexDesc.fConfig = desc.fConfig;
991 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000992
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000993 glRTDesc.fMSColorRenderbufferID = 0;
994 glRTDesc.fRTFBOID = 0;
995 glRTDesc.fTexFBOID = 0;
996 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000997 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000998
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000999 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001000
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001001 const Caps& caps = this->getCaps();
1002
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001003 // We keep GrRenderTargets in GL's normal orientation so that they
1004 // can be drawn to by the outside world without the client having
1005 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001006 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001008
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001009 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001010 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001011 desc.fSampleCnt) {
1012 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 }
1014
reed@google.comac10a2d2010-12-22 21:39:39 +00001015 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001016 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1017 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001018 return return_null_texture();
1019 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001020 }
1021
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001022 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001023 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001024 // provides a hint about how this texture will be used
1025 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1026 GR_GL_TEXTURE_USAGE,
1027 GR_GL_FRAMEBUFFER_ATTACHMENT));
1028 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001029 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001030 return return_null_texture();
1031 }
1032
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001033 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001034 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001035
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001036 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1037 // drivers have a bug where an FBO won't be complete if it includes a
1038 // texture that is not mipmap complete (considering the filter in use).
1039 GrGLTexture::TexParams initialTexParams;
1040 // we only set a subset here so invalidate first
1041 initialTexParams.invalidate();
1042 initialTexParams.fFilter = GR_GL_NEAREST;
1043 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1044 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001045 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1046 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001047 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1049 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001050 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001051 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1052 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001053 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001054 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1055 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001056 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001057 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1058 glTexDesc.fWidth, glTexDesc.fHeight,
1059 desc.fConfig, srcData, rowBytes)) {
1060 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1061 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001062 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001063
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001064 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 if (renderTarget) {
1066#if GR_COLLECT_STATS
1067 ++fStats.fRenderTargetCreateCnt;
1068#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001069 // unbind the texture from the texture unit before binding it to the frame buffer
1070 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1071
bsalomon@google.com99621082011-11-15 16:47:16 +00001072 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1073 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 glTexDesc.fTextureID,
1075 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001076 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 return return_null_texture();
1078 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001079 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001080 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001081 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001082 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001083 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001084#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001085 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1086 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001087#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 return tex;
1089}
1090
1091namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001092
1093const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1094
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001095void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1096 GrGLuint rb,
1097 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001098 // we shouldn't ever know one size and not the other
1099 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1100 (kUnknownBitCount == format->fTotalBits));
1101 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001102 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1104 (GrGLint*)&format->fStencilBits);
1105 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001106 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001107 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1108 (GrGLint*)&format->fTotalBits);
1109 format->fTotalBits += format->fStencilBits;
1110 } else {
1111 format->fTotalBits = format->fStencilBits;
1112 }
1113 }
1114}
1115}
1116
1117bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1118 int width, int height) {
1119
1120 // All internally created RTs are also textures. We don't create
1121 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1122 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001123 GrAssert(width >= rt->width());
1124 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125
1126 int samples = rt->numSamples();
1127 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001128 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001129 if (!sbID) {
1130 return false;
1131 }
1132
1133 GrGLStencilBuffer* sb = NULL;
1134
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001135 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001137 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138 // we start with the last stencil format that succeeded in hopes
1139 // that we won't go through this loop more than once after the
1140 // first (painful) stencil creation.
1141 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001142 const GrGLCaps::StencilFormat& sFmt =
1143 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001144 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001145 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001147 bool created;
1148 if (samples > 0) {
1149 created = renderbuffer_storage_msaa(fGLContextInfo,
1150 samples,
1151 sFmt.fInternalFormat,
1152 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001153 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001154 GL_ALLOC_CALL(this->glInterface(),
1155 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001156 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001157 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001158 created =
1159 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001160 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001161 if (created) {
1162 // After sized formats we attempt an unsized format and take
1163 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001164 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001165 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001166 sb = new GrGLStencilBuffer(this, sbID, width, height,
1167 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1169 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001170 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172 return true;
1173 }
1174 sb->abandon(); // otherwise we lose sbID
1175 sb->unref();
1176 }
1177 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001178 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001179 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180}
1181
1182bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1183 GrRenderTarget* rt) {
1184 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1185
1186 GrGLuint fbo = glrt->renderFBOID();
1187
1188 if (NULL == sb) {
1189 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001190 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 GR_GL_STENCIL_ATTACHMENT,
1192 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001193 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 GR_GL_DEPTH_ATTACHMENT,
1195 GR_GL_RENDERBUFFER, 0));
1196#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001197 GrGLenum status;
1198 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001199 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1200#endif
1201 }
1202 return true;
1203 } else {
1204 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1205 GrGLuint rb = glsb->renderbufferID();
1206
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001207 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001208 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1209 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 GR_GL_STENCIL_ATTACHMENT,
1211 GR_GL_RENDERBUFFER, rb));
1212 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001214 GR_GL_DEPTH_ATTACHMENT,
1215 GR_GL_RENDERBUFFER, rb));
1216 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 GR_GL_DEPTH_ATTACHMENT,
1219 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001220 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001222 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001223 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001224 glsb->format())) {
1225 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1226 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001228 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001230 if (glsb->format().fPacked) {
1231 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1232 GR_GL_DEPTH_ATTACHMENT,
1233 GR_GL_RENDERBUFFER, 0));
1234 }
1235 return false;
1236 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001237 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001238 rt->config(),
1239 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001242 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001243 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001244}
1245
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001246////////////////////////////////////////////////////////////////////////////////
1247
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001248GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001249 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001251 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001252 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001253 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001254 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001255 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001256 GL_ALLOC_CALL(this->glInterface(),
1257 BufferData(GR_GL_ARRAY_BUFFER,
1258 size,
1259 NULL, // data ptr
1260 dynamic ? GR_GL_DYNAMIC_DRAW :
1261 GR_GL_STATIC_DRAW));
1262 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 // deleting bound buffer does implicit bind to 0
1265 fHWGeometryState.fVertexBuffer = NULL;
1266 return NULL;
1267 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001268 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 size, dynamic);
1270 fHWGeometryState.fVertexBuffer = vertexBuffer;
1271 return vertexBuffer;
1272 }
1273 return NULL;
1274}
1275
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001276GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001277 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001278 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001280 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001281 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001283 GL_ALLOC_CALL(this->glInterface(),
1284 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1285 size,
1286 NULL, // data ptr
1287 dynamic ? GR_GL_DYNAMIC_DRAW :
1288 GR_GL_STATIC_DRAW));
1289 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001290 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 // deleting bound buffer does implicit bind to 0
1292 fHWGeometryState.fIndexBuffer = NULL;
1293 return NULL;
1294 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001295 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 size, dynamic);
1297 fHWGeometryState.fIndexBuffer = indexBuffer;
1298 return indexBuffer;
1299 }
1300 return NULL;
1301}
1302
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001303void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001304 const GrDrawState& drawState = this->getDrawState();
1305 const GrGLRenderTarget* rt =
1306 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1307
1308 GrAssert(NULL != rt);
1309 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001310
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001311 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001312 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1313 rect.width(), rect.height());
1314 if (scissor.contains(vp)) {
1315 disableScissor();
1316 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 }
1318
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001319 if (fHWBounds.fScissorRect != scissor) {
1320 scissor.pushToGLScissor(this->glInterface());
1321 fHWBounds.fScissorRect = scissor;
1322 }
1323 if (!fHWBounds.fScissorEnabled) {
1324 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1325 fHWBounds.fScissorEnabled = true;
1326 }
1327}
1328
1329void GrGpuGL::disableScissor() {
1330 if (fHWBounds.fScissorEnabled) {
1331 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1332 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 }
1334}
1335
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001336void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001337 const GrDrawState& drawState = this->getDrawState();
1338 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001339 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001340 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001341
bsalomon@google.com74b98712011-11-11 19:46:16 +00001342 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001343 if (NULL != rect) {
1344 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001345 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001346 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001347 if (clippedRect.intersect(rtRect)) {
1348 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001349 } else {
1350 return;
1351 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001353 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001354 if (NULL != rect)
1355 this->enableScissoring(*rect);
1356 else
1357 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001358
1359 GrGLfloat r, g, b, a;
1360 static const GrGLfloat scale255 = 1.f / 255.f;
1361 a = GrColorUnpackA(color) * scale255;
1362 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001363 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001364 scaleRGB *= a;
1365 }
1366 r = GrColorUnpackR(color) * scaleRGB;
1367 g = GrColorUnpackG(color) * scaleRGB;
1368 b = GrColorUnpackB(color) * scaleRGB;
1369
1370 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001371 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001372 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001373 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001374}
1375
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001376void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001377 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001378 return;
1379 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001380
1381 this->flushRenderTarget(&GrIRect::EmptyIRect());
1382
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001383 this->disableScissor();
1384
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001385 GL_CALL(StencilMask(0xffffffff));
1386 GL_CALL(ClearStencil(0));
1387 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001388 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001389}
1390
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001391void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001392 const GrDrawState& drawState = this->getDrawState();
1393 const GrRenderTarget* rt = drawState.getRenderTarget();
1394 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001395
1396 // this should only be called internally when we know we have a
1397 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001398 GrAssert(NULL != rt->getStencilBuffer());
1399 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001400#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001401 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001402 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001403#else
1404 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001405 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001406 // turned into draws. Our contract on GrDrawTarget says that
1407 // changing the clip between stencil passes may or may not
1408 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001409 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001410#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001411 GrGLint value;
1412 if (insideClip) {
1413 value = (1 << (stencilBitCount - 1));
1414 } else {
1415 value = 0;
1416 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001417 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001418 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001419 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001420 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001421 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001422 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001423}
1424
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001425void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001426 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001427}
1428
bsalomon@google.comc4364992011-11-07 15:54:49 +00001429bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1430 int left, int top,
1431 int width, int height,
1432 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001433 size_t rowBytes) const {
1434 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001435 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001436 return false;
1437 }
1438
1439 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001440 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001441 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001442 return true;
1443 }
1444 // If we have to do memcpys to handle rowBytes then y-flip is free
1445 // Note the rowBytes might be tight to the passed in data, but if data
1446 // gets clipped in x to the target the rowBytes will no longer be tight.
1447 if (left >= 0 && (left + width) < renderTarget->width()) {
1448 return 0 == rowBytes ||
1449 GrBytesPerPixel(config) * width == rowBytes;
1450 } else {
1451 return false;
1452 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001453}
1454
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001455bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001456 int left, int top,
1457 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001458 GrPixelConfig config,
1459 void* buffer,
1460 size_t rowBytes,
1461 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001462 GrGLenum format;
1463 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001464 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001466 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001467 size_t bpp = GrBytesPerPixel(config);
1468 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1469 &left, &top, &width, &height,
1470 const_cast<const void**>(&buffer),
1471 &rowBytes)) {
1472 return false;
1473 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001474
bsalomon@google.comc6980972011-11-02 19:57:21 +00001475 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001476 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001477 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001478 switch (tgt->getResolveType()) {
1479 case GrGLRenderTarget::kCantResolve_ResolveType:
1480 return false;
1481 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001482 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001483 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001484 break;
1485 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001486 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001487 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001488 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1489 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001490 break;
1491 default:
1492 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001493 }
1494
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001495 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001496
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001497 // the read rect is viewport-relative
1498 GrGLIRect readRect;
1499 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001500
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001501 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001502 if (0 == rowBytes) {
1503 rowBytes = tightRowBytes;
1504 }
1505 size_t readDstRowBytes = tightRowBytes;
1506 void* readDst = buffer;
1507
1508 // determine if GL can read using the passed rowBytes or if we need
1509 // a scratch buffer.
1510 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1511 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001512 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001513 GrAssert(!(rowBytes % sizeof(GrColor)));
1514 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1515 readDstRowBytes = rowBytes;
1516 } else {
1517 scratch.reset(tightRowBytes * height);
1518 readDst = scratch.get();
1519 }
1520 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001521 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001522 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1523 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001524 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1525 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001526 format, type, readDst));
1527 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001528 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001529 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1530 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001531 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001532 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1533 invertY = true;
1534 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001535
1536 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537 // API presents top-to-bottom. We must preserve the padding contents. Note
1538 // that the above readPixels did not overwrite the padding.
1539 if (readDst == buffer) {
1540 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001541 if (!invertY) {
1542 scratch.reset(tightRowBytes);
1543 void* tmpRow = scratch.get();
1544 // flip y in-place by rows
1545 const int halfY = height >> 1;
1546 char* top = reinterpret_cast<char*>(buffer);
1547 char* bottom = top + (height - 1) * rowBytes;
1548 for (int y = 0; y < halfY; y++) {
1549 memcpy(tmpRow, top, tightRowBytes);
1550 memcpy(top, bottom, tightRowBytes);
1551 memcpy(bottom, tmpRow, tightRowBytes);
1552 top += rowBytes;
1553 bottom -= rowBytes;
1554 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001555 }
1556 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001557 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001558 // copy from readDst to buffer while flipping y
1559 const int halfY = height >> 1;
1560 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001561 char* dst = reinterpret_cast<char*>(buffer);
1562 if (!invertY) {
1563 dst += (height-1) * rowBytes;
1564 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001565 for (int y = 0; y < height; y++) {
1566 memcpy(dst, src, tightRowBytes);
1567 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001568 if (invertY) {
1569 dst += rowBytes;
1570 } else {
1571 dst -= rowBytes;
1572 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001573 }
1574 }
1575 return true;
1576}
1577
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001578void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001579
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001580 GrGLRenderTarget* rt =
1581 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1582 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001583
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001584 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001585 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001586 #if GR_COLLECT_STATS
1587 ++fStats.fRenderTargetChngCnt;
1588 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001589 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001590 GrGLenum status;
1591 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001592 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001593 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001594 }
1595 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001596 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001597 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001598 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001599 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001600 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001601 fHWBounds.fViewportRect = vp;
1602 }
1603 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001604 if (NULL == bound || !bound->isEmpty()) {
1605 rt->flagAsNeedingResolve(bound);
1606 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001607}
1608
twiz@google.com0f31ca72011-03-18 17:38:11 +00001609GrGLenum gPrimitiveType2GLMode[] = {
1610 GR_GL_TRIANGLES,
1611 GR_GL_TRIANGLE_STRIP,
1612 GR_GL_TRIANGLE_FAN,
1613 GR_GL_POINTS,
1614 GR_GL_LINES,
1615 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001616};
1617
bsalomon@google.comd302f142011-03-03 13:54:13 +00001618#define SWAP_PER_DRAW 0
1619
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001620#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001621 #if GR_MAC_BUILD
1622 #include <AGL/agl.h>
1623 #elif GR_WIN32_BUILD
1624 void SwapBuf() {
1625 DWORD procID = GetCurrentProcessId();
1626 HWND hwnd = GetTopWindow(GetDesktopWindow());
1627 while(hwnd) {
1628 DWORD wndProcID = 0;
1629 GetWindowThreadProcessId(hwnd, &wndProcID);
1630 if(wndProcID == procID) {
1631 SwapBuffers(GetDC(hwnd));
1632 }
1633 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1634 }
1635 }
1636 #endif
1637#endif
1638
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001639void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1640 uint32_t startVertex,
1641 uint32_t startIndex,
1642 uint32_t vertexCount,
1643 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1645
twiz@google.com0f31ca72011-03-18 17:38:11 +00001646 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001647
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001648 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1649 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1650
1651 // our setupGeometry better have adjusted this to zero since
1652 // DrawElements always draws from the begining of the arrays for idx 0.
1653 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001654
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001655 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1656 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001657#if SWAP_PER_DRAW
1658 glFlush();
1659 #if GR_MAC_BUILD
1660 aglSwapBuffers(aglGetCurrentContext());
1661 int set_a_break_pt_here = 9;
1662 aglSwapBuffers(aglGetCurrentContext());
1663 #elif GR_WIN32_BUILD
1664 SwapBuf();
1665 int set_a_break_pt_here = 9;
1666 SwapBuf();
1667 #endif
1668#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001669}
1670
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001671void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1672 uint32_t startVertex,
1673 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001674 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1675
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001676 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1677
1678 // our setupGeometry better have adjusted this to zero.
1679 // DrawElements doesn't take an offset so we always adjus the startVertex.
1680 GrAssert(0 == startVertex);
1681
1682 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1683 // account for startVertex in the DrawElements case. So we always
1684 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001685 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001686#if SWAP_PER_DRAW
1687 glFlush();
1688 #if GR_MAC_BUILD
1689 aglSwapBuffers(aglGetCurrentContext());
1690 int set_a_break_pt_here = 9;
1691 aglSwapBuffers(aglGetCurrentContext());
1692 #elif GR_WIN32_BUILD
1693 SwapBuf();
1694 int set_a_break_pt_here = 9;
1695 SwapBuf();
1696 #endif
1697#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001698}
1699
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001700void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1701
1702 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001703
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001704 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001705 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001706 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001707 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1708 rt->renderFBOID()));
1709 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1710 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001711 #if GR_COLLECT_STATS
1712 ++fStats.fRenderTargetChngCnt;
1713 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001714 // make sure we go through flushRenderTarget() since we've modified
1715 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001716 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001717 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001718 const GrIRect dirtyRect = rt->getResolveRect();
1719 GrGLIRect r;
1720 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1721 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001722
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001723 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001724 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001725#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001726 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1727 GL_CALL(Scissor(r.fLeft, r.fBottom,
1728 r.fWidth, r.fHeight));
1729 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001730 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001731 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001732#else
1733 this->enableScissoring(dirtyRect);
1734 GL_CALL(ResolveMultisampleFramebuffer());
1735#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001737 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001738 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001739 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1740 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001741 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001742 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001743 int right = r.fLeft + r.fWidth;
1744 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001745 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1746 r.fLeft, r.fBottom, right, top,
1747 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001748 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001749 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001750 }
1751}
1752
twiz@google.com0f31ca72011-03-18 17:38:11 +00001753static const GrGLenum grToGLStencilFunc[] = {
1754 GR_GL_ALWAYS, // kAlways_StencilFunc
1755 GR_GL_NEVER, // kNever_StencilFunc
1756 GR_GL_GREATER, // kGreater_StencilFunc
1757 GR_GL_GEQUAL, // kGEqual_StencilFunc
1758 GR_GL_LESS, // kLess_StencilFunc
1759 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1760 GR_GL_EQUAL, // kEqual_StencilFunc,
1761 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001762};
1763GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1764GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1765GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1766GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1767GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1768GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1769GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1770GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1771GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1772
twiz@google.com0f31ca72011-03-18 17:38:11 +00001773static const GrGLenum grToGLStencilOp[] = {
1774 GR_GL_KEEP, // kKeep_StencilOp
1775 GR_GL_REPLACE, // kReplace_StencilOp
1776 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1777 GR_GL_INCR, // kIncClamp_StencilOp
1778 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1779 GR_GL_DECR, // kDecClamp_StencilOp
1780 GR_GL_ZERO, // kZero_StencilOp
1781 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001782};
1783GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1784GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1785GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1786GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1787GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1788GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1789GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1790GR_STATIC_ASSERT(6 == kZero_StencilOp);
1791GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1792
reed@google.comac10a2d2010-12-22 21:39:39 +00001793void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001794 const GrDrawState& drawState = this->getDrawState();
1795
1796 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001797
1798 // use stencil for clipping if clipping is enabled and the clip
1799 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001800 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001801 bool drawClipToStencil =
1802 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001803 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1804 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001805 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1806 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001807
1808 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001809
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001810 // we can't simultaneously perform stencil-clipping and
1811 // modify the stencil clip
1812 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001813
bsalomon@google.comd302f142011-03-03 13:54:13 +00001814 if (settings->isDisabled()) {
1815 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001816 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001817 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001818 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001819
1820 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001821 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001823 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001824 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001825 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001826 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1827 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1828 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1829 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1830 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1831 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1832 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1833 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001834 }
1835 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001836 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001837 GrStencilBuffer* stencilBuffer =
1838 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001839 if (NULL != stencilBuffer) {
1840 stencilBits = stencilBuffer->bits();
1841 }
1842 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001843 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001844
1845 GrGLuint clipStencilMask = 0;
1846 GrGLuint userStencilMask = ~0;
1847 if (stencilBits > 0) {
1848 clipStencilMask = 1 << (stencilBits - 1);
1849 userStencilMask = clipStencilMask - 1;
1850 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001851
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001852 unsigned int frontRef = settings->frontFuncRef();
1853 unsigned int frontMask = settings->frontFuncMask();
1854 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001855 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001856
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001857 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001858 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1859 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001860 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001861 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001862 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001864 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001865 stencilClip,
1866 clipStencilMask,
1867 userStencilMask,
1868 &frontRef,
1869 &frontMask);
1870 frontWriteMask &= userStencilMask;
1871 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001872 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001873 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001874 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001875 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001876 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001877 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001878 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001879 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001880 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001881 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001882
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001883 unsigned int backRef = settings->backFuncRef();
1884 unsigned int backMask = settings->backFuncMask();
1885 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001886
1887
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001888 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001889 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1890 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001891 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001892 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001893 stencilClip, settings->backFunc())];
1894 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001895 stencilClip,
1896 clipStencilMask,
1897 userStencilMask,
1898 &backRef,
1899 &backMask);
1900 backWriteMask &= userStencilMask;
1901 }
1902
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001903 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1904 frontRef, frontMask));
1905 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1906 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1907 backRef, backMask));
1908 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1909 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001910 grToGLStencilOp[settings->frontFailOp()],
1911 grToGLStencilOp[settings->frontPassOp()],
1912 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001913
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001914 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001915 grToGLStencilOp[settings->backFailOp()],
1916 grToGLStencilOp[settings->backPassOp()],
1917 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001918 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001919 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1920 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001921 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1922 grToGLStencilOp[settings->frontPassOp()],
1923 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001924 }
1925 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001926 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001927 fHWStencilClip = stencilClip;
1928 }
1929}
1930
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001931void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001932 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001933 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001934 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1935 // smooth lines.
1936
1937 // we prefer smooth lines over multisampled lines
1938 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001939 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001940 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001941 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001942 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001943 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001944 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001945 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001946 fHWAAState.fSmoothLineEnabled = false;
1947 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001948 if (rt->isMultisampled() &&
1949 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001950 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001951 fHWAAState.fMSAAEnabled = false;
1952 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001953 } else if (rt->isMultisampled() &&
1954 this->getDrawState().isHWAntialiasState() !=
1955 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001957 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001958 fHWAAState.fMSAAEnabled = false;
1959 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001960 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001961 fHWAAState.fMSAAEnabled = true;
1962 }
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.com8f9cbd62011-12-09 15:55:34 +00002109 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002110 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.com8f9cbd62011-12-09 15:55:34 +00002116 fHWDrawState.setTexture(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.coma47a48d2011-04-26 20:22:11 +00002138 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) {
2147 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) {
2153 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)))) {
2163 setTextureUnit(s);
2164 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.com0650e812011-04-08 18:07:53 +00002181
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002182 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2183 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002184 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002185 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002186 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002187 }
2188 }
2189
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002190 if (drawState->isColorWriteDisabled() !=
2191 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002192 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002193 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002194 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002195 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002196 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002197 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002198 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002199 }
2200
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002201 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002202 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002203 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002204 GL_CALL(Enable(GR_GL_CULL_FACE));
2205 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002206 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002207 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002208 GL_CALL(Enable(GR_GL_CULL_FACE));
2209 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002210 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002211 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002212 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002213 break;
2214 default:
2215 GrCrash("Unknown draw face.");
2216 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002217 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002218 }
2219
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002220#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002221 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002222 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002223 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002224 NULL == drawState->getRenderTarget() ||
2225 NULL == drawState->getTexture(s) ||
2226 drawState->getTexture(s)->asRenderTarget() !=
2227 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002228 }
2229#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002230
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002231 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002232
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002233 // This copy must happen after flushStencil() is called. flushStencil()
2234 // relies on detecting when the kModifyStencilClip_StateBit state has
2235 // changed since the last draw.
2236 fHWDrawState.copyStateFlags(*drawState);
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002237
2238 // TODO: may no longer need this
robertphillips@google.com1942c052012-05-03 17:58:27 +00002239 // only GrInOrderDrawBuffer ever needs to ref/unref the textures
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002240 fHWDrawState.disableBehavior(GrDrawState::kTexturesNeedRef_BehaviorBit);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002241 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002242}
2243
2244void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002245 if (fHWGeometryState.fVertexBuffer != buffer) {
2246 fHWGeometryState.fArrayPtrsDirty = true;
2247 fHWGeometryState.fVertexBuffer = buffer;
2248 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002249}
2250
2251void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002252 if (fHWGeometryState.fVertexBuffer == buffer) {
2253 // deleting bound buffer does implied bind to 0
2254 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002255 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002256 }
2257}
2258
2259void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002260 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002261}
2262
2263void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002264 if (fHWGeometryState.fIndexBuffer == buffer) {
2265 // deleting bound buffer does implied bind to 0
2266 fHWGeometryState.fIndexBuffer = NULL;
2267 }
2268}
2269
reed@google.comac10a2d2010-12-22 21:39:39 +00002270void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2271 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002272 GrDrawState* drawState = this->drawState();
2273 if (drawState->getRenderTarget() == renderTarget) {
2274 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002275 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002276 if (fHWDrawState.getRenderTarget() == renderTarget) {
2277 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002278 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002279}
2280
2281void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002282 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002283 GrDrawState* drawState = this->drawState();
2284 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002285 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002286 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002287 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002288 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002289 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002290 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002291 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002292}
2293
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002294bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2295 bool getSizedInternalFormat,
2296 GrGLenum* internalFormat,
2297 GrGLenum* externalFormat,
2298 GrGLenum* externalType) {
2299 GrGLenum dontCare;
2300 if (NULL == internalFormat) {
2301 internalFormat = &dontCare;
2302 }
2303 if (NULL == externalFormat) {
2304 externalFormat = &dontCare;
2305 }
2306 if (NULL == externalType) {
2307 externalType = &dontCare;
2308 }
2309
reed@google.comac10a2d2010-12-22 21:39:39 +00002310 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002311 case kRGBA_8888_PM_GrPixelConfig:
2312 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002313 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002314 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002315 if (getSizedInternalFormat) {
2316 *internalFormat = GR_GL_RGBA8;
2317 } else {
2318 *internalFormat = GR_GL_RGBA;
2319 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002320 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002321 break;
2322 case kBGRA_8888_PM_GrPixelConfig:
2323 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002324 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002325 return false;
2326 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002327 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002328 if (getSizedInternalFormat) {
2329 *internalFormat = GR_GL_BGRA8;
2330 } else {
2331 *internalFormat = GR_GL_BGRA;
2332 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002333 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002334 if (getSizedInternalFormat) {
2335 *internalFormat = GR_GL_RGBA8;
2336 } else {
2337 *internalFormat = GR_GL_RGBA;
2338 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002339 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002340 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002341 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002342 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002343 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002344 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002345 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002346 if (getSizedInternalFormat) {
2347 if (this->glBinding() == kDesktop_GrGLBinding) {
2348 return false;
2349 } else {
2350 *internalFormat = GR_GL_RGB565;
2351 }
2352 } else {
2353 *internalFormat = GR_GL_RGB;
2354 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002355 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002356 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002357 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002358 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002359 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002360 if (getSizedInternalFormat) {
2361 *internalFormat = GR_GL_RGBA4;
2362 } else {
2363 *internalFormat = GR_GL_RGBA;
2364 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002365 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002366 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002367 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002368 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002369 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002370 // glCompressedTexImage doesn't take external params
2371 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002372 // no sized/unsized internal format distinction here
2373 *internalFormat = GR_GL_PALETTE8_RGBA8;
2374 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002375 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002376 } else {
2377 return false;
2378 }
2379 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002380 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002381 if (this->glCaps().textureRedSupport()) {
2382 *internalFormat = GR_GL_RED;
2383 *externalFormat = GR_GL_RED;
2384 if (getSizedInternalFormat) {
2385 *internalFormat = GR_GL_R8;
2386 } else {
2387 *internalFormat = GR_GL_RED;
2388 }
2389 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002390 } else {
2391 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002392 *externalFormat = GR_GL_ALPHA;
2393 if (getSizedInternalFormat) {
2394 *internalFormat = GR_GL_ALPHA8;
2395 } else {
2396 *internalFormat = GR_GL_ALPHA;
2397 }
2398 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002399 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002400 break;
2401 default:
2402 return false;
2403 }
2404 return true;
2405}
2406
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002407void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002408 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002409 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002410 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002411 fActiveTextureUnitIdx = unit;
2412 }
2413}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002414
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002415void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002416 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002417 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002418 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2419 }
2420}
2421
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002422void GrGpuGL::resetDirtyFlags() {
2423 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2424}
2425
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002426void GrGpuGL::setBuffers(bool indexed,
2427 int* extraVertexOffset,
2428 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002429
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002430 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002431
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002432 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2433
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002435 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002436 case kBuffer_GeometrySrcType:
2437 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002438 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439 break;
2440 case kArray_GeometrySrcType:
2441 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002442 this->finalizeReservedVertices();
2443 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2444 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 break;
2446 default:
2447 vbuf = NULL; // suppress warning
2448 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002449 }
2450
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002451 GrAssert(NULL != vbuf);
2452 GrAssert(!vbuf->isLocked());
2453 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002454 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002455 fHWGeometryState.fArrayPtrsDirty = true;
2456 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002457 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002458
2459 if (indexed) {
2460 GrAssert(NULL != extraIndexOffset);
2461
2462 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002463 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002464 case kBuffer_GeometrySrcType:
2465 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002466 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002467 break;
2468 case kArray_GeometrySrcType:
2469 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002470 this->finalizeReservedIndices();
2471 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2472 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002473 break;
2474 default:
2475 ibuf = NULL; // suppress warning
2476 GrCrash("Unknown geometry src type!");
2477 }
2478
2479 GrAssert(NULL != ibuf);
2480 GrAssert(!ibuf->isLocked());
2481 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002482 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002483 fHWGeometryState.fIndexBuffer = ibuf;
2484 }
2485 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002486}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002487