blob: eb5a65c888bc0c7b8348df9e150801eff5e8acfa [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
600 GrGLRenderTarget::GLRenderTargetIDs rtIDs;
601 if (isRenderTarget) {
602 rtIDs.fRTFBOID = desc.fPlatformRenderTarget;
603 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
604 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
605 rtIDs.fTexFBOID = desc.fPlatformResolveDestination;
606 } else {
607 GrAssert(!isTexture); // this should have been filtered by GrContext
608 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
609 }
610 } else {
611 rtIDs.fTexFBOID = desc.fPlatformRenderTarget;
612 }
613 // we don't know what the RB ids are without glGets and we don't care
614 // since we aren't responsible for deleting them.
615 rtIDs.fStencilRenderbufferID = 0;
616 rtIDs.fMSColorRenderbufferID = 0;
617
618 rtIDs.fOwnIDs = false;
619 } else {
620 rtIDs.reset();
621 }
622
623 if (isTexture) {
624 GrGLTexture::GLTextureDesc texDesc;
625 GrGLenum dontCare;
626 if (!canBeTexture(desc.fConfig, &dontCare,
627 &texDesc.fUploadFormat,
628 &texDesc.fUploadType)) {
629 return NULL;
630 }
631
632 GrGLTexture::TexParams params;
633
634 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
635 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
636
bsalomon@google.com90405932011-06-17 15:56:55 +0000637 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000638 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
639 texDesc.fStencilBits = desc.fStencilBits;
640 texDesc.fTextureID = desc.fPlatformTexture;
641 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
642 texDesc.fOwnsID = false;
643
644 params.invalidate(); // rather than do glGets.
645
646 return new GrGLTexture(this, texDesc, rtIDs, params);
647 } else {
648 GrGLIRect viewport;
649 viewport.fLeft = 0;
650 viewport.fBottom = 0;
651 viewport.fWidth = desc.fWidth;
652 viewport.fHeight = desc.fHeight;
653
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000654 bool isMSAA = kIsMultisampled_GrPlatformRenderTargetFlagBit &
655 desc.fRenderTargetFlags;
656
657 return new GrGLRenderTarget(this, rtIDs, NULL, desc.fConfig,
658 desc.fStencilBits, isMSAA, viewport, NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000659 }
660}
661
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000662namespace {
663
664static const GrGLenum kUnknownGLFormat = ~0;
665
666GrGLenum get_fbo_color_format() {
667 GrGLint cbType;
668 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
669 GR_GL_COLOR_ATTACHMENT0,
670 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
671 &cbType);
672 GrGLint cbID;
673 GrGLint cbFormat;
674 switch (cbType) {
675 case GR_GL_RENDERBUFFER:
676 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
677 GR_GL_COLOR_ATTACHMENT0,
678 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
679 &cbID);
680 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
681 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
682 GR_GL_RENDERBUFFER_INTERNAL_FORMAT,
683 &cbFormat);
684 return cbFormat;
685 break;
686 case GR_GL_TEXTURE:
687 // ES doesn't have glGetTexLevelParameter
688 if (GR_GL_SUPPORT_DESKTOP) {
689 GrGLint cbLevel;
690 GrGLint cbFace;
691 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
692 GR_GL_COLOR_ATTACHMENT0,
693 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
694 &cbID);
695 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
696 GR_GL_COLOR_ATTACHMENT0,
697 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
698 &cbLevel);
699 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
700 GR_GL_COLOR_ATTACHMENT0,
701 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
702 &cbFace);
703 GrGLenum bind;
704 GrGLenum target;
705 if (cbFace) {
706 bind = GR_GL_TEXTURE_CUBE_MAP;
707 target = cbFace;
708 } else {
709 bind = GR_GL_TEXTURE_2D;
710 target = GR_GL_TEXTURE_2D;
711 }
712 GR_GL(BindTexture(bind, cbID));
713 GR_GL_GetTexLevelParameteriv(target, cbLevel,
714 GR_GL_TEXTURE_INTERNAL_FORMAT, &cbFormat);
715 return cbFormat;
716 } else {
717 return kUnknownGLFormat;
718 }
719 break;
720 default:
721 // we can get here with FBO 0, not a render buffer or a texture
722 return kUnknownGLFormat;
723 }
724}
725
726GrPixelConfig internal_color_format_to_config(GrGLenum iFormat) {
727 switch (iFormat) {
728 case GR_GL_RGB565:
729 return kRGB_565_GrPixelConfig;
730 case GR_GL_RGBA4:
731 return kRGBA_4444_GrPixelConfig;
732 case GR_GL_RGBA8:
733 case GR_GL_SRGB8_ALPHA8:
734 case GR_GL_SRGB_ALPHA:
735 case GR_GL_RGBA:
736 case GR_GL_BGRA:
737 return kRGBA_8888_GrPixelConfig;
738 case GR_GL_RGB8:
739 case GR_GL_SRGB8:
740 case GR_GL_SRGB:
741 return kRGBX_8888_GrPixelConfig;
742 default:
743 // there are many GL formats we don't have enums
744 // for. We should still render to them if the client
745 // asks us.
746 return kUnknown_GrPixelConfig;
747 }
748}
749
750GrPixelConfig get_implied_color_config(bool arbFBOExtension) {
751 GrGLint rSize, bSize, gSize, aSize;
752 if (arbFBOExtension) {
753 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
754 GR_GL_COLOR_ATTACHMENT0,
755 GR_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &rSize);
756 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
757 GR_GL_COLOR_ATTACHMENT0,
758 GR_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &gSize);
759 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
760 GR_GL_COLOR_ATTACHMENT0,
761 GR_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &bSize);
762 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
763 GR_GL_COLOR_ATTACHMENT0,
764 GR_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &aSize);
765 } else {
766 GR_GL_GetIntegerv(GR_GL_RED_BITS, &rSize);
767 GR_GL_GetIntegerv(GR_GL_GREEN_BITS, &gSize);
768 GR_GL_GetIntegerv(GR_GL_BLUE_BITS, &bSize);
769 GR_GL_GetIntegerv(GR_GL_ALPHA_BITS, &aSize);
770 }
771
772 if(8 == rSize && 8 == gSize && 8 == bSize) {
773 if (0 == aSize) {
774 return kRGBX_8888_GrPixelConfig;
775 } else if (8 == aSize) {
776 return kRGBA_8888_GrPixelConfig;
777 }
778 } else if (4 == rSize && 4 == gSize && 4 == bSize && 4 == aSize) {
779 return kRGBA_4444_GrPixelConfig;
780 } else if (5 == rSize && 6 == gSize && 5 == bSize && 0 == aSize) {
781 return kRGB_565_GrPixelConfig;
782 }
783 return kUnknown_GrPixelConfig;
784}
785
786int get_fbo_stencil_bits(bool arbFBOExtension) {
787 GrGLint stencilBits;
788 if (arbFBOExtension) {
789 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
790 GR_GL_STENCIL_ATTACHMENT,
791 GR_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
792 &stencilBits);
793 } else {
794 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &stencilBits);
795 }
796 return stencilBits;
797}
798}
799
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000800GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() {
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000801
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000802 GrGLRenderTarget::GLRenderTargetIDs rtIDs;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000803
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000804 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, (GrGLint*)&rtIDs.fRTFBOID);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000805 rtIDs.fTexFBOID = rtIDs.fRTFBOID;
806 rtIDs.fMSColorRenderbufferID = 0;
807 rtIDs.fStencilRenderbufferID = 0;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000808
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000809 bool arbFBO = (GR_GL_SUPPORT_DESKTOP && (fGLVersion > 3.0 ||
810 this->hasExtension("GL_ARB_framebuffer_object")));
811
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000812 GrGLIRect viewport;
813 viewport.setFromGLViewport();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000814 int stencilBits = get_fbo_stencil_bits(arbFBO);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000815
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000816 GrPixelConfig config;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000817 GrGLint samples;
818 GR_GL_GetIntegerv(GR_GL_SAMPLES, &samples);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000819 GrGLenum fmat = get_fbo_color_format();
820 if (kUnknownGLFormat == fmat) {
821 config = get_implied_color_config(arbFBO);
822 } else {
823 config = internal_color_format_to_config(fmat);
824 }
825
826 // may have to bind a texture to gets its format
827 this->setSpareTextureUnit();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000828
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000829 rtIDs.fOwnIDs = false;
830
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000831 return new GrGLRenderTarget(this, rtIDs, NULL, config, stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000832 (samples > 0), viewport, NULL);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000833}
834
bsalomon@google.com5782d712011-01-21 21:03:59 +0000835///////////////////////////////////////////////////////////////////////////////
836
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000837void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000838
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000839 // Build up list of legal stencil formats (though perhaps not supported on
840 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000841
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000842 // these consts are in order of most preferred to least preferred
843 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
844 static const StencilFormat gS8 = {GR_GL_STENCIL_INDEX8, 8, false};
845 static const StencilFormat gS16 = {GR_GL_STENCIL_INDEX16, 16, false};
846 static const StencilFormat gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, true };
847 static const StencilFormat gS4 = {GR_GL_STENCIL_INDEX4, 4, false};
848 static const StencilFormat gS = {GR_GL_STENCIL_INDEX, gUNKNOWN_BITCOUNT, false};
849 static const StencilFormat gDS = {GR_GL_DEPTH_STENCIL, gUNKNOWN_BITCOUNT, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000850
851 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000852 bool supportsPackedDS = fGLVersion >= 3.0f ||
853 this->hasExtension("GL_EXT_packed_depth_stencil") ||
854 this->hasExtension("GL_ARB_framebuffer_object");
855
856 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
857 // require FBO support we can expect these are legal formats and don't
858 // check. These also all support the unsized GL_STENCIL_INDEX.
859 fStencilFormats.push_back() = gS8;
860 fStencilFormats.push_back() = gS16;
861 if (supportsPackedDS) {
862 fStencilFormats.push_back() = gD24S8;
863 }
864 fStencilFormats.push_back() = gS4;
865 if (supportsPackedDS) {
866 fStencilFormats.push_back() = gDS;
867 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000868 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000869 // ES2 has STENCIL_INDEX8 without extensions.
870 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
871 // introduces tokens for S1 thu S8 but there are separate extensions
872 // that make them legal (GL_OES_stencil1, ...).
873 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
874 // ES doesn't support using the unsized formats.
875
876 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
877 fStencilFormats.push_back() = gS8;
878 }
879 //fStencilFormats.push_back() = gS16;
880 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
881 fStencilFormats.push_back() = gD24S8;
882 }
883 if (this->hasExtension("GL_OES_stencil4")) {
884 fStencilFormats.push_back() = gS4;
885 }
886 // we require some stencil format.
887 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000888 }
889}
890
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000891// good to set a break-point here to know when createTexture fails
892static GrTexture* return_null_texture() {
893// GrAssert(!"null texture");
894 return NULL;
895}
896
897#if GR_DEBUG
898static size_t as_size_t(int x) {
899 return x;
900}
901#endif
902
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000903GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000904 const void* srcData,
905 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000906
907#if GR_COLLECT_STATS
908 ++fStats.fTextureCreateCnt;
909#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000910
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000911 this->setSpareTextureUnit();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000912
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000913 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000914 GR_GL_NEAREST,
915 GR_GL_CLAMP_TO_EDGE,
916 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000917 };
reed@google.com1fcd51e2011-01-05 15:50:27 +0000918
reed@google.comac10a2d2010-12-22 21:39:39 +0000919 GrGLTexture::GLTextureDesc glDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000920 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +0000921
922 glDesc.fContentWidth = desc.fWidth;
923 glDesc.fContentHeight = desc.fHeight;
924 glDesc.fAllocWidth = desc.fWidth;
925 glDesc.fAllocHeight = desc.fHeight;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000926 glDesc.fStencilBits = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000927 glDesc.fFormat = desc.fFormat;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000928 glDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000929
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000930 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000931 if (!canBeTexture(desc.fFormat,
932 &internalFormat,
933 &glDesc.fUploadFormat,
934 &glDesc.fUploadType)) {
935 return return_null_texture();
936 }
937
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000938 // We keep GrRenderTargets in GL's normal orientation so that they
939 // can be drawn to by the outside world without the client having
940 // to render upside down.
941 glDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
942 GrGLTexture::kTopDown_Orientation;
943
reed@google.comac10a2d2010-12-22 21:39:39 +0000944 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000945 GrGLint samples = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000946 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000947 GrPrintf("AA RT requested but not supported on this platform.");
948 }
949
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000950 glDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +0000951
reed@google.com5e762232011-04-04 18:15:49 +0000952 // in case we need a temporary, trimmed copy of the src pixels
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000953 SkAutoSMalloc<128 * 128> tempStorage;
reed@google.com5e762232011-04-04 18:15:49 +0000954
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000955 if (!rowBytes) {
956 rowBytes = glDesc.fUploadByteCount * desc.fWidth;
957 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000958 /*
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000959 * check whether to allocate a temporary buffer for flipping y or
960 * because our srcData has extra bytes past each row. If so, we need
961 * to trim those off here, since GL ES doesn't let us specify
962 * GL_UNPACK_ROW_LENGTH.
reed@google.comac10a2d2010-12-22 21:39:39 +0000963 */
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000964 bool flipY = GrGLTexture::kBottomUp_Orientation == glDesc.fOrientation;
965 if (GR_GL_SUPPORT_DESKTOP && !flipY) {
966 if (srcData && rowBytes != desc.fWidth * glDesc.fUploadByteCount) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000967 GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
968 rowBytes / glDesc.fUploadByteCount));
reed@google.comac10a2d2010-12-22 21:39:39 +0000969 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000970 } else {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000971 size_t trimRowBytes = desc.fWidth * glDesc.fUploadByteCount;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000972 if (srcData && (trimRowBytes < rowBytes || flipY)) {
reed@google.com5e762232011-04-04 18:15:49 +0000973 // copy the data into our new storage, skipping the trailing bytes
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000974 size_t trimSize = desc.fHeight * trimRowBytes;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000975 const char* src = (const char*)srcData;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000976 if (flipY) {
977 src += (desc.fHeight - 1) * rowBytes;
978 }
979 char* dst = (char*)tempStorage.realloc(trimSize);
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000980 for (int y = 0; y < desc.fHeight; y++) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000981 memcpy(dst, src, trimRowBytes);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000982 if (flipY) {
983 src -= rowBytes;
984 } else {
985 src += rowBytes;
986 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000987 dst += trimRowBytes;
988 }
989 // now point srcData to our trimmed version
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000990 srcData = tempStorage.get();
991 rowBytes = trimRowBytes;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000992 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000993 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000994
reed@google.comac10a2d2010-12-22 21:39:39 +0000995 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000996 if (!this->npotRenderTargetSupport()) {
997 glDesc.fAllocWidth = GrNextPow2(desc.fWidth);
998 glDesc.fAllocHeight = GrNextPow2(desc.fHeight);
999 }
1000
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001001 glDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
1002 glDesc.fAllocWidth);
1003 glDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
1004 glDesc.fAllocHeight);
1005 if (glDesc.fAllocWidth > fMaxRenderTargetSize ||
1006 glDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001007 return return_null_texture();
1008 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001009 } else if (!this->npotTextureSupport()) {
1010 glDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1011 glDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001012 if (glDesc.fAllocWidth > fMaxTextureSize ||
1013 glDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001014 return return_null_texture();
1015 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001016 }
1017
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001018 GR_GL(GenTextures(1, &glDesc.fTextureID));
1019 if (!glDesc.fTextureID) {
1020 return return_null_texture();
1021 }
1022
twiz@google.com0f31ca72011-03-18 17:38:11 +00001023 GR_GL(BindTexture(GR_GL_TEXTURE_2D, glDesc.fTextureID));
1024 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1025 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001026 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001027 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1028 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001029 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001030 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1031 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001032 DEFAULT_PARAMS.fWrapS));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001033 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1034 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001035 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001036
twiz@google.com0f31ca72011-03-18 17:38:11 +00001037 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, glDesc.fUploadByteCount));
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001038 if (kIndex_8_GrPixelConfig == desc.fFormat &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001039 supports8BitPalette()) {
1040 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
1041 GrAssert(desc.fWidth == glDesc.fAllocWidth);
1042 GrAssert(desc.fHeight == glDesc.fAllocHeight);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001043 GrGLsizei imageSize = glDesc.fAllocWidth * glDesc.fAllocHeight +
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001044 kGrColorTableSize;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001045 GR_GL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, glDesc.fUploadFormat,
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 glDesc.fAllocWidth, glDesc.fAllocHeight,
1047 0, imageSize, srcData));
bsalomon@google.comf987d1b2011-04-04 17:13:52 +00001048 GrGLRestoreResetRowLength();
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 } else {
1050 if (NULL != srcData && (glDesc.fAllocWidth != desc.fWidth ||
1051 glDesc.fAllocHeight != desc.fHeight)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001052 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
reed@google.comac10a2d2010-12-22 21:39:39 +00001053 glDesc.fAllocWidth, glDesc.fAllocHeight,
1054 0, glDesc.fUploadFormat, glDesc.fUploadType, NULL));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001055 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fWidth,
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 desc.fHeight, glDesc.fUploadFormat,
1057 glDesc.fUploadType, srcData));
bsalomon@google.comf987d1b2011-04-04 17:13:52 +00001058 GrGLRestoreResetRowLength();
reed@google.comac10a2d2010-12-22 21:39:39 +00001059
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001060 int extraW = glDesc.fAllocWidth - desc.fWidth;
1061 int extraH = glDesc.fAllocHeight - desc.fHeight;
1062 int maxTexels = extraW * extraH;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 maxTexels = GrMax(extraW * desc.fHeight, maxTexels);
1064 maxTexels = GrMax(desc.fWidth * extraH, maxTexels);
1065
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001066 SkAutoSMalloc<128*128> texels(glDesc.fUploadByteCount * maxTexels);
reed@google.comac10a2d2010-12-22 21:39:39 +00001067
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001068 // rowBytes is actual stride between rows in srcData
1069 // rowDataBytes is the actual amount of non-pad data in a row
1070 // and the stride used for uploading extraH rows.
1071 uint32_t rowDataBytes = desc.fWidth * glDesc.fUploadByteCount;
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 if (extraH) {
1073 uint8_t* lastRowStart = (uint8_t*) srcData +
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001074 (desc.fHeight - 1) * rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 uint8_t* extraRowStart = (uint8_t*)texels.get();
1076
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001077 for (int i = 0; i < extraH; ++i) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001078 memcpy(extraRowStart, lastRowStart, rowDataBytes);
1079 extraRowStart += rowDataBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001081 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, desc.fHeight, desc.fWidth,
reed@google.comac10a2d2010-12-22 21:39:39 +00001082 extraH, glDesc.fUploadFormat, glDesc.fUploadType,
1083 texels.get()));
1084 }
1085 if (extraW) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001086 uint8_t* edgeTexel = (uint8_t*)srcData + rowDataBytes - glDesc.fUploadByteCount;
reed@google.comac10a2d2010-12-22 21:39:39 +00001087 uint8_t* extraTexel = (uint8_t*)texels.get();
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001088 for (int j = 0; j < desc.fHeight; ++j) {
1089 for (int i = 0; i < extraW; ++i) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001090 memcpy(extraTexel, edgeTexel, glDesc.fUploadByteCount);
1091 extraTexel += glDesc.fUploadByteCount;
1092 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001093 edgeTexel += rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001095 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fWidth, 0, extraW,
reed@google.comac10a2d2010-12-22 21:39:39 +00001096 desc.fHeight, glDesc.fUploadFormat,
1097 glDesc.fUploadType, texels.get()));
1098 }
1099 if (extraW && extraH) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001100 uint8_t* cornerTexel = (uint8_t*)srcData + desc.fHeight * rowBytes
reed@google.comac10a2d2010-12-22 21:39:39 +00001101 - glDesc.fUploadByteCount;
1102 uint8_t* extraTexel = (uint8_t*)texels.get();
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001103 for (int i = 0; i < extraW*extraH; ++i) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001104 memcpy(extraTexel, cornerTexel, glDesc.fUploadByteCount);
1105 extraTexel += glDesc.fUploadByteCount;
1106 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001107 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fWidth, desc.fHeight,
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 extraW, extraH, glDesc.fUploadFormat,
1109 glDesc.fUploadType, texels.get()));
1110 }
1111
1112 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001113 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat, glDesc.fAllocWidth,
reed@google.comac10a2d2010-12-22 21:39:39 +00001114 glDesc.fAllocHeight, 0, glDesc.fUploadFormat,
1115 glDesc.fUploadType, srcData));
bsalomon@google.comf987d1b2011-04-04 17:13:52 +00001116 GrGLRestoreResetRowLength();
reed@google.comac10a2d2010-12-22 21:39:39 +00001117 }
1118 }
1119
reed@google.comac10a2d2010-12-22 21:39:39 +00001120
1121 GrGLRenderTarget::GLRenderTargetIDs rtIDs;
1122 rtIDs.fStencilRenderbufferID = 0;
1123 rtIDs.fMSColorRenderbufferID = 0;
1124 rtIDs.fRTFBOID = 0;
1125 rtIDs.fTexFBOID = 0;
1126 rtIDs.fOwnIDs = true;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001127 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001128
1129 if (renderTarget) {
1130#if GR_COLLECT_STATS
1131 ++fStats.fRenderTargetCreateCnt;
1132#endif
1133 bool failed = true;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001134 GrGLenum status;
1135 GrGLint err;
reed@google.comac10a2d2010-12-22 21:39:39 +00001136
twiz@google.com59a190b2011-03-14 21:23:01 +00001137 GR_GL(GenFramebuffers(1, &rtIDs.fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 GrAssert(rtIDs.fTexFBOID);
1139
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001140 // If we are using multisampling and we will create two FBOS We render
bsalomon@google.comd1e433532011-03-21 21:38:40 +00001141 // to one and then resolve to the texture bound to the other.
1142 if (samples > 1 && kNone_MSFBO != fMSFBOType) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001143 GR_GL(GenFramebuffers(1, &rtIDs.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 GrAssert(0 != rtIDs.fRTFBOID);
twiz@google.com59a190b2011-03-14 21:23:01 +00001145 GR_GL(GenRenderbuffers(1, &rtIDs.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001146 GrAssert(0 != rtIDs.fMSColorRenderbufferID);
1147 if (!fboInternalFormat(desc.fFormat, &msColorRenderbufferFormat)) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001148 GR_GL(DeleteRenderbuffers(1, &rtIDs.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001149 GR_GL(DeleteTextures(1, &glDesc.fTextureID));
twiz@google.com59a190b2011-03-14 21:23:01 +00001150 GR_GL(DeleteFramebuffers(1, &rtIDs.fTexFBOID));
1151 GR_GL(DeleteFramebuffers(1, &rtIDs.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 return return_null_texture();
1153 }
1154 } else {
1155 rtIDs.fRTFBOID = rtIDs.fTexFBOID;
1156 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001157 if (!(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001158 GR_GL(GenRenderbuffers(1, &rtIDs.fStencilRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001159 GrAssert(0 != rtIDs.fStencilRenderbufferID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001160 }
1161
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001162 // someone suggested that some systems might require
bsalomon@google.com316f99232011-01-13 21:28:12 +00001163 // unbinding the texture before we call FramebufferTexture2D
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001164 // (seems unlikely)
twiz@google.com0f31ca72011-03-18 17:38:11 +00001165 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001166
twiz@google.com0f31ca72011-03-18 17:38:11 +00001167 err = ~GR_GL_NO_ERROR;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00001168
bsalomon@google.comfe676522011-06-17 18:12:21 +00001169 int stencilFmtCnt;
bsalomon@google.comfe676522011-06-17 18:12:21 +00001170 if (rtIDs.fStencilRenderbufferID) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001171 stencilFmtCnt = fStencilFormats.count();
bsalomon@google.comfe676522011-06-17 18:12:21 +00001172 } else {
1173 stencilFmtCnt = 1; // only 1 attempt when we don't need a stencil
1174 }
1175
1176 for (int i = 0; i < stencilFmtCnt; ++i) {
1177 // we start with the last stencil format that succeeded in hopes
1178 // that we won't go through this loop more than once after the
1179 // first (painful) stencil creation.
1180 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
1181
reed@google.comac10a2d2010-12-22 21:39:39 +00001182 if (rtIDs.fStencilRenderbufferID) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001183 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
twiz@google.com59a190b2011-03-14 21:23:01 +00001184 rtIDs.fStencilRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001185 if (samples > 1) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001186 GR_GL_NO_ERR(RenderbufferStorageMultisample(
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001187 GR_GL_RENDERBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001188 samples,
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001189 fStencilFormats[sIdx].fEnum,
reed@google.comac10a2d2010-12-22 21:39:39 +00001190 glDesc.fAllocWidth,
1191 glDesc.fAllocHeight));
1192 } else {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001193 GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001194 fStencilFormats[sIdx].fEnum,
twiz@google.com59a190b2011-03-14 21:23:01 +00001195 glDesc.fAllocWidth,
1196 glDesc.fAllocHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +00001197 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001198 err = GrGLGetGLInterface()->fGetError();
1199 if (err != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001200 continue;
1201 }
1202 }
1203 if (rtIDs.fRTFBOID != rtIDs.fTexFBOID) {
1204 GrAssert(samples > 1);
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001205 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
twiz@google.com59a190b2011-03-14 21:23:01 +00001206 rtIDs.fMSColorRenderbufferID));
1207 GR_GL_NO_ERR(RenderbufferStorageMultisample(
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001208 GR_GL_RENDERBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001209 samples,
1210 msColorRenderbufferFormat,
1211 glDesc.fAllocWidth,
1212 glDesc.fAllocHeight));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001213 err = GrGLGetGLInterface()->fGetError();
1214 if (err != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 continue;
1216 }
1217 }
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001218 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs.fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001219
1220#if GR_COLLECT_STATS
1221 ++fStats.fRenderTargetChngCnt;
1222#endif
bsalomon@google.comd1e433532011-03-21 21:38:40 +00001223 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1224 GR_GL_COLOR_ATTACHMENT0,
1225 GR_GL_TEXTURE_2D,
1226 glDesc.fTextureID, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001227 if (rtIDs.fRTFBOID != rtIDs.fTexFBOID) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001228 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1229 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 continue;
1231 }
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001232 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 #if GR_COLLECT_STATS
1234 ++fStats.fRenderTargetChngCnt;
1235 #endif
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001236 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1237 GR_GL_COLOR_ATTACHMENT0,
1238 GR_GL_RENDERBUFFER,
twiz@google.com59a190b2011-03-14 21:23:01 +00001239 rtIDs.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001240
1241 }
1242 if (rtIDs.fStencilRenderbufferID) {
1243 // bind the stencil to rt fbo if present, othewise the tex fbo
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001244 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1245 GR_GL_STENCIL_ATTACHMENT,
1246 GR_GL_RENDERBUFFER,
twiz@google.com59a190b2011-03-14 21:23:01 +00001247 rtIDs.fStencilRenderbufferID));
bsalomon@google.com9283b582011-04-08 19:00:04 +00001248 // if it is a packed format bind to depth also, otherwise
1249 // we may get an unsupported fbo completeness result
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001250 if (fStencilFormats[sIdx].fPacked) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001251 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1252 GR_GL_DEPTH_ATTACHMENT,
1253 GR_GL_RENDERBUFFER,
1254 rtIDs.fStencilRenderbufferID));
1255 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 }
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001257 status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
reed@google.comac10a2d2010-12-22 21:39:39 +00001258
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001259 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001260 // undo the depth bind
1261 if (rtIDs.fStencilRenderbufferID &&
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001262 fStencilFormats[sIdx].fPacked) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001263 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1264 GR_GL_DEPTH_ATTACHMENT,
1265 GR_GL_RENDERBUFFER,
1266 0));
twiz@google.comb65e0cb2011-03-18 20:41:44 +00001267 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 continue;
1269 }
1270 // we're successful!
1271 failed = false;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001272 if (rtIDs.fStencilRenderbufferID) {
bsalomon@google.comfe676522011-06-17 18:12:21 +00001273 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001274 if (gUNKNOWN_BITCOUNT == fStencilFormats[sIdx].fBits) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001275 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, (GrGLint*)&glDesc.fStencilBits);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001276 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001277 glDesc.fStencilBits = fStencilFormats[sIdx].fBits;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001278 }
1279 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 break;
1281 }
1282 if (failed) {
1283 if (rtIDs.fStencilRenderbufferID) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001284 GR_GL(DeleteRenderbuffers(1, &rtIDs.fStencilRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 }
1286 if (rtIDs.fMSColorRenderbufferID) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001287 GR_GL(DeleteRenderbuffers(1, &rtIDs.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001288 }
1289 if (rtIDs.fRTFBOID != rtIDs.fTexFBOID) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001290 GR_GL(DeleteFramebuffers(1, &rtIDs.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 }
1292 if (rtIDs.fTexFBOID) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001293 GR_GL(DeleteFramebuffers(1, &rtIDs.fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 }
1295 GR_GL(DeleteTextures(1, &glDesc.fTextureID));
1296 return return_null_texture();
1297 }
1298 }
1299#ifdef TRACE_TEXTURE_CREATION
1300 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1301 tex->fTextureID, width, height, tex->fUploadByteCount);
1302#endif
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001303 GrGLTexture* tex = new GrGLTexture(this, glDesc, rtIDs, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001304
1305 if (0 != rtIDs.fTexFBOID) {
1306 GrRenderTarget* rt = tex->asRenderTarget();
1307 // We've messed with FBO state but may not have set the correct viewport
1308 // so just dirty the rendertarget state to force a resend.
1309 fHWDrawState.fRenderTarget = NULL;
1310
1311 // clear the new stencil buffer if we have one
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001312 if (!(desc.fFlags & kNoStencil_GrTextureFlagBit)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 GrRenderTarget* rtSave = fCurrDrawState.fRenderTarget;
1314 fCurrDrawState.fRenderTarget = rt;
bsalomon@google.com398109c2011-04-14 18:40:27 +00001315 this->clearStencil(0, ~0);
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 fCurrDrawState.fRenderTarget = rtSave;
1317 }
1318 }
1319 return tex;
1320}
1321
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001322GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001323 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 GR_GL(GenBuffers(1, &id));
1325 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001326 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001327 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 GrGLClearErr();
1329 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001330 GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1331 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1332 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 GR_GL(DeleteBuffers(1, &id));
1334 // deleting bound buffer does implicit bind to 0
1335 fHWGeometryState.fVertexBuffer = NULL;
1336 return NULL;
1337 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001338 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 size, dynamic);
1340 fHWGeometryState.fVertexBuffer = vertexBuffer;
1341 return vertexBuffer;
1342 }
1343 return NULL;
1344}
1345
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001346GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001347 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001348 GR_GL(GenBuffers(1, &id));
1349 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001350 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 GrGLClearErr();
1352 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001353 GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1354 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1355 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 GR_GL(DeleteBuffers(1, &id));
1357 // deleting bound buffer does implicit bind to 0
1358 fHWGeometryState.fIndexBuffer = NULL;
1359 return NULL;
1360 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001361 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001362 size, dynamic);
1363 fHWGeometryState.fIndexBuffer = indexBuffer;
1364 return indexBuffer;
1365 }
1366 return NULL;
1367}
1368
reed@google.comac10a2d2010-12-22 21:39:39 +00001369void GrGpuGL::flushScissor(const GrIRect* rect) {
1370 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001371 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001372 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001373
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001374 GrGLIRect scissor;
1375 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001376 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001377 rect->width(), rect->height());
1378 if (scissor.contains(vp)) {
1379 rect = NULL;
1380 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 }
1382
1383 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001384 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001385 scissor.pushToGLScissor();
reed@google.comac10a2d2010-12-22 21:39:39 +00001386 fHWBounds.fScissorRect = scissor;
1387 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001388 if (!fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001389 GR_GL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001390 fHWBounds.fScissorEnabled = true;
1391 }
1392 } else {
1393 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001394 GR_GL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001395 fHWBounds.fScissorEnabled = false;
1396 }
1397 }
1398}
1399
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001400void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001401 if (NULL == fCurrDrawState.fRenderTarget) {
1402 return;
1403 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001404 GrIRect r;
1405 if (NULL != rect) {
1406 // flushScissor expects rect to be clipped to the target.
1407 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001408 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1409 fCurrDrawState.fRenderTarget->height());
1410 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001411 rect = &r;
1412 } else {
1413 return;
1414 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001415 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001416 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001417 this->flushScissor(rect);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001418 GR_GL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001419 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
reed@google.comac10a2d2010-12-22 21:39:39 +00001420 GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
1421 GrColorUnpackG(color)/255.f,
1422 GrColorUnpackB(color)/255.f,
1423 GrColorUnpackA(color)/255.f));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001424 GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001425}
1426
bsalomon@google.com398109c2011-04-14 18:40:27 +00001427void GrGpuGL::clearStencil(uint32_t value, uint32_t mask) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001428 if (NULL == fCurrDrawState.fRenderTarget) {
1429 return;
1430 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001431
1432 this->flushRenderTarget(&GrIRect::EmptyIRect());
1433
reed@google.comac10a2d2010-12-22 21:39:39 +00001434 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001435 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001436 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001437 }
1438 GR_GL(StencilMask(mask));
1439 GR_GL(ClearStencil(value));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001440 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001441 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001442}
1443
bsalomon@google.com398109c2011-04-14 18:40:27 +00001444void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001445 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001446#if 0
twiz@google.com0f31ca72011-03-18 17:38:11 +00001447 GrGLint stencilBitCount = fCurrDrawState.fRenderTarget->stencilBits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001448 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001449 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001450#else
1451 // we could just clear the clip bit but when we go through
1452 // angle a partial stencil mask will cause clears to be
1453 // turned into draws. Our contract on GrDrawTarget says that
1454 // changing the clip between stencil passes may or may not
1455 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001456 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001457#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001458 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.comd302f142011-03-03 13:54:13 +00001459 flushScissor(&rect);
1460 GR_GL(StencilMask(clipStencilMask));
1461 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001462 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001463 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001464}
1465
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001466void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001467 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001468}
1469
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001470bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1471 int left, int top, int width, int height,
1472 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001473 GrGLenum internalFormat; // we don't use this for glReadPixels
1474 GrGLenum format;
1475 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001476 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1477 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001478 }
1479 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1480 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1481 switch (tgt->getResolveType()) {
1482 case GrGLRenderTarget::kCantResolve_ResolveType:
1483 return false;
1484 case GrGLRenderTarget::kAutoResolves_ResolveType:
1485 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1486 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001487 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001488 break;
1489 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001490 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001491 // we don't track the state of the READ FBO ID.
1492 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
1493 break;
1494 default:
1495 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001496 }
1497
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001498 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001499
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001500 // the read rect is viewport-relative
1501 GrGLIRect readRect;
1502 readRect.setRelativeTo(glvp, left, top, width, height);
1503 GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001504 readRect.fWidth, readRect.fHeight,
bsalomon@google.com316f99232011-01-13 21:28:12 +00001505 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001506
1507 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1508 // API presents top-to-bottom
1509 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001510 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001511 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001512 void* tmp = rowStorage.get();
1513
1514 const int halfY = height >> 1;
1515 char* top = reinterpret_cast<char*>(buffer);
1516 char* bottom = top + (height - 1) * stride;
1517 for (int y = 0; y < halfY; y++) {
1518 memcpy(tmp, top, stride);
1519 memcpy(top, bottom, stride);
1520 memcpy(bottom, tmp, stride);
1521 top += stride;
1522 bottom -= stride;
1523 }
1524 }
1525 return true;
1526}
1527
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001528void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001529
1530 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1531
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001532 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001533 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001534 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001535 #if GR_COLLECT_STATS
1536 ++fStats.fRenderTargetChngCnt;
1537 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001538 #if GR_DEBUG
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001539 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1540 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001541 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001542 }
1543 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001544 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001545 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001546 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001547 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001548 vp.pushToGLViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001549 fHWBounds.fViewportRect = vp;
1550 }
1551 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001552 if (NULL == bound || !bound->isEmpty()) {
1553 rt->flagAsNeedingResolve(bound);
1554 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001555}
1556
twiz@google.com0f31ca72011-03-18 17:38:11 +00001557GrGLenum gPrimitiveType2GLMode[] = {
1558 GR_GL_TRIANGLES,
1559 GR_GL_TRIANGLE_STRIP,
1560 GR_GL_TRIANGLE_FAN,
1561 GR_GL_POINTS,
1562 GR_GL_LINES,
1563 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001564};
1565
bsalomon@google.comd302f142011-03-03 13:54:13 +00001566#define SWAP_PER_DRAW 0
1567
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001568#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001569 #if GR_MAC_BUILD
1570 #include <AGL/agl.h>
1571 #elif GR_WIN32_BUILD
1572 void SwapBuf() {
1573 DWORD procID = GetCurrentProcessId();
1574 HWND hwnd = GetTopWindow(GetDesktopWindow());
1575 while(hwnd) {
1576 DWORD wndProcID = 0;
1577 GetWindowThreadProcessId(hwnd, &wndProcID);
1578 if(wndProcID == procID) {
1579 SwapBuffers(GetDC(hwnd));
1580 }
1581 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1582 }
1583 }
1584 #endif
1585#endif
1586
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001587void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1588 uint32_t startVertex,
1589 uint32_t startIndex,
1590 uint32_t vertexCount,
1591 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001592 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1593
twiz@google.com0f31ca72011-03-18 17:38:11 +00001594 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001595
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001596 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1597 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1598
1599 // our setupGeometry better have adjusted this to zero since
1600 // DrawElements always draws from the begining of the arrays for idx 0.
1601 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001602
1603 GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
twiz@google.com0f31ca72011-03-18 17:38:11 +00001604 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001605#if SWAP_PER_DRAW
1606 glFlush();
1607 #if GR_MAC_BUILD
1608 aglSwapBuffers(aglGetCurrentContext());
1609 int set_a_break_pt_here = 9;
1610 aglSwapBuffers(aglGetCurrentContext());
1611 #elif GR_WIN32_BUILD
1612 SwapBuf();
1613 int set_a_break_pt_here = 9;
1614 SwapBuf();
1615 #endif
1616#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001617}
1618
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001619void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1620 uint32_t startVertex,
1621 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001622 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1623
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001624 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1625
1626 // our setupGeometry better have adjusted this to zero.
1627 // DrawElements doesn't take an offset so we always adjus the startVertex.
1628 GrAssert(0 == startVertex);
1629
1630 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1631 // account for startVertex in the DrawElements case. So we always
1632 // rely on setupGeometry to have accounted for startVertex.
reed@google.comac10a2d2010-12-22 21:39:39 +00001633 GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001634#if SWAP_PER_DRAW
1635 glFlush();
1636 #if GR_MAC_BUILD
1637 aglSwapBuffers(aglGetCurrentContext());
1638 int set_a_break_pt_here = 9;
1639 aglSwapBuffers(aglGetCurrentContext());
1640 #elif GR_WIN32_BUILD
1641 SwapBuf();
1642 int set_a_break_pt_here = 9;
1643 SwapBuf();
1644 #endif
1645#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001646}
1647
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001648void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001649
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001650 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001651 GrAssert(kNone_MSFBO != fMSFBOType);
1652 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001653 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001654 rt->renderFBOID()));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001655 GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001656 rt->textureFBOID()));
1657 #if GR_COLLECT_STATS
1658 ++fStats.fRenderTargetChngCnt;
1659 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001660 // make sure we go through flushRenderTarget() since we've modified
1661 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001662 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001663 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001664 const GrIRect dirtyRect = rt->getResolveRect();
1665 GrGLIRect r;
1666 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1667 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001668
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001669 if (kAppleES_MSFBO == fMSFBOType) {
1670 // Apple's extension uses the scissor as the blit bounds.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001671 GR_GL(Enable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001672 GR_GL(Scissor(r.fLeft, r.fBottom,
1673 r.fWidth, r.fHeight));
twiz@google.com59a190b2011-03-14 21:23:01 +00001674 GR_GL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001675 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001676 fHWBounds.fScissorEnabled = true;
1677 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001678 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001679 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001680 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1681 flushScissor(NULL);
1682 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001683 int right = r.fLeft + r.fWidth;
1684 int top = r.fBottom + r.fHeight;
1685 GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1686 r.fLeft, r.fBottom, right, top,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001687 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001688 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001689 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001690 }
1691}
1692
twiz@google.com0f31ca72011-03-18 17:38:11 +00001693static const GrGLenum grToGLStencilFunc[] = {
1694 GR_GL_ALWAYS, // kAlways_StencilFunc
1695 GR_GL_NEVER, // kNever_StencilFunc
1696 GR_GL_GREATER, // kGreater_StencilFunc
1697 GR_GL_GEQUAL, // kGEqual_StencilFunc
1698 GR_GL_LESS, // kLess_StencilFunc
1699 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1700 GR_GL_EQUAL, // kEqual_StencilFunc,
1701 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001702};
1703GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1704GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1705GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1706GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1707GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1708GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1709GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1710GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1711GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1712
twiz@google.com0f31ca72011-03-18 17:38:11 +00001713static const GrGLenum grToGLStencilOp[] = {
1714 GR_GL_KEEP, // kKeep_StencilOp
1715 GR_GL_REPLACE, // kReplace_StencilOp
1716 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1717 GR_GL_INCR, // kIncClamp_StencilOp
1718 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1719 GR_GL_DECR, // kDecClamp_StencilOp
1720 GR_GL_ZERO, // kZero_StencilOp
1721 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001722};
1723GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1724GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1725GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1726GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1727GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1728GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1729GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1730GR_STATIC_ASSERT(6 == kZero_StencilOp);
1731GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1732
reed@google.comac10a2d2010-12-22 21:39:39 +00001733void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001734 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001735
1736 // use stencil for clipping if clipping is enabled and the clip
1737 // has been written into the stencil.
1738 bool stencilClip = fClipState.fClipInStencil &&
1739 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001740 bool stencilChange = fHWStencilClip != stencilClip ||
1741 fHWDrawState.fStencilSettings != *settings ||
1742 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1743 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001744
1745 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001746
bsalomon@google.comd302f142011-03-03 13:54:13 +00001747 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1748 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001749
bsalomon@google.comd302f142011-03-03 13:54:13 +00001750 if (settings->isDisabled()) {
1751 if (stencilClip) {
1752 settings = &gClipStencilSettings;
1753 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001755
1756 if (settings->isDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001757 GR_GL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001758 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001759 GR_GL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001760 #if GR_DEBUG
1761 if (!fStencilWrapOpsSupport) {
1762 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1763 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1764 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1765 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1766 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1767 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1768 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1769 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1770 }
1771 #endif
1772 int stencilBits = fCurrDrawState.fRenderTarget->stencilBits();
1773 GrAssert(stencilBits ||
1774 (GrStencilSettings::gDisabled ==
1775 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001776 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1777 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001778
1779 unsigned int frontRef = settings->fFrontFuncRef;
1780 unsigned int frontMask = settings->fFrontFuncMask;
1781 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001782 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001783
1784 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1785
1786 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1787 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1788 } else {
1789 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1790
1791 ConvertStencilFuncAndMask(settings->fFrontFunc,
1792 stencilClip,
1793 clipStencilMask,
1794 userStencilMask,
1795 &frontRef,
1796 &frontMask);
1797 frontWriteMask &= userStencilMask;
1798 }
1799 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001800 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001801 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001802 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001804 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001805 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001806 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001807 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001808 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001809
1810 unsigned int backRef = settings->fBackFuncRef;
1811 unsigned int backMask = settings->fBackFuncMask;
1812 unsigned int backWriteMask = settings->fBackWriteMask;
1813
1814
1815 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1816 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1817 backFunc = grToGLStencilFunc[settings->fBackFunc];
1818 } else {
1819 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1820 ConvertStencilFuncAndMask(settings->fBackFunc,
1821 stencilClip,
1822 clipStencilMask,
1823 userStencilMask,
1824 &backRef,
1825 &backMask);
1826 backWriteMask &= userStencilMask;
1827 }
1828
twiz@google.com0f31ca72011-03-18 17:38:11 +00001829 GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask));
1830 GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1831 GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask));
1832 GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1833 GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001834 grToGLStencilOp[settings->fFrontPassOp],
1835 grToGLStencilOp[settings->fFrontPassOp]));
1836
twiz@google.com0f31ca72011-03-18 17:38:11 +00001837 GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001838 grToGLStencilOp[settings->fBackPassOp],
1839 grToGLStencilOp[settings->fBackPassOp]));
1840 } else {
1841 GR_GL(StencilFunc(frontFunc, frontRef, frontMask));
1842 GR_GL(StencilMask(frontWriteMask));
1843 GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
1844 grToGLStencilOp[settings->fFrontPassOp],
1845 grToGLStencilOp[settings->fFrontPassOp]));
1846 }
1847 }
1848 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001849 fHWStencilClip = stencilClip;
1850 }
1851}
1852
bsalomon@google.com0650e812011-04-08 18:07:53 +00001853bool GrGpuGL::useSmoothLines() {
1854 // there is a conflict between using smooth lines and our use of
1855 // premultiplied alpha. Smooth lines tweak the incoming alpha value
1856 // but not in a premul-alpha way. So we only use them when our alpha
1857 // is 0xff.
1858
1859 // TODO: write a smarter line frag shader.
1860
1861 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
1862 canDisableBlend();
1863}
1864
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001865void GrGpuGL::flushAAState(GrPrimitiveType type) {
1866 if (GR_GL_SUPPORT_DESKTOP) {
1867 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1868 // smooth lines.
1869
1870 // we prefer smooth lines over multisampled lines
1871 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001872 if (GrIsPrimTypeLines(type)) {
1873 bool smooth = useSmoothLines();
1874 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001875 GR_GL(Enable(GR_GL_LINE_SMOOTH));
1876 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001877 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001878 GR_GL(Disable(GR_GL_LINE_SMOOTH));
1879 fHWAAState.fSmoothLineEnabled = false;
1880 }
1881 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1882 fHWAAState.fMSAAEnabled) {
1883 GR_GL(Disable(GR_GL_MULTISAMPLE));
1884 fHWAAState.fMSAAEnabled = false;
1885 }
1886 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1887 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
1888 fHWAAState.fMSAAEnabled) {
1889 if (fHWAAState.fMSAAEnabled) {
1890 GR_GL(Disable(GR_GL_MULTISAMPLE));
1891 fHWAAState.fMSAAEnabled = false;
1892 } else {
1893 GR_GL(Enable(GR_GL_MULTISAMPLE));
1894 fHWAAState.fMSAAEnabled = true;
1895 }
1896 }
1897 }
1898}
1899
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001900void GrGpuGL::flushBlend(GrPrimitiveType type,
1901 GrBlendCoeff srcCoeff,
1902 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001903 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
1904 if (fHWBlendDisabled) {
1905 GR_GL(Enable(GR_GL_BLEND));
1906 fHWBlendDisabled = false;
1907 }
1908 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1909 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
1910 GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1911 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
1912 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1913 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1914 }
1915 } else {
1916 bool blendOff = canDisableBlend();
1917 if (fHWBlendDisabled != blendOff) {
1918 if (blendOff) {
1919 GR_GL(Disable(GR_GL_BLEND));
1920 } else {
1921 GR_GL(Enable(GR_GL_BLEND));
1922 }
1923 fHWBlendDisabled = blendOff;
1924 }
1925 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001926 if (fHWDrawState.fSrcBlend != srcCoeff ||
1927 fHWDrawState.fDstBlend != dstCoeff) {
1928 GR_GL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1929 gXfermodeCoeff2Blend[dstCoeff]));
1930 fHWDrawState.fSrcBlend = srcCoeff;
1931 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001932 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001933 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1934 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001935 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1936
1937 float c[] = {
1938 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1939 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1940 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1941 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1942 };
1943 GR_GL(BlendColor(c[0], c[1], c[2], c[3]));
1944 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1945 }
1946 }
1947 }
1948}
1949
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001950static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1951 switch (filter) {
1952 case GrSamplerState::kBilinear_Filter:
1953 case GrSamplerState::k4x4Downsample_Filter:
1954 return GR_GL_LINEAR;
1955 case GrSamplerState::kNearest_Filter:
1956 case GrSamplerState::kConvolution_Filter:
1957 return GR_GL_NEAREST;
1958 default:
1959 GrAssert(!"Unknown filter type");
1960 return GR_GL_LINEAR;
1961 }
1962}
1963
bsalomon@google.comffca4002011-02-22 20:34:01 +00001964bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001965
1966 // GrGpu::setupClipAndFlushState should have already checked this
1967 // and bailed if not true.
1968 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001969
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001970 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001971 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001972 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001973 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001974
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001975 // true for now, but maybe not with GrEffect.
1976 GrAssert(NULL != nextTexture);
1977 // if we created a rt/tex and rendered to it without using a
1978 // texture and now we're texuring from the rt it will still be
1979 // the last bound texture, but it needs resolving. So keep this
1980 // out of the "last != next" check.
1981 GrGLRenderTarget* texRT =
1982 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1983 if (NULL != texRT) {
1984 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001985 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001986
1987 if (fHWDrawState.fTextures[s] != nextTexture) {
1988 setTextureUnit(s);
1989 GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
1990 #if GR_COLLECT_STATS
1991 ++fStats.fTextureChngCnt;
1992 #endif
1993 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1994 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001995 // The texture matrix has to compensate for texture width/height
1996 // and NPOT-embedded-in-POT
1997 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001998 }
1999
2000 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2001 const GrGLTexture::TexParams& oldTexParams =
2002 nextTexture->getTexParams();
2003 GrGLTexture::TexParams newTexParams;
2004
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002005 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002006
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002007 newTexParams.fWrapS =
2008 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()];
2009 newTexParams.fWrapT =
2010 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()];
2011
2012 if (newTexParams.fFilter != oldTexParams.fFilter) {
2013 setTextureUnit(s);
2014 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2015 GR_GL_TEXTURE_MAG_FILTER,
2016 newTexParams.fFilter));
2017 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2018 GR_GL_TEXTURE_MIN_FILTER,
2019 newTexParams.fFilter));
2020 }
2021 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2022 setTextureUnit(s);
2023 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2024 GR_GL_TEXTURE_WRAP_S,
2025 newTexParams.fWrapS));
2026 }
2027 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2028 setTextureUnit(s);
2029 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2030 GR_GL_TEXTURE_WRAP_T,
2031 newTexParams.fWrapT));
2032 }
2033 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002034 }
2035 }
2036
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002037 GrIRect* rect = NULL;
2038 GrIRect clipBounds;
2039 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2040 fClip.hasConservativeBounds()) {
2041 fClip.getConservativeBounds().roundOut(&clipBounds);
2042 rect = &clipBounds;
2043 }
2044 this->flushRenderTarget(rect);
2045 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002046
reed@google.comac10a2d2010-12-22 21:39:39 +00002047 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2048 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2049 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002050 GR_GL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002051 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002052 GR_GL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002053 }
2054 }
2055
bsalomon@google.comd302f142011-03-03 13:54:13 +00002056 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2057 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002058 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002059 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002060 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002061 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002062 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002063 }
2064 GR_GL(ColorMask(mask, mask, mask, mask));
2065 }
2066
bsalomon@google.comd302f142011-03-03 13:54:13 +00002067 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2068 switch (fCurrDrawState.fDrawFace) {
2069 case kCCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002070 GR_GL(Enable(GR_GL_CULL_FACE));
2071 GR_GL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002072 break;
2073 case kCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002074 GR_GL(Enable(GR_GL_CULL_FACE));
2075 GR_GL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002076 break;
2077 case kBoth_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002078 GR_GL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002079 break;
2080 default:
2081 GrCrash("Unknown draw face.");
2082 }
2083 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2084 }
2085
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002086#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002087 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002088 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002089 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002090 NULL == fCurrDrawState.fRenderTarget ||
2091 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002092 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002093 fCurrDrawState.fRenderTarget);
2094 }
2095#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002096
reed@google.comac10a2d2010-12-22 21:39:39 +00002097 flushStencil();
2098
bsalomon@google.comd302f142011-03-03 13:54:13 +00002099 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002100 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002101 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002102}
2103
2104void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002105 if (fHWGeometryState.fVertexBuffer != buffer) {
2106 fHWGeometryState.fArrayPtrsDirty = true;
2107 fHWGeometryState.fVertexBuffer = buffer;
2108 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002109}
2110
2111void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002112 if (fHWGeometryState.fVertexBuffer == buffer) {
2113 // deleting bound buffer does implied bind to 0
2114 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002115 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002116 }
2117}
2118
2119void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002120 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002121}
2122
2123void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002124 if (fHWGeometryState.fIndexBuffer == buffer) {
2125 // deleting bound buffer does implied bind to 0
2126 fHWGeometryState.fIndexBuffer = NULL;
2127 }
2128}
2129
reed@google.comac10a2d2010-12-22 21:39:39 +00002130void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2131 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002132 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002133 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002134 }
2135 if (fHWDrawState.fRenderTarget == renderTarget) {
2136 fHWDrawState.fRenderTarget = NULL;
2137 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002138}
2139
2140void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002141 for (int s = 0; s < kNumStages; ++s) {
2142 if (fCurrDrawState.fTextures[s] == texture) {
2143 fCurrDrawState.fTextures[s] = NULL;
2144 }
2145 if (fHWDrawState.fTextures[s] == texture) {
2146 // deleting bound texture does implied bind to 0
2147 fHWDrawState.fTextures[s] = NULL;
2148 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002149 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002150}
2151
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002152bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002153 GrGLenum* internalFormat,
2154 GrGLenum* format,
2155 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002156 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002157 case kRGBA_8888_GrPixelConfig:
2158 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002159 *format = GR_GL_32BPP_COLOR_FORMAT;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002160 if (GR_GL_SUPPORT_ES) {
2161 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2162 // format for a BGRA is BGRA not RGBA (as on desktop)
2163 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2164 } else {
2165 *internalFormat = GR_GL_RGBA;
2166 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002167 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002168 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002169 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002170 *format = GR_GL_RGB;
2171 *internalFormat = GR_GL_RGB;
2172 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002173 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002174 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002175 *format = GR_GL_RGBA;
2176 *internalFormat = GR_GL_RGBA;
2177 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002178 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002179 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002180 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002181 *format = GR_GL_PALETTE8_RGBA8;
2182 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002183 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002184 } else {
2185 return false;
2186 }
2187 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002188 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002189 *format = GR_GL_ALPHA;
2190 *internalFormat = GR_GL_ALPHA;
2191 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 break;
2193 default:
2194 return false;
2195 }
2196 return true;
2197}
2198
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002199void GrGpuGL::setTextureUnit(int unit) {
2200 GrAssert(unit >= 0 && unit < kNumStages);
2201 if (fActiveTextureUnitIdx != unit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002202 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002203 fActiveTextureUnitIdx = unit;
2204 }
2205}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002206
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002207void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002208 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
2209 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002210 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2211 }
2212}
2213
reed@google.comac10a2d2010-12-22 21:39:39 +00002214/* On ES the internalFormat and format must match for TexImage and we use
2215 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2216 decide the internalFormat. However, on ES internalFormat for
2217 RenderBufferStorage* has to be a specific format (not a base format like
2218 GL_RGBA).
2219 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002220bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002221 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002222 case kRGBA_8888_GrPixelConfig:
2223 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002224 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002225 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002226 return true;
2227 } else {
2228 return false;
2229 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002230 case kRGB_565_GrPixelConfig:
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002231 GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it
2232 // with FBO extension desktop GL has
2233 // no such internal format
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002234 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002235 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002236 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002237 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002238 return true;
2239 default:
2240 return false;
2241 }
2242}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002243
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002244void GrGpuGL::resetDirtyFlags() {
2245 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2246}
2247
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002248void GrGpuGL::setBuffers(bool indexed,
2249 int* extraVertexOffset,
2250 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002251
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002252 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002253
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002254 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2255
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002256 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002257 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002258 case kBuffer_GeometrySrcType:
2259 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002260 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002261 break;
2262 case kArray_GeometrySrcType:
2263 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002264 this->finalizeReservedVertices();
2265 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2266 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002267 break;
2268 default:
2269 vbuf = NULL; // suppress warning
2270 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002271 }
2272
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002273 GrAssert(NULL != vbuf);
2274 GrAssert(!vbuf->isLocked());
2275 if (fHWGeometryState.fVertexBuffer != vbuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002276 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002277 fHWGeometryState.fArrayPtrsDirty = true;
2278 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002279 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002280
2281 if (indexed) {
2282 GrAssert(NULL != extraIndexOffset);
2283
2284 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002285 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002286 case kBuffer_GeometrySrcType:
2287 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002288 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002289 break;
2290 case kArray_GeometrySrcType:
2291 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002292 this->finalizeReservedIndices();
2293 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2294 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002295 break;
2296 default:
2297 ibuf = NULL; // suppress warning
2298 GrCrash("Unknown geometry src type!");
2299 }
2300
2301 GrAssert(NULL != ibuf);
2302 GrAssert(!ibuf->isLocked());
2303 if (fHWGeometryState.fIndexBuffer != ibuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002304 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002305 fHWGeometryState.fIndexBuffer = ibuf;
2306 }
2307 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002308}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002309
2310int GrGpuGL::getMaxEdges() const {
2311 // FIXME: This is a pessimistic estimate based on how many other things
2312 // want to add uniforms. This should be centralized somewhere.
2313 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2314}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002315