blob: a9b345bdba350338a55c706827fa5b81727b9079 [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) {
869 GL_ALLOC_CALL(ctxInfo.interface(),
870 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
871 sampleCount,
872 format,
873 width, height));
874 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
875 }
876 return created;
877}
878}
879
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880bool GrGpuGL::createRenderTargetObjects(int width, int height,
881 GrGLuint texID,
882 GrGLRenderTarget::Desc* desc) {
883 desc->fMSColorRenderbufferID = 0;
884 desc->fRTFBOID = 0;
885 desc->fTexFBOID = 0;
886 desc->fOwnIDs = true;
887
888 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000889
bsalomon@google.comab15d612011-08-09 12:57:56 +0000890 GrGLenum msColorFormat = 0; // suppress warning
891
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000892 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893 if (!desc->fTexFBOID) {
894 goto FAILED;
895 }
896
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897
898 // If we are using multisampling we will create two FBOS. We render
899 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000900 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000901 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000902 goto FAILED;
903 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000904 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
905 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000906 if (!desc->fRTFBOID ||
907 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000908 !this->configToGLFormats(desc->fConfig,
909 // GLES requires sized internal formats
910 kES2_GrGLBinding == this->glBinding(),
911 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 goto FAILED;
913 }
914 } else {
915 desc->fRTFBOID = desc->fTexFBOID;
916 }
917
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000918 // below here we may bind the FBO
919 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000920 if (desc->fRTFBOID != desc->fTexFBOID) {
921 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000923 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000924 if (!renderbuffer_storage_msaa(fGLContextInfo,
925 desc->fSampleCnt,
926 msColorFormat,
927 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 goto FAILED;
929 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000930 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
931 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932 GR_GL_COLOR_ATTACHMENT0,
933 GR_GL_RENDERBUFFER,
934 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000935 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000936 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
937 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
938 goto FAILED;
939 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000940 fGLContextInfo.caps().markConfigAsValidColorAttachment(
941 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942 }
943 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000944 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000946 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
947 GR_GL_COLOR_ATTACHMENT0,
948 GR_GL_TEXTURE_2D,
949 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000950 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000951 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
952 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
953 goto FAILED;
954 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000955 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000956 }
957
958 return true;
959
960FAILED:
961 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000962 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000963 }
964 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000966 }
967 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000968 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000969 }
970 return false;
971}
972
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000973// good to set a break-point here to know when createTexture fails
974static GrTexture* return_null_texture() {
975// GrAssert(!"null texture");
976 return NULL;
977}
978
979#if GR_DEBUG
980static size_t as_size_t(int x) {
981 return x;
982}
983#endif
984
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000985GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000986 const void* srcData,
987 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000988
989#if GR_COLLECT_STATS
990 ++fStats.fTextureCreateCnt;
991#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000992
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000993 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000994 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000995
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000996 // Attempt to catch un- or wrongly initialized sample counts;
997 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
998
bsalomon@google.com99621082011-11-15 16:47:16 +0000999 glTexDesc.fWidth = desc.fWidth;
1000 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001001 glTexDesc.fConfig = desc.fConfig;
1002 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001004 glRTDesc.fMSColorRenderbufferID = 0;
1005 glRTDesc.fRTFBOID = 0;
1006 glRTDesc.fTexFBOID = 0;
1007 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001008 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001009
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001010 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001011
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001012 const Caps& caps = this->getCaps();
1013
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001014 // We keep GrRenderTargets in GL's normal orientation so that they
1015 // can be drawn to by the outside world without the client having
1016 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001017 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001018 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001019
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001020 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001021 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001022 desc.fSampleCnt) {
1023 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 }
1025
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001027 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1028 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001029 return return_null_texture();
1030 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001031 }
1032
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001033 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001034 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001035 // provides a hint about how this texture will be used
1036 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1037 GR_GL_TEXTURE_USAGE,
1038 GR_GL_FRAMEBUFFER_ATTACHMENT));
1039 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001040 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001041 return return_null_texture();
1042 }
1043
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001044 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001045 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001046
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001047 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1048 // drivers have a bug where an FBO won't be complete if it includes a
1049 // texture that is not mipmap complete (considering the filter in use).
1050 GrGLTexture::TexParams initialTexParams;
1051 // we only set a subset here so invalidate first
1052 initialTexParams.invalidate();
1053 initialTexParams.fFilter = GR_GL_NEAREST;
1054 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1055 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001056 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1057 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001058 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1060 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001061 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001062 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1063 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001064 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001065 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1066 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001067 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001068 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1069 glTexDesc.fWidth, glTexDesc.fHeight,
1070 desc.fConfig, srcData, rowBytes)) {
1071 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1072 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001073 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001074
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001076 if (renderTarget) {
1077#if GR_COLLECT_STATS
1078 ++fStats.fRenderTargetCreateCnt;
1079#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001080 // unbind the texture from the texture unit before binding it to the frame buffer
1081 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1082
bsalomon@google.com99621082011-11-15 16:47:16 +00001083 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1084 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001085 glTexDesc.fTextureID,
1086 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001087 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 return return_null_texture();
1089 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001090 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001092 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001094 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001095#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001096 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1097 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001098#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001099 return tex;
1100}
1101
1102namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001103
1104const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1105
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001106void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1107 GrGLuint rb,
1108 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 // we shouldn't ever know one size and not the other
1110 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1111 (kUnknownBitCount == format->fTotalBits));
1112 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1115 (GrGLint*)&format->fStencilBits);
1116 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001117 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1119 (GrGLint*)&format->fTotalBits);
1120 format->fTotalBits += format->fStencilBits;
1121 } else {
1122 format->fTotalBits = format->fStencilBits;
1123 }
1124 }
1125}
1126}
1127
1128bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1129 int width, int height) {
1130
1131 // All internally created RTs are also textures. We don't create
1132 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1133 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001134 GrAssert(width >= rt->width());
1135 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136
1137 int samples = rt->numSamples();
1138 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001140 if (!sbID) {
1141 return false;
1142 }
1143
1144 GrGLStencilBuffer* sb = NULL;
1145
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001146 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001148 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 // we start with the last stencil format that succeeded in hopes
1150 // that we won't go through this loop more than once after the
1151 // first (painful) stencil creation.
1152 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001153 const GrGLCaps::StencilFormat& sFmt =
1154 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001155 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001156 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001158 bool created;
1159 if (samples > 0) {
1160 created = renderbuffer_storage_msaa(fGLContextInfo,
1161 samples,
1162 sFmt.fInternalFormat,
1163 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001164 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001165 GL_ALLOC_CALL(this->glInterface(),
1166 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001167 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001168 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001169 created =
1170 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001172 if (created) {
1173 // After sized formats we attempt an unsized format and take
1174 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001175 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001176 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001177 sb = new GrGLStencilBuffer(this, sbID, width, height,
1178 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1180 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001181 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 return true;
1184 }
1185 sb->abandon(); // otherwise we lose sbID
1186 sb->unref();
1187 }
1188 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001189 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001190 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191}
1192
1193bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1194 GrRenderTarget* rt) {
1195 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1196
1197 GrGLuint fbo = glrt->renderFBOID();
1198
1199 if (NULL == sb) {
1200 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 GR_GL_STENCIL_ATTACHMENT,
1203 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 GR_GL_DEPTH_ATTACHMENT,
1206 GR_GL_RENDERBUFFER, 0));
1207#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001208 GrGLenum status;
1209 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1211#endif
1212 }
1213 return true;
1214 } else {
1215 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1216 GrGLuint rb = glsb->renderbufferID();
1217
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001218 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 GR_GL_STENCIL_ATTACHMENT,
1222 GR_GL_RENDERBUFFER, rb));
1223 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001224 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 GR_GL_DEPTH_ATTACHMENT,
1226 GR_GL_RENDERBUFFER, rb));
1227 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001228 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229 GR_GL_DEPTH_ATTACHMENT,
1230 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001233 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001234 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001235 glsb->format())) {
1236 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1237 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001238 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001239 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001241 if (glsb->format().fPacked) {
1242 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1243 GR_GL_DEPTH_ATTACHMENT,
1244 GR_GL_RENDERBUFFER, 0));
1245 }
1246 return false;
1247 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001248 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001249 rt->config(),
1250 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001253 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001255}
1256
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001257////////////////////////////////////////////////////////////////////////////////
1258
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001259GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001260 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001264 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001265 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001267 GL_ALLOC_CALL(this->glInterface(),
1268 BufferData(GR_GL_ARRAY_BUFFER,
1269 size,
1270 NULL, // data ptr
1271 dynamic ? GR_GL_DYNAMIC_DRAW :
1272 GR_GL_STATIC_DRAW));
1273 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001274 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001275 // deleting bound buffer does implicit bind to 0
1276 fHWGeometryState.fVertexBuffer = NULL;
1277 return NULL;
1278 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001279 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 size, dynamic);
1281 fHWGeometryState.fVertexBuffer = vertexBuffer;
1282 return vertexBuffer;
1283 }
1284 return NULL;
1285}
1286
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001287GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001288 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001291 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001292 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001294 GL_ALLOC_CALL(this->glInterface(),
1295 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1296 size,
1297 NULL, // data ptr
1298 dynamic ? GR_GL_DYNAMIC_DRAW :
1299 GR_GL_STATIC_DRAW));
1300 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001301 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 // deleting bound buffer does implicit bind to 0
1303 fHWGeometryState.fIndexBuffer = NULL;
1304 return NULL;
1305 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001306 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 size, dynamic);
1308 fHWGeometryState.fIndexBuffer = indexBuffer;
1309 return indexBuffer;
1310 }
1311 return NULL;
1312}
1313
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001314void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001315 const GrDrawState& drawState = this->getDrawState();
1316 const GrGLRenderTarget* rt =
1317 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1318
1319 GrAssert(NULL != rt);
1320 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001321
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001322 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001323 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1324 rect.width(), rect.height());
1325 if (scissor.contains(vp)) {
1326 disableScissor();
1327 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 }
1329
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001330 if (fHWBounds.fScissorRect != scissor) {
1331 scissor.pushToGLScissor(this->glInterface());
1332 fHWBounds.fScissorRect = scissor;
1333 }
1334 if (!fHWBounds.fScissorEnabled) {
1335 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1336 fHWBounds.fScissorEnabled = true;
1337 }
1338}
1339
1340void GrGpuGL::disableScissor() {
1341 if (fHWBounds.fScissorEnabled) {
1342 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1343 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 }
1345}
1346
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001347void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001348 const GrDrawState& drawState = this->getDrawState();
1349 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001350 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001351 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001352
bsalomon@google.com74b98712011-11-11 19:46:16 +00001353 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001354 if (NULL != rect) {
1355 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001356 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001357 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001358 if (clippedRect.intersect(rtRect)) {
1359 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001360 } else {
1361 return;
1362 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001364 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001365 if (NULL != rect)
1366 this->enableScissoring(*rect);
1367 else
1368 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001369
1370 GrGLfloat r, g, b, a;
1371 static const GrGLfloat scale255 = 1.f / 255.f;
1372 a = GrColorUnpackA(color) * scale255;
1373 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001374 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001375 scaleRGB *= a;
1376 }
1377 r = GrColorUnpackR(color) * scaleRGB;
1378 g = GrColorUnpackG(color) * scaleRGB;
1379 b = GrColorUnpackB(color) * scaleRGB;
1380
1381 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001382 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001383 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001384 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001385}
1386
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001387void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001388 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001389 return;
1390 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001391
1392 this->flushRenderTarget(&GrIRect::EmptyIRect());
1393
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001394 this->disableScissor();
1395
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001396 GL_CALL(StencilMask(0xffffffff));
1397 GL_CALL(ClearStencil(0));
1398 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001399 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001400}
1401
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001402void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001403 const GrDrawState& drawState = this->getDrawState();
1404 const GrRenderTarget* rt = drawState.getRenderTarget();
1405 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001406
1407 // this should only be called internally when we know we have a
1408 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001409 GrAssert(NULL != rt->getStencilBuffer());
1410 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001411#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001412 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001413 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001414#else
1415 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001416 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001417 // turned into draws. Our contract on GrDrawTarget says that
1418 // changing the clip between stencil passes may or may not
1419 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001420 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001421#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001422 GrGLint value;
1423 if (insideClip) {
1424 value = (1 << (stencilBitCount - 1));
1425 } else {
1426 value = 0;
1427 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001428 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001429 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001430 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001431 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001432 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001433 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001434}
1435
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001436void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001437 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001438}
1439
bsalomon@google.comc4364992011-11-07 15:54:49 +00001440bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1441 int left, int top,
1442 int width, int height,
1443 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001444 size_t rowBytes) const {
1445 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001446 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001447 return false;
1448 }
1449
1450 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001451 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001452 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001453 return true;
1454 }
1455 // If we have to do memcpys to handle rowBytes then y-flip is free
1456 // Note the rowBytes might be tight to the passed in data, but if data
1457 // gets clipped in x to the target the rowBytes will no longer be tight.
1458 if (left >= 0 && (left + width) < renderTarget->width()) {
1459 return 0 == rowBytes ||
1460 GrBytesPerPixel(config) * width == rowBytes;
1461 } else {
1462 return false;
1463 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001464}
1465
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001466bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001467 int left, int top,
1468 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001469 GrPixelConfig config,
1470 void* buffer,
1471 size_t rowBytes,
1472 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001473 GrGLenum format;
1474 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001475 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001476 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001477 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001478 size_t bpp = GrBytesPerPixel(config);
1479 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1480 &left, &top, &width, &height,
1481 const_cast<const void**>(&buffer),
1482 &rowBytes)) {
1483 return false;
1484 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001485
bsalomon@google.comc6980972011-11-02 19:57:21 +00001486 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001487 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001488 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001489 switch (tgt->getResolveType()) {
1490 case GrGLRenderTarget::kCantResolve_ResolveType:
1491 return false;
1492 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001493 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001494 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001495 break;
1496 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001497 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001498 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001499 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1500 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001501 break;
1502 default:
1503 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001504 }
1505
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001506 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001507
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001508 // the read rect is viewport-relative
1509 GrGLIRect readRect;
1510 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001512 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001513 if (0 == rowBytes) {
1514 rowBytes = tightRowBytes;
1515 }
1516 size_t readDstRowBytes = tightRowBytes;
1517 void* readDst = buffer;
1518
1519 // determine if GL can read using the passed rowBytes or if we need
1520 // a scratch buffer.
1521 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1522 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001523 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001524 GrAssert(!(rowBytes % sizeof(GrColor)));
1525 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1526 readDstRowBytes = rowBytes;
1527 } else {
1528 scratch.reset(tightRowBytes * height);
1529 readDst = scratch.get();
1530 }
1531 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001532 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001533 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1534 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001535 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1536 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537 format, type, readDst));
1538 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001539 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001540 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1541 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001542 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001543 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1544 invertY = true;
1545 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001546
1547 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001548 // API presents top-to-bottom. We must preserve the padding contents. Note
1549 // that the above readPixels did not overwrite the padding.
1550 if (readDst == buffer) {
1551 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001552 if (!invertY) {
1553 scratch.reset(tightRowBytes);
1554 void* tmpRow = scratch.get();
1555 // flip y in-place by rows
1556 const int halfY = height >> 1;
1557 char* top = reinterpret_cast<char*>(buffer);
1558 char* bottom = top + (height - 1) * rowBytes;
1559 for (int y = 0; y < halfY; y++) {
1560 memcpy(tmpRow, top, tightRowBytes);
1561 memcpy(top, bottom, tightRowBytes);
1562 memcpy(bottom, tmpRow, tightRowBytes);
1563 top += rowBytes;
1564 bottom -= rowBytes;
1565 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001566 }
1567 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001568 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001569 // copy from readDst to buffer while flipping y
1570 const int halfY = height >> 1;
1571 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001572 char* dst = reinterpret_cast<char*>(buffer);
1573 if (!invertY) {
1574 dst += (height-1) * rowBytes;
1575 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001576 for (int y = 0; y < height; y++) {
1577 memcpy(dst, src, tightRowBytes);
1578 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001579 if (invertY) {
1580 dst += rowBytes;
1581 } else {
1582 dst -= rowBytes;
1583 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001584 }
1585 }
1586 return true;
1587}
1588
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001589void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001590
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001591 GrGLRenderTarget* rt =
1592 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1593 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001594
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001595 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001596 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001597 #if GR_COLLECT_STATS
1598 ++fStats.fRenderTargetChngCnt;
1599 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001600 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001601 GrGLenum status;
1602 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001603 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001604 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001605 }
1606 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001607 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001608 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001609 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001610 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001611 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 fHWBounds.fViewportRect = vp;
1613 }
1614 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001615 if (NULL == bound || !bound->isEmpty()) {
1616 rt->flagAsNeedingResolve(bound);
1617 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001618}
1619
twiz@google.com0f31ca72011-03-18 17:38:11 +00001620GrGLenum gPrimitiveType2GLMode[] = {
1621 GR_GL_TRIANGLES,
1622 GR_GL_TRIANGLE_STRIP,
1623 GR_GL_TRIANGLE_FAN,
1624 GR_GL_POINTS,
1625 GR_GL_LINES,
1626 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001627};
1628
bsalomon@google.comd302f142011-03-03 13:54:13 +00001629#define SWAP_PER_DRAW 0
1630
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001631#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001632 #if GR_MAC_BUILD
1633 #include <AGL/agl.h>
1634 #elif GR_WIN32_BUILD
1635 void SwapBuf() {
1636 DWORD procID = GetCurrentProcessId();
1637 HWND hwnd = GetTopWindow(GetDesktopWindow());
1638 while(hwnd) {
1639 DWORD wndProcID = 0;
1640 GetWindowThreadProcessId(hwnd, &wndProcID);
1641 if(wndProcID == procID) {
1642 SwapBuffers(GetDC(hwnd));
1643 }
1644 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1645 }
1646 }
1647 #endif
1648#endif
1649
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001650void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1651 uint32_t startVertex,
1652 uint32_t startIndex,
1653 uint32_t vertexCount,
1654 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1656
twiz@google.com0f31ca72011-03-18 17:38:11 +00001657 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001658
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001659 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1660 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1661
1662 // our setupGeometry better have adjusted this to zero since
1663 // DrawElements always draws from the begining of the arrays for idx 0.
1664 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001665
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001666 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1667 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001668#if SWAP_PER_DRAW
1669 glFlush();
1670 #if GR_MAC_BUILD
1671 aglSwapBuffers(aglGetCurrentContext());
1672 int set_a_break_pt_here = 9;
1673 aglSwapBuffers(aglGetCurrentContext());
1674 #elif GR_WIN32_BUILD
1675 SwapBuf();
1676 int set_a_break_pt_here = 9;
1677 SwapBuf();
1678 #endif
1679#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001680}
1681
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001682void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1683 uint32_t startVertex,
1684 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001685 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1686
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001687 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1688
1689 // our setupGeometry better have adjusted this to zero.
1690 // DrawElements doesn't take an offset so we always adjus the startVertex.
1691 GrAssert(0 == startVertex);
1692
1693 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1694 // account for startVertex in the DrawElements case. So we always
1695 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001696 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001697#if SWAP_PER_DRAW
1698 glFlush();
1699 #if GR_MAC_BUILD
1700 aglSwapBuffers(aglGetCurrentContext());
1701 int set_a_break_pt_here = 9;
1702 aglSwapBuffers(aglGetCurrentContext());
1703 #elif GR_WIN32_BUILD
1704 SwapBuf();
1705 int set_a_break_pt_here = 9;
1706 SwapBuf();
1707 #endif
1708#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001709}
1710
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001711void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1712
1713 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001714
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001715 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001716 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001717 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001718 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1719 rt->renderFBOID()));
1720 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1721 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 #if GR_COLLECT_STATS
1723 ++fStats.fRenderTargetChngCnt;
1724 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001725 // make sure we go through flushRenderTarget() since we've modified
1726 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001727 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001728 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001729 const GrIRect dirtyRect = rt->getResolveRect();
1730 GrGLIRect r;
1731 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1732 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001733
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001734 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001735 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001736#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001737 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1738 GL_CALL(Scissor(r.fLeft, r.fBottom,
1739 r.fWidth, r.fHeight));
1740 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001741 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001743#else
1744 this->enableScissoring(dirtyRect);
1745 GL_CALL(ResolveMultisampleFramebuffer());
1746#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001748 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001749 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001750 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1751 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001752 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001753 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001754 int right = r.fLeft + r.fWidth;
1755 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001756 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1757 r.fLeft, r.fBottom, right, top,
1758 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001759 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001760 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 }
1762}
1763
twiz@google.com0f31ca72011-03-18 17:38:11 +00001764static const GrGLenum grToGLStencilFunc[] = {
1765 GR_GL_ALWAYS, // kAlways_StencilFunc
1766 GR_GL_NEVER, // kNever_StencilFunc
1767 GR_GL_GREATER, // kGreater_StencilFunc
1768 GR_GL_GEQUAL, // kGEqual_StencilFunc
1769 GR_GL_LESS, // kLess_StencilFunc
1770 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1771 GR_GL_EQUAL, // kEqual_StencilFunc,
1772 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001773};
1774GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1775GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1776GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1777GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1778GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1779GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1780GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1781GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1782GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1783
twiz@google.com0f31ca72011-03-18 17:38:11 +00001784static const GrGLenum grToGLStencilOp[] = {
1785 GR_GL_KEEP, // kKeep_StencilOp
1786 GR_GL_REPLACE, // kReplace_StencilOp
1787 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1788 GR_GL_INCR, // kIncClamp_StencilOp
1789 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1790 GR_GL_DECR, // kDecClamp_StencilOp
1791 GR_GL_ZERO, // kZero_StencilOp
1792 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001793};
1794GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1795GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1796GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1797GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1798GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1799GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1800GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1801GR_STATIC_ASSERT(6 == kZero_StencilOp);
1802GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1803
reed@google.comac10a2d2010-12-22 21:39:39 +00001804void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001805 const GrDrawState& drawState = this->getDrawState();
1806
1807 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001808
1809 // use stencil for clipping if clipping is enabled and the clip
1810 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001811 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001812 bool drawClipToStencil =
1813 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001814 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1815 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001816 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1817 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001818
1819 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001820
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001821 // we can't simultaneously perform stencil-clipping and
1822 // modify the stencil clip
1823 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001824
bsalomon@google.comd302f142011-03-03 13:54:13 +00001825 if (settings->isDisabled()) {
1826 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001827 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001829 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830
1831 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001832 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001833 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001834 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001836 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001837 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1838 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1839 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1840 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1841 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1842 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1843 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1844 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001845 }
1846 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001847 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001848 GrStencilBuffer* stencilBuffer =
1849 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001850 if (NULL != stencilBuffer) {
1851 stencilBits = stencilBuffer->bits();
1852 }
1853 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001854 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001855
1856 GrGLuint clipStencilMask = 0;
1857 GrGLuint userStencilMask = ~0;
1858 if (stencilBits > 0) {
1859 clipStencilMask = 1 << (stencilBits - 1);
1860 userStencilMask = clipStencilMask - 1;
1861 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001862
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001863 unsigned int frontRef = settings->frontFuncRef();
1864 unsigned int frontMask = settings->frontFuncMask();
1865 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001866 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001867
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001868 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001869 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1870 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001871 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001872 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001873 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001874
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001875 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001876 stencilClip,
1877 clipStencilMask,
1878 userStencilMask,
1879 &frontRef,
1880 &frontMask);
1881 frontWriteMask &= userStencilMask;
1882 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001883 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001884 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001885 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001886 settings->frontPassOp() < 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->backFailOp() < 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->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001891 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001892 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001893
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001894 unsigned int backRef = settings->backFuncRef();
1895 unsigned int backMask = settings->backFuncMask();
1896 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897
1898
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001899 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001900 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1901 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001902 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001903 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001904 stencilClip, settings->backFunc())];
1905 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001906 stencilClip,
1907 clipStencilMask,
1908 userStencilMask,
1909 &backRef,
1910 &backMask);
1911 backWriteMask &= userStencilMask;
1912 }
1913
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001914 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1915 frontRef, frontMask));
1916 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1917 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1918 backRef, backMask));
1919 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1920 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001921 grToGLStencilOp[settings->frontFailOp()],
1922 grToGLStencilOp[settings->frontPassOp()],
1923 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001924
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001925 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001926 grToGLStencilOp[settings->backFailOp()],
1927 grToGLStencilOp[settings->backPassOp()],
1928 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001929 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001930 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1931 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001932 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1933 grToGLStencilOp[settings->frontPassOp()],
1934 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001935 }
1936 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001937 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001938 fHWStencilClip = stencilClip;
1939 }
1940}
1941
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001942void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001943 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001944 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001945 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1946 // smooth lines.
1947
1948 // we prefer smooth lines over multisampled lines
1949 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001950 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001951 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001952 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001953 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001954 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001955 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001956 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001957 fHWAAState.fSmoothLineEnabled = false;
1958 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001959 if (rt->isMultisampled() &&
1960 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001961 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001962 fHWAAState.fMSAAEnabled = false;
1963 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001964 } else if (rt->isMultisampled() &&
1965 this->getDrawState().isHWAntialiasState() !=
1966 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001967 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001968 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001969 fHWAAState.fMSAAEnabled = false;
1970 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001971 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001972 fHWAAState.fMSAAEnabled = true;
1973 }
1974 }
1975 }
1976}
1977
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001978void GrGpuGL::flushBlend(GrPrimitiveType type,
1979 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001980 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001981 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001982 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001983 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001984 fHWBlendDisabled = false;
1985 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001986 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1987 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001988 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1989 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001990 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001991 }
1992 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001993 // any optimization to disable blending should
1994 // have already been applied and tweaked the coeffs
1995 // to (1, 0).
1996 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1997 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001998 if (fHWBlendDisabled != blendOff) {
1999 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002000 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002001 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002002 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002003 }
2004 fHWBlendDisabled = blendOff;
2005 }
2006 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002007 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2008 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002009 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2010 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002011 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002012 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002013 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002014 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2015 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002016 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002017
2018 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002019 GrColorUnpackR(blendConst) / 255.f,
2020 GrColorUnpackG(blendConst) / 255.f,
2021 GrColorUnpackB(blendConst) / 255.f,
2022 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002023 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002024 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002025 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002026 }
2027 }
2028 }
2029}
2030
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002031namespace {
2032
2033unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002034 switch (filter) {
2035 case GrSamplerState::kBilinear_Filter:
2036 case GrSamplerState::k4x4Downsample_Filter:
2037 return GR_GL_LINEAR;
2038 case GrSamplerState::kNearest_Filter:
2039 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002040 case GrSamplerState::kErode_Filter:
2041 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002042 return GR_GL_NEAREST;
2043 default:
2044 GrAssert(!"Unknown filter type");
2045 return GR_GL_LINEAR;
2046 }
2047}
2048
robertphillips@google.com69950682012-04-06 18:06:10 +00002049// get_swizzle is only called from this .cpp so it is OK to inline it here
2050inline const GrGLenum* get_swizzle(GrPixelConfig config,
2051 const GrSamplerState& sampler,
2052 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002053 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com69950682012-04-06 18:06:10 +00002054 if (glCaps.textureRedSupport()) {
2055 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2056 GR_GL_RED, GR_GL_RED };
2057 return gRedSmear;
2058 } else {
2059 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2060 GR_GL_ALPHA, GR_GL_ALPHA };
2061 return gAlphaSmear;
2062 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002063 } else if (sampler.swapsRAndB()) {
2064 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2065 GR_GL_RED, GR_GL_ALPHA };
2066 return gRedBlueSwap;
2067 } else {
2068 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2069 GR_GL_BLUE, GR_GL_ALPHA };
2070 return gStraight;
2071 }
2072}
2073
2074void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2075 // should add texparameteri to interface to make 1 instead of 4 calls here
2076 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2077 GR_GL_TEXTURE_SWIZZLE_R,
2078 swizzle[0]));
2079 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2080 GR_GL_TEXTURE_SWIZZLE_G,
2081 swizzle[1]));
2082 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2083 GR_GL_TEXTURE_SWIZZLE_B,
2084 swizzle[2]));
2085 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2086 GR_GL_TEXTURE_SWIZZLE_A,
2087 swizzle[3]));
2088}
2089}
2090
bsalomon@google.comffca4002011-02-22 20:34:01 +00002091bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002092
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002093 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002094 // GrGpu::setupClipAndFlushState should have already checked this
2095 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002096 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002097
tomhudson@google.com93813632011-10-27 20:21:16 +00002098 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002099 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002100 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002101 GrGLTexture* nextTexture =
2102 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002103
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002104 // true for now, but maybe not with GrEffect.
2105 GrAssert(NULL != nextTexture);
2106 // if we created a rt/tex and rendered to it without using a
2107 // texture and now we're texuring from the rt it will still be
2108 // the last bound texture, but it needs resolving. So keep this
2109 // out of the "last != next" check.
2110 GrGLRenderTarget* texRT =
2111 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2112 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002113 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002114 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002115
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002116 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002117 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002118 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002119 #if GR_COLLECT_STATS
2120 ++fStats.fTextureChngCnt;
2121 #endif
2122 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002123 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002124 // The texture matrix has to compensate for texture width/height
2125 // and NPOT-embedded-in-POT
2126 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002127 }
2128
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002129 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002130 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002131 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002132 nextTexture->getCachedTexParams(&timestamp);
2133 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002134 GrGLTexture::TexParams newTexParams;
2135
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002136 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002137
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002138 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002139 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2140 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002141 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com69950682012-04-06 18:06:10 +00002142 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002143 sizeof(newTexParams.fSwizzleRGBA));
2144 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002145 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002146 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002147 GR_GL_TEXTURE_MAG_FILTER,
2148 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002149 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002150 GR_GL_TEXTURE_MIN_FILTER,
2151 newTexParams.fFilter));
2152 }
2153 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2154 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002155 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002156 GR_GL_TEXTURE_WRAP_S,
2157 newTexParams.fWrapS));
2158 }
2159 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2160 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002161 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002162 GR_GL_TEXTURE_WRAP_T,
2163 newTexParams.fWrapT));
2164 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002165 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002166 (setAll ||
2167 memcmp(newTexParams.fSwizzleRGBA,
2168 oldTexParams.fSwizzleRGBA,
2169 sizeof(newTexParams.fSwizzleRGBA)))) {
2170 setTextureUnit(s);
2171 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2172 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002173 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002174 nextTexture->setCachedTexParams(newTexParams,
2175 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002176 }
2177 }
2178
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002179 GrIRect* rect = NULL;
2180 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002181 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002182 fClip.hasConservativeBounds()) {
2183 fClip.getConservativeBounds().roundOut(&clipBounds);
2184 rect = &clipBounds;
2185 }
2186 this->flushRenderTarget(rect);
2187 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002188
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002189 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2190 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002191 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002193 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002194 }
2195 }
2196
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002197 if (drawState->isColorWriteDisabled() !=
2198 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002199 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002200 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002201 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002202 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002203 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002204 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002205 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002206 }
2207
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002208 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002209 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002210 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002211 GL_CALL(Enable(GR_GL_CULL_FACE));
2212 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002213 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002214 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002215 GL_CALL(Enable(GR_GL_CULL_FACE));
2216 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002218 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002220 break;
2221 default:
2222 GrCrash("Unknown draw face.");
2223 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002224 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002225 }
2226
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002227#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002228 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002229 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002230 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002231 NULL == drawState->getRenderTarget() ||
2232 NULL == drawState->getTexture(s) ||
2233 drawState->getTexture(s)->asRenderTarget() !=
2234 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002235 }
2236#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002237
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002238 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002239
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002240 // This copy must happen after flushStencil() is called. flushStencil()
2241 // relies on detecting when the kModifyStencilClip_StateBit state has
2242 // changed since the last draw.
2243 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002244 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002245}
2246
2247void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002248 if (fHWGeometryState.fVertexBuffer != buffer) {
2249 fHWGeometryState.fArrayPtrsDirty = true;
2250 fHWGeometryState.fVertexBuffer = buffer;
2251 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002252}
2253
2254void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 if (fHWGeometryState.fVertexBuffer == buffer) {
2256 // deleting bound buffer does implied bind to 0
2257 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002258 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 }
2260}
2261
2262void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002263 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002264}
2265
2266void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002267 if (fHWGeometryState.fIndexBuffer == buffer) {
2268 // deleting bound buffer does implied bind to 0
2269 fHWGeometryState.fIndexBuffer = NULL;
2270 }
2271}
2272
reed@google.comac10a2d2010-12-22 21:39:39 +00002273void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2274 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002275 GrDrawState* drawState = this->drawState();
2276 if (drawState->getRenderTarget() == renderTarget) {
2277 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002278 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002279 if (fHWDrawState.getRenderTarget() == renderTarget) {
2280 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002282}
2283
2284void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002285 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002286 GrDrawState* drawState = this->drawState();
2287 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002288 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002289 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002290 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002291 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002292 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002293 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002294 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002295}
2296
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002297bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2298 bool getSizedInternalFormat,
2299 GrGLenum* internalFormat,
2300 GrGLenum* externalFormat,
2301 GrGLenum* externalType) {
2302 GrGLenum dontCare;
2303 if (NULL == internalFormat) {
2304 internalFormat = &dontCare;
2305 }
2306 if (NULL == externalFormat) {
2307 externalFormat = &dontCare;
2308 }
2309 if (NULL == externalType) {
2310 externalType = &dontCare;
2311 }
2312
reed@google.comac10a2d2010-12-22 21:39:39 +00002313 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002314 case kRGBA_8888_PM_GrPixelConfig:
2315 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002316 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002317 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002318 if (getSizedInternalFormat) {
2319 *internalFormat = GR_GL_RGBA8;
2320 } else {
2321 *internalFormat = GR_GL_RGBA;
2322 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002323 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002324 break;
2325 case kBGRA_8888_PM_GrPixelConfig:
2326 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002327 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002328 return false;
2329 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002330 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002331 if (getSizedInternalFormat) {
2332 *internalFormat = GR_GL_BGRA8;
2333 } else {
2334 *internalFormat = GR_GL_BGRA;
2335 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002336 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002337 if (getSizedInternalFormat) {
2338 *internalFormat = GR_GL_RGBA8;
2339 } else {
2340 *internalFormat = GR_GL_RGBA;
2341 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002342 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002343 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002344 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002345 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002346 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002347 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002348 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002349 if (getSizedInternalFormat) {
2350 if (this->glBinding() == kDesktop_GrGLBinding) {
2351 return false;
2352 } else {
2353 *internalFormat = GR_GL_RGB565;
2354 }
2355 } else {
2356 *internalFormat = GR_GL_RGB;
2357 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002358 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002359 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002360 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002361 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002362 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002363 if (getSizedInternalFormat) {
2364 *internalFormat = GR_GL_RGBA4;
2365 } else {
2366 *internalFormat = GR_GL_RGBA;
2367 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002368 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002369 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002370 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002371 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002372 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002373 // glCompressedTexImage doesn't take external params
2374 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002375 // no sized/unsized internal format distinction here
2376 *internalFormat = GR_GL_PALETTE8_RGBA8;
2377 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002378 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002379 } else {
2380 return false;
2381 }
2382 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002383 case kAlpha_8_GrPixelConfig:
robertphillips@google.com69950682012-04-06 18:06:10 +00002384 if (this->glCaps().textureRedSupport()) {
2385 *internalFormat = GR_GL_RED;
2386 *externalFormat = GR_GL_RED;
2387 if (getSizedInternalFormat) {
2388 *internalFormat = GR_GL_R8;
2389 } else {
2390 *internalFormat = GR_GL_RED;
2391 }
2392 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002393 } else {
2394 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com69950682012-04-06 18:06:10 +00002395 *externalFormat = GR_GL_ALPHA;
2396 if (getSizedInternalFormat) {
2397 *internalFormat = GR_GL_ALPHA8;
2398 } else {
2399 *internalFormat = GR_GL_ALPHA;
2400 }
2401 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002402 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002403 break;
2404 default:
2405 return false;
2406 }
2407 return true;
2408}
2409
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002410void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002411 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002412 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002413 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002414 fActiveTextureUnitIdx = unit;
2415 }
2416}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002417
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002418void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002419 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002420 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002421 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2422 }
2423}
2424
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002425void GrGpuGL::resetDirtyFlags() {
2426 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2427}
2428
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002429void GrGpuGL::setBuffers(bool indexed,
2430 int* extraVertexOffset,
2431 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002432
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002433 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002434
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002435 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2436
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002437 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002438 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439 case kBuffer_GeometrySrcType:
2440 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002441 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002442 break;
2443 case kArray_GeometrySrcType:
2444 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002445 this->finalizeReservedVertices();
2446 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2447 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002448 break;
2449 default:
2450 vbuf = NULL; // suppress warning
2451 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002452 }
2453
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002454 GrAssert(NULL != vbuf);
2455 GrAssert(!vbuf->isLocked());
2456 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002457 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002458 fHWGeometryState.fArrayPtrsDirty = true;
2459 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002460 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002461
2462 if (indexed) {
2463 GrAssert(NULL != extraIndexOffset);
2464
2465 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002466 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002467 case kBuffer_GeometrySrcType:
2468 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002469 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002470 break;
2471 case kArray_GeometrySrcType:
2472 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002473 this->finalizeReservedIndices();
2474 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2475 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002476 break;
2477 default:
2478 ibuf = NULL; // suppress warning
2479 GrCrash("Unknown geometry src type!");
2480 }
2481
2482 GrAssert(NULL != ibuf);
2483 GrAssert(!ibuf->isLocked());
2484 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002485 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002486 fHWGeometryState.fIndexBuffer = ibuf;
2487 }
2488 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002489}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002490
2491int GrGpuGL::getMaxEdges() const {
2492 // FIXME: This is a pessimistic estimate based on how many other things
2493 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002494 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002495 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002496}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002497