blob: dcde84e6b2c04c6bf38ba8f16bc33b15fec11183 [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
284 ////////////////////////////////////////////////////////////////////////////
285 // Experiments to determine limitations that can't be queried.
286 // TODO: Make these a preprocess that generate some compile time constants.
287 // TODO: probe once at startup, rather than once per context creation.
288
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000289 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
290 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000291 // Our render targets are always created with textures as the color
292 // attachment, hence this min:
293 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
294
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000295 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000296}
297
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000298void GrGpuGL::fillInConfigRenderableTable() {
299
300 // OpenGL < 3.0
301 // no support for render targets unless the GL_ARB_framebuffer_object
302 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
303 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
304 // probably don't get R8 in this case.
305
306 // OpenGL 3.0
307 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
308 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
309
310 // >= OpenGL 3.1
311 // base color renderable: RED, RG, RGB, and RGBA
312 // sized derivatives: R8, RGBA4, RGBA8
313 // if the GL_ARB_compatibility extension is supported then we get back
314 // support for GL_ALPHA and ALPHA8
315
316 // GL_EXT_bgra adds BGRA render targets to any version
317
318 // ES 2.0
319 // color renderable: RGBA4, RGB5_A1, RGB565
320 // GL_EXT_texture_rg adds support for R8 as a color render target
321 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
322 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
323 // added BGRA support
324
325 if (kDesktop_GrGLBinding == this->glBinding()) {
326 // Post 3.0 we will get R8
327 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
328 if (this->glVersion() >= GR_GL_VER(3,0) ||
329 this->hasExtension("GL_ARB_framebuffer_object")) {
330 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
331 }
332 } else {
333 // On ES we can only hope for R8
334 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
335 this->glCaps().textureRedSupport();
336 }
337
338 if (kDesktop_GrGLBinding != this->glBinding()) {
339 // only available in ES
340 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
341 }
342
343 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
344 // GL_EXT_framebuffer_object for FBO support. Both of these
345 // allow RGBA4 render targets so this is always supported.
346 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
347
348 if (this->glCaps().rgba8RenderbufferSupport()) {
349 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
350 }
351
352 if (this->glCaps().bgraFormatSupport()) {
353 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
354 }
355
356 // the un-premultiplied formats just inherit the premultiplied setting
357 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
358 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
359 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
360 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
361}
362
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000363bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
364 if (kUnknown_CanPreserveUnpremulRoundtrip ==
365 fCanPreserveUnpremulRoundtrip) {
366
367 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
368 uint32_t* srcData = data.get();
369 uint32_t* firstRead = data.get() + 256 * 256;
370 uint32_t* secondRead = data.get() + 2 * 256 * 256;
371
372 for (int y = 0; y < 256; ++y) {
373 for (int x = 0; x < 256; ++x) {
374 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
375 color[3] = y;
376 color[2] = x;
377 color[1] = x;
378 color[0] = x;
379 }
380 }
381
382 // We have broader support for read/write pixels on render targets
383 // than on textures.
384 GrTextureDesc dstDesc;
385 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
386 kNoStencil_GrTextureFlagBit;
387 dstDesc.fWidth = 256;
388 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000389 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000390 dstDesc.fSampleCnt = 0;
391
392 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
393 if (!dstTex.get()) {
394 return false;
395 }
396 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
397 GrAssert(NULL != rt);
398
399 bool failed = true;
400 static const UnpremulConversion gMethods[] = {
401 kUpOnWrite_DownOnRead_UnpremulConversion,
402 kDownOnWrite_UpOnRead_UnpremulConversion,
403 };
404
405 // pretend that we can do the roundtrip to avoid recursive calls to
406 // this function
407 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
408 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
409 fUnpremulConversion = gMethods[i];
410 rt->writePixels(0, 0,
411 256, 256,
412 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
413 rt->readPixels(0, 0,
414 256, 256,
415 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
416 rt->writePixels(0, 0,
417 256, 256,
418 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
419 rt->readPixels(0, 0,
420 256, 256,
421 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
422 failed = false;
423 for (int j = 0; j < 256 * 256; ++j) {
424 if (firstRead[j] != secondRead[j]) {
425 failed = true;
426 break;
427 }
428 }
429 }
430 fCanPreserveUnpremulRoundtrip = failed ?
431 kNo_CanPreserveUnpremulRoundtrip :
432 kYes_CanPreserveUnpremulRoundtrip;
433 }
434
435 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
436 return true;
437 } else {
438 return false;
439 }
440}
441
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000442GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000443 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
444 return GrPixelConfigSwapRAndB(config);
445 } else {
446 return config;
447 }
448}
449
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000450GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000451 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000452 return GrPixelConfigSwapRAndB(config);
453 } else {
454 return config;
455 }
456}
457
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000458bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
459 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
460}
461
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000462void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000463 if (gPrintStartupSpew && !fPrintedCaps) {
464 fPrintedCaps = true;
465 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000466 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000467 }
468
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000469 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000471 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000472
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000473 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000474 GL_CALL(Disable(GR_GL_DEPTH_TEST));
475 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000476
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000477 GL_CALL(Disable(GR_GL_CULL_FACE));
478 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000479 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000480
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000481 GL_CALL(Disable(GR_GL_DITHER));
482 if (kDesktop_GrGLBinding == this->glBinding()) {
483 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
484 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
485 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000486 fHWAAState.fMSAAEnabled = false;
487 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000488 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000489
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000490 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000491 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000492
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000494 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000496 // invalid
497 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000498
reed@google.comac10a2d2010-12-22 21:39:39 +0000499 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000500 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000501
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000503 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000504
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000505 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000506
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000507 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000508
tomhudson@google.com93813632011-10-27 20:21:16 +0000509 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000510 fHWDrawState.setTexture(s, NULL);
511 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
512 -GR_ScalarMax,
513 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000514 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000515 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000516 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000517
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000518 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000519 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000520 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000521 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000522
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000523 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000525 fClipInStencil = false;
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.com81c3f8d2011-08-03 15:18:33 +0000848bool GrGpuGL::createRenderTargetObjects(int width, int height,
849 GrGLuint texID,
850 GrGLRenderTarget::Desc* desc) {
851 desc->fMSColorRenderbufferID = 0;
852 desc->fRTFBOID = 0;
853 desc->fTexFBOID = 0;
854 desc->fOwnIDs = true;
855
856 GrGLenum status;
857 GrGLint err;
858
bsalomon@google.comab15d612011-08-09 12:57:56 +0000859 GrGLenum msColorFormat = 0; // suppress warning
860
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000861 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000862 if (!desc->fTexFBOID) {
863 goto FAILED;
864 }
865
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866
867 // If we are using multisampling we will create two FBOS. We render
868 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000869 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000870 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000871 goto FAILED;
872 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000873 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
874 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000875 if (!desc->fRTFBOID ||
876 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000877 !this->configToGLFormats(desc->fConfig,
878 // GLES requires sized internal formats
879 kES2_GrGLBinding == this->glBinding(),
880 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000881 goto FAILED;
882 }
883 } else {
884 desc->fRTFBOID = desc->fTexFBOID;
885 }
886
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000887 // below here we may bind the FBO
888 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000889 if (desc->fRTFBOID != desc->fTexFBOID) {
890 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000892 desc->fMSColorRenderbufferID));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000893 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
894 GL_ALLOC_CALL(this->glInterface(),
895 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
896 desc->fSampleCnt,
897 msColorFormat,
898 width, height));
899 err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000900 if (err != GR_GL_NO_ERROR) {
901 goto FAILED;
902 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000903 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
904 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905 GR_GL_COLOR_ATTACHMENT0,
906 GR_GL_RENDERBUFFER,
907 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000908 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000909 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
910 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
911 goto FAILED;
912 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000913 fGLContextInfo.caps().markConfigAsValidColorAttachment(
914 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915 }
916 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000917 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000918
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
920 GR_GL_COLOR_ATTACHMENT0,
921 GR_GL_TEXTURE_2D,
922 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000923 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000924 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
925 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
926 goto FAILED;
927 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000928 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 }
930
931 return true;
932
933FAILED:
934 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000936 }
937 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000938 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 }
940 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000941 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942 }
943 return false;
944}
945
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000946// good to set a break-point here to know when createTexture fails
947static GrTexture* return_null_texture() {
948// GrAssert(!"null texture");
949 return NULL;
950}
951
952#if GR_DEBUG
953static size_t as_size_t(int x) {
954 return x;
955}
956#endif
957
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000958GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000959 const void* srcData,
960 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000961
962#if GR_COLLECT_STATS
963 ++fStats.fTextureCreateCnt;
964#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000965
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000966 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000967 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000968
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000969 // Attempt to catch un- or wrongly initialized sample counts;
970 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
971
bsalomon@google.com99621082011-11-15 16:47:16 +0000972 glTexDesc.fWidth = desc.fWidth;
973 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000974 glTexDesc.fConfig = desc.fConfig;
975 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000976
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000977 glRTDesc.fMSColorRenderbufferID = 0;
978 glRTDesc.fRTFBOID = 0;
979 glRTDesc.fTexFBOID = 0;
980 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000981 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000982
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000983 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000984
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000985 const Caps& caps = this->getCaps();
986
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000987 // We keep GrRenderTargets in GL's normal orientation so that they
988 // can be drawn to by the outside world without the client having
989 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000990 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000991 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000992
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000993 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000994 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000995 desc.fSampleCnt) {
996 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +0000997 }
998
reed@google.comac10a2d2010-12-22 21:39:39 +0000999 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001000 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1001 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001002 return return_null_texture();
1003 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001004 }
1005
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001006 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001007 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001008 // provides a hint about how this texture will be used
1009 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1010 GR_GL_TEXTURE_USAGE,
1011 GR_GL_FRAMEBUFFER_ATTACHMENT));
1012 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001013 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001014 return return_null_texture();
1015 }
1016
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001017 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001018 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001019
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001020 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1021 // drivers have a bug where an FBO won't be complete if it includes a
1022 // texture that is not mipmap complete (considering the filter in use).
1023 GrGLTexture::TexParams initialTexParams;
1024 // we only set a subset here so invalidate first
1025 initialTexParams.invalidate();
1026 initialTexParams.fFilter = GR_GL_NEAREST;
1027 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1028 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001029 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1030 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001031 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001032 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1033 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001034 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001035 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1036 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001037 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001038 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1039 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001040 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001041 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1042 glTexDesc.fWidth, glTexDesc.fHeight,
1043 desc.fConfig, srcData, rowBytes)) {
1044 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1045 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001046 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001047
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001048 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 if (renderTarget) {
1050#if GR_COLLECT_STATS
1051 ++fStats.fRenderTargetCreateCnt;
1052#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001053 // unbind the texture from the texture unit before binding it to the frame buffer
1054 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1055
bsalomon@google.com99621082011-11-15 16:47:16 +00001056 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1057 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001058 glTexDesc.fTextureID,
1059 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001060 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 return return_null_texture();
1062 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001063 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001064 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001065 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001067 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001068#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001069 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1070 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001071#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001072 return tex;
1073}
1074
1075namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001076
1077const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1078
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1080 GrGLuint rb,
1081 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 // we shouldn't ever know one size and not the other
1083 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1084 (kUnknownBitCount == format->fTotalBits));
1085 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001086 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001087 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1088 (GrGLint*)&format->fStencilBits);
1089 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001090 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1092 (GrGLint*)&format->fTotalBits);
1093 format->fTotalBits += format->fStencilBits;
1094 } else {
1095 format->fTotalBits = format->fStencilBits;
1096 }
1097 }
1098}
1099}
1100
1101bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1102 int width, int height) {
1103
1104 // All internally created RTs are also textures. We don't create
1105 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1106 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001107 GrAssert(width >= rt->width());
1108 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109
1110 int samples = rt->numSamples();
1111 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001112 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113 if (!sbID) {
1114 return false;
1115 }
1116
1117 GrGLStencilBuffer* sb = NULL;
1118
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001119 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001120 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001121 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001122 // we start with the last stencil format that succeeded in hopes
1123 // that we won't go through this loop more than once after the
1124 // first (painful) stencil creation.
1125 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001126 const GrGLCaps::StencilFormat& sFmt =
1127 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001128 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001129 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 // version on a GL that doesn't have an MSAA extension.
1131 if (samples > 1) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001132 GL_ALLOC_CALL(this->glInterface(),
1133 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1134 samples,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001135 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001136 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001138 GL_ALLOC_CALL(this->glInterface(),
1139 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001140 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001141 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 }
1143
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001144 GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 if (err == GR_GL_NO_ERROR) {
1146 // After sized formats we attempt an unsized format and take whatever
1147 // sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001148 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001149 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001150 sb = new GrGLStencilBuffer(this, sbID, width, height,
1151 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1153 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001154 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 return true;
1157 }
1158 sb->abandon(); // otherwise we lose sbID
1159 sb->unref();
1160 }
1161 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001162 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001163 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001164}
1165
1166bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1167 GrRenderTarget* rt) {
1168 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1169
1170 GrGLuint fbo = glrt->renderFBOID();
1171
1172 if (NULL == sb) {
1173 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001174 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 GR_GL_STENCIL_ATTACHMENT,
1176 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001177 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 GR_GL_DEPTH_ATTACHMENT,
1179 GR_GL_RENDERBUFFER, 0));
1180#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001181 GrGLenum status;
1182 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1184#endif
1185 }
1186 return true;
1187 } else {
1188 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1189 GrGLuint rb = glsb->renderbufferID();
1190
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001191 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1193 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 GR_GL_STENCIL_ATTACHMENT,
1195 GR_GL_RENDERBUFFER, rb));
1196 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001197 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 GR_GL_DEPTH_ATTACHMENT,
1199 GR_GL_RENDERBUFFER, rb));
1200 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 GR_GL_DEPTH_ATTACHMENT,
1203 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001204 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001206 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001207 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001208 glsb->format())) {
1209 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1210 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001211 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001212 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001214 if (glsb->format().fPacked) {
1215 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1216 GR_GL_DEPTH_ATTACHMENT,
1217 GR_GL_RENDERBUFFER, 0));
1218 }
1219 return false;
1220 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001221 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001222 rt->config(),
1223 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001226 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001227 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001228}
1229
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001230////////////////////////////////////////////////////////////////////////////////
1231
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001232GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001233 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001236 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001237 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001238 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001240 GL_ALLOC_CALL(this->glInterface(),
1241 BufferData(GR_GL_ARRAY_BUFFER,
1242 size,
1243 NULL, // data ptr
1244 dynamic ? GR_GL_DYNAMIC_DRAW :
1245 GR_GL_STATIC_DRAW));
1246 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001247 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001248 // deleting bound buffer does implicit bind to 0
1249 fHWGeometryState.fVertexBuffer = NULL;
1250 return NULL;
1251 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001252 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 size, dynamic);
1254 fHWGeometryState.fVertexBuffer = vertexBuffer;
1255 return vertexBuffer;
1256 }
1257 return NULL;
1258}
1259
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001260GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001261 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001262 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001264 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
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_ELEMENT_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.fIndexBuffer = NULL;
1277 return NULL;
1278 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001279 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 size, dynamic);
1281 fHWGeometryState.fIndexBuffer = indexBuffer;
1282 return indexBuffer;
1283 }
1284 return NULL;
1285}
1286
reed@google.comac10a2d2010-12-22 21:39:39 +00001287void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001288 const GrDrawState& drawState = this->getDrawState();
1289 const GrGLRenderTarget* rt =
1290 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1291
1292 GrAssert(NULL != rt);
1293 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001294
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001295 GrGLIRect scissor;
1296 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001297 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001298 rect->width(), rect->height());
1299 if (scissor.contains(vp)) {
1300 rect = NULL;
1301 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 }
1303
1304 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001305 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001306 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 fHWBounds.fScissorRect = scissor;
1308 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001310 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 fHWBounds.fScissorEnabled = true;
1312 }
1313 } else {
1314 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001315 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 fHWBounds.fScissorEnabled = false;
1317 }
1318 }
1319}
1320
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001321void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001322 const GrDrawState& drawState = this->getDrawState();
1323 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001324 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001325 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001326
bsalomon@google.com74b98712011-11-11 19:46:16 +00001327 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001328 if (NULL != rect) {
1329 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001330 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001331 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001332 if (clippedRect.intersect(rtRect)) {
1333 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001334 } else {
1335 return;
1336 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001338 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001339 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001340
1341 GrGLfloat r, g, b, a;
1342 static const GrGLfloat scale255 = 1.f / 255.f;
1343 a = GrColorUnpackA(color) * scale255;
1344 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001345 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001346 scaleRGB *= a;
1347 }
1348 r = GrColorUnpackR(color) * scaleRGB;
1349 g = GrColorUnpackG(color) * scaleRGB;
1350 b = GrColorUnpackB(color) * scaleRGB;
1351
1352 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001353 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001354 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001355 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001356}
1357
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001358void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001359 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001360 return;
1361 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001362
1363 this->flushRenderTarget(&GrIRect::EmptyIRect());
1364
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001366 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001367 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001368 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001369 GL_CALL(StencilMask(0xffffffff));
1370 GL_CALL(ClearStencil(0));
1371 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001372 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001373}
1374
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001375void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001376 const GrDrawState& drawState = this->getDrawState();
1377 const GrRenderTarget* rt = drawState.getRenderTarget();
1378 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001379
1380 // this should only be called internally when we know we have a
1381 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001382 GrAssert(NULL != rt->getStencilBuffer());
1383 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001384#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001385 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001386 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001387#else
1388 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001389 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001390 // turned into draws. Our contract on GrDrawTarget says that
1391 // changing the clip between stencil passes may or may not
1392 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001393 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001394#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001395 GrGLint value;
1396 if (insideClip) {
1397 value = (1 << (stencilBitCount - 1));
1398 } else {
1399 value = 0;
1400 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001401 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001402 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001403 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001404 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001405 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001406 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001407}
1408
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001409void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001410 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001411}
1412
bsalomon@google.comc4364992011-11-07 15:54:49 +00001413bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1414 int left, int top,
1415 int width, int height,
1416 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001417 size_t rowBytes) const {
1418 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001419 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001420 return false;
1421 }
1422
1423 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001424 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001425 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001426 return true;
1427 }
1428 // If we have to do memcpys to handle rowBytes then y-flip is free
1429 // Note the rowBytes might be tight to the passed in data, but if data
1430 // gets clipped in x to the target the rowBytes will no longer be tight.
1431 if (left >= 0 && (left + width) < renderTarget->width()) {
1432 return 0 == rowBytes ||
1433 GrBytesPerPixel(config) * width == rowBytes;
1434 } else {
1435 return false;
1436 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001437}
1438
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001439bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001440 int left, int top,
1441 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001442 GrPixelConfig config,
1443 void* buffer,
1444 size_t rowBytes,
1445 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001446 GrGLenum format;
1447 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001448 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001449 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001450 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001451 size_t bpp = GrBytesPerPixel(config);
1452 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1453 &left, &top, &width, &height,
1454 const_cast<const void**>(&buffer),
1455 &rowBytes)) {
1456 return false;
1457 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001458
bsalomon@google.comc6980972011-11-02 19:57:21 +00001459 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001460 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001461 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001462 switch (tgt->getResolveType()) {
1463 case GrGLRenderTarget::kCantResolve_ResolveType:
1464 return false;
1465 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001466 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001467 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001468 break;
1469 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001470 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001471 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001472 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1473 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001474 break;
1475 default:
1476 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001477 }
1478
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001479 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001480
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001481 // the read rect is viewport-relative
1482 GrGLIRect readRect;
1483 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001484
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001485 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001486 if (0 == rowBytes) {
1487 rowBytes = tightRowBytes;
1488 }
1489 size_t readDstRowBytes = tightRowBytes;
1490 void* readDst = buffer;
1491
1492 // determine if GL can read using the passed rowBytes or if we need
1493 // a scratch buffer.
1494 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1495 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001496 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001497 GrAssert(!(rowBytes % sizeof(GrColor)));
1498 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1499 readDstRowBytes = rowBytes;
1500 } else {
1501 scratch.reset(tightRowBytes * height);
1502 readDst = scratch.get();
1503 }
1504 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001505 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001506 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1507 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001508 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1509 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 format, type, readDst));
1511 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001512 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001513 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1514 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001515 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001516 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1517 invertY = true;
1518 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001519
1520 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001521 // API presents top-to-bottom. We must preserve the padding contents. Note
1522 // that the above readPixels did not overwrite the padding.
1523 if (readDst == buffer) {
1524 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001525 if (!invertY) {
1526 scratch.reset(tightRowBytes);
1527 void* tmpRow = scratch.get();
1528 // flip y in-place by rows
1529 const int halfY = height >> 1;
1530 char* top = reinterpret_cast<char*>(buffer);
1531 char* bottom = top + (height - 1) * rowBytes;
1532 for (int y = 0; y < halfY; y++) {
1533 memcpy(tmpRow, top, tightRowBytes);
1534 memcpy(top, bottom, tightRowBytes);
1535 memcpy(bottom, tmpRow, tightRowBytes);
1536 top += rowBytes;
1537 bottom -= rowBytes;
1538 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 }
1540 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001541 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001542 // copy from readDst to buffer while flipping y
1543 const int halfY = height >> 1;
1544 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001545 char* dst = reinterpret_cast<char*>(buffer);
1546 if (!invertY) {
1547 dst += (height-1) * rowBytes;
1548 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001549 for (int y = 0; y < height; y++) {
1550 memcpy(dst, src, tightRowBytes);
1551 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001552 if (invertY) {
1553 dst += rowBytes;
1554 } else {
1555 dst -= rowBytes;
1556 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001557 }
1558 }
1559 return true;
1560}
1561
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001562void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001563
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001564 GrGLRenderTarget* rt =
1565 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1566 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001567
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001568 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001569 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001570 #if GR_COLLECT_STATS
1571 ++fStats.fRenderTargetChngCnt;
1572 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001573 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001574 GrGLenum status;
1575 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001576 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001577 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001578 }
1579 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001580 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001581 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001582 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001583 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001584 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001585 fHWBounds.fViewportRect = vp;
1586 }
1587 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001588 if (NULL == bound || !bound->isEmpty()) {
1589 rt->flagAsNeedingResolve(bound);
1590 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001591}
1592
twiz@google.com0f31ca72011-03-18 17:38:11 +00001593GrGLenum gPrimitiveType2GLMode[] = {
1594 GR_GL_TRIANGLES,
1595 GR_GL_TRIANGLE_STRIP,
1596 GR_GL_TRIANGLE_FAN,
1597 GR_GL_POINTS,
1598 GR_GL_LINES,
1599 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001600};
1601
bsalomon@google.comd302f142011-03-03 13:54:13 +00001602#define SWAP_PER_DRAW 0
1603
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001604#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001605 #if GR_MAC_BUILD
1606 #include <AGL/agl.h>
1607 #elif GR_WIN32_BUILD
1608 void SwapBuf() {
1609 DWORD procID = GetCurrentProcessId();
1610 HWND hwnd = GetTopWindow(GetDesktopWindow());
1611 while(hwnd) {
1612 DWORD wndProcID = 0;
1613 GetWindowThreadProcessId(hwnd, &wndProcID);
1614 if(wndProcID == procID) {
1615 SwapBuffers(GetDC(hwnd));
1616 }
1617 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1618 }
1619 }
1620 #endif
1621#endif
1622
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001623void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1624 uint32_t startVertex,
1625 uint32_t startIndex,
1626 uint32_t vertexCount,
1627 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1629
twiz@google.com0f31ca72011-03-18 17:38:11 +00001630 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001631
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001632 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1633 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1634
1635 // our setupGeometry better have adjusted this to zero since
1636 // DrawElements always draws from the begining of the arrays for idx 0.
1637 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001638
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001639 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1640 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001641#if SWAP_PER_DRAW
1642 glFlush();
1643 #if GR_MAC_BUILD
1644 aglSwapBuffers(aglGetCurrentContext());
1645 int set_a_break_pt_here = 9;
1646 aglSwapBuffers(aglGetCurrentContext());
1647 #elif GR_WIN32_BUILD
1648 SwapBuf();
1649 int set_a_break_pt_here = 9;
1650 SwapBuf();
1651 #endif
1652#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001653}
1654
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001655void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1656 uint32_t startVertex,
1657 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001658 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1659
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001660 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1661
1662 // our setupGeometry better have adjusted this to zero.
1663 // DrawElements doesn't take an offset so we always adjus the startVertex.
1664 GrAssert(0 == startVertex);
1665
1666 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1667 // account for startVertex in the DrawElements case. So we always
1668 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001669 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001670#if SWAP_PER_DRAW
1671 glFlush();
1672 #if GR_MAC_BUILD
1673 aglSwapBuffers(aglGetCurrentContext());
1674 int set_a_break_pt_here = 9;
1675 aglSwapBuffers(aglGetCurrentContext());
1676 #elif GR_WIN32_BUILD
1677 SwapBuf();
1678 int set_a_break_pt_here = 9;
1679 SwapBuf();
1680 #endif
1681#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001682}
1683
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001684void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1685
1686 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001687
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001688 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001689 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001690 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001691 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1692 rt->renderFBOID()));
1693 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1694 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001695 #if GR_COLLECT_STATS
1696 ++fStats.fRenderTargetChngCnt;
1697 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001698 // make sure we go through flushRenderTarget() since we've modified
1699 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001700 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001701 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001702 const GrIRect dirtyRect = rt->getResolveRect();
1703 GrGLIRect r;
1704 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1705 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001706
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001707 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001708 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001709 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1710 GL_CALL(Scissor(r.fLeft, r.fBottom,
1711 r.fWidth, r.fHeight));
1712 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001713 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 fHWBounds.fScissorEnabled = true;
1715 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001716 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001717 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001718 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1719 this->glCaps().msFBOType());
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001720 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001721 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001722 int right = r.fLeft + r.fWidth;
1723 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001724 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1725 r.fLeft, r.fBottom, right, top,
1726 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001727 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001728 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001729 }
1730}
1731
twiz@google.com0f31ca72011-03-18 17:38:11 +00001732static const GrGLenum grToGLStencilFunc[] = {
1733 GR_GL_ALWAYS, // kAlways_StencilFunc
1734 GR_GL_NEVER, // kNever_StencilFunc
1735 GR_GL_GREATER, // kGreater_StencilFunc
1736 GR_GL_GEQUAL, // kGEqual_StencilFunc
1737 GR_GL_LESS, // kLess_StencilFunc
1738 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1739 GR_GL_EQUAL, // kEqual_StencilFunc,
1740 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001741};
1742GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1743GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1744GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1745GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1746GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1747GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1748GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1749GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1750GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1751
twiz@google.com0f31ca72011-03-18 17:38:11 +00001752static const GrGLenum grToGLStencilOp[] = {
1753 GR_GL_KEEP, // kKeep_StencilOp
1754 GR_GL_REPLACE, // kReplace_StencilOp
1755 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1756 GR_GL_INCR, // kIncClamp_StencilOp
1757 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1758 GR_GL_DECR, // kDecClamp_StencilOp
1759 GR_GL_ZERO, // kZero_StencilOp
1760 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001761};
1762GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1763GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1764GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1765GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1766GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1767GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1768GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1769GR_STATIC_ASSERT(6 == kZero_StencilOp);
1770GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1771
reed@google.comac10a2d2010-12-22 21:39:39 +00001772void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001773 const GrDrawState& drawState = this->getDrawState();
1774
1775 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001776
1777 // use stencil for clipping if clipping is enabled and the clip
1778 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001779 bool stencilClip = fClipInStencil && drawState.isClipState();
1780 bool drawClipToStencil =
1781 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001782 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1783 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001784 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1785 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001786
1787 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001788
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001789 // we can't simultaneously perform stencil-clipping and
1790 // modify the stencil clip
1791 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001792
bsalomon@google.comd302f142011-03-03 13:54:13 +00001793 if (settings->isDisabled()) {
1794 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001795 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001796 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001797 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001798
1799 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001800 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001801 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001802 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001804 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001805 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1806 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1807 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1808 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1809 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1810 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1811 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1812 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001813 }
1814 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001815 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001816 GrStencilBuffer* stencilBuffer =
1817 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001818 if (NULL != stencilBuffer) {
1819 stencilBits = stencilBuffer->bits();
1820 }
1821 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001822 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001823
1824 GrGLuint clipStencilMask = 0;
1825 GrGLuint userStencilMask = ~0;
1826 if (stencilBits > 0) {
1827 clipStencilMask = 1 << (stencilBits - 1);
1828 userStencilMask = clipStencilMask - 1;
1829 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001831 unsigned int frontRef = settings->frontFuncRef();
1832 unsigned int frontMask = settings->frontFuncMask();
1833 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001834 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001836 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001837 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1838 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001839 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001840 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001841 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001842
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001843 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001844 stencilClip,
1845 clipStencilMask,
1846 userStencilMask,
1847 &frontRef,
1848 &frontMask);
1849 frontWriteMask &= userStencilMask;
1850 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001851 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001852 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001853 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001854 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001855 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001856 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001857 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001858 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001859 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001860 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001862 unsigned int backRef = settings->backFuncRef();
1863 unsigned int backMask = settings->backFuncMask();
1864 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001865
1866
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001867 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001868 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1869 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001870 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001871 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001872 stencilClip, settings->backFunc())];
1873 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001874 stencilClip,
1875 clipStencilMask,
1876 userStencilMask,
1877 &backRef,
1878 &backMask);
1879 backWriteMask &= userStencilMask;
1880 }
1881
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001882 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1883 frontRef, frontMask));
1884 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1885 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1886 backRef, backMask));
1887 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1888 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001889 grToGLStencilOp[settings->frontFailOp()],
1890 grToGLStencilOp[settings->frontPassOp()],
1891 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001892
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001893 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001894 grToGLStencilOp[settings->backFailOp()],
1895 grToGLStencilOp[settings->backPassOp()],
1896 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001898 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1899 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001900 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1901 grToGLStencilOp[settings->frontPassOp()],
1902 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001903 }
1904 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001905 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001906 fHWStencilClip = stencilClip;
1907 }
1908}
1909
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001910void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001911 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001912 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001913 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1914 // smooth lines.
1915
1916 // we prefer smooth lines over multisampled lines
1917 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001918 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001919 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001920 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001921 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001922 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001923 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001924 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001925 fHWAAState.fSmoothLineEnabled = false;
1926 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001927 if (rt->isMultisampled() &&
1928 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001929 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001930 fHWAAState.fMSAAEnabled = false;
1931 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001932 } else if (rt->isMultisampled() &&
1933 this->getDrawState().isHWAntialiasState() !=
1934 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001935 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001936 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001937 fHWAAState.fMSAAEnabled = false;
1938 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001939 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001940 fHWAAState.fMSAAEnabled = true;
1941 }
1942 }
1943 }
1944}
1945
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001946void GrGpuGL::flushBlend(GrPrimitiveType type,
1947 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001948 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001949 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001950 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001951 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001952 fHWBlendDisabled = false;
1953 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001954 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1955 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001956 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1957 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001958 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001959 }
1960 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001961 // any optimization to disable blending should
1962 // have already been applied and tweaked the coeffs
1963 // to (1, 0).
1964 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1965 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001966 if (fHWBlendDisabled != blendOff) {
1967 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001968 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001969 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001970 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001971 }
1972 fHWBlendDisabled = blendOff;
1973 }
1974 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001975 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
1976 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001977 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1978 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001979 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001980 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001981 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001982 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1983 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001984 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001985
1986 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001987 GrColorUnpackR(blendConst) / 255.f,
1988 GrColorUnpackG(blendConst) / 255.f,
1989 GrColorUnpackB(blendConst) / 255.f,
1990 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001991 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001992 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001993 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001994 }
1995 }
1996 }
1997}
1998
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001999namespace {
2000
2001unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002002 switch (filter) {
2003 case GrSamplerState::kBilinear_Filter:
2004 case GrSamplerState::k4x4Downsample_Filter:
2005 return GR_GL_LINEAR;
2006 case GrSamplerState::kNearest_Filter:
2007 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002008 case GrSamplerState::kErode_Filter:
2009 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002010 return GR_GL_NEAREST;
2011 default:
2012 GrAssert(!"Unknown filter type");
2013 return GR_GL_LINEAR;
2014 }
2015}
2016
robertphillips@google.com69950682012-04-06 18:06:10 +00002017// get_swizzle is only called from this .cpp so it is OK to inline it here
2018inline const GrGLenum* get_swizzle(GrPixelConfig config,
2019 const GrSamplerState& sampler,
2020 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002021 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com69950682012-04-06 18:06:10 +00002022 if (glCaps.textureRedSupport()) {
2023 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2024 GR_GL_RED, GR_GL_RED };
2025 return gRedSmear;
2026 } else {
2027 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2028 GR_GL_ALPHA, GR_GL_ALPHA };
2029 return gAlphaSmear;
2030 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002031 } else if (sampler.swapsRAndB()) {
2032 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2033 GR_GL_RED, GR_GL_ALPHA };
2034 return gRedBlueSwap;
2035 } else {
2036 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2037 GR_GL_BLUE, GR_GL_ALPHA };
2038 return gStraight;
2039 }
2040}
2041
2042void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2043 // should add texparameteri to interface to make 1 instead of 4 calls here
2044 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2045 GR_GL_TEXTURE_SWIZZLE_R,
2046 swizzle[0]));
2047 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2048 GR_GL_TEXTURE_SWIZZLE_G,
2049 swizzle[1]));
2050 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2051 GR_GL_TEXTURE_SWIZZLE_B,
2052 swizzle[2]));
2053 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2054 GR_GL_TEXTURE_SWIZZLE_A,
2055 swizzle[3]));
2056}
2057}
2058
bsalomon@google.comffca4002011-02-22 20:34:01 +00002059bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002060
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002061 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002062 // GrGpu::setupClipAndFlushState should have already checked this
2063 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002064 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002065
tomhudson@google.com93813632011-10-27 20:21:16 +00002066 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002067 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002068 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002069 GrGLTexture* nextTexture =
2070 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002071
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002072 // true for now, but maybe not with GrEffect.
2073 GrAssert(NULL != nextTexture);
2074 // if we created a rt/tex and rendered to it without using a
2075 // texture and now we're texuring from the rt it will still be
2076 // the last bound texture, but it needs resolving. So keep this
2077 // out of the "last != next" check.
2078 GrGLRenderTarget* texRT =
2079 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2080 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002081 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002082 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002083
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002084 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002085 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002086 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002087 #if GR_COLLECT_STATS
2088 ++fStats.fTextureChngCnt;
2089 #endif
2090 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002091 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002092 // The texture matrix has to compensate for texture width/height
2093 // and NPOT-embedded-in-POT
2094 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002095 }
2096
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002097 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002098 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002099 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002100 nextTexture->getCachedTexParams(&timestamp);
2101 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002102 GrGLTexture::TexParams newTexParams;
2103
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002104 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002105
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002106 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002107 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2108 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002109 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com69950682012-04-06 18:06:10 +00002110 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002111 sizeof(newTexParams.fSwizzleRGBA));
2112 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002113 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002114 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002115 GR_GL_TEXTURE_MAG_FILTER,
2116 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002117 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002118 GR_GL_TEXTURE_MIN_FILTER,
2119 newTexParams.fFilter));
2120 }
2121 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2122 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002123 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002124 GR_GL_TEXTURE_WRAP_S,
2125 newTexParams.fWrapS));
2126 }
2127 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2128 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002129 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002130 GR_GL_TEXTURE_WRAP_T,
2131 newTexParams.fWrapT));
2132 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002133 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002134 (setAll ||
2135 memcmp(newTexParams.fSwizzleRGBA,
2136 oldTexParams.fSwizzleRGBA,
2137 sizeof(newTexParams.fSwizzleRGBA)))) {
2138 setTextureUnit(s);
2139 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2140 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002141 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002142 nextTexture->setCachedTexParams(newTexParams,
2143 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002144 }
2145 }
2146
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002147 GrIRect* rect = NULL;
2148 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002149 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002150 fClip.hasConservativeBounds()) {
2151 fClip.getConservativeBounds().roundOut(&clipBounds);
2152 rect = &clipBounds;
2153 }
2154 this->flushRenderTarget(rect);
2155 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002156
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002157 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2158 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002159 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002160 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002161 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002162 }
2163 }
2164
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002165 if (drawState->isColorWriteDisabled() !=
2166 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002167 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002168 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002169 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002170 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002171 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002172 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002173 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002174 }
2175
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002176 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002177 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002178 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002179 GL_CALL(Enable(GR_GL_CULL_FACE));
2180 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002181 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002182 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002183 GL_CALL(Enable(GR_GL_CULL_FACE));
2184 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002185 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002186 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002187 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002188 break;
2189 default:
2190 GrCrash("Unknown draw face.");
2191 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002192 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002193 }
2194
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002195#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002196 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002197 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002198 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002199 NULL == drawState->getRenderTarget() ||
2200 NULL == drawState->getTexture(s) ||
2201 drawState->getTexture(s)->asRenderTarget() !=
2202 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002203 }
2204#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002205
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002206 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002207
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002208 // This copy must happen after flushStencil() is called. flushStencil()
2209 // relies on detecting when the kModifyStencilClip_StateBit state has
2210 // changed since the last draw.
2211 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002212 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002213}
2214
2215void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002216 if (fHWGeometryState.fVertexBuffer != buffer) {
2217 fHWGeometryState.fArrayPtrsDirty = true;
2218 fHWGeometryState.fVertexBuffer = buffer;
2219 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002220}
2221
2222void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002223 if (fHWGeometryState.fVertexBuffer == buffer) {
2224 // deleting bound buffer does implied bind to 0
2225 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002226 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002227 }
2228}
2229
2230void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002231 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002232}
2233
2234void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002235 if (fHWGeometryState.fIndexBuffer == buffer) {
2236 // deleting bound buffer does implied bind to 0
2237 fHWGeometryState.fIndexBuffer = NULL;
2238 }
2239}
2240
reed@google.comac10a2d2010-12-22 21:39:39 +00002241void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2242 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002243 GrDrawState* drawState = this->drawState();
2244 if (drawState->getRenderTarget() == renderTarget) {
2245 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002246 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002247 if (fHWDrawState.getRenderTarget() == renderTarget) {
2248 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002249 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002250}
2251
2252void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002253 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002254 GrDrawState* drawState = this->drawState();
2255 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002256 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002257 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002258 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002259 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002260 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002261 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002263}
2264
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002265bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2266 bool getSizedInternalFormat,
2267 GrGLenum* internalFormat,
2268 GrGLenum* externalFormat,
2269 GrGLenum* externalType) {
2270 GrGLenum dontCare;
2271 if (NULL == internalFormat) {
2272 internalFormat = &dontCare;
2273 }
2274 if (NULL == externalFormat) {
2275 externalFormat = &dontCare;
2276 }
2277 if (NULL == externalType) {
2278 externalType = &dontCare;
2279 }
2280
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002282 case kRGBA_8888_PM_GrPixelConfig:
2283 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002284 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002285 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002286 if (getSizedInternalFormat) {
2287 *internalFormat = GR_GL_RGBA8;
2288 } else {
2289 *internalFormat = GR_GL_RGBA;
2290 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002291 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002292 break;
2293 case kBGRA_8888_PM_GrPixelConfig:
2294 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002295 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002296 return false;
2297 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002298 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002299 if (getSizedInternalFormat) {
2300 *internalFormat = GR_GL_BGRA8;
2301 } else {
2302 *internalFormat = GR_GL_BGRA;
2303 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002304 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002305 if (getSizedInternalFormat) {
2306 *internalFormat = GR_GL_RGBA8;
2307 } else {
2308 *internalFormat = GR_GL_RGBA;
2309 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002310 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002311 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002312 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002313 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002314 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002315 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002316 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002317 if (getSizedInternalFormat) {
2318 if (this->glBinding() == kDesktop_GrGLBinding) {
2319 return false;
2320 } else {
2321 *internalFormat = GR_GL_RGB565;
2322 }
2323 } else {
2324 *internalFormat = GR_GL_RGB;
2325 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002326 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002327 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002328 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002329 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002330 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002331 if (getSizedInternalFormat) {
2332 *internalFormat = GR_GL_RGBA4;
2333 } else {
2334 *internalFormat = GR_GL_RGBA;
2335 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002336 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002337 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002338 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002339 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002340 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002341 // glCompressedTexImage doesn't take external params
2342 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002343 // no sized/unsized internal format distinction here
2344 *internalFormat = GR_GL_PALETTE8_RGBA8;
2345 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002346 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002347 } else {
2348 return false;
2349 }
2350 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002351 case kAlpha_8_GrPixelConfig:
robertphillips@google.com69950682012-04-06 18:06:10 +00002352 if (this->glCaps().textureRedSupport()) {
2353 *internalFormat = GR_GL_RED;
2354 *externalFormat = GR_GL_RED;
2355 if (getSizedInternalFormat) {
2356 *internalFormat = GR_GL_R8;
2357 } else {
2358 *internalFormat = GR_GL_RED;
2359 }
2360 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002361 } else {
2362 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com69950682012-04-06 18:06:10 +00002363 *externalFormat = GR_GL_ALPHA;
2364 if (getSizedInternalFormat) {
2365 *internalFormat = GR_GL_ALPHA8;
2366 } else {
2367 *internalFormat = GR_GL_ALPHA;
2368 }
2369 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002370 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002371 break;
2372 default:
2373 return false;
2374 }
2375 return true;
2376}
2377
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002378void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002379 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002380 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002381 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002382 fActiveTextureUnitIdx = unit;
2383 }
2384}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002385
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002386void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002387 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002388 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002389 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2390 }
2391}
2392
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002393void GrGpuGL::resetDirtyFlags() {
2394 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2395}
2396
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002397void GrGpuGL::setBuffers(bool indexed,
2398 int* extraVertexOffset,
2399 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002400
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002401 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002402
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002403 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2404
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002405 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002406 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002407 case kBuffer_GeometrySrcType:
2408 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002409 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002410 break;
2411 case kArray_GeometrySrcType:
2412 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002413 this->finalizeReservedVertices();
2414 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2415 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002416 break;
2417 default:
2418 vbuf = NULL; // suppress warning
2419 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002420 }
2421
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002422 GrAssert(NULL != vbuf);
2423 GrAssert(!vbuf->isLocked());
2424 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002425 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002426 fHWGeometryState.fArrayPtrsDirty = true;
2427 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002428 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002429
2430 if (indexed) {
2431 GrAssert(NULL != extraIndexOffset);
2432
2433 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002434 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002435 case kBuffer_GeometrySrcType:
2436 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002437 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002438 break;
2439 case kArray_GeometrySrcType:
2440 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002441 this->finalizeReservedIndices();
2442 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2443 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002444 break;
2445 default:
2446 ibuf = NULL; // suppress warning
2447 GrCrash("Unknown geometry src type!");
2448 }
2449
2450 GrAssert(NULL != ibuf);
2451 GrAssert(!ibuf->isLocked());
2452 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002453 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002454 fHWGeometryState.fIndexBuffer = ibuf;
2455 }
2456 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002457}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002458
2459int GrGpuGL::getMaxEdges() const {
2460 // FIXME: This is a pessimistic estimate based on how many other things
2461 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002462 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002463 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002464}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002465