blob: b8a04068ed3cee5bd890897029d8f9de886b6f3b [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.com1c13c962011-02-14 16:51:21 +0000581 fClipState.fClipIsDirty = true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000582 fClipState.fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000583
584 fHWGeometryState.fIndexBuffer = NULL;
585 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000586
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000587 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
twiz@google.com0f31ca72011-03-18 17:38:11 +0000589 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 fHWDrawState.fRenderTarget = NULL;
591}
592
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000593GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
594
595 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
596 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
597 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
598 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
599
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000600 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000601 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000602 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
603#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
604 if (desc.fSampleCnt) {
605#else
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000606 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000607#endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000608 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000609 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000610 } else {
611 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000612 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000613 }
614 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000615 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000616 }
617 // we don't know what the RB ids are without glGets and we don't care
618 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000619 rtDesc.fStencilRenderbufferID = 0;
620 rtDesc.fMSColorRenderbufferID = 0;
621#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
622 rtDesc.fSampleCnt = desc.fSampleCnt;
623#else
624 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
625 // just guess, this code path is only compiled in WK and we aren't
626 // using MSAA anyway. This will be stripped out soon when WK sets
627 // the fSampleCnt in GrPlatformSurfaceDesc.
628 rtDesc.fSampleCnt = 4;
629 } else {
630 rtDesc.fSampleCnt = 0;
631 }
632#endif
633 rtDesc.fStencilBits = desc.fStencilBits;
634 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000635 }
636
637 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000638 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000639 GrGLenum dontCare;
640 if (!canBeTexture(desc.fConfig, &dontCare,
641 &texDesc.fUploadFormat,
642 &texDesc.fUploadType)) {
643 return NULL;
644 }
645
646 GrGLTexture::TexParams params;
647
648 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
649 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
650
bsalomon@google.com90405932011-06-17 15:56:55 +0000651 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000652 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000653 texDesc.fTextureID = desc.fPlatformTexture;
654 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
655 texDesc.fOwnsID = false;
656
657 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000658 if (isRenderTarget) {
659 return new GrGLTexture(this, texDesc, rtDesc, params);
660 } else {
661 return new GrGLTexture(this, texDesc, params);
662 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000663 } else {
664 GrGLIRect viewport;
665 viewport.fLeft = 0;
666 viewport.fBottom = 0;
667 viewport.fWidth = desc.fWidth;
668 viewport.fHeight = desc.fHeight;
669
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000670 return new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000671 }
672}
673
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000674namespace {
675
676static const GrGLenum kUnknownGLFormat = ~0;
677
678GrGLenum get_fbo_color_format() {
679 GrGLint cbType;
680 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
681 GR_GL_COLOR_ATTACHMENT0,
682 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
683 &cbType);
684 GrGLint cbID;
685 GrGLint cbFormat;
686 switch (cbType) {
687 case GR_GL_RENDERBUFFER:
688 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
689 GR_GL_COLOR_ATTACHMENT0,
690 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
691 &cbID);
692 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
693 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
694 GR_GL_RENDERBUFFER_INTERNAL_FORMAT,
695 &cbFormat);
696 return cbFormat;
697 break;
698 case GR_GL_TEXTURE:
699 // ES doesn't have glGetTexLevelParameter
700 if (GR_GL_SUPPORT_DESKTOP) {
701 GrGLint cbLevel;
702 GrGLint cbFace;
703 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
704 GR_GL_COLOR_ATTACHMENT0,
705 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
706 &cbID);
707 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
708 GR_GL_COLOR_ATTACHMENT0,
709 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
710 &cbLevel);
711 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
712 GR_GL_COLOR_ATTACHMENT0,
713 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
714 &cbFace);
715 GrGLenum bind;
716 GrGLenum target;
717 if (cbFace) {
718 bind = GR_GL_TEXTURE_CUBE_MAP;
719 target = cbFace;
720 } else {
721 bind = GR_GL_TEXTURE_2D;
722 target = GR_GL_TEXTURE_2D;
723 }
724 GR_GL(BindTexture(bind, cbID));
725 GR_GL_GetTexLevelParameteriv(target, cbLevel,
726 GR_GL_TEXTURE_INTERNAL_FORMAT, &cbFormat);
727 return cbFormat;
728 } else {
729 return kUnknownGLFormat;
730 }
731 break;
732 default:
733 // we can get here with FBO 0, not a render buffer or a texture
734 return kUnknownGLFormat;
735 }
736}
737
738GrPixelConfig internal_color_format_to_config(GrGLenum iFormat) {
739 switch (iFormat) {
740 case GR_GL_RGB565:
741 return kRGB_565_GrPixelConfig;
742 case GR_GL_RGBA4:
743 return kRGBA_4444_GrPixelConfig;
744 case GR_GL_RGBA8:
745 case GR_GL_SRGB8_ALPHA8:
746 case GR_GL_SRGB_ALPHA:
747 case GR_GL_RGBA:
748 case GR_GL_BGRA:
749 return kRGBA_8888_GrPixelConfig;
750 case GR_GL_RGB8:
751 case GR_GL_SRGB8:
752 case GR_GL_SRGB:
753 return kRGBX_8888_GrPixelConfig;
754 default:
755 // there are many GL formats we don't have enums
756 // for. We should still render to them if the client
757 // asks us.
758 return kUnknown_GrPixelConfig;
759 }
760}
761
762GrPixelConfig get_implied_color_config(bool arbFBOExtension) {
763 GrGLint rSize, bSize, gSize, aSize;
764 if (arbFBOExtension) {
765 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
766 GR_GL_COLOR_ATTACHMENT0,
767 GR_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &rSize);
768 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
769 GR_GL_COLOR_ATTACHMENT0,
770 GR_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &gSize);
771 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
772 GR_GL_COLOR_ATTACHMENT0,
773 GR_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &bSize);
774 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
775 GR_GL_COLOR_ATTACHMENT0,
776 GR_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &aSize);
777 } else {
778 GR_GL_GetIntegerv(GR_GL_RED_BITS, &rSize);
779 GR_GL_GetIntegerv(GR_GL_GREEN_BITS, &gSize);
780 GR_GL_GetIntegerv(GR_GL_BLUE_BITS, &bSize);
781 GR_GL_GetIntegerv(GR_GL_ALPHA_BITS, &aSize);
782 }
783
784 if(8 == rSize && 8 == gSize && 8 == bSize) {
785 if (0 == aSize) {
786 return kRGBX_8888_GrPixelConfig;
787 } else if (8 == aSize) {
788 return kRGBA_8888_GrPixelConfig;
789 }
790 } else if (4 == rSize && 4 == gSize && 4 == bSize && 4 == aSize) {
791 return kRGBA_4444_GrPixelConfig;
792 } else if (5 == rSize && 6 == gSize && 5 == bSize && 0 == aSize) {
793 return kRGB_565_GrPixelConfig;
794 }
795 return kUnknown_GrPixelConfig;
796}
797
798int get_fbo_stencil_bits(bool arbFBOExtension) {
799 GrGLint stencilBits;
800 if (arbFBOExtension) {
801 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
802 GR_GL_STENCIL_ATTACHMENT,
803 GR_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
804 &stencilBits);
805 } else {
806 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &stencilBits);
807 }
808 return stencilBits;
809}
810}
811
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000812GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() {
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000813
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000814 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000815
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000816 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, (GrGLint*)&rtDesc.fRTFBOID);
817 rtDesc.fTexFBOID = rtDesc.fRTFBOID;
818 rtDesc.fMSColorRenderbufferID = 0;
819 rtDesc.fStencilRenderbufferID = 0;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000820
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000821 bool arbFBO = (GR_GL_SUPPORT_DESKTOP && (fGLVersion > 3.0 ||
822 this->hasExtension("GL_ARB_framebuffer_object")));
823
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000824 GrGLIRect viewport;
825 viewport.setFromGLViewport();
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000826 rtDesc.fStencilBits = get_fbo_stencil_bits(arbFBO);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000827
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000828 GR_GL_GetIntegerv(GR_GL_SAMPLES, &rtDesc.fSampleCnt);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000829 GrGLenum fmat = get_fbo_color_format();
830 if (kUnknownGLFormat == fmat) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000831 rtDesc.fConfig = get_implied_color_config(arbFBO);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000832 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000833 rtDesc.fConfig = internal_color_format_to_config(fmat);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000834 }
835
836 // may have to bind a texture to gets its format
837 this->setSpareTextureUnit();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000838
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000839 rtDesc.fOwnIDs = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000840
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000841 return new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000842}
843
bsalomon@google.com5782d712011-01-21 21:03:59 +0000844///////////////////////////////////////////////////////////////////////////////
845
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000846void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000847
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000848 // Build up list of legal stencil formats (though perhaps not supported on
849 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000850
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000851 // these consts are in order of most preferred to least preferred
852 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
853 static const StencilFormat gS8 = {GR_GL_STENCIL_INDEX8, 8, false};
854 static const StencilFormat gS16 = {GR_GL_STENCIL_INDEX16, 16, false};
855 static const StencilFormat gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, true };
856 static const StencilFormat gS4 = {GR_GL_STENCIL_INDEX4, 4, false};
857 static const StencilFormat gS = {GR_GL_STENCIL_INDEX, gUNKNOWN_BITCOUNT, false};
858 static const StencilFormat gDS = {GR_GL_DEPTH_STENCIL, gUNKNOWN_BITCOUNT, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000859
860 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000861 bool supportsPackedDS = fGLVersion >= 3.0f ||
862 this->hasExtension("GL_EXT_packed_depth_stencil") ||
863 this->hasExtension("GL_ARB_framebuffer_object");
864
865 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
866 // require FBO support we can expect these are legal formats and don't
867 // check. These also all support the unsized GL_STENCIL_INDEX.
868 fStencilFormats.push_back() = gS8;
869 fStencilFormats.push_back() = gS16;
870 if (supportsPackedDS) {
871 fStencilFormats.push_back() = gD24S8;
872 }
873 fStencilFormats.push_back() = gS4;
874 if (supportsPackedDS) {
875 fStencilFormats.push_back() = gDS;
876 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000877 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000878 // ES2 has STENCIL_INDEX8 without extensions.
879 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
880 // introduces tokens for S1 thu S8 but there are separate extensions
881 // that make them legal (GL_OES_stencil1, ...).
882 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
883 // ES doesn't support using the unsized formats.
884
885 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
886 fStencilFormats.push_back() = gS8;
887 }
888 //fStencilFormats.push_back() = gS16;
889 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
890 fStencilFormats.push_back() = gD24S8;
891 }
892 if (this->hasExtension("GL_OES_stencil4")) {
893 fStencilFormats.push_back() = gS4;
894 }
895 // we require some stencil format.
896 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000897 }
898}
899
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000900////////////////////////////////////////////////////////////////////////////////
901
902void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
903 GrGLenum internalFormat,
904 const void* data,
905 size_t rowBytes) {
906 // we assume the texture is bound;
907 if (!rowBytes) {
908 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
909 }
910
911 // in case we need a temporary, trimmed copy of the src pixels
912 SkAutoSMalloc<128 * 128> tempStorage;
913
914 /*
915 * check whether to allocate a temporary buffer for flipping y or
916 * because our data has extra bytes past each row. If so, we need
917 * to trim those off here, since GL ES doesn't let us specify
918 * GL_UNPACK_ROW_LENGTH.
919 */
920 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
921 if (GR_GL_SUPPORT_DESKTOP && !flipY) {
922 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
923 GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
924 rowBytes / desc.fUploadByteCount));
925 }
926 } else {
927 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
928 if (data && (trimRowBytes < rowBytes || flipY)) {
929 // copy the data into our new storage, skipping the trailing bytes
930 size_t trimSize = desc.fContentHeight * trimRowBytes;
931 const char* src = (const char*)data;
932 if (flipY) {
933 src += (desc.fContentHeight - 1) * rowBytes;
934 }
935 char* dst = (char*)tempStorage.realloc(trimSize);
936 for (int y = 0; y < desc.fContentHeight; y++) {
937 memcpy(dst, src, trimRowBytes);
938 if (flipY) {
939 src -= rowBytes;
940 } else {
941 src += rowBytes;
942 }
943 dst += trimRowBytes;
944 }
945 // now point data to our trimmed version
946 data = tempStorage.get();
947 rowBytes = trimRowBytes;
948 }
949 }
950
951 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
952 if (kIndex_8_GrPixelConfig == desc.fFormat &&
953 supports8BitPalette()) {
954 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
955 GrAssert(desc.fContentWidth == desc.fAllocWidth);
956 GrAssert(desc.fContentHeight == desc.fAllocHeight);
957 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
958 kGrColorTableSize;
959 GR_GL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
960 desc.fAllocWidth, desc.fAllocHeight,
961 0, imageSize, data));
962 GrGLRestoreResetRowLength();
963 } else {
964 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
965 desc.fAllocHeight != desc.fContentHeight)) {
966 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
967 desc.fAllocWidth, desc.fAllocHeight,
968 0, desc.fUploadFormat, desc.fUploadType, NULL));
969 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
970 desc.fContentHeight, desc.fUploadFormat,
971 desc.fUploadType, data));
972 GrGLRestoreResetRowLength();
973
974 int extraW = desc.fAllocWidth - desc.fContentWidth;
975 int extraH = desc.fAllocHeight - desc.fContentHeight;
976 int maxTexels = extraW * extraH;
977 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
978 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
979
980 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
981
982 // rowBytes is actual stride between rows in data
983 // rowDataBytes is the actual amount of non-pad data in a row
984 // and the stride used for uploading extraH rows.
985 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
986 if (extraH) {
987 uint8_t* lastRowStart = (uint8_t*) data +
988 (desc.fContentHeight - 1) * rowBytes;
989 uint8_t* extraRowStart = (uint8_t*)texels.get();
990
991 for (int i = 0; i < extraH; ++i) {
992 memcpy(extraRowStart, lastRowStart, rowDataBytes);
993 extraRowStart += rowDataBytes;
994 }
995 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, desc.fContentHeight,
996 desc.fContentWidth, extraH,
997 desc.fUploadFormat, desc.fUploadType,
998 texels.get()));
999 }
1000 if (extraW) {
1001 uint8_t* edgeTexel = (uint8_t*)data +
1002 rowDataBytes - desc.fUploadByteCount;
1003 uint8_t* extraTexel = (uint8_t*)texels.get();
1004 for (int j = 0; j < desc.fContentHeight; ++j) {
1005 for (int i = 0; i < extraW; ++i) {
1006 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
1007 extraTexel += desc.fUploadByteCount;
1008 }
1009 edgeTexel += rowBytes;
1010 }
1011 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth, 0,
1012 extraW, desc.fContentHeight,
1013 desc.fUploadFormat, desc.fUploadType,
1014 texels.get()));
1015 }
1016 if (extraW && extraH) {
1017 uint8_t* cornerTexel = (uint8_t*)data +
1018 desc.fContentHeight * rowBytes -
1019 desc.fUploadByteCount;
1020 uint8_t* extraTexel = (uint8_t*)texels.get();
1021 for (int i = 0; i < extraW*extraH; ++i) {
1022 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
1023 extraTexel += desc.fUploadByteCount;
1024 }
1025 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
1026 desc.fContentHeight, extraW, extraH,
1027 desc.fUploadFormat, desc.fUploadType,
1028 texels.get()));
1029 }
1030
1031 } else {
1032 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1033 desc.fAllocWidth, desc.fAllocHeight, 0,
1034 desc.fUploadFormat, desc.fUploadType, data));
1035 GrGLRestoreResetRowLength();
1036 }
1037 }
1038}
1039
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001040// good to set a break-point here to know when createTexture fails
1041static GrTexture* return_null_texture() {
1042// GrAssert(!"null texture");
1043 return NULL;
1044}
1045
1046#if GR_DEBUG
1047static size_t as_size_t(int x) {
1048 return x;
1049}
1050#endif
1051
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001052GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001053 const void* srcData,
1054 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001055
1056#if GR_COLLECT_STATS
1057 ++fStats.fTextureCreateCnt;
1058#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001059
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001060 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001061 GR_GL_NEAREST,
1062 GR_GL_CLAMP_TO_EDGE,
1063 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001064 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001065
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001066 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001067 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001068 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001069
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001070 glTexDesc.fContentWidth = desc.fWidth;
1071 glTexDesc.fContentHeight = desc.fHeight;
1072 glTexDesc.fAllocWidth = desc.fWidth;
1073 glTexDesc.fAllocHeight = desc.fHeight;
1074 glTexDesc.fFormat = desc.fFormat;
1075 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001076
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001077 glRTDesc.fStencilRenderbufferID = 0;
1078 glRTDesc.fMSColorRenderbufferID = 0;
1079 glRTDesc.fRTFBOID = 0;
1080 glRTDesc.fTexFBOID = 0;
1081 glRTDesc.fOwnIDs = true;
1082 glRTDesc.fConfig = glTexDesc.fFormat;
1083
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001084 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 if (!canBeTexture(desc.fFormat,
1086 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001087 &glTexDesc.fUploadFormat,
1088 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 return return_null_texture();
1090 }
1091
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001092 // We keep GrRenderTargets in GL's normal orientation so that they
1093 // can be drawn to by the outside world without the client having
1094 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001095 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001096 GrGLTexture::kTopDown_Orientation;
1097
reed@google.comac10a2d2010-12-22 21:39:39 +00001098 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001099 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001100 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001101 GrPrintf("AA RT requested but not supported on this platform.");
1102 }
1103
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001104 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001105
reed@google.comac10a2d2010-12-22 21:39:39 +00001106 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001107 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001108 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1109 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001110 }
1111
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001112 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
1113 glTexDesc.fAllocWidth);
1114 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
1115 glTexDesc.fAllocHeight);
1116 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1117 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001118 return return_null_texture();
1119 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001120 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001121 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1122 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1123 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1124 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001125 return return_null_texture();
1126 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001127 }
1128
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001129 GR_GL(GenTextures(1, &glTexDesc.fTextureID));
1130 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001131 return return_null_texture();
1132 }
1133
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001134 this->setSpareTextureUnit();
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001135 GR_GL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001136 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1137 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001138 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001139 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1140 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001141 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001142 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1143 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001144 DEFAULT_PARAMS.fWrapS));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001145 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1146 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001147 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001148
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001149 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001150
1151 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001152 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001153#if GR_COLLECT_STATS
1154 ++fStats.fRenderTargetCreateCnt;
1155#endif
1156 bool failed = true;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001157 GrGLenum status;
1158 GrGLint err;
reed@google.comac10a2d2010-12-22 21:39:39 +00001159
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001160 GR_GL(GenFramebuffers(1, &glRTDesc.fTexFBOID));
1161 GrAssert(glRTDesc.fTexFBOID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001162
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001163 // If we are using multisampling and we will create two FBOS We render
bsalomon@google.comd1e433532011-03-21 21:38:40 +00001164 // to one and then resolve to the texture bound to the other.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001165 if (glRTDesc.fSampleCnt > 0 && kNone_MSFBO != fMSFBOType) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001166 GR_GL(GenFramebuffers(1, &glRTDesc.fRTFBOID));
1167 GrAssert(0 != glRTDesc.fRTFBOID);
1168 GR_GL(GenRenderbuffers(1, &glRTDesc.fMSColorRenderbufferID));
1169 GrAssert(0 != glRTDesc.fMSColorRenderbufferID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001170 if (!fboInternalFormat(desc.fFormat, &msColorRenderbufferFormat)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001171 GR_GL(DeleteRenderbuffers(1, &glRTDesc.fMSColorRenderbufferID));
1172 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
1173 GR_GL(DeleteFramebuffers(1, &glRTDesc.fTexFBOID));
1174 GR_GL(DeleteFramebuffers(1, &glRTDesc.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001175 return return_null_texture();
1176 }
1177 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001178 glRTDesc.fRTFBOID = glRTDesc.fTexFBOID;
reed@google.comac10a2d2010-12-22 21:39:39 +00001179 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001180 if (!(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001181 GR_GL(GenRenderbuffers(1, &glRTDesc.fStencilRenderbufferID));
1182 GrAssert(0 != glRTDesc.fStencilRenderbufferID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001183 }
1184
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001185 // someone suggested that some systems might require
bsalomon@google.com316f99232011-01-13 21:28:12 +00001186 // unbinding the texture before we call FramebufferTexture2D
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001187 // (seems unlikely)
twiz@google.com0f31ca72011-03-18 17:38:11 +00001188 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001189
twiz@google.com0f31ca72011-03-18 17:38:11 +00001190 err = ~GR_GL_NO_ERROR;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00001191
bsalomon@google.comfe676522011-06-17 18:12:21 +00001192 int stencilFmtCnt;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001193 if (glRTDesc.fStencilRenderbufferID) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001194 stencilFmtCnt = fStencilFormats.count();
bsalomon@google.comfe676522011-06-17 18:12:21 +00001195 } else {
1196 stencilFmtCnt = 1; // only 1 attempt when we don't need a stencil
1197 }
1198
1199 for (int i = 0; i < stencilFmtCnt; ++i) {
1200 // we start with the last stencil format that succeeded in hopes
1201 // that we won't go through this loop more than once after the
1202 // first (painful) stencil creation.
1203 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
1204
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001205 if (glRTDesc.fStencilRenderbufferID) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001206 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001207 glRTDesc.fStencilRenderbufferID));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001208 if (glRTDesc.fSampleCnt > 0) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001209 GR_GL_NO_ERR(RenderbufferStorageMultisample(
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001210 GR_GL_RENDERBUFFER,
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001211 glRTDesc.fSampleCnt,
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001212 fStencilFormats[sIdx].fEnum,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001213 glTexDesc.fAllocWidth,
1214 glTexDesc.fAllocHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 } else {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001216 GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001217 fStencilFormats[sIdx].fEnum,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001218 glTexDesc.fAllocWidth,
1219 glTexDesc.fAllocHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +00001220 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001221 err = GrGLGetGLInterface()->fGetError();
1222 if (err != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001223 continue;
1224 }
1225 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001226 if (glRTDesc.fRTFBOID != glRTDesc.fTexFBOID) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001227 GrAssert(glRTDesc.fSampleCnt > 0);
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001228 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001229 glRTDesc.fMSColorRenderbufferID));
twiz@google.com59a190b2011-03-14 21:23:01 +00001230 GR_GL_NO_ERR(RenderbufferStorageMultisample(
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001231 GR_GL_RENDERBUFFER,
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001232 glRTDesc.fSampleCnt,
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 msColorRenderbufferFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001234 glTexDesc.fAllocWidth,
1235 glTexDesc.fAllocHeight));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001236 err = GrGLGetGLInterface()->fGetError();
1237 if (err != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 continue;
1239 }
1240 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001241 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRTDesc.fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001242
1243#if GR_COLLECT_STATS
1244 ++fStats.fRenderTargetChngCnt;
1245#endif
bsalomon@google.comd1e433532011-03-21 21:38:40 +00001246 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1247 GR_GL_COLOR_ATTACHMENT0,
1248 GR_GL_TEXTURE_2D,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001249 glTexDesc.fTextureID, 0));
1250 if (glRTDesc.fRTFBOID != glRTDesc.fTexFBOID) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001251 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1252 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 continue;
1254 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001255 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRTDesc.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 #if GR_COLLECT_STATS
1257 ++fStats.fRenderTargetChngCnt;
1258 #endif
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001259 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1260 GR_GL_COLOR_ATTACHMENT0,
1261 GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001262 glRTDesc.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001263
1264 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001265 if (glRTDesc.fStencilRenderbufferID) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 // bind the stencil to rt fbo if present, othewise the tex fbo
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001267 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1268 GR_GL_STENCIL_ATTACHMENT,
1269 GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001270 glRTDesc.fStencilRenderbufferID));
bsalomon@google.com9283b582011-04-08 19:00:04 +00001271 // if it is a packed format bind to depth also, otherwise
1272 // we may get an unsupported fbo completeness result
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001273 if (fStencilFormats[sIdx].fPacked) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001274 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1275 GR_GL_DEPTH_ATTACHMENT,
1276 GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001277 glRTDesc.fStencilRenderbufferID));
bsalomon@google.com9283b582011-04-08 19:00:04 +00001278 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 }
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001280 status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
reed@google.comac10a2d2010-12-22 21:39:39 +00001281
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001282 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001283 // undo the depth bind
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001284 if (glRTDesc.fStencilRenderbufferID &&
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001285 fStencilFormats[sIdx].fPacked) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001286 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001287 GR_GL_DEPTH_ATTACHMENT,
1288 GR_GL_RENDERBUFFER, 0));
twiz@google.comb65e0cb2011-03-18 20:41:44 +00001289 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 continue;
1291 }
1292 // we're successful!
1293 failed = false;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001294 if (glRTDesc.fStencilRenderbufferID) {
bsalomon@google.comfe676522011-06-17 18:12:21 +00001295 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001296 if (gUNKNOWN_BITCOUNT == fStencilFormats[sIdx].fBits) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001297 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, (GrGLint*)&glRTDesc.fStencilBits);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001298 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001299 glRTDesc.fStencilBits = fStencilFormats[sIdx].fBits;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001300 }
1301 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 break;
1303 }
1304 if (failed) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001305 if (glRTDesc.fStencilRenderbufferID) {
1306 GR_GL(DeleteRenderbuffers(1, &glRTDesc.fStencilRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001308 if (glRTDesc.fMSColorRenderbufferID) {
1309 GR_GL(DeleteRenderbuffers(1, &glRTDesc.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001311 if (glRTDesc.fRTFBOID != glRTDesc.fTexFBOID) {
1312 GR_GL(DeleteFramebuffers(1, &glRTDesc.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001314 if (glRTDesc.fTexFBOID) {
1315 GR_GL(DeleteFramebuffers(1, &glRTDesc.fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001317 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001318 return return_null_texture();
1319 }
1320 }
1321#ifdef TRACE_TEXTURE_CREATION
1322 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1323 tex->fTextureID, width, height, tex->fUploadByteCount);
1324#endif
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001325 if (renderTarget) {
1326 GrGLTexture* tex = new GrGLTexture(this, glTexDesc,
1327 glRTDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 GrRenderTarget* rt = tex->asRenderTarget();
1329 // We've messed with FBO state but may not have set the correct viewport
1330 // so just dirty the rendertarget state to force a resend.
1331 fHWDrawState.fRenderTarget = NULL;
1332
1333 // clear the new stencil buffer if we have one
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001334 if (!(desc.fFlags & kNoStencil_GrTextureFlagBit)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 GrRenderTarget* rtSave = fCurrDrawState.fRenderTarget;
1336 fCurrDrawState.fRenderTarget = rt;
bsalomon@google.com398109c2011-04-14 18:40:27 +00001337 this->clearStencil(0, ~0);
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 fCurrDrawState.fRenderTarget = rtSave;
1339 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001340 return tex;
1341 } else {
1342 return new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001343 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001344}
1345
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001346////////////////////////////////////////////////////////////////////////////////
1347
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001348GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001349 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 GR_GL(GenBuffers(1, &id));
1351 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001352 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001353 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 GrGLClearErr();
1355 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001356 GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1357 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1358 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001359 GR_GL(DeleteBuffers(1, &id));
1360 // deleting bound buffer does implicit bind to 0
1361 fHWGeometryState.fVertexBuffer = NULL;
1362 return NULL;
1363 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001364 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 size, dynamic);
1366 fHWGeometryState.fVertexBuffer = vertexBuffer;
1367 return vertexBuffer;
1368 }
1369 return NULL;
1370}
1371
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001372GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001373 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001374 GR_GL(GenBuffers(1, &id));
1375 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001376 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 GrGLClearErr();
1378 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001379 GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1380 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1381 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 GR_GL(DeleteBuffers(1, &id));
1383 // deleting bound buffer does implicit bind to 0
1384 fHWGeometryState.fIndexBuffer = NULL;
1385 return NULL;
1386 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001387 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001388 size, dynamic);
1389 fHWGeometryState.fIndexBuffer = indexBuffer;
1390 return indexBuffer;
1391 }
1392 return NULL;
1393}
1394
reed@google.comac10a2d2010-12-22 21:39:39 +00001395void GrGpuGL::flushScissor(const GrIRect* rect) {
1396 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001397 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001398 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001399
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001400 GrGLIRect scissor;
1401 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001402 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001403 rect->width(), rect->height());
1404 if (scissor.contains(vp)) {
1405 rect = NULL;
1406 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001407 }
1408
1409 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001410 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001411 scissor.pushToGLScissor();
reed@google.comac10a2d2010-12-22 21:39:39 +00001412 fHWBounds.fScissorRect = scissor;
1413 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001414 if (!fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001415 GR_GL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001416 fHWBounds.fScissorEnabled = true;
1417 }
1418 } else {
1419 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001420 GR_GL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001421 fHWBounds.fScissorEnabled = false;
1422 }
1423 }
1424}
1425
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001426void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001427 if (NULL == fCurrDrawState.fRenderTarget) {
1428 return;
1429 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001430 GrIRect r;
1431 if (NULL != rect) {
1432 // flushScissor expects rect to be clipped to the target.
1433 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001434 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1435 fCurrDrawState.fRenderTarget->height());
1436 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001437 rect = &r;
1438 } else {
1439 return;
1440 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001441 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001442 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001443 this->flushScissor(rect);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001444 GR_GL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001445 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
reed@google.comac10a2d2010-12-22 21:39:39 +00001446 GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
1447 GrColorUnpackG(color)/255.f,
1448 GrColorUnpackB(color)/255.f,
1449 GrColorUnpackA(color)/255.f));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001450 GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001451}
1452
bsalomon@google.com398109c2011-04-14 18:40:27 +00001453void GrGpuGL::clearStencil(uint32_t value, uint32_t mask) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001454 if (NULL == fCurrDrawState.fRenderTarget) {
1455 return;
1456 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001457
1458 this->flushRenderTarget(&GrIRect::EmptyIRect());
1459
reed@google.comac10a2d2010-12-22 21:39:39 +00001460 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001461 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001462 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 }
1464 GR_GL(StencilMask(mask));
1465 GR_GL(ClearStencil(value));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001466 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001467 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001468}
1469
bsalomon@google.com398109c2011-04-14 18:40:27 +00001470void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001471 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001472#if 0
twiz@google.com0f31ca72011-03-18 17:38:11 +00001473 GrGLint stencilBitCount = fCurrDrawState.fRenderTarget->stencilBits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001474 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001475 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001476#else
1477 // we could just clear the clip bit but when we go through
1478 // angle a partial stencil mask will cause clears to be
1479 // turned into draws. Our contract on GrDrawTarget says that
1480 // changing the clip between stencil passes may or may not
1481 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001482 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001483#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001484 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.comd302f142011-03-03 13:54:13 +00001485 flushScissor(&rect);
1486 GR_GL(StencilMask(clipStencilMask));
1487 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001488 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001489 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001490}
1491
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001492void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001493 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001494}
1495
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001496bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1497 int left, int top, int width, int height,
1498 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001499 GrGLenum internalFormat; // we don't use this for glReadPixels
1500 GrGLenum format;
1501 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1503 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001504 }
1505 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1506 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1507 switch (tgt->getResolveType()) {
1508 case GrGLRenderTarget::kCantResolve_ResolveType:
1509 return false;
1510 case GrGLRenderTarget::kAutoResolves_ResolveType:
1511 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1512 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001513 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001514 break;
1515 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001516 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001517 // we don't track the state of the READ FBO ID.
1518 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
1519 break;
1520 default:
1521 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001522 }
1523
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001524 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001525
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001526 // the read rect is viewport-relative
1527 GrGLIRect readRect;
1528 readRect.setRelativeTo(glvp, left, top, width, height);
1529 GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001530 readRect.fWidth, readRect.fHeight,
bsalomon@google.com316f99232011-01-13 21:28:12 +00001531 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001532
1533 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1534 // API presents top-to-bottom
1535 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001536 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001537 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001538 void* tmp = rowStorage.get();
1539
1540 const int halfY = height >> 1;
1541 char* top = reinterpret_cast<char*>(buffer);
1542 char* bottom = top + (height - 1) * stride;
1543 for (int y = 0; y < halfY; y++) {
1544 memcpy(tmp, top, stride);
1545 memcpy(top, bottom, stride);
1546 memcpy(bottom, tmp, stride);
1547 top += stride;
1548 bottom -= stride;
1549 }
1550 }
1551 return true;
1552}
1553
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001554void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001555
1556 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1557
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001558 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001559 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001560 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001561 #if GR_COLLECT_STATS
1562 ++fStats.fRenderTargetChngCnt;
1563 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001564 #if GR_DEBUG
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001565 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1566 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001567 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001568 }
1569 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001570 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001571 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001572 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001573 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001574 vp.pushToGLViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001575 fHWBounds.fViewportRect = vp;
1576 }
1577 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001578 if (NULL == bound || !bound->isEmpty()) {
1579 rt->flagAsNeedingResolve(bound);
1580 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001581}
1582
twiz@google.com0f31ca72011-03-18 17:38:11 +00001583GrGLenum gPrimitiveType2GLMode[] = {
1584 GR_GL_TRIANGLES,
1585 GR_GL_TRIANGLE_STRIP,
1586 GR_GL_TRIANGLE_FAN,
1587 GR_GL_POINTS,
1588 GR_GL_LINES,
1589 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001590};
1591
bsalomon@google.comd302f142011-03-03 13:54:13 +00001592#define SWAP_PER_DRAW 0
1593
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001594#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001595 #if GR_MAC_BUILD
1596 #include <AGL/agl.h>
1597 #elif GR_WIN32_BUILD
1598 void SwapBuf() {
1599 DWORD procID = GetCurrentProcessId();
1600 HWND hwnd = GetTopWindow(GetDesktopWindow());
1601 while(hwnd) {
1602 DWORD wndProcID = 0;
1603 GetWindowThreadProcessId(hwnd, &wndProcID);
1604 if(wndProcID == procID) {
1605 SwapBuffers(GetDC(hwnd));
1606 }
1607 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1608 }
1609 }
1610 #endif
1611#endif
1612
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001613void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1614 uint32_t startVertex,
1615 uint32_t startIndex,
1616 uint32_t vertexCount,
1617 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001618 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1619
twiz@google.com0f31ca72011-03-18 17:38:11 +00001620 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001621
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001622 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1623 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1624
1625 // our setupGeometry better have adjusted this to zero since
1626 // DrawElements always draws from the begining of the arrays for idx 0.
1627 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001628
1629 GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
twiz@google.com0f31ca72011-03-18 17:38:11 +00001630 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001631#if SWAP_PER_DRAW
1632 glFlush();
1633 #if GR_MAC_BUILD
1634 aglSwapBuffers(aglGetCurrentContext());
1635 int set_a_break_pt_here = 9;
1636 aglSwapBuffers(aglGetCurrentContext());
1637 #elif GR_WIN32_BUILD
1638 SwapBuf();
1639 int set_a_break_pt_here = 9;
1640 SwapBuf();
1641 #endif
1642#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001643}
1644
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001645void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1646 uint32_t startVertex,
1647 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001648 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1649
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001650 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1651
1652 // our setupGeometry better have adjusted this to zero.
1653 // DrawElements doesn't take an offset so we always adjus the startVertex.
1654 GrAssert(0 == startVertex);
1655
1656 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1657 // account for startVertex in the DrawElements case. So we always
1658 // rely on setupGeometry to have accounted for startVertex.
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001660#if SWAP_PER_DRAW
1661 glFlush();
1662 #if GR_MAC_BUILD
1663 aglSwapBuffers(aglGetCurrentContext());
1664 int set_a_break_pt_here = 9;
1665 aglSwapBuffers(aglGetCurrentContext());
1666 #elif GR_WIN32_BUILD
1667 SwapBuf();
1668 int set_a_break_pt_here = 9;
1669 SwapBuf();
1670 #endif
1671#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001672}
1673
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001674void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001675
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001676 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001677 GrAssert(kNone_MSFBO != fMSFBOType);
1678 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001679 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001680 rt->renderFBOID()));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001681 GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001682 rt->textureFBOID()));
1683 #if GR_COLLECT_STATS
1684 ++fStats.fRenderTargetChngCnt;
1685 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001686 // make sure we go through flushRenderTarget() since we've modified
1687 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001688 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001689 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001690 const GrIRect dirtyRect = rt->getResolveRect();
1691 GrGLIRect r;
1692 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1693 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001694
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001695 if (kAppleES_MSFBO == fMSFBOType) {
1696 // Apple's extension uses the scissor as the blit bounds.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001697 GR_GL(Enable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001698 GR_GL(Scissor(r.fLeft, r.fBottom,
1699 r.fWidth, r.fHeight));
twiz@google.com59a190b2011-03-14 21:23:01 +00001700 GR_GL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001701 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 fHWBounds.fScissorEnabled = true;
1703 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001704 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001705 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001706 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1707 flushScissor(NULL);
1708 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001709 int right = r.fLeft + r.fWidth;
1710 int top = r.fBottom + r.fHeight;
1711 GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1712 r.fLeft, r.fBottom, right, top,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001713 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001715 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001716 }
1717}
1718
twiz@google.com0f31ca72011-03-18 17:38:11 +00001719static const GrGLenum grToGLStencilFunc[] = {
1720 GR_GL_ALWAYS, // kAlways_StencilFunc
1721 GR_GL_NEVER, // kNever_StencilFunc
1722 GR_GL_GREATER, // kGreater_StencilFunc
1723 GR_GL_GEQUAL, // kGEqual_StencilFunc
1724 GR_GL_LESS, // kLess_StencilFunc
1725 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1726 GR_GL_EQUAL, // kEqual_StencilFunc,
1727 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001728};
1729GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1730GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1731GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1732GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1733GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1734GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1735GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1736GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1737GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1738
twiz@google.com0f31ca72011-03-18 17:38:11 +00001739static const GrGLenum grToGLStencilOp[] = {
1740 GR_GL_KEEP, // kKeep_StencilOp
1741 GR_GL_REPLACE, // kReplace_StencilOp
1742 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1743 GR_GL_INCR, // kIncClamp_StencilOp
1744 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1745 GR_GL_DECR, // kDecClamp_StencilOp
1746 GR_GL_ZERO, // kZero_StencilOp
1747 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001748};
1749GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1750GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1751GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1752GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1753GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1754GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1755GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1756GR_STATIC_ASSERT(6 == kZero_StencilOp);
1757GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1758
reed@google.comac10a2d2010-12-22 21:39:39 +00001759void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001760 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001761
1762 // use stencil for clipping if clipping is enabled and the clip
1763 // has been written into the stencil.
1764 bool stencilClip = fClipState.fClipInStencil &&
1765 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001766 bool stencilChange = fHWStencilClip != stencilClip ||
1767 fHWDrawState.fStencilSettings != *settings ||
1768 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1769 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001770
1771 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001772
bsalomon@google.comd302f142011-03-03 13:54:13 +00001773 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1774 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001775
bsalomon@google.comd302f142011-03-03 13:54:13 +00001776 if (settings->isDisabled()) {
1777 if (stencilClip) {
1778 settings = &gClipStencilSettings;
1779 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001781
1782 if (settings->isDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001783 GR_GL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001784 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001785 GR_GL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001786 #if GR_DEBUG
1787 if (!fStencilWrapOpsSupport) {
1788 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1789 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1790 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1791 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1792 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1793 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1794 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1795 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1796 }
1797 #endif
1798 int stencilBits = fCurrDrawState.fRenderTarget->stencilBits();
1799 GrAssert(stencilBits ||
1800 (GrStencilSettings::gDisabled ==
1801 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001802 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1803 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001804
1805 unsigned int frontRef = settings->fFrontFuncRef;
1806 unsigned int frontMask = settings->fFrontFuncMask;
1807 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001808 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001809
1810 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1811
1812 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1813 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1814 } else {
1815 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1816
1817 ConvertStencilFuncAndMask(settings->fFrontFunc,
1818 stencilClip,
1819 clipStencilMask,
1820 userStencilMask,
1821 &frontRef,
1822 &frontMask);
1823 frontWriteMask &= userStencilMask;
1824 }
1825 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001826 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001827 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001828 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001829 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001830 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001831 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001832 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001833 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001834 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835
1836 unsigned int backRef = settings->fBackFuncRef;
1837 unsigned int backMask = settings->fBackFuncMask;
1838 unsigned int backWriteMask = settings->fBackWriteMask;
1839
1840
1841 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1842 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1843 backFunc = grToGLStencilFunc[settings->fBackFunc];
1844 } else {
1845 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1846 ConvertStencilFuncAndMask(settings->fBackFunc,
1847 stencilClip,
1848 clipStencilMask,
1849 userStencilMask,
1850 &backRef,
1851 &backMask);
1852 backWriteMask &= userStencilMask;
1853 }
1854
twiz@google.com0f31ca72011-03-18 17:38:11 +00001855 GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask));
1856 GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1857 GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask));
1858 GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1859 GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001860 grToGLStencilOp[settings->fFrontPassOp],
1861 grToGLStencilOp[settings->fFrontPassOp]));
1862
twiz@google.com0f31ca72011-03-18 17:38:11 +00001863 GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864 grToGLStencilOp[settings->fBackPassOp],
1865 grToGLStencilOp[settings->fBackPassOp]));
1866 } else {
1867 GR_GL(StencilFunc(frontFunc, frontRef, frontMask));
1868 GR_GL(StencilMask(frontWriteMask));
1869 GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
1870 grToGLStencilOp[settings->fFrontPassOp],
1871 grToGLStencilOp[settings->fFrontPassOp]));
1872 }
1873 }
1874 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001875 fHWStencilClip = stencilClip;
1876 }
1877}
1878
bsalomon@google.com0650e812011-04-08 18:07:53 +00001879bool GrGpuGL::useSmoothLines() {
1880 // there is a conflict between using smooth lines and our use of
1881 // premultiplied alpha. Smooth lines tweak the incoming alpha value
1882 // but not in a premul-alpha way. So we only use them when our alpha
1883 // is 0xff.
1884
1885 // TODO: write a smarter line frag shader.
1886
1887 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
1888 canDisableBlend();
1889}
1890
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001891void GrGpuGL::flushAAState(GrPrimitiveType type) {
1892 if (GR_GL_SUPPORT_DESKTOP) {
1893 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1894 // smooth lines.
1895
1896 // we prefer smooth lines over multisampled lines
1897 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001898 if (GrIsPrimTypeLines(type)) {
1899 bool smooth = useSmoothLines();
1900 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001901 GR_GL(Enable(GR_GL_LINE_SMOOTH));
1902 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001903 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001904 GR_GL(Disable(GR_GL_LINE_SMOOTH));
1905 fHWAAState.fSmoothLineEnabled = false;
1906 }
1907 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1908 fHWAAState.fMSAAEnabled) {
1909 GR_GL(Disable(GR_GL_MULTISAMPLE));
1910 fHWAAState.fMSAAEnabled = false;
1911 }
1912 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1913 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
1914 fHWAAState.fMSAAEnabled) {
1915 if (fHWAAState.fMSAAEnabled) {
1916 GR_GL(Disable(GR_GL_MULTISAMPLE));
1917 fHWAAState.fMSAAEnabled = false;
1918 } else {
1919 GR_GL(Enable(GR_GL_MULTISAMPLE));
1920 fHWAAState.fMSAAEnabled = true;
1921 }
1922 }
1923 }
1924}
1925
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001926void GrGpuGL::flushBlend(GrPrimitiveType type,
1927 GrBlendCoeff srcCoeff,
1928 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001929 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
1930 if (fHWBlendDisabled) {
1931 GR_GL(Enable(GR_GL_BLEND));
1932 fHWBlendDisabled = false;
1933 }
1934 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1935 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
1936 GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1937 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
1938 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1939 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1940 }
1941 } else {
1942 bool blendOff = canDisableBlend();
1943 if (fHWBlendDisabled != blendOff) {
1944 if (blendOff) {
1945 GR_GL(Disable(GR_GL_BLEND));
1946 } else {
1947 GR_GL(Enable(GR_GL_BLEND));
1948 }
1949 fHWBlendDisabled = blendOff;
1950 }
1951 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001952 if (fHWDrawState.fSrcBlend != srcCoeff ||
1953 fHWDrawState.fDstBlend != dstCoeff) {
1954 GR_GL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1955 gXfermodeCoeff2Blend[dstCoeff]));
1956 fHWDrawState.fSrcBlend = srcCoeff;
1957 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001958 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001959 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1960 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001961 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1962
1963 float c[] = {
1964 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1965 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1966 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1967 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1968 };
1969 GR_GL(BlendColor(c[0], c[1], c[2], c[3]));
1970 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1971 }
1972 }
1973 }
1974}
1975
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001976static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1977 switch (filter) {
1978 case GrSamplerState::kBilinear_Filter:
1979 case GrSamplerState::k4x4Downsample_Filter:
1980 return GR_GL_LINEAR;
1981 case GrSamplerState::kNearest_Filter:
1982 case GrSamplerState::kConvolution_Filter:
1983 return GR_GL_NEAREST;
1984 default:
1985 GrAssert(!"Unknown filter type");
1986 return GR_GL_LINEAR;
1987 }
1988}
1989
bsalomon@google.comffca4002011-02-22 20:34:01 +00001990bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001991
1992 // GrGpu::setupClipAndFlushState should have already checked this
1993 // and bailed if not true.
1994 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001995
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001996 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001997 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001998 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001999 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002000
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002001 // true for now, but maybe not with GrEffect.
2002 GrAssert(NULL != nextTexture);
2003 // if we created a rt/tex and rendered to it without using a
2004 // texture and now we're texuring from the rt it will still be
2005 // the last bound texture, but it needs resolving. So keep this
2006 // out of the "last != next" check.
2007 GrGLRenderTarget* texRT =
2008 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2009 if (NULL != texRT) {
2010 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002011 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002012
2013 if (fHWDrawState.fTextures[s] != nextTexture) {
2014 setTextureUnit(s);
2015 GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
2016 #if GR_COLLECT_STATS
2017 ++fStats.fTextureChngCnt;
2018 #endif
2019 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2020 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002021 // The texture matrix has to compensate for texture width/height
2022 // and NPOT-embedded-in-POT
2023 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002024 }
2025
2026 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2027 const GrGLTexture::TexParams& oldTexParams =
2028 nextTexture->getTexParams();
2029 GrGLTexture::TexParams newTexParams;
2030
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002031 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002032
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002033 newTexParams.fWrapS =
2034 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()];
2035 newTexParams.fWrapT =
2036 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()];
2037
2038 if (newTexParams.fFilter != oldTexParams.fFilter) {
2039 setTextureUnit(s);
2040 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2041 GR_GL_TEXTURE_MAG_FILTER,
2042 newTexParams.fFilter));
2043 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2044 GR_GL_TEXTURE_MIN_FILTER,
2045 newTexParams.fFilter));
2046 }
2047 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2048 setTextureUnit(s);
2049 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2050 GR_GL_TEXTURE_WRAP_S,
2051 newTexParams.fWrapS));
2052 }
2053 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2054 setTextureUnit(s);
2055 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2056 GR_GL_TEXTURE_WRAP_T,
2057 newTexParams.fWrapT));
2058 }
2059 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002060 }
2061 }
2062
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002063 GrIRect* rect = NULL;
2064 GrIRect clipBounds;
2065 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2066 fClip.hasConservativeBounds()) {
2067 fClip.getConservativeBounds().roundOut(&clipBounds);
2068 rect = &clipBounds;
2069 }
2070 this->flushRenderTarget(rect);
2071 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002072
reed@google.comac10a2d2010-12-22 21:39:39 +00002073 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2074 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2075 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002076 GR_GL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002077 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002078 GR_GL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002079 }
2080 }
2081
bsalomon@google.comd302f142011-03-03 13:54:13 +00002082 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2083 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002084 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002085 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002086 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002087 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002088 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002089 }
2090 GR_GL(ColorMask(mask, mask, mask, mask));
2091 }
2092
bsalomon@google.comd302f142011-03-03 13:54:13 +00002093 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2094 switch (fCurrDrawState.fDrawFace) {
2095 case kCCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002096 GR_GL(Enable(GR_GL_CULL_FACE));
2097 GR_GL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002098 break;
2099 case kCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002100 GR_GL(Enable(GR_GL_CULL_FACE));
2101 GR_GL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002102 break;
2103 case kBoth_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002104 GR_GL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002105 break;
2106 default:
2107 GrCrash("Unknown draw face.");
2108 }
2109 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2110 }
2111
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002112#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002113 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002114 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002115 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002116 NULL == fCurrDrawState.fRenderTarget ||
2117 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002118 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002119 fCurrDrawState.fRenderTarget);
2120 }
2121#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002122
reed@google.comac10a2d2010-12-22 21:39:39 +00002123 flushStencil();
2124
bsalomon@google.comd302f142011-03-03 13:54:13 +00002125 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002126 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002127 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002128}
2129
2130void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002131 if (fHWGeometryState.fVertexBuffer != buffer) {
2132 fHWGeometryState.fArrayPtrsDirty = true;
2133 fHWGeometryState.fVertexBuffer = buffer;
2134 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002135}
2136
2137void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002138 if (fHWGeometryState.fVertexBuffer == buffer) {
2139 // deleting bound buffer does implied bind to 0
2140 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002141 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002142 }
2143}
2144
2145void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002146 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002147}
2148
2149void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002150 if (fHWGeometryState.fIndexBuffer == buffer) {
2151 // deleting bound buffer does implied bind to 0
2152 fHWGeometryState.fIndexBuffer = NULL;
2153 }
2154}
2155
reed@google.comac10a2d2010-12-22 21:39:39 +00002156void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2157 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002158 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002159 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002160 }
2161 if (fHWDrawState.fRenderTarget == renderTarget) {
2162 fHWDrawState.fRenderTarget = NULL;
2163 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002164}
2165
2166void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002167 for (int s = 0; s < kNumStages; ++s) {
2168 if (fCurrDrawState.fTextures[s] == texture) {
2169 fCurrDrawState.fTextures[s] = NULL;
2170 }
2171 if (fHWDrawState.fTextures[s] == texture) {
2172 // deleting bound texture does implied bind to 0
2173 fHWDrawState.fTextures[s] = NULL;
2174 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002175 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002176}
2177
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002178bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002179 GrGLenum* internalFormat,
2180 GrGLenum* format,
2181 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002182 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002183 case kRGBA_8888_GrPixelConfig:
2184 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002185 *format = GR_GL_32BPP_COLOR_FORMAT;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002186 if (GR_GL_SUPPORT_ES) {
2187 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2188 // format for a BGRA is BGRA not RGBA (as on desktop)
2189 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2190 } else {
2191 *internalFormat = GR_GL_RGBA;
2192 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002193 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002194 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002195 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002196 *format = GR_GL_RGB;
2197 *internalFormat = GR_GL_RGB;
2198 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002199 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002200 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002201 *format = GR_GL_RGBA;
2202 *internalFormat = GR_GL_RGBA;
2203 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002204 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002205 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002206 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002207 *format = GR_GL_PALETTE8_RGBA8;
2208 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002209 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002210 } else {
2211 return false;
2212 }
2213 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002214 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002215 *format = GR_GL_ALPHA;
2216 *internalFormat = GR_GL_ALPHA;
2217 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002218 break;
2219 default:
2220 return false;
2221 }
2222 return true;
2223}
2224
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002225void GrGpuGL::setTextureUnit(int unit) {
2226 GrAssert(unit >= 0 && unit < kNumStages);
2227 if (fActiveTextureUnitIdx != unit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002228 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002229 fActiveTextureUnitIdx = unit;
2230 }
2231}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002232
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002233void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002234 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
2235 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002236 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2237 }
2238}
2239
reed@google.comac10a2d2010-12-22 21:39:39 +00002240/* On ES the internalFormat and format must match for TexImage and we use
2241 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2242 decide the internalFormat. However, on ES internalFormat for
2243 RenderBufferStorage* has to be a specific format (not a base format like
2244 GL_RGBA).
2245 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002246bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002247 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002248 case kRGBA_8888_GrPixelConfig:
2249 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002250 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002251 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002252 return true;
2253 } else {
2254 return false;
2255 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002256 case kRGB_565_GrPixelConfig:
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002257 GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it
2258 // with FBO extension desktop GL has
2259 // no such internal format
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002260 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002261 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002262 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002263 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002264 return true;
2265 default:
2266 return false;
2267 }
2268}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002269
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002270void GrGpuGL::resetDirtyFlags() {
2271 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2272}
2273
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002274void GrGpuGL::setBuffers(bool indexed,
2275 int* extraVertexOffset,
2276 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002277
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002278 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002279
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002280 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2281
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002282 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002283 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002284 case kBuffer_GeometrySrcType:
2285 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002286 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002287 break;
2288 case kArray_GeometrySrcType:
2289 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002290 this->finalizeReservedVertices();
2291 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2292 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002293 break;
2294 default:
2295 vbuf = NULL; // suppress warning
2296 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002297 }
2298
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002299 GrAssert(NULL != vbuf);
2300 GrAssert(!vbuf->isLocked());
2301 if (fHWGeometryState.fVertexBuffer != vbuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002302 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002303 fHWGeometryState.fArrayPtrsDirty = true;
2304 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002305 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002306
2307 if (indexed) {
2308 GrAssert(NULL != extraIndexOffset);
2309
2310 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002311 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002312 case kBuffer_GeometrySrcType:
2313 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002314 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002315 break;
2316 case kArray_GeometrySrcType:
2317 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002318 this->finalizeReservedIndices();
2319 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2320 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002321 break;
2322 default:
2323 ibuf = NULL; // suppress warning
2324 GrCrash("Unknown geometry src type!");
2325 }
2326
2327 GrAssert(NULL != ibuf);
2328 GrAssert(!ibuf->isLocked());
2329 if (fHWGeometryState.fIndexBuffer != ibuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002330 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002331 fHWGeometryState.fIndexBuffer = ibuf;
2332 }
2333 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002334}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002335
2336int GrGpuGL::getMaxEdges() const {
2337 // FIXME: This is a pessimistic estimate based on how many other things
2338 // want to add uniforms. This should be centralized somewhere.
2339 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2340}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002341