blob: 70d28d67854dd15449fc1bec5992d3e1bfdf0fb4 [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
twiz@google.com0f31ca72011-03-18 17:38:11 +000037static const GrGLenum gXfermodeCoeff2Blend[] = {
38 GR_GL_ZERO,
39 GR_GL_ONE,
40 GR_GL_SRC_COLOR,
41 GR_GL_ONE_MINUS_SRC_COLOR,
42 GR_GL_DST_COLOR,
43 GR_GL_ONE_MINUS_DST_COLOR,
44 GR_GL_SRC_ALPHA,
45 GR_GL_ONE_MINUS_SRC_ALPHA,
46 GR_GL_DST_ALPHA,
47 GR_GL_ONE_MINUS_DST_ALPHA,
48 GR_GL_CONSTANT_COLOR,
49 GR_GL_ONE_MINUS_CONSTANT_COLOR,
50 GR_GL_CONSTANT_ALPHA,
51 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000052
53 // extended blend coeffs
54 GR_GL_SRC1_COLOR,
55 GR_GL_ONE_MINUS_SRC1_COLOR,
56 GR_GL_SRC1_ALPHA,
57 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000058};
59
bsalomon@google.com271cffc2011-05-20 14:13:56 +000060bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000061 static const bool gCoeffReferencesBlendConst[] = {
62 false,
63 false,
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 true,
73 true,
74 true,
75 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000076
77 // extended blend coeffs
78 false,
79 false,
80 false,
81 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000082 };
83 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000084 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
85
86 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
87 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
88 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
89 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
90 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
91 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
92 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
93 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
94 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
95 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
96 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
97 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
98 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
99 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
100
101 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
102 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
103 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
104 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
105
106 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
107 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000108}
109
reed@google.comac10a2d2010-12-22 21:39:39 +0000110///////////////////////////////////////////////////////////////////////////////
111
bsalomon@google.comd302f142011-03-03 13:54:13 +0000112void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
113 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000114 GrMatrix* matrix) {
115 GrAssert(NULL != texture);
116 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000117 GrGLTexture::Orientation orientation = texture->orientation();
118 if (GrGLTexture::kBottomUp_Orientation == orientation) {
119 GrMatrix invY;
120 invY.setAll(GR_Scalar1, 0, 0,
121 0, -GR_Scalar1, GR_Scalar1,
122 0, 0, GrMatrix::I()[8]);
123 matrix->postConcat(invY);
124 } else {
125 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
126 }
127}
128
bsalomon@google.comd302f142011-03-03 13:54:13 +0000129bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000130 const GrSamplerState& sampler) {
131 GrAssert(NULL != texture);
132 if (!sampler.getMatrix().isIdentity()) {
133 return false;
134 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000135 GrGLTexture::Orientation orientation = texture->orientation();
136 if (GrGLTexture::kBottomUp_Orientation == orientation) {
137 return false;
138 } else {
139 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
140 }
141 return true;
142}
143
144///////////////////////////////////////////////////////////////////////////////
145
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000146static bool gPrintStartupSpew;
147
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000148static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000149
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000150 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000151
twiz@google.com0f31ca72011-03-18 17:38:11 +0000152 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000153 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
154 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000155 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000156 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
157 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000158 // some implementations require texture to be mip-map complete before
159 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000160 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_TEXTURE_MIN_FILTER,
162 GR_GL_NEAREST));
163 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
164 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
165 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
166 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
167 GR_GL_COLOR_ATTACHMENT0,
168 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000169 GrGLenum status;
170 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
172 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000173
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000174 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000175}
176
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000177GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000178
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000179 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000180
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000181 gl->ref();
182 fGL = gl;
183 fGLBinding = glBinding;
184 switch (glBinding) {
185 case kDesktop_GrGLBinding:
186 GrAssert(gl->supportsDesktop());
187 break;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000188 case kES2_GrGLBinding:
189 GrAssert(gl->supportsES2());
190 break;
191 default:
192 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000193 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000194
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000195 GrGLClearErr(fGL);
196
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000197 const GrGLubyte* ext;
198 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000199 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000200 const GrGLubyte* vendor;
201 const GrGLubyte* renderer;
202 const GrGLubyte* version;
203 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
204 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
205 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000206 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
207 this);
208 GrPrintf("------ VENDOR %s\n", vendor);
209 GrPrintf("------ RENDERER %s\n", renderer);
210 GrPrintf("------ VERSION %s\n", version);
211 GrPrintf("------ EXTENSIONS\n %s \n", ext);
212 }
213
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000214 fGLVersion = GrGLGetVersion(gl);
215 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000216 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000217
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000218 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000219
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000220 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000221
bsalomon@google.comfe676522011-06-17 18:12:21 +0000222 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000223}
224
225GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000226 // This must be called by before the GrDrawTarget destructor
227 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000228 // This subclass must do this before the base class destructor runs
229 // since we will unref the GrGLInterface.
230 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000231 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000232}
233
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234///////////////////////////////////////////////////////////////////////////////
235
236static const GrGLuint kUnknownBitCount = ~0;
237
238void GrGpuGL::initCaps() {
239 GrGLint maxTextureUnits;
240 // check FS and fixed-function texture unit limits
241 // we only use textures in the fragment stage currently.
242 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000243 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
244 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000245 if (kES2_GrGLBinding != this->glBinding()) {
246 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000247 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000248 }
249 if (kES2_GrGLBinding == this->glBinding()) {
250 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
251 &fGLCaps.fMaxFragmentUniformVectors);
252 } else if (kDesktop_GrGLBinding != this->glBinding()) {
253 GrGLint max;
254 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
255 fGLCaps.fMaxFragmentUniformVectors = max / 4;
256 } else {
257 fGLCaps.fMaxFragmentUniformVectors = 16;
258 }
259
260 GrGLint numFormats;
261 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
262 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
263 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
264 for (int i = 0; i < numFormats; ++i) {
265 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
266 fCaps.f8BitPaletteSupport = true;
267 break;
268 }
269 }
270
271 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000272 // we could also look for GL_ATI_separate_stencil extension or
273 // GL_EXT_stencil_two_side but they use different function signatures
274 // than GL2.0+ (and than each other).
275 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
276 // supported on GL 1.4 and higher or by extension
277 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
278 this->hasExtension("GL_EXT_stencil_wrap");
279 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000280 // ES 2 has two sided stencil and stencil wrap
281 fCaps.fTwoSidedStencilSupport = true;
282 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000283 }
284
285 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000286 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000287 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000288 fGLCaps.fRGBA8RenderbufferSupport =
289 this->hasExtension("GL_OES_rgb8_rgba8") ||
290 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000291 }
292
293
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000295 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
296 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 } else {
298 bool hasBGRAExt = false;
299 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000300 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000302 fGLCaps.fBGRAFormatSupport = true;
303 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000304 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000305 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000306 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000307 }
308
309 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000310 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000311 this->hasExtension("GL_ARB_texture_swizzle");
312 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000313 fGLCaps.fTextureSwizzleSupport = false;
314 }
315
316 if (kDesktop_GrGLBinding == this->glBinding()) {
317 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000318 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000319 fGLCaps.fPackRowLengthSupport = true;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000320 fGLCaps.fPackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000321 } else {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000322 fGLCaps.fUnpackRowLengthSupport =this->hasExtension("GL_EXT_unpack_subimage");
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000323 fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy");
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000324 // no extension for pack row length
325 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000326 fGLCaps.fPackFlipYSupport =
327 this->hasExtension("GL_ANGLE_pack_reverse_row_order");
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000328 }
329
330 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000331 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
332 // extension includes glMapBuffer.
333 } else {
334 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
335 }
336
337 if (kDesktop_GrGLBinding == this->glBinding()) {
338 if (fGLVersion >= GR_GL_VER(2,0) ||
339 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
340 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000341 } else {
342 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000343 }
344 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000345 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000346 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000347 }
348
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000349 fGLCaps.fTextureUsageSupport = (kES2_GrGLBinding == this->glBinding()) &&
350 this->hasExtension("GL_ANGLE_texture_usage");
351
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000352 // Tex storage is in desktop 4.2 and can be an extension to desktop or ES.
353 fGLCaps.fTexStorageSupport = (kDesktop_GrGLBinding == this->glBinding() &&
354 fGLVersion >= GR_GL_VER(4,2)) ||
355 this->hasExtension("GL_ARB_texture_storage") ||
356 this->hasExtension("GL_EXT_texture_storage");
357
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000358 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
359
360 ////////////////////////////////////////////////////////////////////////////
361 // Experiments to determine limitations that can't be queried.
362 // TODO: Make these a preprocess that generate some compile time constants.
363 // TODO: probe once at startup, rather than once per context creation.
364
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000365 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
366 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
367 // Our render targets are always created with textures as the color
368 // attachment, hence this min:
369 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
370
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000371 this->initFSAASupport();
372 this->initStencilFormats();
373}
374
375void GrGpuGL::initFSAASupport() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000376
377 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
378 if (kDesktop_GrGLBinding != this->glBinding()) {
379 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
380 // chrome's extension is equivalent to the EXT msaa
381 // and fbo_blit extensions.
382 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
383 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
384 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
385 }
386 } else {
387 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
388 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
389 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
390 this->hasExtension("GL_EXT_framebuffer_blit")) {
391 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
392 }
393 }
394
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000395 fCaps.fFSAASupport = GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000396}
397
398void GrGpuGL::initStencilFormats() {
399
400 // Build up list of legal stencil formats (though perhaps not supported on
401 // the particular gpu/driver) from most preferred to least.
402
403 // these consts are in order of most preferred to least preferred
404 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
405 static const GrGLStencilBuffer::Format
406 // internal Format stencil bits total bits packed?
407 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
408 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
409 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
410 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
411 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
412 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
413
414 if (kDesktop_GrGLBinding == this->glBinding()) {
415 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
416 this->hasExtension("GL_EXT_packed_depth_stencil") ||
417 this->hasExtension("GL_ARB_framebuffer_object");
418
419 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
420 // require FBO support we can expect these are legal formats and don't
421 // check. These also all support the unsized GL_STENCIL_INDEX.
422 fGLCaps.fStencilFormats.push_back() = gS8;
423 fGLCaps.fStencilFormats.push_back() = gS16;
424 if (supportsPackedDS) {
425 fGLCaps.fStencilFormats.push_back() = gD24S8;
426 }
427 fGLCaps.fStencilFormats.push_back() = gS4;
428 if (supportsPackedDS) {
429 fGLCaps.fStencilFormats.push_back() = gDS;
430 }
431 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000432 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
433 // for other formats.
434 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000435
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000436 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000437 //fStencilFormats.push_back() = gS16;
438 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
439 fGLCaps.fStencilFormats.push_back() = gD24S8;
440 }
441 if (this->hasExtension("GL_OES_stencil4")) {
442 fGLCaps.fStencilFormats.push_back() = gS4;
443 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000444 }
445}
446
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000447GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000448 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
449 return GrPixelConfigSwapRAndB(config);
450 } else {
451 return config;
452 }
453}
454
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000455GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000456 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000457 return GrPixelConfigSwapRAndB(config);
458 } else {
459 return config;
460 }
461}
462
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000463bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
464 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
465}
466
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000467void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000468 if (gPrintStartupSpew && !fPrintedCaps) {
469 fPrintedCaps = true;
470 this->getCaps().print();
471 fGLCaps.print();
472 }
473
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000474 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000476 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000478 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000479 GL_CALL(Disable(GR_GL_DEPTH_TEST));
480 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000481
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000482 GL_CALL(Disable(GR_GL_CULL_FACE));
483 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000484 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000486 GL_CALL(Disable(GR_GL_DITHER));
487 if (kDesktop_GrGLBinding == this->glBinding()) {
488 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
489 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
490 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000491 fHWAAState.fMSAAEnabled = false;
492 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000493 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000494
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000495 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000496 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000497
reed@google.comac10a2d2010-12-22 21:39:39 +0000498 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000499 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000501 // invalid
502 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000505 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000506
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000507 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000508 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000509
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000510 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000511
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000512 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000513
tomhudson@google.com93813632011-10-27 20:21:16 +0000514 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000515 fHWDrawState.setTexture(s, NULL);
516 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
517 -GR_ScalarMax,
518 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000519 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000520 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000521 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000522
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000523 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000525 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000526 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000527
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000528 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000529 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000530 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000531
532 fHWGeometryState.fIndexBuffer = NULL;
533 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000534
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000535 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000536
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000537 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000538 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000539
540 // we assume these values
541 if (this->glCaps().fUnpackRowLengthSupport) {
542 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
543 }
544 if (this->glCaps().fPackRowLengthSupport) {
545 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
546 }
547 if (this->glCaps().fUnpackFlipYSupport) {
548 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
549 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000550 if (this->glCaps().fPackFlipYSupport) {
551 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
552 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000553}
554
bsalomon@google.come269f212011-11-07 13:29:52 +0000555GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000556 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000557 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000558 return NULL;
559 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000560
bsalomon@google.com99621082011-11-15 16:47:16 +0000561 glTexDesc.fWidth = desc.fWidth;
562 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000563 glTexDesc.fConfig = desc.fConfig;
564 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
565 glTexDesc.fOwnsID = false;
566 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
567
568 GrGLTexture* texture = NULL;
569 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
570 GrGLRenderTarget::Desc glRTDesc;
571 glRTDesc.fRTFBOID = 0;
572 glRTDesc.fTexFBOID = 0;
573 glRTDesc.fMSColorRenderbufferID = 0;
574 glRTDesc.fOwnIDs = true;
575 glRTDesc.fConfig = desc.fConfig;
576 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000577 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
578 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000579 glTexDesc.fTextureID,
580 &glRTDesc)) {
581 return NULL;
582 }
583 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
584 } else {
585 texture = new GrGLTexture(this, glTexDesc);
586 }
587 if (NULL == texture) {
588 return NULL;
589 }
590
591 this->setSpareTextureUnit();
592 return texture;
593}
594
595GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
596 GrGLRenderTarget::Desc glDesc;
597 glDesc.fConfig = desc.fConfig;
598 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
599 glDesc.fMSColorRenderbufferID = 0;
600 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
601 glDesc.fSampleCnt = desc.fSampleCnt;
602 glDesc.fOwnIDs = false;
603 GrGLIRect viewport;
604 viewport.fLeft = 0;
605 viewport.fBottom = 0;
606 viewport.fWidth = desc.fWidth;
607 viewport.fHeight = desc.fHeight;
608
609 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
610 if (desc.fStencilBits) {
611 GrGLStencilBuffer::Format format;
612 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
613 format.fPacked = false;
614 format.fStencilBits = desc.fStencilBits;
615 format.fTotalBits = desc.fStencilBits;
616 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
617 0,
618 desc.fWidth,
619 desc.fHeight,
620 desc.fSampleCnt,
621 format);
622 tgt->setStencilBuffer(sb);
623 sb->unref();
624 }
625 return tgt;
626}
627
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000628////////////////////////////////////////////////////////////////////////////////
629
bsalomon@google.com6f379512011-11-16 20:36:03 +0000630void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
631 int left, int top, int width, int height,
632 GrPixelConfig config, const void* buffer,
633 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000634 if (NULL == buffer) {
635 return;
636 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000637 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
638
bsalomon@google.com6f379512011-11-16 20:36:03 +0000639 this->setSpareTextureUnit();
640 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
641 GrGLTexture::Desc desc;
642 desc.fConfig = glTex->config();
643 desc.fWidth = glTex->width();
644 desc.fHeight = glTex->height();
645 desc.fOrientation = glTex->orientation();
646 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000647
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000648 this->uploadTexData(desc, false,
649 left, top, width, height,
650 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000651}
652
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000653namespace {
654bool adjust_pixel_ops_params(int surfaceWidth,
655 int surfaceHeight,
656 size_t bpp,
657 int* left, int* top, int* width, int* height,
658 const void** data,
659 size_t* rowBytes) {
660 if (!*rowBytes) {
661 *rowBytes = *width * bpp;
662 }
663
664 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
665 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
666
667 if (!subRect.intersect(bounds)) {
668 return false;
669 }
670 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
671 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
672
673 *left = subRect.fLeft;
674 *top = subRect.fTop;
675 *width = subRect.width();
676 *height = subRect.height();
677 return true;
678}
679}
680
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000681bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
682 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000683 int left, int top, int width, int height,
684 GrPixelConfig dataConfig,
685 const void* data,
686 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000687 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000688
689 size_t bpp = GrBytesPerPixel(dataConfig);
690 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
691 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000692 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000693 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000694 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000695
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000696 // in case we need a temporary, trimmed copy of the src pixels
697 SkAutoSMalloc<128 * 128> tempStorage;
698
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000699 bool useTexStorage = isNewTexture &&
700 this->glCaps().fTexStorageSupport;
701 if (useTexStorage) {
702 if (kDesktop_GrGLBinding == this->glBinding()) {
703 // 565 is not a sized internal format on desktop GL. So on desktop
704 // with 565 we always use an unsized internal format to let the
705 // system pick the best sized format to convert the 565 data to.
706 // Since glTexStorage only allows sized internal formats we will
707 // instead fallback to glTexImage2D.
708 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
709 } else {
710 // ES doesn't allow paletted textures to be used with tex storage
711 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
712 }
713 }
714
715 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000716 GrGLenum externalFormat;
717 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000718 // glTexStorage requires sized internal formats on both desktop and ES. ES
719 // glTexImage requires an unsized format.
720 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
721 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000722 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000723 }
724
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000725 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000726 // paletted textures cannot be updated
727 return false;
728 }
729
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000730 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000731 * check whether to allocate a temporary buffer for flipping y or
732 * because our srcData has extra bytes past each row. If so, we need
733 * to trim those off here, since GL ES may not let us specify
734 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000735 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000736 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000737 bool swFlipY = false;
738 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000739 if (NULL != data) {
740 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
741 if (this->glCaps().fUnpackFlipYSupport) {
742 glFlipY = true;
743 } else {
744 swFlipY = true;
745 }
746 }
747 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
748 // can't use this for flipping, only non-neg values allowed. :(
749 if (rowBytes != trimRowBytes) {
750 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
751 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
752 restoreGLRowLength = true;
753 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000754 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000755 if (trimRowBytes != rowBytes || swFlipY) {
756 // copy data into our new storage, skipping the trailing bytes
757 size_t trimSize = height * trimRowBytes;
758 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000759 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000760 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000761 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000762 char* dst = (char*)tempStorage.reset(trimSize);
763 for (int y = 0; y < height; y++) {
764 memcpy(dst, src, trimRowBytes);
765 if (swFlipY) {
766 src -= rowBytes;
767 } else {
768 src += rowBytes;
769 }
770 dst += trimRowBytes;
771 }
772 // now point data to our copied version
773 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000774 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000775 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000776 if (glFlipY) {
777 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
778 }
779 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000780 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000781 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000782 if (isNewTexture &&
783 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000784 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000785 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000786 if (useTexStorage) {
787 // We never resize or change formats of textures. We don't use
788 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000789 GL_ALLOC_CALL(this->glInterface(),
790 TexStorage2D(GR_GL_TEXTURE_2D,
791 1, // levels
792 internalFormat,
793 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000794 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000795 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
796 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
797 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000798 GL_ALLOC_CALL(this->glInterface(),
799 CompressedTexImage2D(GR_GL_TEXTURE_2D,
800 0, // level
801 internalFormat,
802 desc.fWidth, desc.fHeight,
803 0, // border
804 imageSize,
805 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000806 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000807 GL_ALLOC_CALL(this->glInterface(),
808 TexImage2D(GR_GL_TEXTURE_2D,
809 0, // level
810 internalFormat,
811 desc.fWidth, desc.fHeight,
812 0, // border
813 externalFormat, externalType,
814 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000815 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000816 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000817 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000818 if (error != GR_GL_NO_ERROR) {
819 succeeded = false;
820 } else {
821 // if we have data and we used TexStorage to create the texture, we
822 // now upload with TexSubImage.
823 if (NULL != data && useTexStorage) {
824 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
825 0, // level
826 left, top,
827 width, height,
828 externalFormat, externalType,
829 data));
830 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000831 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000832 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000833 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000834 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000835 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000836 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
837 0, // level
838 left, top,
839 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000840 externalFormat, externalType, data));
841 }
842
843 if (restoreGLRowLength) {
844 GrAssert(this->glCaps().fUnpackRowLengthSupport);
845 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000847 if (glFlipY) {
848 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
849 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000850 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000851}
852
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000853bool GrGpuGL::createRenderTargetObjects(int width, int height,
854 GrGLuint texID,
855 GrGLRenderTarget::Desc* desc) {
856 desc->fMSColorRenderbufferID = 0;
857 desc->fRTFBOID = 0;
858 desc->fTexFBOID = 0;
859 desc->fOwnIDs = true;
860
861 GrGLenum status;
862 GrGLint err;
863
bsalomon@google.comab15d612011-08-09 12:57:56 +0000864 GrGLenum msColorFormat = 0; // suppress warning
865
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000866 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000867 if (!desc->fTexFBOID) {
868 goto FAILED;
869 }
870
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000871
872 // If we are using multisampling we will create two FBOS. We render
873 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000874 if (desc->fSampleCnt > 0) {
875 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
876 goto FAILED;
877 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000878 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
879 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880 if (!desc->fRTFBOID ||
881 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000882 !this->configToGLFormats(desc->fConfig,
883 // GLES requires sized internal formats
884 kES2_GrGLBinding == this->glBinding(),
885 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000886 goto FAILED;
887 }
888 } else {
889 desc->fRTFBOID = desc->fTexFBOID;
890 }
891
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000892 // below here we may bind the FBO
893 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000894 if (desc->fRTFBOID != desc->fTexFBOID) {
895 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000896 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897 desc->fMSColorRenderbufferID));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000898 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
899 GL_ALLOC_CALL(this->glInterface(),
900 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
901 desc->fSampleCnt,
902 msColorFormat,
903 width, height));
904 err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905 if (err != GR_GL_NO_ERROR) {
906 goto FAILED;
907 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000908 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
909 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910 GR_GL_COLOR_ATTACHMENT0,
911 GR_GL_RENDERBUFFER,
912 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000913 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000914 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
915 goto FAILED;
916 }
917 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000918 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000920 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
921 GR_GL_COLOR_ATTACHMENT0,
922 GR_GL_TEXTURE_2D,
923 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000924 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000925 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
926 goto FAILED;
927 }
928
929 return true;
930
931FAILED:
932 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000933 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000934 }
935 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000937 }
938 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000939 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940 }
941 return false;
942}
943
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000944// good to set a break-point here to know when createTexture fails
945static GrTexture* return_null_texture() {
946// GrAssert(!"null texture");
947 return NULL;
948}
949
950#if GR_DEBUG
951static size_t as_size_t(int x) {
952 return x;
953}
954#endif
955
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000956GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000957 const void* srcData,
958 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000959
960#if GR_COLLECT_STATS
961 ++fStats.fTextureCreateCnt;
962#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000963
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000964 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000965 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000966
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000967 // Attempt to catch un- or wrongly initialized sample counts;
968 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
969
bsalomon@google.com99621082011-11-15 16:47:16 +0000970 glTexDesc.fWidth = desc.fWidth;
971 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000972 glTexDesc.fConfig = desc.fConfig;
973 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000974
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000975 glRTDesc.fMSColorRenderbufferID = 0;
976 glRTDesc.fRTFBOID = 0;
977 glRTDesc.fTexFBOID = 0;
978 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000979 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000980
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000981 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000982
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000983 const Caps& caps = this->getCaps();
984
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000985 // We keep GrRenderTargets in GL's normal orientation so that they
986 // can be drawn to by the outside world without the client having
987 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000988 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000989 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000990
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000991 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000992 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000993 desc.fSampleCnt) {
994 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +0000995 }
996
reed@google.comac10a2d2010-12-22 21:39:39 +0000997 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +0000998 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
999 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001000 return return_null_texture();
1001 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001002 }
1003
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001004 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001005 if (renderTarget && this->glCaps().fTextureUsageSupport) {
1006 // provides a hint about how this texture will be used
1007 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1008 GR_GL_TEXTURE_USAGE,
1009 GR_GL_FRAMEBUFFER_ATTACHMENT));
1010 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001011 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001012 return return_null_texture();
1013 }
1014
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001015 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001017
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001018 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1019 // drivers have a bug where an FBO won't be complete if it includes a
1020 // texture that is not mipmap complete (considering the filter in use).
1021 GrGLTexture::TexParams initialTexParams;
1022 // we only set a subset here so invalidate first
1023 initialTexParams.invalidate();
1024 initialTexParams.fFilter = GR_GL_NEAREST;
1025 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1026 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1028 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001029 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001030 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1031 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001032 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001033 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1034 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001035 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001036 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1037 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001038 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001039 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1040 glTexDesc.fWidth, glTexDesc.fHeight,
1041 desc.fConfig, srcData, rowBytes)) {
1042 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1043 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001044 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001045
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001046 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001047 if (renderTarget) {
1048#if GR_COLLECT_STATS
1049 ++fStats.fRenderTargetCreateCnt;
1050#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001051 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1052 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001053 glTexDesc.fTextureID,
1054 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001055 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 return return_null_texture();
1057 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001058 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001059 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001060 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001062 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001063#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001064 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1065 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001066#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001067 return tex;
1068}
1069
1070namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001071void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1072 GrGLuint rb,
1073 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 // we shouldn't ever know one size and not the other
1075 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1076 (kUnknownBitCount == format->fTotalBits));
1077 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001079 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1080 (GrGLint*)&format->fStencilBits);
1081 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001082 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001083 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1084 (GrGLint*)&format->fTotalBits);
1085 format->fTotalBits += format->fStencilBits;
1086 } else {
1087 format->fTotalBits = format->fStencilBits;
1088 }
1089 }
1090}
1091}
1092
1093bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1094 int width, int height) {
1095
1096 // All internally created RTs are also textures. We don't create
1097 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1098 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001099 GrAssert(width >= rt->width());
1100 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101
1102 int samples = rt->numSamples();
1103 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001104 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 if (!sbID) {
1106 return false;
1107 }
1108
1109 GrGLStencilBuffer* sb = NULL;
1110
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001111 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 // we start with the last stencil format that succeeded in hopes
1115 // that we won't go through this loop more than once after the
1116 // first (painful) stencil creation.
1117 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001118 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001119 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001120 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 // version on a GL that doesn't have an MSAA extension.
1122 if (samples > 1) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001123 GL_ALLOC_CALL(this->glInterface(),
1124 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1125 samples,
1126 sFmt.fInternalFormat,
1127 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001129 GL_ALLOC_CALL(this->glInterface(),
1130 RenderbufferStorage(GR_GL_RENDERBUFFER,
1131 sFmt.fInternalFormat,
1132 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 }
1134
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001135 GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 if (err == GR_GL_NO_ERROR) {
1137 // After sized formats we attempt an unsized format and take whatever
1138 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001139 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001140 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001141 sb = new GrGLStencilBuffer(this, sbID, width, height,
1142 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1144 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001145 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 return true;
1148 }
1149 sb->abandon(); // otherwise we lose sbID
1150 sb->unref();
1151 }
1152 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001153 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001154 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155}
1156
1157bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1158 GrRenderTarget* rt) {
1159 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1160
1161 GrGLuint fbo = glrt->renderFBOID();
1162
1163 if (NULL == sb) {
1164 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001165 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 GR_GL_STENCIL_ATTACHMENT,
1167 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001168 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 GR_GL_DEPTH_ATTACHMENT,
1170 GR_GL_RENDERBUFFER, 0));
1171#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001172 GrGLenum status;
1173 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1175#endif
1176 }
1177 return true;
1178 } else {
1179 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1180 GrGLuint rb = glsb->renderbufferID();
1181
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001182 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001183 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1184 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 GR_GL_STENCIL_ATTACHMENT,
1186 GR_GL_RENDERBUFFER, rb));
1187 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001188 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 GR_GL_DEPTH_ATTACHMENT,
1190 GR_GL_RENDERBUFFER, rb));
1191 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 GR_GL_DEPTH_ATTACHMENT,
1194 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001195 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001197 GrGLenum status;
1198 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001199 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001200 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 GR_GL_STENCIL_ATTACHMENT,
1202 GR_GL_RENDERBUFFER, 0));
1203 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 GR_GL_DEPTH_ATTACHMENT,
1206 GR_GL_RENDERBUFFER, 0));
1207 }
1208 return false;
1209 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 return true;
1211 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001212 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001213}
1214
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001215////////////////////////////////////////////////////////////////////////////////
1216
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001217GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001218 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001220 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001222 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001223 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001224 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001225 GL_ALLOC_CALL(this->glInterface(),
1226 BufferData(GR_GL_ARRAY_BUFFER,
1227 size,
1228 NULL, // data ptr
1229 dynamic ? GR_GL_DYNAMIC_DRAW :
1230 GR_GL_STATIC_DRAW));
1231 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 // deleting bound buffer does implicit bind to 0
1234 fHWGeometryState.fVertexBuffer = NULL;
1235 return NULL;
1236 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001237 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 size, dynamic);
1239 fHWGeometryState.fVertexBuffer = vertexBuffer;
1240 return vertexBuffer;
1241 }
1242 return NULL;
1243}
1244
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001245GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001246 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001247 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001248 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001249 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001250 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001251 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001252 GL_ALLOC_CALL(this->glInterface(),
1253 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1254 size,
1255 NULL, // data ptr
1256 dynamic ? GR_GL_DYNAMIC_DRAW :
1257 GR_GL_STATIC_DRAW));
1258 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001259 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 // deleting bound buffer does implicit bind to 0
1261 fHWGeometryState.fIndexBuffer = NULL;
1262 return NULL;
1263 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001264 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 size, dynamic);
1266 fHWGeometryState.fIndexBuffer = indexBuffer;
1267 return indexBuffer;
1268 }
1269 return NULL;
1270}
1271
reed@google.comac10a2d2010-12-22 21:39:39 +00001272void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001273 const GrDrawState& drawState = this->getDrawState();
1274 const GrGLRenderTarget* rt =
1275 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1276
1277 GrAssert(NULL != rt);
1278 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001279
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001280 GrGLIRect scissor;
1281 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001282 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001283 rect->width(), rect->height());
1284 if (scissor.contains(vp)) {
1285 rect = NULL;
1286 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 }
1288
1289 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001291 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001292 fHWBounds.fScissorRect = scissor;
1293 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001295 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 fHWBounds.fScissorEnabled = true;
1297 }
1298 } else {
1299 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001300 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 fHWBounds.fScissorEnabled = false;
1302 }
1303 }
1304}
1305
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001306void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001307 const GrDrawState& drawState = this->getDrawState();
1308 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001309 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001310 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001311
bsalomon@google.com74b98712011-11-11 19:46:16 +00001312 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001313 if (NULL != rect) {
1314 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001315 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001316 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001317 if (clippedRect.intersect(rtRect)) {
1318 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001319 } else {
1320 return;
1321 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001323 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001324 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001325
1326 GrGLfloat r, g, b, a;
1327 static const GrGLfloat scale255 = 1.f / 255.f;
1328 a = GrColorUnpackA(color) * scale255;
1329 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001330 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001331 scaleRGB *= a;
1332 }
1333 r = GrColorUnpackR(color) * scaleRGB;
1334 g = GrColorUnpackG(color) * scaleRGB;
1335 b = GrColorUnpackB(color) * scaleRGB;
1336
1337 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001338 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001339 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001340 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001341}
1342
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001343void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001344 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001345 return;
1346 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001347
1348 this->flushRenderTarget(&GrIRect::EmptyIRect());
1349
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001351 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001352 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001354 GL_CALL(StencilMask(0xffffffff));
1355 GL_CALL(ClearStencil(0));
1356 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001357 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001358}
1359
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001360void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001361 const GrDrawState& drawState = this->getDrawState();
1362 const GrRenderTarget* rt = drawState.getRenderTarget();
1363 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001364
1365 // this should only be called internally when we know we have a
1366 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001367 GrAssert(NULL != rt->getStencilBuffer());
1368 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001369#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001371 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001372#else
1373 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001374 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001375 // turned into draws. Our contract on GrDrawTarget says that
1376 // changing the clip between stencil passes may or may not
1377 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001378 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001379#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001380 GrGLint value;
1381 if (insideClip) {
1382 value = (1 << (stencilBitCount - 1));
1383 } else {
1384 value = 0;
1385 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001386 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001387 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001388 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001389 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001390 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001391 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001392}
1393
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001394void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001395 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001396}
1397
bsalomon@google.comc4364992011-11-07 15:54:49 +00001398bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1399 int left, int top,
1400 int width, int height,
1401 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001402 size_t rowBytes) const {
1403 // if GL can do the flip then we'll never pay for it.
1404 if (this->glCaps().fPackFlipYSupport) {
1405 return false;
1406 }
1407
1408 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001409 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001410 if (this->glCaps().fPackRowLengthSupport) {
1411 return true;
1412 }
1413 // If we have to do memcpys to handle rowBytes then y-flip is free
1414 // Note the rowBytes might be tight to the passed in data, but if data
1415 // gets clipped in x to the target the rowBytes will no longer be tight.
1416 if (left >= 0 && (left + width) < renderTarget->width()) {
1417 return 0 == rowBytes ||
1418 GrBytesPerPixel(config) * width == rowBytes;
1419 } else {
1420 return false;
1421 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001422}
1423
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001424bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001425 int left, int top,
1426 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001427 GrPixelConfig config,
1428 void* buffer,
1429 size_t rowBytes,
1430 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001431 GrGLenum format;
1432 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001433 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001434 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001435 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001436 size_t bpp = GrBytesPerPixel(config);
1437 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1438 &left, &top, &width, &height,
1439 const_cast<const void**>(&buffer),
1440 &rowBytes)) {
1441 return false;
1442 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001443
bsalomon@google.comc6980972011-11-02 19:57:21 +00001444 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001445 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001446 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001447 switch (tgt->getResolveType()) {
1448 case GrGLRenderTarget::kCantResolve_ResolveType:
1449 return false;
1450 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001451 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001452 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001453 break;
1454 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001455 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001456 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001457 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1458 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001459 break;
1460 default:
1461 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001462 }
1463
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001464 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001465
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001466 // the read rect is viewport-relative
1467 GrGLIRect readRect;
1468 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001469
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001470 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001471 if (0 == rowBytes) {
1472 rowBytes = tightRowBytes;
1473 }
1474 size_t readDstRowBytes = tightRowBytes;
1475 void* readDst = buffer;
1476
1477 // determine if GL can read using the passed rowBytes or if we need
1478 // a scratch buffer.
1479 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1480 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001481 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001482 GrAssert(!(rowBytes % sizeof(GrColor)));
1483 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1484 readDstRowBytes = rowBytes;
1485 } else {
1486 scratch.reset(tightRowBytes * height);
1487 readDst = scratch.get();
1488 }
1489 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001490 if (!invertY && this->glCaps().fPackFlipYSupport) {
1491 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1492 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001493 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1494 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001495 format, type, readDst));
1496 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001497 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1499 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001500 if (!invertY && this->glCaps().fPackFlipYSupport) {
1501 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1502 invertY = true;
1503 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001504
1505 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001506 // API presents top-to-bottom. We must preserve the padding contents. Note
1507 // that the above readPixels did not overwrite the padding.
1508 if (readDst == buffer) {
1509 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001510 if (!invertY) {
1511 scratch.reset(tightRowBytes);
1512 void* tmpRow = scratch.get();
1513 // flip y in-place by rows
1514 const int halfY = height >> 1;
1515 char* top = reinterpret_cast<char*>(buffer);
1516 char* bottom = top + (height - 1) * rowBytes;
1517 for (int y = 0; y < halfY; y++) {
1518 memcpy(tmpRow, top, tightRowBytes);
1519 memcpy(top, bottom, tightRowBytes);
1520 memcpy(bottom, tmpRow, tightRowBytes);
1521 top += rowBytes;
1522 bottom -= rowBytes;
1523 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001524 }
1525 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001526 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001527 // copy from readDst to buffer while flipping y
1528 const int halfY = height >> 1;
1529 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001530 char* dst = reinterpret_cast<char*>(buffer);
1531 if (!invertY) {
1532 dst += (height-1) * rowBytes;
1533 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001534 for (int y = 0; y < height; y++) {
1535 memcpy(dst, src, tightRowBytes);
1536 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001537 if (invertY) {
1538 dst += rowBytes;
1539 } else {
1540 dst -= rowBytes;
1541 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001542 }
1543 }
1544 return true;
1545}
1546
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001547void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001548
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001549 GrGLRenderTarget* rt =
1550 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1551 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001552
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001553 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001554 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001555 #if GR_COLLECT_STATS
1556 ++fStats.fRenderTargetChngCnt;
1557 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001558 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001559 GrGLenum status;
1560 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001561 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001562 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001563 }
1564 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001565 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001566 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001567 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001568 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001569 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001570 fHWBounds.fViewportRect = vp;
1571 }
1572 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001573 if (NULL == bound || !bound->isEmpty()) {
1574 rt->flagAsNeedingResolve(bound);
1575 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001576}
1577
twiz@google.com0f31ca72011-03-18 17:38:11 +00001578GrGLenum gPrimitiveType2GLMode[] = {
1579 GR_GL_TRIANGLES,
1580 GR_GL_TRIANGLE_STRIP,
1581 GR_GL_TRIANGLE_FAN,
1582 GR_GL_POINTS,
1583 GR_GL_LINES,
1584 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001585};
1586
bsalomon@google.comd302f142011-03-03 13:54:13 +00001587#define SWAP_PER_DRAW 0
1588
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001589#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001590 #if GR_MAC_BUILD
1591 #include <AGL/agl.h>
1592 #elif GR_WIN32_BUILD
1593 void SwapBuf() {
1594 DWORD procID = GetCurrentProcessId();
1595 HWND hwnd = GetTopWindow(GetDesktopWindow());
1596 while(hwnd) {
1597 DWORD wndProcID = 0;
1598 GetWindowThreadProcessId(hwnd, &wndProcID);
1599 if(wndProcID == procID) {
1600 SwapBuffers(GetDC(hwnd));
1601 }
1602 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1603 }
1604 }
1605 #endif
1606#endif
1607
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001608void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1609 uint32_t startVertex,
1610 uint32_t startIndex,
1611 uint32_t vertexCount,
1612 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1614
twiz@google.com0f31ca72011-03-18 17:38:11 +00001615 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001616
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001617 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1618 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1619
1620 // our setupGeometry better have adjusted this to zero since
1621 // DrawElements always draws from the begining of the arrays for idx 0.
1622 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001623
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001624 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1625 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001626#if SWAP_PER_DRAW
1627 glFlush();
1628 #if GR_MAC_BUILD
1629 aglSwapBuffers(aglGetCurrentContext());
1630 int set_a_break_pt_here = 9;
1631 aglSwapBuffers(aglGetCurrentContext());
1632 #elif GR_WIN32_BUILD
1633 SwapBuf();
1634 int set_a_break_pt_here = 9;
1635 SwapBuf();
1636 #endif
1637#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001638}
1639
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001640void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1641 uint32_t startVertex,
1642 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001643 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1644
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001645 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1646
1647 // our setupGeometry better have adjusted this to zero.
1648 // DrawElements doesn't take an offset so we always adjus the startVertex.
1649 GrAssert(0 == startVertex);
1650
1651 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1652 // account for startVertex in the DrawElements case. So we always
1653 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001654 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001655#if SWAP_PER_DRAW
1656 glFlush();
1657 #if GR_MAC_BUILD
1658 aglSwapBuffers(aglGetCurrentContext());
1659 int set_a_break_pt_here = 9;
1660 aglSwapBuffers(aglGetCurrentContext());
1661 #elif GR_WIN32_BUILD
1662 SwapBuf();
1663 int set_a_break_pt_here = 9;
1664 SwapBuf();
1665 #endif
1666#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001667}
1668
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001669void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1670
1671 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001672
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001673 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001674 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001675 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001676 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1677 rt->renderFBOID()));
1678 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1679 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001680 #if GR_COLLECT_STATS
1681 ++fStats.fRenderTargetChngCnt;
1682 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001683 // make sure we go through flushRenderTarget() since we've modified
1684 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001685 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001686 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001687 const GrIRect dirtyRect = rt->getResolveRect();
1688 GrGLIRect r;
1689 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1690 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001691
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001692 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001693 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001694 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1695 GL_CALL(Scissor(r.fLeft, r.fBottom,
1696 r.fWidth, r.fHeight));
1697 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001698 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001699 fHWBounds.fScissorEnabled = true;
1700 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001701 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001702 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001703 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1704 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001705 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001706 int right = r.fLeft + r.fWidth;
1707 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001708 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1709 r.fLeft, r.fBottom, right, top,
1710 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001711 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001712 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001713 }
1714}
1715
twiz@google.com0f31ca72011-03-18 17:38:11 +00001716static const GrGLenum grToGLStencilFunc[] = {
1717 GR_GL_ALWAYS, // kAlways_StencilFunc
1718 GR_GL_NEVER, // kNever_StencilFunc
1719 GR_GL_GREATER, // kGreater_StencilFunc
1720 GR_GL_GEQUAL, // kGEqual_StencilFunc
1721 GR_GL_LESS, // kLess_StencilFunc
1722 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1723 GR_GL_EQUAL, // kEqual_StencilFunc,
1724 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001725};
1726GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1727GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1728GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1729GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1730GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1731GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1732GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1733GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1734GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1735
twiz@google.com0f31ca72011-03-18 17:38:11 +00001736static const GrGLenum grToGLStencilOp[] = {
1737 GR_GL_KEEP, // kKeep_StencilOp
1738 GR_GL_REPLACE, // kReplace_StencilOp
1739 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1740 GR_GL_INCR, // kIncClamp_StencilOp
1741 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1742 GR_GL_DECR, // kDecClamp_StencilOp
1743 GR_GL_ZERO, // kZero_StencilOp
1744 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001745};
1746GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1747GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1748GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1749GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1750GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1751GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1752GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1753GR_STATIC_ASSERT(6 == kZero_StencilOp);
1754GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1755
reed@google.comac10a2d2010-12-22 21:39:39 +00001756void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001757 const GrDrawState& drawState = this->getDrawState();
1758
1759 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001760
1761 // use stencil for clipping if clipping is enabled and the clip
1762 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001763 bool stencilClip = fClipInStencil && drawState.isClipState();
1764 bool drawClipToStencil =
1765 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001766 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1767 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001768 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1769 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001770
1771 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001772
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001773 // we can't simultaneously perform stencil-clipping and
1774 // modify the stencil clip
1775 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001776
bsalomon@google.comd302f142011-03-03 13:54:13 +00001777 if (settings->isDisabled()) {
1778 if (stencilClip) {
1779 settings = &gClipStencilSettings;
1780 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001781 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001782
1783 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001784 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001785 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001786 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001787 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001788 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001789 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1790 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1791 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1792 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1793 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1794 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1795 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1796 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001797 }
1798 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001799 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001800 GrStencilBuffer* stencilBuffer =
1801 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001802 if (NULL != stencilBuffer) {
1803 stencilBits = stencilBuffer->bits();
1804 }
1805 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001806 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001807
1808 GrGLuint clipStencilMask = 0;
1809 GrGLuint userStencilMask = ~0;
1810 if (stencilBits > 0) {
1811 clipStencilMask = 1 << (stencilBits - 1);
1812 userStencilMask = clipStencilMask - 1;
1813 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001814
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001815 unsigned int frontRef = settings->frontFuncRef();
1816 unsigned int frontMask = settings->frontFuncMask();
1817 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001818 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001819
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001820 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001821 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1822 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001823 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001824 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001825 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001826
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001827 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828 stencilClip,
1829 clipStencilMask,
1830 userStencilMask,
1831 &frontRef,
1832 &frontMask);
1833 frontWriteMask &= userStencilMask;
1834 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001835 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001836 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001837 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001838 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001839 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001840 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001841 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001842 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001843 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001844 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001845
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001846 unsigned int backRef = settings->backFuncRef();
1847 unsigned int backMask = settings->backFuncMask();
1848 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001849
1850
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001851 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001852 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1853 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001854 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001855 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001856 stencilClip, settings->backFunc())];
1857 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858 stencilClip,
1859 clipStencilMask,
1860 userStencilMask,
1861 &backRef,
1862 &backMask);
1863 backWriteMask &= userStencilMask;
1864 }
1865
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001866 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1867 frontRef, frontMask));
1868 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1869 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1870 backRef, backMask));
1871 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1872 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001873 grToGLStencilOp[settings->frontFailOp()],
1874 grToGLStencilOp[settings->frontPassOp()],
1875 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001876
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001877 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001878 grToGLStencilOp[settings->backFailOp()],
1879 grToGLStencilOp[settings->backPassOp()],
1880 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001881 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001882 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1883 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001884 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1885 grToGLStencilOp[settings->frontPassOp()],
1886 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887 }
1888 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001889 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001890 fHWStencilClip = stencilClip;
1891 }
1892}
1893
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001894void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001895 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001896 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001897 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1898 // smooth lines.
1899
1900 // we prefer smooth lines over multisampled lines
1901 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001902 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001903 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001904 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001905 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001906 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001907 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001908 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001909 fHWAAState.fSmoothLineEnabled = false;
1910 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001911 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001912 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001913 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001914 fHWAAState.fMSAAEnabled = false;
1915 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001916 } else if (rt->isMultisampled() &&
1917 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001918 fHWAAState.fMSAAEnabled) {
1919 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001920 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001921 fHWAAState.fMSAAEnabled = false;
1922 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001923 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001924 fHWAAState.fMSAAEnabled = true;
1925 }
1926 }
1927 }
1928}
1929
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001930void GrGpuGL::flushBlend(GrPrimitiveType type,
1931 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001932 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001933 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001934 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001935 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001936 fHWBlendDisabled = false;
1937 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001938 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1939 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001940 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1941 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001942 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001943 }
1944 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001945 // any optimization to disable blending should
1946 // have already been applied and tweaked the coeffs
1947 // to (1, 0).
1948 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1949 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001950 if (fHWBlendDisabled != blendOff) {
1951 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001952 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001953 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001954 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001955 }
1956 fHWBlendDisabled = blendOff;
1957 }
1958 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001959 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
1960 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001961 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1962 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001963 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001964 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001965 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001966 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1967 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001968 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001969
1970 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001971 GrColorUnpackR(blendConst) / 255.f,
1972 GrColorUnpackG(blendConst) / 255.f,
1973 GrColorUnpackB(blendConst) / 255.f,
1974 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001975 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001976 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001977 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001978 }
1979 }
1980 }
1981}
1982
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001983namespace {
1984
1985unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001986 switch (filter) {
1987 case GrSamplerState::kBilinear_Filter:
1988 case GrSamplerState::k4x4Downsample_Filter:
1989 return GR_GL_LINEAR;
1990 case GrSamplerState::kNearest_Filter:
1991 case GrSamplerState::kConvolution_Filter:
1992 return GR_GL_NEAREST;
1993 default:
1994 GrAssert(!"Unknown filter type");
1995 return GR_GL_LINEAR;
1996 }
1997}
1998
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001999const GrGLenum* get_swizzle(GrPixelConfig config,
2000 const GrSamplerState& sampler) {
2001 if (GrPixelConfigIsAlphaOnly(config)) {
2002 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2003 GR_GL_ALPHA, GR_GL_ALPHA };
2004 return gAlphaSmear;
2005 } else if (sampler.swapsRAndB()) {
2006 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2007 GR_GL_RED, GR_GL_ALPHA };
2008 return gRedBlueSwap;
2009 } else {
2010 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2011 GR_GL_BLUE, GR_GL_ALPHA };
2012 return gStraight;
2013 }
2014}
2015
2016void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2017 // should add texparameteri to interface to make 1 instead of 4 calls here
2018 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2019 GR_GL_TEXTURE_SWIZZLE_R,
2020 swizzle[0]));
2021 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2022 GR_GL_TEXTURE_SWIZZLE_G,
2023 swizzle[1]));
2024 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2025 GR_GL_TEXTURE_SWIZZLE_B,
2026 swizzle[2]));
2027 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2028 GR_GL_TEXTURE_SWIZZLE_A,
2029 swizzle[3]));
2030}
2031}
2032
bsalomon@google.comffca4002011-02-22 20:34:01 +00002033bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002034
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002035 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002036 // GrGpu::setupClipAndFlushState should have already checked this
2037 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002038 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002039
tomhudson@google.com93813632011-10-27 20:21:16 +00002040 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002041 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002042 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002043 GrGLTexture* nextTexture =
2044 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002045
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002046 // true for now, but maybe not with GrEffect.
2047 GrAssert(NULL != nextTexture);
2048 // if we created a rt/tex and rendered to it without using a
2049 // texture and now we're texuring from the rt it will still be
2050 // the last bound texture, but it needs resolving. So keep this
2051 // out of the "last != next" check.
2052 GrGLRenderTarget* texRT =
2053 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2054 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002055 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002056 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002057
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002058 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002059 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002060 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002061 #if GR_COLLECT_STATS
2062 ++fStats.fTextureChngCnt;
2063 #endif
2064 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002065 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002066 // The texture matrix has to compensate for texture width/height
2067 // and NPOT-embedded-in-POT
2068 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002069 }
2070
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002071 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002072 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002073 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002074 nextTexture->getCachedTexParams(&timestamp);
2075 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002076 GrGLTexture::TexParams newTexParams;
2077
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002078 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002079
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002080 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002081 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2082 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002083 memcpy(newTexParams.fSwizzleRGBA,
2084 get_swizzle(nextTexture->config(), sampler),
2085 sizeof(newTexParams.fSwizzleRGBA));
2086 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002087 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002088 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002089 GR_GL_TEXTURE_MAG_FILTER,
2090 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002091 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002092 GR_GL_TEXTURE_MIN_FILTER,
2093 newTexParams.fFilter));
2094 }
2095 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2096 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002097 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002098 GR_GL_TEXTURE_WRAP_S,
2099 newTexParams.fWrapS));
2100 }
2101 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2102 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002103 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002104 GR_GL_TEXTURE_WRAP_T,
2105 newTexParams.fWrapT));
2106 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002107 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002108 (setAll ||
2109 memcmp(newTexParams.fSwizzleRGBA,
2110 oldTexParams.fSwizzleRGBA,
2111 sizeof(newTexParams.fSwizzleRGBA)))) {
2112 setTextureUnit(s);
2113 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2114 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002115 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002116 nextTexture->setCachedTexParams(newTexParams,
2117 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002118 }
2119 }
2120
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002121 GrIRect* rect = NULL;
2122 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002123 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002124 fClip.hasConservativeBounds()) {
2125 fClip.getConservativeBounds().roundOut(&clipBounds);
2126 rect = &clipBounds;
2127 }
2128 this->flushRenderTarget(rect);
2129 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002130
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002131 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2132 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002133 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002134 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002135 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002136 }
2137 }
2138
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002139 if (drawState->isColorWriteDisabled() !=
2140 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002141 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002142 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002143 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002144 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002145 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002146 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002147 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002148 }
2149
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002150 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2151 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002152 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 GL_CALL(Enable(GR_GL_CULL_FACE));
2154 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002155 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002156 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002157 GL_CALL(Enable(GR_GL_CULL_FACE));
2158 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002159 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002160 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002161 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002162 break;
2163 default:
2164 GrCrash("Unknown draw face.");
2165 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002166 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002167 }
2168
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002169#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002170 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002171 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002172 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002173 NULL == drawState->getRenderTarget() ||
2174 NULL == drawState->getTexture(s) ||
2175 drawState->getTexture(s)->asRenderTarget() !=
2176 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002177 }
2178#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002179
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002180 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002181
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002182 // This copy must happen after flushStencil() is called. flushStencil()
2183 // relies on detecting when the kModifyStencilClip_StateBit state has
2184 // changed since the last draw.
2185 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002186 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002187}
2188
2189void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002190 if (fHWGeometryState.fVertexBuffer != buffer) {
2191 fHWGeometryState.fArrayPtrsDirty = true;
2192 fHWGeometryState.fVertexBuffer = buffer;
2193 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002194}
2195
2196void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002197 if (fHWGeometryState.fVertexBuffer == buffer) {
2198 // deleting bound buffer does implied bind to 0
2199 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002200 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002201 }
2202}
2203
2204void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002205 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002206}
2207
2208void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002209 if (fHWGeometryState.fIndexBuffer == buffer) {
2210 // deleting bound buffer does implied bind to 0
2211 fHWGeometryState.fIndexBuffer = NULL;
2212 }
2213}
2214
reed@google.comac10a2d2010-12-22 21:39:39 +00002215void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2216 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002217 GrDrawState* drawState = this->drawState();
2218 if (drawState->getRenderTarget() == renderTarget) {
2219 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002220 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002221 if (fHWDrawState.getRenderTarget() == renderTarget) {
2222 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002223 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002224}
2225
2226void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002227 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002228 GrDrawState* drawState = this->drawState();
2229 if (drawState->getTexture(s) == texture) {
2230 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002231 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002232 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002233 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002234 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002235 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002236 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002237}
2238
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002239bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2240 bool getSizedInternalFormat,
2241 GrGLenum* internalFormat,
2242 GrGLenum* externalFormat,
2243 GrGLenum* externalType) {
2244 GrGLenum dontCare;
2245 if (NULL == internalFormat) {
2246 internalFormat = &dontCare;
2247 }
2248 if (NULL == externalFormat) {
2249 externalFormat = &dontCare;
2250 }
2251 if (NULL == externalType) {
2252 externalType = &dontCare;
2253 }
2254
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002256 case kRGBA_8888_PM_GrPixelConfig:
2257 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002258 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002259 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002260 if (getSizedInternalFormat) {
2261 *internalFormat = GR_GL_RGBA8;
2262 } else {
2263 *internalFormat = GR_GL_RGBA;
2264 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002265 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002266 break;
2267 case kBGRA_8888_PM_GrPixelConfig:
2268 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002269 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002270 return false;
2271 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002272 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002273 if (getSizedInternalFormat) {
2274 *internalFormat = GR_GL_BGRA8;
2275 } else {
2276 *internalFormat = GR_GL_BGRA;
2277 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002278 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002279 if (getSizedInternalFormat) {
2280 *internalFormat = GR_GL_RGBA8;
2281 } else {
2282 *internalFormat = GR_GL_RGBA;
2283 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002284 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002285 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002286 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002287 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002288 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002289 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002290 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002291 if (getSizedInternalFormat) {
2292 if (this->glBinding() == kDesktop_GrGLBinding) {
2293 return false;
2294 } else {
2295 *internalFormat = GR_GL_RGB565;
2296 }
2297 } else {
2298 *internalFormat = GR_GL_RGB;
2299 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002300 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002301 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002302 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002303 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002304 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002305 if (getSizedInternalFormat) {
2306 *internalFormat = GR_GL_RGBA4;
2307 } else {
2308 *internalFormat = GR_GL_RGBA;
2309 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002310 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002311 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002312 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002313 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002314 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002315 // glCompressedTexImage doesn't take external params
2316 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002317 // no sized/unsized internal format distinction here
2318 *internalFormat = GR_GL_PALETTE8_RGBA8;
2319 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002320 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002321 } else {
2322 return false;
2323 }
2324 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002325 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002326 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002327 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002328 if (getSizedInternalFormat) {
2329 *internalFormat = GR_GL_ALPHA8;
2330 } else {
2331 *internalFormat = GR_GL_ALPHA;
2332 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002333 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002334 break;
2335 default:
2336 return false;
2337 }
2338 return true;
2339}
2340
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002341void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002342 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002343 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002344 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002345 fActiveTextureUnitIdx = unit;
2346 }
2347}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002348
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002349void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002350 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002351 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002352 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2353 }
2354}
2355
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002356void GrGpuGL::resetDirtyFlags() {
2357 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2358}
2359
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002360void GrGpuGL::setBuffers(bool indexed,
2361 int* extraVertexOffset,
2362 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002363
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002364 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002365
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002366 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2367
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002368 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002369 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002370 case kBuffer_GeometrySrcType:
2371 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002372 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002373 break;
2374 case kArray_GeometrySrcType:
2375 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002376 this->finalizeReservedVertices();
2377 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2378 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002379 break;
2380 default:
2381 vbuf = NULL; // suppress warning
2382 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002383 }
2384
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002385 GrAssert(NULL != vbuf);
2386 GrAssert(!vbuf->isLocked());
2387 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002388 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002389 fHWGeometryState.fArrayPtrsDirty = true;
2390 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002391 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002392
2393 if (indexed) {
2394 GrAssert(NULL != extraIndexOffset);
2395
2396 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002397 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002398 case kBuffer_GeometrySrcType:
2399 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002400 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002401 break;
2402 case kArray_GeometrySrcType:
2403 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002404 this->finalizeReservedIndices();
2405 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2406 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002407 break;
2408 default:
2409 ibuf = NULL; // suppress warning
2410 GrCrash("Unknown geometry src type!");
2411 }
2412
2413 GrAssert(NULL != ibuf);
2414 GrAssert(!ibuf->isLocked());
2415 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002416 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002417 fHWGeometryState.fIndexBuffer = ibuf;
2418 }
2419 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002420}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002421
2422int GrGpuGL::getMaxEdges() const {
2423 // FIXME: This is a pessimistic estimate based on how many other things
2424 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002425 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2426 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002427}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002428
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002429void GrGpuGL::GLCaps::print() const {
2430 for (int i = 0; i < fStencilFormats.count(); ++i) {
2431 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2432 i,
2433 fStencilFormats[i].fStencilBits,
2434 fStencilFormats[i].fTotalBits);
2435 }
2436
2437 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2438 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2439 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2440 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2441 static const char* gMSFBOExtStr[] = {
2442 "None",
2443 "ARB",
2444 "EXT",
2445 "Apple",
2446 };
2447 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002448 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2449 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002450 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002451 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002452 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002453 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002454 (fTextureSwizzleSupport ? "YES": "NO"));
2455 GrPrintf("Unpack Row length support: %s\n",
2456 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002457 GrPrintf("Unpack Flip Y support: %s\n",
2458 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002459 GrPrintf("Pack Row length support: %s\n",
2460 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002461 GrPrintf("Pack Flip Y support: %s\n",
2462 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002463}