blob: 249897246c59a3322b1ce61fe098b8bf14fc88f0 [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"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000011#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000012#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
twiz@google.com0f31ca72011-03-18 17:38:11 +000014static const GrGLuint GR_MAX_GLUINT = ~0;
15static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com316f99232011-01-13 21:28:12 +000017// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000018// mucking with the state of any of the stages.
bsalomon@google.com316f99232011-01-13 21:28:12 +000019static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021#define SKIP_CACHE_CHECK true
22
twiz@google.com0f31ca72011-03-18 17:38:11 +000023static const GrGLenum gXfermodeCoeff2Blend[] = {
24 GR_GL_ZERO,
25 GR_GL_ONE,
26 GR_GL_SRC_COLOR,
27 GR_GL_ONE_MINUS_SRC_COLOR,
28 GR_GL_DST_COLOR,
29 GR_GL_ONE_MINUS_DST_COLOR,
30 GR_GL_SRC_ALPHA,
31 GR_GL_ONE_MINUS_SRC_ALPHA,
32 GR_GL_DST_ALPHA,
33 GR_GL_ONE_MINUS_DST_ALPHA,
34 GR_GL_CONSTANT_COLOR,
35 GR_GL_ONE_MINUS_CONSTANT_COLOR,
36 GR_GL_CONSTANT_ALPHA,
37 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000038
39 // extended blend coeffs
40 GR_GL_SRC1_COLOR,
41 GR_GL_ONE_MINUS_SRC1_COLOR,
42 GR_GL_SRC1_ALPHA,
43 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000044};
45
bsalomon@google.com271cffc2011-05-20 14:13:56 +000046bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000047 static const bool gCoeffReferencesBlendConst[] = {
48 false,
49 false,
50 false,
51 false,
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 true,
59 true,
60 true,
61 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062
63 // extended blend coeffs
64 false,
65 false,
66 false,
67 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000068 };
69 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000070 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
71
72 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
73 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
74 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
75 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
76 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
77 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
78 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
79 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
80 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
81 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
82 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
83 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
84 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
85 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
86
87 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
88 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
89 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
90 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
91
92 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
93 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000094}
95
reed@google.comac10a2d2010-12-22 21:39:39 +000096///////////////////////////////////////////////////////////////////////////////
97
bsalomon@google.comd302f142011-03-03 13:54:13 +000098void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
99 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000100 GrMatrix* matrix) {
101 GrAssert(NULL != texture);
102 GrAssert(NULL != matrix);
103 if (GR_Scalar1 != texture->contentScaleX() ||
104 GR_Scalar1 != texture->contentScaleY()) {
105 if (GrSamplerState::kRadial_SampleMode == mode) {
106 GrMatrix scale;
107 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
108 matrix->postConcat(scale);
109 } else if (GrSamplerState::kNormal_SampleMode == mode) {
110 GrMatrix scale;
111 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
112 matrix->postConcat(scale);
113 } else {
114 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
115 }
116 }
117 GrGLTexture::Orientation orientation = texture->orientation();
118 if (GrGLTexture::kBottomUp_Orientation == orientation) {
119 GrMatrix invY;
120 invY.setAll(GR_Scalar1, 0, 0,
121 0, -GR_Scalar1, GR_Scalar1,
122 0, 0, GrMatrix::I()[8]);
123 matrix->postConcat(invY);
124 } else {
125 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
126 }
127}
128
bsalomon@google.comd302f142011-03-03 13:54:13 +0000129bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000130 const GrSamplerState& sampler) {
131 GrAssert(NULL != texture);
132 if (!sampler.getMatrix().isIdentity()) {
133 return false;
134 }
135 if (GR_Scalar1 != texture->contentScaleX() ||
136 GR_Scalar1 != texture->contentScaleY()) {
137 return false;
138 }
139 GrGLTexture::Orientation orientation = texture->orientation();
140 if (GrGLTexture::kBottomUp_Orientation == orientation) {
141 return false;
142 } else {
143 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
144 }
145 return true;
146}
147
148///////////////////////////////////////////////////////////////////////////////
149
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000150static bool gPrintStartupSpew;
151
twiz@google.com59a190b2011-03-14 21:23:01 +0000152static bool fbo_test(int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000153
twiz@google.com0f31ca72011-03-18 17:38:11 +0000154 GrGLint savedFBO;
155 GrGLint savedTexUnit;
156 GR_GL_GetIntegerv(GR_GL_ACTIVE_TEXTURE, &savedTexUnit);
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000157 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &savedFBO);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000158
twiz@google.com0f31ca72011-03-18 17:38:11 +0000159 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000160
twiz@google.com0f31ca72011-03-18 17:38:11 +0000161 GrGLuint testFBO;
twiz@google.com59a190b2011-03-14 21:23:01 +0000162 GR_GL(GenFramebuffers(1, &testFBO));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000163 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000164 GrGLuint testRTTex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 GR_GL(GenTextures(1, &testRTTex));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000166 GR_GL(BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000167 // some implementations require texture to be mip-map complete before
168 // FBO with level 0 bound as color attachment will be framebuffer complete.
twiz@google.com0f31ca72011-03-18 17:38:11 +0000169 GR_GL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
170 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
171 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
172 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000173 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000174 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000175 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000176 GR_GL(DeleteFramebuffers(1, &testFBO));
reed@google.comac10a2d2010-12-22 21:39:39 +0000177 GR_GL(DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000178
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000179 GR_GL(ActiveTexture(savedTexUnit));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000180 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, savedFBO));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000181
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000182 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000183}
184
tomhudson@google.com747bf292011-06-14 18:16:52 +0000185static bool probe_for_npot_render_target_support(bool hasNPOTTextureSupport) {
186
187 /* Experimentation has found that some GLs that support NPOT textures
188 do not support FBOs with a NPOT texture. They report "unsupported" FBO
189 status. I don't know how to explicitly query for this. Do an
190 experiment. Note they may support NPOT with a renderbuffer but not a
191 texture. Presumably, the implementation bloats the renderbuffer
192 internally to the next POT.
193 */
194 if (hasNPOTTextureSupport) {
195 return fbo_test(200, 200);
196 }
197 return false;
198}
199
200static int probe_for_min_render_target_height(bool hasNPOTRenderTargetSupport,
201 int maxRenderTargetSize) {
202 /* The iPhone 4 has a restriction that for an FBO with texture color
203 attachment with height <= 8 then the width must be <= height. Here
204 we look for such a limitation.
205 */
206 if (gPrintStartupSpew) {
207 GrPrintf("Small height FBO texture experiments\n");
208 }
209 int minRenderTargetHeight = GR_INVAL_GLINT;
210 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
211 GrGLuint w = maxRenderTargetSize;
212 GrGLuint h = i;
213 if (fbo_test(w, h)) {
214 if (gPrintStartupSpew) {
215 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
216 }
217 minRenderTargetHeight = i;
218 break;
219 } else {
220 if (gPrintStartupSpew) {
221 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
222 }
223 }
224 }
225 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
226
227 return minRenderTargetHeight;
228}
229
230static int probe_for_min_render_target_width(bool hasNPOTRenderTargetSupport,
231 int maxRenderTargetSize) {
232
233 if (gPrintStartupSpew) {
234 GrPrintf("Small width FBO texture experiments\n");
235 }
236 int minRenderTargetWidth = GR_INVAL_GLINT;
237 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
238 GrGLuint w = i;
239 GrGLuint h = maxRenderTargetSize;
240 if (fbo_test(w, h)) {
241 if (gPrintStartupSpew) {
242 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
243 }
244 minRenderTargetWidth = i;
245 break;
246 } else {
247 if (gPrintStartupSpew) {
248 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
249 }
250 }
251 }
252 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
253
254 return minRenderTargetWidth;
255}
256
257
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000258GrGpuGL::GrGpuGL()
259 : fStencilFormats(8) {
260
261 GrGLClearErr();
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000262
reed@google.comeeeb5a02010-12-23 15:12:59 +0000263 if (gPrintStartupSpew) {
264 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
265 this);
twiz@google.com59a190b2011-03-14 21:23:01 +0000266 GrPrintf("------ VENDOR %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000267 GrGLGetGLInterface()->fGetString(GR_GL_VENDOR));
twiz@google.com59a190b2011-03-14 21:23:01 +0000268 GrPrintf("------ RENDERER %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000269 GrGLGetGLInterface()->fGetString(GR_GL_RENDERER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000270 GrPrintf("------ VERSION %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000271 GrGLGetGLInterface()->fGetString(GR_GL_VERSION));
twiz@google.com59a190b2011-03-14 21:23:01 +0000272 GrPrintf("------ EXTENSIONS\n %s \n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000273 GrGLGetGLInterface()->fGetString(GR_GL_EXTENSIONS));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000274 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000275
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000276 fGLVersion = gl_version_as_float();
277 fExtensionString = (const char*) GR_GL(GetString(GR_GL_EXTENSIONS));
reed@google.comac10a2d2010-12-22 21:39:39 +0000278
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000279 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000280
twiz@google.com0f31ca72011-03-18 17:38:11 +0000281 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000282 // check FS and fixed-function texture unit limits
283 // we only use textures in the fragment stage currently.
284 // checks are > to make sure we have a spare unit.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000285 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES2) {
286 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
287 GrAssert(maxTextureUnits > kNumStages);
288 }
289 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES1) {
290 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
291 GrAssert(maxTextureUnits > kNumStages);
292 }
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000293 if (GR_GL_SUPPORT_ES2) {
294 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
295 &fMaxFragmentUniformVectors);
296 } else if (GR_GL_SUPPORT_DESKTOP) {
297 GrGLint max;
298 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
299 fMaxFragmentUniformVectors = max / 4;
300 } else {
301 fMaxFragmentUniformVectors = 16;
302 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000303
reed@google.comac10a2d2010-12-22 21:39:39 +0000304 ////////////////////////////////////////////////////////////////////////////
305 // Check for supported features.
306
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000307 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000308
twiz@google.com0f31ca72011-03-18 17:38:11 +0000309 GrGLint numFormats;
310 GR_GL_GetIntegerv(GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000311 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
twiz@google.com0f31ca72011-03-18 17:38:11 +0000312 GR_GL_GetIntegerv(GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000313 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000314 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000315 f8bitPaletteSupport = true;
316 break;
317 }
318 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000319
320 if (gPrintStartupSpew) {
321 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
322 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000323
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000324 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
325 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
326 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
327 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
329 memset(fAASamples, 0, sizeof(fAASamples));
330 fMSFBOType = kNone_MSFBO;
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000331 if (GR_GL_SUPPORT_ES) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000332 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000333 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000334 // and fbo_blit extensions.
335 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000336 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000337 fMSFBOType = kAppleES_MSFBO;
338 }
339 } else {
340 GrAssert(GR_GL_SUPPORT_DESKTOP);
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000341 if ((fGLVersion >= 3.f) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000342 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000343 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
344 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000345 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000346 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000347 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000348 if (gPrintStartupSpew) {
349 switch (fMSFBOType) {
350 case kNone_MSFBO:
351 GrPrintf("MSAA Support: NONE\n");
352 break;
353 case kDesktopARB_MSFBO:
354 GrPrintf("MSAA Support: DESKTOP ARB.\n");
355 break;
356 case kDesktopEXT_MSFBO:
357 GrPrintf("MSAA Support: DESKTOP EXT.\n");
358 break;
359 case kAppleES_MSFBO:
360 GrPrintf("MSAA Support: APPLE ES.\n");
361 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000362 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000363 }
364
365 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000366 GrGLint maxSamples;
bsalomon@google.comd1e433532011-03-21 21:38:40 +0000367 GR_GL_GetIntegerv(GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000368 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000369 fAASamples[kNone_GrAALevel] = 0;
370 fAASamples[kLow_GrAALevel] = GrMax(2,
371 GrFixedFloorToInt((GR_FixedHalf) *
372 maxSamples));
373 fAASamples[kMed_GrAALevel] = GrMax(2,
374 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
375 maxSamples));
376 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000378 if (gPrintStartupSpew) {
379 GrPrintf("\tMax Samples: %d\n", maxSamples);
380 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000381 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000382 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000383
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000384 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000385 fHasStencilWrap = (fGLVersion >= 1.4f) ||
386 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000387 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000388 fHasStencilWrap = (fGLVersion >= 2.0f) || this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000389 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000390 if (gPrintStartupSpew) {
391 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
392 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000394 if (GR_GL_SUPPORT_DESKTOP) {
395 // we could also look for GL_ATI_separate_stencil extension or
396 // GL_EXT_stencil_two_side but they use different function signatures
397 // than GL2.0+ (and than each other).
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000398 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000399 // supported on GL 1.4 and higher or by extension
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000400 fStencilWrapOpsSupport = (fGLVersion >= 1.4f) ||
401 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000402 } else {
403 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
404 // an ES1 extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000405 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000406 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000407 fStencilWrapOpsSupport = (fGLVersion >= 2.f) ||
408 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000409 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000410 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000411 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
412 (fTwoSidedStencilSupport ? "YES" : "NO"),
413 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000414 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000416 if (GR_GL_SUPPORT_DESKTOP) {
417 fRGBA8Renderbuffer = true;
418 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000419 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000420 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000421 if (gPrintStartupSpew) {
422 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
423 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000424
425
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000426 if (GR_GL_SUPPORT_ES) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000427 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000428 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000429 }
430 }
431
432 if (GR_GL_SUPPORT_DESKTOP) {
433 fBufferLockSupport = true; // we require VBO support and the desktop VBO
434 // extension includes glMapBuffer.
435 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000436 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000437 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000438
reed@google.comeeeb5a02010-12-23 15:12:59 +0000439 if (gPrintStartupSpew) {
440 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
441 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000442
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000443 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000444 if (fGLVersion >= 2.f ||
445 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000446 fNPOTTextureTileSupport = true;
447 fNPOTTextureSupport = true;
448 } else {
449 fNPOTTextureTileSupport = false;
450 fNPOTTextureSupport = false;
451 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000453 if (fGLVersion >= 2.f) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000454 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000455 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000456 } else {
457 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000458 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000459 fNPOTTextureTileSupport = false;
460 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000461 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000462
bsalomon@google.com205d4602011-04-25 12:43:45 +0000463 fAALineSupport = GR_GL_SUPPORT_DESKTOP;
464
reed@google.comac10a2d2010-12-22 21:39:39 +0000465 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000466 // Experiments to determine limitations that can't be queried.
467 // TODO: Make these a preprocess that generate some compile time constants.
468 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000469
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000470 int expectNPOTTargets = GrGLGetGLInterface()->fNPOTRenderTargetSupport;
471 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000472 fNPOTRenderTargetSupport =
473 probe_for_npot_render_target_support(fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000474 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000475 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
476 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000477 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000478
bsalomon@google.com0748f212011-02-01 22:56:16 +0000479 if (gPrintStartupSpew) {
480 if (fNPOTTextureSupport) {
481 GrPrintf("NPOT textures supported\n");
482 if (fNPOTTextureTileSupport) {
483 GrPrintf("NPOT texture tiling supported\n");
484 } else {
485 GrPrintf("NPOT texture tiling NOT supported\n");
486 }
487 if (fNPOTRenderTargetSupport) {
488 GrPrintf("NPOT render targets supported\n");
489 } else {
490 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000491 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000492 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000493 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000494 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000495 }
496
bsalomon@google.com91958362011-06-13 17:58:13 +0000497 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
498 GR_GL_GetIntegerv(GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000499 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000500 // attachment, hence this min:
501 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000502
tomhudson@google.com747bf292011-06-14 18:16:52 +0000503 fMinRenderTargetHeight = GrGLGetGLInterface()->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000504 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000505 fMinRenderTargetHeight =
506 probe_for_min_render_target_height(fNPOTRenderTargetSupport,
507 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000508 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000509
tomhudson@google.com747bf292011-06-14 18:16:52 +0000510 fMinRenderTargetWidth = GrGLGetGLInterface()->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000511 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000512 fMinRenderTargetWidth =
513 probe_for_min_render_target_width(fNPOTRenderTargetSupport,
514 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000515 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000516
bsalomon@google.comfe676522011-06-17 18:12:21 +0000517 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000518}
519
520GrGpuGL::~GrGpuGL() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000521}
522
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000523void GrGpuGL::resetContext() {
524 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000525 fHWBlendDisabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000526 GR_GL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000527
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000528 // we don't use the zb at all
twiz@google.com0f31ca72011-03-18 17:38:11 +0000529 GR_GL(Disable(GR_GL_DEPTH_TEST));
530 GR_GL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000531
twiz@google.com0f31ca72011-03-18 17:38:11 +0000532 GR_GL(Disable(GR_GL_CULL_FACE));
533 GR_GL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000534 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
twiz@google.com0f31ca72011-03-18 17:38:11 +0000536 GR_GL(Disable(GR_GL_DITHER));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000537 if (GR_GL_SUPPORT_DESKTOP) {
538 GR_GL(Disable(GR_GL_LINE_SMOOTH));
539 GR_GL(Disable(GR_GL_POINT_SMOOTH));
540 GR_GL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000541 fHWAAState.fMSAAEnabled = false;
542 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000543 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000544
twiz@google.com0f31ca72011-03-18 17:38:11 +0000545 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000546 fHWDrawState.fFlagBits = 0;
547
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 // we only ever use lines in hairline mode
549 GR_GL(LineWidth(1));
550
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000551 // invalid
552 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000553
reed@google.comac10a2d2010-12-22 21:39:39 +0000554 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000555 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
556 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000557
558 fHWDrawState.fBlendConstant = 0x00000000;
559 GR_GL(BlendColor(0,0,0,0));
560
reed@google.comac10a2d2010-12-22 21:39:39 +0000561 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000562
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000563 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000564
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000565 for (int s = 0; s < kNumStages; ++s) {
566 fHWDrawState.fTextures[s] = NULL;
567 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
568 -GR_ScalarMax,
569 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000570 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000571 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000572 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000573
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000574 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 fHWBounds.fScissorEnabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000576 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000577 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000578
bsalomon@google.comd302f142011-03-03 13:54:13 +0000579 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000580 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000581 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000582
583 fHWGeometryState.fIndexBuffer = NULL;
584 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000585
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000586 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000587
twiz@google.com0f31ca72011-03-18 17:38:11 +0000588 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000589 fHWDrawState.fRenderTarget = NULL;
590}
591
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000592GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
593
594 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
595 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
596 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
597 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
598
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000599 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000600 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000601 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
602#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
603 if (desc.fSampleCnt) {
604#else
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000605 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000606#endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000607 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000608 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000609 } else {
610 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000611 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000612 }
613 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000614 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000615 }
616 // we don't know what the RB ids are without glGets and we don't care
617 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000618 rtDesc.fStencilRenderbufferID = 0;
619 rtDesc.fMSColorRenderbufferID = 0;
620#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
621 rtDesc.fSampleCnt = desc.fSampleCnt;
622#else
623 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
624 // just guess, this code path is only compiled in WK and we aren't
625 // using MSAA anyway. This will be stripped out soon when WK sets
626 // the fSampleCnt in GrPlatformSurfaceDesc.
627 rtDesc.fSampleCnt = 4;
628 } else {
629 rtDesc.fSampleCnt = 0;
630 }
631#endif
632 rtDesc.fStencilBits = desc.fStencilBits;
633 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000634 }
635
636 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000637 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000638 GrGLenum dontCare;
639 if (!canBeTexture(desc.fConfig, &dontCare,
640 &texDesc.fUploadFormat,
641 &texDesc.fUploadType)) {
642 return NULL;
643 }
644
645 GrGLTexture::TexParams params;
646
647 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
648 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
649
bsalomon@google.com90405932011-06-17 15:56:55 +0000650 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000651 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000652 texDesc.fTextureID = desc.fPlatformTexture;
653 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
654 texDesc.fOwnsID = false;
655
656 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000657 if (isRenderTarget) {
658 return new GrGLTexture(this, texDesc, rtDesc, params);
659 } else {
660 return new GrGLTexture(this, texDesc, params);
661 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000662 } else {
663 GrGLIRect viewport;
664 viewport.fLeft = 0;
665 viewport.fBottom = 0;
666 viewport.fWidth = desc.fWidth;
667 viewport.fHeight = desc.fHeight;
668
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000669 return new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000670 }
671}
672
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000673namespace {
674
675static const GrGLenum kUnknownGLFormat = ~0;
676
677GrGLenum get_fbo_color_format() {
678 GrGLint cbType;
679 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
680 GR_GL_COLOR_ATTACHMENT0,
681 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
682 &cbType);
683 GrGLint cbID;
684 GrGLint cbFormat;
685 switch (cbType) {
686 case GR_GL_RENDERBUFFER:
687 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
688 GR_GL_COLOR_ATTACHMENT0,
689 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
690 &cbID);
691 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
692 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
693 GR_GL_RENDERBUFFER_INTERNAL_FORMAT,
694 &cbFormat);
695 return cbFormat;
696 break;
697 case GR_GL_TEXTURE:
698 // ES doesn't have glGetTexLevelParameter
699 if (GR_GL_SUPPORT_DESKTOP) {
700 GrGLint cbLevel;
701 GrGLint cbFace;
702 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
703 GR_GL_COLOR_ATTACHMENT0,
704 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
705 &cbID);
706 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
707 GR_GL_COLOR_ATTACHMENT0,
708 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
709 &cbLevel);
710 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
711 GR_GL_COLOR_ATTACHMENT0,
712 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
713 &cbFace);
714 GrGLenum bind;
715 GrGLenum target;
716 if (cbFace) {
717 bind = GR_GL_TEXTURE_CUBE_MAP;
718 target = cbFace;
719 } else {
720 bind = GR_GL_TEXTURE_2D;
721 target = GR_GL_TEXTURE_2D;
722 }
723 GR_GL(BindTexture(bind, cbID));
724 GR_GL_GetTexLevelParameteriv(target, cbLevel,
725 GR_GL_TEXTURE_INTERNAL_FORMAT, &cbFormat);
726 return cbFormat;
727 } else {
728 return kUnknownGLFormat;
729 }
730 break;
731 default:
732 // we can get here with FBO 0, not a render buffer or a texture
733 return kUnknownGLFormat;
734 }
735}
736
737GrPixelConfig internal_color_format_to_config(GrGLenum iFormat) {
738 switch (iFormat) {
739 case GR_GL_RGB565:
740 return kRGB_565_GrPixelConfig;
741 case GR_GL_RGBA4:
742 return kRGBA_4444_GrPixelConfig;
743 case GR_GL_RGBA8:
744 case GR_GL_SRGB8_ALPHA8:
745 case GR_GL_SRGB_ALPHA:
746 case GR_GL_RGBA:
747 case GR_GL_BGRA:
748 return kRGBA_8888_GrPixelConfig;
749 case GR_GL_RGB8:
750 case GR_GL_SRGB8:
751 case GR_GL_SRGB:
752 return kRGBX_8888_GrPixelConfig;
753 default:
754 // there are many GL formats we don't have enums
755 // for. We should still render to them if the client
756 // asks us.
757 return kUnknown_GrPixelConfig;
758 }
759}
760
761GrPixelConfig get_implied_color_config(bool arbFBOExtension) {
762 GrGLint rSize, bSize, gSize, aSize;
763 if (arbFBOExtension) {
764 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
765 GR_GL_COLOR_ATTACHMENT0,
766 GR_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &rSize);
767 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
768 GR_GL_COLOR_ATTACHMENT0,
769 GR_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &gSize);
770 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
771 GR_GL_COLOR_ATTACHMENT0,
772 GR_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &bSize);
773 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
774 GR_GL_COLOR_ATTACHMENT0,
775 GR_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &aSize);
776 } else {
777 GR_GL_GetIntegerv(GR_GL_RED_BITS, &rSize);
778 GR_GL_GetIntegerv(GR_GL_GREEN_BITS, &gSize);
779 GR_GL_GetIntegerv(GR_GL_BLUE_BITS, &bSize);
780 GR_GL_GetIntegerv(GR_GL_ALPHA_BITS, &aSize);
781 }
782
783 if(8 == rSize && 8 == gSize && 8 == bSize) {
784 if (0 == aSize) {
785 return kRGBX_8888_GrPixelConfig;
786 } else if (8 == aSize) {
787 return kRGBA_8888_GrPixelConfig;
788 }
789 } else if (4 == rSize && 4 == gSize && 4 == bSize && 4 == aSize) {
790 return kRGBA_4444_GrPixelConfig;
791 } else if (5 == rSize && 6 == gSize && 5 == bSize && 0 == aSize) {
792 return kRGB_565_GrPixelConfig;
793 }
794 return kUnknown_GrPixelConfig;
795}
796
797int get_fbo_stencil_bits(bool arbFBOExtension) {
798 GrGLint stencilBits;
799 if (arbFBOExtension) {
800 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
801 GR_GL_STENCIL_ATTACHMENT,
802 GR_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
803 &stencilBits);
804 } else {
805 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &stencilBits);
806 }
807 return stencilBits;
808}
809}
810
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000811GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() {
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000812
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000813 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000814
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000815 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, (GrGLint*)&rtDesc.fRTFBOID);
816 rtDesc.fTexFBOID = rtDesc.fRTFBOID;
817 rtDesc.fMSColorRenderbufferID = 0;
818 rtDesc.fStencilRenderbufferID = 0;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000819
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000820 bool arbFBO = (GR_GL_SUPPORT_DESKTOP && (fGLVersion > 3.0 ||
821 this->hasExtension("GL_ARB_framebuffer_object")));
822
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000823 GrGLIRect viewport;
824 viewport.setFromGLViewport();
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000825 rtDesc.fStencilBits = get_fbo_stencil_bits(arbFBO);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000826
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000827 GR_GL_GetIntegerv(GR_GL_SAMPLES, &rtDesc.fSampleCnt);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000828 GrGLenum fmat = get_fbo_color_format();
829 if (kUnknownGLFormat == fmat) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000830 rtDesc.fConfig = get_implied_color_config(arbFBO);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000831 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000832 rtDesc.fConfig = internal_color_format_to_config(fmat);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000833 }
834
835 // may have to bind a texture to gets its format
836 this->setSpareTextureUnit();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000837
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000838 rtDesc.fOwnIDs = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000839
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000840 return new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000841}
842
bsalomon@google.com5782d712011-01-21 21:03:59 +0000843///////////////////////////////////////////////////////////////////////////////
844
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000845void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000846
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000847 // Build up list of legal stencil formats (though perhaps not supported on
848 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000849
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000850 // these consts are in order of most preferred to least preferred
851 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
852 static const StencilFormat gS8 = {GR_GL_STENCIL_INDEX8, 8, false};
853 static const StencilFormat gS16 = {GR_GL_STENCIL_INDEX16, 16, false};
854 static const StencilFormat gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, true };
855 static const StencilFormat gS4 = {GR_GL_STENCIL_INDEX4, 4, false};
856 static const StencilFormat gS = {GR_GL_STENCIL_INDEX, gUNKNOWN_BITCOUNT, false};
857 static const StencilFormat gDS = {GR_GL_DEPTH_STENCIL, gUNKNOWN_BITCOUNT, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000858
859 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000860 bool supportsPackedDS = fGLVersion >= 3.0f ||
861 this->hasExtension("GL_EXT_packed_depth_stencil") ||
862 this->hasExtension("GL_ARB_framebuffer_object");
863
864 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
865 // require FBO support we can expect these are legal formats and don't
866 // check. These also all support the unsized GL_STENCIL_INDEX.
867 fStencilFormats.push_back() = gS8;
868 fStencilFormats.push_back() = gS16;
869 if (supportsPackedDS) {
870 fStencilFormats.push_back() = gD24S8;
871 }
872 fStencilFormats.push_back() = gS4;
873 if (supportsPackedDS) {
874 fStencilFormats.push_back() = gDS;
875 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000876 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000877 // ES2 has STENCIL_INDEX8 without extensions.
878 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
879 // introduces tokens for S1 thu S8 but there are separate extensions
880 // that make them legal (GL_OES_stencil1, ...).
881 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
882 // ES doesn't support using the unsized formats.
883
884 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
885 fStencilFormats.push_back() = gS8;
886 }
887 //fStencilFormats.push_back() = gS16;
888 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
889 fStencilFormats.push_back() = gD24S8;
890 }
891 if (this->hasExtension("GL_OES_stencil4")) {
892 fStencilFormats.push_back() = gS4;
893 }
894 // we require some stencil format.
895 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000896 }
897}
898
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000899////////////////////////////////////////////////////////////////////////////////
900
901void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
902 GrGLenum internalFormat,
903 const void* data,
904 size_t rowBytes) {
905 // we assume the texture is bound;
906 if (!rowBytes) {
907 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
908 }
909
910 // in case we need a temporary, trimmed copy of the src pixels
911 SkAutoSMalloc<128 * 128> tempStorage;
912
913 /*
914 * check whether to allocate a temporary buffer for flipping y or
915 * because our data has extra bytes past each row. If so, we need
916 * to trim those off here, since GL ES doesn't let us specify
917 * GL_UNPACK_ROW_LENGTH.
918 */
919 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
920 if (GR_GL_SUPPORT_DESKTOP && !flipY) {
921 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
922 GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
923 rowBytes / desc.fUploadByteCount));
924 }
925 } else {
926 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
927 if (data && (trimRowBytes < rowBytes || flipY)) {
928 // copy the data into our new storage, skipping the trailing bytes
929 size_t trimSize = desc.fContentHeight * trimRowBytes;
930 const char* src = (const char*)data;
931 if (flipY) {
932 src += (desc.fContentHeight - 1) * rowBytes;
933 }
934 char* dst = (char*)tempStorage.realloc(trimSize);
935 for (int y = 0; y < desc.fContentHeight; y++) {
936 memcpy(dst, src, trimRowBytes);
937 if (flipY) {
938 src -= rowBytes;
939 } else {
940 src += rowBytes;
941 }
942 dst += trimRowBytes;
943 }
944 // now point data to our trimmed version
945 data = tempStorage.get();
946 rowBytes = trimRowBytes;
947 }
948 }
949
950 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
951 if (kIndex_8_GrPixelConfig == desc.fFormat &&
952 supports8BitPalette()) {
953 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
954 GrAssert(desc.fContentWidth == desc.fAllocWidth);
955 GrAssert(desc.fContentHeight == desc.fAllocHeight);
956 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
957 kGrColorTableSize;
958 GR_GL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
959 desc.fAllocWidth, desc.fAllocHeight,
960 0, imageSize, data));
961 GrGLRestoreResetRowLength();
962 } else {
963 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
964 desc.fAllocHeight != desc.fContentHeight)) {
965 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
966 desc.fAllocWidth, desc.fAllocHeight,
967 0, desc.fUploadFormat, desc.fUploadType, NULL));
968 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
969 desc.fContentHeight, desc.fUploadFormat,
970 desc.fUploadType, data));
971 GrGLRestoreResetRowLength();
972
973 int extraW = desc.fAllocWidth - desc.fContentWidth;
974 int extraH = desc.fAllocHeight - desc.fContentHeight;
975 int maxTexels = extraW * extraH;
976 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
977 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
978
979 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
980
981 // rowBytes is actual stride between rows in data
982 // rowDataBytes is the actual amount of non-pad data in a row
983 // and the stride used for uploading extraH rows.
984 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
985 if (extraH) {
986 uint8_t* lastRowStart = (uint8_t*) data +
987 (desc.fContentHeight - 1) * rowBytes;
988 uint8_t* extraRowStart = (uint8_t*)texels.get();
989
990 for (int i = 0; i < extraH; ++i) {
991 memcpy(extraRowStart, lastRowStart, rowDataBytes);
992 extraRowStart += rowDataBytes;
993 }
994 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, desc.fContentHeight,
995 desc.fContentWidth, extraH,
996 desc.fUploadFormat, desc.fUploadType,
997 texels.get()));
998 }
999 if (extraW) {
1000 uint8_t* edgeTexel = (uint8_t*)data +
1001 rowDataBytes - desc.fUploadByteCount;
1002 uint8_t* extraTexel = (uint8_t*)texels.get();
1003 for (int j = 0; j < desc.fContentHeight; ++j) {
1004 for (int i = 0; i < extraW; ++i) {
1005 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
1006 extraTexel += desc.fUploadByteCount;
1007 }
1008 edgeTexel += rowBytes;
1009 }
1010 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth, 0,
1011 extraW, desc.fContentHeight,
1012 desc.fUploadFormat, desc.fUploadType,
1013 texels.get()));
1014 }
1015 if (extraW && extraH) {
1016 uint8_t* cornerTexel = (uint8_t*)data +
1017 desc.fContentHeight * rowBytes -
1018 desc.fUploadByteCount;
1019 uint8_t* extraTexel = (uint8_t*)texels.get();
1020 for (int i = 0; i < extraW*extraH; ++i) {
1021 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
1022 extraTexel += desc.fUploadByteCount;
1023 }
1024 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
1025 desc.fContentHeight, extraW, extraH,
1026 desc.fUploadFormat, desc.fUploadType,
1027 texels.get()));
1028 }
1029
1030 } else {
1031 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1032 desc.fAllocWidth, desc.fAllocHeight, 0,
1033 desc.fUploadFormat, desc.fUploadType, data));
1034 GrGLRestoreResetRowLength();
1035 }
1036 }
1037}
1038
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001039// good to set a break-point here to know when createTexture fails
1040static GrTexture* return_null_texture() {
1041// GrAssert(!"null texture");
1042 return NULL;
1043}
1044
1045#if GR_DEBUG
1046static size_t as_size_t(int x) {
1047 return x;
1048}
1049#endif
1050
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001051GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001052 const void* srcData,
1053 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001054
1055#if GR_COLLECT_STATS
1056 ++fStats.fTextureCreateCnt;
1057#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001058
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001059 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001060 GR_GL_NEAREST,
1061 GR_GL_CLAMP_TO_EDGE,
1062 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001063 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001064
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001065 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001066 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001067 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001068
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001069 glTexDesc.fContentWidth = desc.fWidth;
1070 glTexDesc.fContentHeight = desc.fHeight;
1071 glTexDesc.fAllocWidth = desc.fWidth;
1072 glTexDesc.fAllocHeight = desc.fHeight;
1073 glTexDesc.fFormat = desc.fFormat;
1074 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001075
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001076 glRTDesc.fStencilRenderbufferID = 0;
1077 glRTDesc.fMSColorRenderbufferID = 0;
1078 glRTDesc.fRTFBOID = 0;
1079 glRTDesc.fTexFBOID = 0;
1080 glRTDesc.fOwnIDs = true;
1081 glRTDesc.fConfig = glTexDesc.fFormat;
1082
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001083 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001084 if (!canBeTexture(desc.fFormat,
1085 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001086 &glTexDesc.fUploadFormat,
1087 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 return return_null_texture();
1089 }
1090
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001091 // We keep GrRenderTargets in GL's normal orientation so that they
1092 // can be drawn to by the outside world without the client having
1093 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001094 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001095 GrGLTexture::kTopDown_Orientation;
1096
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001098 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001099 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 GrPrintf("AA RT requested but not supported on this platform.");
1101 }
1102
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001103 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001104
reed@google.comac10a2d2010-12-22 21:39:39 +00001105 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001106 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001107 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1108 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001109 }
1110
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001111 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
1112 glTexDesc.fAllocWidth);
1113 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
1114 glTexDesc.fAllocHeight);
1115 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1116 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001117 return return_null_texture();
1118 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001119 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001120 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1121 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1122 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1123 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001124 return return_null_texture();
1125 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001126 }
1127
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001128 GR_GL(GenTextures(1, &glTexDesc.fTextureID));
1129 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001130 return return_null_texture();
1131 }
1132
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001133 this->setSpareTextureUnit();
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001134 GR_GL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001135 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1136 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001137 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001138 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1139 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001140 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001141 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1142 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001143 DEFAULT_PARAMS.fWrapS));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001144 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1145 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001146 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001147
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001148 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001149
1150 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001151 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001152#if GR_COLLECT_STATS
1153 ++fStats.fRenderTargetCreateCnt;
1154#endif
1155 bool failed = true;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001156 GrGLenum status;
1157 GrGLint err;
reed@google.comac10a2d2010-12-22 21:39:39 +00001158
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001159 GR_GL(GenFramebuffers(1, &glRTDesc.fTexFBOID));
1160 GrAssert(glRTDesc.fTexFBOID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001161
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001162 // If we are using multisampling and we will create two FBOS We render
bsalomon@google.comd1e433532011-03-21 21:38:40 +00001163 // to one and then resolve to the texture bound to the other.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001164 if (glRTDesc.fSampleCnt > 0 && kNone_MSFBO != fMSFBOType) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001165 GR_GL(GenFramebuffers(1, &glRTDesc.fRTFBOID));
1166 GrAssert(0 != glRTDesc.fRTFBOID);
1167 GR_GL(GenRenderbuffers(1, &glRTDesc.fMSColorRenderbufferID));
1168 GrAssert(0 != glRTDesc.fMSColorRenderbufferID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001169 if (!fboInternalFormat(desc.fFormat, &msColorRenderbufferFormat)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001170 GR_GL(DeleteRenderbuffers(1, &glRTDesc.fMSColorRenderbufferID));
1171 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
1172 GR_GL(DeleteFramebuffers(1, &glRTDesc.fTexFBOID));
1173 GR_GL(DeleteFramebuffers(1, &glRTDesc.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001174 return return_null_texture();
1175 }
1176 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001177 glRTDesc.fRTFBOID = glRTDesc.fTexFBOID;
reed@google.comac10a2d2010-12-22 21:39:39 +00001178 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001179 if (!(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001180 GR_GL(GenRenderbuffers(1, &glRTDesc.fStencilRenderbufferID));
1181 GrAssert(0 != glRTDesc.fStencilRenderbufferID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001182 }
1183
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001184 // someone suggested that some systems might require
bsalomon@google.com316f99232011-01-13 21:28:12 +00001185 // unbinding the texture before we call FramebufferTexture2D
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001186 // (seems unlikely)
twiz@google.com0f31ca72011-03-18 17:38:11 +00001187 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001188
twiz@google.com0f31ca72011-03-18 17:38:11 +00001189 err = ~GR_GL_NO_ERROR;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00001190
bsalomon@google.comfe676522011-06-17 18:12:21 +00001191 int stencilFmtCnt;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001192 if (glRTDesc.fStencilRenderbufferID) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001193 stencilFmtCnt = fStencilFormats.count();
bsalomon@google.comfe676522011-06-17 18:12:21 +00001194 } else {
1195 stencilFmtCnt = 1; // only 1 attempt when we don't need a stencil
1196 }
1197
1198 for (int i = 0; i < stencilFmtCnt; ++i) {
1199 // we start with the last stencil format that succeeded in hopes
1200 // that we won't go through this loop more than once after the
1201 // first (painful) stencil creation.
1202 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
1203
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001204 if (glRTDesc.fStencilRenderbufferID) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001205 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001206 glRTDesc.fStencilRenderbufferID));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001207 if (glRTDesc.fSampleCnt > 0) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001208 GR_GL_NO_ERR(RenderbufferStorageMultisample(
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001209 GR_GL_RENDERBUFFER,
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001210 glRTDesc.fSampleCnt,
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001211 fStencilFormats[sIdx].fEnum,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001212 glTexDesc.fAllocWidth,
1213 glTexDesc.fAllocHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +00001214 } else {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001215 GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001216 fStencilFormats[sIdx].fEnum,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001217 glTexDesc.fAllocWidth,
1218 glTexDesc.fAllocHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +00001219 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001220 err = GrGLGetGLInterface()->fGetError();
1221 if (err != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 continue;
1223 }
1224 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001225 if (glRTDesc.fRTFBOID != glRTDesc.fTexFBOID) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001226 GrAssert(glRTDesc.fSampleCnt > 0);
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001227 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001228 glRTDesc.fMSColorRenderbufferID));
twiz@google.com59a190b2011-03-14 21:23:01 +00001229 GR_GL_NO_ERR(RenderbufferStorageMultisample(
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001230 GR_GL_RENDERBUFFER,
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001231 glRTDesc.fSampleCnt,
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 msColorRenderbufferFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001233 glTexDesc.fAllocWidth,
1234 glTexDesc.fAllocHeight));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001235 err = GrGLGetGLInterface()->fGetError();
1236 if (err != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001237 continue;
1238 }
1239 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001240 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRTDesc.fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001241
1242#if GR_COLLECT_STATS
1243 ++fStats.fRenderTargetChngCnt;
1244#endif
bsalomon@google.comd1e433532011-03-21 21:38:40 +00001245 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1246 GR_GL_COLOR_ATTACHMENT0,
1247 GR_GL_TEXTURE_2D,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001248 glTexDesc.fTextureID, 0));
1249 if (glRTDesc.fRTFBOID != glRTDesc.fTexFBOID) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001250 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1251 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001252 continue;
1253 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001254 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRTDesc.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001255 #if GR_COLLECT_STATS
1256 ++fStats.fRenderTargetChngCnt;
1257 #endif
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001258 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1259 GR_GL_COLOR_ATTACHMENT0,
1260 GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001261 glRTDesc.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001262
1263 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001264 if (glRTDesc.fStencilRenderbufferID) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 // bind the stencil to rt fbo if present, othewise the tex fbo
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001266 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1267 GR_GL_STENCIL_ATTACHMENT,
1268 GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001269 glRTDesc.fStencilRenderbufferID));
bsalomon@google.com9283b582011-04-08 19:00:04 +00001270 // if it is a packed format bind to depth also, otherwise
1271 // we may get an unsupported fbo completeness result
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001272 if (fStencilFormats[sIdx].fPacked) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001273 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1274 GR_GL_DEPTH_ATTACHMENT,
1275 GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001276 glRTDesc.fStencilRenderbufferID));
bsalomon@google.com9283b582011-04-08 19:00:04 +00001277 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 }
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001279 status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
reed@google.comac10a2d2010-12-22 21:39:39 +00001280
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001281 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001282 // undo the depth bind
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001283 if (glRTDesc.fStencilRenderbufferID &&
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001284 fStencilFormats[sIdx].fPacked) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001285 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001286 GR_GL_DEPTH_ATTACHMENT,
1287 GR_GL_RENDERBUFFER, 0));
twiz@google.comb65e0cb2011-03-18 20:41:44 +00001288 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 continue;
1290 }
1291 // we're successful!
1292 failed = false;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001293 if (glRTDesc.fStencilRenderbufferID) {
bsalomon@google.comfe676522011-06-17 18:12:21 +00001294 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001295 if (gUNKNOWN_BITCOUNT == fStencilFormats[sIdx].fBits) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001296 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, (GrGLint*)&glRTDesc.fStencilBits);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001297 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001298 glRTDesc.fStencilBits = fStencilFormats[sIdx].fBits;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001299 }
1300 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 break;
1302 }
1303 if (failed) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001304 if (glRTDesc.fStencilRenderbufferID) {
1305 GR_GL(DeleteRenderbuffers(1, &glRTDesc.fStencilRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001306 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001307 if (glRTDesc.fMSColorRenderbufferID) {
1308 GR_GL(DeleteRenderbuffers(1, &glRTDesc.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001310 if (glRTDesc.fRTFBOID != glRTDesc.fTexFBOID) {
1311 GR_GL(DeleteFramebuffers(1, &glRTDesc.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001313 if (glRTDesc.fTexFBOID) {
1314 GR_GL(DeleteFramebuffers(1, &glRTDesc.fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001316 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 return return_null_texture();
1318 }
1319 }
1320#ifdef TRACE_TEXTURE_CREATION
1321 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1322 tex->fTextureID, width, height, tex->fUploadByteCount);
1323#endif
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001324 if (renderTarget) {
1325 GrGLTexture* tex = new GrGLTexture(this, glTexDesc,
1326 glRTDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 GrRenderTarget* rt = tex->asRenderTarget();
1328 // We've messed with FBO state but may not have set the correct viewport
1329 // so just dirty the rendertarget state to force a resend.
1330 fHWDrawState.fRenderTarget = NULL;
1331
1332 // clear the new stencil buffer if we have one
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001333 if (!(desc.fFlags & kNoStencil_GrTextureFlagBit)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 GrRenderTarget* rtSave = fCurrDrawState.fRenderTarget;
1335 fCurrDrawState.fRenderTarget = rt;
bsalomon@google.com398109c2011-04-14 18:40:27 +00001336 this->clearStencil(0, ~0);
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 fCurrDrawState.fRenderTarget = rtSave;
1338 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001339 return tex;
1340 } else {
1341 return new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001342 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001343}
1344
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001345////////////////////////////////////////////////////////////////////////////////
1346
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001347GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001348 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 GR_GL(GenBuffers(1, &id));
1350 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001351 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001352 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 GrGLClearErr();
1354 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001355 GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1356 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1357 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 GR_GL(DeleteBuffers(1, &id));
1359 // deleting bound buffer does implicit bind to 0
1360 fHWGeometryState.fVertexBuffer = NULL;
1361 return NULL;
1362 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001363 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001364 size, dynamic);
1365 fHWGeometryState.fVertexBuffer = vertexBuffer;
1366 return vertexBuffer;
1367 }
1368 return NULL;
1369}
1370
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001371GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001372 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001373 GR_GL(GenBuffers(1, &id));
1374 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001375 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 GrGLClearErr();
1377 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001378 GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1379 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1380 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 GR_GL(DeleteBuffers(1, &id));
1382 // deleting bound buffer does implicit bind to 0
1383 fHWGeometryState.fIndexBuffer = NULL;
1384 return NULL;
1385 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001386 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001387 size, dynamic);
1388 fHWGeometryState.fIndexBuffer = indexBuffer;
1389 return indexBuffer;
1390 }
1391 return NULL;
1392}
1393
reed@google.comac10a2d2010-12-22 21:39:39 +00001394void GrGpuGL::flushScissor(const GrIRect* rect) {
1395 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001396 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001397 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001398
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001399 GrGLIRect scissor;
1400 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001401 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001402 rect->width(), rect->height());
1403 if (scissor.contains(vp)) {
1404 rect = NULL;
1405 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001406 }
1407
1408 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001409 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001410 scissor.pushToGLScissor();
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 fHWBounds.fScissorRect = scissor;
1412 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 if (!fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001414 GR_GL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001415 fHWBounds.fScissorEnabled = true;
1416 }
1417 } else {
1418 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001419 GR_GL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001420 fHWBounds.fScissorEnabled = false;
1421 }
1422 }
1423}
1424
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001425void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001426 if (NULL == fCurrDrawState.fRenderTarget) {
1427 return;
1428 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001429 GrIRect r;
1430 if (NULL != rect) {
1431 // flushScissor expects rect to be clipped to the target.
1432 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001433 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1434 fCurrDrawState.fRenderTarget->height());
1435 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001436 rect = &r;
1437 } else {
1438 return;
1439 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001440 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001441 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001442 this->flushScissor(rect);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001443 GR_GL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001444 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
reed@google.comac10a2d2010-12-22 21:39:39 +00001445 GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
1446 GrColorUnpackG(color)/255.f,
1447 GrColorUnpackB(color)/255.f,
1448 GrColorUnpackA(color)/255.f));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001449 GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001450}
1451
bsalomon@google.com398109c2011-04-14 18:40:27 +00001452void GrGpuGL::clearStencil(uint32_t value, uint32_t mask) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001453 if (NULL == fCurrDrawState.fRenderTarget) {
1454 return;
1455 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001456
1457 this->flushRenderTarget(&GrIRect::EmptyIRect());
1458
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001460 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001461 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001462 }
1463 GR_GL(StencilMask(mask));
1464 GR_GL(ClearStencil(value));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001465 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001466 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001467}
1468
bsalomon@google.com398109c2011-04-14 18:40:27 +00001469void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001470 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001471#if 0
twiz@google.com0f31ca72011-03-18 17:38:11 +00001472 GrGLint stencilBitCount = fCurrDrawState.fRenderTarget->stencilBits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001473 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001474 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001475#else
1476 // we could just clear the clip bit but when we go through
1477 // angle a partial stencil mask will cause clears to be
1478 // turned into draws. Our contract on GrDrawTarget says that
1479 // changing the clip between stencil passes may or may not
1480 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001481 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001482#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001483 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.comd302f142011-03-03 13:54:13 +00001484 flushScissor(&rect);
1485 GR_GL(StencilMask(clipStencilMask));
1486 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001487 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001488 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001489}
1490
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001491void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001492 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001493}
1494
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001495bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1496 int left, int top, int width, int height,
1497 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001498 GrGLenum internalFormat; // we don't use this for glReadPixels
1499 GrGLenum format;
1500 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001501 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1502 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001503 }
1504 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1505 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1506 switch (tgt->getResolveType()) {
1507 case GrGLRenderTarget::kCantResolve_ResolveType:
1508 return false;
1509 case GrGLRenderTarget::kAutoResolves_ResolveType:
1510 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1511 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001512 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001513 break;
1514 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001515 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001516 // we don't track the state of the READ FBO ID.
1517 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
1518 break;
1519 default:
1520 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 }
1522
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001523 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001524
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001525 // the read rect is viewport-relative
1526 GrGLIRect readRect;
1527 readRect.setRelativeTo(glvp, left, top, width, height);
1528 GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001529 readRect.fWidth, readRect.fHeight,
bsalomon@google.com316f99232011-01-13 21:28:12 +00001530 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001531
1532 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1533 // API presents top-to-bottom
1534 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001535 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001536 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001537 void* tmp = rowStorage.get();
1538
1539 const int halfY = height >> 1;
1540 char* top = reinterpret_cast<char*>(buffer);
1541 char* bottom = top + (height - 1) * stride;
1542 for (int y = 0; y < halfY; y++) {
1543 memcpy(tmp, top, stride);
1544 memcpy(top, bottom, stride);
1545 memcpy(bottom, tmp, stride);
1546 top += stride;
1547 bottom -= stride;
1548 }
1549 }
1550 return true;
1551}
1552
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001553void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001554
1555 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1556
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001557 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001558 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001559 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001560 #if GR_COLLECT_STATS
1561 ++fStats.fRenderTargetChngCnt;
1562 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001563 #if GR_DEBUG
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001564 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1565 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001566 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001567 }
1568 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001569 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001570 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001571 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001572 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001573 vp.pushToGLViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001574 fHWBounds.fViewportRect = vp;
1575 }
1576 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001577 if (NULL == bound || !bound->isEmpty()) {
1578 rt->flagAsNeedingResolve(bound);
1579 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001580}
1581
twiz@google.com0f31ca72011-03-18 17:38:11 +00001582GrGLenum gPrimitiveType2GLMode[] = {
1583 GR_GL_TRIANGLES,
1584 GR_GL_TRIANGLE_STRIP,
1585 GR_GL_TRIANGLE_FAN,
1586 GR_GL_POINTS,
1587 GR_GL_LINES,
1588 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001589};
1590
bsalomon@google.comd302f142011-03-03 13:54:13 +00001591#define SWAP_PER_DRAW 0
1592
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001593#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001594 #if GR_MAC_BUILD
1595 #include <AGL/agl.h>
1596 #elif GR_WIN32_BUILD
1597 void SwapBuf() {
1598 DWORD procID = GetCurrentProcessId();
1599 HWND hwnd = GetTopWindow(GetDesktopWindow());
1600 while(hwnd) {
1601 DWORD wndProcID = 0;
1602 GetWindowThreadProcessId(hwnd, &wndProcID);
1603 if(wndProcID == procID) {
1604 SwapBuffers(GetDC(hwnd));
1605 }
1606 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1607 }
1608 }
1609 #endif
1610#endif
1611
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001612void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1613 uint32_t startVertex,
1614 uint32_t startIndex,
1615 uint32_t vertexCount,
1616 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1618
twiz@google.com0f31ca72011-03-18 17:38:11 +00001619 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001620
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001621 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1622 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1623
1624 // our setupGeometry better have adjusted this to zero since
1625 // DrawElements always draws from the begining of the arrays for idx 0.
1626 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001627
1628 GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
twiz@google.com0f31ca72011-03-18 17:38:11 +00001629 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001630#if SWAP_PER_DRAW
1631 glFlush();
1632 #if GR_MAC_BUILD
1633 aglSwapBuffers(aglGetCurrentContext());
1634 int set_a_break_pt_here = 9;
1635 aglSwapBuffers(aglGetCurrentContext());
1636 #elif GR_WIN32_BUILD
1637 SwapBuf();
1638 int set_a_break_pt_here = 9;
1639 SwapBuf();
1640 #endif
1641#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001642}
1643
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001644void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1645 uint32_t startVertex,
1646 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1648
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001649 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1650
1651 // our setupGeometry better have adjusted this to zero.
1652 // DrawElements doesn't take an offset so we always adjus the startVertex.
1653 GrAssert(0 == startVertex);
1654
1655 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1656 // account for startVertex in the DrawElements case. So we always
1657 // rely on setupGeometry to have accounted for startVertex.
reed@google.comac10a2d2010-12-22 21:39:39 +00001658 GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001659#if SWAP_PER_DRAW
1660 glFlush();
1661 #if GR_MAC_BUILD
1662 aglSwapBuffers(aglGetCurrentContext());
1663 int set_a_break_pt_here = 9;
1664 aglSwapBuffers(aglGetCurrentContext());
1665 #elif GR_WIN32_BUILD
1666 SwapBuf();
1667 int set_a_break_pt_here = 9;
1668 SwapBuf();
1669 #endif
1670#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001671}
1672
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001673void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001674
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001675 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001676 GrAssert(kNone_MSFBO != fMSFBOType);
1677 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001678 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001679 rt->renderFBOID()));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001680 GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001681 rt->textureFBOID()));
1682 #if GR_COLLECT_STATS
1683 ++fStats.fRenderTargetChngCnt;
1684 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001685 // make sure we go through flushRenderTarget() since we've modified
1686 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001687 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001688 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001689 const GrIRect dirtyRect = rt->getResolveRect();
1690 GrGLIRect r;
1691 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1692 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001693
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001694 if (kAppleES_MSFBO == fMSFBOType) {
1695 // Apple's extension uses the scissor as the blit bounds.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001696 GR_GL(Enable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001697 GR_GL(Scissor(r.fLeft, r.fBottom,
1698 r.fWidth, r.fHeight));
twiz@google.com59a190b2011-03-14 21:23:01 +00001699 GR_GL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001700 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001701 fHWBounds.fScissorEnabled = true;
1702 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001703 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001704 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001705 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1706 flushScissor(NULL);
1707 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001708 int right = r.fLeft + r.fWidth;
1709 int top = r.fBottom + r.fHeight;
1710 GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1711 r.fLeft, r.fBottom, right, top,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001712 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001713 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001714 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001715 }
1716}
1717
twiz@google.com0f31ca72011-03-18 17:38:11 +00001718static const GrGLenum grToGLStencilFunc[] = {
1719 GR_GL_ALWAYS, // kAlways_StencilFunc
1720 GR_GL_NEVER, // kNever_StencilFunc
1721 GR_GL_GREATER, // kGreater_StencilFunc
1722 GR_GL_GEQUAL, // kGEqual_StencilFunc
1723 GR_GL_LESS, // kLess_StencilFunc
1724 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1725 GR_GL_EQUAL, // kEqual_StencilFunc,
1726 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001727};
1728GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1729GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1730GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1731GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1732GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1733GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1734GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1735GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1736GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1737
twiz@google.com0f31ca72011-03-18 17:38:11 +00001738static const GrGLenum grToGLStencilOp[] = {
1739 GR_GL_KEEP, // kKeep_StencilOp
1740 GR_GL_REPLACE, // kReplace_StencilOp
1741 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1742 GR_GL_INCR, // kIncClamp_StencilOp
1743 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1744 GR_GL_DECR, // kDecClamp_StencilOp
1745 GR_GL_ZERO, // kZero_StencilOp
1746 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001747};
1748GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1749GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1750GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1751GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1752GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1753GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1754GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1755GR_STATIC_ASSERT(6 == kZero_StencilOp);
1756GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1757
reed@google.comac10a2d2010-12-22 21:39:39 +00001758void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001759 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001760
1761 // use stencil for clipping if clipping is enabled and the clip
1762 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001763 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001765 bool stencilChange = fHWStencilClip != stencilClip ||
1766 fHWDrawState.fStencilSettings != *settings ||
1767 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1768 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001769
1770 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001771
bsalomon@google.comd302f142011-03-03 13:54:13 +00001772 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1773 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001774
bsalomon@google.comd302f142011-03-03 13:54:13 +00001775 if (settings->isDisabled()) {
1776 if (stencilClip) {
1777 settings = &gClipStencilSettings;
1778 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001779 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001780
1781 if (settings->isDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001782 GR_GL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001783 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001784 GR_GL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001785 #if GR_DEBUG
1786 if (!fStencilWrapOpsSupport) {
1787 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1788 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1789 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1790 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1791 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1792 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1793 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1794 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1795 }
1796 #endif
1797 int stencilBits = fCurrDrawState.fRenderTarget->stencilBits();
1798 GrAssert(stencilBits ||
1799 (GrStencilSettings::gDisabled ==
1800 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001801 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1802 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803
1804 unsigned int frontRef = settings->fFrontFuncRef;
1805 unsigned int frontMask = settings->fFrontFuncMask;
1806 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001807 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001808
1809 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1810
1811 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1812 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1813 } else {
1814 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1815
1816 ConvertStencilFuncAndMask(settings->fFrontFunc,
1817 stencilClip,
1818 clipStencilMask,
1819 userStencilMask,
1820 &frontRef,
1821 &frontMask);
1822 frontWriteMask &= userStencilMask;
1823 }
1824 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001825 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001826 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001827 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001829 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001831 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001832 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001833 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001834
1835 unsigned int backRef = settings->fBackFuncRef;
1836 unsigned int backMask = settings->fBackFuncMask;
1837 unsigned int backWriteMask = settings->fBackWriteMask;
1838
1839
1840 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1841 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1842 backFunc = grToGLStencilFunc[settings->fBackFunc];
1843 } else {
1844 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1845 ConvertStencilFuncAndMask(settings->fBackFunc,
1846 stencilClip,
1847 clipStencilMask,
1848 userStencilMask,
1849 &backRef,
1850 &backMask);
1851 backWriteMask &= userStencilMask;
1852 }
1853
twiz@google.com0f31ca72011-03-18 17:38:11 +00001854 GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask));
1855 GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1856 GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask));
1857 GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1858 GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859 grToGLStencilOp[settings->fFrontPassOp],
1860 grToGLStencilOp[settings->fFrontPassOp]));
1861
twiz@google.com0f31ca72011-03-18 17:38:11 +00001862 GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863 grToGLStencilOp[settings->fBackPassOp],
1864 grToGLStencilOp[settings->fBackPassOp]));
1865 } else {
1866 GR_GL(StencilFunc(frontFunc, frontRef, frontMask));
1867 GR_GL(StencilMask(frontWriteMask));
1868 GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
1869 grToGLStencilOp[settings->fFrontPassOp],
1870 grToGLStencilOp[settings->fFrontPassOp]));
1871 }
1872 }
1873 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001874 fHWStencilClip = stencilClip;
1875 }
1876}
1877
bsalomon@google.com0650e812011-04-08 18:07:53 +00001878bool GrGpuGL::useSmoothLines() {
1879 // there is a conflict between using smooth lines and our use of
1880 // premultiplied alpha. Smooth lines tweak the incoming alpha value
1881 // but not in a premul-alpha way. So we only use them when our alpha
1882 // is 0xff.
1883
1884 // TODO: write a smarter line frag shader.
1885
1886 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
1887 canDisableBlend();
1888}
1889
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001890void GrGpuGL::flushAAState(GrPrimitiveType type) {
1891 if (GR_GL_SUPPORT_DESKTOP) {
1892 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1893 // smooth lines.
1894
1895 // we prefer smooth lines over multisampled lines
1896 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001897 if (GrIsPrimTypeLines(type)) {
1898 bool smooth = useSmoothLines();
1899 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001900 GR_GL(Enable(GR_GL_LINE_SMOOTH));
1901 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001902 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001903 GR_GL(Disable(GR_GL_LINE_SMOOTH));
1904 fHWAAState.fSmoothLineEnabled = false;
1905 }
1906 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1907 fHWAAState.fMSAAEnabled) {
1908 GR_GL(Disable(GR_GL_MULTISAMPLE));
1909 fHWAAState.fMSAAEnabled = false;
1910 }
1911 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1912 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
1913 fHWAAState.fMSAAEnabled) {
1914 if (fHWAAState.fMSAAEnabled) {
1915 GR_GL(Disable(GR_GL_MULTISAMPLE));
1916 fHWAAState.fMSAAEnabled = false;
1917 } else {
1918 GR_GL(Enable(GR_GL_MULTISAMPLE));
1919 fHWAAState.fMSAAEnabled = true;
1920 }
1921 }
1922 }
1923}
1924
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001925void GrGpuGL::flushBlend(GrPrimitiveType type,
1926 GrBlendCoeff srcCoeff,
1927 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001928 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
1929 if (fHWBlendDisabled) {
1930 GR_GL(Enable(GR_GL_BLEND));
1931 fHWBlendDisabled = false;
1932 }
1933 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1934 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
1935 GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1936 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
1937 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1938 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1939 }
1940 } else {
1941 bool blendOff = canDisableBlend();
1942 if (fHWBlendDisabled != blendOff) {
1943 if (blendOff) {
1944 GR_GL(Disable(GR_GL_BLEND));
1945 } else {
1946 GR_GL(Enable(GR_GL_BLEND));
1947 }
1948 fHWBlendDisabled = blendOff;
1949 }
1950 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001951 if (fHWDrawState.fSrcBlend != srcCoeff ||
1952 fHWDrawState.fDstBlend != dstCoeff) {
1953 GR_GL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1954 gXfermodeCoeff2Blend[dstCoeff]));
1955 fHWDrawState.fSrcBlend = srcCoeff;
1956 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001957 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001958 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1959 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001960 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1961
1962 float c[] = {
1963 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1964 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1965 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1966 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1967 };
1968 GR_GL(BlendColor(c[0], c[1], c[2], c[3]));
1969 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1970 }
1971 }
1972 }
1973}
1974
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001975static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1976 switch (filter) {
1977 case GrSamplerState::kBilinear_Filter:
1978 case GrSamplerState::k4x4Downsample_Filter:
1979 return GR_GL_LINEAR;
1980 case GrSamplerState::kNearest_Filter:
1981 case GrSamplerState::kConvolution_Filter:
1982 return GR_GL_NEAREST;
1983 default:
1984 GrAssert(!"Unknown filter type");
1985 return GR_GL_LINEAR;
1986 }
1987}
1988
bsalomon@google.comffca4002011-02-22 20:34:01 +00001989bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001990
1991 // GrGpu::setupClipAndFlushState should have already checked this
1992 // and bailed if not true.
1993 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001994
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001995 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001996 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001997 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001998 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001999
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002000 // true for now, but maybe not with GrEffect.
2001 GrAssert(NULL != nextTexture);
2002 // if we created a rt/tex and rendered to it without using a
2003 // texture and now we're texuring from the rt it will still be
2004 // the last bound texture, but it needs resolving. So keep this
2005 // out of the "last != next" check.
2006 GrGLRenderTarget* texRT =
2007 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2008 if (NULL != texRT) {
2009 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002010 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002011
2012 if (fHWDrawState.fTextures[s] != nextTexture) {
2013 setTextureUnit(s);
2014 GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
2015 #if GR_COLLECT_STATS
2016 ++fStats.fTextureChngCnt;
2017 #endif
2018 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2019 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002020 // The texture matrix has to compensate for texture width/height
2021 // and NPOT-embedded-in-POT
2022 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002023 }
2024
2025 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2026 const GrGLTexture::TexParams& oldTexParams =
2027 nextTexture->getTexParams();
2028 GrGLTexture::TexParams newTexParams;
2029
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002030 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002031
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002032 newTexParams.fWrapS =
2033 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()];
2034 newTexParams.fWrapT =
2035 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()];
2036
2037 if (newTexParams.fFilter != oldTexParams.fFilter) {
2038 setTextureUnit(s);
2039 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2040 GR_GL_TEXTURE_MAG_FILTER,
2041 newTexParams.fFilter));
2042 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2043 GR_GL_TEXTURE_MIN_FILTER,
2044 newTexParams.fFilter));
2045 }
2046 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2047 setTextureUnit(s);
2048 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2049 GR_GL_TEXTURE_WRAP_S,
2050 newTexParams.fWrapS));
2051 }
2052 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2053 setTextureUnit(s);
2054 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2055 GR_GL_TEXTURE_WRAP_T,
2056 newTexParams.fWrapT));
2057 }
2058 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002059 }
2060 }
2061
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002062 GrIRect* rect = NULL;
2063 GrIRect clipBounds;
2064 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2065 fClip.hasConservativeBounds()) {
2066 fClip.getConservativeBounds().roundOut(&clipBounds);
2067 rect = &clipBounds;
2068 }
2069 this->flushRenderTarget(rect);
2070 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002071
reed@google.comac10a2d2010-12-22 21:39:39 +00002072 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2073 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2074 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002075 GR_GL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002076 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002077 GR_GL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002078 }
2079 }
2080
bsalomon@google.comd302f142011-03-03 13:54:13 +00002081 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2082 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002083 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002084 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002085 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002086 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002087 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002088 }
2089 GR_GL(ColorMask(mask, mask, mask, mask));
2090 }
2091
bsalomon@google.comd302f142011-03-03 13:54:13 +00002092 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2093 switch (fCurrDrawState.fDrawFace) {
2094 case kCCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002095 GR_GL(Enable(GR_GL_CULL_FACE));
2096 GR_GL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002097 break;
2098 case kCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002099 GR_GL(Enable(GR_GL_CULL_FACE));
2100 GR_GL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002101 break;
2102 case kBoth_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002103 GR_GL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002104 break;
2105 default:
2106 GrCrash("Unknown draw face.");
2107 }
2108 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2109 }
2110
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002111#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002112 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002113 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002114 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002115 NULL == fCurrDrawState.fRenderTarget ||
2116 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002117 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002118 fCurrDrawState.fRenderTarget);
2119 }
2120#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002121
reed@google.comac10a2d2010-12-22 21:39:39 +00002122 flushStencil();
2123
bsalomon@google.comd302f142011-03-03 13:54:13 +00002124 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002125 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002126 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002127}
2128
2129void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002130 if (fHWGeometryState.fVertexBuffer != buffer) {
2131 fHWGeometryState.fArrayPtrsDirty = true;
2132 fHWGeometryState.fVertexBuffer = buffer;
2133 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002134}
2135
2136void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002137 if (fHWGeometryState.fVertexBuffer == buffer) {
2138 // deleting bound buffer does implied bind to 0
2139 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002140 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002141 }
2142}
2143
2144void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002145 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002146}
2147
2148void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002149 if (fHWGeometryState.fIndexBuffer == buffer) {
2150 // deleting bound buffer does implied bind to 0
2151 fHWGeometryState.fIndexBuffer = NULL;
2152 }
2153}
2154
reed@google.comac10a2d2010-12-22 21:39:39 +00002155void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2156 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002157 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002158 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002159 }
2160 if (fHWDrawState.fRenderTarget == renderTarget) {
2161 fHWDrawState.fRenderTarget = NULL;
2162 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002163}
2164
2165void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002166 for (int s = 0; s < kNumStages; ++s) {
2167 if (fCurrDrawState.fTextures[s] == texture) {
2168 fCurrDrawState.fTextures[s] = NULL;
2169 }
2170 if (fHWDrawState.fTextures[s] == texture) {
2171 // deleting bound texture does implied bind to 0
2172 fHWDrawState.fTextures[s] = NULL;
2173 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002174 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002175}
2176
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002177bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002178 GrGLenum* internalFormat,
2179 GrGLenum* format,
2180 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002181 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002182 case kRGBA_8888_GrPixelConfig:
2183 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002184 *format = GR_GL_32BPP_COLOR_FORMAT;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002185 if (GR_GL_SUPPORT_ES) {
2186 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2187 // format for a BGRA is BGRA not RGBA (as on desktop)
2188 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2189 } else {
2190 *internalFormat = GR_GL_RGBA;
2191 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002192 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002193 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002194 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002195 *format = GR_GL_RGB;
2196 *internalFormat = GR_GL_RGB;
2197 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002198 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002199 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002200 *format = GR_GL_RGBA;
2201 *internalFormat = GR_GL_RGBA;
2202 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002203 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002204 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002205 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002206 *format = GR_GL_PALETTE8_RGBA8;
2207 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002208 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002209 } else {
2210 return false;
2211 }
2212 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002213 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002214 *format = GR_GL_ALPHA;
2215 *internalFormat = GR_GL_ALPHA;
2216 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002217 break;
2218 default:
2219 return false;
2220 }
2221 return true;
2222}
2223
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002224void GrGpuGL::setTextureUnit(int unit) {
2225 GrAssert(unit >= 0 && unit < kNumStages);
2226 if (fActiveTextureUnitIdx != unit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002227 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002228 fActiveTextureUnitIdx = unit;
2229 }
2230}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002231
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002232void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002233 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
2234 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002235 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2236 }
2237}
2238
reed@google.comac10a2d2010-12-22 21:39:39 +00002239/* On ES the internalFormat and format must match for TexImage and we use
2240 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2241 decide the internalFormat. However, on ES internalFormat for
2242 RenderBufferStorage* has to be a specific format (not a base format like
2243 GL_RGBA).
2244 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002245bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002246 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002247 case kRGBA_8888_GrPixelConfig:
2248 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002249 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002250 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 return true;
2252 } else {
2253 return false;
2254 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002255 case kRGB_565_GrPixelConfig:
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002256 GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it
2257 // with FBO extension desktop GL has
2258 // no such internal format
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002259 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002260 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002261 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002262 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002263 return true;
2264 default:
2265 return false;
2266 }
2267}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002268
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002269void GrGpuGL::resetDirtyFlags() {
2270 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2271}
2272
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002273void GrGpuGL::setBuffers(bool indexed,
2274 int* extraVertexOffset,
2275 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002276
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002277 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002278
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002279 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2280
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002281 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002282 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002283 case kBuffer_GeometrySrcType:
2284 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002285 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002286 break;
2287 case kArray_GeometrySrcType:
2288 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002289 this->finalizeReservedVertices();
2290 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2291 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002292 break;
2293 default:
2294 vbuf = NULL; // suppress warning
2295 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002296 }
2297
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002298 GrAssert(NULL != vbuf);
2299 GrAssert(!vbuf->isLocked());
2300 if (fHWGeometryState.fVertexBuffer != vbuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002301 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002302 fHWGeometryState.fArrayPtrsDirty = true;
2303 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002304 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002305
2306 if (indexed) {
2307 GrAssert(NULL != extraIndexOffset);
2308
2309 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002310 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002311 case kBuffer_GeometrySrcType:
2312 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002313 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002314 break;
2315 case kArray_GeometrySrcType:
2316 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002317 this->finalizeReservedIndices();
2318 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2319 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002320 break;
2321 default:
2322 ibuf = NULL; // suppress warning
2323 GrCrash("Unknown geometry src type!");
2324 }
2325
2326 GrAssert(NULL != ibuf);
2327 GrAssert(!ibuf->isLocked());
2328 if (fHWGeometryState.fIndexBuffer != ibuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002329 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002330 fHWGeometryState.fIndexBuffer = ibuf;
2331 }
2332 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002333}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002334
2335int GrGpuGL::getMaxEdges() const {
2336 // FIXME: This is a pessimistic estimate based on how many other things
2337 // want to add uniforms. This should be centralized somewhere.
2338 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2339}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002340