blob: 9abd01c966f2f16a209a01719e8b167f5084b4da [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
31#else
32 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000037
38///////////////////////////////////////////////////////////////////////////////
39
twiz@google.com0f31ca72011-03-18 17:38:11 +000040static const GrGLenum gXfermodeCoeff2Blend[] = {
41 GR_GL_ZERO,
42 GR_GL_ONE,
43 GR_GL_SRC_COLOR,
44 GR_GL_ONE_MINUS_SRC_COLOR,
45 GR_GL_DST_COLOR,
46 GR_GL_ONE_MINUS_DST_COLOR,
47 GR_GL_SRC_ALPHA,
48 GR_GL_ONE_MINUS_SRC_ALPHA,
49 GR_GL_DST_ALPHA,
50 GR_GL_ONE_MINUS_DST_ALPHA,
51 GR_GL_CONSTANT_COLOR,
52 GR_GL_ONE_MINUS_CONSTANT_COLOR,
53 GR_GL_CONSTANT_ALPHA,
54 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000055
56 // extended blend coeffs
57 GR_GL_SRC1_COLOR,
58 GR_GL_ONE_MINUS_SRC1_COLOR,
59 GR_GL_SRC1_ALPHA,
60 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000061};
62
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000064 static const bool gCoeffReferencesBlendConst[] = {
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 true,
76 true,
77 true,
78 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000079
80 // extended blend coeffs
81 false,
82 false,
83 false,
84 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000085 };
86 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000087 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
88
89 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
90 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
91 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
92 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
93 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
94 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
95 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
96 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
97 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
98 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
99 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
100 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
101 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
102 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
103
104 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
105 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
106 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
107 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
108
109 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
110 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000111}
112
reed@google.comac10a2d2010-12-22 21:39:39 +0000113///////////////////////////////////////////////////////////////////////////////
114
bsalomon@google.comd302f142011-03-03 13:54:13 +0000115void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
116 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000117 GrMatrix* matrix) {
118 GrAssert(NULL != texture);
119 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120 GrGLTexture::Orientation orientation = texture->orientation();
121 if (GrGLTexture::kBottomUp_Orientation == orientation) {
122 GrMatrix invY;
123 invY.setAll(GR_Scalar1, 0, 0,
124 0, -GR_Scalar1, GR_Scalar1,
125 0, 0, GrMatrix::I()[8]);
126 matrix->postConcat(invY);
127 } else {
128 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
129 }
130}
131
bsalomon@google.comd302f142011-03-03 13:54:13 +0000132bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000133 const GrSamplerState& sampler) {
134 GrAssert(NULL != texture);
135 if (!sampler.getMatrix().isIdentity()) {
136 return false;
137 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000138 GrGLTexture::Orientation orientation = texture->orientation();
139 if (GrGLTexture::kBottomUp_Orientation == orientation) {
140 return false;
141 } else {
142 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
143 }
144 return true;
145}
146
147///////////////////////////////////////////////////////////////////////////////
148
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000149static bool gPrintStartupSpew;
150
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000151static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000152
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000153 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000154
twiz@google.com0f31ca72011-03-18 17:38:11 +0000155 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000156 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
157 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000158 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000159 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
160 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000161 // some implementations require texture to be mip-map complete before
162 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000163 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000164 GR_GL_TEXTURE_MIN_FILTER,
165 GR_GL_NEAREST));
166 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
167 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
168 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
169 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
170 GR_GL_COLOR_ATTACHMENT0,
171 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000172 GrGLenum status;
173 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000174 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
175 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000176
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000177 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000178}
179
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000180GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
181
182 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000183
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000184 fillInConfigRenderableTable();
185
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000186 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000187
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000188 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000189
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000190 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000191 const GrGLubyte* ext;
192 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000193 const GrGLubyte* vendor;
194 const GrGLubyte* renderer;
195 const GrGLubyte* version;
196 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
197 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
198 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000199 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
200 this);
201 GrPrintf("------ VENDOR %s\n", vendor);
202 GrPrintf("------ RENDERER %s\n", renderer);
203 GrPrintf("------ VERSION %s\n", version);
204 GrPrintf("------ EXTENSIONS\n %s \n", ext);
205 }
206
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000207 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000208
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000209 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000210
bsalomon@google.comfe676522011-06-17 18:12:21 +0000211 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000212 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213}
214
215GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000216 // This must be called by before the GrDrawTarget destructor
217 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000218 // This subclass must do this before the base class destructor runs
219 // since we will unref the GrGLInterface.
220 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000221}
222
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000223///////////////////////////////////////////////////////////////////////////////
224
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000225void GrGpuGL::initCaps() {
226 GrGLint maxTextureUnits;
227 // check FS and fixed-function texture unit limits
228 // we only use textures in the fragment stage currently.
229 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000230 const GrGLInterface* gl = this->glInterface();
231 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000232 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000233 if (kES2_GrGLBinding != this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000234 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000235 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000236 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237
238 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000239 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000240 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000241 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000242 for (int i = 0; i < numFormats; ++i) {
243 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
244 fCaps.f8BitPaletteSupport = true;
245 break;
246 }
247 }
248
249 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 // we could also look for GL_ATI_separate_stencil extension or
251 // GL_EXT_stencil_two_side but they use different function signatures
252 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000253 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000254 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000255 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000256 this->hasExtension("GL_EXT_stencil_wrap");
257 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000258 // ES 2 has two sided stencil and stencil wrap
259 fCaps.fTwoSidedStencilSupport = true;
260 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000261 }
262
263 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000264 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
265 // extension includes glMapBuffer.
266 } else {
267 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
268 }
269
270 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000271 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000272 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
273 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000274 } else {
275 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000276 }
277 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000278 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000279 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000280 }
281
282 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
283
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000284 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
285 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000286 // Our render targets are always created with textures as the color
287 // attachment, hence this min:
288 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
289
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000290 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000291}
292
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000293void GrGpuGL::fillInConfigRenderableTable() {
294
295 // OpenGL < 3.0
296 // no support for render targets unless the GL_ARB_framebuffer_object
297 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
298 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
299 // probably don't get R8 in this case.
300
301 // OpenGL 3.0
302 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
303 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
304
305 // >= OpenGL 3.1
306 // base color renderable: RED, RG, RGB, and RGBA
307 // sized derivatives: R8, RGBA4, RGBA8
308 // if the GL_ARB_compatibility extension is supported then we get back
309 // support for GL_ALPHA and ALPHA8
310
311 // GL_EXT_bgra adds BGRA render targets to any version
312
313 // ES 2.0
314 // color renderable: RGBA4, RGB5_A1, RGB565
315 // GL_EXT_texture_rg adds support for R8 as a color render target
316 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
317 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
318 // added BGRA support
319
320 if (kDesktop_GrGLBinding == this->glBinding()) {
321 // Post 3.0 we will get R8
322 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
323 if (this->glVersion() >= GR_GL_VER(3,0) ||
324 this->hasExtension("GL_ARB_framebuffer_object")) {
325 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
326 }
327 } else {
328 // On ES we can only hope for R8
329 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
330 this->glCaps().textureRedSupport();
331 }
332
333 if (kDesktop_GrGLBinding != this->glBinding()) {
334 // only available in ES
335 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
336 }
337
338 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
339 // GL_EXT_framebuffer_object for FBO support. Both of these
340 // allow RGBA4 render targets so this is always supported.
341 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
342
343 if (this->glCaps().rgba8RenderbufferSupport()) {
344 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
345 }
346
347 if (this->glCaps().bgraFormatSupport()) {
348 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
349 }
350
351 // the un-premultiplied formats just inherit the premultiplied setting
352 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
353 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
354 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
355 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
356}
357
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000358bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
359 if (kUnknown_CanPreserveUnpremulRoundtrip ==
360 fCanPreserveUnpremulRoundtrip) {
361
362 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
363 uint32_t* srcData = data.get();
364 uint32_t* firstRead = data.get() + 256 * 256;
365 uint32_t* secondRead = data.get() + 2 * 256 * 256;
366
367 for (int y = 0; y < 256; ++y) {
368 for (int x = 0; x < 256; ++x) {
369 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
370 color[3] = y;
371 color[2] = x;
372 color[1] = x;
373 color[0] = x;
374 }
375 }
376
377 // We have broader support for read/write pixels on render targets
378 // than on textures.
379 GrTextureDesc dstDesc;
380 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
381 kNoStencil_GrTextureFlagBit;
382 dstDesc.fWidth = 256;
383 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000384 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000385 dstDesc.fSampleCnt = 0;
386
387 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
388 if (!dstTex.get()) {
389 return false;
390 }
391 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
392 GrAssert(NULL != rt);
393
394 bool failed = true;
395 static const UnpremulConversion gMethods[] = {
396 kUpOnWrite_DownOnRead_UnpremulConversion,
397 kDownOnWrite_UpOnRead_UnpremulConversion,
398 };
399
400 // pretend that we can do the roundtrip to avoid recursive calls to
401 // this function
402 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
403 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
404 fUnpremulConversion = gMethods[i];
405 rt->writePixels(0, 0,
406 256, 256,
407 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
408 rt->readPixels(0, 0,
409 256, 256,
410 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
411 rt->writePixels(0, 0,
412 256, 256,
413 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
414 rt->readPixels(0, 0,
415 256, 256,
416 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
417 failed = false;
418 for (int j = 0; j < 256 * 256; ++j) {
419 if (firstRead[j] != secondRead[j]) {
420 failed = true;
421 break;
422 }
423 }
424 }
425 fCanPreserveUnpremulRoundtrip = failed ?
426 kNo_CanPreserveUnpremulRoundtrip :
427 kYes_CanPreserveUnpremulRoundtrip;
428 }
429
430 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
431 return true;
432 } else {
433 return false;
434 }
435}
436
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000437GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000438 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
439 return GrPixelConfigSwapRAndB(config);
440 } else {
441 return config;
442 }
443}
444
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000445GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000446 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000447 return GrPixelConfigSwapRAndB(config);
448 } else {
449 return config;
450 }
451}
452
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000453bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
454 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
455}
456
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000457void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000458 if (gPrintStartupSpew && !fPrintedCaps) {
459 fPrintedCaps = true;
460 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000461 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000462 }
463
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000464 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000465 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000466 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000468 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000469 GL_CALL(Disable(GR_GL_DEPTH_TEST));
470 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000471
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000472 GL_CALL(Disable(GR_GL_CULL_FACE));
473 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000474 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000475
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000476 GL_CALL(Disable(GR_GL_DITHER));
477 if (kDesktop_GrGLBinding == this->glBinding()) {
478 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
479 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
480 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000481 fHWAAState.fMSAAEnabled = false;
482 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000483 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000484
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000485 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000486 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000487
reed@google.comac10a2d2010-12-22 21:39:39 +0000488 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000489 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000490
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000491 // invalid
492 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000495 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000496
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000497 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000498 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000499
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000500 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000501
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000503
tomhudson@google.com93813632011-10-27 20:21:16 +0000504 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000505 fHWDrawState.setTexture(s, NULL);
506 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
507 -GR_ScalarMax,
508 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000509 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000510 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000511 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000512
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000513 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000514 // set to true to force disableScissor to make a GL call.
515 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000516 this->disableScissor();
517
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000518 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000519
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000520 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 fHWStencilClip = false;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000522
523 // TODO: I believe this should actually go in GrGpu::onResetContext
524 // rather than here
525 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000526
527 fHWGeometryState.fIndexBuffer = NULL;
528 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000529
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000530 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000531
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000532 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000533 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000534
535 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000536 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000537 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
538 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000539 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000540 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
541 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000542 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000543 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
544 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000545 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000546 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
547 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000548}
549
bsalomon@google.come269f212011-11-07 13:29:52 +0000550GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000551 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000552 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000553 return NULL;
554 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000555
bsalomon@google.com99621082011-11-15 16:47:16 +0000556 glTexDesc.fWidth = desc.fWidth;
557 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000558 glTexDesc.fConfig = desc.fConfig;
559 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
560 glTexDesc.fOwnsID = false;
561 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
562
563 GrGLTexture* texture = NULL;
564 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
565 GrGLRenderTarget::Desc glRTDesc;
566 glRTDesc.fRTFBOID = 0;
567 glRTDesc.fTexFBOID = 0;
568 glRTDesc.fMSColorRenderbufferID = 0;
569 glRTDesc.fOwnIDs = true;
570 glRTDesc.fConfig = desc.fConfig;
571 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000572 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
573 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000574 glTexDesc.fTextureID,
575 &glRTDesc)) {
576 return NULL;
577 }
578 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
579 } else {
580 texture = new GrGLTexture(this, glTexDesc);
581 }
582 if (NULL == texture) {
583 return NULL;
584 }
585
586 this->setSpareTextureUnit();
587 return texture;
588}
589
590GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
591 GrGLRenderTarget::Desc glDesc;
592 glDesc.fConfig = desc.fConfig;
593 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
594 glDesc.fMSColorRenderbufferID = 0;
595 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
596 glDesc.fSampleCnt = desc.fSampleCnt;
597 glDesc.fOwnIDs = false;
598 GrGLIRect viewport;
599 viewport.fLeft = 0;
600 viewport.fBottom = 0;
601 viewport.fWidth = desc.fWidth;
602 viewport.fHeight = desc.fHeight;
603
604 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
605 if (desc.fStencilBits) {
606 GrGLStencilBuffer::Format format;
607 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
608 format.fPacked = false;
609 format.fStencilBits = desc.fStencilBits;
610 format.fTotalBits = desc.fStencilBits;
611 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
612 0,
613 desc.fWidth,
614 desc.fHeight,
615 desc.fSampleCnt,
616 format);
617 tgt->setStencilBuffer(sb);
618 sb->unref();
619 }
620 return tgt;
621}
622
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000623////////////////////////////////////////////////////////////////////////////////
624
bsalomon@google.com6f379512011-11-16 20:36:03 +0000625void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
626 int left, int top, int width, int height,
627 GrPixelConfig config, const void* buffer,
628 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000629 if (NULL == buffer) {
630 return;
631 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000632 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
633
bsalomon@google.com6f379512011-11-16 20:36:03 +0000634 this->setSpareTextureUnit();
635 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
636 GrGLTexture::Desc desc;
637 desc.fConfig = glTex->config();
638 desc.fWidth = glTex->width();
639 desc.fHeight = glTex->height();
640 desc.fOrientation = glTex->orientation();
641 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000642
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000643 this->uploadTexData(desc, false,
644 left, top, width, height,
645 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000646}
647
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000648namespace {
649bool adjust_pixel_ops_params(int surfaceWidth,
650 int surfaceHeight,
651 size_t bpp,
652 int* left, int* top, int* width, int* height,
653 const void** data,
654 size_t* rowBytes) {
655 if (!*rowBytes) {
656 *rowBytes = *width * bpp;
657 }
658
659 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
660 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
661
662 if (!subRect.intersect(bounds)) {
663 return false;
664 }
665 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
666 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
667
668 *left = subRect.fLeft;
669 *top = subRect.fTop;
670 *width = subRect.width();
671 *height = subRect.height();
672 return true;
673}
674}
675
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000676bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
677 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000678 int left, int top, int width, int height,
679 GrPixelConfig dataConfig,
680 const void* data,
681 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000682 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000683
684 size_t bpp = GrBytesPerPixel(dataConfig);
685 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
686 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000687 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000688 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000689 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000690
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000691 // in case we need a temporary, trimmed copy of the src pixels
692 SkAutoSMalloc<128 * 128> tempStorage;
693
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000694 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000695 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000696 if (useTexStorage) {
697 if (kDesktop_GrGLBinding == this->glBinding()) {
698 // 565 is not a sized internal format on desktop GL. So on desktop
699 // with 565 we always use an unsized internal format to let the
700 // system pick the best sized format to convert the 565 data to.
701 // Since glTexStorage only allows sized internal formats we will
702 // instead fallback to glTexImage2D.
703 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
704 } else {
705 // ES doesn't allow paletted textures to be used with tex storage
706 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
707 }
708 }
709
710 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000711 GrGLenum externalFormat;
712 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000713 // glTexStorage requires sized internal formats on both desktop and ES. ES
714 // glTexImage requires an unsized format.
715 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
716 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000717 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000718 }
719
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000720 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000721 // paletted textures cannot be updated
722 return false;
723 }
724
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000725 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000726 * check whether to allocate a temporary buffer for flipping y or
727 * because our srcData has extra bytes past each row. If so, we need
728 * to trim those off here, since GL ES may not let us specify
729 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000730 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000731 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000732 bool swFlipY = false;
733 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000734 if (NULL != data) {
735 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000736 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000737 glFlipY = true;
738 } else {
739 swFlipY = true;
740 }
741 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000742 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000743 // can't use this for flipping, only non-neg values allowed. :(
744 if (rowBytes != trimRowBytes) {
745 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
746 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
747 restoreGLRowLength = true;
748 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000749 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000750 if (trimRowBytes != rowBytes || swFlipY) {
751 // copy data into our new storage, skipping the trailing bytes
752 size_t trimSize = height * trimRowBytes;
753 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000754 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000755 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000756 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000757 char* dst = (char*)tempStorage.reset(trimSize);
758 for (int y = 0; y < height; y++) {
759 memcpy(dst, src, trimRowBytes);
760 if (swFlipY) {
761 src -= rowBytes;
762 } else {
763 src += rowBytes;
764 }
765 dst += trimRowBytes;
766 }
767 // now point data to our copied version
768 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000769 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000771 if (glFlipY) {
772 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
773 }
774 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000775 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000776 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000777 if (isNewTexture &&
778 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000779 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000780 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000781 if (useTexStorage) {
782 // We never resize or change formats of textures. We don't use
783 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000784 GL_ALLOC_CALL(this->glInterface(),
785 TexStorage2D(GR_GL_TEXTURE_2D,
786 1, // levels
787 internalFormat,
788 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000789 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000790 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
791 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
792 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000793 GL_ALLOC_CALL(this->glInterface(),
794 CompressedTexImage2D(GR_GL_TEXTURE_2D,
795 0, // level
796 internalFormat,
797 desc.fWidth, desc.fHeight,
798 0, // border
799 imageSize,
800 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000801 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000802 GL_ALLOC_CALL(this->glInterface(),
803 TexImage2D(GR_GL_TEXTURE_2D,
804 0, // level
805 internalFormat,
806 desc.fWidth, desc.fHeight,
807 0, // border
808 externalFormat, externalType,
809 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000810 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000811 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000812 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000813 if (error != GR_GL_NO_ERROR) {
814 succeeded = false;
815 } else {
816 // if we have data and we used TexStorage to create the texture, we
817 // now upload with TexSubImage.
818 if (NULL != data && useTexStorage) {
819 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
820 0, // level
821 left, top,
822 width, height,
823 externalFormat, externalType,
824 data));
825 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000826 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000827 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000828 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000829 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000830 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000831 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
832 0, // level
833 left, top,
834 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000835 externalFormat, externalType, data));
836 }
837
838 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000839 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000840 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000841 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000842 if (glFlipY) {
843 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
844 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000845 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846}
847
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000848namespace {
849bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
850 int sampleCount,
851 GrGLenum format,
852 int width, int height) {
853 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
854 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
855 bool created = false;
856 if (GrGLCaps::kNVDesktop_CoverageAAType ==
857 ctxInfo.caps().coverageAAType()) {
858 const GrGLCaps::MSAACoverageMode& mode =
859 ctxInfo.caps().getMSAACoverageMode(sampleCount);
860 GL_ALLOC_CALL(ctxInfo.interface(),
861 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
862 mode.fCoverageSampleCnt,
863 mode.fColorSampleCnt,
864 format,
865 width, height));
866 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
867 }
868 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000869 // glRBMS will fail if requested samples is > max samples.
870 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000871 GL_ALLOC_CALL(ctxInfo.interface(),
872 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
873 sampleCount,
874 format,
875 width, height));
876 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
877 }
878 return created;
879}
880}
881
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000882bool GrGpuGL::createRenderTargetObjects(int width, int height,
883 GrGLuint texID,
884 GrGLRenderTarget::Desc* desc) {
885 desc->fMSColorRenderbufferID = 0;
886 desc->fRTFBOID = 0;
887 desc->fTexFBOID = 0;
888 desc->fOwnIDs = true;
889
890 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000891
bsalomon@google.comab15d612011-08-09 12:57:56 +0000892 GrGLenum msColorFormat = 0; // suppress warning
893
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000894 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000895 if (!desc->fTexFBOID) {
896 goto FAILED;
897 }
898
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899
900 // If we are using multisampling we will create two FBOS. We render
901 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000902 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000903 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000904 goto FAILED;
905 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000906 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
907 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 if (!desc->fRTFBOID ||
909 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000910 !this->configToGLFormats(desc->fConfig,
911 // GLES requires sized internal formats
912 kES2_GrGLBinding == this->glBinding(),
913 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000914 goto FAILED;
915 }
916 } else {
917 desc->fRTFBOID = desc->fTexFBOID;
918 }
919
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000920 // below here we may bind the FBO
921 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000922 if (desc->fRTFBOID != desc->fTexFBOID) {
923 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000924 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000925 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000926 if (!renderbuffer_storage_msaa(fGLContextInfo,
927 desc->fSampleCnt,
928 msColorFormat,
929 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000930 goto FAILED;
931 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000932 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
933 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000934 GR_GL_COLOR_ATTACHMENT0,
935 GR_GL_RENDERBUFFER,
936 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000937 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000938 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
939 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
940 goto FAILED;
941 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000942 fGLContextInfo.caps().markConfigAsValidColorAttachment(
943 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000944 }
945 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000946 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000947
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000948 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
949 GR_GL_COLOR_ATTACHMENT0,
950 GR_GL_TEXTURE_2D,
951 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000952 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000953 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
954 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
955 goto FAILED;
956 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000957 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000958 }
959
960 return true;
961
962FAILED:
963 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000964 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000965 }
966 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000967 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000968 }
969 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000970 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 }
972 return false;
973}
974
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000975// good to set a break-point here to know when createTexture fails
976static GrTexture* return_null_texture() {
977// GrAssert(!"null texture");
978 return NULL;
979}
980
981#if GR_DEBUG
982static size_t as_size_t(int x) {
983 return x;
984}
985#endif
986
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000987GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000988 const void* srcData,
989 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000990
991#if GR_COLLECT_STATS
992 ++fStats.fTextureCreateCnt;
993#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000994
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000995 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000996 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000997
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000998 // Attempt to catch un- or wrongly initialized sample counts;
999 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1000
bsalomon@google.com99621082011-11-15 16:47:16 +00001001 glTexDesc.fWidth = desc.fWidth;
1002 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001003 glTexDesc.fConfig = desc.fConfig;
1004 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001005
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001006 glRTDesc.fMSColorRenderbufferID = 0;
1007 glRTDesc.fRTFBOID = 0;
1008 glRTDesc.fTexFBOID = 0;
1009 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001010 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001011
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001012 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001013
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001014 const Caps& caps = this->getCaps();
1015
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001016 // We keep GrRenderTargets in GL's normal orientation so that they
1017 // can be drawn to by the outside world without the client having
1018 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001019 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001020 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001021
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001022 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001023 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001024 desc.fSampleCnt) {
1025 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 }
1027
reed@google.comac10a2d2010-12-22 21:39:39 +00001028 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001029 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1030 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001031 return return_null_texture();
1032 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001033 }
1034
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001035 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001036 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001037 // provides a hint about how this texture will be used
1038 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1039 GR_GL_TEXTURE_USAGE,
1040 GR_GL_FRAMEBUFFER_ATTACHMENT));
1041 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001042 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001043 return return_null_texture();
1044 }
1045
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001046 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001047 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001048
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001049 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1050 // drivers have a bug where an FBO won't be complete if it includes a
1051 // texture that is not mipmap complete (considering the filter in use).
1052 GrGLTexture::TexParams initialTexParams;
1053 // we only set a subset here so invalidate first
1054 initialTexParams.invalidate();
1055 initialTexParams.fFilter = GR_GL_NEAREST;
1056 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1057 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001058 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1059 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001060 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001061 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1062 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001063 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001064 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1065 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001066 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001067 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1068 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001069 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001070 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1071 glTexDesc.fWidth, glTexDesc.fHeight,
1072 desc.fConfig, srcData, rowBytes)) {
1073 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1074 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001075 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001076
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001078 if (renderTarget) {
1079#if GR_COLLECT_STATS
1080 ++fStats.fRenderTargetCreateCnt;
1081#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001082 // unbind the texture from the texture unit before binding it to the frame buffer
1083 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1084
bsalomon@google.com99621082011-11-15 16:47:16 +00001085 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1086 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001087 glTexDesc.fTextureID,
1088 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001089 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001090 return return_null_texture();
1091 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001092 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001093 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001094 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001096 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001097#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001098 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1099 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001100#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 return tex;
1102}
1103
1104namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001105
1106const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1107
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001108void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1109 GrGLuint rb,
1110 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001111 // we shouldn't ever know one size and not the other
1112 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1113 (kUnknownBitCount == format->fTotalBits));
1114 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001115 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001116 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1117 (GrGLint*)&format->fStencilBits);
1118 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001119 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001120 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1121 (GrGLint*)&format->fTotalBits);
1122 format->fTotalBits += format->fStencilBits;
1123 } else {
1124 format->fTotalBits = format->fStencilBits;
1125 }
1126 }
1127}
1128}
1129
1130bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1131 int width, int height) {
1132
1133 // All internally created RTs are also textures. We don't create
1134 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1135 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001136 GrAssert(width >= rt->width());
1137 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138
1139 int samples = rt->numSamples();
1140 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001141 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 if (!sbID) {
1143 return false;
1144 }
1145
1146 GrGLStencilBuffer* sb = NULL;
1147
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001148 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001150 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151 // we start with the last stencil format that succeeded in hopes
1152 // that we won't go through this loop more than once after the
1153 // first (painful) stencil creation.
1154 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001155 const GrGLCaps::StencilFormat& sFmt =
1156 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001157 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001158 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001160 bool created;
1161 if (samples > 0) {
1162 created = renderbuffer_storage_msaa(fGLContextInfo,
1163 samples,
1164 sFmt.fInternalFormat,
1165 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001167 GL_ALLOC_CALL(this->glInterface(),
1168 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001169 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001170 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001171 created =
1172 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001174 if (created) {
1175 // After sized formats we attempt an unsized format and take
1176 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001177 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001178 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001179 sb = new GrGLStencilBuffer(this, sbID, width, height,
1180 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1182 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001183 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 return true;
1186 }
1187 sb->abandon(); // otherwise we lose sbID
1188 sb->unref();
1189 }
1190 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001192 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193}
1194
1195bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1196 GrRenderTarget* rt) {
1197 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1198
1199 GrGLuint fbo = glrt->renderFBOID();
1200
1201 if (NULL == sb) {
1202 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001203 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 GR_GL_STENCIL_ATTACHMENT,
1205 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 GR_GL_DEPTH_ATTACHMENT,
1208 GR_GL_RENDERBUFFER, 0));
1209#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001210 GrGLenum status;
1211 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1213#endif
1214 }
1215 return true;
1216 } else {
1217 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1218 GrGLuint rb = glsb->renderbufferID();
1219
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001220 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1222 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 GR_GL_STENCIL_ATTACHMENT,
1224 GR_GL_RENDERBUFFER, rb));
1225 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001226 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 GR_GL_DEPTH_ATTACHMENT,
1228 GR_GL_RENDERBUFFER, rb));
1229 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001230 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231 GR_GL_DEPTH_ATTACHMENT,
1232 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001235 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001236 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001237 glsb->format())) {
1238 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1239 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001241 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001243 if (glsb->format().fPacked) {
1244 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1245 GR_GL_DEPTH_ATTACHMENT,
1246 GR_GL_RENDERBUFFER, 0));
1247 }
1248 return false;
1249 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001250 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001251 rt->config(),
1252 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001253 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001254 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001255 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001257}
1258
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001259////////////////////////////////////////////////////////////////////////////////
1260
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001261GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001262 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001265 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001266 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001267 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001269 GL_ALLOC_CALL(this->glInterface(),
1270 BufferData(GR_GL_ARRAY_BUFFER,
1271 size,
1272 NULL, // data ptr
1273 dynamic ? GR_GL_DYNAMIC_DRAW :
1274 GR_GL_STATIC_DRAW));
1275 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001276 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 // deleting bound buffer does implicit bind to 0
1278 fHWGeometryState.fVertexBuffer = NULL;
1279 return NULL;
1280 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001281 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 size, dynamic);
1283 fHWGeometryState.fVertexBuffer = vertexBuffer;
1284 return vertexBuffer;
1285 }
1286 return NULL;
1287}
1288
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001289GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001290 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001291 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001292 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001294 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001296 GL_ALLOC_CALL(this->glInterface(),
1297 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1298 size,
1299 NULL, // data ptr
1300 dynamic ? GR_GL_DYNAMIC_DRAW :
1301 GR_GL_STATIC_DRAW));
1302 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001303 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 // deleting bound buffer does implicit bind to 0
1305 fHWGeometryState.fIndexBuffer = NULL;
1306 return NULL;
1307 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001308 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 size, dynamic);
1310 fHWGeometryState.fIndexBuffer = indexBuffer;
1311 return indexBuffer;
1312 }
1313 return NULL;
1314}
1315
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001316void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001317 const GrDrawState& drawState = this->getDrawState();
1318 const GrGLRenderTarget* rt =
1319 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1320
1321 GrAssert(NULL != rt);
1322 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001323
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001324 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001325 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1326 rect.width(), rect.height());
1327 if (scissor.contains(vp)) {
1328 disableScissor();
1329 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 }
1331
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001332 if (fHWBounds.fScissorRect != scissor) {
1333 scissor.pushToGLScissor(this->glInterface());
1334 fHWBounds.fScissorRect = scissor;
1335 }
1336 if (!fHWBounds.fScissorEnabled) {
1337 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1338 fHWBounds.fScissorEnabled = true;
1339 }
1340}
1341
1342void GrGpuGL::disableScissor() {
1343 if (fHWBounds.fScissorEnabled) {
1344 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1345 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 }
1347}
1348
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001349void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001350 const GrDrawState& drawState = this->getDrawState();
1351 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001352 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001353 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001354
bsalomon@google.com74b98712011-11-11 19:46:16 +00001355 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001356 if (NULL != rect) {
1357 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001358 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001359 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001360 if (clippedRect.intersect(rtRect)) {
1361 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001362 } else {
1363 return;
1364 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001366 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001367 if (NULL != rect)
1368 this->enableScissoring(*rect);
1369 else
1370 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001371
1372 GrGLfloat r, g, b, a;
1373 static const GrGLfloat scale255 = 1.f / 255.f;
1374 a = GrColorUnpackA(color) * scale255;
1375 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001376 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001377 scaleRGB *= a;
1378 }
1379 r = GrColorUnpackR(color) * scaleRGB;
1380 g = GrColorUnpackG(color) * scaleRGB;
1381 b = GrColorUnpackB(color) * scaleRGB;
1382
1383 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001384 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001385 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001386 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001387}
1388
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001389void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001390 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001391 return;
1392 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001393
1394 this->flushRenderTarget(&GrIRect::EmptyIRect());
1395
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001396 this->disableScissor();
1397
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001398 GL_CALL(StencilMask(0xffffffff));
1399 GL_CALL(ClearStencil(0));
1400 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001401 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001402}
1403
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001404void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001405 const GrDrawState& drawState = this->getDrawState();
1406 const GrRenderTarget* rt = drawState.getRenderTarget();
1407 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001408
1409 // this should only be called internally when we know we have a
1410 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001411 GrAssert(NULL != rt->getStencilBuffer());
1412 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001413#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001414 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001415 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001416#else
1417 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001418 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001419 // turned into draws. Our contract on GrDrawTarget says that
1420 // changing the clip between stencil passes may or may not
1421 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001422 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001423#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001424 GrGLint value;
1425 if (insideClip) {
1426 value = (1 << (stencilBitCount - 1));
1427 } else {
1428 value = 0;
1429 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001430 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001431 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001432 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001433 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001434 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001435 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001436}
1437
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001438void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001439 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001440}
1441
bsalomon@google.comc4364992011-11-07 15:54:49 +00001442bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1443 int left, int top,
1444 int width, int height,
1445 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001446 size_t rowBytes) const {
1447 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001448 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001449 return false;
1450 }
1451
1452 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001453 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001454 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001455 return true;
1456 }
1457 // If we have to do memcpys to handle rowBytes then y-flip is free
1458 // Note the rowBytes might be tight to the passed in data, but if data
1459 // gets clipped in x to the target the rowBytes will no longer be tight.
1460 if (left >= 0 && (left + width) < renderTarget->width()) {
1461 return 0 == rowBytes ||
1462 GrBytesPerPixel(config) * width == rowBytes;
1463 } else {
1464 return false;
1465 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001466}
1467
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001468bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001469 int left, int top,
1470 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001471 GrPixelConfig config,
1472 void* buffer,
1473 size_t rowBytes,
1474 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001475 GrGLenum format;
1476 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001477 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001478 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001479 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001480 size_t bpp = GrBytesPerPixel(config);
1481 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1482 &left, &top, &width, &height,
1483 const_cast<const void**>(&buffer),
1484 &rowBytes)) {
1485 return false;
1486 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001487
bsalomon@google.comc6980972011-11-02 19:57:21 +00001488 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001489 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001490 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001491 switch (tgt->getResolveType()) {
1492 case GrGLRenderTarget::kCantResolve_ResolveType:
1493 return false;
1494 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001495 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001496 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001497 break;
1498 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001499 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001500 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001501 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1502 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001503 break;
1504 default:
1505 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001506 }
1507
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001508 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001509
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001510 // the read rect is viewport-relative
1511 GrGLIRect readRect;
1512 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001513
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001514 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001515 if (0 == rowBytes) {
1516 rowBytes = tightRowBytes;
1517 }
1518 size_t readDstRowBytes = tightRowBytes;
1519 void* readDst = buffer;
1520
1521 // determine if GL can read using the passed rowBytes or if we need
1522 // a scratch buffer.
1523 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1524 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001525 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001526 GrAssert(!(rowBytes % sizeof(GrColor)));
1527 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1528 readDstRowBytes = rowBytes;
1529 } else {
1530 scratch.reset(tightRowBytes * height);
1531 readDst = scratch.get();
1532 }
1533 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001534 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001535 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1536 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001537 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1538 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 format, type, readDst));
1540 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001541 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001542 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1543 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001544 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001545 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1546 invertY = true;
1547 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001548
1549 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001550 // API presents top-to-bottom. We must preserve the padding contents. Note
1551 // that the above readPixels did not overwrite the padding.
1552 if (readDst == buffer) {
1553 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001554 if (!invertY) {
1555 scratch.reset(tightRowBytes);
1556 void* tmpRow = scratch.get();
1557 // flip y in-place by rows
1558 const int halfY = height >> 1;
1559 char* top = reinterpret_cast<char*>(buffer);
1560 char* bottom = top + (height - 1) * rowBytes;
1561 for (int y = 0; y < halfY; y++) {
1562 memcpy(tmpRow, top, tightRowBytes);
1563 memcpy(top, bottom, tightRowBytes);
1564 memcpy(bottom, tmpRow, tightRowBytes);
1565 top += rowBytes;
1566 bottom -= rowBytes;
1567 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001568 }
1569 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001570 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001571 // copy from readDst to buffer while flipping y
1572 const int halfY = height >> 1;
1573 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001574 char* dst = reinterpret_cast<char*>(buffer);
1575 if (!invertY) {
1576 dst += (height-1) * rowBytes;
1577 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001578 for (int y = 0; y < height; y++) {
1579 memcpy(dst, src, tightRowBytes);
1580 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001581 if (invertY) {
1582 dst += rowBytes;
1583 } else {
1584 dst -= rowBytes;
1585 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001586 }
1587 }
1588 return true;
1589}
1590
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001591void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001592
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001593 GrGLRenderTarget* rt =
1594 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1595 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001596
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001597 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001598 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001599 #if GR_COLLECT_STATS
1600 ++fStats.fRenderTargetChngCnt;
1601 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001602 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001603 GrGLenum status;
1604 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001605 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001606 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001607 }
1608 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001609 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001610 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001611 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001612 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001613 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 fHWBounds.fViewportRect = vp;
1615 }
1616 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001617 if (NULL == bound || !bound->isEmpty()) {
1618 rt->flagAsNeedingResolve(bound);
1619 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001620}
1621
twiz@google.com0f31ca72011-03-18 17:38:11 +00001622GrGLenum gPrimitiveType2GLMode[] = {
1623 GR_GL_TRIANGLES,
1624 GR_GL_TRIANGLE_STRIP,
1625 GR_GL_TRIANGLE_FAN,
1626 GR_GL_POINTS,
1627 GR_GL_LINES,
1628 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001629};
1630
bsalomon@google.comd302f142011-03-03 13:54:13 +00001631#define SWAP_PER_DRAW 0
1632
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001633#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001634 #if GR_MAC_BUILD
1635 #include <AGL/agl.h>
1636 #elif GR_WIN32_BUILD
1637 void SwapBuf() {
1638 DWORD procID = GetCurrentProcessId();
1639 HWND hwnd = GetTopWindow(GetDesktopWindow());
1640 while(hwnd) {
1641 DWORD wndProcID = 0;
1642 GetWindowThreadProcessId(hwnd, &wndProcID);
1643 if(wndProcID == procID) {
1644 SwapBuffers(GetDC(hwnd));
1645 }
1646 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1647 }
1648 }
1649 #endif
1650#endif
1651
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001652void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1653 uint32_t startVertex,
1654 uint32_t startIndex,
1655 uint32_t vertexCount,
1656 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1658
twiz@google.com0f31ca72011-03-18 17:38:11 +00001659 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001660
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001661 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1662 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1663
1664 // our setupGeometry better have adjusted this to zero since
1665 // DrawElements always draws from the begining of the arrays for idx 0.
1666 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001667
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001668 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1669 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001670#if SWAP_PER_DRAW
1671 glFlush();
1672 #if GR_MAC_BUILD
1673 aglSwapBuffers(aglGetCurrentContext());
1674 int set_a_break_pt_here = 9;
1675 aglSwapBuffers(aglGetCurrentContext());
1676 #elif GR_WIN32_BUILD
1677 SwapBuf();
1678 int set_a_break_pt_here = 9;
1679 SwapBuf();
1680 #endif
1681#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001682}
1683
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001684void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1685 uint32_t startVertex,
1686 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001687 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1688
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001689 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1690
1691 // our setupGeometry better have adjusted this to zero.
1692 // DrawElements doesn't take an offset so we always adjus the startVertex.
1693 GrAssert(0 == startVertex);
1694
1695 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1696 // account for startVertex in the DrawElements case. So we always
1697 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001698 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001699#if SWAP_PER_DRAW
1700 glFlush();
1701 #if GR_MAC_BUILD
1702 aglSwapBuffers(aglGetCurrentContext());
1703 int set_a_break_pt_here = 9;
1704 aglSwapBuffers(aglGetCurrentContext());
1705 #elif GR_WIN32_BUILD
1706 SwapBuf();
1707 int set_a_break_pt_here = 9;
1708 SwapBuf();
1709 #endif
1710#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001711}
1712
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001713void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1714
1715 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001716
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001717 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001718 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001719 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001720 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1721 rt->renderFBOID()));
1722 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1723 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001724 #if GR_COLLECT_STATS
1725 ++fStats.fRenderTargetChngCnt;
1726 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001727 // make sure we go through flushRenderTarget() since we've modified
1728 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001729 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001730 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001731 const GrIRect dirtyRect = rt->getResolveRect();
1732 GrGLIRect r;
1733 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1734 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001735
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001736 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001737 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001738#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001739 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1740 GL_CALL(Scissor(r.fLeft, r.fBottom,
1741 r.fWidth, r.fHeight));
1742 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001743 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001744 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001745#else
1746 this->enableScissoring(dirtyRect);
1747 GL_CALL(ResolveMultisampleFramebuffer());
1748#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001750 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001751 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001752 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1753 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001754 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001755 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001756 int right = r.fLeft + r.fWidth;
1757 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001758 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1759 r.fLeft, r.fBottom, right, top,
1760 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001762 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 }
1764}
1765
twiz@google.com0f31ca72011-03-18 17:38:11 +00001766static const GrGLenum grToGLStencilFunc[] = {
1767 GR_GL_ALWAYS, // kAlways_StencilFunc
1768 GR_GL_NEVER, // kNever_StencilFunc
1769 GR_GL_GREATER, // kGreater_StencilFunc
1770 GR_GL_GEQUAL, // kGEqual_StencilFunc
1771 GR_GL_LESS, // kLess_StencilFunc
1772 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1773 GR_GL_EQUAL, // kEqual_StencilFunc,
1774 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001775};
1776GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1777GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1778GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1779GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1780GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1781GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1782GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1783GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1784GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1785
twiz@google.com0f31ca72011-03-18 17:38:11 +00001786static const GrGLenum grToGLStencilOp[] = {
1787 GR_GL_KEEP, // kKeep_StencilOp
1788 GR_GL_REPLACE, // kReplace_StencilOp
1789 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1790 GR_GL_INCR, // kIncClamp_StencilOp
1791 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1792 GR_GL_DECR, // kDecClamp_StencilOp
1793 GR_GL_ZERO, // kZero_StencilOp
1794 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001795};
1796GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1797GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1798GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1799GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1800GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1801GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1802GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1803GR_STATIC_ASSERT(6 == kZero_StencilOp);
1804GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1805
reed@google.comac10a2d2010-12-22 21:39:39 +00001806void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001807 const GrDrawState& drawState = this->getDrawState();
1808
1809 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001810
1811 // use stencil for clipping if clipping is enabled and the clip
1812 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001813 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001814 bool drawClipToStencil =
1815 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001816 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1817 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001818 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1819 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001820
1821 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001822
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001823 // we can't simultaneously perform stencil-clipping and
1824 // modify the stencil clip
1825 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001826
bsalomon@google.comd302f142011-03-03 13:54:13 +00001827 if (settings->isDisabled()) {
1828 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001829 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001831 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001832
1833 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001834 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001836 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001837 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001838 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001839 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1840 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1841 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1842 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1843 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1844 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1845 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1846 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001847 }
1848 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001849 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001850 GrStencilBuffer* stencilBuffer =
1851 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001852 if (NULL != stencilBuffer) {
1853 stencilBits = stencilBuffer->bits();
1854 }
1855 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001856 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001857
1858 GrGLuint clipStencilMask = 0;
1859 GrGLuint userStencilMask = ~0;
1860 if (stencilBits > 0) {
1861 clipStencilMask = 1 << (stencilBits - 1);
1862 userStencilMask = clipStencilMask - 1;
1863 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001865 unsigned int frontRef = settings->frontFuncRef();
1866 unsigned int frontMask = settings->frontFuncMask();
1867 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001868 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001870 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001871 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1872 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001873 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001874 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001875 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001876
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001877 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001878 stencilClip,
1879 clipStencilMask,
1880 userStencilMask,
1881 &frontRef,
1882 &frontMask);
1883 frontWriteMask &= userStencilMask;
1884 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001885 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001886 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001887 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001888 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001889 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001890 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001891 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001892 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001893 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001894 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001895
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001896 unsigned int backRef = settings->backFuncRef();
1897 unsigned int backMask = settings->backFuncMask();
1898 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001899
1900
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001901 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001902 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1903 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001904 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001905 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001906 stencilClip, settings->backFunc())];
1907 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001908 stencilClip,
1909 clipStencilMask,
1910 userStencilMask,
1911 &backRef,
1912 &backMask);
1913 backWriteMask &= userStencilMask;
1914 }
1915
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001916 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1917 frontRef, frontMask));
1918 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1919 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1920 backRef, backMask));
1921 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1922 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001923 grToGLStencilOp[settings->frontFailOp()],
1924 grToGLStencilOp[settings->frontPassOp()],
1925 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001926
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001927 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001928 grToGLStencilOp[settings->backFailOp()],
1929 grToGLStencilOp[settings->backPassOp()],
1930 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001931 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001932 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1933 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001934 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1935 grToGLStencilOp[settings->frontPassOp()],
1936 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001937 }
1938 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001939 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001940 fHWStencilClip = stencilClip;
1941 }
1942}
1943
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001944void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001945 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001946 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001947 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1948 // smooth lines.
1949
1950 // we prefer smooth lines over multisampled lines
1951 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001952 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001953 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001954 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001955 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001957 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 fHWAAState.fSmoothLineEnabled = false;
1960 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001961 if (rt->isMultisampled() &&
1962 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001963 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001964 fHWAAState.fMSAAEnabled = false;
1965 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001966 } else if (rt->isMultisampled() &&
1967 this->getDrawState().isHWAntialiasState() !=
1968 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001969 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001970 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001971 fHWAAState.fMSAAEnabled = false;
1972 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001973 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001974 fHWAAState.fMSAAEnabled = true;
1975 }
1976 }
1977 }
1978}
1979
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001980void GrGpuGL::flushBlend(GrPrimitiveType type,
1981 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001982 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001983 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001984 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001985 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001986 fHWBlendDisabled = false;
1987 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001988 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1989 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001990 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1991 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001992 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001993 }
1994 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001995 // any optimization to disable blending should
1996 // have already been applied and tweaked the coeffs
1997 // to (1, 0).
1998 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1999 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002000 if (fHWBlendDisabled != blendOff) {
2001 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002002 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002003 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002004 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002005 }
2006 fHWBlendDisabled = blendOff;
2007 }
2008 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002009 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2010 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002011 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2012 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002013 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002014 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002015 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002016 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2017 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002018 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002019
2020 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002021 GrColorUnpackR(blendConst) / 255.f,
2022 GrColorUnpackG(blendConst) / 255.f,
2023 GrColorUnpackB(blendConst) / 255.f,
2024 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002025 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002026 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002027 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002028 }
2029 }
2030 }
2031}
2032
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002033namespace {
2034
2035unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002036 switch (filter) {
2037 case GrSamplerState::kBilinear_Filter:
2038 case GrSamplerState::k4x4Downsample_Filter:
2039 return GR_GL_LINEAR;
2040 case GrSamplerState::kNearest_Filter:
2041 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002042 case GrSamplerState::kErode_Filter:
2043 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002044 return GR_GL_NEAREST;
2045 default:
2046 GrAssert(!"Unknown filter type");
2047 return GR_GL_LINEAR;
2048 }
2049}
2050
robertphillips@google.com69950682012-04-06 18:06:10 +00002051// get_swizzle is only called from this .cpp so it is OK to inline it here
2052inline const GrGLenum* get_swizzle(GrPixelConfig config,
2053 const GrSamplerState& sampler,
2054 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002055 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com69950682012-04-06 18:06:10 +00002056 if (glCaps.textureRedSupport()) {
2057 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2058 GR_GL_RED, GR_GL_RED };
2059 return gRedSmear;
2060 } else {
2061 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2062 GR_GL_ALPHA, GR_GL_ALPHA };
2063 return gAlphaSmear;
2064 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002065 } else if (sampler.swapsRAndB()) {
2066 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2067 GR_GL_RED, GR_GL_ALPHA };
2068 return gRedBlueSwap;
2069 } else {
2070 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2071 GR_GL_BLUE, GR_GL_ALPHA };
2072 return gStraight;
2073 }
2074}
2075
2076void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2077 // should add texparameteri to interface to make 1 instead of 4 calls here
2078 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2079 GR_GL_TEXTURE_SWIZZLE_R,
2080 swizzle[0]));
2081 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2082 GR_GL_TEXTURE_SWIZZLE_G,
2083 swizzle[1]));
2084 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2085 GR_GL_TEXTURE_SWIZZLE_B,
2086 swizzle[2]));
2087 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2088 GR_GL_TEXTURE_SWIZZLE_A,
2089 swizzle[3]));
2090}
2091}
2092
bsalomon@google.comffca4002011-02-22 20:34:01 +00002093bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002094
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002095 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002096 // GrGpu::setupClipAndFlushState should have already checked this
2097 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002098 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002099
tomhudson@google.com93813632011-10-27 20:21:16 +00002100 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002101 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002102 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002103 GrGLTexture* nextTexture =
2104 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002105
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002106 // true for now, but maybe not with GrEffect.
2107 GrAssert(NULL != nextTexture);
2108 // if we created a rt/tex and rendered to it without using a
2109 // texture and now we're texuring from the rt it will still be
2110 // the last bound texture, but it needs resolving. So keep this
2111 // out of the "last != next" check.
2112 GrGLRenderTarget* texRT =
2113 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2114 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002115 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002116 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002117
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002118 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002119 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002120 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002121 #if GR_COLLECT_STATS
2122 ++fStats.fTextureChngCnt;
2123 #endif
2124 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002125 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002126 // The texture matrix has to compensate for texture width/height
2127 // and NPOT-embedded-in-POT
2128 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002129 }
2130
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002131 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002132 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002133 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002134 nextTexture->getCachedTexParams(&timestamp);
2135 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002136 GrGLTexture::TexParams newTexParams;
2137
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002138 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002139
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002140 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002141 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2142 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002143 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com69950682012-04-06 18:06:10 +00002144 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002145 sizeof(newTexParams.fSwizzleRGBA));
2146 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002147 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002148 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002149 GR_GL_TEXTURE_MAG_FILTER,
2150 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002151 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002152 GR_GL_TEXTURE_MIN_FILTER,
2153 newTexParams.fFilter));
2154 }
2155 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2156 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002157 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002158 GR_GL_TEXTURE_WRAP_S,
2159 newTexParams.fWrapS));
2160 }
2161 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2162 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002163 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002164 GR_GL_TEXTURE_WRAP_T,
2165 newTexParams.fWrapT));
2166 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002167 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002168 (setAll ||
2169 memcmp(newTexParams.fSwizzleRGBA,
2170 oldTexParams.fSwizzleRGBA,
2171 sizeof(newTexParams.fSwizzleRGBA)))) {
2172 setTextureUnit(s);
2173 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2174 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002175 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002176 nextTexture->setCachedTexParams(newTexParams,
2177 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002178 }
2179 }
2180
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002181 GrIRect* rect = NULL;
2182 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002183 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002184 fClip.hasConservativeBounds()) {
2185 fClip.getConservativeBounds().roundOut(&clipBounds);
2186 rect = &clipBounds;
2187 }
2188 this->flushRenderTarget(rect);
2189 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002190
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002191 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2192 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002193 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002194 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002195 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002196 }
2197 }
2198
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002199 if (drawState->isColorWriteDisabled() !=
2200 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002201 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002202 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002203 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002204 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002205 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002206 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002207 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002208 }
2209
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002210 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002211 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002212 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002213 GL_CALL(Enable(GR_GL_CULL_FACE));
2214 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002215 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002216 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002217 GL_CALL(Enable(GR_GL_CULL_FACE));
2218 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002219 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002220 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002221 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002222 break;
2223 default:
2224 GrCrash("Unknown draw face.");
2225 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002226 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002227 }
2228
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002229#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002230 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002231 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002232 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002233 NULL == drawState->getRenderTarget() ||
2234 NULL == drawState->getTexture(s) ||
2235 drawState->getTexture(s)->asRenderTarget() !=
2236 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002237 }
2238#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002239
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002240 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002241
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002242 // This copy must happen after flushStencil() is called. flushStencil()
2243 // relies on detecting when the kModifyStencilClip_StateBit state has
2244 // changed since the last draw.
2245 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002246 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002247}
2248
2249void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002250 if (fHWGeometryState.fVertexBuffer != buffer) {
2251 fHWGeometryState.fArrayPtrsDirty = true;
2252 fHWGeometryState.fVertexBuffer = buffer;
2253 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002254}
2255
2256void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002257 if (fHWGeometryState.fVertexBuffer == buffer) {
2258 // deleting bound buffer does implied bind to 0
2259 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002260 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002261 }
2262}
2263
2264void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002265 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002266}
2267
2268void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002269 if (fHWGeometryState.fIndexBuffer == buffer) {
2270 // deleting bound buffer does implied bind to 0
2271 fHWGeometryState.fIndexBuffer = NULL;
2272 }
2273}
2274
reed@google.comac10a2d2010-12-22 21:39:39 +00002275void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2276 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002277 GrDrawState* drawState = this->drawState();
2278 if (drawState->getRenderTarget() == renderTarget) {
2279 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002281 if (fHWDrawState.getRenderTarget() == renderTarget) {
2282 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002283 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002284}
2285
2286void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002287 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002288 GrDrawState* drawState = this->drawState();
2289 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002290 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002291 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002292 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002293 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002294 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002296 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002297}
2298
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002299bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2300 bool getSizedInternalFormat,
2301 GrGLenum* internalFormat,
2302 GrGLenum* externalFormat,
2303 GrGLenum* externalType) {
2304 GrGLenum dontCare;
2305 if (NULL == internalFormat) {
2306 internalFormat = &dontCare;
2307 }
2308 if (NULL == externalFormat) {
2309 externalFormat = &dontCare;
2310 }
2311 if (NULL == externalType) {
2312 externalType = &dontCare;
2313 }
2314
reed@google.comac10a2d2010-12-22 21:39:39 +00002315 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002316 case kRGBA_8888_PM_GrPixelConfig:
2317 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002318 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002319 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002320 if (getSizedInternalFormat) {
2321 *internalFormat = GR_GL_RGBA8;
2322 } else {
2323 *internalFormat = GR_GL_RGBA;
2324 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002325 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002326 break;
2327 case kBGRA_8888_PM_GrPixelConfig:
2328 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002329 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002330 return false;
2331 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002332 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002333 if (getSizedInternalFormat) {
2334 *internalFormat = GR_GL_BGRA8;
2335 } else {
2336 *internalFormat = GR_GL_BGRA;
2337 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002338 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002339 if (getSizedInternalFormat) {
2340 *internalFormat = GR_GL_RGBA8;
2341 } else {
2342 *internalFormat = GR_GL_RGBA;
2343 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002344 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002345 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002346 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002347 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002348 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002349 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002350 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002351 if (getSizedInternalFormat) {
2352 if (this->glBinding() == kDesktop_GrGLBinding) {
2353 return false;
2354 } else {
2355 *internalFormat = GR_GL_RGB565;
2356 }
2357 } else {
2358 *internalFormat = GR_GL_RGB;
2359 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002360 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002361 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002362 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002363 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002364 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002365 if (getSizedInternalFormat) {
2366 *internalFormat = GR_GL_RGBA4;
2367 } else {
2368 *internalFormat = GR_GL_RGBA;
2369 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002370 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002371 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002372 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002373 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002374 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002375 // glCompressedTexImage doesn't take external params
2376 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002377 // no sized/unsized internal format distinction here
2378 *internalFormat = GR_GL_PALETTE8_RGBA8;
2379 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002380 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002381 } else {
2382 return false;
2383 }
2384 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002385 case kAlpha_8_GrPixelConfig:
robertphillips@google.com69950682012-04-06 18:06:10 +00002386 if (this->glCaps().textureRedSupport()) {
2387 *internalFormat = GR_GL_RED;
2388 *externalFormat = GR_GL_RED;
2389 if (getSizedInternalFormat) {
2390 *internalFormat = GR_GL_R8;
2391 } else {
2392 *internalFormat = GR_GL_RED;
2393 }
2394 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002395 } else {
2396 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com69950682012-04-06 18:06:10 +00002397 *externalFormat = GR_GL_ALPHA;
2398 if (getSizedInternalFormat) {
2399 *internalFormat = GR_GL_ALPHA8;
2400 } else {
2401 *internalFormat = GR_GL_ALPHA;
2402 }
2403 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002404 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002405 break;
2406 default:
2407 return false;
2408 }
2409 return true;
2410}
2411
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002412void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002413 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002414 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002415 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002416 fActiveTextureUnitIdx = unit;
2417 }
2418}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002419
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002420void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002421 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002422 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002423 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2424 }
2425}
2426
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002427void GrGpuGL::resetDirtyFlags() {
2428 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2429}
2430
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002431void GrGpuGL::setBuffers(bool indexed,
2432 int* extraVertexOffset,
2433 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002434
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002435 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002436
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002437 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2438
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002440 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002441 case kBuffer_GeometrySrcType:
2442 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002443 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002444 break;
2445 case kArray_GeometrySrcType:
2446 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002447 this->finalizeReservedVertices();
2448 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2449 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002450 break;
2451 default:
2452 vbuf = NULL; // suppress warning
2453 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002454 }
2455
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002456 GrAssert(NULL != vbuf);
2457 GrAssert(!vbuf->isLocked());
2458 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002459 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460 fHWGeometryState.fArrayPtrsDirty = true;
2461 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002462 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002463
2464 if (indexed) {
2465 GrAssert(NULL != extraIndexOffset);
2466
2467 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002468 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002469 case kBuffer_GeometrySrcType:
2470 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002471 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002472 break;
2473 case kArray_GeometrySrcType:
2474 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002475 this->finalizeReservedIndices();
2476 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2477 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002478 break;
2479 default:
2480 ibuf = NULL; // suppress warning
2481 GrCrash("Unknown geometry src type!");
2482 }
2483
2484 GrAssert(NULL != ibuf);
2485 GrAssert(!ibuf->isLocked());
2486 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002487 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002488 fHWGeometryState.fIndexBuffer = ibuf;
2489 }
2490 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002491}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002492
2493int GrGpuGL::getMaxEdges() const {
2494 // FIXME: This is a pessimistic estimate based on how many other things
2495 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002496 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002497 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002498}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002499