blob: 2966f59633358779a4e7a8fb2fdf272c6fb08f68 [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.coma7f84e12011-03-10 14:13:19 +0000459 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000460 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000461 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000463 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000464 GL_CALL(Disable(GR_GL_DEPTH_TEST));
465 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000466
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000467 GL_CALL(Disable(GR_GL_CULL_FACE));
468 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000469 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000471 GL_CALL(Disable(GR_GL_DITHER));
472 if (kDesktop_GrGLBinding == this->glBinding()) {
473 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
474 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
475 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000476 fHWAAState.fMSAAEnabled = false;
477 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000478 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000479
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000480 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000481 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000482
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000484 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000486 // invalid
487 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000488
reed@google.comac10a2d2010-12-22 21:39:39 +0000489 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000490 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000491
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000492 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000493 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000494
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000495 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000496
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000497 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000498
tomhudson@google.com93813632011-10-27 20:21:16 +0000499 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000500 fHWDrawState.setTexture(s, NULL);
501 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
502 -GR_ScalarMax,
503 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000504 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000505 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000506 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000507
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000508 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000509 // set to true to force disableScissor to make a GL call.
510 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000511 this->disableScissor();
512
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000513 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000514
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000515 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000516 fHWStencilClip = false;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000517
518 // TODO: I believe this should actually go in GrGpu::onResetContext
519 // rather than here
520 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000521
522 fHWGeometryState.fIndexBuffer = NULL;
523 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000524
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000525 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000526
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000527 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000528 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000529
530 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000531 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000532 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
533 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000534 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000535 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
536 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000537 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000538 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
539 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000540 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000541 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
542 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000543}
544
bsalomon@google.come269f212011-11-07 13:29:52 +0000545GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000546 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000547 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000548 return NULL;
549 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000550
bsalomon@google.com99621082011-11-15 16:47:16 +0000551 glTexDesc.fWidth = desc.fWidth;
552 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000553 glTexDesc.fConfig = desc.fConfig;
554 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
555 glTexDesc.fOwnsID = false;
556 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
557
558 GrGLTexture* texture = NULL;
559 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
560 GrGLRenderTarget::Desc glRTDesc;
561 glRTDesc.fRTFBOID = 0;
562 glRTDesc.fTexFBOID = 0;
563 glRTDesc.fMSColorRenderbufferID = 0;
564 glRTDesc.fOwnIDs = true;
565 glRTDesc.fConfig = desc.fConfig;
566 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000567 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
568 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000569 glTexDesc.fTextureID,
570 &glRTDesc)) {
571 return NULL;
572 }
573 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
574 } else {
575 texture = new GrGLTexture(this, glTexDesc);
576 }
577 if (NULL == texture) {
578 return NULL;
579 }
580
581 this->setSpareTextureUnit();
582 return texture;
583}
584
585GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
586 GrGLRenderTarget::Desc glDesc;
587 glDesc.fConfig = desc.fConfig;
588 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
589 glDesc.fMSColorRenderbufferID = 0;
590 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
591 glDesc.fSampleCnt = desc.fSampleCnt;
592 glDesc.fOwnIDs = false;
593 GrGLIRect viewport;
594 viewport.fLeft = 0;
595 viewport.fBottom = 0;
596 viewport.fWidth = desc.fWidth;
597 viewport.fHeight = desc.fHeight;
598
599 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
600 if (desc.fStencilBits) {
601 GrGLStencilBuffer::Format format;
602 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
603 format.fPacked = false;
604 format.fStencilBits = desc.fStencilBits;
605 format.fTotalBits = desc.fStencilBits;
606 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
607 0,
608 desc.fWidth,
609 desc.fHeight,
610 desc.fSampleCnt,
611 format);
612 tgt->setStencilBuffer(sb);
613 sb->unref();
614 }
615 return tgt;
616}
617
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000618////////////////////////////////////////////////////////////////////////////////
619
bsalomon@google.com6f379512011-11-16 20:36:03 +0000620void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
621 int left, int top, int width, int height,
622 GrPixelConfig config, const void* buffer,
623 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000624 if (NULL == buffer) {
625 return;
626 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000627 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
628
bsalomon@google.com6f379512011-11-16 20:36:03 +0000629 this->setSpareTextureUnit();
630 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
631 GrGLTexture::Desc desc;
632 desc.fConfig = glTex->config();
633 desc.fWidth = glTex->width();
634 desc.fHeight = glTex->height();
635 desc.fOrientation = glTex->orientation();
636 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000637
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000638 this->uploadTexData(desc, false,
639 left, top, width, height,
640 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000641}
642
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000643namespace {
644bool adjust_pixel_ops_params(int surfaceWidth,
645 int surfaceHeight,
646 size_t bpp,
647 int* left, int* top, int* width, int* height,
648 const void** data,
649 size_t* rowBytes) {
650 if (!*rowBytes) {
651 *rowBytes = *width * bpp;
652 }
653
654 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
655 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
656
657 if (!subRect.intersect(bounds)) {
658 return false;
659 }
660 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
661 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
662
663 *left = subRect.fLeft;
664 *top = subRect.fTop;
665 *width = subRect.width();
666 *height = subRect.height();
667 return true;
668}
669}
670
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000671bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
672 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000673 int left, int top, int width, int height,
674 GrPixelConfig dataConfig,
675 const void* data,
676 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000677 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000678
679 size_t bpp = GrBytesPerPixel(dataConfig);
680 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
681 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000682 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000683 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000684 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000685
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000686 // in case we need a temporary, trimmed copy of the src pixels
687 SkAutoSMalloc<128 * 128> tempStorage;
688
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000689 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000690 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000691 if (useTexStorage) {
692 if (kDesktop_GrGLBinding == this->glBinding()) {
693 // 565 is not a sized internal format on desktop GL. So on desktop
694 // with 565 we always use an unsized internal format to let the
695 // system pick the best sized format to convert the 565 data to.
696 // Since glTexStorage only allows sized internal formats we will
697 // instead fallback to glTexImage2D.
698 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
699 } else {
700 // ES doesn't allow paletted textures to be used with tex storage
701 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
702 }
703 }
704
705 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000706 GrGLenum externalFormat;
707 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000708 // glTexStorage requires sized internal formats on both desktop and ES. ES
709 // glTexImage requires an unsized format.
710 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
711 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000712 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000713 }
714
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000715 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000716 // paletted textures cannot be updated
717 return false;
718 }
719
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000720 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000721 * check whether to allocate a temporary buffer for flipping y or
722 * because our srcData has extra bytes past each row. If so, we need
723 * to trim those off here, since GL ES may not let us specify
724 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000725 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000726 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000727 bool swFlipY = false;
728 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000729 if (NULL != data) {
730 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000731 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000732 glFlipY = true;
733 } else {
734 swFlipY = true;
735 }
736 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000737 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000738 // can't use this for flipping, only non-neg values allowed. :(
739 if (rowBytes != trimRowBytes) {
740 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
741 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
742 restoreGLRowLength = true;
743 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000744 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000745 if (trimRowBytes != rowBytes || swFlipY) {
746 // copy data into our new storage, skipping the trailing bytes
747 size_t trimSize = height * trimRowBytes;
748 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000749 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000750 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000751 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000752 char* dst = (char*)tempStorage.reset(trimSize);
753 for (int y = 0; y < height; y++) {
754 memcpy(dst, src, trimRowBytes);
755 if (swFlipY) {
756 src -= rowBytes;
757 } else {
758 src += rowBytes;
759 }
760 dst += trimRowBytes;
761 }
762 // now point data to our copied version
763 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000764 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000765 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000766 if (glFlipY) {
767 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
768 }
769 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000771 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000772 if (isNewTexture &&
773 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000774 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000775 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000776 if (useTexStorage) {
777 // We never resize or change formats of textures. We don't use
778 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000779 GL_ALLOC_CALL(this->glInterface(),
780 TexStorage2D(GR_GL_TEXTURE_2D,
781 1, // levels
782 internalFormat,
783 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000784 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000785 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
786 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
787 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000788 GL_ALLOC_CALL(this->glInterface(),
789 CompressedTexImage2D(GR_GL_TEXTURE_2D,
790 0, // level
791 internalFormat,
792 desc.fWidth, desc.fHeight,
793 0, // border
794 imageSize,
795 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000796 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000797 GL_ALLOC_CALL(this->glInterface(),
798 TexImage2D(GR_GL_TEXTURE_2D,
799 0, // level
800 internalFormat,
801 desc.fWidth, desc.fHeight,
802 0, // border
803 externalFormat, externalType,
804 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000805 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000806 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000807 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000808 if (error != GR_GL_NO_ERROR) {
809 succeeded = false;
810 } else {
811 // if we have data and we used TexStorage to create the texture, we
812 // now upload with TexSubImage.
813 if (NULL != data && useTexStorage) {
814 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
815 0, // level
816 left, top,
817 width, height,
818 externalFormat, externalType,
819 data));
820 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000821 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000822 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000823 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000824 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000825 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000826 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
827 0, // level
828 left, top,
829 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000830 externalFormat, externalType, data));
831 }
832
833 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000834 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000835 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000836 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000837 if (glFlipY) {
838 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
839 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000840 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000841}
842
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000843namespace {
844bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
845 int sampleCount,
846 GrGLenum format,
847 int width, int height) {
848 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
849 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
850 bool created = false;
851 if (GrGLCaps::kNVDesktop_CoverageAAType ==
852 ctxInfo.caps().coverageAAType()) {
853 const GrGLCaps::MSAACoverageMode& mode =
854 ctxInfo.caps().getMSAACoverageMode(sampleCount);
855 GL_ALLOC_CALL(ctxInfo.interface(),
856 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
857 mode.fCoverageSampleCnt,
858 mode.fColorSampleCnt,
859 format,
860 width, height));
861 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
862 }
863 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000864 // glRBMS will fail if requested samples is > max samples.
865 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000866 GL_ALLOC_CALL(ctxInfo.interface(),
867 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
868 sampleCount,
869 format,
870 width, height));
871 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
872 }
873 return created;
874}
875}
876
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000877bool GrGpuGL::createRenderTargetObjects(int width, int height,
878 GrGLuint texID,
879 GrGLRenderTarget::Desc* desc) {
880 desc->fMSColorRenderbufferID = 0;
881 desc->fRTFBOID = 0;
882 desc->fTexFBOID = 0;
883 desc->fOwnIDs = true;
884
885 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000886
bsalomon@google.comab15d612011-08-09 12:57:56 +0000887 GrGLenum msColorFormat = 0; // suppress warning
888
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000889 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000890 if (!desc->fTexFBOID) {
891 goto FAILED;
892 }
893
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000894
895 // If we are using multisampling we will create two FBOS. We render
896 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000897 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000898 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000899 goto FAILED;
900 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000901 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
902 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000903 if (!desc->fRTFBOID ||
904 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000905 !this->configToGLFormats(desc->fConfig,
906 // GLES requires sized internal formats
907 kES2_GrGLBinding == this->glBinding(),
908 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 goto FAILED;
910 }
911 } else {
912 desc->fRTFBOID = desc->fTexFBOID;
913 }
914
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000915 // below here we may bind the FBO
916 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917 if (desc->fRTFBOID != desc->fTexFBOID) {
918 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000920 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000921 if (!renderbuffer_storage_msaa(fGLContextInfo,
922 desc->fSampleCnt,
923 msColorFormat,
924 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000925 goto FAILED;
926 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
928 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 GR_GL_COLOR_ATTACHMENT0,
930 GR_GL_RENDERBUFFER,
931 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000932 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000933 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
934 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
935 goto FAILED;
936 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000937 fGLContextInfo.caps().markConfigAsValidColorAttachment(
938 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 }
940 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000941 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000943 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
944 GR_GL_COLOR_ATTACHMENT0,
945 GR_GL_TEXTURE_2D,
946 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000947 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000948 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
949 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
950 goto FAILED;
951 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000952 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000953 }
954
955 return true;
956
957FAILED:
958 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000959 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000960 }
961 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000962 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000963 }
964 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000966 }
967 return false;
968}
969
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000970// good to set a break-point here to know when createTexture fails
971static GrTexture* return_null_texture() {
972// GrAssert(!"null texture");
973 return NULL;
974}
975
976#if GR_DEBUG
977static size_t as_size_t(int x) {
978 return x;
979}
980#endif
981
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000982GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000983 const void* srcData,
984 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000985
986#if GR_COLLECT_STATS
987 ++fStats.fTextureCreateCnt;
988#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000989
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000990 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000991 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000992
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000993 // Attempt to catch un- or wrongly initialized sample counts;
994 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
995
bsalomon@google.com99621082011-11-15 16:47:16 +0000996 glTexDesc.fWidth = desc.fWidth;
997 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000998 glTexDesc.fConfig = desc.fConfig;
999 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001000
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001001 glRTDesc.fMSColorRenderbufferID = 0;
1002 glRTDesc.fRTFBOID = 0;
1003 glRTDesc.fTexFBOID = 0;
1004 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001005 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001006
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001007 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001008
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001009 const Caps& caps = this->getCaps();
1010
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001011 // We keep GrRenderTargets in GL's normal orientation so that they
1012 // can be drawn to by the outside world without the client having
1013 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001014 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001015 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001016
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001017 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001018 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001019 desc.fSampleCnt) {
1020 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001021 }
1022
reed@google.comac10a2d2010-12-22 21:39:39 +00001023 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001024 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1025 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001026 return return_null_texture();
1027 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001028 }
1029
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001030 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001031 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001032 // provides a hint about how this texture will be used
1033 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1034 GR_GL_TEXTURE_USAGE,
1035 GR_GL_FRAMEBUFFER_ATTACHMENT));
1036 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001037 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001038 return return_null_texture();
1039 }
1040
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001041 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001042 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001043
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001044 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1045 // drivers have a bug where an FBO won't be complete if it includes a
1046 // texture that is not mipmap complete (considering the filter in use).
1047 GrGLTexture::TexParams initialTexParams;
1048 // we only set a subset here so invalidate first
1049 initialTexParams.invalidate();
1050 initialTexParams.fFilter = GR_GL_NEAREST;
1051 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1052 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001053 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1054 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001055 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001056 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1057 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001058 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1060 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001061 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001062 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1063 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001064 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001065 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1066 glTexDesc.fWidth, glTexDesc.fHeight,
1067 desc.fConfig, srcData, rowBytes)) {
1068 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1069 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001070 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001071
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001072 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001073 if (renderTarget) {
1074#if GR_COLLECT_STATS
1075 ++fStats.fRenderTargetCreateCnt;
1076#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001077 // unbind the texture from the texture unit before binding it to the frame buffer
1078 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1079
bsalomon@google.com99621082011-11-15 16:47:16 +00001080 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1081 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 glTexDesc.fTextureID,
1083 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001084 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 return return_null_texture();
1086 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001087 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001089 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001090 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001091 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001092#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001093 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1094 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001095#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001096 return tex;
1097}
1098
1099namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001100
1101const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1102
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001103void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1104 GrGLuint rb,
1105 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106 // we shouldn't ever know one size and not the other
1107 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1108 (kUnknownBitCount == format->fTotalBits));
1109 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001110 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001111 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1112 (GrGLint*)&format->fStencilBits);
1113 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001114 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1116 (GrGLint*)&format->fTotalBits);
1117 format->fTotalBits += format->fStencilBits;
1118 } else {
1119 format->fTotalBits = format->fStencilBits;
1120 }
1121 }
1122}
1123}
1124
1125bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1126 int width, int height) {
1127
1128 // All internally created RTs are also textures. We don't create
1129 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1130 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001131 GrAssert(width >= rt->width());
1132 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133
1134 int samples = rt->numSamples();
1135 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001136 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 if (!sbID) {
1138 return false;
1139 }
1140
1141 GrGLStencilBuffer* sb = NULL;
1142
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001143 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001145 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 // we start with the last stencil format that succeeded in hopes
1147 // that we won't go through this loop more than once after the
1148 // first (painful) stencil creation.
1149 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001150 const GrGLCaps::StencilFormat& sFmt =
1151 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001152 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001153 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001155 bool created;
1156 if (samples > 0) {
1157 created = renderbuffer_storage_msaa(fGLContextInfo,
1158 samples,
1159 sFmt.fInternalFormat,
1160 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001162 GL_ALLOC_CALL(this->glInterface(),
1163 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001164 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001165 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001166 created =
1167 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001169 if (created) {
1170 // After sized formats we attempt an unsized format and take
1171 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001172 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001173 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001174 sb = new GrGLStencilBuffer(this, sbID, width, height,
1175 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1177 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001178 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 return true;
1181 }
1182 sb->abandon(); // otherwise we lose sbID
1183 sb->unref();
1184 }
1185 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001186 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001187 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188}
1189
1190bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1191 GrRenderTarget* rt) {
1192 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1193
1194 GrGLuint fbo = glrt->renderFBOID();
1195
1196 if (NULL == sb) {
1197 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001198 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001199 GR_GL_STENCIL_ATTACHMENT,
1200 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 GR_GL_DEPTH_ATTACHMENT,
1203 GR_GL_RENDERBUFFER, 0));
1204#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001205 GrGLenum status;
1206 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1208#endif
1209 }
1210 return true;
1211 } else {
1212 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1213 GrGLuint rb = glsb->renderbufferID();
1214
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001215 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 GR_GL_STENCIL_ATTACHMENT,
1219 GR_GL_RENDERBUFFER, rb));
1220 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 GR_GL_DEPTH_ATTACHMENT,
1223 GR_GL_RENDERBUFFER, rb));
1224 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 GR_GL_DEPTH_ATTACHMENT,
1227 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001230 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001231 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001232 glsb->format())) {
1233 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1234 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001235 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001236 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001238 if (glsb->format().fPacked) {
1239 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1240 GR_GL_DEPTH_ATTACHMENT,
1241 GR_GL_RENDERBUFFER, 0));
1242 }
1243 return false;
1244 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001245 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001246 rt->config(),
1247 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001250 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001251 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001252}
1253
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001254////////////////////////////////////////////////////////////////////////////////
1255
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001256GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001257 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001258 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001259 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001260 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001261 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001262 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001264 GL_ALLOC_CALL(this->glInterface(),
1265 BufferData(GR_GL_ARRAY_BUFFER,
1266 size,
1267 NULL, // data ptr
1268 dynamic ? GR_GL_DYNAMIC_DRAW :
1269 GR_GL_STATIC_DRAW));
1270 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001271 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 // deleting bound buffer does implicit bind to 0
1273 fHWGeometryState.fVertexBuffer = NULL;
1274 return NULL;
1275 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001276 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 size, dynamic);
1278 fHWGeometryState.fVertexBuffer = vertexBuffer;
1279 return vertexBuffer;
1280 }
1281 return NULL;
1282}
1283
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001284GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001285 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001286 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001289 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001291 GL_ALLOC_CALL(this->glInterface(),
1292 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1293 size,
1294 NULL, // data ptr
1295 dynamic ? GR_GL_DYNAMIC_DRAW :
1296 GR_GL_STATIC_DRAW));
1297 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001298 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 // deleting bound buffer does implicit bind to 0
1300 fHWGeometryState.fIndexBuffer = NULL;
1301 return NULL;
1302 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001303 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 size, dynamic);
1305 fHWGeometryState.fIndexBuffer = indexBuffer;
1306 return indexBuffer;
1307 }
1308 return NULL;
1309}
1310
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001311void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001312 const GrDrawState& drawState = this->getDrawState();
1313 const GrGLRenderTarget* rt =
1314 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1315
1316 GrAssert(NULL != rt);
1317 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001318
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001319 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001320 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1321 rect.width(), rect.height());
1322 if (scissor.contains(vp)) {
1323 disableScissor();
1324 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 }
1326
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001327 if (fHWBounds.fScissorRect != scissor) {
1328 scissor.pushToGLScissor(this->glInterface());
1329 fHWBounds.fScissorRect = scissor;
1330 }
1331 if (!fHWBounds.fScissorEnabled) {
1332 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1333 fHWBounds.fScissorEnabled = true;
1334 }
1335}
1336
1337void GrGpuGL::disableScissor() {
1338 if (fHWBounds.fScissorEnabled) {
1339 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1340 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 }
1342}
1343
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001344void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001345 const GrDrawState& drawState = this->getDrawState();
1346 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001347 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001348 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001349
bsalomon@google.com74b98712011-11-11 19:46:16 +00001350 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001351 if (NULL != rect) {
1352 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001353 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001354 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001355 if (clippedRect.intersect(rtRect)) {
1356 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001357 } else {
1358 return;
1359 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001360 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001361 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001362 if (NULL != rect)
1363 this->enableScissoring(*rect);
1364 else
1365 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001366
1367 GrGLfloat r, g, b, a;
1368 static const GrGLfloat scale255 = 1.f / 255.f;
1369 a = GrColorUnpackA(color) * scale255;
1370 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001371 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001372 scaleRGB *= a;
1373 }
1374 r = GrColorUnpackR(color) * scaleRGB;
1375 g = GrColorUnpackG(color) * scaleRGB;
1376 b = GrColorUnpackB(color) * scaleRGB;
1377
1378 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001379 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001380 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001381 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001382}
1383
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001384void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001385 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001386 return;
1387 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001388
1389 this->flushRenderTarget(&GrIRect::EmptyIRect());
1390
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001391 this->disableScissor();
1392
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001393 GL_CALL(StencilMask(0xffffffff));
1394 GL_CALL(ClearStencil(0));
1395 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001396 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001397}
1398
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001399void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001400 const GrDrawState& drawState = this->getDrawState();
1401 const GrRenderTarget* rt = drawState.getRenderTarget();
1402 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001403
1404 // this should only be called internally when we know we have a
1405 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001406 GrAssert(NULL != rt->getStencilBuffer());
1407 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001408#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001409 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001410 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001411#else
1412 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001413 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001414 // turned into draws. Our contract on GrDrawTarget says that
1415 // changing the clip between stencil passes may or may not
1416 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001417 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001418#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001419 GrGLint value;
1420 if (insideClip) {
1421 value = (1 << (stencilBitCount - 1));
1422 } else {
1423 value = 0;
1424 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001425 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001426 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001427 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001428 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001429 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001430 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001431}
1432
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001433void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001434 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001435}
1436
bsalomon@google.comc4364992011-11-07 15:54:49 +00001437bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1438 int left, int top,
1439 int width, int height,
1440 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001441 size_t rowBytes) const {
1442 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001443 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001444 return false;
1445 }
1446
1447 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001448 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001449 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001450 return true;
1451 }
1452 // If we have to do memcpys to handle rowBytes then y-flip is free
1453 // Note the rowBytes might be tight to the passed in data, but if data
1454 // gets clipped in x to the target the rowBytes will no longer be tight.
1455 if (left >= 0 && (left + width) < renderTarget->width()) {
1456 return 0 == rowBytes ||
1457 GrBytesPerPixel(config) * width == rowBytes;
1458 } else {
1459 return false;
1460 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001461}
1462
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001463bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001464 int left, int top,
1465 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001466 GrPixelConfig config,
1467 void* buffer,
1468 size_t rowBytes,
1469 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001470 GrGLenum format;
1471 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001472 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001473 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001474 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001475 size_t bpp = GrBytesPerPixel(config);
1476 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1477 &left, &top, &width, &height,
1478 const_cast<const void**>(&buffer),
1479 &rowBytes)) {
1480 return false;
1481 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001482
bsalomon@google.comc6980972011-11-02 19:57:21 +00001483 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001484 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001485 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001486 switch (tgt->getResolveType()) {
1487 case GrGLRenderTarget::kCantResolve_ResolveType:
1488 return false;
1489 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001490 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001491 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001492 break;
1493 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001494 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001495 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001496 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1497 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001498 break;
1499 default:
1500 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001501 }
1502
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001503 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001504
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001505 // the read rect is viewport-relative
1506 GrGLIRect readRect;
1507 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001508
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001509 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 if (0 == rowBytes) {
1511 rowBytes = tightRowBytes;
1512 }
1513 size_t readDstRowBytes = tightRowBytes;
1514 void* readDst = buffer;
1515
1516 // determine if GL can read using the passed rowBytes or if we need
1517 // a scratch buffer.
1518 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1519 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001520 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001521 GrAssert(!(rowBytes % sizeof(GrColor)));
1522 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1523 readDstRowBytes = rowBytes;
1524 } else {
1525 scratch.reset(tightRowBytes * height);
1526 readDst = scratch.get();
1527 }
1528 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001529 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001530 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1531 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001532 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1533 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001534 format, type, readDst));
1535 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001536 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1538 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001539 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001540 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1541 invertY = true;
1542 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001543
1544 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001545 // API presents top-to-bottom. We must preserve the padding contents. Note
1546 // that the above readPixels did not overwrite the padding.
1547 if (readDst == buffer) {
1548 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001549 if (!invertY) {
1550 scratch.reset(tightRowBytes);
1551 void* tmpRow = scratch.get();
1552 // flip y in-place by rows
1553 const int halfY = height >> 1;
1554 char* top = reinterpret_cast<char*>(buffer);
1555 char* bottom = top + (height - 1) * rowBytes;
1556 for (int y = 0; y < halfY; y++) {
1557 memcpy(tmpRow, top, tightRowBytes);
1558 memcpy(top, bottom, tightRowBytes);
1559 memcpy(bottom, tmpRow, tightRowBytes);
1560 top += rowBytes;
1561 bottom -= rowBytes;
1562 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001563 }
1564 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001565 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001566 // copy from readDst to buffer while flipping y
1567 const int halfY = height >> 1;
1568 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001569 char* dst = reinterpret_cast<char*>(buffer);
1570 if (!invertY) {
1571 dst += (height-1) * rowBytes;
1572 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001573 for (int y = 0; y < height; y++) {
1574 memcpy(dst, src, tightRowBytes);
1575 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001576 if (invertY) {
1577 dst += rowBytes;
1578 } else {
1579 dst -= rowBytes;
1580 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001581 }
1582 }
1583 return true;
1584}
1585
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001586void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001587
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001588 GrGLRenderTarget* rt =
1589 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1590 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001591
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001592 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001593 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001594 #if GR_COLLECT_STATS
1595 ++fStats.fRenderTargetChngCnt;
1596 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001597 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001598 GrGLenum status;
1599 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001600 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001601 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001602 }
1603 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001604 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001605 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001606 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001607 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001608 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 fHWBounds.fViewportRect = vp;
1610 }
1611 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001612 if (NULL == bound || !bound->isEmpty()) {
1613 rt->flagAsNeedingResolve(bound);
1614 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001615}
1616
twiz@google.com0f31ca72011-03-18 17:38:11 +00001617GrGLenum gPrimitiveType2GLMode[] = {
1618 GR_GL_TRIANGLES,
1619 GR_GL_TRIANGLE_STRIP,
1620 GR_GL_TRIANGLE_FAN,
1621 GR_GL_POINTS,
1622 GR_GL_LINES,
1623 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001624};
1625
bsalomon@google.comd302f142011-03-03 13:54:13 +00001626#define SWAP_PER_DRAW 0
1627
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001628#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001629 #if GR_MAC_BUILD
1630 #include <AGL/agl.h>
1631 #elif GR_WIN32_BUILD
1632 void SwapBuf() {
1633 DWORD procID = GetCurrentProcessId();
1634 HWND hwnd = GetTopWindow(GetDesktopWindow());
1635 while(hwnd) {
1636 DWORD wndProcID = 0;
1637 GetWindowThreadProcessId(hwnd, &wndProcID);
1638 if(wndProcID == procID) {
1639 SwapBuffers(GetDC(hwnd));
1640 }
1641 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1642 }
1643 }
1644 #endif
1645#endif
1646
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001647void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1648 uint32_t startVertex,
1649 uint32_t startIndex,
1650 uint32_t vertexCount,
1651 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001652 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1653
twiz@google.com0f31ca72011-03-18 17:38:11 +00001654 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001655
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001656 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1657 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1658
1659 // our setupGeometry better have adjusted this to zero since
1660 // DrawElements always draws from the begining of the arrays for idx 0.
1661 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001662
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001663 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1664 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001665#if SWAP_PER_DRAW
1666 glFlush();
1667 #if GR_MAC_BUILD
1668 aglSwapBuffers(aglGetCurrentContext());
1669 int set_a_break_pt_here = 9;
1670 aglSwapBuffers(aglGetCurrentContext());
1671 #elif GR_WIN32_BUILD
1672 SwapBuf();
1673 int set_a_break_pt_here = 9;
1674 SwapBuf();
1675 #endif
1676#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001677}
1678
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001679void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1680 uint32_t startVertex,
1681 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001682 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1683
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001684 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1685
1686 // our setupGeometry better have adjusted this to zero.
1687 // DrawElements doesn't take an offset so we always adjus the startVertex.
1688 GrAssert(0 == startVertex);
1689
1690 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1691 // account for startVertex in the DrawElements case. So we always
1692 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001693 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001694#if SWAP_PER_DRAW
1695 glFlush();
1696 #if GR_MAC_BUILD
1697 aglSwapBuffers(aglGetCurrentContext());
1698 int set_a_break_pt_here = 9;
1699 aglSwapBuffers(aglGetCurrentContext());
1700 #elif GR_WIN32_BUILD
1701 SwapBuf();
1702 int set_a_break_pt_here = 9;
1703 SwapBuf();
1704 #endif
1705#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001706}
1707
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001708void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1709
1710 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001711
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001712 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001713 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001715 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1716 rt->renderFBOID()));
1717 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1718 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001719 #if GR_COLLECT_STATS
1720 ++fStats.fRenderTargetChngCnt;
1721 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001722 // make sure we go through flushRenderTarget() since we've modified
1723 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001724 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001725 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001726 const GrIRect dirtyRect = rt->getResolveRect();
1727 GrGLIRect r;
1728 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1729 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001730
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001731 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001732 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001733#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001734 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1735 GL_CALL(Scissor(r.fLeft, r.fBottom,
1736 r.fWidth, r.fHeight));
1737 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001738 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001740#else
1741 this->enableScissoring(dirtyRect);
1742 GL_CALL(ResolveMultisampleFramebuffer());
1743#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001744 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001745 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001746 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001747 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1748 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001749 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001750 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001751 int right = r.fLeft + r.fWidth;
1752 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001753 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1754 r.fLeft, r.fBottom, right, top,
1755 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001757 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001758 }
1759}
1760
twiz@google.com0f31ca72011-03-18 17:38:11 +00001761static const GrGLenum grToGLStencilFunc[] = {
1762 GR_GL_ALWAYS, // kAlways_StencilFunc
1763 GR_GL_NEVER, // kNever_StencilFunc
1764 GR_GL_GREATER, // kGreater_StencilFunc
1765 GR_GL_GEQUAL, // kGEqual_StencilFunc
1766 GR_GL_LESS, // kLess_StencilFunc
1767 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1768 GR_GL_EQUAL, // kEqual_StencilFunc,
1769 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001770};
1771GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1772GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1773GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1774GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1775GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1776GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1777GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1778GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1779GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1780
twiz@google.com0f31ca72011-03-18 17:38:11 +00001781static const GrGLenum grToGLStencilOp[] = {
1782 GR_GL_KEEP, // kKeep_StencilOp
1783 GR_GL_REPLACE, // kReplace_StencilOp
1784 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1785 GR_GL_INCR, // kIncClamp_StencilOp
1786 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1787 GR_GL_DECR, // kDecClamp_StencilOp
1788 GR_GL_ZERO, // kZero_StencilOp
1789 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001790};
1791GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1792GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1793GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1794GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1795GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1796GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1797GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1798GR_STATIC_ASSERT(6 == kZero_StencilOp);
1799GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1800
reed@google.comac10a2d2010-12-22 21:39:39 +00001801void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001802 const GrDrawState& drawState = this->getDrawState();
1803
1804 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001805
1806 // use stencil for clipping if clipping is enabled and the clip
1807 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001808 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001809 bool drawClipToStencil =
1810 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001811 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1812 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001813 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1814 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001815
1816 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001817
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001818 // we can't simultaneously perform stencil-clipping and
1819 // modify the stencil clip
1820 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001821
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822 if (settings->isDisabled()) {
1823 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001824 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001825 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001826 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001827
1828 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001829 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001831 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001832 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001833 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001834 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1835 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1836 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1837 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1838 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1839 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1840 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1841 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001842 }
1843 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001844 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001845 GrStencilBuffer* stencilBuffer =
1846 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001847 if (NULL != stencilBuffer) {
1848 stencilBits = stencilBuffer->bits();
1849 }
1850 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001851 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001852
1853 GrGLuint clipStencilMask = 0;
1854 GrGLuint userStencilMask = ~0;
1855 if (stencilBits > 0) {
1856 clipStencilMask = 1 << (stencilBits - 1);
1857 userStencilMask = clipStencilMask - 1;
1858 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001860 unsigned int frontRef = settings->frontFuncRef();
1861 unsigned int frontMask = settings->frontFuncMask();
1862 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001863 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001865 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001866 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1867 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001868 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001869 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001870 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001871
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001872 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001873 stencilClip,
1874 clipStencilMask,
1875 userStencilMask,
1876 &frontRef,
1877 &frontMask);
1878 frontWriteMask &= userStencilMask;
1879 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001880 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001881 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001882 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001883 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001884 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001885 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001886 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001887 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001888 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001889 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001890
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001891 unsigned int backRef = settings->backFuncRef();
1892 unsigned int backMask = settings->backFuncMask();
1893 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001894
1895
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001896 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001897 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1898 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001899 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001900 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001901 stencilClip, settings->backFunc())];
1902 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001903 stencilClip,
1904 clipStencilMask,
1905 userStencilMask,
1906 &backRef,
1907 &backMask);
1908 backWriteMask &= userStencilMask;
1909 }
1910
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001911 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1912 frontRef, frontMask));
1913 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1914 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1915 backRef, backMask));
1916 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1917 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001918 grToGLStencilOp[settings->frontFailOp()],
1919 grToGLStencilOp[settings->frontPassOp()],
1920 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001921
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001922 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001923 grToGLStencilOp[settings->backFailOp()],
1924 grToGLStencilOp[settings->backPassOp()],
1925 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001926 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001927 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1928 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001929 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1930 grToGLStencilOp[settings->frontPassOp()],
1931 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001932 }
1933 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001934 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001935 fHWStencilClip = stencilClip;
1936 }
1937}
1938
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001939void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001940 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001941 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001942 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1943 // smooth lines.
1944
1945 // we prefer smooth lines over multisampled lines
1946 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001947 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001948 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001949 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001950 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001951 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001952 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001953 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001954 fHWAAState.fSmoothLineEnabled = false;
1955 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001956 if (rt->isMultisampled() &&
1957 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 fHWAAState.fMSAAEnabled = false;
1960 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001961 } else if (rt->isMultisampled() &&
1962 this->getDrawState().isHWAntialiasState() !=
1963 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001964 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001965 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001966 fHWAAState.fMSAAEnabled = false;
1967 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001968 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001969 fHWAAState.fMSAAEnabled = true;
1970 }
1971 }
1972 }
1973}
1974
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001975void GrGpuGL::flushBlend(GrPrimitiveType type,
1976 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001977 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001978 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001979 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001980 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001981 fHWBlendDisabled = false;
1982 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001983 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1984 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001985 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1986 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001987 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001988 }
1989 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001990 // any optimization to disable blending should
1991 // have already been applied and tweaked the coeffs
1992 // to (1, 0).
1993 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1994 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001995 if (fHWBlendDisabled != blendOff) {
1996 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001997 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001998 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001999 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002000 }
2001 fHWBlendDisabled = blendOff;
2002 }
2003 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002004 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2005 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002006 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2007 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002008 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002009 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002010 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002011 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2012 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002013 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002014
2015 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002016 GrColorUnpackR(blendConst) / 255.f,
2017 GrColorUnpackG(blendConst) / 255.f,
2018 GrColorUnpackB(blendConst) / 255.f,
2019 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002020 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002021 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002022 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002023 }
2024 }
2025 }
2026}
2027
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002028namespace {
2029
2030unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002031 switch (filter) {
2032 case GrSamplerState::kBilinear_Filter:
2033 case GrSamplerState::k4x4Downsample_Filter:
2034 return GR_GL_LINEAR;
2035 case GrSamplerState::kNearest_Filter:
2036 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002037 case GrSamplerState::kErode_Filter:
2038 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002039 return GR_GL_NEAREST;
2040 default:
2041 GrAssert(!"Unknown filter type");
2042 return GR_GL_LINEAR;
2043 }
2044}
2045
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002046// get_swizzle is only called from this .cpp so it is OK to inline it here
2047inline const GrGLenum* get_swizzle(GrPixelConfig config,
2048 const GrSamplerState& sampler,
2049 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002050 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002051 if (glCaps.textureRedSupport()) {
2052 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2053 GR_GL_RED, GR_GL_RED };
2054 return gRedSmear;
2055 } else {
2056 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2057 GR_GL_ALPHA, GR_GL_ALPHA };
2058 return gAlphaSmear;
2059 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002060 } else if (sampler.swapsRAndB()) {
2061 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2062 GR_GL_RED, GR_GL_ALPHA };
2063 return gRedBlueSwap;
2064 } else {
2065 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2066 GR_GL_BLUE, GR_GL_ALPHA };
2067 return gStraight;
2068 }
2069}
2070
2071void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2072 // should add texparameteri to interface to make 1 instead of 4 calls here
2073 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2074 GR_GL_TEXTURE_SWIZZLE_R,
2075 swizzle[0]));
2076 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2077 GR_GL_TEXTURE_SWIZZLE_G,
2078 swizzle[1]));
2079 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2080 GR_GL_TEXTURE_SWIZZLE_B,
2081 swizzle[2]));
2082 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2083 GR_GL_TEXTURE_SWIZZLE_A,
2084 swizzle[3]));
2085}
2086}
2087
bsalomon@google.comffca4002011-02-22 20:34:01 +00002088bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002089
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002090 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002091 // GrGpu::setupClipAndFlushState should have already checked this
2092 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002093 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002094
tomhudson@google.com93813632011-10-27 20:21:16 +00002095 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002096 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002097 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002098 GrGLTexture* nextTexture =
2099 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002100
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002101 // true for now, but maybe not with GrEffect.
2102 GrAssert(NULL != nextTexture);
2103 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002104 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002105 // the last bound texture, but it needs resolving. So keep this
2106 // out of the "last != next" check.
2107 GrGLRenderTarget* texRT =
2108 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2109 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002110 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002111 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002112
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002113 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002114 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002115 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002116 #if GR_COLLECT_STATS
2117 ++fStats.fTextureChngCnt;
2118 #endif
2119 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002120 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002121 // The texture matrix has to compensate for texture width/height
2122 // and NPOT-embedded-in-POT
2123 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002124 }
2125
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002126 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002127 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002128 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002129 nextTexture->getCachedTexParams(&timestamp);
2130 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002131 GrGLTexture::TexParams newTexParams;
2132
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002133 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002134
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002135 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002136 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2137 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002138 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002139 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002140 sizeof(newTexParams.fSwizzleRGBA));
2141 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002142 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002143 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002144 GR_GL_TEXTURE_MAG_FILTER,
2145 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002146 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002147 GR_GL_TEXTURE_MIN_FILTER,
2148 newTexParams.fFilter));
2149 }
2150 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2151 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002152 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002153 GR_GL_TEXTURE_WRAP_S,
2154 newTexParams.fWrapS));
2155 }
2156 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2157 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002158 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002159 GR_GL_TEXTURE_WRAP_T,
2160 newTexParams.fWrapT));
2161 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002162 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002163 (setAll ||
2164 memcmp(newTexParams.fSwizzleRGBA,
2165 oldTexParams.fSwizzleRGBA,
2166 sizeof(newTexParams.fSwizzleRGBA)))) {
2167 setTextureUnit(s);
2168 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2169 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002170 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002171 nextTexture->setCachedTexParams(newTexParams,
2172 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002173 }
2174 }
2175
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002176 GrIRect* rect = NULL;
2177 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002178 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002179 fClip.hasConservativeBounds()) {
2180 fClip.getConservativeBounds().roundOut(&clipBounds);
2181 rect = &clipBounds;
2182 }
2183 this->flushRenderTarget(rect);
2184 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002185
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002186 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2187 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002188 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002189 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002190 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002191 }
2192 }
2193
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002194 if (drawState->isColorWriteDisabled() !=
2195 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002196 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002197 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002198 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002199 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002200 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002201 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002202 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002203 }
2204
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002205 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002206 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002207 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002208 GL_CALL(Enable(GR_GL_CULL_FACE));
2209 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002210 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002211 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002212 GL_CALL(Enable(GR_GL_CULL_FACE));
2213 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002214 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002215 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002216 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 break;
2218 default:
2219 GrCrash("Unknown draw face.");
2220 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002221 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002222 }
2223
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002224#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002225 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002226 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002227 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002228 NULL == drawState->getRenderTarget() ||
2229 NULL == drawState->getTexture(s) ||
2230 drawState->getTexture(s)->asRenderTarget() !=
2231 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002232 }
2233#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002234
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002235 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002236
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002237 // This copy must happen after flushStencil() is called. flushStencil()
2238 // relies on detecting when the kModifyStencilClip_StateBit state has
2239 // changed since the last draw.
2240 fHWDrawState.copyStateFlags(*drawState);
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002241
2242 // TODO: may no longer need this
robertphillips@google.com1942c052012-05-03 17:58:27 +00002243 // only GrInOrderDrawBuffer ever needs to ref/unref the textures
robertphillips@google.com28b4bce2012-05-04 16:34:52 +00002244 fHWDrawState.disableBehavior(GrDrawState::kTexturesNeedRef_BehaviorBit);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002245 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002246}
2247
2248void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002249 if (fHWGeometryState.fVertexBuffer != buffer) {
2250 fHWGeometryState.fArrayPtrsDirty = true;
2251 fHWGeometryState.fVertexBuffer = buffer;
2252 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002253}
2254
2255void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002256 if (fHWGeometryState.fVertexBuffer == buffer) {
2257 // deleting bound buffer does implied bind to 0
2258 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002259 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002260 }
2261}
2262
2263void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002264 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002265}
2266
2267void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002268 if (fHWGeometryState.fIndexBuffer == buffer) {
2269 // deleting bound buffer does implied bind to 0
2270 fHWGeometryState.fIndexBuffer = NULL;
2271 }
2272}
2273
reed@google.comac10a2d2010-12-22 21:39:39 +00002274void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2275 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002276 GrDrawState* drawState = this->drawState();
2277 if (drawState->getRenderTarget() == renderTarget) {
2278 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002279 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002280 if (fHWDrawState.getRenderTarget() == renderTarget) {
2281 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002282 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002283}
2284
2285void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002286 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002287 GrDrawState* drawState = this->drawState();
2288 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002289 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002290 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002291 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002292 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002293 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002294 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002296}
2297
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002298bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2299 bool getSizedInternalFormat,
2300 GrGLenum* internalFormat,
2301 GrGLenum* externalFormat,
2302 GrGLenum* externalType) {
2303 GrGLenum dontCare;
2304 if (NULL == internalFormat) {
2305 internalFormat = &dontCare;
2306 }
2307 if (NULL == externalFormat) {
2308 externalFormat = &dontCare;
2309 }
2310 if (NULL == externalType) {
2311 externalType = &dontCare;
2312 }
2313
reed@google.comac10a2d2010-12-22 21:39:39 +00002314 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002315 case kRGBA_8888_PM_GrPixelConfig:
2316 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002317 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002318 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002319 if (getSizedInternalFormat) {
2320 *internalFormat = GR_GL_RGBA8;
2321 } else {
2322 *internalFormat = GR_GL_RGBA;
2323 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002324 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002325 break;
2326 case kBGRA_8888_PM_GrPixelConfig:
2327 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002328 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002329 return false;
2330 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002331 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002332 if (getSizedInternalFormat) {
2333 *internalFormat = GR_GL_BGRA8;
2334 } else {
2335 *internalFormat = GR_GL_BGRA;
2336 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002337 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002338 if (getSizedInternalFormat) {
2339 *internalFormat = GR_GL_RGBA8;
2340 } else {
2341 *internalFormat = GR_GL_RGBA;
2342 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002343 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002344 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002345 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002346 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002347 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002348 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002349 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002350 if (getSizedInternalFormat) {
2351 if (this->glBinding() == kDesktop_GrGLBinding) {
2352 return false;
2353 } else {
2354 *internalFormat = GR_GL_RGB565;
2355 }
2356 } else {
2357 *internalFormat = GR_GL_RGB;
2358 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002359 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002360 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002361 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002362 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002363 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002364 if (getSizedInternalFormat) {
2365 *internalFormat = GR_GL_RGBA4;
2366 } else {
2367 *internalFormat = GR_GL_RGBA;
2368 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002369 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002370 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002371 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002372 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002373 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002374 // glCompressedTexImage doesn't take external params
2375 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002376 // no sized/unsized internal format distinction here
2377 *internalFormat = GR_GL_PALETTE8_RGBA8;
2378 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002379 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002380 } else {
2381 return false;
2382 }
2383 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002384 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002385 if (this->glCaps().textureRedSupport()) {
2386 *internalFormat = GR_GL_RED;
2387 *externalFormat = GR_GL_RED;
2388 if (getSizedInternalFormat) {
2389 *internalFormat = GR_GL_R8;
2390 } else {
2391 *internalFormat = GR_GL_RED;
2392 }
2393 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002394 } else {
2395 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002396 *externalFormat = GR_GL_ALPHA;
2397 if (getSizedInternalFormat) {
2398 *internalFormat = GR_GL_ALPHA8;
2399 } else {
2400 *internalFormat = GR_GL_ALPHA;
2401 }
2402 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002403 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002404 break;
2405 default:
2406 return false;
2407 }
2408 return true;
2409}
2410
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002411void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002412 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002413 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002414 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002415 fActiveTextureUnitIdx = unit;
2416 }
2417}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002418
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002419void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002420 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002421 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002422 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2423 }
2424}
2425
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002426void GrGpuGL::resetDirtyFlags() {
2427 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2428}
2429
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002430void GrGpuGL::setBuffers(bool indexed,
2431 int* extraVertexOffset,
2432 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002433
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002435
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002436 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2437
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002438 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002439 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002440 case kBuffer_GeometrySrcType:
2441 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002442 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443 break;
2444 case kArray_GeometrySrcType:
2445 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002446 this->finalizeReservedVertices();
2447 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2448 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002449 break;
2450 default:
2451 vbuf = NULL; // suppress warning
2452 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002453 }
2454
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002455 GrAssert(NULL != vbuf);
2456 GrAssert(!vbuf->isLocked());
2457 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002458 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459 fHWGeometryState.fArrayPtrsDirty = true;
2460 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002461 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462
2463 if (indexed) {
2464 GrAssert(NULL != extraIndexOffset);
2465
2466 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002467 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002468 case kBuffer_GeometrySrcType:
2469 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002470 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002471 break;
2472 case kArray_GeometrySrcType:
2473 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002474 this->finalizeReservedIndices();
2475 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2476 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002477 break;
2478 default:
2479 ibuf = NULL; // suppress warning
2480 GrCrash("Unknown geometry src type!");
2481 }
2482
2483 GrAssert(NULL != ibuf);
2484 GrAssert(!ibuf->isLocked());
2485 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002486 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002487 fHWGeometryState.fIndexBuffer = ibuf;
2488 }
2489 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002490}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002491