blob: 7916a9175471cc927dabf99409e424574a3a4c90 [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.com316f99232011-01-13 21:28:12 +000018// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000019// mucking with the state of any of the stages.
bsalomon@google.com316f99232011-01-13 21:28:12 +000020static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022#define SKIP_CACHE_CHECK true
23
twiz@google.com0f31ca72011-03-18 17:38:11 +000024static const GrGLenum gXfermodeCoeff2Blend[] = {
25 GR_GL_ZERO,
26 GR_GL_ONE,
27 GR_GL_SRC_COLOR,
28 GR_GL_ONE_MINUS_SRC_COLOR,
29 GR_GL_DST_COLOR,
30 GR_GL_ONE_MINUS_DST_COLOR,
31 GR_GL_SRC_ALPHA,
32 GR_GL_ONE_MINUS_SRC_ALPHA,
33 GR_GL_DST_ALPHA,
34 GR_GL_ONE_MINUS_DST_ALPHA,
35 GR_GL_CONSTANT_COLOR,
36 GR_GL_ONE_MINUS_CONSTANT_COLOR,
37 GR_GL_CONSTANT_ALPHA,
38 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000039
40 // extended blend coeffs
41 GR_GL_SRC1_COLOR,
42 GR_GL_ONE_MINUS_SRC1_COLOR,
43 GR_GL_SRC1_ALPHA,
44 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000045};
46
bsalomon@google.com271cffc2011-05-20 14:13:56 +000047bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000048 static const bool gCoeffReferencesBlendConst[] = {
49 false,
50 false,
51 false,
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 true,
60 true,
61 true,
62 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063
64 // extended blend coeffs
65 false,
66 false,
67 false,
68 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000069 };
70 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000071 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
72
73 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
74 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
75 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
76 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
77 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
78 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
79 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
80 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
81 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
82 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
83 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
84 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
85 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
86 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
87
88 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
89 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
90 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
91 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
92
93 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
94 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000095}
96
reed@google.comac10a2d2010-12-22 21:39:39 +000097///////////////////////////////////////////////////////////////////////////////
98
bsalomon@google.comd302f142011-03-03 13:54:13 +000099void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
100 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000101 GrMatrix* matrix) {
102 GrAssert(NULL != texture);
103 GrAssert(NULL != matrix);
104 if (GR_Scalar1 != texture->contentScaleX() ||
105 GR_Scalar1 != texture->contentScaleY()) {
106 if (GrSamplerState::kRadial_SampleMode == mode) {
107 GrMatrix scale;
108 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
109 matrix->postConcat(scale);
110 } else if (GrSamplerState::kNormal_SampleMode == mode) {
111 GrMatrix scale;
112 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
113 matrix->postConcat(scale);
114 } else {
115 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
116 }
117 }
118 GrGLTexture::Orientation orientation = texture->orientation();
119 if (GrGLTexture::kBottomUp_Orientation == orientation) {
120 GrMatrix invY;
121 invY.setAll(GR_Scalar1, 0, 0,
122 0, -GR_Scalar1, GR_Scalar1,
123 0, 0, GrMatrix::I()[8]);
124 matrix->postConcat(invY);
125 } else {
126 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
127 }
128}
129
bsalomon@google.comd302f142011-03-03 13:54:13 +0000130bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000131 const GrSamplerState& sampler) {
132 GrAssert(NULL != texture);
133 if (!sampler.getMatrix().isIdentity()) {
134 return false;
135 }
136 if (GR_Scalar1 != texture->contentScaleX() ||
137 GR_Scalar1 != texture->contentScaleY()) {
138 return false;
139 }
140 GrGLTexture::Orientation orientation = texture->orientation();
141 if (GrGLTexture::kBottomUp_Orientation == orientation) {
142 return false;
143 } else {
144 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
145 }
146 return true;
147}
148
149///////////////////////////////////////////////////////////////////////////////
150
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000151static bool gPrintStartupSpew;
152
twiz@google.com59a190b2011-03-14 21:23:01 +0000153static bool fbo_test(int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000154
twiz@google.com0f31ca72011-03-18 17:38:11 +0000155 GrGLint savedFBO;
156 GrGLint savedTexUnit;
157 GR_GL_GetIntegerv(GR_GL_ACTIVE_TEXTURE, &savedTexUnit);
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000158 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &savedFBO);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000159
twiz@google.com0f31ca72011-03-18 17:38:11 +0000160 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000161
twiz@google.com0f31ca72011-03-18 17:38:11 +0000162 GrGLuint testFBO;
twiz@google.com59a190b2011-03-14 21:23:01 +0000163 GR_GL(GenFramebuffers(1, &testFBO));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000165 GrGLuint testRTTex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 GR_GL(GenTextures(1, &testRTTex));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000167 GR_GL(BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000168 // some implementations require texture to be mip-map complete before
169 // FBO with level 0 bound as color attachment will be framebuffer complete.
twiz@google.com0f31ca72011-03-18 17:38:11 +0000170 GR_GL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
171 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
172 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
173 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000174 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000175 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000176 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000177 GR_GL(DeleteFramebuffers(1, &testFBO));
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 GR_GL(DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000179
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000180 GR_GL(ActiveTexture(savedTexUnit));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000181 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, savedFBO));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000182
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000183 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000184}
185
tomhudson@google.com747bf292011-06-14 18:16:52 +0000186static bool probe_for_npot_render_target_support(bool hasNPOTTextureSupport) {
187
188 /* Experimentation has found that some GLs that support NPOT textures
189 do not support FBOs with a NPOT texture. They report "unsupported" FBO
190 status. I don't know how to explicitly query for this. Do an
191 experiment. Note they may support NPOT with a renderbuffer but not a
192 texture. Presumably, the implementation bloats the renderbuffer
193 internally to the next POT.
194 */
195 if (hasNPOTTextureSupport) {
196 return fbo_test(200, 200);
197 }
198 return false;
199}
200
201static int probe_for_min_render_target_height(bool hasNPOTRenderTargetSupport,
202 int maxRenderTargetSize) {
203 /* The iPhone 4 has a restriction that for an FBO with texture color
204 attachment with height <= 8 then the width must be <= height. Here
205 we look for such a limitation.
206 */
207 if (gPrintStartupSpew) {
208 GrPrintf("Small height FBO texture experiments\n");
209 }
210 int minRenderTargetHeight = GR_INVAL_GLINT;
211 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
212 GrGLuint w = maxRenderTargetSize;
213 GrGLuint h = i;
214 if (fbo_test(w, h)) {
215 if (gPrintStartupSpew) {
216 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
217 }
218 minRenderTargetHeight = i;
219 break;
220 } else {
221 if (gPrintStartupSpew) {
222 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
223 }
224 }
225 }
226 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
227
228 return minRenderTargetHeight;
229}
230
231static int probe_for_min_render_target_width(bool hasNPOTRenderTargetSupport,
232 int maxRenderTargetSize) {
233
234 if (gPrintStartupSpew) {
235 GrPrintf("Small width FBO texture experiments\n");
236 }
237 int minRenderTargetWidth = GR_INVAL_GLINT;
238 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
239 GrGLuint w = i;
240 GrGLuint h = maxRenderTargetSize;
241 if (fbo_test(w, h)) {
242 if (gPrintStartupSpew) {
243 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
244 }
245 minRenderTargetWidth = i;
246 break;
247 } else {
248 if (gPrintStartupSpew) {
249 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
250 }
251 }
252 }
253 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
254
255 return minRenderTargetWidth;
256}
257
258
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000259GrGpuGL::GrGpuGL()
260 : fStencilFormats(8) {
261
262 GrGLClearErr();
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000263
reed@google.comeeeb5a02010-12-23 15:12:59 +0000264 if (gPrintStartupSpew) {
265 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
266 this);
twiz@google.com59a190b2011-03-14 21:23:01 +0000267 GrPrintf("------ VENDOR %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000268 GrGLGetGLInterface()->fGetString(GR_GL_VENDOR));
twiz@google.com59a190b2011-03-14 21:23:01 +0000269 GrPrintf("------ RENDERER %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000270 GrGLGetGLInterface()->fGetString(GR_GL_RENDERER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000271 GrPrintf("------ VERSION %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000272 GrGLGetGLInterface()->fGetString(GR_GL_VERSION));
twiz@google.com59a190b2011-03-14 21:23:01 +0000273 GrPrintf("------ EXTENSIONS\n %s \n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000274 GrGLGetGLInterface()->fGetString(GR_GL_EXTENSIONS));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000275 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000277 fGLVersion = gl_version_as_float();
278 fExtensionString = (const char*) GR_GL(GetString(GR_GL_EXTENSIONS));
reed@google.comac10a2d2010-12-22 21:39:39 +0000279
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000280 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000281
twiz@google.com0f31ca72011-03-18 17:38:11 +0000282 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000283 // check FS and fixed-function texture unit limits
284 // we only use textures in the fragment stage currently.
285 // checks are > to make sure we have a spare unit.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000286 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES2) {
287 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
288 GrAssert(maxTextureUnits > kNumStages);
289 }
290 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES1) {
291 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
292 GrAssert(maxTextureUnits > kNumStages);
293 }
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000294 if (GR_GL_SUPPORT_ES2) {
295 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
296 &fMaxFragmentUniformVectors);
297 } else if (GR_GL_SUPPORT_DESKTOP) {
298 GrGLint max;
299 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
300 fMaxFragmentUniformVectors = max / 4;
301 } else {
302 fMaxFragmentUniformVectors = 16;
303 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000304
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 ////////////////////////////////////////////////////////////////////////////
306 // Check for supported features.
307
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000308 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
twiz@google.com0f31ca72011-03-18 17:38:11 +0000310 GrGLint numFormats;
311 GR_GL_GetIntegerv(GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000312 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
twiz@google.com0f31ca72011-03-18 17:38:11 +0000313 GR_GL_GetIntegerv(GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000315 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000316 f8bitPaletteSupport = true;
317 break;
318 }
319 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000320
321 if (gPrintStartupSpew) {
322 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
323 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000324
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000325 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
326 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
327 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
328 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000329
330 memset(fAASamples, 0, sizeof(fAASamples));
331 fMSFBOType = kNone_MSFBO;
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000332 if (GR_GL_SUPPORT_ES) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000333 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000334 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000335 // and fbo_blit extensions.
336 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000337 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000338 fMSFBOType = kAppleES_MSFBO;
339 }
340 } else {
341 GrAssert(GR_GL_SUPPORT_DESKTOP);
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000342 if ((fGLVersion >= 3.f) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000343 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000344 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
345 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000346 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000347 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000349 if (gPrintStartupSpew) {
350 switch (fMSFBOType) {
351 case kNone_MSFBO:
352 GrPrintf("MSAA Support: NONE\n");
353 break;
354 case kDesktopARB_MSFBO:
355 GrPrintf("MSAA Support: DESKTOP ARB.\n");
356 break;
357 case kDesktopEXT_MSFBO:
358 GrPrintf("MSAA Support: DESKTOP EXT.\n");
359 break;
360 case kAppleES_MSFBO:
361 GrPrintf("MSAA Support: APPLE ES.\n");
362 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000363 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 }
365
366 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000367 GrGLint maxSamples;
bsalomon@google.comd1e433532011-03-21 21:38:40 +0000368 GR_GL_GetIntegerv(GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000370 fAASamples[kNone_GrAALevel] = 0;
371 fAASamples[kLow_GrAALevel] = GrMax(2,
372 GrFixedFloorToInt((GR_FixedHalf) *
373 maxSamples));
374 fAASamples[kMed_GrAALevel] = GrMax(2,
375 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
376 maxSamples));
377 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000379 if (gPrintStartupSpew) {
380 GrPrintf("\tMax Samples: %d\n", maxSamples);
381 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000382 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000383 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000384
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000385 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000386 fHasStencilWrap = (fGLVersion >= 1.4f) ||
387 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000388 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000389 fHasStencilWrap = (fGLVersion >= 2.0f) || this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000390 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000391 if (gPrintStartupSpew) {
392 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
393 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000395 if (GR_GL_SUPPORT_DESKTOP) {
396 // we could also look for GL_ATI_separate_stencil extension or
397 // GL_EXT_stencil_two_side but they use different function signatures
398 // than GL2.0+ (and than each other).
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000399 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000400 // supported on GL 1.4 and higher or by extension
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000401 fStencilWrapOpsSupport = (fGLVersion >= 1.4f) ||
402 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000403 } else {
404 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
405 // an ES1 extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000406 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000407 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000408 fStencilWrapOpsSupport = (fGLVersion >= 2.f) ||
409 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000410 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000411 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000412 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
413 (fTwoSidedStencilSupport ? "YES" : "NO"),
414 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000415 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000416
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000417 if (GR_GL_SUPPORT_DESKTOP) {
418 fRGBA8Renderbuffer = true;
419 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000420 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000421 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000422 if (gPrintStartupSpew) {
423 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
424 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000425
426
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000427 if (GR_GL_SUPPORT_ES) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000428 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000429 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000430 }
431 }
432
433 if (GR_GL_SUPPORT_DESKTOP) {
434 fBufferLockSupport = true; // we require VBO support and the desktop VBO
435 // extension includes glMapBuffer.
436 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000437 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000438 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000439
reed@google.comeeeb5a02010-12-23 15:12:59 +0000440 if (gPrintStartupSpew) {
441 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
442 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000443
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000444 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000445 if (fGLVersion >= 2.f ||
446 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000447 fNPOTTextureTileSupport = true;
448 fNPOTTextureSupport = true;
449 } else {
450 fNPOTTextureTileSupport = false;
451 fNPOTTextureSupport = false;
452 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000453 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000454 if (fGLVersion >= 2.f) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000455 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000456 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000457 } else {
458 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000459 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000460 fNPOTTextureTileSupport = false;
461 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000462 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000463
bsalomon@google.com205d4602011-04-25 12:43:45 +0000464 fAALineSupport = GR_GL_SUPPORT_DESKTOP;
465
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000467 // Experiments to determine limitations that can't be queried.
468 // TODO: Make these a preprocess that generate some compile time constants.
469 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000471 int expectNPOTTargets = GrGLGetGLInterface()->fNPOTRenderTargetSupport;
472 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000473 fNPOTRenderTargetSupport =
474 probe_for_npot_render_target_support(fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000475 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000476 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
477 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000478 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000479
bsalomon@google.com0748f212011-02-01 22:56:16 +0000480 if (gPrintStartupSpew) {
481 if (fNPOTTextureSupport) {
482 GrPrintf("NPOT textures supported\n");
483 if (fNPOTTextureTileSupport) {
484 GrPrintf("NPOT texture tiling supported\n");
485 } else {
486 GrPrintf("NPOT texture tiling NOT supported\n");
487 }
488 if (fNPOTRenderTargetSupport) {
489 GrPrintf("NPOT render targets supported\n");
490 } else {
491 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000492 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000494 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000495 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 }
497
bsalomon@google.com91958362011-06-13 17:58:13 +0000498 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
499 GR_GL_GetIntegerv(GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000500 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000501 // attachment, hence this min:
502 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000503
tomhudson@google.com747bf292011-06-14 18:16:52 +0000504 fMinRenderTargetHeight = GrGLGetGLInterface()->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000505 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000506 fMinRenderTargetHeight =
507 probe_for_min_render_target_height(fNPOTRenderTargetSupport,
508 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000509 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000510
tomhudson@google.com747bf292011-06-14 18:16:52 +0000511 fMinRenderTargetWidth = GrGLGetGLInterface()->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000512 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000513 fMinRenderTargetWidth =
514 probe_for_min_render_target_width(fNPOTRenderTargetSupport,
515 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000516 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000517
bsalomon@google.comfe676522011-06-17 18:12:21 +0000518 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000519
520 fStencilClearFBO = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000521}
522
523GrGpuGL::~GrGpuGL() {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000524 if (fStencilClearFBO) {
525 GR_GL(DeleteFramebuffers(1, &fStencilClearFBO));
526 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000527}
528
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000529void GrGpuGL::resetContext() {
530 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 fHWBlendDisabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000532 GR_GL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000533
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000534 // we don't use the zb at all
twiz@google.com0f31ca72011-03-18 17:38:11 +0000535 GR_GL(Disable(GR_GL_DEPTH_TEST));
536 GR_GL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000537
twiz@google.com0f31ca72011-03-18 17:38:11 +0000538 GR_GL(Disable(GR_GL_CULL_FACE));
539 GR_GL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000540 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000541
twiz@google.com0f31ca72011-03-18 17:38:11 +0000542 GR_GL(Disable(GR_GL_DITHER));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000543 if (GR_GL_SUPPORT_DESKTOP) {
544 GR_GL(Disable(GR_GL_LINE_SMOOTH));
545 GR_GL(Disable(GR_GL_POINT_SMOOTH));
546 GR_GL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000547 fHWAAState.fMSAAEnabled = false;
548 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000549 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000550
twiz@google.com0f31ca72011-03-18 17:38:11 +0000551 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000552 fHWDrawState.fFlagBits = 0;
553
reed@google.comac10a2d2010-12-22 21:39:39 +0000554 // we only ever use lines in hairline mode
555 GR_GL(LineWidth(1));
556
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000557 // invalid
558 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000559
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000561 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
562 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000563
564 fHWDrawState.fBlendConstant = 0x00000000;
565 GR_GL(BlendColor(0,0,0,0));
566
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000568
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000569 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000570
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000571 for (int s = 0; s < kNumStages; ++s) {
572 fHWDrawState.fTextures[s] = NULL;
573 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
574 -GR_ScalarMax,
575 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000576 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000577 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000578 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000579
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000580 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 fHWBounds.fScissorEnabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000582 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000583 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000584
bsalomon@google.comd302f142011-03-03 13:54:13 +0000585 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000586 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000587 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
589 fHWGeometryState.fIndexBuffer = NULL;
590 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000591
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000592 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000593
twiz@google.com0f31ca72011-03-18 17:38:11 +0000594 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000595 fHWDrawState.fRenderTarget = NULL;
596}
597
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000598void GrGpuGL::abandonResources() {
599 INHERITED::abandonResources();
600
601 fStencilClearFBO = 0;
602}
603
604void GrGpuGL::releaseResources() {
605 INHERITED::releaseResources();
606
607 if (fStencilClearFBO) {
608 GR_GL(DeleteFramebuffers(1, &fStencilClearFBO));
609 fStencilClearFBO = 0;
610 }
611}
612
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000613GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
614
615 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
616 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
617 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
618 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
619
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000620 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000621 GrGLStencilBuffer* sb = NULL;
622
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000623 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000624 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
625#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
626 if (desc.fSampleCnt) {
627#else
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000628 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000629#endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000630 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000631 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000632 } else {
633 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000634 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000635 }
636 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000637 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000638 }
639 // we don't know what the RB ids are without glGets and we don't care
640 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000641 rtDesc.fMSColorRenderbufferID = 0;
642#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
643 rtDesc.fSampleCnt = desc.fSampleCnt;
644#else
645 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
646 // just guess, this code path is only compiled in WK and we aren't
647 // using MSAA anyway. This will be stripped out soon when WK sets
648 // the fSampleCnt in GrPlatformSurfaceDesc.
649 rtDesc.fSampleCnt = 4;
650 } else {
651 rtDesc.fSampleCnt = 0;
652 }
653#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000654 if (desc.fStencilBits) {
655 GrGLStencilBuffer::Format format;
656 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
657 format.fPacked = false;
658 format.fStencilBits = desc.fStencilBits;
659 format.fTotalBits = desc.fStencilBits;
660 sb = new GrGLStencilBuffer(this, 0, desc.fWidth,
661 desc.fHeight, format);
662 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000663 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000664 }
665
666 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000667 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000668 GrGLenum dontCare;
669 if (!canBeTexture(desc.fConfig, &dontCare,
670 &texDesc.fUploadFormat,
671 &texDesc.fUploadType)) {
672 return NULL;
673 }
674
675 GrGLTexture::TexParams params;
676
677 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
678 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
679
bsalomon@google.com90405932011-06-17 15:56:55 +0000680 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000681 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000682 texDesc.fTextureID = desc.fPlatformTexture;
683 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
684 texDesc.fOwnsID = false;
685
686 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000687 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000688 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
689 tex->asRenderTarget()->setStencilBuffer(sb);
690 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000691 } else {
692 return new GrGLTexture(this, texDesc, params);
693 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000694 } else {
695 GrGLIRect viewport;
696 viewport.fLeft = 0;
697 viewport.fBottom = 0;
698 viewport.fWidth = desc.fWidth;
699 viewport.fHeight = desc.fHeight;
700
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000701 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
702 rt->setStencilBuffer(sb);
703 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000704 }
705}
706
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000707namespace {
708
709static const GrGLenum kUnknownGLFormat = ~0;
710
711GrGLenum get_fbo_color_format() {
712 GrGLint cbType;
713 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
714 GR_GL_COLOR_ATTACHMENT0,
715 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
716 &cbType);
717 GrGLint cbID;
718 GrGLint cbFormat;
719 switch (cbType) {
720 case GR_GL_RENDERBUFFER:
721 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
722 GR_GL_COLOR_ATTACHMENT0,
723 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
724 &cbID);
725 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
726 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
727 GR_GL_RENDERBUFFER_INTERNAL_FORMAT,
728 &cbFormat);
729 return cbFormat;
730 break;
731 case GR_GL_TEXTURE:
732 // ES doesn't have glGetTexLevelParameter
733 if (GR_GL_SUPPORT_DESKTOP) {
734 GrGLint cbLevel;
735 GrGLint cbFace;
736 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
737 GR_GL_COLOR_ATTACHMENT0,
738 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
739 &cbID);
740 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
741 GR_GL_COLOR_ATTACHMENT0,
742 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
743 &cbLevel);
744 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
745 GR_GL_COLOR_ATTACHMENT0,
746 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
747 &cbFace);
748 GrGLenum bind;
749 GrGLenum target;
750 if (cbFace) {
751 bind = GR_GL_TEXTURE_CUBE_MAP;
752 target = cbFace;
753 } else {
754 bind = GR_GL_TEXTURE_2D;
755 target = GR_GL_TEXTURE_2D;
756 }
757 GR_GL(BindTexture(bind, cbID));
758 GR_GL_GetTexLevelParameteriv(target, cbLevel,
759 GR_GL_TEXTURE_INTERNAL_FORMAT, &cbFormat);
760 return cbFormat;
761 } else {
762 return kUnknownGLFormat;
763 }
764 break;
765 default:
766 // we can get here with FBO 0, not a render buffer or a texture
767 return kUnknownGLFormat;
768 }
769}
770
771GrPixelConfig internal_color_format_to_config(GrGLenum iFormat) {
772 switch (iFormat) {
773 case GR_GL_RGB565:
774 return kRGB_565_GrPixelConfig;
775 case GR_GL_RGBA4:
776 return kRGBA_4444_GrPixelConfig;
777 case GR_GL_RGBA8:
778 case GR_GL_SRGB8_ALPHA8:
779 case GR_GL_SRGB_ALPHA:
780 case GR_GL_RGBA:
781 case GR_GL_BGRA:
782 return kRGBA_8888_GrPixelConfig;
783 case GR_GL_RGB8:
784 case GR_GL_SRGB8:
785 case GR_GL_SRGB:
786 return kRGBX_8888_GrPixelConfig;
787 default:
788 // there are many GL formats we don't have enums
789 // for. We should still render to them if the client
790 // asks us.
791 return kUnknown_GrPixelConfig;
792 }
793}
794
795GrPixelConfig get_implied_color_config(bool arbFBOExtension) {
796 GrGLint rSize, bSize, gSize, aSize;
797 if (arbFBOExtension) {
798 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
799 GR_GL_COLOR_ATTACHMENT0,
800 GR_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &rSize);
801 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
802 GR_GL_COLOR_ATTACHMENT0,
803 GR_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &gSize);
804 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
805 GR_GL_COLOR_ATTACHMENT0,
806 GR_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &bSize);
807 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
808 GR_GL_COLOR_ATTACHMENT0,
809 GR_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &aSize);
810 } else {
811 GR_GL_GetIntegerv(GR_GL_RED_BITS, &rSize);
812 GR_GL_GetIntegerv(GR_GL_GREEN_BITS, &gSize);
813 GR_GL_GetIntegerv(GR_GL_BLUE_BITS, &bSize);
814 GR_GL_GetIntegerv(GR_GL_ALPHA_BITS, &aSize);
815 }
816
817 if(8 == rSize && 8 == gSize && 8 == bSize) {
818 if (0 == aSize) {
819 return kRGBX_8888_GrPixelConfig;
820 } else if (8 == aSize) {
821 return kRGBA_8888_GrPixelConfig;
822 }
823 } else if (4 == rSize && 4 == gSize && 4 == bSize && 4 == aSize) {
824 return kRGBA_4444_GrPixelConfig;
825 } else if (5 == rSize && 6 == gSize && 5 == bSize && 0 == aSize) {
826 return kRGB_565_GrPixelConfig;
827 }
828 return kUnknown_GrPixelConfig;
829}
830
831int get_fbo_stencil_bits(bool arbFBOExtension) {
832 GrGLint stencilBits;
833 if (arbFBOExtension) {
834 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
835 GR_GL_STENCIL_ATTACHMENT,
836 GR_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
837 &stencilBits);
838 } else {
839 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &stencilBits);
840 }
841 return stencilBits;
842}
843}
844
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000845GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() {
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000846
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000847 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000848
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000849 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, (GrGLint*)&rtDesc.fRTFBOID);
850 rtDesc.fTexFBOID = rtDesc.fRTFBOID;
851 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000852
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000853 bool arbFBO = (GR_GL_SUPPORT_DESKTOP && (fGLVersion > 3.0 ||
854 this->hasExtension("GL_ARB_framebuffer_object")));
855
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000856 GrGLIRect viewport;
857 viewport.setFromGLViewport();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000858 int stencilBits = get_fbo_stencil_bits(arbFBO);
859
860 GrGLStencilBuffer* sb = NULL;
861 if (stencilBits) {
862 GrGLStencilBuffer::Format format;
863 // we could query this but we don't really need it
864 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
865 format.fPacked = false;
866 format.fStencilBits = stencilBits;
867 format.fTotalBits = stencilBits;
868 sb = new GrGLStencilBuffer(this, 0, viewport.fWidth,
869 viewport.fHeight, format);
870 }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000871
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000872 GR_GL_GetIntegerv(GR_GL_SAMPLES, &rtDesc.fSampleCnt);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000873 GrGLenum fmat = get_fbo_color_format();
874 if (kUnknownGLFormat == fmat) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000875 rtDesc.fConfig = get_implied_color_config(arbFBO);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000876 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000877 rtDesc.fConfig = internal_color_format_to_config(fmat);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000878 }
879
880 // may have to bind a texture to gets its format
881 this->setSpareTextureUnit();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000882
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000883 rtDesc.fOwnIDs = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000884
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000885 GrGLRenderTarget* target = new GrGLRenderTarget(this, rtDesc, viewport);
886 target->setStencilBuffer(sb);
887 return target;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000888}
889
bsalomon@google.com5782d712011-01-21 21:03:59 +0000890///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000891static const GrGLuint kUnknownBitCount = ~0;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000892
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000893void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000894
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000895 // Build up list of legal stencil formats (though perhaps not supported on
896 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000897
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000898 // these consts are in order of most preferred to least preferred
899 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000900 static const GrGLStencilBuffer::Format
901 // internal Format stencil bits total bits packed?
902 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
903 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
904 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
905 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
906 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
907 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000908
909 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000910 bool supportsPackedDS = fGLVersion >= 3.0f ||
911 this->hasExtension("GL_EXT_packed_depth_stencil") ||
912 this->hasExtension("GL_ARB_framebuffer_object");
913
914 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
915 // require FBO support we can expect these are legal formats and don't
916 // check. These also all support the unsized GL_STENCIL_INDEX.
917 fStencilFormats.push_back() = gS8;
918 fStencilFormats.push_back() = gS16;
919 if (supportsPackedDS) {
920 fStencilFormats.push_back() = gD24S8;
921 }
922 fStencilFormats.push_back() = gS4;
923 if (supportsPackedDS) {
924 fStencilFormats.push_back() = gDS;
925 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000926 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000927 // ES2 has STENCIL_INDEX8 without extensions.
928 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
929 // introduces tokens for S1 thu S8 but there are separate extensions
930 // that make them legal (GL_OES_stencil1, ...).
931 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
932 // ES doesn't support using the unsized formats.
933
934 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
935 fStencilFormats.push_back() = gS8;
936 }
937 //fStencilFormats.push_back() = gS16;
938 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
939 fStencilFormats.push_back() = gD24S8;
940 }
941 if (this->hasExtension("GL_OES_stencil4")) {
942 fStencilFormats.push_back() = gS4;
943 }
944 // we require some stencil format.
945 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000946 }
947}
948
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000949////////////////////////////////////////////////////////////////////////////////
950
951void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
952 GrGLenum internalFormat,
953 const void* data,
954 size_t rowBytes) {
955 // we assume the texture is bound;
956 if (!rowBytes) {
957 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
958 }
959
960 // in case we need a temporary, trimmed copy of the src pixels
961 SkAutoSMalloc<128 * 128> tempStorage;
962
963 /*
964 * check whether to allocate a temporary buffer for flipping y or
965 * because our data has extra bytes past each row. If so, we need
966 * to trim those off here, since GL ES doesn't let us specify
967 * GL_UNPACK_ROW_LENGTH.
968 */
969 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
970 if (GR_GL_SUPPORT_DESKTOP && !flipY) {
971 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
972 GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
973 rowBytes / desc.fUploadByteCount));
974 }
975 } else {
976 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
977 if (data && (trimRowBytes < rowBytes || flipY)) {
978 // copy the data into our new storage, skipping the trailing bytes
979 size_t trimSize = desc.fContentHeight * trimRowBytes;
980 const char* src = (const char*)data;
981 if (flipY) {
982 src += (desc.fContentHeight - 1) * rowBytes;
983 }
984 char* dst = (char*)tempStorage.realloc(trimSize);
985 for (int y = 0; y < desc.fContentHeight; y++) {
986 memcpy(dst, src, trimRowBytes);
987 if (flipY) {
988 src -= rowBytes;
989 } else {
990 src += rowBytes;
991 }
992 dst += trimRowBytes;
993 }
994 // now point data to our trimmed version
995 data = tempStorage.get();
996 rowBytes = trimRowBytes;
997 }
998 }
999
1000 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
1001 if (kIndex_8_GrPixelConfig == desc.fFormat &&
1002 supports8BitPalette()) {
1003 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
1004 GrAssert(desc.fContentWidth == desc.fAllocWidth);
1005 GrAssert(desc.fContentHeight == desc.fAllocHeight);
1006 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
1007 kGrColorTableSize;
1008 GR_GL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
1009 desc.fAllocWidth, desc.fAllocHeight,
1010 0, imageSize, data));
1011 GrGLRestoreResetRowLength();
1012 } else {
1013 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
1014 desc.fAllocHeight != desc.fContentHeight)) {
1015 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1016 desc.fAllocWidth, desc.fAllocHeight,
1017 0, desc.fUploadFormat, desc.fUploadType, NULL));
1018 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
1019 desc.fContentHeight, desc.fUploadFormat,
1020 desc.fUploadType, data));
1021 GrGLRestoreResetRowLength();
1022
1023 int extraW = desc.fAllocWidth - desc.fContentWidth;
1024 int extraH = desc.fAllocHeight - desc.fContentHeight;
1025 int maxTexels = extraW * extraH;
1026 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
1027 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
1028
1029 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
1030
1031 // rowBytes is actual stride between rows in data
1032 // rowDataBytes is the actual amount of non-pad data in a row
1033 // and the stride used for uploading extraH rows.
1034 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
1035 if (extraH) {
1036 uint8_t* lastRowStart = (uint8_t*) data +
1037 (desc.fContentHeight - 1) * rowBytes;
1038 uint8_t* extraRowStart = (uint8_t*)texels.get();
1039
1040 for (int i = 0; i < extraH; ++i) {
1041 memcpy(extraRowStart, lastRowStart, rowDataBytes);
1042 extraRowStart += rowDataBytes;
1043 }
1044 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, desc.fContentHeight,
1045 desc.fContentWidth, extraH,
1046 desc.fUploadFormat, desc.fUploadType,
1047 texels.get()));
1048 }
1049 if (extraW) {
1050 uint8_t* edgeTexel = (uint8_t*)data +
1051 rowDataBytes - desc.fUploadByteCount;
1052 uint8_t* extraTexel = (uint8_t*)texels.get();
1053 for (int j = 0; j < desc.fContentHeight; ++j) {
1054 for (int i = 0; i < extraW; ++i) {
1055 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
1056 extraTexel += desc.fUploadByteCount;
1057 }
1058 edgeTexel += rowBytes;
1059 }
1060 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth, 0,
1061 extraW, desc.fContentHeight,
1062 desc.fUploadFormat, desc.fUploadType,
1063 texels.get()));
1064 }
1065 if (extraW && extraH) {
1066 uint8_t* cornerTexel = (uint8_t*)data +
1067 desc.fContentHeight * rowBytes -
1068 desc.fUploadByteCount;
1069 uint8_t* extraTexel = (uint8_t*)texels.get();
1070 for (int i = 0; i < extraW*extraH; ++i) {
1071 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
1072 extraTexel += desc.fUploadByteCount;
1073 }
1074 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
1075 desc.fContentHeight, extraW, extraH,
1076 desc.fUploadFormat, desc.fUploadType,
1077 texels.get()));
1078 }
1079
1080 } else {
1081 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1082 desc.fAllocWidth, desc.fAllocHeight, 0,
1083 desc.fUploadFormat, desc.fUploadType, data));
1084 GrGLRestoreResetRowLength();
1085 }
1086 }
1087}
1088
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089bool GrGpuGL::createRenderTargetObjects(int width, int height,
1090 GrGLuint texID,
1091 GrGLRenderTarget::Desc* desc) {
1092 desc->fMSColorRenderbufferID = 0;
1093 desc->fRTFBOID = 0;
1094 desc->fTexFBOID = 0;
1095 desc->fOwnIDs = true;
1096
1097 GrGLenum status;
1098 GrGLint err;
1099
1100 GR_GL(GenFramebuffers(1, &desc->fTexFBOID));
1101 if (!desc->fTexFBOID) {
1102 goto FAILED;
1103 }
1104
1105 GrGLenum msColorFormat;
1106
1107 // If we are using multisampling we will create two FBOS. We render
1108 // to one and then resolve to the texture bound to the other.
1109 if (desc->fSampleCnt > 1 && kNone_MSFBO != fMSFBOType) {
1110 GR_GL(GenFramebuffers(1, &desc->fRTFBOID));
1111 GR_GL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
1112 if (!desc->fRTFBOID ||
1113 !desc->fMSColorRenderbufferID ||
1114 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
1115 goto FAILED;
1116 }
1117 } else {
1118 desc->fRTFBOID = desc->fTexFBOID;
1119 }
1120
1121 if (desc->fRTFBOID != desc->fTexFBOID) {
1122 GrAssert(desc->fSampleCnt > 1);
1123 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
1124 desc->fMSColorRenderbufferID));
1125 GR_GL_NO_ERR(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1126 desc->fSampleCnt,
1127 msColorFormat,
1128 width, height));
1129 err = GrGLGetGLInterface()->fGetError();
1130 if (err != GR_GL_NO_ERROR) {
1131 goto FAILED;
1132 }
1133 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
1134 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1135 GR_GL_COLOR_ATTACHMENT0,
1136 GR_GL_RENDERBUFFER,
1137 desc->fMSColorRenderbufferID));
1138 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1139 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1140 goto FAILED;
1141 }
1142 }
1143 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
1144
1145 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1146 GR_GL_COLOR_ATTACHMENT0,
1147 GR_GL_TEXTURE_2D,
1148 texID, 0));
1149 status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1150 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1151 goto FAILED;
1152 }
1153
1154 return true;
1155
1156FAILED:
1157 if (desc->fMSColorRenderbufferID) {
1158 GR_GL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
1159 }
1160 if (desc->fRTFBOID != desc->fTexFBOID) {
1161 GR_GL(DeleteFramebuffers(1, &desc->fRTFBOID));
1162 }
1163 if (desc->fTexFBOID) {
1164 GR_GL(DeleteFramebuffers(1, &desc->fTexFBOID));
1165 }
1166 return false;
1167}
1168
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001169// good to set a break-point here to know when createTexture fails
1170static GrTexture* return_null_texture() {
1171// GrAssert(!"null texture");
1172 return NULL;
1173}
1174
1175#if GR_DEBUG
1176static size_t as_size_t(int x) {
1177 return x;
1178}
1179#endif
1180
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001181GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001182 const void* srcData,
1183 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001184
1185#if GR_COLLECT_STATS
1186 ++fStats.fTextureCreateCnt;
1187#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001188
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001189 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001190 GR_GL_NEAREST,
1191 GR_GL_CLAMP_TO_EDGE,
1192 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001193 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001194
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001195 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001196 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001197 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001198
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001199 glTexDesc.fContentWidth = desc.fWidth;
1200 glTexDesc.fContentHeight = desc.fHeight;
1201 glTexDesc.fAllocWidth = desc.fWidth;
1202 glTexDesc.fAllocHeight = desc.fHeight;
1203 glTexDesc.fFormat = desc.fFormat;
1204 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001205
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001206 glRTDesc.fMSColorRenderbufferID = 0;
1207 glRTDesc.fRTFBOID = 0;
1208 glRTDesc.fTexFBOID = 0;
1209 glRTDesc.fOwnIDs = true;
1210 glRTDesc.fConfig = glTexDesc.fFormat;
1211
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001212 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001213 if (!canBeTexture(desc.fFormat,
1214 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001215 &glTexDesc.fUploadFormat,
1216 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001217 return return_null_texture();
1218 }
1219
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001220 // We keep GrRenderTargets in GL's normal orientation so that they
1221 // can be drawn to by the outside world without the client having
1222 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001223 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001225
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001227 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001228 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 GrPrintf("AA RT requested but not supported on this platform.");
1230 }
1231
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001232 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001233
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001235 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001236 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1237 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001238 }
1239
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001240 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 glTexDesc.fAllocWidth);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001242 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 glTexDesc.fAllocHeight);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001244 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1245 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001246 return return_null_texture();
1247 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001248 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001249 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1250 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1251 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1252 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001253 return return_null_texture();
1254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001255 }
1256
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001257 GR_GL(GenTextures(1, &glTexDesc.fTextureID));
1258 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001259 return return_null_texture();
1260 }
1261
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001262 this->setSpareTextureUnit();
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001263 GR_GL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001264 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1265 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001266 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001267 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1268 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001269 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001270 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1271 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001272 DEFAULT_PARAMS.fWrapS));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001273 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1274 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001275 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001276
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001277 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001278
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001281 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001282#if GR_COLLECT_STATS
1283 ++fStats.fRenderTargetCreateCnt;
1284#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001285 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1286 glTexDesc.fAllocHeight,
1287 glTexDesc.fTextureID,
1288 &glRTDesc)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001289 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 return return_null_texture();
1291 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001292 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1293 } else {
1294 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 }
1296#ifdef TRACE_TEXTURE_CREATION
1297 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1298 tex->fTextureID, width, height, tex->fUploadByteCount);
1299#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001300 return tex;
1301}
1302
1303namespace {
1304void inline get_stencil_rb_sizes(GrGLuint rb, GrGLStencilBuffer::Format* format) {
1305 // we shouldn't ever know one size and not the other
1306 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1307 (kUnknownBitCount == format->fTotalBits));
1308 if (kUnknownBitCount == format->fStencilBits) {
1309 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
1310 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1311 (GrGLint*)&format->fStencilBits);
1312 if (format->fPacked) {
1313 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
1314 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1315 (GrGLint*)&format->fTotalBits);
1316 format->fTotalBits += format->fStencilBits;
1317 } else {
1318 format->fTotalBits = format->fStencilBits;
1319 }
1320 }
1321}
1322}
1323
1324bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1325 int width, int height) {
1326
1327 // All internally created RTs are also textures. We don't create
1328 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1329 GrAssert(rt->asTexture());
1330 // if this thing is bloated for NPOT reasons we'll have to bloat the SB
1331 // as well.
1332 GrGLTexture* tex = (GrGLTexture*) rt->asTexture();
1333 width = GrMax(width, tex->allocWidth());
bsalomon@google.comc7e6d082011-08-05 15:38:49 +00001334 height = GrMax(height, tex->allocHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001335
1336 int samples = rt->numSamples();
1337 GrGLuint sbID;
1338 GR_GL(GenRenderbuffers(1, &sbID));
1339 if (!sbID) {
1340 return false;
1341 }
1342
1343 GrGLStencilBuffer* sb = NULL;
1344
1345 int stencilFmtCnt = fStencilFormats.count();
1346 for (int i = 0; i < stencilFmtCnt; ++i) {
1347 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
1348 // we start with the last stencil format that succeeded in hopes
1349 // that we won't go through this loop more than once after the
1350 // first (painful) stencil creation.
1351 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
1352 // we do this if so that we don't call the multisample
1353 // version on a GL that doesn't have an MSAA extension.
1354 if (samples > 1) {
1355 GR_GL_NO_ERR(RenderbufferStorageMultisample(
1356 GR_GL_RENDERBUFFER,
1357 samples,
1358 fStencilFormats[sIdx].fInternalFormat,
1359 width,
1360 height));
1361 } else {
1362 GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER,
1363 fStencilFormats[sIdx].fInternalFormat,
1364 width, height));
1365 }
1366
1367 GrGLenum err = GrGLGetGLInterface()->fGetError();
1368 if (err == GR_GL_NO_ERROR) {
1369 // After sized formats we attempt an unsized format and take whatever
1370 // sizes GL gives us. In that case we query for the size.
1371 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
1372 get_stencil_rb_sizes(sbID, &format);
1373 sb = new GrGLStencilBuffer(this, sbID, width, height, format);
1374 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1375 fLastSuccessfulStencilFmtIdx = sIdx;
1376 sb->unref();
1377 fHWDrawState.fRenderTarget = NULL;
1378 // initial clear zeros the entire sb by attaching it alone
1379 // to an fbo (that we create here on demand).
1380 if (!fStencilClearFBO) {
1381 GR_GL(GenFramebuffers(1, &fStencilClearFBO));
1382 if (0 == fStencilClearFBO) {
1383 rt->setStencilBuffer(NULL);
1384 return false;
1385 }
1386 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, fStencilClearFBO));
1387 if (GR_GL_SUPPORT_DESKTOP) {
1388 // We won't be binding a color buffer, set the draw
1389 // buffer to NONE to avoid
1390 // FRAMEBUFFER_INCOMPLETE_READ_BUFFER.
1391 GR_GL(DrawBuffer(GR_GL_NONE));
1392 // We bind to FRAMEBUFFER not DRAW_FRAMEBUFFER or
1393 // READ_FRAMEBUFFER because earlier versions of desktop
1394 // GL and unextended ES only have FRAMEBUFFER. But this
1395 // means we're binding both READ and DRAW when
1396 // FramebufferBlit is supported. So to avoid
1397 // FRAMEBUFFER_INCOMPLETE_READ_BUFFER status we also set
1398 // the read buffer to none.
1399 GR_GL(ReadBuffer(GR_GL_NONE));
1400 // DrawBuffer and ReadBuffer are framebuffer state so
1401 // we only have to set these the first time.
1402 }
1403 } else {
1404 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, fStencilClearFBO));
1405 }
1406 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1407 GR_GL_STENCIL_ATTACHMENT,
1408 GR_GL_RENDERBUFFER, sbID));
1409 if (fStencilFormats[sIdx].fPacked) {
1410 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1411 GR_GL_DEPTH_ATTACHMENT,
1412 GR_GL_RENDERBUFFER, sbID));
1413 } else {
1414 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1415 GR_GL_DEPTH_ATTACHMENT,
1416 GR_GL_RENDERBUFFER, 0));
1417 }
1418#if GR_DEBUG
1419 GrGLenum status =
1420 GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1421 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1422#endif
1423
1424 this->flushScissor(NULL);
1425 GR_GL(ClearStencil(0));
1426 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
1427 return true;
1428 }
1429 sb->abandon(); // otherwise we lose sbID
1430 sb->unref();
1431 }
1432 }
1433 GR_GL(DeleteRenderbuffers(1, &sbID));
1434 return NULL;
1435}
1436
1437bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1438 GrRenderTarget* rt) {
1439 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1440
1441 GrGLuint fbo = glrt->renderFBOID();
1442
1443 if (NULL == sb) {
1444 if (NULL != rt->getStencilBuffer()) {
1445 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1446 GR_GL_STENCIL_ATTACHMENT,
1447 GR_GL_RENDERBUFFER, 0));
1448 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1449 GR_GL_DEPTH_ATTACHMENT,
1450 GR_GL_RENDERBUFFER, 0));
1451#if GR_DEBUG
1452 GrGLenum status =
1453 GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1454 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1455#endif
1456 }
1457 return true;
1458 } else {
1459 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1460 GrGLuint rb = glsb->renderbufferID();
1461
reed@google.comac10a2d2010-12-22 21:39:39 +00001462 fHWDrawState.fRenderTarget = NULL;
1463
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001464 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1465 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1466 GR_GL_STENCIL_ATTACHMENT,
1467 GR_GL_RENDERBUFFER, rb));
1468 if (glsb->format().fPacked) {
1469 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1470 GR_GL_DEPTH_ATTACHMENT,
1471 GR_GL_RENDERBUFFER, rb));
1472 } else {
1473 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1474 GR_GL_DEPTH_ATTACHMENT,
1475 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001476 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001477
1478 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1479 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1480 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1481 GR_GL_STENCIL_ATTACHMENT,
1482 GR_GL_RENDERBUFFER, 0));
1483 if (glsb->format().fPacked) {
1484 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1485 GR_GL_DEPTH_ATTACHMENT,
1486 GR_GL_RENDERBUFFER, 0));
1487 }
1488 return false;
1489 } else {
1490 rt->setStencilBuffer(sb);
1491 return true;
1492 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001493 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001494}
1495
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001496////////////////////////////////////////////////////////////////////////////////
1497
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001498GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001499 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 GR_GL(GenBuffers(1, &id));
1501 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001502 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001503 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001504 GrGLClearErr();
1505 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001506 GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1507 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1508 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001509 GR_GL(DeleteBuffers(1, &id));
1510 // deleting bound buffer does implicit bind to 0
1511 fHWGeometryState.fVertexBuffer = NULL;
1512 return NULL;
1513 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001514 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 size, dynamic);
1516 fHWGeometryState.fVertexBuffer = vertexBuffer;
1517 return vertexBuffer;
1518 }
1519 return NULL;
1520}
1521
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001522GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001523 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001524 GR_GL(GenBuffers(1, &id));
1525 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001526 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001527 GrGLClearErr();
1528 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001529 GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1530 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1531 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001532 GR_GL(DeleteBuffers(1, &id));
1533 // deleting bound buffer does implicit bind to 0
1534 fHWGeometryState.fIndexBuffer = NULL;
1535 return NULL;
1536 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001537 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001538 size, dynamic);
1539 fHWGeometryState.fIndexBuffer = indexBuffer;
1540 return indexBuffer;
1541 }
1542 return NULL;
1543}
1544
reed@google.comac10a2d2010-12-22 21:39:39 +00001545void GrGpuGL::flushScissor(const GrIRect* rect) {
1546 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001547 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001548 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001549
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001550 GrGLIRect scissor;
1551 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001552 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001553 rect->width(), rect->height());
1554 if (scissor.contains(vp)) {
1555 rect = NULL;
1556 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001557 }
1558
1559 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001560 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001561 scissor.pushToGLScissor();
reed@google.comac10a2d2010-12-22 21:39:39 +00001562 fHWBounds.fScissorRect = scissor;
1563 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001564 if (!fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001565 GR_GL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001566 fHWBounds.fScissorEnabled = true;
1567 }
1568 } else {
1569 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001570 GR_GL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001571 fHWBounds.fScissorEnabled = false;
1572 }
1573 }
1574}
1575
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001576void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001577 if (NULL == fCurrDrawState.fRenderTarget) {
1578 return;
1579 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001580 GrIRect r;
1581 if (NULL != rect) {
1582 // flushScissor expects rect to be clipped to the target.
1583 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001584 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1585 fCurrDrawState.fRenderTarget->height());
1586 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001587 rect = &r;
1588 } else {
1589 return;
1590 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001592 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001593 this->flushScissor(rect);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001594 GR_GL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001595 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
reed@google.comac10a2d2010-12-22 21:39:39 +00001596 GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
1597 GrColorUnpackG(color)/255.f,
1598 GrColorUnpackB(color)/255.f,
1599 GrColorUnpackA(color)/255.f));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001600 GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001601}
1602
bsalomon@google.com398109c2011-04-14 18:40:27 +00001603void GrGpuGL::clearStencil(uint32_t value, uint32_t mask) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001604 if (NULL == fCurrDrawState.fRenderTarget) {
1605 return;
1606 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001607
1608 this->flushRenderTarget(&GrIRect::EmptyIRect());
1609
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001611 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001612 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 }
1614 GR_GL(StencilMask(mask));
1615 GR_GL(ClearStencil(value));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001616 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001617 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001618}
1619
bsalomon@google.com398109c2011-04-14 18:40:27 +00001620void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001621 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001622
1623 // this should only be called internally when we know we have a
1624 // stencil buffer.
1625 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001626#if 0
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001627 GrGLint stencilBitCount =
1628 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001629 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001630 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001631#else
1632 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001633 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001634 // turned into draws. Our contract on GrDrawTarget says that
1635 // changing the clip between stencil passes may or may not
1636 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001637 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001638#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001639 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001640 this->flushScissor(&rect);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001641 GR_GL(StencilMask(clipStencilMask));
1642 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001643 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001644 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001645}
1646
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001647void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001648 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001649}
1650
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001651bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1652 int left, int top, int width, int height,
1653 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001654 GrGLenum internalFormat; // we don't use this for glReadPixels
1655 GrGLenum format;
1656 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1658 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001659 }
1660 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1661 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1662 switch (tgt->getResolveType()) {
1663 case GrGLRenderTarget::kCantResolve_ResolveType:
1664 return false;
1665 case GrGLRenderTarget::kAutoResolves_ResolveType:
1666 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1667 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001668 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001669 break;
1670 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001671 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001672 // we don't track the state of the READ FBO ID.
1673 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
1674 break;
1675 default:
1676 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001677 }
1678
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001679 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001680
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001681 // the read rect is viewport-relative
1682 GrGLIRect readRect;
1683 readRect.setRelativeTo(glvp, left, top, width, height);
1684 GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001685 readRect.fWidth, readRect.fHeight,
bsalomon@google.com316f99232011-01-13 21:28:12 +00001686 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001687
1688 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1689 // API presents top-to-bottom
1690 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001691 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001692 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001693 void* tmp = rowStorage.get();
1694
1695 const int halfY = height >> 1;
1696 char* top = reinterpret_cast<char*>(buffer);
1697 char* bottom = top + (height - 1) * stride;
1698 for (int y = 0; y < halfY; y++) {
1699 memcpy(tmp, top, stride);
1700 memcpy(top, bottom, stride);
1701 memcpy(bottom, tmp, stride);
1702 top += stride;
1703 bottom -= stride;
1704 }
1705 }
1706 return true;
1707}
1708
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001709void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001710
1711 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1712
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001713 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001715 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001716 #if GR_COLLECT_STATS
1717 ++fStats.fRenderTargetChngCnt;
1718 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001719 #if GR_DEBUG
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001720 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1721 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001722 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001723 }
1724 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001725 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001726 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001727 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001728 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001729 vp.pushToGLViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 fHWBounds.fViewportRect = vp;
1731 }
1732 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001733 if (NULL == bound || !bound->isEmpty()) {
1734 rt->flagAsNeedingResolve(bound);
1735 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001736}
1737
twiz@google.com0f31ca72011-03-18 17:38:11 +00001738GrGLenum gPrimitiveType2GLMode[] = {
1739 GR_GL_TRIANGLES,
1740 GR_GL_TRIANGLE_STRIP,
1741 GR_GL_TRIANGLE_FAN,
1742 GR_GL_POINTS,
1743 GR_GL_LINES,
1744 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001745};
1746
bsalomon@google.comd302f142011-03-03 13:54:13 +00001747#define SWAP_PER_DRAW 0
1748
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001749#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001750 #if GR_MAC_BUILD
1751 #include <AGL/agl.h>
1752 #elif GR_WIN32_BUILD
1753 void SwapBuf() {
1754 DWORD procID = GetCurrentProcessId();
1755 HWND hwnd = GetTopWindow(GetDesktopWindow());
1756 while(hwnd) {
1757 DWORD wndProcID = 0;
1758 GetWindowThreadProcessId(hwnd, &wndProcID);
1759 if(wndProcID == procID) {
1760 SwapBuffers(GetDC(hwnd));
1761 }
1762 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1763 }
1764 }
1765 #endif
1766#endif
1767
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001768void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1769 uint32_t startVertex,
1770 uint32_t startIndex,
1771 uint32_t vertexCount,
1772 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001773 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1774
twiz@google.com0f31ca72011-03-18 17:38:11 +00001775 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001776
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001777 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1778 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1779
1780 // our setupGeometry better have adjusted this to zero since
1781 // DrawElements always draws from the begining of the arrays for idx 0.
1782 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001783
1784 GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
twiz@google.com0f31ca72011-03-18 17:38:11 +00001785 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001786#if SWAP_PER_DRAW
1787 glFlush();
1788 #if GR_MAC_BUILD
1789 aglSwapBuffers(aglGetCurrentContext());
1790 int set_a_break_pt_here = 9;
1791 aglSwapBuffers(aglGetCurrentContext());
1792 #elif GR_WIN32_BUILD
1793 SwapBuf();
1794 int set_a_break_pt_here = 9;
1795 SwapBuf();
1796 #endif
1797#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001798}
1799
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001800void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1801 uint32_t startVertex,
1802 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001803 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1804
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001805 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1806
1807 // our setupGeometry better have adjusted this to zero.
1808 // DrawElements doesn't take an offset so we always adjus the startVertex.
1809 GrAssert(0 == startVertex);
1810
1811 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1812 // account for startVertex in the DrawElements case. So we always
1813 // rely on setupGeometry to have accounted for startVertex.
reed@google.comac10a2d2010-12-22 21:39:39 +00001814 GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001815#if SWAP_PER_DRAW
1816 glFlush();
1817 #if GR_MAC_BUILD
1818 aglSwapBuffers(aglGetCurrentContext());
1819 int set_a_break_pt_here = 9;
1820 aglSwapBuffers(aglGetCurrentContext());
1821 #elif GR_WIN32_BUILD
1822 SwapBuf();
1823 int set_a_break_pt_here = 9;
1824 SwapBuf();
1825 #endif
1826#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001827}
1828
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001829void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001830
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001831 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001832 GrAssert(kNone_MSFBO != fMSFBOType);
1833 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001834 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001835 rt->renderFBOID()));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001836 GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001837 rt->textureFBOID()));
1838 #if GR_COLLECT_STATS
1839 ++fStats.fRenderTargetChngCnt;
1840 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001841 // make sure we go through flushRenderTarget() since we've modified
1842 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001843 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001844 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001845 const GrIRect dirtyRect = rt->getResolveRect();
1846 GrGLIRect r;
1847 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1848 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001849
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001850 if (kAppleES_MSFBO == fMSFBOType) {
1851 // Apple's extension uses the scissor as the blit bounds.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001852 GR_GL(Enable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001853 GR_GL(Scissor(r.fLeft, r.fBottom,
1854 r.fWidth, r.fHeight));
twiz@google.com59a190b2011-03-14 21:23:01 +00001855 GR_GL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001856 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001857 fHWBounds.fScissorEnabled = true;
1858 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001859 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001860 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001861 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1862 flushScissor(NULL);
1863 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001864 int right = r.fLeft + r.fWidth;
1865 int top = r.fBottom + r.fHeight;
1866 GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1867 r.fLeft, r.fBottom, right, top,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001868 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001869 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001870 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001871 }
1872}
1873
twiz@google.com0f31ca72011-03-18 17:38:11 +00001874static const GrGLenum grToGLStencilFunc[] = {
1875 GR_GL_ALWAYS, // kAlways_StencilFunc
1876 GR_GL_NEVER, // kNever_StencilFunc
1877 GR_GL_GREATER, // kGreater_StencilFunc
1878 GR_GL_GEQUAL, // kGEqual_StencilFunc
1879 GR_GL_LESS, // kLess_StencilFunc
1880 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1881 GR_GL_EQUAL, // kEqual_StencilFunc,
1882 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883};
1884GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1885GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1886GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1887GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1888GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1889GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1890GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1891GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1892GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1893
twiz@google.com0f31ca72011-03-18 17:38:11 +00001894static const GrGLenum grToGLStencilOp[] = {
1895 GR_GL_KEEP, // kKeep_StencilOp
1896 GR_GL_REPLACE, // kReplace_StencilOp
1897 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1898 GR_GL_INCR, // kIncClamp_StencilOp
1899 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1900 GR_GL_DECR, // kDecClamp_StencilOp
1901 GR_GL_ZERO, // kZero_StencilOp
1902 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001903};
1904GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1905GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1906GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1907GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1908GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1909GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1910GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1911GR_STATIC_ASSERT(6 == kZero_StencilOp);
1912GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1913
reed@google.comac10a2d2010-12-22 21:39:39 +00001914void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001915 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001916
1917 // use stencil for clipping if clipping is enabled and the clip
1918 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001919 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001920 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001921 bool stencilChange = fHWStencilClip != stencilClip ||
1922 fHWDrawState.fStencilSettings != *settings ||
1923 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1924 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001925
1926 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001927
bsalomon@google.comd302f142011-03-03 13:54:13 +00001928 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1929 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001930
bsalomon@google.comd302f142011-03-03 13:54:13 +00001931 if (settings->isDisabled()) {
1932 if (stencilClip) {
1933 settings = &gClipStencilSettings;
1934 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001935 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001936
1937 if (settings->isDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001938 GR_GL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001939 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001940 GR_GL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001941 #if GR_DEBUG
1942 if (!fStencilWrapOpsSupport) {
1943 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1944 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1945 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1946 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1947 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1948 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1949 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1950 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1951 }
1952 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001953 int stencilBits = 0;
1954 GrStencilBuffer* stencilBuffer =
1955 fCurrDrawState.fRenderTarget->getStencilBuffer();
1956 if (NULL != stencilBuffer) {
1957 stencilBits = stencilBuffer->bits();
1958 }
1959 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001960 GrAssert(stencilBits ||
1961 (GrStencilSettings::gDisabled ==
1962 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001963 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1964 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001965
1966 unsigned int frontRef = settings->fFrontFuncRef;
1967 unsigned int frontMask = settings->fFrontFuncMask;
1968 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001969 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001970
1971 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1972
1973 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1974 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1975 } else {
1976 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1977
1978 ConvertStencilFuncAndMask(settings->fFrontFunc,
1979 stencilClip,
1980 clipStencilMask,
1981 userStencilMask,
1982 &frontRef,
1983 &frontMask);
1984 frontWriteMask &= userStencilMask;
1985 }
1986 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001987 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001988 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001989 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001990 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001991 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001992 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001993 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001994 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001995 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001996
1997 unsigned int backRef = settings->fBackFuncRef;
1998 unsigned int backMask = settings->fBackFuncMask;
1999 unsigned int backWriteMask = settings->fBackWriteMask;
2000
2001
2002 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
2003 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
2004 backFunc = grToGLStencilFunc[settings->fBackFunc];
2005 } else {
2006 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
2007 ConvertStencilFuncAndMask(settings->fBackFunc,
2008 stencilClip,
2009 clipStencilMask,
2010 userStencilMask,
2011 &backRef,
2012 &backMask);
2013 backWriteMask &= userStencilMask;
2014 }
2015
twiz@google.com0f31ca72011-03-18 17:38:11 +00002016 GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask));
2017 GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
2018 GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask));
2019 GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
2020 GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00002021 grToGLStencilOp[settings->fFrontPassOp],
2022 grToGLStencilOp[settings->fFrontPassOp]));
2023
twiz@google.com0f31ca72011-03-18 17:38:11 +00002024 GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00002025 grToGLStencilOp[settings->fBackPassOp],
2026 grToGLStencilOp[settings->fBackPassOp]));
2027 } else {
2028 GR_GL(StencilFunc(frontFunc, frontRef, frontMask));
2029 GR_GL(StencilMask(frontWriteMask));
2030 GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
2031 grToGLStencilOp[settings->fFrontPassOp],
2032 grToGLStencilOp[settings->fFrontPassOp]));
2033 }
2034 }
2035 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002036 fHWStencilClip = stencilClip;
2037 }
2038}
2039
bsalomon@google.com0650e812011-04-08 18:07:53 +00002040bool GrGpuGL::useSmoothLines() {
2041 // there is a conflict between using smooth lines and our use of
2042 // premultiplied alpha. Smooth lines tweak the incoming alpha value
2043 // but not in a premul-alpha way. So we only use them when our alpha
2044 // is 0xff.
2045
2046 // TODO: write a smarter line frag shader.
2047
2048 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
2049 canDisableBlend();
2050}
2051
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002052void GrGpuGL::flushAAState(GrPrimitiveType type) {
2053 if (GR_GL_SUPPORT_DESKTOP) {
2054 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
2055 // smooth lines.
2056
2057 // we prefer smooth lines over multisampled lines
2058 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00002059 if (GrIsPrimTypeLines(type)) {
2060 bool smooth = useSmoothLines();
2061 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002062 GR_GL(Enable(GR_GL_LINE_SMOOTH));
2063 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002064 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002065 GR_GL(Disable(GR_GL_LINE_SMOOTH));
2066 fHWAAState.fSmoothLineEnabled = false;
2067 }
2068 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2069 fHWAAState.fMSAAEnabled) {
2070 GR_GL(Disable(GR_GL_MULTISAMPLE));
2071 fHWAAState.fMSAAEnabled = false;
2072 }
2073 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2074 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
2075 fHWAAState.fMSAAEnabled) {
2076 if (fHWAAState.fMSAAEnabled) {
2077 GR_GL(Disable(GR_GL_MULTISAMPLE));
2078 fHWAAState.fMSAAEnabled = false;
2079 } else {
2080 GR_GL(Enable(GR_GL_MULTISAMPLE));
2081 fHWAAState.fMSAAEnabled = true;
2082 }
2083 }
2084 }
2085}
2086
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002087void GrGpuGL::flushBlend(GrPrimitiveType type,
2088 GrBlendCoeff srcCoeff,
2089 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002090 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
2091 if (fHWBlendDisabled) {
2092 GR_GL(Enable(GR_GL_BLEND));
2093 fHWBlendDisabled = false;
2094 }
2095 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
2096 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
2097 GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2098 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
2099 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
2100 fHWDrawState.fDstBlend = kISA_BlendCoeff;
2101 }
2102 } else {
2103 bool blendOff = canDisableBlend();
2104 if (fHWBlendDisabled != blendOff) {
2105 if (blendOff) {
2106 GR_GL(Disable(GR_GL_BLEND));
2107 } else {
2108 GR_GL(Enable(GR_GL_BLEND));
2109 }
2110 fHWBlendDisabled = blendOff;
2111 }
2112 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002113 if (fHWDrawState.fSrcBlend != srcCoeff ||
2114 fHWDrawState.fDstBlend != dstCoeff) {
2115 GR_GL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2116 gXfermodeCoeff2Blend[dstCoeff]));
2117 fHWDrawState.fSrcBlend = srcCoeff;
2118 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002119 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002120 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2121 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002122 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2123
2124 float c[] = {
2125 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2126 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2127 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2128 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2129 };
2130 GR_GL(BlendColor(c[0], c[1], c[2], c[3]));
2131 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2132 }
2133 }
2134 }
2135}
2136
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002137static unsigned grToGLFilter(GrSamplerState::Filter filter) {
2138 switch (filter) {
2139 case GrSamplerState::kBilinear_Filter:
2140 case GrSamplerState::k4x4Downsample_Filter:
2141 return GR_GL_LINEAR;
2142 case GrSamplerState::kNearest_Filter:
2143 case GrSamplerState::kConvolution_Filter:
2144 return GR_GL_NEAREST;
2145 default:
2146 GrAssert(!"Unknown filter type");
2147 return GR_GL_LINEAR;
2148 }
2149}
2150
bsalomon@google.comffca4002011-02-22 20:34:01 +00002151bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002152
2153 // GrGpu::setupClipAndFlushState should have already checked this
2154 // and bailed if not true.
2155 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002156
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002157 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002158 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002159 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002160 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002161
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002162 // true for now, but maybe not with GrEffect.
2163 GrAssert(NULL != nextTexture);
2164 // if we created a rt/tex and rendered to it without using a
2165 // texture and now we're texuring from the rt it will still be
2166 // the last bound texture, but it needs resolving. So keep this
2167 // out of the "last != next" check.
2168 GrGLRenderTarget* texRT =
2169 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2170 if (NULL != texRT) {
2171 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002172 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002173
2174 if (fHWDrawState.fTextures[s] != nextTexture) {
2175 setTextureUnit(s);
2176 GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
2177 #if GR_COLLECT_STATS
2178 ++fStats.fTextureChngCnt;
2179 #endif
2180 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2181 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002182 // The texture matrix has to compensate for texture width/height
2183 // and NPOT-embedded-in-POT
2184 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002185 }
2186
2187 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2188 const GrGLTexture::TexParams& oldTexParams =
2189 nextTexture->getTexParams();
2190 GrGLTexture::TexParams newTexParams;
2191
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002192 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002193
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002194 newTexParams.fWrapS =
2195 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()];
2196 newTexParams.fWrapT =
2197 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()];
2198
2199 if (newTexParams.fFilter != oldTexParams.fFilter) {
2200 setTextureUnit(s);
2201 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2202 GR_GL_TEXTURE_MAG_FILTER,
2203 newTexParams.fFilter));
2204 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2205 GR_GL_TEXTURE_MIN_FILTER,
2206 newTexParams.fFilter));
2207 }
2208 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2209 setTextureUnit(s);
2210 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2211 GR_GL_TEXTURE_WRAP_S,
2212 newTexParams.fWrapS));
2213 }
2214 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2215 setTextureUnit(s);
2216 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2217 GR_GL_TEXTURE_WRAP_T,
2218 newTexParams.fWrapT));
2219 }
2220 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002221 }
2222 }
2223
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002224 GrIRect* rect = NULL;
2225 GrIRect clipBounds;
2226 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2227 fClip.hasConservativeBounds()) {
2228 fClip.getConservativeBounds().roundOut(&clipBounds);
2229 rect = &clipBounds;
2230 }
2231 this->flushRenderTarget(rect);
2232 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002233
reed@google.comac10a2d2010-12-22 21:39:39 +00002234 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2235 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2236 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002237 GR_GL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002238 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002239 GR_GL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002240 }
2241 }
2242
bsalomon@google.comd302f142011-03-03 13:54:13 +00002243 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2244 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002245 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002246 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002247 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002248 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002249 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002250 }
2251 GR_GL(ColorMask(mask, mask, mask, mask));
2252 }
2253
bsalomon@google.comd302f142011-03-03 13:54:13 +00002254 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2255 switch (fCurrDrawState.fDrawFace) {
2256 case kCCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002257 GR_GL(Enable(GR_GL_CULL_FACE));
2258 GR_GL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002259 break;
2260 case kCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002261 GR_GL(Enable(GR_GL_CULL_FACE));
2262 GR_GL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002263 break;
2264 case kBoth_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002265 GR_GL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002266 break;
2267 default:
2268 GrCrash("Unknown draw face.");
2269 }
2270 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2271 }
2272
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002273#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002274 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002275 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002276 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002277 NULL == fCurrDrawState.fRenderTarget ||
2278 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002279 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002280 fCurrDrawState.fRenderTarget);
2281 }
2282#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002283
reed@google.comac10a2d2010-12-22 21:39:39 +00002284 flushStencil();
2285
bsalomon@google.comd302f142011-03-03 13:54:13 +00002286 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002287 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002288 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002289}
2290
2291void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002292 if (fHWGeometryState.fVertexBuffer != buffer) {
2293 fHWGeometryState.fArrayPtrsDirty = true;
2294 fHWGeometryState.fVertexBuffer = buffer;
2295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002296}
2297
2298void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002299 if (fHWGeometryState.fVertexBuffer == buffer) {
2300 // deleting bound buffer does implied bind to 0
2301 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002302 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002303 }
2304}
2305
2306void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002307 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002308}
2309
2310void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002311 if (fHWGeometryState.fIndexBuffer == buffer) {
2312 // deleting bound buffer does implied bind to 0
2313 fHWGeometryState.fIndexBuffer = NULL;
2314 }
2315}
2316
reed@google.comac10a2d2010-12-22 21:39:39 +00002317void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2318 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002320 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002321 }
2322 if (fHWDrawState.fRenderTarget == renderTarget) {
2323 fHWDrawState.fRenderTarget = NULL;
2324 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002325}
2326
2327void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002328 for (int s = 0; s < kNumStages; ++s) {
2329 if (fCurrDrawState.fTextures[s] == texture) {
2330 fCurrDrawState.fTextures[s] = NULL;
2331 }
2332 if (fHWDrawState.fTextures[s] == texture) {
2333 // deleting bound texture does implied bind to 0
2334 fHWDrawState.fTextures[s] = NULL;
2335 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002336 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002337}
2338
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002339bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002340 GrGLenum* internalFormat,
2341 GrGLenum* format,
2342 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002343 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002344 case kRGBA_8888_GrPixelConfig:
2345 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002346 *format = GR_GL_32BPP_COLOR_FORMAT;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002347 if (GR_GL_SUPPORT_ES) {
2348 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2349 // format for a BGRA is BGRA not RGBA (as on desktop)
2350 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2351 } else {
2352 *internalFormat = GR_GL_RGBA;
2353 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002354 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002355 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002356 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002357 *format = GR_GL_RGB;
2358 *internalFormat = GR_GL_RGB;
2359 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002360 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002361 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002362 *format = GR_GL_RGBA;
2363 *internalFormat = GR_GL_RGBA;
2364 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002365 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002366 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002367 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002368 *format = GR_GL_PALETTE8_RGBA8;
2369 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002370 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002371 } else {
2372 return false;
2373 }
2374 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002375 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002376 *format = GR_GL_ALPHA;
2377 *internalFormat = GR_GL_ALPHA;
2378 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002379 break;
2380 default:
2381 return false;
2382 }
2383 return true;
2384}
2385
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002386void GrGpuGL::setTextureUnit(int unit) {
2387 GrAssert(unit >= 0 && unit < kNumStages);
2388 if (fActiveTextureUnitIdx != unit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002389 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002390 fActiveTextureUnitIdx = unit;
2391 }
2392}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002393
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002394void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002395 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
2396 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002397 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2398 }
2399}
2400
reed@google.comac10a2d2010-12-22 21:39:39 +00002401/* On ES the internalFormat and format must match for TexImage and we use
2402 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2403 decide the internalFormat. However, on ES internalFormat for
2404 RenderBufferStorage* has to be a specific format (not a base format like
2405 GL_RGBA).
2406 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002407bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002408 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002409 case kRGBA_8888_GrPixelConfig:
2410 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002411 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002412 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002413 return true;
2414 } else {
2415 return false;
2416 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002417 case kRGB_565_GrPixelConfig:
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002418 GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it
2419 // with FBO extension desktop GL has
2420 // no such internal format
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002421 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002422 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002423 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002424 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002425 return true;
2426 default:
2427 return false;
2428 }
2429}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002430
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002431void GrGpuGL::resetDirtyFlags() {
2432 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2433}
2434
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002435void GrGpuGL::setBuffers(bool indexed,
2436 int* extraVertexOffset,
2437 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002438
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002440
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002441 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2442
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002444 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 case kBuffer_GeometrySrcType:
2446 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002447 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002448 break;
2449 case kArray_GeometrySrcType:
2450 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002451 this->finalizeReservedVertices();
2452 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2453 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002454 break;
2455 default:
2456 vbuf = NULL; // suppress warning
2457 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002458 }
2459
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460 GrAssert(NULL != vbuf);
2461 GrAssert(!vbuf->isLocked());
2462 if (fHWGeometryState.fVertexBuffer != vbuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002463 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002464 fHWGeometryState.fArrayPtrsDirty = true;
2465 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002466 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002467
2468 if (indexed) {
2469 GrAssert(NULL != extraIndexOffset);
2470
2471 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002472 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002473 case kBuffer_GeometrySrcType:
2474 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002475 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002476 break;
2477 case kArray_GeometrySrcType:
2478 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002479 this->finalizeReservedIndices();
2480 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2481 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002482 break;
2483 default:
2484 ibuf = NULL; // suppress warning
2485 GrCrash("Unknown geometry src type!");
2486 }
2487
2488 GrAssert(NULL != ibuf);
2489 GrAssert(!ibuf->isLocked());
2490 if (fHWGeometryState.fIndexBuffer != ibuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002491 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002492 fHWGeometryState.fIndexBuffer = ibuf;
2493 }
2494 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002495}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002496
2497int GrGpuGL::getMaxEdges() const {
2498 // FIXME: This is a pessimistic estimate based on how many other things
2499 // want to add uniforms. This should be centralized somewhere.
2500 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2501}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002502