blob: 692df0eef72eb50a8c90fc1fb8f163dd0106e91d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com316f99232011-01-13 21:28:12 +000018// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000019// mucking with the state of any of the stages.
bsalomon@google.com316f99232011-01-13 21:28:12 +000020static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022#define SKIP_CACHE_CHECK true
23
twiz@google.com0f31ca72011-03-18 17:38:11 +000024static const GrGLenum gXfermodeCoeff2Blend[] = {
25 GR_GL_ZERO,
26 GR_GL_ONE,
27 GR_GL_SRC_COLOR,
28 GR_GL_ONE_MINUS_SRC_COLOR,
29 GR_GL_DST_COLOR,
30 GR_GL_ONE_MINUS_DST_COLOR,
31 GR_GL_SRC_ALPHA,
32 GR_GL_ONE_MINUS_SRC_ALPHA,
33 GR_GL_DST_ALPHA,
34 GR_GL_ONE_MINUS_DST_ALPHA,
35 GR_GL_CONSTANT_COLOR,
36 GR_GL_ONE_MINUS_CONSTANT_COLOR,
37 GR_GL_CONSTANT_ALPHA,
38 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000039
40 // extended blend coeffs
41 GR_GL_SRC1_COLOR,
42 GR_GL_ONE_MINUS_SRC1_COLOR,
43 GR_GL_SRC1_ALPHA,
44 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000045};
46
bsalomon@google.com271cffc2011-05-20 14:13:56 +000047bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000048 static const bool gCoeffReferencesBlendConst[] = {
49 false,
50 false,
51 false,
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 true,
60 true,
61 true,
62 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063
64 // extended blend coeffs
65 false,
66 false,
67 false,
68 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000069 };
70 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000071 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
72
73 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
74 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
75 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
76 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
77 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
78 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
79 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
80 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
81 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
82 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
83 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
84 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
85 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
86 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
87
88 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
89 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
90 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
91 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
92
93 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
94 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000095}
96
reed@google.comac10a2d2010-12-22 21:39:39 +000097///////////////////////////////////////////////////////////////////////////////
98
bsalomon@google.comd302f142011-03-03 13:54:13 +000099void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
100 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000101 GrMatrix* matrix) {
102 GrAssert(NULL != texture);
103 GrAssert(NULL != matrix);
104 if (GR_Scalar1 != texture->contentScaleX() ||
105 GR_Scalar1 != texture->contentScaleY()) {
106 if (GrSamplerState::kRadial_SampleMode == mode) {
107 GrMatrix scale;
108 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
109 matrix->postConcat(scale);
110 } else if (GrSamplerState::kNormal_SampleMode == mode) {
111 GrMatrix scale;
112 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
113 matrix->postConcat(scale);
114 } else {
115 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
116 }
117 }
118 GrGLTexture::Orientation orientation = texture->orientation();
119 if (GrGLTexture::kBottomUp_Orientation == orientation) {
120 GrMatrix invY;
121 invY.setAll(GR_Scalar1, 0, 0,
122 0, -GR_Scalar1, GR_Scalar1,
123 0, 0, GrMatrix::I()[8]);
124 matrix->postConcat(invY);
125 } else {
126 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
127 }
128}
129
bsalomon@google.comd302f142011-03-03 13:54:13 +0000130bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000131 const GrSamplerState& sampler) {
132 GrAssert(NULL != texture);
133 if (!sampler.getMatrix().isIdentity()) {
134 return false;
135 }
136 if (GR_Scalar1 != texture->contentScaleX() ||
137 GR_Scalar1 != texture->contentScaleY()) {
138 return false;
139 }
140 GrGLTexture::Orientation orientation = texture->orientation();
141 if (GrGLTexture::kBottomUp_Orientation == orientation) {
142 return false;
143 } else {
144 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
145 }
146 return true;
147}
148
149///////////////////////////////////////////////////////////////////////////////
150
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000151static bool gPrintStartupSpew;
152
twiz@google.com59a190b2011-03-14 21:23:01 +0000153static bool fbo_test(int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000154
twiz@google.com0f31ca72011-03-18 17:38:11 +0000155 GrGLint savedFBO;
156 GrGLint savedTexUnit;
157 GR_GL_GetIntegerv(GR_GL_ACTIVE_TEXTURE, &savedTexUnit);
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000158 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &savedFBO);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000159
twiz@google.com0f31ca72011-03-18 17:38:11 +0000160 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000161
twiz@google.com0f31ca72011-03-18 17:38:11 +0000162 GrGLuint testFBO;
twiz@google.com59a190b2011-03-14 21:23:01 +0000163 GR_GL(GenFramebuffers(1, &testFBO));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000165 GrGLuint testRTTex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 GR_GL(GenTextures(1, &testRTTex));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000167 GR_GL(BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000168 // some implementations require texture to be mip-map complete before
169 // FBO with level 0 bound as color attachment will be framebuffer complete.
twiz@google.com0f31ca72011-03-18 17:38:11 +0000170 GR_GL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
171 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
172 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
173 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000174 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000175 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000176 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000177 GR_GL(DeleteFramebuffers(1, &testFBO));
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 GR_GL(DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000179
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000180 GR_GL(ActiveTexture(savedTexUnit));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000181 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, savedFBO));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000182
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000183 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000184}
185
tomhudson@google.com747bf292011-06-14 18:16:52 +0000186static bool probe_for_npot_render_target_support(bool hasNPOTTextureSupport) {
187
188 /* Experimentation has found that some GLs that support NPOT textures
189 do not support FBOs with a NPOT texture. They report "unsupported" FBO
190 status. I don't know how to explicitly query for this. Do an
191 experiment. Note they may support NPOT with a renderbuffer but not a
192 texture. Presumably, the implementation bloats the renderbuffer
193 internally to the next POT.
194 */
195 if (hasNPOTTextureSupport) {
196 return fbo_test(200, 200);
197 }
198 return false;
199}
200
201static int probe_for_min_render_target_height(bool hasNPOTRenderTargetSupport,
202 int maxRenderTargetSize) {
203 /* The iPhone 4 has a restriction that for an FBO with texture color
204 attachment with height <= 8 then the width must be <= height. Here
205 we look for such a limitation.
206 */
207 if (gPrintStartupSpew) {
208 GrPrintf("Small height FBO texture experiments\n");
209 }
210 int minRenderTargetHeight = GR_INVAL_GLINT;
211 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
212 GrGLuint w = maxRenderTargetSize;
213 GrGLuint h = i;
214 if (fbo_test(w, h)) {
215 if (gPrintStartupSpew) {
216 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
217 }
218 minRenderTargetHeight = i;
219 break;
220 } else {
221 if (gPrintStartupSpew) {
222 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
223 }
224 }
225 }
226 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
227
228 return minRenderTargetHeight;
229}
230
231static int probe_for_min_render_target_width(bool hasNPOTRenderTargetSupport,
232 int maxRenderTargetSize) {
233
234 if (gPrintStartupSpew) {
235 GrPrintf("Small width FBO texture experiments\n");
236 }
237 int minRenderTargetWidth = GR_INVAL_GLINT;
238 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
239 GrGLuint w = i;
240 GrGLuint h = maxRenderTargetSize;
241 if (fbo_test(w, h)) {
242 if (gPrintStartupSpew) {
243 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
244 }
245 minRenderTargetWidth = i;
246 break;
247 } else {
248 if (gPrintStartupSpew) {
249 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
250 }
251 }
252 }
253 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
254
255 return minRenderTargetWidth;
256}
257
258
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000259GrGpuGL::GrGpuGL()
260 : fStencilFormats(8) {
261
262 GrGLClearErr();
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000263
reed@google.comeeeb5a02010-12-23 15:12:59 +0000264 if (gPrintStartupSpew) {
265 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
266 this);
twiz@google.com59a190b2011-03-14 21:23:01 +0000267 GrPrintf("------ VENDOR %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000268 GrGLGetGLInterface()->fGetString(GR_GL_VENDOR));
twiz@google.com59a190b2011-03-14 21:23:01 +0000269 GrPrintf("------ RENDERER %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000270 GrGLGetGLInterface()->fGetString(GR_GL_RENDERER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000271 GrPrintf("------ VERSION %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000272 GrGLGetGLInterface()->fGetString(GR_GL_VERSION));
twiz@google.com59a190b2011-03-14 21:23:01 +0000273 GrPrintf("------ EXTENSIONS\n %s \n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000274 GrGLGetGLInterface()->fGetString(GR_GL_EXTENSIONS));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000275 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000277 fGLVersion = gl_version_as_float();
278 fExtensionString = (const char*) GR_GL(GetString(GR_GL_EXTENSIONS));
reed@google.comac10a2d2010-12-22 21:39:39 +0000279
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000280 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000281
twiz@google.com0f31ca72011-03-18 17:38:11 +0000282 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000283 // check FS and fixed-function texture unit limits
284 // we only use textures in the fragment stage currently.
285 // checks are > to make sure we have a spare unit.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000286 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES2) {
287 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
288 GrAssert(maxTextureUnits > kNumStages);
289 }
290 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES1) {
291 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
292 GrAssert(maxTextureUnits > kNumStages);
293 }
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000294 if (GR_GL_SUPPORT_ES2) {
295 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
296 &fMaxFragmentUniformVectors);
297 } else if (GR_GL_SUPPORT_DESKTOP) {
298 GrGLint max;
299 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
300 fMaxFragmentUniformVectors = max / 4;
301 } else {
302 fMaxFragmentUniformVectors = 16;
303 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000304
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 ////////////////////////////////////////////////////////////////////////////
306 // Check for supported features.
307
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000308 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
twiz@google.com0f31ca72011-03-18 17:38:11 +0000310 GrGLint numFormats;
311 GR_GL_GetIntegerv(GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000312 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
twiz@google.com0f31ca72011-03-18 17:38:11 +0000313 GR_GL_GetIntegerv(GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000315 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000316 f8bitPaletteSupport = true;
317 break;
318 }
319 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000320
321 if (gPrintStartupSpew) {
322 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
323 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000324
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000325 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
326 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
327 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
328 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000329
330 memset(fAASamples, 0, sizeof(fAASamples));
331 fMSFBOType = kNone_MSFBO;
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000332 if (GR_GL_SUPPORT_ES) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000333 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000334 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000335 // and fbo_blit extensions.
336 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000337 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000338 fMSFBOType = kAppleES_MSFBO;
339 }
340 } else {
341 GrAssert(GR_GL_SUPPORT_DESKTOP);
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000342 if ((fGLVersion >= 3.f) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000343 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000344 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
345 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000346 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000347 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000349 if (gPrintStartupSpew) {
350 switch (fMSFBOType) {
351 case kNone_MSFBO:
352 GrPrintf("MSAA Support: NONE\n");
353 break;
354 case kDesktopARB_MSFBO:
355 GrPrintf("MSAA Support: DESKTOP ARB.\n");
356 break;
357 case kDesktopEXT_MSFBO:
358 GrPrintf("MSAA Support: DESKTOP EXT.\n");
359 break;
360 case kAppleES_MSFBO:
361 GrPrintf("MSAA Support: APPLE ES.\n");
362 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000363 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 }
365
366 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000367 GrGLint maxSamples;
bsalomon@google.comd1e433532011-03-21 21:38:40 +0000368 GR_GL_GetIntegerv(GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000370 fAASamples[kNone_GrAALevel] = 0;
371 fAASamples[kLow_GrAALevel] = GrMax(2,
372 GrFixedFloorToInt((GR_FixedHalf) *
373 maxSamples));
374 fAASamples[kMed_GrAALevel] = GrMax(2,
375 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
376 maxSamples));
377 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000379 if (gPrintStartupSpew) {
380 GrPrintf("\tMax Samples: %d\n", maxSamples);
381 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000382 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000383 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000384
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000385 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000386 fHasStencilWrap = (fGLVersion >= 1.4f) ||
387 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000388 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000389 fHasStencilWrap = (fGLVersion >= 2.0f) || this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000390 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000391 if (gPrintStartupSpew) {
392 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
393 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000395 if (GR_GL_SUPPORT_DESKTOP) {
396 // we could also look for GL_ATI_separate_stencil extension or
397 // GL_EXT_stencil_two_side but they use different function signatures
398 // than GL2.0+ (and than each other).
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000399 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000400 // supported on GL 1.4 and higher or by extension
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000401 fStencilWrapOpsSupport = (fGLVersion >= 1.4f) ||
402 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000403 } else {
404 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
405 // an ES1 extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000406 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000407 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000408 fStencilWrapOpsSupport = (fGLVersion >= 2.f) ||
409 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000410 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000411 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000412 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
413 (fTwoSidedStencilSupport ? "YES" : "NO"),
414 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000415 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000416
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000417 if (GR_GL_SUPPORT_DESKTOP) {
418 fRGBA8Renderbuffer = true;
419 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000420 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000421 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000422 if (gPrintStartupSpew) {
423 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
424 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000425
426
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000427 if (GR_GL_SUPPORT_ES) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000428 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000429 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000430 }
431 }
432
433 if (GR_GL_SUPPORT_DESKTOP) {
434 fBufferLockSupport = true; // we require VBO support and the desktop VBO
435 // extension includes glMapBuffer.
436 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000437 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000438 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000439
reed@google.comeeeb5a02010-12-23 15:12:59 +0000440 if (gPrintStartupSpew) {
441 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
442 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000443
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000444 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000445 if (fGLVersion >= 2.f ||
446 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000447 fNPOTTextureTileSupport = true;
448 fNPOTTextureSupport = true;
449 } else {
450 fNPOTTextureTileSupport = false;
451 fNPOTTextureSupport = false;
452 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000453 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000454 if (fGLVersion >= 2.f) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000455 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000456 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000457 } else {
458 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000459 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000460 fNPOTTextureTileSupport = false;
461 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000462 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000463
bsalomon@google.com205d4602011-04-25 12:43:45 +0000464 fAALineSupport = GR_GL_SUPPORT_DESKTOP;
465
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000467 // Experiments to determine limitations that can't be queried.
468 // TODO: Make these a preprocess that generate some compile time constants.
469 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000471 int expectNPOTTargets = GrGLGetGLInterface()->fNPOTRenderTargetSupport;
472 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000473 fNPOTRenderTargetSupport =
474 probe_for_npot_render_target_support(fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000475 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000476 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
477 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000478 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000479
bsalomon@google.com0748f212011-02-01 22:56:16 +0000480 if (gPrintStartupSpew) {
481 if (fNPOTTextureSupport) {
482 GrPrintf("NPOT textures supported\n");
483 if (fNPOTTextureTileSupport) {
484 GrPrintf("NPOT texture tiling supported\n");
485 } else {
486 GrPrintf("NPOT texture tiling NOT supported\n");
487 }
488 if (fNPOTRenderTargetSupport) {
489 GrPrintf("NPOT render targets supported\n");
490 } else {
491 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000492 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000494 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000495 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 }
497
bsalomon@google.com91958362011-06-13 17:58:13 +0000498 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
499 GR_GL_GetIntegerv(GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000500 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000501 // attachment, hence this min:
502 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000503
tomhudson@google.com747bf292011-06-14 18:16:52 +0000504 fMinRenderTargetHeight = GrGLGetGLInterface()->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000505 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000506 fMinRenderTargetHeight =
507 probe_for_min_render_target_height(fNPOTRenderTargetSupport,
508 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000509 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000510
tomhudson@google.com747bf292011-06-14 18:16:52 +0000511 fMinRenderTargetWidth = GrGLGetGLInterface()->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000512 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000513 fMinRenderTargetWidth =
514 probe_for_min_render_target_width(fNPOTRenderTargetSupport,
515 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000516 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000517
bsalomon@google.comfe676522011-06-17 18:12:21 +0000518 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000519}
520
521GrGpuGL::~GrGpuGL() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000522}
523
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000524void GrGpuGL::resetContext() {
525 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000526 fHWBlendDisabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000527 GR_GL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000528
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000529 // we don't use the zb at all
twiz@google.com0f31ca72011-03-18 17:38:11 +0000530 GR_GL(Disable(GR_GL_DEPTH_TEST));
531 GR_GL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000532
twiz@google.com0f31ca72011-03-18 17:38:11 +0000533 GR_GL(Disable(GR_GL_CULL_FACE));
534 GR_GL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000535 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000536
twiz@google.com0f31ca72011-03-18 17:38:11 +0000537 GR_GL(Disable(GR_GL_DITHER));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000538 if (GR_GL_SUPPORT_DESKTOP) {
539 GR_GL(Disable(GR_GL_LINE_SMOOTH));
540 GR_GL(Disable(GR_GL_POINT_SMOOTH));
541 GR_GL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000542 fHWAAState.fMSAAEnabled = false;
543 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000544 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000545
twiz@google.com0f31ca72011-03-18 17:38:11 +0000546 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000547 fHWDrawState.fFlagBits = 0;
548
reed@google.comac10a2d2010-12-22 21:39:39 +0000549 // we only ever use lines in hairline mode
550 GR_GL(LineWidth(1));
551
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000552 // invalid
553 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000556 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
557 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000558
559 fHWDrawState.fBlendConstant = 0x00000000;
560 GR_GL(BlendColor(0,0,0,0));
561
reed@google.comac10a2d2010-12-22 21:39:39 +0000562 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000563
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000564 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000565
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000566 for (int s = 0; s < kNumStages; ++s) {
567 fHWDrawState.fTextures[s] = NULL;
568 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
569 -GR_ScalarMax,
570 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000571 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000572 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000573 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000574
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000575 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000576 fHWBounds.fScissorEnabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000577 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000578 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000579
bsalomon@google.comd302f142011-03-03 13:54:13 +0000580 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000582 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000583
584 fHWGeometryState.fIndexBuffer = NULL;
585 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000586
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000587 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
twiz@google.com0f31ca72011-03-18 17:38:11 +0000589 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 fHWDrawState.fRenderTarget = NULL;
591}
592
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000593GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
594
595 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
596 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
597 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
598 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
599
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000600 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000601 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000602
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000603 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000604 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000605 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000606#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
607 if (desc.fSampleCnt) {
608#else
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000609 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000610#endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000611 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000612 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000613 } else {
614 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000615 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000616 }
617 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000618 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000619 }
620 // we don't know what the RB ids are without glGets and we don't care
621 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000622 rtDesc.fMSColorRenderbufferID = 0;
623#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
624 rtDesc.fSampleCnt = desc.fSampleCnt;
625#else
626 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
627 // just guess, this code path is only compiled in WK and we aren't
628 // using MSAA anyway. This will be stripped out soon when WK sets
629 // the fSampleCnt in GrPlatformSurfaceDesc.
630 rtDesc.fSampleCnt = 4;
631 } else {
632 rtDesc.fSampleCnt = 0;
633 }
634#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000635 if (desc.fStencilBits) {
636 GrGLStencilBuffer::Format format;
637 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
638 format.fPacked = false;
639 format.fStencilBits = desc.fStencilBits;
640 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000641 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
642 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000643 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000644 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000645 }
646
647 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000648 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000649 GrGLenum dontCare;
650 if (!canBeTexture(desc.fConfig, &dontCare,
651 &texDesc.fUploadFormat,
652 &texDesc.fUploadType)) {
653 return NULL;
654 }
655
656 GrGLTexture::TexParams params;
657
658 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
659 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
660
bsalomon@google.com90405932011-06-17 15:56:55 +0000661 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000662 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000663 texDesc.fTextureID = desc.fPlatformTexture;
664 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
665 texDesc.fOwnsID = false;
666
667 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000668 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000669 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000670 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000671 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000672 } else {
673 return new GrGLTexture(this, texDesc, params);
674 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000675 } else {
676 GrGLIRect viewport;
677 viewport.fLeft = 0;
678 viewport.fBottom = 0;
679 viewport.fWidth = desc.fWidth;
680 viewport.fHeight = desc.fHeight;
681
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000682 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000683 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000684 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000685 }
686}
687
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000688namespace {
689
690static const GrGLenum kUnknownGLFormat = ~0;
691
692GrGLenum get_fbo_color_format() {
693 GrGLint cbType;
694 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
695 GR_GL_COLOR_ATTACHMENT0,
696 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
697 &cbType);
698 GrGLint cbID;
699 GrGLint cbFormat;
700 switch (cbType) {
701 case GR_GL_RENDERBUFFER:
702 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
703 GR_GL_COLOR_ATTACHMENT0,
704 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
705 &cbID);
706 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
707 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
708 GR_GL_RENDERBUFFER_INTERNAL_FORMAT,
709 &cbFormat);
710 return cbFormat;
711 break;
712 case GR_GL_TEXTURE:
713 // ES doesn't have glGetTexLevelParameter
714 if (GR_GL_SUPPORT_DESKTOP) {
715 GrGLint cbLevel;
716 GrGLint cbFace;
717 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
718 GR_GL_COLOR_ATTACHMENT0,
719 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
720 &cbID);
721 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
722 GR_GL_COLOR_ATTACHMENT0,
723 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
724 &cbLevel);
725 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
726 GR_GL_COLOR_ATTACHMENT0,
727 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
728 &cbFace);
729 GrGLenum bind;
730 GrGLenum target;
731 if (cbFace) {
732 bind = GR_GL_TEXTURE_CUBE_MAP;
733 target = cbFace;
734 } else {
735 bind = GR_GL_TEXTURE_2D;
736 target = GR_GL_TEXTURE_2D;
737 }
738 GR_GL(BindTexture(bind, cbID));
739 GR_GL_GetTexLevelParameteriv(target, cbLevel,
740 GR_GL_TEXTURE_INTERNAL_FORMAT, &cbFormat);
741 return cbFormat;
742 } else {
743 return kUnknownGLFormat;
744 }
745 break;
746 default:
747 // we can get here with FBO 0, not a render buffer or a texture
748 return kUnknownGLFormat;
749 }
750}
751
752GrPixelConfig internal_color_format_to_config(GrGLenum iFormat) {
753 switch (iFormat) {
754 case GR_GL_RGB565:
755 return kRGB_565_GrPixelConfig;
756 case GR_GL_RGBA4:
757 return kRGBA_4444_GrPixelConfig;
758 case GR_GL_RGBA8:
759 case GR_GL_SRGB8_ALPHA8:
760 case GR_GL_SRGB_ALPHA:
761 case GR_GL_RGBA:
762 case GR_GL_BGRA:
763 return kRGBA_8888_GrPixelConfig;
764 case GR_GL_RGB8:
765 case GR_GL_SRGB8:
766 case GR_GL_SRGB:
767 return kRGBX_8888_GrPixelConfig;
768 default:
769 // there are many GL formats we don't have enums
770 // for. We should still render to them if the client
771 // asks us.
772 return kUnknown_GrPixelConfig;
773 }
774}
775
776GrPixelConfig get_implied_color_config(bool arbFBOExtension) {
777 GrGLint rSize, bSize, gSize, aSize;
778 if (arbFBOExtension) {
779 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
780 GR_GL_COLOR_ATTACHMENT0,
781 GR_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &rSize);
782 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
783 GR_GL_COLOR_ATTACHMENT0,
784 GR_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &gSize);
785 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
786 GR_GL_COLOR_ATTACHMENT0,
787 GR_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &bSize);
788 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
789 GR_GL_COLOR_ATTACHMENT0,
790 GR_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &aSize);
791 } else {
792 GR_GL_GetIntegerv(GR_GL_RED_BITS, &rSize);
793 GR_GL_GetIntegerv(GR_GL_GREEN_BITS, &gSize);
794 GR_GL_GetIntegerv(GR_GL_BLUE_BITS, &bSize);
795 GR_GL_GetIntegerv(GR_GL_ALPHA_BITS, &aSize);
796 }
797
798 if(8 == rSize && 8 == gSize && 8 == bSize) {
799 if (0 == aSize) {
800 return kRGBX_8888_GrPixelConfig;
801 } else if (8 == aSize) {
802 return kRGBA_8888_GrPixelConfig;
803 }
804 } else if (4 == rSize && 4 == gSize && 4 == bSize && 4 == aSize) {
805 return kRGBA_4444_GrPixelConfig;
806 } else if (5 == rSize && 6 == gSize && 5 == bSize && 0 == aSize) {
807 return kRGB_565_GrPixelConfig;
808 }
809 return kUnknown_GrPixelConfig;
810}
811
812int get_fbo_stencil_bits(bool arbFBOExtension) {
813 GrGLint stencilBits;
814 if (arbFBOExtension) {
815 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
816 GR_GL_STENCIL_ATTACHMENT,
817 GR_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
818 &stencilBits);
819 } else {
820 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &stencilBits);
821 }
822 return stencilBits;
823}
824}
825
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000826GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() {
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000827
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000828 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000829
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000830 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, (GrGLint*)&rtDesc.fRTFBOID);
831 rtDesc.fTexFBOID = rtDesc.fRTFBOID;
832 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000833
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000834 bool arbFBO = (GR_GL_SUPPORT_DESKTOP && (fGLVersion > 3.0 ||
835 this->hasExtension("GL_ARB_framebuffer_object")));
836
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000837 GrGLIRect viewport;
838 viewport.setFromGLViewport();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000839 int stencilBits = get_fbo_stencil_bits(arbFBO);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000840 GR_GL_GetIntegerv(GR_GL_SAMPLES, &rtDesc.fSampleCnt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000841
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000842 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000843 if (stencilBits) {
844 GrGLStencilBuffer::Format format;
845 // we could query this but we don't really need it
846 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
847 format.fPacked = false;
848 format.fStencilBits = stencilBits;
849 format.fTotalBits = stencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000850 sb.reset(new GrGLStencilBuffer(this, 0, viewport.fWidth,
851 viewport.fHeight, rtDesc.fSampleCnt,
852 format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000853 }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000854
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000855 GrGLenum fmat = get_fbo_color_format();
856 if (kUnknownGLFormat == fmat) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000857 rtDesc.fConfig = get_implied_color_config(arbFBO);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000858 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000859 rtDesc.fConfig = internal_color_format_to_config(fmat);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000860 }
861
862 // may have to bind a texture to gets its format
863 this->setSpareTextureUnit();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000864
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000865 rtDesc.fOwnIDs = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000866
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000867 GrGLRenderTarget* target = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000868 target->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000869 return target;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000870}
871
bsalomon@google.com5782d712011-01-21 21:03:59 +0000872///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000873static const GrGLuint kUnknownBitCount = ~0;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000874
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000875void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000876
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000877 // Build up list of legal stencil formats (though perhaps not supported on
878 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000879
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000880 // these consts are in order of most preferred to least preferred
881 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000882 static const GrGLStencilBuffer::Format
883 // internal Format stencil bits total bits packed?
884 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
885 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
886 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
887 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
888 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
889 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000890
891 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000892 bool supportsPackedDS = fGLVersion >= 3.0f ||
893 this->hasExtension("GL_EXT_packed_depth_stencil") ||
894 this->hasExtension("GL_ARB_framebuffer_object");
895
896 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
897 // require FBO support we can expect these are legal formats and don't
898 // check. These also all support the unsized GL_STENCIL_INDEX.
899 fStencilFormats.push_back() = gS8;
900 fStencilFormats.push_back() = gS16;
901 if (supportsPackedDS) {
902 fStencilFormats.push_back() = gD24S8;
903 }
904 fStencilFormats.push_back() = gS4;
905 if (supportsPackedDS) {
906 fStencilFormats.push_back() = gDS;
907 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000908 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000909 // ES2 has STENCIL_INDEX8 without extensions.
910 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
911 // introduces tokens for S1 thu S8 but there are separate extensions
912 // that make them legal (GL_OES_stencil1, ...).
913 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
914 // ES doesn't support using the unsized formats.
915
916 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
917 fStencilFormats.push_back() = gS8;
918 }
919 //fStencilFormats.push_back() = gS16;
920 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
921 fStencilFormats.push_back() = gD24S8;
922 }
923 if (this->hasExtension("GL_OES_stencil4")) {
924 fStencilFormats.push_back() = gS4;
925 }
926 // we require some stencil format.
927 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000928 }
929}
930
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000931////////////////////////////////////////////////////////////////////////////////
932
933void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
934 GrGLenum internalFormat,
935 const void* data,
936 size_t rowBytes) {
937 // we assume the texture is bound;
938 if (!rowBytes) {
939 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
940 }
941
942 // in case we need a temporary, trimmed copy of the src pixels
943 SkAutoSMalloc<128 * 128> tempStorage;
944
945 /*
946 * check whether to allocate a temporary buffer for flipping y or
947 * because our data has extra bytes past each row. If so, we need
948 * to trim those off here, since GL ES doesn't let us specify
949 * GL_UNPACK_ROW_LENGTH.
950 */
951 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
952 if (GR_GL_SUPPORT_DESKTOP && !flipY) {
953 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
954 GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
955 rowBytes / desc.fUploadByteCount));
956 }
957 } else {
958 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
959 if (data && (trimRowBytes < rowBytes || flipY)) {
960 // copy the data into our new storage, skipping the trailing bytes
961 size_t trimSize = desc.fContentHeight * trimRowBytes;
962 const char* src = (const char*)data;
963 if (flipY) {
964 src += (desc.fContentHeight - 1) * rowBytes;
965 }
966 char* dst = (char*)tempStorage.realloc(trimSize);
967 for (int y = 0; y < desc.fContentHeight; y++) {
968 memcpy(dst, src, trimRowBytes);
969 if (flipY) {
970 src -= rowBytes;
971 } else {
972 src += rowBytes;
973 }
974 dst += trimRowBytes;
975 }
976 // now point data to our trimmed version
977 data = tempStorage.get();
978 rowBytes = trimRowBytes;
979 }
980 }
981
982 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
983 if (kIndex_8_GrPixelConfig == desc.fFormat &&
984 supports8BitPalette()) {
985 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
986 GrAssert(desc.fContentWidth == desc.fAllocWidth);
987 GrAssert(desc.fContentHeight == desc.fAllocHeight);
988 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
989 kGrColorTableSize;
990 GR_GL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
991 desc.fAllocWidth, desc.fAllocHeight,
992 0, imageSize, data));
993 GrGLRestoreResetRowLength();
994 } else {
995 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
996 desc.fAllocHeight != desc.fContentHeight)) {
997 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
998 desc.fAllocWidth, desc.fAllocHeight,
999 0, desc.fUploadFormat, desc.fUploadType, NULL));
1000 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
1001 desc.fContentHeight, desc.fUploadFormat,
1002 desc.fUploadType, data));
1003 GrGLRestoreResetRowLength();
1004
1005 int extraW = desc.fAllocWidth - desc.fContentWidth;
1006 int extraH = desc.fAllocHeight - desc.fContentHeight;
1007 int maxTexels = extraW * extraH;
1008 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
1009 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
1010
1011 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
1012
1013 // rowBytes is actual stride between rows in data
1014 // rowDataBytes is the actual amount of non-pad data in a row
1015 // and the stride used for uploading extraH rows.
1016 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
1017 if (extraH) {
1018 uint8_t* lastRowStart = (uint8_t*) data +
1019 (desc.fContentHeight - 1) * rowBytes;
1020 uint8_t* extraRowStart = (uint8_t*)texels.get();
1021
1022 for (int i = 0; i < extraH; ++i) {
1023 memcpy(extraRowStart, lastRowStart, rowDataBytes);
1024 extraRowStart += rowDataBytes;
1025 }
1026 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, desc.fContentHeight,
1027 desc.fContentWidth, extraH,
1028 desc.fUploadFormat, desc.fUploadType,
1029 texels.get()));
1030 }
1031 if (extraW) {
1032 uint8_t* edgeTexel = (uint8_t*)data +
1033 rowDataBytes - desc.fUploadByteCount;
1034 uint8_t* extraTexel = (uint8_t*)texels.get();
1035 for (int j = 0; j < desc.fContentHeight; ++j) {
1036 for (int i = 0; i < extraW; ++i) {
1037 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
1038 extraTexel += desc.fUploadByteCount;
1039 }
1040 edgeTexel += rowBytes;
1041 }
1042 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth, 0,
1043 extraW, desc.fContentHeight,
1044 desc.fUploadFormat, desc.fUploadType,
1045 texels.get()));
1046 }
1047 if (extraW && extraH) {
1048 uint8_t* cornerTexel = (uint8_t*)data +
1049 desc.fContentHeight * rowBytes -
1050 desc.fUploadByteCount;
1051 uint8_t* extraTexel = (uint8_t*)texels.get();
1052 for (int i = 0; i < extraW*extraH; ++i) {
1053 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
1054 extraTexel += desc.fUploadByteCount;
1055 }
1056 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
1057 desc.fContentHeight, extraW, extraH,
1058 desc.fUploadFormat, desc.fUploadType,
1059 texels.get()));
1060 }
1061
1062 } else {
1063 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1064 desc.fAllocWidth, desc.fAllocHeight, 0,
1065 desc.fUploadFormat, desc.fUploadType, data));
1066 GrGLRestoreResetRowLength();
1067 }
1068 }
1069}
1070
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001071bool GrGpuGL::createRenderTargetObjects(int width, int height,
1072 GrGLuint texID,
1073 GrGLRenderTarget::Desc* desc) {
1074 desc->fMSColorRenderbufferID = 0;
1075 desc->fRTFBOID = 0;
1076 desc->fTexFBOID = 0;
1077 desc->fOwnIDs = true;
1078
1079 GrGLenum status;
1080 GrGLint err;
1081
bsalomon@google.comab15d612011-08-09 12:57:56 +00001082 GrGLenum msColorFormat = 0; // suppress warning
1083
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001084 GR_GL(GenFramebuffers(1, &desc->fTexFBOID));
1085 if (!desc->fTexFBOID) {
1086 goto FAILED;
1087 }
1088
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089
1090 // If we are using multisampling we will create two FBOS. We render
1091 // to one and then resolve to the texture bound to the other.
1092 if (desc->fSampleCnt > 1 && kNone_MSFBO != fMSFBOType) {
1093 GR_GL(GenFramebuffers(1, &desc->fRTFBOID));
1094 GR_GL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
1095 if (!desc->fRTFBOID ||
1096 !desc->fMSColorRenderbufferID ||
1097 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
1098 goto FAILED;
1099 }
1100 } else {
1101 desc->fRTFBOID = desc->fTexFBOID;
1102 }
1103
1104 if (desc->fRTFBOID != desc->fTexFBOID) {
1105 GrAssert(desc->fSampleCnt > 1);
1106 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
1107 desc->fMSColorRenderbufferID));
1108 GR_GL_NO_ERR(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1109 desc->fSampleCnt,
1110 msColorFormat,
1111 width, height));
1112 err = GrGLGetGLInterface()->fGetError();
1113 if (err != GR_GL_NO_ERROR) {
1114 goto FAILED;
1115 }
1116 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
1117 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1118 GR_GL_COLOR_ATTACHMENT0,
1119 GR_GL_RENDERBUFFER,
1120 desc->fMSColorRenderbufferID));
1121 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1122 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1123 goto FAILED;
1124 }
1125 }
1126 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
1127
1128 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1129 GR_GL_COLOR_ATTACHMENT0,
1130 GR_GL_TEXTURE_2D,
1131 texID, 0));
1132 status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1133 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1134 goto FAILED;
1135 }
1136
1137 return true;
1138
1139FAILED:
1140 if (desc->fMSColorRenderbufferID) {
1141 GR_GL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
1142 }
1143 if (desc->fRTFBOID != desc->fTexFBOID) {
1144 GR_GL(DeleteFramebuffers(1, &desc->fRTFBOID));
1145 }
1146 if (desc->fTexFBOID) {
1147 GR_GL(DeleteFramebuffers(1, &desc->fTexFBOID));
1148 }
1149 return false;
1150}
1151
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001152// good to set a break-point here to know when createTexture fails
1153static GrTexture* return_null_texture() {
1154// GrAssert(!"null texture");
1155 return NULL;
1156}
1157
1158#if GR_DEBUG
1159static size_t as_size_t(int x) {
1160 return x;
1161}
1162#endif
1163
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001164GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001165 const void* srcData,
1166 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001167
1168#if GR_COLLECT_STATS
1169 ++fStats.fTextureCreateCnt;
1170#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001171
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001172 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001173 GR_GL_NEAREST,
1174 GR_GL_CLAMP_TO_EDGE,
1175 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001176 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001177
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001178 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001179 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001180 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001181
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001182 glTexDesc.fContentWidth = desc.fWidth;
1183 glTexDesc.fContentHeight = desc.fHeight;
1184 glTexDesc.fAllocWidth = desc.fWidth;
1185 glTexDesc.fAllocHeight = desc.fHeight;
1186 glTexDesc.fFormat = desc.fFormat;
1187 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001188
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001189 glRTDesc.fMSColorRenderbufferID = 0;
1190 glRTDesc.fRTFBOID = 0;
1191 glRTDesc.fTexFBOID = 0;
1192 glRTDesc.fOwnIDs = true;
1193 glRTDesc.fConfig = glTexDesc.fFormat;
1194
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001195 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 if (!canBeTexture(desc.fFormat,
1197 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001198 &glTexDesc.fUploadFormat,
1199 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001200 return return_null_texture();
1201 }
1202
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001203 // We keep GrRenderTargets in GL's normal orientation so that they
1204 // can be drawn to by the outside world without the client having
1205 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001206 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001208
reed@google.comac10a2d2010-12-22 21:39:39 +00001209 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001210 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001211 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001212 GrPrintf("AA RT requested but not supported on this platform.");
1213 }
1214
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001215 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001216
reed@google.comac10a2d2010-12-22 21:39:39 +00001217 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001218 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001219 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1220 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001221 }
1222
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001223 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 glTexDesc.fAllocWidth);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001225 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 glTexDesc.fAllocHeight);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001227 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1228 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001229 return return_null_texture();
1230 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001231 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001232 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1233 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1234 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1235 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001236 return return_null_texture();
1237 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 }
1239
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001240 GR_GL(GenTextures(1, &glTexDesc.fTextureID));
1241 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001242 return return_null_texture();
1243 }
1244
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001245 this->setSpareTextureUnit();
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001246 GR_GL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001247 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1248 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001249 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001250 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1251 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001252 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001253 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1254 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001255 DEFAULT_PARAMS.fWrapS));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001256 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1257 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001258 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001259
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001260 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001261
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001262 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001264 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001265#if GR_COLLECT_STATS
1266 ++fStats.fRenderTargetCreateCnt;
1267#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1269 glTexDesc.fAllocHeight,
1270 glTexDesc.fTextureID,
1271 &glRTDesc)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001272 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 return return_null_texture();
1274 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001275 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1276 } else {
1277 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 }
1279#ifdef TRACE_TEXTURE_CREATION
1280 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1281 tex->fTextureID, width, height, tex->fUploadByteCount);
1282#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001283 return tex;
1284}
1285
1286namespace {
1287void inline get_stencil_rb_sizes(GrGLuint rb, GrGLStencilBuffer::Format* format) {
1288 // we shouldn't ever know one size and not the other
1289 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1290 (kUnknownBitCount == format->fTotalBits));
1291 if (kUnknownBitCount == format->fStencilBits) {
1292 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
1293 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1294 (GrGLint*)&format->fStencilBits);
1295 if (format->fPacked) {
1296 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
1297 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1298 (GrGLint*)&format->fTotalBits);
1299 format->fTotalBits += format->fStencilBits;
1300 } else {
1301 format->fTotalBits = format->fStencilBits;
1302 }
1303 }
1304}
1305}
1306
1307bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1308 int width, int height) {
1309
1310 // All internally created RTs are also textures. We don't create
1311 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1312 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001313 GrAssert(width >= rt->allocatedWidth());
1314 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001315
1316 int samples = rt->numSamples();
1317 GrGLuint sbID;
1318 GR_GL(GenRenderbuffers(1, &sbID));
1319 if (!sbID) {
1320 return false;
1321 }
1322
1323 GrGLStencilBuffer* sb = NULL;
1324
1325 int stencilFmtCnt = fStencilFormats.count();
1326 for (int i = 0; i < stencilFmtCnt; ++i) {
1327 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
1328 // we start with the last stencil format that succeeded in hopes
1329 // that we won't go through this loop more than once after the
1330 // first (painful) stencil creation.
1331 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001332 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001333 // version on a GL that doesn't have an MSAA extension.
1334 if (samples > 1) {
1335 GR_GL_NO_ERR(RenderbufferStorageMultisample(
1336 GR_GL_RENDERBUFFER,
1337 samples,
1338 fStencilFormats[sIdx].fInternalFormat,
1339 width,
1340 height));
1341 } else {
1342 GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER,
1343 fStencilFormats[sIdx].fInternalFormat,
1344 width, height));
1345 }
1346
1347 GrGLenum err = GrGLGetGLInterface()->fGetError();
1348 if (err == GR_GL_NO_ERROR) {
1349 // After sized formats we attempt an unsized format and take whatever
1350 // sizes GL gives us. In that case we query for the size.
1351 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
1352 get_stencil_rb_sizes(sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001353 sb = new GrGLStencilBuffer(this, sbID, width, height,
1354 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001355 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1356 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001357 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001358 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001359 return true;
1360 }
1361 sb->abandon(); // otherwise we lose sbID
1362 sb->unref();
1363 }
1364 }
1365 GR_GL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001366 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001367}
1368
1369bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1370 GrRenderTarget* rt) {
1371 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1372
1373 GrGLuint fbo = glrt->renderFBOID();
1374
1375 if (NULL == sb) {
1376 if (NULL != rt->getStencilBuffer()) {
1377 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1378 GR_GL_STENCIL_ATTACHMENT,
1379 GR_GL_RENDERBUFFER, 0));
1380 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1381 GR_GL_DEPTH_ATTACHMENT,
1382 GR_GL_RENDERBUFFER, 0));
1383#if GR_DEBUG
1384 GrGLenum status =
1385 GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1386 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1387#endif
1388 }
1389 return true;
1390 } else {
1391 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1392 GrGLuint rb = glsb->renderbufferID();
1393
reed@google.comac10a2d2010-12-22 21:39:39 +00001394 fHWDrawState.fRenderTarget = NULL;
1395
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001396 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1397 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1398 GR_GL_STENCIL_ATTACHMENT,
1399 GR_GL_RENDERBUFFER, rb));
1400 if (glsb->format().fPacked) {
1401 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1402 GR_GL_DEPTH_ATTACHMENT,
1403 GR_GL_RENDERBUFFER, rb));
1404 } else {
1405 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1406 GR_GL_DEPTH_ATTACHMENT,
1407 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001409
1410 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1411 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1412 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1413 GR_GL_STENCIL_ATTACHMENT,
1414 GR_GL_RENDERBUFFER, 0));
1415 if (glsb->format().fPacked) {
1416 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1417 GR_GL_DEPTH_ATTACHMENT,
1418 GR_GL_RENDERBUFFER, 0));
1419 }
1420 return false;
1421 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001422 return true;
1423 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001424 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001425}
1426
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001427////////////////////////////////////////////////////////////////////////////////
1428
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001429GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001430 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001431 GR_GL(GenBuffers(1, &id));
1432 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001433 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001434 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001435 GrGLClearErr();
1436 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001437 GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1438 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1439 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001440 GR_GL(DeleteBuffers(1, &id));
1441 // deleting bound buffer does implicit bind to 0
1442 fHWGeometryState.fVertexBuffer = NULL;
1443 return NULL;
1444 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001445 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001446 size, dynamic);
1447 fHWGeometryState.fVertexBuffer = vertexBuffer;
1448 return vertexBuffer;
1449 }
1450 return NULL;
1451}
1452
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001453GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001454 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001455 GR_GL(GenBuffers(1, &id));
1456 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001457 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001458 GrGLClearErr();
1459 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001460 GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1461 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1462 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 GR_GL(DeleteBuffers(1, &id));
1464 // deleting bound buffer does implicit bind to 0
1465 fHWGeometryState.fIndexBuffer = NULL;
1466 return NULL;
1467 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001468 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001469 size, dynamic);
1470 fHWGeometryState.fIndexBuffer = indexBuffer;
1471 return indexBuffer;
1472 }
1473 return NULL;
1474}
1475
reed@google.comac10a2d2010-12-22 21:39:39 +00001476void GrGpuGL::flushScissor(const GrIRect* rect) {
1477 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001478 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001479 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001480
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001481 GrGLIRect scissor;
1482 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001483 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001484 rect->width(), rect->height());
1485 if (scissor.contains(vp)) {
1486 rect = NULL;
1487 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001488 }
1489
1490 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001492 scissor.pushToGLScissor();
reed@google.comac10a2d2010-12-22 21:39:39 +00001493 fHWBounds.fScissorRect = scissor;
1494 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001495 if (!fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001496 GR_GL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001497 fHWBounds.fScissorEnabled = true;
1498 }
1499 } else {
1500 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001501 GR_GL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 fHWBounds.fScissorEnabled = false;
1503 }
1504 }
1505}
1506
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001507void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001508 if (NULL == fCurrDrawState.fRenderTarget) {
1509 return;
1510 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001511 GrIRect r;
1512 if (NULL != rect) {
1513 // flushScissor expects rect to be clipped to the target.
1514 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001515 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1516 fCurrDrawState.fRenderTarget->height());
1517 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001518 rect = &r;
1519 } else {
1520 return;
1521 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001522 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001523 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001524 this->flushScissor(rect);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001525 GR_GL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001526 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
reed@google.comac10a2d2010-12-22 21:39:39 +00001527 GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
1528 GrColorUnpackG(color)/255.f,
1529 GrColorUnpackB(color)/255.f,
1530 GrColorUnpackA(color)/255.f));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001531 GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001532}
1533
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001534void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001535 if (NULL == fCurrDrawState.fRenderTarget) {
1536 return;
1537 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001538
1539 this->flushRenderTarget(&GrIRect::EmptyIRect());
1540
reed@google.comac10a2d2010-12-22 21:39:39 +00001541 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001542 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001543 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001544 }
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001545 GR_GL(StencilMask(0xffffffff));
1546 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001547 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001548 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001549}
1550
bsalomon@google.com398109c2011-04-14 18:40:27 +00001551void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001552 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001553
1554 // this should only be called internally when we know we have a
1555 // stencil buffer.
1556 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001557#if 0
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001558 GrGLint stencilBitCount =
1559 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001560 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001561 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001562#else
1563 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001564 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001565 // turned into draws. Our contract on GrDrawTarget says that
1566 // changing the clip between stencil passes may or may not
1567 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001568 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001569#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001570 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001571 this->flushScissor(&rect);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001572 GR_GL(StencilMask(clipStencilMask));
1573 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001574 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001575 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001576}
1577
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001578void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001579 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001580}
1581
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001582bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1583 int left, int top, int width, int height,
1584 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001585 GrGLenum internalFormat; // we don't use this for glReadPixels
1586 GrGLenum format;
1587 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1589 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001590 }
1591 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1592 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1593 switch (tgt->getResolveType()) {
1594 case GrGLRenderTarget::kCantResolve_ResolveType:
1595 return false;
1596 case GrGLRenderTarget::kAutoResolves_ResolveType:
1597 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1598 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001599 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001600 break;
1601 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001602 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001603 // we don't track the state of the READ FBO ID.
1604 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
1605 break;
1606 default:
1607 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001608 }
1609
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001610 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001611
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001612 // the read rect is viewport-relative
1613 GrGLIRect readRect;
1614 readRect.setRelativeTo(glvp, left, top, width, height);
1615 GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001616 readRect.fWidth, readRect.fHeight,
bsalomon@google.com316f99232011-01-13 21:28:12 +00001617 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001618
1619 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1620 // API presents top-to-bottom
1621 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001622 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001623 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001624 void* tmp = rowStorage.get();
1625
1626 const int halfY = height >> 1;
1627 char* top = reinterpret_cast<char*>(buffer);
1628 char* bottom = top + (height - 1) * stride;
1629 for (int y = 0; y < halfY; y++) {
1630 memcpy(tmp, top, stride);
1631 memcpy(top, bottom, stride);
1632 memcpy(bottom, tmp, stride);
1633 top += stride;
1634 bottom -= stride;
1635 }
1636 }
1637 return true;
1638}
1639
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001640void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001641
1642 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1643
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001644 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001645 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001646 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 #if GR_COLLECT_STATS
1648 ++fStats.fRenderTargetChngCnt;
1649 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001650 #if GR_DEBUG
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001651 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1652 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001653 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001654 }
1655 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001656 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001657 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001658 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001659 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001660 vp.pushToGLViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001661 fHWBounds.fViewportRect = vp;
1662 }
1663 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001664 if (NULL == bound || !bound->isEmpty()) {
1665 rt->flagAsNeedingResolve(bound);
1666 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001667}
1668
twiz@google.com0f31ca72011-03-18 17:38:11 +00001669GrGLenum gPrimitiveType2GLMode[] = {
1670 GR_GL_TRIANGLES,
1671 GR_GL_TRIANGLE_STRIP,
1672 GR_GL_TRIANGLE_FAN,
1673 GR_GL_POINTS,
1674 GR_GL_LINES,
1675 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001676};
1677
bsalomon@google.comd302f142011-03-03 13:54:13 +00001678#define SWAP_PER_DRAW 0
1679
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001680#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001681 #if GR_MAC_BUILD
1682 #include <AGL/agl.h>
1683 #elif GR_WIN32_BUILD
1684 void SwapBuf() {
1685 DWORD procID = GetCurrentProcessId();
1686 HWND hwnd = GetTopWindow(GetDesktopWindow());
1687 while(hwnd) {
1688 DWORD wndProcID = 0;
1689 GetWindowThreadProcessId(hwnd, &wndProcID);
1690 if(wndProcID == procID) {
1691 SwapBuffers(GetDC(hwnd));
1692 }
1693 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1694 }
1695 }
1696 #endif
1697#endif
1698
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001699void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1700 uint32_t startVertex,
1701 uint32_t startIndex,
1702 uint32_t vertexCount,
1703 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001704 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1705
twiz@google.com0f31ca72011-03-18 17:38:11 +00001706 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001707
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001708 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1709 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1710
1711 // our setupGeometry better have adjusted this to zero since
1712 // DrawElements always draws from the begining of the arrays for idx 0.
1713 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001714
1715 GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
twiz@google.com0f31ca72011-03-18 17:38:11 +00001716 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001717#if SWAP_PER_DRAW
1718 glFlush();
1719 #if GR_MAC_BUILD
1720 aglSwapBuffers(aglGetCurrentContext());
1721 int set_a_break_pt_here = 9;
1722 aglSwapBuffers(aglGetCurrentContext());
1723 #elif GR_WIN32_BUILD
1724 SwapBuf();
1725 int set_a_break_pt_here = 9;
1726 SwapBuf();
1727 #endif
1728#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001729}
1730
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001731void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1732 uint32_t startVertex,
1733 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1735
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001736 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1737
1738 // our setupGeometry better have adjusted this to zero.
1739 // DrawElements doesn't take an offset so we always adjus the startVertex.
1740 GrAssert(0 == startVertex);
1741
1742 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1743 // account for startVertex in the DrawElements case. So we always
1744 // rely on setupGeometry to have accounted for startVertex.
reed@google.comac10a2d2010-12-22 21:39:39 +00001745 GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001746#if SWAP_PER_DRAW
1747 glFlush();
1748 #if GR_MAC_BUILD
1749 aglSwapBuffers(aglGetCurrentContext());
1750 int set_a_break_pt_here = 9;
1751 aglSwapBuffers(aglGetCurrentContext());
1752 #elif GR_WIN32_BUILD
1753 SwapBuf();
1754 int set_a_break_pt_here = 9;
1755 SwapBuf();
1756 #endif
1757#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001758}
1759
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001760void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001761
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001762 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 GrAssert(kNone_MSFBO != fMSFBOType);
1764 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001765 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 rt->renderFBOID()));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001767 GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 rt->textureFBOID()));
1769 #if GR_COLLECT_STATS
1770 ++fStats.fRenderTargetChngCnt;
1771 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001772 // make sure we go through flushRenderTarget() since we've modified
1773 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001774 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001775 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001776 const GrIRect dirtyRect = rt->getResolveRect();
1777 GrGLIRect r;
1778 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1779 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001780
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001781 if (kAppleES_MSFBO == fMSFBOType) {
1782 // Apple's extension uses the scissor as the blit bounds.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001783 GR_GL(Enable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001784 GR_GL(Scissor(r.fLeft, r.fBottom,
1785 r.fWidth, r.fHeight));
twiz@google.com59a190b2011-03-14 21:23:01 +00001786 GR_GL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001787 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001788 fHWBounds.fScissorEnabled = true;
1789 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001790 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001791 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001792 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1793 flushScissor(NULL);
1794 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001795 int right = r.fLeft + r.fWidth;
1796 int top = r.fBottom + r.fHeight;
1797 GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1798 r.fLeft, r.fBottom, right, top,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001799 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001800 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001801 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001802 }
1803}
1804
twiz@google.com0f31ca72011-03-18 17:38:11 +00001805static const GrGLenum grToGLStencilFunc[] = {
1806 GR_GL_ALWAYS, // kAlways_StencilFunc
1807 GR_GL_NEVER, // kNever_StencilFunc
1808 GR_GL_GREATER, // kGreater_StencilFunc
1809 GR_GL_GEQUAL, // kGEqual_StencilFunc
1810 GR_GL_LESS, // kLess_StencilFunc
1811 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1812 GR_GL_EQUAL, // kEqual_StencilFunc,
1813 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001814};
1815GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1816GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1817GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1818GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1819GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1820GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1821GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1822GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1823GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1824
twiz@google.com0f31ca72011-03-18 17:38:11 +00001825static const GrGLenum grToGLStencilOp[] = {
1826 GR_GL_KEEP, // kKeep_StencilOp
1827 GR_GL_REPLACE, // kReplace_StencilOp
1828 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1829 GR_GL_INCR, // kIncClamp_StencilOp
1830 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1831 GR_GL_DECR, // kDecClamp_StencilOp
1832 GR_GL_ZERO, // kZero_StencilOp
1833 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001834};
1835GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1836GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1837GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1838GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1839GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1840GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1841GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1842GR_STATIC_ASSERT(6 == kZero_StencilOp);
1843GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1844
reed@google.comac10a2d2010-12-22 21:39:39 +00001845void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001846 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001847
1848 // use stencil for clipping if clipping is enabled and the clip
1849 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001850 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001851 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001852 bool stencilChange = fHWStencilClip != stencilClip ||
1853 fHWDrawState.fStencilSettings != *settings ||
1854 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1855 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001856
1857 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001858
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1860 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001861
bsalomon@google.comd302f142011-03-03 13:54:13 +00001862 if (settings->isDisabled()) {
1863 if (stencilClip) {
1864 settings = &gClipStencilSettings;
1865 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001866 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001867
1868 if (settings->isDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001869 GR_GL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001870 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001871 GR_GL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001872 #if GR_DEBUG
1873 if (!fStencilWrapOpsSupport) {
1874 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1875 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1876 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1877 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1878 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1879 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1880 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1881 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1882 }
1883 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001884 int stencilBits = 0;
1885 GrStencilBuffer* stencilBuffer =
1886 fCurrDrawState.fRenderTarget->getStencilBuffer();
1887 if (NULL != stencilBuffer) {
1888 stencilBits = stencilBuffer->bits();
1889 }
1890 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001891 GrAssert(stencilBits ||
1892 (GrStencilSettings::gDisabled ==
1893 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001894 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1895 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001896
1897 unsigned int frontRef = settings->fFrontFuncRef;
1898 unsigned int frontMask = settings->fFrontFuncMask;
1899 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001900 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001901
1902 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1903
1904 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1905 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1906 } else {
1907 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1908
1909 ConvertStencilFuncAndMask(settings->fFrontFunc,
1910 stencilClip,
1911 clipStencilMask,
1912 userStencilMask,
1913 &frontRef,
1914 &frontMask);
1915 frontWriteMask &= userStencilMask;
1916 }
1917 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001918 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001919 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001920 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001921 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001922 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001924 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001925 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001926 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001927
1928 unsigned int backRef = settings->fBackFuncRef;
1929 unsigned int backMask = settings->fBackFuncMask;
1930 unsigned int backWriteMask = settings->fBackWriteMask;
1931
1932
1933 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1934 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1935 backFunc = grToGLStencilFunc[settings->fBackFunc];
1936 } else {
1937 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1938 ConvertStencilFuncAndMask(settings->fBackFunc,
1939 stencilClip,
1940 clipStencilMask,
1941 userStencilMask,
1942 &backRef,
1943 &backMask);
1944 backWriteMask &= userStencilMask;
1945 }
1946
twiz@google.com0f31ca72011-03-18 17:38:11 +00001947 GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask));
1948 GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1949 GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask));
1950 GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1951 GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001952 grToGLStencilOp[settings->fFrontPassOp],
1953 grToGLStencilOp[settings->fFrontPassOp]));
1954
twiz@google.com0f31ca72011-03-18 17:38:11 +00001955 GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001956 grToGLStencilOp[settings->fBackPassOp],
1957 grToGLStencilOp[settings->fBackPassOp]));
1958 } else {
1959 GR_GL(StencilFunc(frontFunc, frontRef, frontMask));
1960 GR_GL(StencilMask(frontWriteMask));
1961 GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
1962 grToGLStencilOp[settings->fFrontPassOp],
1963 grToGLStencilOp[settings->fFrontPassOp]));
1964 }
1965 }
1966 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001967 fHWStencilClip = stencilClip;
1968 }
1969}
1970
bsalomon@google.com0650e812011-04-08 18:07:53 +00001971bool GrGpuGL::useSmoothLines() {
1972 // there is a conflict between using smooth lines and our use of
1973 // premultiplied alpha. Smooth lines tweak the incoming alpha value
1974 // but not in a premul-alpha way. So we only use them when our alpha
1975 // is 0xff.
1976
1977 // TODO: write a smarter line frag shader.
1978
1979 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
1980 canDisableBlend();
1981}
1982
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001983void GrGpuGL::flushAAState(GrPrimitiveType type) {
1984 if (GR_GL_SUPPORT_DESKTOP) {
1985 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1986 // smooth lines.
1987
1988 // we prefer smooth lines over multisampled lines
1989 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001990 if (GrIsPrimTypeLines(type)) {
1991 bool smooth = useSmoothLines();
1992 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001993 GR_GL(Enable(GR_GL_LINE_SMOOTH));
1994 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001995 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001996 GR_GL(Disable(GR_GL_LINE_SMOOTH));
1997 fHWAAState.fSmoothLineEnabled = false;
1998 }
1999 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2000 fHWAAState.fMSAAEnabled) {
2001 GR_GL(Disable(GR_GL_MULTISAMPLE));
2002 fHWAAState.fMSAAEnabled = false;
2003 }
2004 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2005 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
2006 fHWAAState.fMSAAEnabled) {
2007 if (fHWAAState.fMSAAEnabled) {
2008 GR_GL(Disable(GR_GL_MULTISAMPLE));
2009 fHWAAState.fMSAAEnabled = false;
2010 } else {
2011 GR_GL(Enable(GR_GL_MULTISAMPLE));
2012 fHWAAState.fMSAAEnabled = true;
2013 }
2014 }
2015 }
2016}
2017
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002018void GrGpuGL::flushBlend(GrPrimitiveType type,
2019 GrBlendCoeff srcCoeff,
2020 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002021 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
2022 if (fHWBlendDisabled) {
2023 GR_GL(Enable(GR_GL_BLEND));
2024 fHWBlendDisabled = false;
2025 }
2026 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
2027 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
2028 GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2029 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
2030 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
2031 fHWDrawState.fDstBlend = kISA_BlendCoeff;
2032 }
2033 } else {
2034 bool blendOff = canDisableBlend();
2035 if (fHWBlendDisabled != blendOff) {
2036 if (blendOff) {
2037 GR_GL(Disable(GR_GL_BLEND));
2038 } else {
2039 GR_GL(Enable(GR_GL_BLEND));
2040 }
2041 fHWBlendDisabled = blendOff;
2042 }
2043 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002044 if (fHWDrawState.fSrcBlend != srcCoeff ||
2045 fHWDrawState.fDstBlend != dstCoeff) {
2046 GR_GL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2047 gXfermodeCoeff2Blend[dstCoeff]));
2048 fHWDrawState.fSrcBlend = srcCoeff;
2049 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002050 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002051 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2052 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002053 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2054
2055 float c[] = {
2056 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2057 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2058 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2059 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2060 };
2061 GR_GL(BlendColor(c[0], c[1], c[2], c[3]));
2062 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2063 }
2064 }
2065 }
2066}
2067
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002068static unsigned grToGLFilter(GrSamplerState::Filter filter) {
2069 switch (filter) {
2070 case GrSamplerState::kBilinear_Filter:
2071 case GrSamplerState::k4x4Downsample_Filter:
2072 return GR_GL_LINEAR;
2073 case GrSamplerState::kNearest_Filter:
2074 case GrSamplerState::kConvolution_Filter:
2075 return GR_GL_NEAREST;
2076 default:
2077 GrAssert(!"Unknown filter type");
2078 return GR_GL_LINEAR;
2079 }
2080}
2081
bsalomon@google.comffca4002011-02-22 20:34:01 +00002082bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002083
2084 // GrGpu::setupClipAndFlushState should have already checked this
2085 // and bailed if not true.
2086 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002087
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002088 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002089 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002090 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002091 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002092
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002093 // true for now, but maybe not with GrEffect.
2094 GrAssert(NULL != nextTexture);
2095 // if we created a rt/tex and rendered to it without using a
2096 // texture and now we're texuring from the rt it will still be
2097 // the last bound texture, but it needs resolving. So keep this
2098 // out of the "last != next" check.
2099 GrGLRenderTarget* texRT =
2100 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2101 if (NULL != texRT) {
2102 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002103 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002104
2105 if (fHWDrawState.fTextures[s] != nextTexture) {
2106 setTextureUnit(s);
2107 GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
2108 #if GR_COLLECT_STATS
2109 ++fStats.fTextureChngCnt;
2110 #endif
2111 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2112 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002113 // The texture matrix has to compensate for texture width/height
2114 // and NPOT-embedded-in-POT
2115 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002116 }
2117
2118 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2119 const GrGLTexture::TexParams& oldTexParams =
2120 nextTexture->getTexParams();
2121 GrGLTexture::TexParams newTexParams;
2122
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002123 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002124
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002125 newTexParams.fWrapS =
2126 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()];
2127 newTexParams.fWrapT =
2128 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()];
2129
2130 if (newTexParams.fFilter != oldTexParams.fFilter) {
2131 setTextureUnit(s);
2132 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2133 GR_GL_TEXTURE_MAG_FILTER,
2134 newTexParams.fFilter));
2135 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2136 GR_GL_TEXTURE_MIN_FILTER,
2137 newTexParams.fFilter));
2138 }
2139 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2140 setTextureUnit(s);
2141 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2142 GR_GL_TEXTURE_WRAP_S,
2143 newTexParams.fWrapS));
2144 }
2145 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2146 setTextureUnit(s);
2147 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2148 GR_GL_TEXTURE_WRAP_T,
2149 newTexParams.fWrapT));
2150 }
2151 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002152 }
2153 }
2154
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002155 GrIRect* rect = NULL;
2156 GrIRect clipBounds;
2157 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2158 fClip.hasConservativeBounds()) {
2159 fClip.getConservativeBounds().roundOut(&clipBounds);
2160 rect = &clipBounds;
2161 }
2162 this->flushRenderTarget(rect);
2163 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002164
reed@google.comac10a2d2010-12-22 21:39:39 +00002165 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2166 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2167 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002168 GR_GL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002169 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002170 GR_GL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002171 }
2172 }
2173
bsalomon@google.comd302f142011-03-03 13:54:13 +00002174 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2175 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002176 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002177 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002178 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002179 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002180 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002181 }
2182 GR_GL(ColorMask(mask, mask, mask, mask));
2183 }
2184
bsalomon@google.comd302f142011-03-03 13:54:13 +00002185 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2186 switch (fCurrDrawState.fDrawFace) {
2187 case kCCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002188 GR_GL(Enable(GR_GL_CULL_FACE));
2189 GR_GL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002190 break;
2191 case kCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002192 GR_GL(Enable(GR_GL_CULL_FACE));
2193 GR_GL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002194 break;
2195 case kBoth_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002196 GR_GL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002197 break;
2198 default:
2199 GrCrash("Unknown draw face.");
2200 }
2201 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2202 }
2203
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002204#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002205 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002206 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002207 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002208 NULL == fCurrDrawState.fRenderTarget ||
2209 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002210 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002211 fCurrDrawState.fRenderTarget);
2212 }
2213#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002214
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 flushStencil();
2216
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002218 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002219 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002220}
2221
2222void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002223 if (fHWGeometryState.fVertexBuffer != buffer) {
2224 fHWGeometryState.fArrayPtrsDirty = true;
2225 fHWGeometryState.fVertexBuffer = buffer;
2226 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002227}
2228
2229void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002230 if (fHWGeometryState.fVertexBuffer == buffer) {
2231 // deleting bound buffer does implied bind to 0
2232 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002233 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002234 }
2235}
2236
2237void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002238 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002239}
2240
2241void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002242 if (fHWGeometryState.fIndexBuffer == buffer) {
2243 // deleting bound buffer does implied bind to 0
2244 fHWGeometryState.fIndexBuffer = NULL;
2245 }
2246}
2247
reed@google.comac10a2d2010-12-22 21:39:39 +00002248void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2249 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002250 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002251 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002252 }
2253 if (fHWDrawState.fRenderTarget == renderTarget) {
2254 fHWDrawState.fRenderTarget = NULL;
2255 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002256}
2257
2258void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002259 for (int s = 0; s < kNumStages; ++s) {
2260 if (fCurrDrawState.fTextures[s] == texture) {
2261 fCurrDrawState.fTextures[s] = NULL;
2262 }
2263 if (fHWDrawState.fTextures[s] == texture) {
2264 // deleting bound texture does implied bind to 0
2265 fHWDrawState.fTextures[s] = NULL;
2266 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002267 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002268}
2269
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002270bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002271 GrGLenum* internalFormat,
2272 GrGLenum* format,
2273 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002274 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002275 case kRGBA_8888_GrPixelConfig:
2276 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002277 *format = GR_GL_32BPP_COLOR_FORMAT;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002278 if (GR_GL_SUPPORT_ES) {
2279 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2280 // format for a BGRA is BGRA not RGBA (as on desktop)
2281 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2282 } else {
2283 *internalFormat = GR_GL_RGBA;
2284 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002285 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002286 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002287 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002288 *format = GR_GL_RGB;
2289 *internalFormat = GR_GL_RGB;
2290 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002291 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002292 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002293 *format = GR_GL_RGBA;
2294 *internalFormat = GR_GL_RGBA;
2295 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002296 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002297 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002298 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002299 *format = GR_GL_PALETTE8_RGBA8;
2300 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002301 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002302 } else {
2303 return false;
2304 }
2305 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002306 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002307 *format = GR_GL_ALPHA;
2308 *internalFormat = GR_GL_ALPHA;
2309 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002310 break;
2311 default:
2312 return false;
2313 }
2314 return true;
2315}
2316
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002317void GrGpuGL::setTextureUnit(int unit) {
2318 GrAssert(unit >= 0 && unit < kNumStages);
2319 if (fActiveTextureUnitIdx != unit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002320 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002321 fActiveTextureUnitIdx = unit;
2322 }
2323}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002324
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002325void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002326 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
2327 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002328 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2329 }
2330}
2331
reed@google.comac10a2d2010-12-22 21:39:39 +00002332/* On ES the internalFormat and format must match for TexImage and we use
2333 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2334 decide the internalFormat. However, on ES internalFormat for
2335 RenderBufferStorage* has to be a specific format (not a base format like
2336 GL_RGBA).
2337 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002338bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002339 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002340 case kRGBA_8888_GrPixelConfig:
2341 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002342 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002343 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002344 return true;
2345 } else {
2346 return false;
2347 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002348 case kRGB_565_GrPixelConfig:
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002349 GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it
2350 // with FBO extension desktop GL has
2351 // no such internal format
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002352 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002353 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002354 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002355 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002356 return true;
2357 default:
2358 return false;
2359 }
2360}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002361
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002362void GrGpuGL::resetDirtyFlags() {
2363 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2364}
2365
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002366void GrGpuGL::setBuffers(bool indexed,
2367 int* extraVertexOffset,
2368 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002369
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002370 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002371
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002372 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2373
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002374 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002375 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002376 case kBuffer_GeometrySrcType:
2377 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002378 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002379 break;
2380 case kArray_GeometrySrcType:
2381 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002382 this->finalizeReservedVertices();
2383 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2384 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002385 break;
2386 default:
2387 vbuf = NULL; // suppress warning
2388 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002389 }
2390
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002391 GrAssert(NULL != vbuf);
2392 GrAssert(!vbuf->isLocked());
2393 if (fHWGeometryState.fVertexBuffer != vbuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002394 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002395 fHWGeometryState.fArrayPtrsDirty = true;
2396 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002397 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002398
2399 if (indexed) {
2400 GrAssert(NULL != extraIndexOffset);
2401
2402 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002403 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002404 case kBuffer_GeometrySrcType:
2405 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002406 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002407 break;
2408 case kArray_GeometrySrcType:
2409 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002410 this->finalizeReservedIndices();
2411 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2412 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002413 break;
2414 default:
2415 ibuf = NULL; // suppress warning
2416 GrCrash("Unknown geometry src type!");
2417 }
2418
2419 GrAssert(NULL != ibuf);
2420 GrAssert(!ibuf->isLocked());
2421 if (fHWGeometryState.fIndexBuffer != ibuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002422 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002423 fHWGeometryState.fIndexBuffer = ibuf;
2424 }
2425 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002426}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002427
2428int GrGpuGL::getMaxEdges() const {
2429 // FIXME: This is a pessimistic estimate based on how many other things
2430 // want to add uniforms. This should be centralized somewhere.
2431 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2432}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002433