blob: 35e918b78c3ce6235d8f5eb6febb5bf0e5ac53aa [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)
19
bsalomon@google.com316f99232011-01-13 21:28:12 +000020// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021// mucking with the state of any of the stages.
bsalomon@google.com316f99232011-01-13 21:28:12 +000022static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
twiz@google.com0f31ca72011-03-18 17:38:11 +000026static const GrGLenum gXfermodeCoeff2Blend[] = {
27 GR_GL_ZERO,
28 GR_GL_ONE,
29 GR_GL_SRC_COLOR,
30 GR_GL_ONE_MINUS_SRC_COLOR,
31 GR_GL_DST_COLOR,
32 GR_GL_ONE_MINUS_DST_COLOR,
33 GR_GL_SRC_ALPHA,
34 GR_GL_ONE_MINUS_SRC_ALPHA,
35 GR_GL_DST_ALPHA,
36 GR_GL_ONE_MINUS_DST_ALPHA,
37 GR_GL_CONSTANT_COLOR,
38 GR_GL_ONE_MINUS_CONSTANT_COLOR,
39 GR_GL_CONSTANT_ALPHA,
40 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000041
42 // extended blend coeffs
43 GR_GL_SRC1_COLOR,
44 GR_GL_ONE_MINUS_SRC1_COLOR,
45 GR_GL_SRC1_ALPHA,
46 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000047};
48
bsalomon@google.com271cffc2011-05-20 14:13:56 +000049bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000050 static const bool gCoeffReferencesBlendConst[] = {
51 false,
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 true,
62 true,
63 true,
64 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000065
66 // extended blend coeffs
67 false,
68 false,
69 false,
70 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000071 };
72 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000073 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
74
75 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
76 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
77 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
78 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
79 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
80 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
81 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
82 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
83 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
84 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
85 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
86 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
87 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
88 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
89
90 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
91 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
92 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
93 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
94
95 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
96 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000097}
98
reed@google.comac10a2d2010-12-22 21:39:39 +000099///////////////////////////////////////////////////////////////////////////////
100
bsalomon@google.comd302f142011-03-03 13:54:13 +0000101void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
102 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000103 GrMatrix* matrix) {
104 GrAssert(NULL != texture);
105 GrAssert(NULL != matrix);
106 if (GR_Scalar1 != texture->contentScaleX() ||
107 GR_Scalar1 != texture->contentScaleY()) {
108 if (GrSamplerState::kRadial_SampleMode == mode) {
109 GrMatrix scale;
110 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
111 matrix->postConcat(scale);
112 } else if (GrSamplerState::kNormal_SampleMode == mode) {
113 GrMatrix scale;
114 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
115 matrix->postConcat(scale);
116 } else {
117 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
118 }
119 }
120 GrGLTexture::Orientation orientation = texture->orientation();
121 if (GrGLTexture::kBottomUp_Orientation == orientation) {
122 GrMatrix invY;
123 invY.setAll(GR_Scalar1, 0, 0,
124 0, -GR_Scalar1, GR_Scalar1,
125 0, 0, GrMatrix::I()[8]);
126 matrix->postConcat(invY);
127 } else {
128 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
129 }
130}
131
bsalomon@google.comd302f142011-03-03 13:54:13 +0000132bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000133 const GrSamplerState& sampler) {
134 GrAssert(NULL != texture);
135 if (!sampler.getMatrix().isIdentity()) {
136 return false;
137 }
138 if (GR_Scalar1 != texture->contentScaleX() ||
139 GR_Scalar1 != texture->contentScaleY()) {
140 return false;
141 }
142 GrGLTexture::Orientation orientation = texture->orientation();
143 if (GrGLTexture::kBottomUp_Orientation == orientation) {
144 return false;
145 } else {
146 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
147 }
148 return true;
149}
150
151///////////////////////////////////////////////////////////////////////////////
152
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000153static bool gPrintStartupSpew;
154
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000155static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000156
twiz@google.com0f31ca72011-03-18 17:38:11 +0000157 GrGLint savedFBO;
158 GrGLint savedTexUnit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000159 GR_GL_GetIntegerv(gl, GR_GL_ACTIVE_TEXTURE, &savedTexUnit);
160 GR_GL_GetIntegerv(gl, GR_GL_FRAMEBUFFER_BINDING, &savedFBO);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000161
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000162 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000163
twiz@google.com0f31ca72011-03-18 17:38:11 +0000164 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000165 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
166 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000167 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000168 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
169 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000170 // some implementations require texture to be mip-map complete before
171 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000172 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
173 GR_GL_TEXTURE_MIN_FILTER,
174 GR_GL_NEAREST));
175 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
176 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
177 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
178 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
179 GR_GL_COLOR_ATTACHMENT0,
180 GR_GL_TEXTURE_2D, testRTTex, 0));
181 GrGLenum status = GR_GL_CALL(gl, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
182 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
183 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185 GR_GL_CALL(gl, ActiveTexture(savedTexUnit));
186 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, savedFBO));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000187
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000188 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000189}
190
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000191static bool probe_for_npot_render_target_support(const GrGLInterface* gl,
192 bool hasNPOTTextureSupport) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000193
194 /* Experimentation has found that some GLs that support NPOT textures
195 do not support FBOs with a NPOT texture. They report "unsupported" FBO
196 status. I don't know how to explicitly query for this. Do an
197 experiment. Note they may support NPOT with a renderbuffer but not a
198 texture. Presumably, the implementation bloats the renderbuffer
199 internally to the next POT.
200 */
201 if (hasNPOTTextureSupport) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000202 return fbo_test(gl, 200, 200);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000203 }
204 return false;
205}
206
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000207static int probe_for_min_render_target_height(const GrGLInterface* gl,
208 bool hasNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000209 int maxRenderTargetSize) {
210 /* The iPhone 4 has a restriction that for an FBO with texture color
211 attachment with height <= 8 then the width must be <= height. Here
212 we look for such a limitation.
213 */
214 if (gPrintStartupSpew) {
215 GrPrintf("Small height FBO texture experiments\n");
216 }
217 int minRenderTargetHeight = GR_INVAL_GLINT;
218 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
219 GrGLuint w = maxRenderTargetSize;
220 GrGLuint h = i;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000221 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000222 if (gPrintStartupSpew) {
223 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
224 }
225 minRenderTargetHeight = i;
226 break;
227 } else {
228 if (gPrintStartupSpew) {
229 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
230 }
231 }
232 }
233 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
234
235 return minRenderTargetHeight;
236}
237
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000238static int probe_for_min_render_target_width(const GrGLInterface* gl,
239 bool hasNPOTRenderTargetSupport,
240 int maxRenderTargetSize) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000241
242 if (gPrintStartupSpew) {
243 GrPrintf("Small width FBO texture experiments\n");
244 }
245 int minRenderTargetWidth = GR_INVAL_GLINT;
246 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
247 GrGLuint w = i;
248 GrGLuint h = maxRenderTargetSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000249 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000250 if (gPrintStartupSpew) {
251 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
252 }
253 minRenderTargetWidth = i;
254 break;
255 } else {
256 if (gPrintStartupSpew) {
257 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
258 }
259 }
260 }
261 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
262
263 return minRenderTargetWidth;
264}
265
266
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000267GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding)
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000268 : fStencilFormats(8) {
269
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000270 gl->ref();
271 fGL = gl;
272 fGLBinding = glBinding;
273 switch (glBinding) {
274 case kDesktop_GrGLBinding:
275 GrAssert(gl->supportsDesktop());
276 break;
277 case kES1_GrGLBinding:
278 GrAssert(gl->supportsES1());
279 break;
280 case kES2_GrGLBinding:
281 GrAssert(gl->supportsES2());
282 break;
283 default:
284 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000285 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000286
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000287 GrGLClearErr(fGL);
288
289 if (gPrintStartupSpew) {
290 const GrGLubyte* vendor = GL_CALL(GetString(GR_GL_VENDOR));
291 const GrGLubyte* renderer = GL_CALL(GetString(GR_GL_RENDERER));
292 const GrGLubyte* version = GL_CALL(GetString(GR_GL_VERSION));
293 const GrGLubyte* ext = GL_CALL(GetString(GR_GL_EXTENSIONS));
294 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
295 this);
296 GrPrintf("------ VENDOR %s\n", vendor);
297 GrPrintf("------ RENDERER %s\n", renderer);
298 GrPrintf("------ VERSION %s\n", version);
299 GrPrintf("------ EXTENSIONS\n %s \n", ext);
300 }
301
302 fGLVersion = gl_version_as_float(gl);
303 fExtensionString = (const char*) GL_CALL(GetString(GR_GL_EXTENSIONS));
reed@google.comac10a2d2010-12-22 21:39:39 +0000304
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000305 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000306
twiz@google.com0f31ca72011-03-18 17:38:11 +0000307 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000308 // check FS and fixed-function texture unit limits
309 // we only use textures in the fragment stage currently.
310 // checks are > to make sure we have a spare unit.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000311 if (kES1_GrGLBinding != this->glBinding()) {
312 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000313 GrAssert(maxTextureUnits > kNumStages);
314 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000315 if (kES2_GrGLBinding != this->glBinding()) {
316 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000317 GrAssert(maxTextureUnits > kNumStages);
318 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000319 if (kES2_GrGLBinding == this->glBinding()) {
320 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000321 &fMaxFragmentUniformVectors);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000322 } else if (kDesktop_GrGLBinding != this->glBinding()) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000323 GrGLint max;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000324 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000325 fMaxFragmentUniformVectors = max / 4;
326 } else {
327 fMaxFragmentUniformVectors = 16;
328 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000329
reed@google.comac10a2d2010-12-22 21:39:39 +0000330 ////////////////////////////////////////////////////////////////////////////
331 // Check for supported features.
332
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000333 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000334
twiz@google.com0f31ca72011-03-18 17:38:11 +0000335 GrGLint numFormats;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000336 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000337 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000338 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000339 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000340 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000341 f8bitPaletteSupport = true;
342 break;
343 }
344 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000345
346 if (gPrintStartupSpew) {
347 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
348 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000349
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000350 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
351 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
352 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
353 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000354
355 memset(fAASamples, 0, sizeof(fAASamples));
356 fMSFBOType = kNone_MSFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000357 if (kDesktop_GrGLBinding != this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000358 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000359 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000360 // and fbo_blit extensions.
361 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000362 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000363 fMSFBOType = kAppleES_MSFBO;
364 }
365 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000366 if ((fGLVersion >= 3.f) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000367 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000368 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
369 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000370 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000371 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000372 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000373 if (gPrintStartupSpew) {
374 switch (fMSFBOType) {
375 case kNone_MSFBO:
376 GrPrintf("MSAA Support: NONE\n");
377 break;
378 case kDesktopARB_MSFBO:
379 GrPrintf("MSAA Support: DESKTOP ARB.\n");
380 break;
381 case kDesktopEXT_MSFBO:
382 GrPrintf("MSAA Support: DESKTOP EXT.\n");
383 break;
384 case kAppleES_MSFBO:
385 GrPrintf("MSAA Support: APPLE ES.\n");
386 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000387 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000388 }
389
390 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000391 GrGLint maxSamples;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000392 GR_GL_GetIntegerv(gl, GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000393 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000394 fAASamples[kNone_GrAALevel] = 0;
395 fAASamples[kLow_GrAALevel] = GrMax(2,
396 GrFixedFloorToInt((GR_FixedHalf) *
397 maxSamples));
398 fAASamples[kMed_GrAALevel] = GrMax(2,
399 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
400 maxSamples));
401 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000402 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000403 if (gPrintStartupSpew) {
404 GrPrintf("\tMax Samples: %d\n", maxSamples);
405 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000406 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000407 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000408
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000409 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000410 fHasStencilWrap = (fGLVersion >= 1.4f) ||
411 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000412 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000413 fHasStencilWrap = (fGLVersion >= 2.0f) || this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000414 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000415 if (gPrintStartupSpew) {
416 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
417 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000418
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000419 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000420 // we could also look for GL_ATI_separate_stencil extension or
421 // GL_EXT_stencil_two_side but they use different function signatures
422 // than GL2.0+ (and than each other).
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000423 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000424 // supported on GL 1.4 and higher or by extension
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000425 fStencilWrapOpsSupport = (fGLVersion >= 1.4f) ||
426 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000427 } else {
428 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
429 // an ES1 extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000430 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000431 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000432 fStencilWrapOpsSupport = (fGLVersion >= 2.f) ||
433 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000434 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000435 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000436 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
437 (fTwoSidedStencilSupport ? "YES" : "NO"),
438 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000439 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000440
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000441 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000442 fRGBA8Renderbuffer = true;
443 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000444 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000445 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000446 if (gPrintStartupSpew) {
447 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
448 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
450
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000451 if (kDesktop_GrGLBinding != this->glBinding()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000452 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000453 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000454 }
455 }
456
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000457 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000458 fBufferLockSupport = true; // we require VBO support and the desktop VBO
459 // extension includes glMapBuffer.
460 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000461 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000462 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000463
reed@google.comeeeb5a02010-12-23 15:12:59 +0000464 if (gPrintStartupSpew) {
465 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
466 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000468 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000469 if (fGLVersion >= 2.f ||
470 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000471 fNPOTTextureTileSupport = true;
472 fNPOTTextureSupport = true;
473 } else {
474 fNPOTTextureTileSupport = false;
475 fNPOTTextureSupport = false;
476 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000477 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000478 if (fGLVersion >= 2.f) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000479 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000480 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000481 } else {
482 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000483 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000484 fNPOTTextureTileSupport = false;
485 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000486 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000487
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000488 fAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com205d4602011-04-25 12:43:45 +0000489
reed@google.comac10a2d2010-12-22 21:39:39 +0000490 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000491 // Experiments to determine limitations that can't be queried.
492 // TODO: Make these a preprocess that generate some compile time constants.
493 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000494
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000495 int expectNPOTTargets = gl->fNPOTRenderTargetSupport;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000496 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000497 fNPOTRenderTargetSupport =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000498 probe_for_npot_render_target_support(gl, fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000499 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000500 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
501 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000502 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000503
bsalomon@google.com0748f212011-02-01 22:56:16 +0000504 if (gPrintStartupSpew) {
505 if (fNPOTTextureSupport) {
506 GrPrintf("NPOT textures supported\n");
507 if (fNPOTTextureTileSupport) {
508 GrPrintf("NPOT texture tiling supported\n");
509 } else {
510 GrPrintf("NPOT texture tiling NOT supported\n");
511 }
512 if (fNPOTRenderTargetSupport) {
513 GrPrintf("NPOT render targets supported\n");
514 } else {
515 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000516 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000518 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000519 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000520 }
521
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000522 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
523 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000524 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000525 // attachment, hence this min:
526 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000527
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000528 fMinRenderTargetHeight = gl->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000529 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000530 fMinRenderTargetHeight =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000531 probe_for_min_render_target_height(gl,fNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000532 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000533 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000534
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000535 fMinRenderTargetWidth = gl->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000536 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000537 fMinRenderTargetWidth =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000538 probe_for_min_render_target_width(gl, fNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000539 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000540 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000541
bsalomon@google.comfe676522011-06-17 18:12:21 +0000542 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000543}
544
545GrGpuGL::~GrGpuGL() {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000546 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000547}
548
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000549void GrGpuGL::resetContext() {
550 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000551 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000552 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000553
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000554 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000555 GL_CALL(Disable(GR_GL_DEPTH_TEST));
556 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000557
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000558 GL_CALL(Disable(GR_GL_CULL_FACE));
559 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000560 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000561
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000562 GL_CALL(Disable(GR_GL_DITHER));
563 if (kDesktop_GrGLBinding == this->glBinding()) {
564 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
565 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
566 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000567 fHWAAState.fMSAAEnabled = false;
568 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000569 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000572 fHWDrawState.fFlagBits = 0;
573
reed@google.comac10a2d2010-12-22 21:39:39 +0000574 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000575 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000576
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000577 // invalid
578 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000579
reed@google.comac10a2d2010-12-22 21:39:39 +0000580 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000581 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
582 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000583
584 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000585 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000586
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000588
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000589 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000590
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000591 for (int s = 0; s < kNumStages; ++s) {
592 fHWDrawState.fTextures[s] = NULL;
593 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
594 -GR_ScalarMax,
595 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000596 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000597 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000598 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000599
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000600 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000601 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000602 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000603 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000604
bsalomon@google.comd302f142011-03-03 13:54:13 +0000605 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000607 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000608
609 fHWGeometryState.fIndexBuffer = NULL;
610 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000611
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000612 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000613
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000614 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000615 fHWDrawState.fRenderTarget = NULL;
616}
617
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000618GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
619
620 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
621 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
622 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
623 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
624
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000625 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000626 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000627
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000628 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000629 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000630 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000631#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
632 if (desc.fSampleCnt) {
633#else
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000634 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000635#endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000636 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000637 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000638 } else {
639 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000640 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000641 }
642 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000643 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000644 }
645 // we don't know what the RB ids are without glGets and we don't care
646 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000647 rtDesc.fMSColorRenderbufferID = 0;
648#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
649 rtDesc.fSampleCnt = desc.fSampleCnt;
650#else
651 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
652 // just guess, this code path is only compiled in WK and we aren't
653 // using MSAA anyway. This will be stripped out soon when WK sets
654 // the fSampleCnt in GrPlatformSurfaceDesc.
655 rtDesc.fSampleCnt = 4;
656 } else {
657 rtDesc.fSampleCnt = 0;
658 }
659#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000660 if (desc.fStencilBits) {
661 GrGLStencilBuffer::Format format;
662 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
663 format.fPacked = false;
664 format.fStencilBits = desc.fStencilBits;
665 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000666 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
667 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000668 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000669 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000670 }
671
672 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000673 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000674 GrGLenum dontCare;
675 if (!canBeTexture(desc.fConfig, &dontCare,
676 &texDesc.fUploadFormat,
677 &texDesc.fUploadType)) {
678 return NULL;
679 }
680
681 GrGLTexture::TexParams params;
682
683 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
684 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
685
bsalomon@google.com90405932011-06-17 15:56:55 +0000686 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000687 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000688 texDesc.fTextureID = desc.fPlatformTexture;
689 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
690 texDesc.fOwnsID = false;
691
692 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000693 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000694 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000695 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000696 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000697 } else {
698 return new GrGLTexture(this, texDesc, params);
699 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000700 } else {
701 GrGLIRect viewport;
702 viewport.fLeft = 0;
703 viewport.fBottom = 0;
704 viewport.fWidth = desc.fWidth;
705 viewport.fHeight = desc.fHeight;
706
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000707 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000708 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000709 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000710 }
711}
712
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000713namespace {
714
715static const GrGLenum kUnknownGLFormat = ~0;
716
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000717GrGLenum get_fbo_color_format(const GrGLInterface* gl) {
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000718 GrGLint cbType;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000719 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000720 GR_GL_COLOR_ATTACHMENT0,
721 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
722 &cbType);
723 GrGLint cbID;
724 GrGLint cbFormat;
725 switch (cbType) {
726 case GR_GL_RENDERBUFFER:
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000727 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000728 GR_GL_COLOR_ATTACHMENT0,
729 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
730 &cbID);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000731 GR_GL_CALL(gl, BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
732 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000733 GR_GL_RENDERBUFFER_INTERNAL_FORMAT,
734 &cbFormat);
735 return cbFormat;
736 break;
737 case GR_GL_TEXTURE:
738 // ES doesn't have glGetTexLevelParameter
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000739 if (gl->supportsDesktop()) {
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000740 GrGLint cbLevel;
741 GrGLint cbFace;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000742 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000743 GR_GL_COLOR_ATTACHMENT0,
744 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
745 &cbID);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000746 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000747 GR_GL_COLOR_ATTACHMENT0,
748 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
749 &cbLevel);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000750 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000751 GR_GL_COLOR_ATTACHMENT0,
752 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
753 &cbFace);
754 GrGLenum bind;
755 GrGLenum target;
756 if (cbFace) {
757 bind = GR_GL_TEXTURE_CUBE_MAP;
758 target = cbFace;
759 } else {
760 bind = GR_GL_TEXTURE_2D;
761 target = GR_GL_TEXTURE_2D;
762 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000763 GR_GL_CALL(gl, BindTexture(bind, cbID));
764 GR_GL_GetTexLevelParameteriv(gl, target, cbLevel,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000765 GR_GL_TEXTURE_INTERNAL_FORMAT, &cbFormat);
766 return cbFormat;
767 } else {
768 return kUnknownGLFormat;
769 }
770 break;
771 default:
772 // we can get here with FBO 0, not a render buffer or a texture
773 return kUnknownGLFormat;
774 }
775}
776
777GrPixelConfig internal_color_format_to_config(GrGLenum iFormat) {
778 switch (iFormat) {
779 case GR_GL_RGB565:
780 return kRGB_565_GrPixelConfig;
781 case GR_GL_RGBA4:
782 return kRGBA_4444_GrPixelConfig;
783 case GR_GL_RGBA8:
784 case GR_GL_SRGB8_ALPHA8:
785 case GR_GL_SRGB_ALPHA:
786 case GR_GL_RGBA:
787 case GR_GL_BGRA:
788 return kRGBA_8888_GrPixelConfig;
789 case GR_GL_RGB8:
790 case GR_GL_SRGB8:
791 case GR_GL_SRGB:
792 return kRGBX_8888_GrPixelConfig;
793 default:
794 // there are many GL formats we don't have enums
795 // for. We should still render to them if the client
796 // asks us.
797 return kUnknown_GrPixelConfig;
798 }
799}
800
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000801GrPixelConfig get_implied_color_config(const GrGLInterface* gl,
802 bool arbFBOExtension) {
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000803 GrGLint rSize, bSize, gSize, aSize;
804 if (arbFBOExtension) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000805 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000806 GR_GL_COLOR_ATTACHMENT0,
807 GR_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &rSize);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000808 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000809 GR_GL_COLOR_ATTACHMENT0,
810 GR_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &gSize);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000811 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000812 GR_GL_COLOR_ATTACHMENT0,
813 GR_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &bSize);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000814 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000815 GR_GL_COLOR_ATTACHMENT0,
816 GR_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &aSize);
817 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000818 GR_GL_GetIntegerv(gl, GR_GL_RED_BITS, &rSize);
819 GR_GL_GetIntegerv(gl, GR_GL_GREEN_BITS, &gSize);
820 GR_GL_GetIntegerv(gl, GR_GL_BLUE_BITS, &bSize);
821 GR_GL_GetIntegerv(gl, GR_GL_ALPHA_BITS, &aSize);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000822 }
823
824 if(8 == rSize && 8 == gSize && 8 == bSize) {
825 if (0 == aSize) {
826 return kRGBX_8888_GrPixelConfig;
827 } else if (8 == aSize) {
828 return kRGBA_8888_GrPixelConfig;
829 }
830 } else if (4 == rSize && 4 == gSize && 4 == bSize && 4 == aSize) {
831 return kRGBA_4444_GrPixelConfig;
832 } else if (5 == rSize && 6 == gSize && 5 == bSize && 0 == aSize) {
833 return kRGB_565_GrPixelConfig;
834 }
835 return kUnknown_GrPixelConfig;
836}
837
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000838int get_fbo_stencil_bits(const GrGLInterface* gl, bool arbFBOExtension) {
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000839 GrGLint stencilBits;
840 if (arbFBOExtension) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000841 GR_GL_GetFramebufferAttachmentParameteriv(gl, GR_GL_FRAMEBUFFER,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000842 GR_GL_STENCIL_ATTACHMENT,
843 GR_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
844 &stencilBits);
845 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000846 GR_GL_GetIntegerv(gl, GR_GL_STENCIL_BITS, &stencilBits);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000847 }
848 return stencilBits;
849}
850}
851
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000852GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() {
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000853
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000854 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000855
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000856 GR_GL_GetIntegerv(this->glInterface(), GR_GL_FRAMEBUFFER_BINDING,
857 (GrGLint*)&rtDesc.fRTFBOID);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000858 rtDesc.fTexFBOID = rtDesc.fRTFBOID;
859 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000860
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000861 bool arbFBO = ((kDesktop_GrGLBinding == fGLBinding) && (fGLVersion > 3.0 ||
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000862 this->hasExtension("GL_ARB_framebuffer_object")));
863
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000864 GrGLIRect viewport;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000865 viewport.setFromGLViewport(this->glInterface());
866 int stencilBits = get_fbo_stencil_bits(this->glInterface(), arbFBO);
867 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &rtDesc.fSampleCnt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000868
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000869 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000870 if (stencilBits) {
871 GrGLStencilBuffer::Format format;
872 // we could query this but we don't really need it
873 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
874 format.fPacked = false;
875 format.fStencilBits = stencilBits;
876 format.fTotalBits = stencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000877 sb.reset(new GrGLStencilBuffer(this, 0, viewport.fWidth,
878 viewport.fHeight, rtDesc.fSampleCnt,
879 format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880 }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000881
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000882 GrGLenum fmat = get_fbo_color_format(this->glInterface());
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000883 if (kUnknownGLFormat == fmat) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000884 rtDesc.fConfig = get_implied_color_config(this->glInterface(), arbFBO);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000885 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000886 rtDesc.fConfig = internal_color_format_to_config(fmat);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000887 }
888
889 // may have to bind a texture to gets its format
890 this->setSpareTextureUnit();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000891
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000892 rtDesc.fOwnIDs = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000893
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000894 GrGLRenderTarget* target = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000895 target->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000896 return target;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000897}
898
bsalomon@google.com5782d712011-01-21 21:03:59 +0000899///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000900static const GrGLuint kUnknownBitCount = ~0;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000901
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000902void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000903
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000904 // Build up list of legal stencil formats (though perhaps not supported on
905 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000906
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000907 // these consts are in order of most preferred to least preferred
908 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 static const GrGLStencilBuffer::Format
910 // internal Format stencil bits total bits packed?
911 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
912 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
913 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
914 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
915 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
916 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000917
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000918 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000919 bool supportsPackedDS = fGLVersion >= 3.0f ||
920 this->hasExtension("GL_EXT_packed_depth_stencil") ||
921 this->hasExtension("GL_ARB_framebuffer_object");
922
923 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
924 // require FBO support we can expect these are legal formats and don't
925 // check. These also all support the unsized GL_STENCIL_INDEX.
926 fStencilFormats.push_back() = gS8;
927 fStencilFormats.push_back() = gS16;
928 if (supportsPackedDS) {
929 fStencilFormats.push_back() = gD24S8;
930 }
931 fStencilFormats.push_back() = gS4;
932 if (supportsPackedDS) {
933 fStencilFormats.push_back() = gDS;
934 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000935 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000936 // ES2 has STENCIL_INDEX8 without extensions.
937 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
938 // introduces tokens for S1 thu S8 but there are separate extensions
939 // that make them legal (GL_OES_stencil1, ...).
940 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
941 // ES doesn't support using the unsized formats.
942
943 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
944 fStencilFormats.push_back() = gS8;
945 }
946 //fStencilFormats.push_back() = gS16;
947 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
948 fStencilFormats.push_back() = gD24S8;
949 }
950 if (this->hasExtension("GL_OES_stencil4")) {
951 fStencilFormats.push_back() = gS4;
952 }
953 // we require some stencil format.
954 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000955 }
956}
957
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000958////////////////////////////////////////////////////////////////////////////////
959
960void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
961 GrGLenum internalFormat,
962 const void* data,
963 size_t rowBytes) {
964 // we assume the texture is bound;
965 if (!rowBytes) {
966 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
967 }
968
969 // in case we need a temporary, trimmed copy of the src pixels
970 SkAutoSMalloc<128 * 128> tempStorage;
971
972 /*
973 * check whether to allocate a temporary buffer for flipping y or
974 * because our data has extra bytes past each row. If so, we need
975 * to trim those off here, since GL ES doesn't let us specify
976 * GL_UNPACK_ROW_LENGTH.
977 */
978 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000979 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000980 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000981 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
982 rowBytes / desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000983 }
984 } else {
985 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
986 if (data && (trimRowBytes < rowBytes || flipY)) {
987 // copy the data into our new storage, skipping the trailing bytes
988 size_t trimSize = desc.fContentHeight * trimRowBytes;
989 const char* src = (const char*)data;
990 if (flipY) {
991 src += (desc.fContentHeight - 1) * rowBytes;
992 }
993 char* dst = (char*)tempStorage.realloc(trimSize);
994 for (int y = 0; y < desc.fContentHeight; y++) {
995 memcpy(dst, src, trimRowBytes);
996 if (flipY) {
997 src -= rowBytes;
998 } else {
999 src += rowBytes;
1000 }
1001 dst += trimRowBytes;
1002 }
1003 // now point data to our trimmed version
1004 data = tempStorage.get();
1005 rowBytes = trimRowBytes;
1006 }
1007 }
1008
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001009 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001010 if (kIndex_8_GrPixelConfig == desc.fFormat &&
1011 supports8BitPalette()) {
1012 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
1013 GrAssert(desc.fContentWidth == desc.fAllocWidth);
1014 GrAssert(desc.fContentHeight == desc.fAllocHeight);
1015 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
1016 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001017 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
1018 desc.fAllocWidth, desc.fAllocHeight,
1019 0, imageSize, data));
1020 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001021 } else {
1022 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
1023 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001024 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1025 desc.fAllocWidth, desc.fAllocHeight,
1026 0, desc.fUploadFormat, desc.fUploadType, NULL));
1027 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
1028 desc.fContentHeight, desc.fUploadFormat,
1029 desc.fUploadType, data));
1030 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001031
1032 int extraW = desc.fAllocWidth - desc.fContentWidth;
1033 int extraH = desc.fAllocHeight - desc.fContentHeight;
1034 int maxTexels = extraW * extraH;
1035 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
1036 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
1037
1038 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
1039
1040 // rowBytes is actual stride between rows in data
1041 // rowDataBytes is the actual amount of non-pad data in a row
1042 // and the stride used for uploading extraH rows.
1043 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
1044 if (extraH) {
1045 uint8_t* lastRowStart = (uint8_t*) data +
1046 (desc.fContentHeight - 1) * rowBytes;
1047 uint8_t* extraRowStart = (uint8_t*)texels.get();
1048
1049 for (int i = 0; i < extraH; ++i) {
1050 memcpy(extraRowStart, lastRowStart, rowDataBytes);
1051 extraRowStart += rowDataBytes;
1052 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001053 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
1054 desc.fContentHeight, desc.fContentWidth,
1055 extraH, desc.fUploadFormat,
1056 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001057 }
1058 if (extraW) {
1059 uint8_t* edgeTexel = (uint8_t*)data +
1060 rowDataBytes - desc.fUploadByteCount;
1061 uint8_t* extraTexel = (uint8_t*)texels.get();
1062 for (int j = 0; j < desc.fContentHeight; ++j) {
1063 for (int i = 0; i < extraW; ++i) {
1064 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
1065 extraTexel += desc.fUploadByteCount;
1066 }
1067 edgeTexel += rowBytes;
1068 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001069 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
1070 0, extraW, desc.fContentHeight,
1071 desc.fUploadFormat, desc.fUploadType,
1072 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001073 }
1074 if (extraW && extraH) {
1075 uint8_t* cornerTexel = (uint8_t*)data +
1076 desc.fContentHeight * rowBytes -
1077 desc.fUploadByteCount;
1078 uint8_t* extraTexel = (uint8_t*)texels.get();
1079 for (int i = 0; i < extraW*extraH; ++i) {
1080 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
1081 extraTexel += desc.fUploadByteCount;
1082 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001083 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
1084 desc.fContentHeight, extraW, extraH,
1085 desc.fUploadFormat, desc.fUploadType,
1086 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001087 }
1088
1089 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001090 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1091 desc.fAllocWidth, desc.fAllocHeight, 0,
1092 desc.fUploadFormat, desc.fUploadType, data));
1093 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001094 }
1095 }
1096}
1097
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001098bool GrGpuGL::createRenderTargetObjects(int width, int height,
1099 GrGLuint texID,
1100 GrGLRenderTarget::Desc* desc) {
1101 desc->fMSColorRenderbufferID = 0;
1102 desc->fRTFBOID = 0;
1103 desc->fTexFBOID = 0;
1104 desc->fOwnIDs = true;
1105
1106 GrGLenum status;
1107 GrGLint err;
1108
bsalomon@google.comab15d612011-08-09 12:57:56 +00001109 GrGLenum msColorFormat = 0; // suppress warning
1110
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 if (!desc->fTexFBOID) {
1113 goto FAILED;
1114 }
1115
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001116
1117 // If we are using multisampling we will create two FBOS. We render
1118 // to one and then resolve to the texture bound to the other.
1119 if (desc->fSampleCnt > 1 && kNone_MSFBO != fMSFBOType) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
1121 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001122 if (!desc->fRTFBOID ||
1123 !desc->fMSColorRenderbufferID ||
1124 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
1125 goto FAILED;
1126 }
1127 } else {
1128 desc->fRTFBOID = desc->fTexFBOID;
1129 }
1130
1131 if (desc->fRTFBOID != desc->fTexFBOID) {
1132 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001133 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1136 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1137 desc->fSampleCnt,
1138 msColorFormat,
1139 width, height));
1140 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141 if (err != GR_GL_NO_ERROR) {
1142 goto FAILED;
1143 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
1145 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 GR_GL_COLOR_ATTACHMENT0,
1147 GR_GL_RENDERBUFFER,
1148 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001149 GrGLenum status = GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1151 goto FAILED;
1152 }
1153 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001154 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001156 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1157 GR_GL_COLOR_ATTACHMENT0,
1158 GR_GL_TEXTURE_2D,
1159 texID, 0));
1160 status = GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1162 goto FAILED;
1163 }
1164
1165 return true;
1166
1167FAILED:
1168 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 }
1171 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001172 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 }
1174 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001175 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 }
1177 return false;
1178}
1179
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001180// good to set a break-point here to know when createTexture fails
1181static GrTexture* return_null_texture() {
1182// GrAssert(!"null texture");
1183 return NULL;
1184}
1185
1186#if GR_DEBUG
1187static size_t as_size_t(int x) {
1188 return x;
1189}
1190#endif
1191
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001192GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001193 const void* srcData,
1194 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001195
1196#if GR_COLLECT_STATS
1197 ++fStats.fTextureCreateCnt;
1198#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001199
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001200 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001201 GR_GL_NEAREST,
1202 GR_GL_CLAMP_TO_EDGE,
1203 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001204 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001205
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001206 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001207 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001208 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001209
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001210 glTexDesc.fContentWidth = desc.fWidth;
1211 glTexDesc.fContentHeight = desc.fHeight;
1212 glTexDesc.fAllocWidth = desc.fWidth;
1213 glTexDesc.fAllocHeight = desc.fHeight;
1214 glTexDesc.fFormat = desc.fFormat;
1215 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001216
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001217 glRTDesc.fMSColorRenderbufferID = 0;
1218 glRTDesc.fRTFBOID = 0;
1219 glRTDesc.fTexFBOID = 0;
1220 glRTDesc.fOwnIDs = true;
1221 glRTDesc.fConfig = glTexDesc.fFormat;
1222
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001223 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001224 if (!canBeTexture(desc.fFormat,
1225 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001226 &glTexDesc.fUploadFormat,
1227 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 return return_null_texture();
1229 }
1230
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001231 // We keep GrRenderTargets in GL's normal orientation so that they
1232 // can be drawn to by the outside world without the client having
1233 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001234 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001236
reed@google.comac10a2d2010-12-22 21:39:39 +00001237 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001238 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001239 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 GrPrintf("AA RT requested but not supported on this platform.");
1241 }
1242
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001243 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001244
reed@google.comac10a2d2010-12-22 21:39:39 +00001245 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001246 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001247 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1248 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001249 }
1250
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001251 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 glTexDesc.fAllocWidth);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001253 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001254 glTexDesc.fAllocHeight);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001255 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1256 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001257 return return_null_texture();
1258 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001259 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001260 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1261 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1262 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1263 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001264 return return_null_texture();
1265 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 }
1267
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001268 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001269 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001270 return return_null_texture();
1271 }
1272
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001273 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001274 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1275 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1276 GR_GL_TEXTURE_MAG_FILTER,
1277 DEFAULT_PARAMS.fFilter));
1278 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1279 GR_GL_TEXTURE_MIN_FILTER,
1280 DEFAULT_PARAMS.fFilter));
1281 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1282 GR_GL_TEXTURE_WRAP_S,
1283 DEFAULT_PARAMS.fWrapS));
1284 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1285 GR_GL_TEXTURE_WRAP_T,
1286 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001287
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001288 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001289
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001292 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001293#if GR_COLLECT_STATS
1294 ++fStats.fRenderTargetCreateCnt;
1295#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001296 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1297 glTexDesc.fAllocHeight,
1298 glTexDesc.fTextureID,
1299 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001300 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 return return_null_texture();
1302 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001303 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1304 } else {
1305 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001306 }
1307#ifdef TRACE_TEXTURE_CREATION
1308 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1309 tex->fTextureID, width, height, tex->fUploadByteCount);
1310#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001311 return tex;
1312}
1313
1314namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001315void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1316 GrGLuint rb,
1317 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001318 // we shouldn't ever know one size and not the other
1319 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1320 (kUnknownBitCount == format->fTotalBits));
1321 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001322 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001323 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1324 (GrGLint*)&format->fStencilBits);
1325 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001326 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001327 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1328 (GrGLint*)&format->fTotalBits);
1329 format->fTotalBits += format->fStencilBits;
1330 } else {
1331 format->fTotalBits = format->fStencilBits;
1332 }
1333 }
1334}
1335}
1336
1337bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1338 int width, int height) {
1339
1340 // All internally created RTs are also textures. We don't create
1341 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1342 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001343 GrAssert(width >= rt->allocatedWidth());
1344 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001345
1346 int samples = rt->numSamples();
1347 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001348 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001349 if (!sbID) {
1350 return false;
1351 }
1352
1353 GrGLStencilBuffer* sb = NULL;
1354
1355 int stencilFmtCnt = fStencilFormats.count();
1356 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001357 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001358 // we start with the last stencil format that succeeded in hopes
1359 // that we won't go through this loop more than once after the
1360 // first (painful) stencil creation.
1361 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001362 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001363 // version on a GL that doesn't have an MSAA extension.
1364 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001365 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1366 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001367 GR_GL_RENDERBUFFER,
1368 samples,
1369 fStencilFormats[sIdx].fInternalFormat,
1370 width,
1371 height));
1372 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001373 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1374 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001375 fStencilFormats[sIdx].fInternalFormat,
1376 width, height));
1377 }
1378
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001379 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001380 if (err == GR_GL_NO_ERROR) {
1381 // After sized formats we attempt an unsized format and take whatever
1382 // sizes GL gives us. In that case we query for the size.
1383 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001384 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001385 sb = new GrGLStencilBuffer(this, sbID, width, height,
1386 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001387 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1388 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001389 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001390 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001391 return true;
1392 }
1393 sb->abandon(); // otherwise we lose sbID
1394 sb->unref();
1395 }
1396 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001397 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001398 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001399}
1400
1401bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1402 GrRenderTarget* rt) {
1403 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1404
1405 GrGLuint fbo = glrt->renderFBOID();
1406
1407 if (NULL == sb) {
1408 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001409 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001410 GR_GL_STENCIL_ATTACHMENT,
1411 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001412 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001413 GR_GL_DEPTH_ATTACHMENT,
1414 GR_GL_RENDERBUFFER, 0));
1415#if GR_DEBUG
1416 GrGLenum status =
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001417 GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001418 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1419#endif
1420 }
1421 return true;
1422 } else {
1423 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1424 GrGLuint rb = glsb->renderbufferID();
1425
reed@google.comac10a2d2010-12-22 21:39:39 +00001426 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001427 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1428 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001429 GR_GL_STENCIL_ATTACHMENT,
1430 GR_GL_RENDERBUFFER, rb));
1431 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001432 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001433 GR_GL_DEPTH_ATTACHMENT,
1434 GR_GL_RENDERBUFFER, rb));
1435 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001436 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001437 GR_GL_DEPTH_ATTACHMENT,
1438 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001439 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001440
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001441 GrGLenum status = GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001442 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001443 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001444 GR_GL_STENCIL_ATTACHMENT,
1445 GR_GL_RENDERBUFFER, 0));
1446 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001447 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001448 GR_GL_DEPTH_ATTACHMENT,
1449 GR_GL_RENDERBUFFER, 0));
1450 }
1451 return false;
1452 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001453 return true;
1454 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001455 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001456}
1457
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001458////////////////////////////////////////////////////////////////////////////////
1459
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001460GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001461 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001462 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001464 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001465 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001466 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001467 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001468 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1469 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1470 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1471 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1472 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001473 // deleting bound buffer does implicit bind to 0
1474 fHWGeometryState.fVertexBuffer = NULL;
1475 return NULL;
1476 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001477 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001478 size, dynamic);
1479 fHWGeometryState.fVertexBuffer = vertexBuffer;
1480 return vertexBuffer;
1481 }
1482 return NULL;
1483}
1484
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001485GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001486 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001487 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001488 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001489 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1490 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001492 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1493 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1494 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1495 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1496 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001497 // deleting bound buffer does implicit bind to 0
1498 fHWGeometryState.fIndexBuffer = NULL;
1499 return NULL;
1500 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001501 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 size, dynamic);
1503 fHWGeometryState.fIndexBuffer = indexBuffer;
1504 return indexBuffer;
1505 }
1506 return NULL;
1507}
1508
reed@google.comac10a2d2010-12-22 21:39:39 +00001509void GrGpuGL::flushScissor(const GrIRect* rect) {
1510 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001511 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001512 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001513
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001514 GrGLIRect scissor;
1515 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001516 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001517 rect->width(), rect->height());
1518 if (scissor.contains(vp)) {
1519 rect = NULL;
1520 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 }
1522
1523 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001524 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001525 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001526 fHWBounds.fScissorRect = scissor;
1527 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001528 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001529 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001530 fHWBounds.fScissorEnabled = true;
1531 }
1532 } else {
1533 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001534 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001535 fHWBounds.fScissorEnabled = false;
1536 }
1537 }
1538}
1539
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001540void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001541 if (NULL == fCurrDrawState.fRenderTarget) {
1542 return;
1543 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001544 GrIRect r;
1545 if (NULL != rect) {
1546 // flushScissor expects rect to be clipped to the target.
1547 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001548 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1549 fCurrDrawState.fRenderTarget->height());
1550 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001551 rect = &r;
1552 } else {
1553 return;
1554 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001555 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001556 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001557 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001558 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001559 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001560 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1561 GrColorUnpackG(color)/255.f,
1562 GrColorUnpackB(color)/255.f,
1563 GrColorUnpackA(color)/255.f));
1564 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001565}
1566
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001567void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001568 if (NULL == fCurrDrawState.fRenderTarget) {
1569 return;
1570 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001571
1572 this->flushRenderTarget(&GrIRect::EmptyIRect());
1573
reed@google.comac10a2d2010-12-22 21:39:39 +00001574 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001575 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001576 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001577 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001578 GL_CALL(StencilMask(0xffffffff));
1579 GL_CALL(ClearStencil(0));
1580 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001581 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001582}
1583
bsalomon@google.com398109c2011-04-14 18:40:27 +00001584void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001585 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001586
1587 // this should only be called internally when we know we have a
1588 // stencil buffer.
1589 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001590#if 0
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001591 GrGLint stencilBitCount =
1592 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001593 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001594 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001595#else
1596 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001597 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001598 // turned into draws. Our contract on GrDrawTarget says that
1599 // changing the clip between stencil passes may or may not
1600 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001601 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001602#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001603 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001604 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001605 GL_CALL(StencilMask(clipStencilMask));
1606 GL_CALL(ClearStencil(0));
1607 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001608 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001609}
1610
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001611void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001612 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001613}
1614
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001615bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1616 int left, int top, int width, int height,
1617 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001618 GrGLenum internalFormat; // we don't use this for glReadPixels
1619 GrGLenum format;
1620 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001621 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1622 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001623 }
1624 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1625 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1626 switch (tgt->getResolveType()) {
1627 case GrGLRenderTarget::kCantResolve_ResolveType:
1628 return false;
1629 case GrGLRenderTarget::kAutoResolves_ResolveType:
1630 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1631 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001632 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001633 break;
1634 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001635 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001636 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001637 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1638 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001639 break;
1640 default:
1641 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 }
1643
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001644 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001645
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001646 // the read rect is viewport-relative
1647 GrGLIRect readRect;
1648 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001649 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1650 readRect.fWidth, readRect.fHeight,
1651 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001652
1653 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1654 // API presents top-to-bottom
1655 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001656 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001657 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001658 void* tmp = rowStorage.get();
1659
1660 const int halfY = height >> 1;
1661 char* top = reinterpret_cast<char*>(buffer);
1662 char* bottom = top + (height - 1) * stride;
1663 for (int y = 0; y < halfY; y++) {
1664 memcpy(tmp, top, stride);
1665 memcpy(top, bottom, stride);
1666 memcpy(bottom, tmp, stride);
1667 top += stride;
1668 bottom -= stride;
1669 }
1670 }
1671 return true;
1672}
1673
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001674void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001675
1676 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1677
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001678 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001679 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001680 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001681 #if GR_COLLECT_STATS
1682 ++fStats.fRenderTargetChngCnt;
1683 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001684 #if GR_DEBUG
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001685 GrGLenum status = GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001686 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001687 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001688 }
1689 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001690 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001691 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001692 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001693 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001694 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001695 fHWBounds.fViewportRect = vp;
1696 }
1697 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001698 if (NULL == bound || !bound->isEmpty()) {
1699 rt->flagAsNeedingResolve(bound);
1700 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001701}
1702
twiz@google.com0f31ca72011-03-18 17:38:11 +00001703GrGLenum gPrimitiveType2GLMode[] = {
1704 GR_GL_TRIANGLES,
1705 GR_GL_TRIANGLE_STRIP,
1706 GR_GL_TRIANGLE_FAN,
1707 GR_GL_POINTS,
1708 GR_GL_LINES,
1709 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001710};
1711
bsalomon@google.comd302f142011-03-03 13:54:13 +00001712#define SWAP_PER_DRAW 0
1713
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001714#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001715 #if GR_MAC_BUILD
1716 #include <AGL/agl.h>
1717 #elif GR_WIN32_BUILD
1718 void SwapBuf() {
1719 DWORD procID = GetCurrentProcessId();
1720 HWND hwnd = GetTopWindow(GetDesktopWindow());
1721 while(hwnd) {
1722 DWORD wndProcID = 0;
1723 GetWindowThreadProcessId(hwnd, &wndProcID);
1724 if(wndProcID == procID) {
1725 SwapBuffers(GetDC(hwnd));
1726 }
1727 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1728 }
1729 }
1730 #endif
1731#endif
1732
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001733void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1734 uint32_t startVertex,
1735 uint32_t startIndex,
1736 uint32_t vertexCount,
1737 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001738 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1739
twiz@google.com0f31ca72011-03-18 17:38:11 +00001740 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001741
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001742 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1743 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1744
1745 // our setupGeometry better have adjusted this to zero since
1746 // DrawElements always draws from the begining of the arrays for idx 0.
1747 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001748
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001749 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1750 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001751#if SWAP_PER_DRAW
1752 glFlush();
1753 #if GR_MAC_BUILD
1754 aglSwapBuffers(aglGetCurrentContext());
1755 int set_a_break_pt_here = 9;
1756 aglSwapBuffers(aglGetCurrentContext());
1757 #elif GR_WIN32_BUILD
1758 SwapBuf();
1759 int set_a_break_pt_here = 9;
1760 SwapBuf();
1761 #endif
1762#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001763}
1764
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001765void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1766 uint32_t startVertex,
1767 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1769
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001770 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1771
1772 // our setupGeometry better have adjusted this to zero.
1773 // DrawElements doesn't take an offset so we always adjus the startVertex.
1774 GrAssert(0 == startVertex);
1775
1776 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1777 // account for startVertex in the DrawElements case. So we always
1778 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001779 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001780#if SWAP_PER_DRAW
1781 glFlush();
1782 #if GR_MAC_BUILD
1783 aglSwapBuffers(aglGetCurrentContext());
1784 int set_a_break_pt_here = 9;
1785 aglSwapBuffers(aglGetCurrentContext());
1786 #elif GR_WIN32_BUILD
1787 SwapBuf();
1788 int set_a_break_pt_here = 9;
1789 SwapBuf();
1790 #endif
1791#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001792}
1793
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001794void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001795
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001796 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001797 GrAssert(kNone_MSFBO != fMSFBOType);
1798 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001799 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1800 rt->renderFBOID()));
1801 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1802 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001803 #if GR_COLLECT_STATS
1804 ++fStats.fRenderTargetChngCnt;
1805 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001806 // make sure we go through flushRenderTarget() since we've modified
1807 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001808 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001809 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001810 const GrIRect dirtyRect = rt->getResolveRect();
1811 GrGLIRect r;
1812 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1813 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001814
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001815 if (kAppleES_MSFBO == fMSFBOType) {
1816 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001817 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1818 GL_CALL(Scissor(r.fLeft, r.fBottom,
1819 r.fWidth, r.fHeight));
1820 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001821 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001822 fHWBounds.fScissorEnabled = true;
1823 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001824 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001825 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001826 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1827 flushScissor(NULL);
1828 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001829 int right = r.fLeft + r.fWidth;
1830 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001831 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1832 r.fLeft, r.fBottom, right, top,
1833 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001834 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001835 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001836 }
1837}
1838
twiz@google.com0f31ca72011-03-18 17:38:11 +00001839static const GrGLenum grToGLStencilFunc[] = {
1840 GR_GL_ALWAYS, // kAlways_StencilFunc
1841 GR_GL_NEVER, // kNever_StencilFunc
1842 GR_GL_GREATER, // kGreater_StencilFunc
1843 GR_GL_GEQUAL, // kGEqual_StencilFunc
1844 GR_GL_LESS, // kLess_StencilFunc
1845 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1846 GR_GL_EQUAL, // kEqual_StencilFunc,
1847 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001848};
1849GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1850GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1851GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1852GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1853GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1854GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1855GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1856GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1857GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1858
twiz@google.com0f31ca72011-03-18 17:38:11 +00001859static const GrGLenum grToGLStencilOp[] = {
1860 GR_GL_KEEP, // kKeep_StencilOp
1861 GR_GL_REPLACE, // kReplace_StencilOp
1862 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1863 GR_GL_INCR, // kIncClamp_StencilOp
1864 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1865 GR_GL_DECR, // kDecClamp_StencilOp
1866 GR_GL_ZERO, // kZero_StencilOp
1867 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001868};
1869GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1870GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1871GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1872GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1873GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1874GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1875GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1876GR_STATIC_ASSERT(6 == kZero_StencilOp);
1877GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1878
reed@google.comac10a2d2010-12-22 21:39:39 +00001879void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001880 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001881
1882 // use stencil for clipping if clipping is enabled and the clip
1883 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001884 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001885 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001886 bool stencilChange = fHWStencilClip != stencilClip ||
1887 fHWDrawState.fStencilSettings != *settings ||
1888 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1889 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001890
1891 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001892
bsalomon@google.comd302f142011-03-03 13:54:13 +00001893 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1894 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001895
bsalomon@google.comd302f142011-03-03 13:54:13 +00001896 if (settings->isDisabled()) {
1897 if (stencilClip) {
1898 settings = &gClipStencilSettings;
1899 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001900 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001901
1902 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001903 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001904 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001905 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001906 #if GR_DEBUG
1907 if (!fStencilWrapOpsSupport) {
1908 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1909 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1910 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1911 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1912 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1913 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1914 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1915 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1916 }
1917 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001918 int stencilBits = 0;
1919 GrStencilBuffer* stencilBuffer =
1920 fCurrDrawState.fRenderTarget->getStencilBuffer();
1921 if (NULL != stencilBuffer) {
1922 stencilBits = stencilBuffer->bits();
1923 }
1924 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001925 GrAssert(stencilBits ||
1926 (GrStencilSettings::gDisabled ==
1927 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001928 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1929 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930
1931 unsigned int frontRef = settings->fFrontFuncRef;
1932 unsigned int frontMask = settings->fFrontFuncMask;
1933 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001934 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001935
1936 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1937
1938 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1939 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1940 } else {
1941 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1942
1943 ConvertStencilFuncAndMask(settings->fFrontFunc,
1944 stencilClip,
1945 clipStencilMask,
1946 userStencilMask,
1947 &frontRef,
1948 &frontMask);
1949 frontWriteMask &= userStencilMask;
1950 }
1951 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001952 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001953 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001954 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001955 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001956 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001957 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001958 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001959 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001960 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001961
1962 unsigned int backRef = settings->fBackFuncRef;
1963 unsigned int backMask = settings->fBackFuncMask;
1964 unsigned int backWriteMask = settings->fBackWriteMask;
1965
1966
1967 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1968 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1969 backFunc = grToGLStencilFunc[settings->fBackFunc];
1970 } else {
1971 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1972 ConvertStencilFuncAndMask(settings->fBackFunc,
1973 stencilClip,
1974 clipStencilMask,
1975 userStencilMask,
1976 &backRef,
1977 &backMask);
1978 backWriteMask &= userStencilMask;
1979 }
1980
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001981 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1982 frontRef, frontMask));
1983 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1984 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1985 backRef, backMask));
1986 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1987 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1988 grToGLStencilOp[settings->fFrontFailOp],
1989 grToGLStencilOp[settings->fFrontPassOp],
1990 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001991
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001992 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1993 grToGLStencilOp[settings->fBackFailOp],
1994 grToGLStencilOp[settings->fBackPassOp],
1995 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001996 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001997 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1998 GL_CALL(StencilMask(frontWriteMask));
1999 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00002000 grToGLStencilOp[settings->fFrontPassOp],
2001 grToGLStencilOp[settings->fFrontPassOp]));
2002 }
2003 }
2004 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002005 fHWStencilClip = stencilClip;
2006 }
2007}
2008
bsalomon@google.com0650e812011-04-08 18:07:53 +00002009bool GrGpuGL::useSmoothLines() {
2010 // there is a conflict between using smooth lines and our use of
2011 // premultiplied alpha. Smooth lines tweak the incoming alpha value
2012 // but not in a premul-alpha way. So we only use them when our alpha
2013 // is 0xff.
2014
2015 // TODO: write a smarter line frag shader.
2016
2017 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
2018 canDisableBlend();
2019}
2020
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002021void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002022 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002023 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
2024 // smooth lines.
2025
2026 // we prefer smooth lines over multisampled lines
2027 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00002028 if (GrIsPrimTypeLines(type)) {
2029 bool smooth = useSmoothLines();
2030 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002031 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002032 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002033 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002034 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002035 fHWAAState.fSmoothLineEnabled = false;
2036 }
2037 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2038 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002039 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002040 fHWAAState.fMSAAEnabled = false;
2041 }
2042 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2043 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
2044 fHWAAState.fMSAAEnabled) {
2045 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002046 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002047 fHWAAState.fMSAAEnabled = false;
2048 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002049 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002050 fHWAAState.fMSAAEnabled = true;
2051 }
2052 }
2053 }
2054}
2055
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002056void GrGpuGL::flushBlend(GrPrimitiveType type,
2057 GrBlendCoeff srcCoeff,
2058 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002059 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
2060 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002061 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002062 fHWBlendDisabled = false;
2063 }
2064 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
2065 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002066 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2067 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002068 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
2069 fHWDrawState.fDstBlend = kISA_BlendCoeff;
2070 }
2071 } else {
2072 bool blendOff = canDisableBlend();
2073 if (fHWBlendDisabled != blendOff) {
2074 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002075 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002076 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002077 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002078 }
2079 fHWBlendDisabled = blendOff;
2080 }
2081 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002082 if (fHWDrawState.fSrcBlend != srcCoeff ||
2083 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002084 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2085 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002086 fHWDrawState.fSrcBlend = srcCoeff;
2087 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002088 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002089 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2090 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002091 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2092
2093 float c[] = {
2094 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2095 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2096 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2097 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2098 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002099 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002100 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2101 }
2102 }
2103 }
2104}
2105
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002106static unsigned grToGLFilter(GrSamplerState::Filter filter) {
2107 switch (filter) {
2108 case GrSamplerState::kBilinear_Filter:
2109 case GrSamplerState::k4x4Downsample_Filter:
2110 return GR_GL_LINEAR;
2111 case GrSamplerState::kNearest_Filter:
2112 case GrSamplerState::kConvolution_Filter:
2113 return GR_GL_NEAREST;
2114 default:
2115 GrAssert(!"Unknown filter type");
2116 return GR_GL_LINEAR;
2117 }
2118}
2119
bsalomon@google.comffca4002011-02-22 20:34:01 +00002120bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002121
2122 // GrGpu::setupClipAndFlushState should have already checked this
2123 // and bailed if not true.
2124 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002125
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002126 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002127 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002128 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002129 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002130
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002131 // true for now, but maybe not with GrEffect.
2132 GrAssert(NULL != nextTexture);
2133 // if we created a rt/tex and rendered to it without using a
2134 // texture and now we're texuring from the rt it will still be
2135 // the last bound texture, but it needs resolving. So keep this
2136 // out of the "last != next" check.
2137 GrGLRenderTarget* texRT =
2138 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2139 if (NULL != texRT) {
2140 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002141 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002142
2143 if (fHWDrawState.fTextures[s] != nextTexture) {
2144 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002145 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002146 #if GR_COLLECT_STATS
2147 ++fStats.fTextureChngCnt;
2148 #endif
2149 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2150 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002151 // The texture matrix has to compensate for texture width/height
2152 // and NPOT-embedded-in-POT
2153 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002154 }
2155
2156 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2157 const GrGLTexture::TexParams& oldTexParams =
2158 nextTexture->getTexParams();
2159 GrGLTexture::TexParams newTexParams;
2160
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002161 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002162
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002163 const GrGLenum* wraps =
2164 GrGLTexture::WrapMode2GLWrap(this->glBinding());
2165 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2166 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002167
2168 if (newTexParams.fFilter != oldTexParams.fFilter) {
2169 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002170 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2171 GR_GL_TEXTURE_MAG_FILTER,
2172 newTexParams.fFilter));
2173 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2174 GR_GL_TEXTURE_MIN_FILTER,
2175 newTexParams.fFilter));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002176 }
2177 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2178 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002179 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2180 GR_GL_TEXTURE_WRAP_S,
2181 newTexParams.fWrapS));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002182 }
2183 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2184 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002185 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2186 GR_GL_TEXTURE_WRAP_T,
2187 newTexParams.fWrapT));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002188 }
2189 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002190 }
2191 }
2192
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002193 GrIRect* rect = NULL;
2194 GrIRect clipBounds;
2195 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2196 fClip.hasConservativeBounds()) {
2197 fClip.getConservativeBounds().roundOut(&clipBounds);
2198 rect = &clipBounds;
2199 }
2200 this->flushRenderTarget(rect);
2201 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002202
reed@google.comac10a2d2010-12-22 21:39:39 +00002203 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2204 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2205 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002206 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002207 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002208 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002209 }
2210 }
2211
bsalomon@google.comd302f142011-03-03 13:54:13 +00002212 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2213 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002214 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002215 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002216 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002218 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002219 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002220 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002221 }
2222
bsalomon@google.comd302f142011-03-03 13:54:13 +00002223 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2224 switch (fCurrDrawState.fDrawFace) {
2225 case kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002226 GL_CALL(Enable(GR_GL_CULL_FACE));
2227 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002228 break;
2229 case kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002230 GL_CALL(Enable(GR_GL_CULL_FACE));
2231 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002232 break;
2233 case kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002234 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002235 break;
2236 default:
2237 GrCrash("Unknown draw face.");
2238 }
2239 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2240 }
2241
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002242#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002243 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002244 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002245 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002246 NULL == fCurrDrawState.fRenderTarget ||
2247 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002248 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002249 fCurrDrawState.fRenderTarget);
2250 }
2251#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002252
reed@google.comac10a2d2010-12-22 21:39:39 +00002253 flushStencil();
2254
bsalomon@google.comd302f142011-03-03 13:54:13 +00002255 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002256 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002257 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002258}
2259
2260void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002261 if (fHWGeometryState.fVertexBuffer != buffer) {
2262 fHWGeometryState.fArrayPtrsDirty = true;
2263 fHWGeometryState.fVertexBuffer = buffer;
2264 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002265}
2266
2267void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002268 if (fHWGeometryState.fVertexBuffer == buffer) {
2269 // deleting bound buffer does implied bind to 0
2270 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002271 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002272 }
2273}
2274
2275void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002276 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002277}
2278
2279void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 if (fHWGeometryState.fIndexBuffer == buffer) {
2281 // deleting bound buffer does implied bind to 0
2282 fHWGeometryState.fIndexBuffer = NULL;
2283 }
2284}
2285
reed@google.comac10a2d2010-12-22 21:39:39 +00002286void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2287 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002289 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 }
2291 if (fHWDrawState.fRenderTarget == renderTarget) {
2292 fHWDrawState.fRenderTarget = NULL;
2293 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002294}
2295
2296void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002297 for (int s = 0; s < kNumStages; ++s) {
2298 if (fCurrDrawState.fTextures[s] == texture) {
2299 fCurrDrawState.fTextures[s] = NULL;
2300 }
2301 if (fHWDrawState.fTextures[s] == texture) {
2302 // deleting bound texture does implied bind to 0
2303 fHWDrawState.fTextures[s] = NULL;
2304 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002305 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002306}
2307
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002308bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002309 GrGLenum* internalFormat,
2310 GrGLenum* format,
2311 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002312 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002313 case kRGBA_8888_GrPixelConfig:
2314 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002315 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002316 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002317 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2318 // format for a BGRA is BGRA not RGBA (as on desktop)
2319 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2320 } else {
2321 *internalFormat = GR_GL_RGBA;
2322 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002323 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002324 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002325 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002326 *format = GR_GL_RGB;
2327 *internalFormat = GR_GL_RGB;
2328 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002329 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002330 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002331 *format = GR_GL_RGBA;
2332 *internalFormat = GR_GL_RGBA;
2333 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002334 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002335 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002336 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002337 *format = GR_GL_PALETTE8_RGBA8;
2338 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002339 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002340 } else {
2341 return false;
2342 }
2343 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002344 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002345 *format = GR_GL_ALPHA;
2346 *internalFormat = GR_GL_ALPHA;
2347 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002348 break;
2349 default:
2350 return false;
2351 }
2352 return true;
2353}
2354
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002355void GrGpuGL::setTextureUnit(int unit) {
2356 GrAssert(unit >= 0 && unit < kNumStages);
2357 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002358 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002359 fActiveTextureUnitIdx = unit;
2360 }
2361}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002362
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002363void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002364 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002365 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002366 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2367 }
2368}
2369
reed@google.comac10a2d2010-12-22 21:39:39 +00002370/* On ES the internalFormat and format must match for TexImage and we use
2371 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2372 decide the internalFormat. However, on ES internalFormat for
2373 RenderBufferStorage* has to be a specific format (not a base format like
2374 GL_RGBA).
2375 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002376bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002377 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002378 case kRGBA_8888_GrPixelConfig:
2379 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002380 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002381 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002382 return true;
2383 } else {
2384 return false;
2385 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002386 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002387 // ES2 supports 565. ES1 supports it
2388 // with FBO extension desktop GL has
2389 // no such internal format
2390 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002391 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002392 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002393 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002394 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002395 return true;
2396 default:
2397 return false;
2398 }
2399}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002400
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002401void GrGpuGL::resetDirtyFlags() {
2402 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2403}
2404
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002405void GrGpuGL::setBuffers(bool indexed,
2406 int* extraVertexOffset,
2407 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002408
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002409 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002410
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002411 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2412
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002413 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002414 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002415 case kBuffer_GeometrySrcType:
2416 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002417 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002418 break;
2419 case kArray_GeometrySrcType:
2420 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002421 this->finalizeReservedVertices();
2422 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2423 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002424 break;
2425 default:
2426 vbuf = NULL; // suppress warning
2427 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002428 }
2429
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002430 GrAssert(NULL != vbuf);
2431 GrAssert(!vbuf->isLocked());
2432 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002433 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434 fHWGeometryState.fArrayPtrsDirty = true;
2435 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002436 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002437
2438 if (indexed) {
2439 GrAssert(NULL != extraIndexOffset);
2440
2441 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002442 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443 case kBuffer_GeometrySrcType:
2444 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002445 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002446 break;
2447 case kArray_GeometrySrcType:
2448 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002449 this->finalizeReservedIndices();
2450 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2451 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002452 break;
2453 default:
2454 ibuf = NULL; // suppress warning
2455 GrCrash("Unknown geometry src type!");
2456 }
2457
2458 GrAssert(NULL != ibuf);
2459 GrAssert(!ibuf->isLocked());
2460 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002461 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462 fHWGeometryState.fIndexBuffer = ibuf;
2463 }
2464 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002465}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002466
2467int GrGpuGL::getMaxEdges() const {
2468 // FIXME: This is a pessimistic estimate based on how many other things
2469 // want to add uniforms. This should be centralized somewhere.
2470 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2471}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002472