blob: 3ced743ecf1795d50a0f207fa6587d8fbc85a53d [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.coma16d6502011-08-02 14:07:52 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com316f99232011-01-13 21:28:12 +000018// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000019// mucking with the state of any of the stages.
bsalomon@google.com316f99232011-01-13 21:28:12 +000020static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022#define SKIP_CACHE_CHECK true
23
twiz@google.com0f31ca72011-03-18 17:38:11 +000024static const GrGLenum gXfermodeCoeff2Blend[] = {
25 GR_GL_ZERO,
26 GR_GL_ONE,
27 GR_GL_SRC_COLOR,
28 GR_GL_ONE_MINUS_SRC_COLOR,
29 GR_GL_DST_COLOR,
30 GR_GL_ONE_MINUS_DST_COLOR,
31 GR_GL_SRC_ALPHA,
32 GR_GL_ONE_MINUS_SRC_ALPHA,
33 GR_GL_DST_ALPHA,
34 GR_GL_ONE_MINUS_DST_ALPHA,
35 GR_GL_CONSTANT_COLOR,
36 GR_GL_ONE_MINUS_CONSTANT_COLOR,
37 GR_GL_CONSTANT_ALPHA,
38 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000039
40 // extended blend coeffs
41 GR_GL_SRC1_COLOR,
42 GR_GL_ONE_MINUS_SRC1_COLOR,
43 GR_GL_SRC1_ALPHA,
44 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000045};
46
bsalomon@google.com271cffc2011-05-20 14:13:56 +000047bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000048 static const bool gCoeffReferencesBlendConst[] = {
49 false,
50 false,
51 false,
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 true,
60 true,
61 true,
62 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063
64 // extended blend coeffs
65 false,
66 false,
67 false,
68 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000069 };
70 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000071 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
72
73 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
74 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
75 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
76 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
77 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
78 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
79 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
80 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
81 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
82 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
83 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
84 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
85 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
86 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
87
88 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
89 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
90 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
91 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
92
93 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
94 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000095}
96
reed@google.comac10a2d2010-12-22 21:39:39 +000097///////////////////////////////////////////////////////////////////////////////
98
bsalomon@google.comd302f142011-03-03 13:54:13 +000099void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
100 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000101 GrMatrix* matrix) {
102 GrAssert(NULL != texture);
103 GrAssert(NULL != matrix);
104 if (GR_Scalar1 != texture->contentScaleX() ||
105 GR_Scalar1 != texture->contentScaleY()) {
106 if (GrSamplerState::kRadial_SampleMode == mode) {
107 GrMatrix scale;
108 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
109 matrix->postConcat(scale);
110 } else if (GrSamplerState::kNormal_SampleMode == mode) {
111 GrMatrix scale;
112 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
113 matrix->postConcat(scale);
114 } else {
115 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
116 }
117 }
118 GrGLTexture::Orientation orientation = texture->orientation();
119 if (GrGLTexture::kBottomUp_Orientation == orientation) {
120 GrMatrix invY;
121 invY.setAll(GR_Scalar1, 0, 0,
122 0, -GR_Scalar1, GR_Scalar1,
123 0, 0, GrMatrix::I()[8]);
124 matrix->postConcat(invY);
125 } else {
126 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
127 }
128}
129
bsalomon@google.comd302f142011-03-03 13:54:13 +0000130bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000131 const GrSamplerState& sampler) {
132 GrAssert(NULL != texture);
133 if (!sampler.getMatrix().isIdentity()) {
134 return false;
135 }
136 if (GR_Scalar1 != texture->contentScaleX() ||
137 GR_Scalar1 != texture->contentScaleY()) {
138 return false;
139 }
140 GrGLTexture::Orientation orientation = texture->orientation();
141 if (GrGLTexture::kBottomUp_Orientation == orientation) {
142 return false;
143 } else {
144 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
145 }
146 return true;
147}
148
149///////////////////////////////////////////////////////////////////////////////
150
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000151static bool gPrintStartupSpew;
152
twiz@google.com59a190b2011-03-14 21:23:01 +0000153static bool fbo_test(int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000154
twiz@google.com0f31ca72011-03-18 17:38:11 +0000155 GrGLint savedFBO;
156 GrGLint savedTexUnit;
157 GR_GL_GetIntegerv(GR_GL_ACTIVE_TEXTURE, &savedTexUnit);
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000158 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &savedFBO);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000159
twiz@google.com0f31ca72011-03-18 17:38:11 +0000160 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000161
twiz@google.com0f31ca72011-03-18 17:38:11 +0000162 GrGLuint testFBO;
twiz@google.com59a190b2011-03-14 21:23:01 +0000163 GR_GL(GenFramebuffers(1, &testFBO));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000165 GrGLuint testRTTex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 GR_GL(GenTextures(1, &testRTTex));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000167 GR_GL(BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000168 // some implementations require texture to be mip-map complete before
169 // FBO with level 0 bound as color attachment will be framebuffer complete.
twiz@google.com0f31ca72011-03-18 17:38:11 +0000170 GR_GL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
171 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
172 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
173 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000174 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000175 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000176 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000177 GR_GL(DeleteFramebuffers(1, &testFBO));
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 GR_GL(DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000179
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000180 GR_GL(ActiveTexture(savedTexUnit));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000181 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, savedFBO));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000182
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000183 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000184}
185
tomhudson@google.com747bf292011-06-14 18:16:52 +0000186static bool probe_for_npot_render_target_support(bool hasNPOTTextureSupport) {
187
188 /* Experimentation has found that some GLs that support NPOT textures
189 do not support FBOs with a NPOT texture. They report "unsupported" FBO
190 status. I don't know how to explicitly query for this. Do an
191 experiment. Note they may support NPOT with a renderbuffer but not a
192 texture. Presumably, the implementation bloats the renderbuffer
193 internally to the next POT.
194 */
195 if (hasNPOTTextureSupport) {
196 return fbo_test(200, 200);
197 }
198 return false;
199}
200
201static int probe_for_min_render_target_height(bool hasNPOTRenderTargetSupport,
202 int maxRenderTargetSize) {
203 /* The iPhone 4 has a restriction that for an FBO with texture color
204 attachment with height <= 8 then the width must be <= height. Here
205 we look for such a limitation.
206 */
207 if (gPrintStartupSpew) {
208 GrPrintf("Small height FBO texture experiments\n");
209 }
210 int minRenderTargetHeight = GR_INVAL_GLINT;
211 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
212 GrGLuint w = maxRenderTargetSize;
213 GrGLuint h = i;
214 if (fbo_test(w, h)) {
215 if (gPrintStartupSpew) {
216 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
217 }
218 minRenderTargetHeight = i;
219 break;
220 } else {
221 if (gPrintStartupSpew) {
222 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
223 }
224 }
225 }
226 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
227
228 return minRenderTargetHeight;
229}
230
231static int probe_for_min_render_target_width(bool hasNPOTRenderTargetSupport,
232 int maxRenderTargetSize) {
233
234 if (gPrintStartupSpew) {
235 GrPrintf("Small width FBO texture experiments\n");
236 }
237 int minRenderTargetWidth = GR_INVAL_GLINT;
238 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
239 GrGLuint w = i;
240 GrGLuint h = maxRenderTargetSize;
241 if (fbo_test(w, h)) {
242 if (gPrintStartupSpew) {
243 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
244 }
245 minRenderTargetWidth = i;
246 break;
247 } else {
248 if (gPrintStartupSpew) {
249 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
250 }
251 }
252 }
253 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
254
255 return minRenderTargetWidth;
256}
257
258
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000259GrGpuGL::GrGpuGL()
260 : fStencilFormats(8) {
261
262 GrGLClearErr();
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000263
reed@google.comeeeb5a02010-12-23 15:12:59 +0000264 if (gPrintStartupSpew) {
265 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
266 this);
twiz@google.com59a190b2011-03-14 21:23:01 +0000267 GrPrintf("------ VENDOR %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000268 GrGLGetGLInterface()->fGetString(GR_GL_VENDOR));
twiz@google.com59a190b2011-03-14 21:23:01 +0000269 GrPrintf("------ RENDERER %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000270 GrGLGetGLInterface()->fGetString(GR_GL_RENDERER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000271 GrPrintf("------ VERSION %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000272 GrGLGetGLInterface()->fGetString(GR_GL_VERSION));
twiz@google.com59a190b2011-03-14 21:23:01 +0000273 GrPrintf("------ EXTENSIONS\n %s \n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000274 GrGLGetGLInterface()->fGetString(GR_GL_EXTENSIONS));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000275 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000277 fGLVersion = gl_version_as_float();
278 fExtensionString = (const char*) GR_GL(GetString(GR_GL_EXTENSIONS));
reed@google.comac10a2d2010-12-22 21:39:39 +0000279
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000280 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000281
twiz@google.com0f31ca72011-03-18 17:38:11 +0000282 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000283 // check FS and fixed-function texture unit limits
284 // we only use textures in the fragment stage currently.
285 // checks are > to make sure we have a spare unit.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000286 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES2) {
287 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
288 GrAssert(maxTextureUnits > kNumStages);
289 }
290 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES1) {
291 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
292 GrAssert(maxTextureUnits > kNumStages);
293 }
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000294 if (GR_GL_SUPPORT_ES2) {
295 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
296 &fMaxFragmentUniformVectors);
297 } else if (GR_GL_SUPPORT_DESKTOP) {
298 GrGLint max;
299 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
300 fMaxFragmentUniformVectors = max / 4;
301 } else {
302 fMaxFragmentUniformVectors = 16;
303 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000304
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 ////////////////////////////////////////////////////////////////////////////
306 // Check for supported features.
307
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000308 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
twiz@google.com0f31ca72011-03-18 17:38:11 +0000310 GrGLint numFormats;
311 GR_GL_GetIntegerv(GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000312 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
twiz@google.com0f31ca72011-03-18 17:38:11 +0000313 GR_GL_GetIntegerv(GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000315 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000316 f8bitPaletteSupport = true;
317 break;
318 }
319 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000320
321 if (gPrintStartupSpew) {
322 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
323 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000324
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000325 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
326 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
327 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
328 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000329
330 memset(fAASamples, 0, sizeof(fAASamples));
331 fMSFBOType = kNone_MSFBO;
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000332 if (GR_GL_SUPPORT_ES) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000333 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000334 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000335 // and fbo_blit extensions.
336 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000337 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000338 fMSFBOType = kAppleES_MSFBO;
339 }
340 } else {
341 GrAssert(GR_GL_SUPPORT_DESKTOP);
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000342 if ((fGLVersion >= 3.f) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000343 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000344 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
345 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000346 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000347 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000349 if (gPrintStartupSpew) {
350 switch (fMSFBOType) {
351 case kNone_MSFBO:
352 GrPrintf("MSAA Support: NONE\n");
353 break;
354 case kDesktopARB_MSFBO:
355 GrPrintf("MSAA Support: DESKTOP ARB.\n");
356 break;
357 case kDesktopEXT_MSFBO:
358 GrPrintf("MSAA Support: DESKTOP EXT.\n");
359 break;
360 case kAppleES_MSFBO:
361 GrPrintf("MSAA Support: APPLE ES.\n");
362 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000363 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 }
365
366 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000367 GrGLint maxSamples;
bsalomon@google.comd1e433532011-03-21 21:38:40 +0000368 GR_GL_GetIntegerv(GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000370 fAASamples[kNone_GrAALevel] = 0;
371 fAASamples[kLow_GrAALevel] = GrMax(2,
372 GrFixedFloorToInt((GR_FixedHalf) *
373 maxSamples));
374 fAASamples[kMed_GrAALevel] = GrMax(2,
375 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
376 maxSamples));
377 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000379 if (gPrintStartupSpew) {
380 GrPrintf("\tMax Samples: %d\n", maxSamples);
381 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000382 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000383 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000384
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000385 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000386 fHasStencilWrap = (fGLVersion >= 1.4f) ||
387 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000388 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000389 fHasStencilWrap = (fGLVersion >= 2.0f) || this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000390 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000391 if (gPrintStartupSpew) {
392 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
393 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000395 if (GR_GL_SUPPORT_DESKTOP) {
396 // we could also look for GL_ATI_separate_stencil extension or
397 // GL_EXT_stencil_two_side but they use different function signatures
398 // than GL2.0+ (and than each other).
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000399 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000400 // supported on GL 1.4 and higher or by extension
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000401 fStencilWrapOpsSupport = (fGLVersion >= 1.4f) ||
402 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000403 } else {
404 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
405 // an ES1 extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000406 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000407 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000408 fStencilWrapOpsSupport = (fGLVersion >= 2.f) ||
409 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000410 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000411 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000412 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
413 (fTwoSidedStencilSupport ? "YES" : "NO"),
414 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000415 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000416
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000417 if (GR_GL_SUPPORT_DESKTOP) {
418 fRGBA8Renderbuffer = true;
419 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000420 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000421 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000422 if (gPrintStartupSpew) {
423 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
424 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000425
426
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000427 if (GR_GL_SUPPORT_ES) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000428 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000429 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000430 }
431 }
432
433 if (GR_GL_SUPPORT_DESKTOP) {
434 fBufferLockSupport = true; // we require VBO support and the desktop VBO
435 // extension includes glMapBuffer.
436 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000437 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000438 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000439
reed@google.comeeeb5a02010-12-23 15:12:59 +0000440 if (gPrintStartupSpew) {
441 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
442 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000443
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000444 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000445 if (fGLVersion >= 2.f ||
446 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000447 fNPOTTextureTileSupport = true;
448 fNPOTTextureSupport = true;
449 } else {
450 fNPOTTextureTileSupport = false;
451 fNPOTTextureSupport = false;
452 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000453 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000454 if (fGLVersion >= 2.f) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000455 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000456 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000457 } else {
458 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000459 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000460 fNPOTTextureTileSupport = false;
461 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000462 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000463
bsalomon@google.com205d4602011-04-25 12:43:45 +0000464 fAALineSupport = GR_GL_SUPPORT_DESKTOP;
465
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000467 // Experiments to determine limitations that can't be queried.
468 // TODO: Make these a preprocess that generate some compile time constants.
469 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000471 int expectNPOTTargets = GrGLGetGLInterface()->fNPOTRenderTargetSupport;
472 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000473 fNPOTRenderTargetSupport =
474 probe_for_npot_render_target_support(fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000475 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000476 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
477 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000478 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000479
bsalomon@google.com0748f212011-02-01 22:56:16 +0000480 if (gPrintStartupSpew) {
481 if (fNPOTTextureSupport) {
482 GrPrintf("NPOT textures supported\n");
483 if (fNPOTTextureTileSupport) {
484 GrPrintf("NPOT texture tiling supported\n");
485 } else {
486 GrPrintf("NPOT texture tiling NOT supported\n");
487 }
488 if (fNPOTRenderTargetSupport) {
489 GrPrintf("NPOT render targets supported\n");
490 } else {
491 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000492 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000494 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000495 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 }
497
bsalomon@google.com91958362011-06-13 17:58:13 +0000498 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
499 GR_GL_GetIntegerv(GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000500 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000501 // attachment, hence this min:
502 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000503
tomhudson@google.com747bf292011-06-14 18:16:52 +0000504 fMinRenderTargetHeight = GrGLGetGLInterface()->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000505 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000506 fMinRenderTargetHeight =
507 probe_for_min_render_target_height(fNPOTRenderTargetSupport,
508 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000509 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000510
tomhudson@google.com747bf292011-06-14 18:16:52 +0000511 fMinRenderTargetWidth = GrGLGetGLInterface()->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000512 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000513 fMinRenderTargetWidth =
514 probe_for_min_render_target_width(fNPOTRenderTargetSupport,
515 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000516 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000517
bsalomon@google.comfe676522011-06-17 18:12:21 +0000518 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000519
520 fStencilClearFBO = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000521}
522
523GrGpuGL::~GrGpuGL() {
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000524 if (fStencilClearFBO) {
525 GR_GL(DeleteFramebuffers(1, &fStencilClearFBO));
526 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000527}
528
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000529void GrGpuGL::resetContext() {
530 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 fHWBlendDisabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000532 GR_GL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000533
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000534 // we don't use the zb at all
twiz@google.com0f31ca72011-03-18 17:38:11 +0000535 GR_GL(Disable(GR_GL_DEPTH_TEST));
536 GR_GL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000537
twiz@google.com0f31ca72011-03-18 17:38:11 +0000538 GR_GL(Disable(GR_GL_CULL_FACE));
539 GR_GL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000540 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000541
twiz@google.com0f31ca72011-03-18 17:38:11 +0000542 GR_GL(Disable(GR_GL_DITHER));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000543 if (GR_GL_SUPPORT_DESKTOP) {
544 GR_GL(Disable(GR_GL_LINE_SMOOTH));
545 GR_GL(Disable(GR_GL_POINT_SMOOTH));
546 GR_GL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000547 fHWAAState.fMSAAEnabled = false;
548 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000549 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000550
twiz@google.com0f31ca72011-03-18 17:38:11 +0000551 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000552 fHWDrawState.fFlagBits = 0;
553
reed@google.comac10a2d2010-12-22 21:39:39 +0000554 // we only ever use lines in hairline mode
555 GR_GL(LineWidth(1));
556
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000557 // invalid
558 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000559
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000561 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
562 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000563
564 fHWDrawState.fBlendConstant = 0x00000000;
565 GR_GL(BlendColor(0,0,0,0));
566
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000568
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000569 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000570
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000571 for (int s = 0; s < kNumStages; ++s) {
572 fHWDrawState.fTextures[s] = NULL;
573 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
574 -GR_ScalarMax,
575 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000576 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000577 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000578 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000579
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000580 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 fHWBounds.fScissorEnabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000582 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000583 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000584
bsalomon@google.comd302f142011-03-03 13:54:13 +0000585 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000586 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000587 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
589 fHWGeometryState.fIndexBuffer = NULL;
590 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000591
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000592 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000593
twiz@google.com0f31ca72011-03-18 17:38:11 +0000594 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000595 fHWDrawState.fRenderTarget = NULL;
596}
597
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000598void GrGpuGL::abandonResources() {
599 INHERITED::abandonResources();
600
601 fStencilClearFBO = 0;
602}
603
604void GrGpuGL::releaseResources() {
605 INHERITED::releaseResources();
606
607 if (fStencilClearFBO) {
608 GR_GL(DeleteFramebuffers(1, &fStencilClearFBO));
609 fStencilClearFBO = 0;
610 }
611}
612
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000613GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
614
615 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
616 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
617 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
618 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
619
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000620 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000621 GrGLStencilBuffer* sb = NULL;
622
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000623 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000624 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
625#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
626 if (desc.fSampleCnt) {
627#else
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000628 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000629#endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000630 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000631 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000632 } else {
633 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000634 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000635 }
636 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000637 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000638 }
639 // we don't know what the RB ids are without glGets and we don't care
640 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000641 rtDesc.fMSColorRenderbufferID = 0;
642#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
643 rtDesc.fSampleCnt = desc.fSampleCnt;
644#else
645 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
646 // just guess, this code path is only compiled in WK and we aren't
647 // using MSAA anyway. This will be stripped out soon when WK sets
648 // the fSampleCnt in GrPlatformSurfaceDesc.
649 rtDesc.fSampleCnt = 4;
650 } else {
651 rtDesc.fSampleCnt = 0;
652 }
653#endif
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000654 if (desc.fStencilBits) {
655 GrGLStencilBuffer::Format format;
656 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
657 format.fPacked = false;
658 format.fStencilBits = desc.fStencilBits;
659 format.fTotalBits = desc.fStencilBits;
660 sb = new GrGLStencilBuffer(this, 0, desc.fWidth,
661 desc.fHeight, format);
662 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000663 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000664 }
665
666 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000667 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000668 GrGLenum dontCare;
669 if (!canBeTexture(desc.fConfig, &dontCare,
670 &texDesc.fUploadFormat,
671 &texDesc.fUploadType)) {
672 return NULL;
673 }
674
675 GrGLTexture::TexParams params;
676
677 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
678 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
679
bsalomon@google.com90405932011-06-17 15:56:55 +0000680 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000681 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000682 texDesc.fTextureID = desc.fPlatformTexture;
683 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
684 texDesc.fOwnsID = false;
685
686 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000687 if (isRenderTarget) {
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000688 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
689 tex->asRenderTarget()->setStencilBuffer(sb);
690 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000691 } else {
692 return new GrGLTexture(this, texDesc, params);
693 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000694 } else {
695 GrGLIRect viewport;
696 viewport.fLeft = 0;
697 viewport.fBottom = 0;
698 viewport.fWidth = desc.fWidth;
699 viewport.fHeight = desc.fHeight;
700
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000701 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
702 rt->setStencilBuffer(sb);
703 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000704 }
705}
706
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000707namespace {
708
709static const GrGLenum kUnknownGLFormat = ~0;
710
711GrGLenum get_fbo_color_format() {
712 GrGLint cbType;
713 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
714 GR_GL_COLOR_ATTACHMENT0,
715 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
716 &cbType);
717 GrGLint cbID;
718 GrGLint cbFormat;
719 switch (cbType) {
720 case GR_GL_RENDERBUFFER:
721 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
722 GR_GL_COLOR_ATTACHMENT0,
723 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
724 &cbID);
725 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
726 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
727 GR_GL_RENDERBUFFER_INTERNAL_FORMAT,
728 &cbFormat);
729 return cbFormat;
730 break;
731 case GR_GL_TEXTURE:
732 // ES doesn't have glGetTexLevelParameter
733 if (GR_GL_SUPPORT_DESKTOP) {
734 GrGLint cbLevel;
735 GrGLint cbFace;
736 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
737 GR_GL_COLOR_ATTACHMENT0,
738 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
739 &cbID);
740 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
741 GR_GL_COLOR_ATTACHMENT0,
742 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
743 &cbLevel);
744 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
745 GR_GL_COLOR_ATTACHMENT0,
746 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
747 &cbFace);
748 GrGLenum bind;
749 GrGLenum target;
750 if (cbFace) {
751 bind = GR_GL_TEXTURE_CUBE_MAP;
752 target = cbFace;
753 } else {
754 bind = GR_GL_TEXTURE_2D;
755 target = GR_GL_TEXTURE_2D;
756 }
757 GR_GL(BindTexture(bind, cbID));
758 GR_GL_GetTexLevelParameteriv(target, cbLevel,
759 GR_GL_TEXTURE_INTERNAL_FORMAT, &cbFormat);
760 return cbFormat;
761 } else {
762 return kUnknownGLFormat;
763 }
764 break;
765 default:
766 // we can get here with FBO 0, not a render buffer or a texture
767 return kUnknownGLFormat;
768 }
769}
770
771GrPixelConfig internal_color_format_to_config(GrGLenum iFormat) {
772 switch (iFormat) {
773 case GR_GL_RGB565:
774 return kRGB_565_GrPixelConfig;
775 case GR_GL_RGBA4:
776 return kRGBA_4444_GrPixelConfig;
777 case GR_GL_RGBA8:
778 case GR_GL_SRGB8_ALPHA8:
779 case GR_GL_SRGB_ALPHA:
780 case GR_GL_RGBA:
781 case GR_GL_BGRA:
782 return kRGBA_8888_GrPixelConfig;
783 case GR_GL_RGB8:
784 case GR_GL_SRGB8:
785 case GR_GL_SRGB:
786 return kRGBX_8888_GrPixelConfig;
787 default:
788 // there are many GL formats we don't have enums
789 // for. We should still render to them if the client
790 // asks us.
791 return kUnknown_GrPixelConfig;
792 }
793}
794
795GrPixelConfig get_implied_color_config(bool arbFBOExtension) {
796 GrGLint rSize, bSize, gSize, aSize;
797 if (arbFBOExtension) {
798 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
799 GR_GL_COLOR_ATTACHMENT0,
800 GR_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &rSize);
801 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
802 GR_GL_COLOR_ATTACHMENT0,
803 GR_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &gSize);
804 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
805 GR_GL_COLOR_ATTACHMENT0,
806 GR_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &bSize);
807 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
808 GR_GL_COLOR_ATTACHMENT0,
809 GR_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &aSize);
810 } else {
811 GR_GL_GetIntegerv(GR_GL_RED_BITS, &rSize);
812 GR_GL_GetIntegerv(GR_GL_GREEN_BITS, &gSize);
813 GR_GL_GetIntegerv(GR_GL_BLUE_BITS, &bSize);
814 GR_GL_GetIntegerv(GR_GL_ALPHA_BITS, &aSize);
815 }
816
817 if(8 == rSize && 8 == gSize && 8 == bSize) {
818 if (0 == aSize) {
819 return kRGBX_8888_GrPixelConfig;
820 } else if (8 == aSize) {
821 return kRGBA_8888_GrPixelConfig;
822 }
823 } else if (4 == rSize && 4 == gSize && 4 == bSize && 4 == aSize) {
824 return kRGBA_4444_GrPixelConfig;
825 } else if (5 == rSize && 6 == gSize && 5 == bSize && 0 == aSize) {
826 return kRGB_565_GrPixelConfig;
827 }
828 return kUnknown_GrPixelConfig;
829}
830
831int get_fbo_stencil_bits(bool arbFBOExtension) {
832 GrGLint stencilBits;
833 if (arbFBOExtension) {
834 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
835 GR_GL_STENCIL_ATTACHMENT,
836 GR_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
837 &stencilBits);
838 } else {
839 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &stencilBits);
840 }
841 return stencilBits;
842}
843}
844
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000845GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() {
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000846
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000847 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000848
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000849 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, (GrGLint*)&rtDesc.fRTFBOID);
850 rtDesc.fTexFBOID = rtDesc.fRTFBOID;
851 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000852
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000853 bool arbFBO = (GR_GL_SUPPORT_DESKTOP && (fGLVersion > 3.0 ||
854 this->hasExtension("GL_ARB_framebuffer_object")));
855
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000856 GrGLIRect viewport;
857 viewport.setFromGLViewport();
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000858 int stencilBits = get_fbo_stencil_bits(arbFBO);
859
860 GrGLStencilBuffer* sb = NULL;
861 if (stencilBits) {
862 GrGLStencilBuffer::Format format;
863 // we could query this but we don't really need it
864 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
865 format.fPacked = false;
866 format.fStencilBits = stencilBits;
867 format.fTotalBits = stencilBits;
868 sb = new GrGLStencilBuffer(this, 0, viewport.fWidth,
869 viewport.fHeight, format);
870 }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000871
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000872 GR_GL_GetIntegerv(GR_GL_SAMPLES, &rtDesc.fSampleCnt);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000873 GrGLenum fmat = get_fbo_color_format();
874 if (kUnknownGLFormat == fmat) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000875 rtDesc.fConfig = get_implied_color_config(arbFBO);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000876 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000877 rtDesc.fConfig = internal_color_format_to_config(fmat);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000878 }
879
880 // may have to bind a texture to gets its format
881 this->setSpareTextureUnit();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000882
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000883 rtDesc.fOwnIDs = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000884
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000885 GrGLRenderTarget* target = new GrGLRenderTarget(this, rtDesc, viewport);
886 target->setStencilBuffer(sb);
887 return target;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000888}
889
bsalomon@google.com5782d712011-01-21 21:03:59 +0000890///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000891static const GrGLuint kUnknownBitCount = ~0;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000892
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000893void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000894
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000895 // Build up list of legal stencil formats (though perhaps not supported on
896 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000897
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000898 // these consts are in order of most preferred to least preferred
899 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000900 static const GrGLStencilBuffer::Format
901 // internal Format stencil bits total bits packed?
902 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
903 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
904 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
905 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
906 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
907 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000908
909 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000910 bool supportsPackedDS = fGLVersion >= 3.0f ||
911 this->hasExtension("GL_EXT_packed_depth_stencil") ||
912 this->hasExtension("GL_ARB_framebuffer_object");
913
914 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
915 // require FBO support we can expect these are legal formats and don't
916 // check. These also all support the unsized GL_STENCIL_INDEX.
917 fStencilFormats.push_back() = gS8;
918 fStencilFormats.push_back() = gS16;
919 if (supportsPackedDS) {
920 fStencilFormats.push_back() = gD24S8;
921 }
922 fStencilFormats.push_back() = gS4;
923 if (supportsPackedDS) {
924 fStencilFormats.push_back() = gDS;
925 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000926 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000927 // ES2 has STENCIL_INDEX8 without extensions.
928 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
929 // introduces tokens for S1 thu S8 but there are separate extensions
930 // that make them legal (GL_OES_stencil1, ...).
931 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
932 // ES doesn't support using the unsized formats.
933
934 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
935 fStencilFormats.push_back() = gS8;
936 }
937 //fStencilFormats.push_back() = gS16;
938 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
939 fStencilFormats.push_back() = gD24S8;
940 }
941 if (this->hasExtension("GL_OES_stencil4")) {
942 fStencilFormats.push_back() = gS4;
943 }
944 // we require some stencil format.
945 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000946 }
947}
948
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000949////////////////////////////////////////////////////////////////////////////////
950
951void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
952 GrGLenum internalFormat,
953 const void* data,
954 size_t rowBytes) {
955 // we assume the texture is bound;
956 if (!rowBytes) {
957 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
958 }
959
960 // in case we need a temporary, trimmed copy of the src pixels
961 SkAutoSMalloc<128 * 128> tempStorage;
962
963 /*
964 * check whether to allocate a temporary buffer for flipping y or
965 * because our data has extra bytes past each row. If so, we need
966 * to trim those off here, since GL ES doesn't let us specify
967 * GL_UNPACK_ROW_LENGTH.
968 */
969 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
970 if (GR_GL_SUPPORT_DESKTOP && !flipY) {
971 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
972 GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
973 rowBytes / desc.fUploadByteCount));
974 }
975 } else {
976 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
977 if (data && (trimRowBytes < rowBytes || flipY)) {
978 // copy the data into our new storage, skipping the trailing bytes
979 size_t trimSize = desc.fContentHeight * trimRowBytes;
980 const char* src = (const char*)data;
981 if (flipY) {
982 src += (desc.fContentHeight - 1) * rowBytes;
983 }
984 char* dst = (char*)tempStorage.realloc(trimSize);
985 for (int y = 0; y < desc.fContentHeight; y++) {
986 memcpy(dst, src, trimRowBytes);
987 if (flipY) {
988 src -= rowBytes;
989 } else {
990 src += rowBytes;
991 }
992 dst += trimRowBytes;
993 }
994 // now point data to our trimmed version
995 data = tempStorage.get();
996 rowBytes = trimRowBytes;
997 }
998 }
999
1000 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
1001 if (kIndex_8_GrPixelConfig == desc.fFormat &&
1002 supports8BitPalette()) {
1003 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
1004 GrAssert(desc.fContentWidth == desc.fAllocWidth);
1005 GrAssert(desc.fContentHeight == desc.fAllocHeight);
1006 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
1007 kGrColorTableSize;
1008 GR_GL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
1009 desc.fAllocWidth, desc.fAllocHeight,
1010 0, imageSize, data));
1011 GrGLRestoreResetRowLength();
1012 } else {
1013 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
1014 desc.fAllocHeight != desc.fContentHeight)) {
1015 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1016 desc.fAllocWidth, desc.fAllocHeight,
1017 0, desc.fUploadFormat, desc.fUploadType, NULL));
1018 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
1019 desc.fContentHeight, desc.fUploadFormat,
1020 desc.fUploadType, data));
1021 GrGLRestoreResetRowLength();
1022
1023 int extraW = desc.fAllocWidth - desc.fContentWidth;
1024 int extraH = desc.fAllocHeight - desc.fContentHeight;
1025 int maxTexels = extraW * extraH;
1026 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
1027 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
1028
1029 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
1030
1031 // rowBytes is actual stride between rows in data
1032 // rowDataBytes is the actual amount of non-pad data in a row
1033 // and the stride used for uploading extraH rows.
1034 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
1035 if (extraH) {
1036 uint8_t* lastRowStart = (uint8_t*) data +
1037 (desc.fContentHeight - 1) * rowBytes;
1038 uint8_t* extraRowStart = (uint8_t*)texels.get();
1039
1040 for (int i = 0; i < extraH; ++i) {
1041 memcpy(extraRowStart, lastRowStart, rowDataBytes);
1042 extraRowStart += rowDataBytes;
1043 }
1044 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, desc.fContentHeight,
1045 desc.fContentWidth, extraH,
1046 desc.fUploadFormat, desc.fUploadType,
1047 texels.get()));
1048 }
1049 if (extraW) {
1050 uint8_t* edgeTexel = (uint8_t*)data +
1051 rowDataBytes - desc.fUploadByteCount;
1052 uint8_t* extraTexel = (uint8_t*)texels.get();
1053 for (int j = 0; j < desc.fContentHeight; ++j) {
1054 for (int i = 0; i < extraW; ++i) {
1055 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
1056 extraTexel += desc.fUploadByteCount;
1057 }
1058 edgeTexel += rowBytes;
1059 }
1060 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth, 0,
1061 extraW, desc.fContentHeight,
1062 desc.fUploadFormat, desc.fUploadType,
1063 texels.get()));
1064 }
1065 if (extraW && extraH) {
1066 uint8_t* cornerTexel = (uint8_t*)data +
1067 desc.fContentHeight * rowBytes -
1068 desc.fUploadByteCount;
1069 uint8_t* extraTexel = (uint8_t*)texels.get();
1070 for (int i = 0; i < extraW*extraH; ++i) {
1071 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
1072 extraTexel += desc.fUploadByteCount;
1073 }
1074 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
1075 desc.fContentHeight, extraW, extraH,
1076 desc.fUploadFormat, desc.fUploadType,
1077 texels.get()));
1078 }
1079
1080 } else {
1081 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1082 desc.fAllocWidth, desc.fAllocHeight, 0,
1083 desc.fUploadFormat, desc.fUploadType, data));
1084 GrGLRestoreResetRowLength();
1085 }
1086 }
1087}
1088
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001089bool GrGpuGL::createRenderTargetObjects(int width, int height,
1090 GrGLuint texID,
1091 GrGLRenderTarget::Desc* desc) {
1092 desc->fMSColorRenderbufferID = 0;
1093 desc->fRTFBOID = 0;
1094 desc->fTexFBOID = 0;
1095 desc->fOwnIDs = true;
1096
1097 GrGLenum status;
1098 GrGLint err;
1099
1100 GR_GL(GenFramebuffers(1, &desc->fTexFBOID));
1101 if (!desc->fTexFBOID) {
1102 goto FAILED;
1103 }
1104
1105 GrGLenum msColorFormat;
1106
1107 // If we are using multisampling we will create two FBOs. We render
1108 // to one and then resolve to the texture bound to the other.
1109 if (desc->fSampleCnt > 1 && kNone_MSFBO != fMSFBOType) {
1110 GR_GL(GenFramebuffers(1, &desc->fRTFBOID));
1111 GR_GL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
1112 if (!desc->fRTFBOID ||
1113 !desc->fMSColorRenderbufferID ||
1114 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
1115 goto FAILED;
1116 }
1117 } else {
1118 desc->fRTFBOID = desc->fTexFBOID;
1119 }
1120
1121 if (desc->fRTFBOID != desc->fTexFBOID) {
1122 GrAssert(desc->fSampleCnt > 1);
1123 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
1124 desc->fMSColorRenderbufferID));
1125 GR_GL_NO_ERR(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1126 desc->fSampleCnt,
1127 msColorFormat,
1128 width, height));
1129 err = GrGLGetGLInterface()->fGetError();
1130 if (err != GR_GL_NO_ERROR) {
1131 goto FAILED;
1132 }
1133 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
1134 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1135 GR_GL_COLOR_ATTACHMENT0,
1136 GR_GL_RENDERBUFFER,
1137 desc->fMSColorRenderbufferID));
1138 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1139 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1140 goto FAILED;
1141 }
1142 }
1143 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
1144
1145 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1146 GR_GL_COLOR_ATTACHMENT0,
1147 GR_GL_TEXTURE_2D,
1148 texID, 0));
1149 status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1150 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1151 goto FAILED;
1152 }
1153
1154 return true;
1155
1156FAILED:
1157 if (desc->fMSColorRenderbufferID) {
1158 GR_GL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
1159 }
1160 if (desc->fRTFBOID != desc->fTexFBOID) {
1161 GR_GL(DeleteFramebuffers(1, &desc->fRTFBOID));
1162 }
1163 if (desc->fTexFBOID) {
1164 GR_GL(DeleteFramebuffers(1, &desc->fTexFBOID));
1165 }
1166 return false;
1167}
1168
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001169// good to set a break-point here to know when createTexture fails
1170static GrTexture* return_null_texture() {
1171// GrAssert(!"null texture");
1172 return NULL;
1173}
1174
1175#if GR_DEBUG
1176static size_t as_size_t(int x) {
1177 return x;
1178}
1179#endif
1180
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001181GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001182 const void* srcData,
1183 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001184
1185#if GR_COLLECT_STATS
1186 ++fStats.fTextureCreateCnt;
1187#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001188
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001189 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001190 GR_GL_NEAREST,
1191 GR_GL_CLAMP_TO_EDGE,
1192 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001193 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001194
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001195 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001196 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001197 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001198
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001199 glTexDesc.fContentWidth = desc.fWidth;
1200 glTexDesc.fContentHeight = desc.fHeight;
1201 glTexDesc.fAllocWidth = desc.fWidth;
1202 glTexDesc.fAllocHeight = desc.fHeight;
1203 glTexDesc.fFormat = desc.fFormat;
1204 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001205
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001206 glRTDesc.fMSColorRenderbufferID = 0;
1207 glRTDesc.fRTFBOID = 0;
1208 glRTDesc.fTexFBOID = 0;
1209 glRTDesc.fOwnIDs = true;
1210 glRTDesc.fConfig = glTexDesc.fFormat;
1211
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001212 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001213 if (!canBeTexture(desc.fFormat,
1214 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001215 &glTexDesc.fUploadFormat,
1216 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001217 return return_null_texture();
1218 }
1219
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001220 // We keep GrRenderTargets in GL's normal orientation so that they
1221 // can be drawn to by the outside world without the client having
1222 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001223 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001224 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001225
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001227 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001228 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 GrPrintf("AA RT requested but not supported on this platform.");
1230 }
1231
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001232 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001233
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001235 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001236 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1237 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001238 }
1239
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001240 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001241 glTexDesc.fAllocWidth);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001242 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001243 glTexDesc.fAllocHeight);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001244 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1245 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001246 return return_null_texture();
1247 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001248 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001249 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1250 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1251 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1252 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001253 return return_null_texture();
1254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001255 }
1256
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001257 GR_GL(GenTextures(1, &glTexDesc.fTextureID));
1258 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001259 return return_null_texture();
1260 }
1261
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001262 this->setSpareTextureUnit();
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001263 GR_GL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001264 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1265 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001266 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001267 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1268 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001269 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001270 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1271 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001272 DEFAULT_PARAMS.fWrapS));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001273 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1274 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001275 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001276
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001277 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001278
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001279 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001281 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001282#if GR_COLLECT_STATS
1283 ++fStats.fRenderTargetCreateCnt;
1284#endif
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001285 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1286 glTexDesc.fAllocHeight,
1287 glTexDesc.fTextureID,
1288 &glRTDesc)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001289 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 return return_null_texture();
1291 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001292 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1293 } else {
1294 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 }
1296#ifdef TRACE_TEXTURE_CREATION
1297 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1298 tex->fTextureID, width, height, tex->fUploadByteCount);
1299#endif
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001300 return tex;
1301}
1302
1303namespace {
1304void inline get_stencil_rb_sizes(GrGLuint rb, GrGLStencilBuffer::Format* format) {
1305 // we shouldn't ever know one size and not the other
1306 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1307 (kUnknownBitCount == format->fTotalBits));
1308 if (kUnknownBitCount == format->fStencilBits) {
1309 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
1310 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1311 (GrGLint*)&format->fStencilBits);
1312 if (format->fPacked) {
1313 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
1314 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1315 (GrGLint*)&format->fTotalBits);
1316 format->fTotalBits += format->fStencilBits;
1317 } else {
1318 format->fTotalBits = format->fStencilBits;
1319 }
1320 }
1321}
1322}
1323
1324bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1325 int width, int height) {
1326
1327 // All internally created RTs are also textures. We don't create
1328 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1329 GrAssert(rt->asTexture());
1330 // if this thing is bloated for NPOT reasons we'll have to bloat the SB
1331 // as well.
1332 GrGLTexture* tex = (GrGLTexture*) rt->asTexture();
1333 width = GrMax(width, tex->allocWidth());
1334 height = GrMax(height, tex->allocWidth());
1335
1336 int samples = rt->numSamples();
1337 GrGLuint sbID;
1338 GR_GL(GenRenderbuffers(1, &sbID));
1339 if (!sbID) {
1340 return false;
1341 }
1342
1343 GrGLStencilBuffer* sb = NULL;
1344
1345 int stencilFmtCnt = fStencilFormats.count();
1346 for (int i = 0; i < stencilFmtCnt; ++i) {
1347 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
1348 // we start with the last stencil format that succeeded in hopes
1349 // that we won't go through this loop more than once after the
1350 // first (painful) stencil creation.
1351 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
1352 // we do this if so that we don't call the multisample
1353 // version on a GL that doesn't have an MSAA extension.
1354 if (samples > 1) {
1355 GR_GL_NO_ERR(RenderbufferStorageMultisample(
1356 GR_GL_RENDERBUFFER,
1357 samples,
1358 fStencilFormats[sIdx].fInternalFormat,
1359 width,
1360 height));
1361 } else {
1362 GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER,
1363 fStencilFormats[sIdx].fInternalFormat,
1364 width, height));
1365 }
1366
1367 GrGLenum err = GrGLGetGLInterface()->fGetError();
1368 if (err == GR_GL_NO_ERROR) {
1369 // After sized formats we attempt an unsized format and take whatever
1370 // sizes GL gives us. In that case we query for the size.
1371 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
1372 get_stencil_rb_sizes(sbID, &format);
1373 sb = new GrGLStencilBuffer(this, sbID, width, height, format);
1374 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1375 fLastSuccessfulStencilFmtIdx = sIdx;
1376 sb->unref();
1377 // initial clear zeros the entire sb by attaching it alone
1378 // to an fbo (that we create here on demand).
1379 if (!fStencilClearFBO) {
1380 GR_GL(GenFramebuffers(1, &fStencilClearFBO));
1381 if (0 == fStencilClearFBO) {
1382 rt->setStencilBuffer(NULL);
1383 return false;
1384 }
1385 }
1386 fHWDrawState.fRenderTarget = NULL;
1387
1388 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, fStencilClearFBO));
1389 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1390 GR_GL_STENCIL_ATTACHMENT,
1391 GR_GL_RENDERBUFFER, sbID));
1392 if (fStencilFormats[sIdx].fPacked) {
1393 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1394 GR_GL_DEPTH_ATTACHMENT,
1395 GR_GL_RENDERBUFFER, sbID));
1396 } else {
1397 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1398 GR_GL_DEPTH_ATTACHMENT,
1399 GR_GL_RENDERBUFFER, 0));
1400 }
1401#if GR_DEBUG
1402 GrGLenum status =
1403 GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1404 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1405#endif
1406
1407 this->flushScissor(NULL);
1408 GR_GL(ClearStencil(0));
1409 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
1410 return true;
1411 }
1412 sb->abandon(); // otherwise we lose sbID
1413 sb->unref();
1414 }
1415 }
1416 GR_GL(DeleteRenderbuffers(1, &sbID));
1417 return NULL;
1418}
1419
1420bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1421 GrRenderTarget* rt) {
1422 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1423
1424 GrGLuint fbo = glrt->renderFBOID();
1425
1426 if (NULL == sb) {
1427 if (NULL != rt->getStencilBuffer()) {
1428 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1429 GR_GL_STENCIL_ATTACHMENT,
1430 GR_GL_RENDERBUFFER, 0));
1431 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1432 GR_GL_DEPTH_ATTACHMENT,
1433 GR_GL_RENDERBUFFER, 0));
1434#if GR_DEBUG
1435 GrGLenum status =
1436 GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1437 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1438#endif
1439 }
1440 return true;
1441 } else {
1442 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1443 GrGLuint rb = glsb->renderbufferID();
1444
reed@google.comac10a2d2010-12-22 21:39:39 +00001445 fHWDrawState.fRenderTarget = NULL;
1446
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001447 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1448 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1449 GR_GL_STENCIL_ATTACHMENT,
1450 GR_GL_RENDERBUFFER, rb));
1451 if (glsb->format().fPacked) {
1452 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1453 GR_GL_DEPTH_ATTACHMENT,
1454 GR_GL_RENDERBUFFER, rb));
1455 } else {
1456 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1457 GR_GL_DEPTH_ATTACHMENT,
1458 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001460
1461 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1462 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1463 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1464 GR_GL_STENCIL_ATTACHMENT,
1465 GR_GL_RENDERBUFFER, 0));
1466 if (glsb->format().fPacked) {
1467 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1468 GR_GL_DEPTH_ATTACHMENT,
1469 GR_GL_RENDERBUFFER, 0));
1470 }
1471 return false;
1472 } else {
1473 rt->setStencilBuffer(sb);
1474 return true;
1475 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001476 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001477}
1478
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001479////////////////////////////////////////////////////////////////////////////////
1480
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001481GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001482 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001483 GR_GL(GenBuffers(1, &id));
1484 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001485 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001486 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 GrGLClearErr();
1488 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001489 GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1490 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1491 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001492 GR_GL(DeleteBuffers(1, &id));
1493 // deleting bound buffer does implicit bind to 0
1494 fHWGeometryState.fVertexBuffer = NULL;
1495 return NULL;
1496 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001497 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001498 size, dynamic);
1499 fHWGeometryState.fVertexBuffer = vertexBuffer;
1500 return vertexBuffer;
1501 }
1502 return NULL;
1503}
1504
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001505GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001506 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001507 GR_GL(GenBuffers(1, &id));
1508 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001509 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001510 GrGLClearErr();
1511 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001512 GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1513 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1514 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 GR_GL(DeleteBuffers(1, &id));
1516 // deleting bound buffer does implicit bind to 0
1517 fHWGeometryState.fIndexBuffer = NULL;
1518 return NULL;
1519 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001520 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 size, dynamic);
1522 fHWGeometryState.fIndexBuffer = indexBuffer;
1523 return indexBuffer;
1524 }
1525 return NULL;
1526}
1527
reed@google.comac10a2d2010-12-22 21:39:39 +00001528void GrGpuGL::flushScissor(const GrIRect* rect) {
1529 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001530 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001531 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001532
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001533 GrGLIRect scissor;
1534 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001535 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001536 rect->width(), rect->height());
1537 if (scissor.contains(vp)) {
1538 rect = NULL;
1539 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001540 }
1541
1542 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001543 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001544 scissor.pushToGLScissor();
reed@google.comac10a2d2010-12-22 21:39:39 +00001545 fHWBounds.fScissorRect = scissor;
1546 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001547 if (!fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001548 GR_GL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001549 fHWBounds.fScissorEnabled = true;
1550 }
1551 } else {
1552 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001553 GR_GL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001554 fHWBounds.fScissorEnabled = false;
1555 }
1556 }
1557}
1558
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001559void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001560 if (NULL == fCurrDrawState.fRenderTarget) {
1561 return;
1562 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001563 GrIRect r;
1564 if (NULL != rect) {
1565 // flushScissor expects rect to be clipped to the target.
1566 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001567 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1568 fCurrDrawState.fRenderTarget->height());
1569 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001570 rect = &r;
1571 } else {
1572 return;
1573 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001574 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001575 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001576 this->flushScissor(rect);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001577 GR_GL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001578 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
reed@google.comac10a2d2010-12-22 21:39:39 +00001579 GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
1580 GrColorUnpackG(color)/255.f,
1581 GrColorUnpackB(color)/255.f,
1582 GrColorUnpackA(color)/255.f));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001583 GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001584}
1585
bsalomon@google.com398109c2011-04-14 18:40:27 +00001586void GrGpuGL::clearStencil(uint32_t value, uint32_t mask) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001587 if (NULL == fCurrDrawState.fRenderTarget) {
1588 return;
1589 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001590
1591 this->flushRenderTarget(&GrIRect::EmptyIRect());
1592
reed@google.comac10a2d2010-12-22 21:39:39 +00001593 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001594 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001595 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001596 }
1597 GR_GL(StencilMask(mask));
1598 GR_GL(ClearStencil(value));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001599 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001600 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001601}
1602
bsalomon@google.com398109c2011-04-14 18:40:27 +00001603void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001604 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001605
1606 // this should only be called internally when we know we have a
1607 // stencil buffer.
1608 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001609#if 0
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001610 GrGLint stencilBitCount =
1611 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001613 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001614#else
1615 // we could just clear the clip bit but when we go through
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001616 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001617 // turned into draws. Our contract on GrDrawTarget says that
1618 // changing the clip between stencil passes may or may not
1619 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001620 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001621#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001622 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001623 this->flushScissor(&rect);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001624 GR_GL(StencilMask(clipStencilMask));
1625 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001626 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001627 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001628}
1629
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001630void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001631 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001632}
1633
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001634bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1635 int left, int top, int width, int height,
1636 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001637 GrGLenum internalFormat; // we don't use this for glReadPixels
1638 GrGLenum format;
1639 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1641 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001642 }
1643 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1644 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1645 switch (tgt->getResolveType()) {
1646 case GrGLRenderTarget::kCantResolve_ResolveType:
1647 return false;
1648 case GrGLRenderTarget::kAutoResolves_ResolveType:
1649 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1650 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001651 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001652 break;
1653 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001654 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001655 // we don't track the state of the READ FBO ID.
1656 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
1657 break;
1658 default:
1659 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001660 }
1661
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001662 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001663
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001664 // the read rect is viewport-relative
1665 GrGLIRect readRect;
1666 readRect.setRelativeTo(glvp, left, top, width, height);
1667 GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001668 readRect.fWidth, readRect.fHeight,
bsalomon@google.com316f99232011-01-13 21:28:12 +00001669 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001670
1671 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1672 // API presents top-to-bottom
1673 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001674 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001675 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001676 void* tmp = rowStorage.get();
1677
1678 const int halfY = height >> 1;
1679 char* top = reinterpret_cast<char*>(buffer);
1680 char* bottom = top + (height - 1) * stride;
1681 for (int y = 0; y < halfY; y++) {
1682 memcpy(tmp, top, stride);
1683 memcpy(top, bottom, stride);
1684 memcpy(bottom, tmp, stride);
1685 top += stride;
1686 bottom -= stride;
1687 }
1688 }
1689 return true;
1690}
1691
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001692void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001693
1694 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1695
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001696 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001698 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001699 #if GR_COLLECT_STATS
1700 ++fStats.fRenderTargetChngCnt;
1701 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 #if GR_DEBUG
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001703 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1704 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001705 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001706 }
1707 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001708 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001709 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001710 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001711 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001712 vp.pushToGLViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001713 fHWBounds.fViewportRect = vp;
1714 }
1715 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001716 if (NULL == bound || !bound->isEmpty()) {
1717 rt->flagAsNeedingResolve(bound);
1718 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001719}
1720
twiz@google.com0f31ca72011-03-18 17:38:11 +00001721GrGLenum gPrimitiveType2GLMode[] = {
1722 GR_GL_TRIANGLES,
1723 GR_GL_TRIANGLE_STRIP,
1724 GR_GL_TRIANGLE_FAN,
1725 GR_GL_POINTS,
1726 GR_GL_LINES,
1727 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001728};
1729
bsalomon@google.comd302f142011-03-03 13:54:13 +00001730#define SWAP_PER_DRAW 0
1731
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001732#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001733 #if GR_MAC_BUILD
1734 #include <AGL/agl.h>
1735 #elif GR_WIN32_BUILD
1736 void SwapBuf() {
1737 DWORD procID = GetCurrentProcessId();
1738 HWND hwnd = GetTopWindow(GetDesktopWindow());
1739 while(hwnd) {
1740 DWORD wndProcID = 0;
1741 GetWindowThreadProcessId(hwnd, &wndProcID);
1742 if(wndProcID == procID) {
1743 SwapBuffers(GetDC(hwnd));
1744 }
1745 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1746 }
1747 }
1748 #endif
1749#endif
1750
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001751void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1752 uint32_t startVertex,
1753 uint32_t startIndex,
1754 uint32_t vertexCount,
1755 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1757
twiz@google.com0f31ca72011-03-18 17:38:11 +00001758 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001759
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001760 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1761 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1762
1763 // our setupGeometry better have adjusted this to zero since
1764 // DrawElements always draws from the begining of the arrays for idx 0.
1765 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001766
1767 GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
twiz@google.com0f31ca72011-03-18 17:38:11 +00001768 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001769#if SWAP_PER_DRAW
1770 glFlush();
1771 #if GR_MAC_BUILD
1772 aglSwapBuffers(aglGetCurrentContext());
1773 int set_a_break_pt_here = 9;
1774 aglSwapBuffers(aglGetCurrentContext());
1775 #elif GR_WIN32_BUILD
1776 SwapBuf();
1777 int set_a_break_pt_here = 9;
1778 SwapBuf();
1779 #endif
1780#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001781}
1782
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001783void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1784 uint32_t startVertex,
1785 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001786 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1787
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001788 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1789
1790 // our setupGeometry better have adjusted this to zero.
1791 // DrawElements doesn't take an offset so we always adjus the startVertex.
1792 GrAssert(0 == startVertex);
1793
1794 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1795 // account for startVertex in the DrawElements case. So we always
1796 // rely on setupGeometry to have accounted for startVertex.
reed@google.comac10a2d2010-12-22 21:39:39 +00001797 GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001798#if SWAP_PER_DRAW
1799 glFlush();
1800 #if GR_MAC_BUILD
1801 aglSwapBuffers(aglGetCurrentContext());
1802 int set_a_break_pt_here = 9;
1803 aglSwapBuffers(aglGetCurrentContext());
1804 #elif GR_WIN32_BUILD
1805 SwapBuf();
1806 int set_a_break_pt_here = 9;
1807 SwapBuf();
1808 #endif
1809#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001810}
1811
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001812void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001813
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001814 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001815 GrAssert(kNone_MSFBO != fMSFBOType);
1816 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001817 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001818 rt->renderFBOID()));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001819 GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001820 rt->textureFBOID()));
1821 #if GR_COLLECT_STATS
1822 ++fStats.fRenderTargetChngCnt;
1823 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001824 // make sure we go through flushRenderTarget() since we've modified
1825 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001826 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001827 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001828 const GrIRect dirtyRect = rt->getResolveRect();
1829 GrGLIRect r;
1830 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1831 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001832
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001833 if (kAppleES_MSFBO == fMSFBOType) {
1834 // Apple's extension uses the scissor as the blit bounds.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001835 GR_GL(Enable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001836 GR_GL(Scissor(r.fLeft, r.fBottom,
1837 r.fWidth, r.fHeight));
twiz@google.com59a190b2011-03-14 21:23:01 +00001838 GR_GL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001839 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001840 fHWBounds.fScissorEnabled = true;
1841 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001842 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001843 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001844 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1845 flushScissor(NULL);
1846 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001847 int right = r.fLeft + r.fWidth;
1848 int top = r.fBottom + r.fHeight;
1849 GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1850 r.fLeft, r.fBottom, right, top,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001851 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001852 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001853 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001854 }
1855}
1856
twiz@google.com0f31ca72011-03-18 17:38:11 +00001857static const GrGLenum grToGLStencilFunc[] = {
1858 GR_GL_ALWAYS, // kAlways_StencilFunc
1859 GR_GL_NEVER, // kNever_StencilFunc
1860 GR_GL_GREATER, // kGreater_StencilFunc
1861 GR_GL_GEQUAL, // kGEqual_StencilFunc
1862 GR_GL_LESS, // kLess_StencilFunc
1863 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1864 GR_GL_EQUAL, // kEqual_StencilFunc,
1865 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866};
1867GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1868GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1869GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1870GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1871GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1872GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1873GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1874GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1875GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1876
twiz@google.com0f31ca72011-03-18 17:38:11 +00001877static const GrGLenum grToGLStencilOp[] = {
1878 GR_GL_KEEP, // kKeep_StencilOp
1879 GR_GL_REPLACE, // kReplace_StencilOp
1880 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1881 GR_GL_INCR, // kIncClamp_StencilOp
1882 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1883 GR_GL_DECR, // kDecClamp_StencilOp
1884 GR_GL_ZERO, // kZero_StencilOp
1885 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001886};
1887GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1888GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1889GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1890GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1891GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1892GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1893GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1894GR_STATIC_ASSERT(6 == kZero_StencilOp);
1895GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1896
reed@google.comac10a2d2010-12-22 21:39:39 +00001897void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001898 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001899
1900 // use stencil for clipping if clipping is enabled and the clip
1901 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001902 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001903 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001904 bool stencilChange = fHWStencilClip != stencilClip ||
1905 fHWDrawState.fStencilSettings != *settings ||
1906 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1907 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001908
1909 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001910
bsalomon@google.comd302f142011-03-03 13:54:13 +00001911 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1912 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001913
bsalomon@google.comd302f142011-03-03 13:54:13 +00001914 if (settings->isDisabled()) {
1915 if (stencilClip) {
1916 settings = &gClipStencilSettings;
1917 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001918 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001919
1920 if (settings->isDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001921 GR_GL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001923 GR_GL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001924 #if GR_DEBUG
1925 if (!fStencilWrapOpsSupport) {
1926 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1927 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1928 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1929 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1930 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1931 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1932 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1933 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1934 }
1935 #endif
bsalomon@google.coma16d6502011-08-02 14:07:52 +00001936 int stencilBits = 0;
1937 GrStencilBuffer* stencilBuffer =
1938 fCurrDrawState.fRenderTarget->getStencilBuffer();
1939 if (NULL != stencilBuffer) {
1940 stencilBits = stencilBuffer->bits();
1941 }
1942 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001943 GrAssert(stencilBits ||
1944 (GrStencilSettings::gDisabled ==
1945 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001946 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1947 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001948
1949 unsigned int frontRef = settings->fFrontFuncRef;
1950 unsigned int frontMask = settings->fFrontFuncMask;
1951 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001952 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001953
1954 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1955
1956 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1957 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1958 } else {
1959 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1960
1961 ConvertStencilFuncAndMask(settings->fFrontFunc,
1962 stencilClip,
1963 clipStencilMask,
1964 userStencilMask,
1965 &frontRef,
1966 &frontMask);
1967 frontWriteMask &= userStencilMask;
1968 }
1969 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001970 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001971 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001972 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001973 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001974 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001975 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001976 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001977 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001978 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001979
1980 unsigned int backRef = settings->fBackFuncRef;
1981 unsigned int backMask = settings->fBackFuncMask;
1982 unsigned int backWriteMask = settings->fBackWriteMask;
1983
1984
1985 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1986 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1987 backFunc = grToGLStencilFunc[settings->fBackFunc];
1988 } else {
1989 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1990 ConvertStencilFuncAndMask(settings->fBackFunc,
1991 stencilClip,
1992 clipStencilMask,
1993 userStencilMask,
1994 &backRef,
1995 &backMask);
1996 backWriteMask &= userStencilMask;
1997 }
1998
twiz@google.com0f31ca72011-03-18 17:38:11 +00001999 GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask));
2000 GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
2001 GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask));
2002 GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
2003 GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00002004 grToGLStencilOp[settings->fFrontPassOp],
2005 grToGLStencilOp[settings->fFrontPassOp]));
2006
twiz@google.com0f31ca72011-03-18 17:38:11 +00002007 GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00002008 grToGLStencilOp[settings->fBackPassOp],
2009 grToGLStencilOp[settings->fBackPassOp]));
2010 } else {
2011 GR_GL(StencilFunc(frontFunc, frontRef, frontMask));
2012 GR_GL(StencilMask(frontWriteMask));
2013 GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
2014 grToGLStencilOp[settings->fFrontPassOp],
2015 grToGLStencilOp[settings->fFrontPassOp]));
2016 }
2017 }
2018 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002019 fHWStencilClip = stencilClip;
2020 }
2021}
2022
bsalomon@google.com0650e812011-04-08 18:07:53 +00002023bool GrGpuGL::useSmoothLines() {
2024 // there is a conflict between using smooth lines and our use of
2025 // premultiplied alpha. Smooth lines tweak the incoming alpha value
2026 // but not in a premul-alpha way. So we only use them when our alpha
2027 // is 0xff.
2028
2029 // TODO: write a smarter line frag shader.
2030
2031 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
2032 canDisableBlend();
2033}
2034
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002035void GrGpuGL::flushAAState(GrPrimitiveType type) {
2036 if (GR_GL_SUPPORT_DESKTOP) {
2037 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
2038 // smooth lines.
2039
2040 // we prefer smooth lines over multisampled lines
2041 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00002042 if (GrIsPrimTypeLines(type)) {
2043 bool smooth = useSmoothLines();
2044 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002045 GR_GL(Enable(GR_GL_LINE_SMOOTH));
2046 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002047 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002048 GR_GL(Disable(GR_GL_LINE_SMOOTH));
2049 fHWAAState.fSmoothLineEnabled = false;
2050 }
2051 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2052 fHWAAState.fMSAAEnabled) {
2053 GR_GL(Disable(GR_GL_MULTISAMPLE));
2054 fHWAAState.fMSAAEnabled = false;
2055 }
2056 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2057 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
2058 fHWAAState.fMSAAEnabled) {
2059 if (fHWAAState.fMSAAEnabled) {
2060 GR_GL(Disable(GR_GL_MULTISAMPLE));
2061 fHWAAState.fMSAAEnabled = false;
2062 } else {
2063 GR_GL(Enable(GR_GL_MULTISAMPLE));
2064 fHWAAState.fMSAAEnabled = true;
2065 }
2066 }
2067 }
2068}
2069
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002070void GrGpuGL::flushBlend(GrPrimitiveType type,
2071 GrBlendCoeff srcCoeff,
2072 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002073 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
2074 if (fHWBlendDisabled) {
2075 GR_GL(Enable(GR_GL_BLEND));
2076 fHWBlendDisabled = false;
2077 }
2078 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
2079 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
2080 GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2081 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
2082 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
2083 fHWDrawState.fDstBlend = kISA_BlendCoeff;
2084 }
2085 } else {
2086 bool blendOff = canDisableBlend();
2087 if (fHWBlendDisabled != blendOff) {
2088 if (blendOff) {
2089 GR_GL(Disable(GR_GL_BLEND));
2090 } else {
2091 GR_GL(Enable(GR_GL_BLEND));
2092 }
2093 fHWBlendDisabled = blendOff;
2094 }
2095 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002096 if (fHWDrawState.fSrcBlend != srcCoeff ||
2097 fHWDrawState.fDstBlend != dstCoeff) {
2098 GR_GL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2099 gXfermodeCoeff2Blend[dstCoeff]));
2100 fHWDrawState.fSrcBlend = srcCoeff;
2101 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002102 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002103 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2104 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002105 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2106
2107 float c[] = {
2108 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2109 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2110 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2111 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2112 };
2113 GR_GL(BlendColor(c[0], c[1], c[2], c[3]));
2114 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2115 }
2116 }
2117 }
2118}
2119
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002120static unsigned grToGLFilter(GrSamplerState::Filter filter) {
2121 switch (filter) {
2122 case GrSamplerState::kBilinear_Filter:
2123 case GrSamplerState::k4x4Downsample_Filter:
2124 return GR_GL_LINEAR;
2125 case GrSamplerState::kNearest_Filter:
2126 case GrSamplerState::kConvolution_Filter:
2127 return GR_GL_NEAREST;
2128 default:
2129 GrAssert(!"Unknown filter type");
2130 return GR_GL_LINEAR;
2131 }
2132}
2133
bsalomon@google.comffca4002011-02-22 20:34:01 +00002134bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002135
2136 // GrGpu::setupClipAndFlushState should have already checked this
2137 // and bailed if not true.
2138 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002139
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002140 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002141 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002142 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002143 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002144
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002145 // true for now, but maybe not with GrEffect.
2146 GrAssert(NULL != nextTexture);
2147 // if we created a rt/tex and rendered to it without using a
2148 // texture and now we're texuring from the rt it will still be
2149 // the last bound texture, but it needs resolving. So keep this
2150 // out of the "last != next" check.
2151 GrGLRenderTarget* texRT =
2152 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2153 if (NULL != texRT) {
2154 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002155 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002156
2157 if (fHWDrawState.fTextures[s] != nextTexture) {
2158 setTextureUnit(s);
2159 GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
2160 #if GR_COLLECT_STATS
2161 ++fStats.fTextureChngCnt;
2162 #endif
2163 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2164 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002165 // The texture matrix has to compensate for texture width/height
2166 // and NPOT-embedded-in-POT
2167 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002168 }
2169
2170 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2171 const GrGLTexture::TexParams& oldTexParams =
2172 nextTexture->getTexParams();
2173 GrGLTexture::TexParams newTexParams;
2174
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002175 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002176
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002177 newTexParams.fWrapS =
2178 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()];
2179 newTexParams.fWrapT =
2180 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()];
2181
2182 if (newTexParams.fFilter != oldTexParams.fFilter) {
2183 setTextureUnit(s);
2184 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2185 GR_GL_TEXTURE_MAG_FILTER,
2186 newTexParams.fFilter));
2187 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2188 GR_GL_TEXTURE_MIN_FILTER,
2189 newTexParams.fFilter));
2190 }
2191 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2192 setTextureUnit(s);
2193 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2194 GR_GL_TEXTURE_WRAP_S,
2195 newTexParams.fWrapS));
2196 }
2197 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2198 setTextureUnit(s);
2199 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2200 GR_GL_TEXTURE_WRAP_T,
2201 newTexParams.fWrapT));
2202 }
2203 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002204 }
2205 }
2206
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002207 GrIRect* rect = NULL;
2208 GrIRect clipBounds;
2209 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2210 fClip.hasConservativeBounds()) {
2211 fClip.getConservativeBounds().roundOut(&clipBounds);
2212 rect = &clipBounds;
2213 }
2214 this->flushRenderTarget(rect);
2215 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002216
reed@google.comac10a2d2010-12-22 21:39:39 +00002217 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2218 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2219 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002220 GR_GL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002221 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002222 GR_GL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002223 }
2224 }
2225
bsalomon@google.comd302f142011-03-03 13:54:13 +00002226 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2227 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002228 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002229 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002230 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002231 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002232 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002233 }
2234 GR_GL(ColorMask(mask, mask, mask, mask));
2235 }
2236
bsalomon@google.comd302f142011-03-03 13:54:13 +00002237 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2238 switch (fCurrDrawState.fDrawFace) {
2239 case kCCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002240 GR_GL(Enable(GR_GL_CULL_FACE));
2241 GR_GL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002242 break;
2243 case kCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002244 GR_GL(Enable(GR_GL_CULL_FACE));
2245 GR_GL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002246 break;
2247 case kBoth_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002248 GR_GL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002249 break;
2250 default:
2251 GrCrash("Unknown draw face.");
2252 }
2253 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2254 }
2255
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002256#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002257 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002258 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002259 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002260 NULL == fCurrDrawState.fRenderTarget ||
2261 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002262 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002263 fCurrDrawState.fRenderTarget);
2264 }
2265#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002266
reed@google.comac10a2d2010-12-22 21:39:39 +00002267 flushStencil();
2268
bsalomon@google.comd302f142011-03-03 13:54:13 +00002269 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002270 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002271 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002272}
2273
2274void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002275 if (fHWGeometryState.fVertexBuffer != buffer) {
2276 fHWGeometryState.fArrayPtrsDirty = true;
2277 fHWGeometryState.fVertexBuffer = buffer;
2278 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002279}
2280
2281void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002282 if (fHWGeometryState.fVertexBuffer == buffer) {
2283 // deleting bound buffer does implied bind to 0
2284 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002285 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002286 }
2287}
2288
2289void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002290 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002291}
2292
2293void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002294 if (fHWGeometryState.fIndexBuffer == buffer) {
2295 // deleting bound buffer does implied bind to 0
2296 fHWGeometryState.fIndexBuffer = NULL;
2297 }
2298}
2299
reed@google.comac10a2d2010-12-22 21:39:39 +00002300void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2301 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002302 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002303 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002304 }
2305 if (fHWDrawState.fRenderTarget == renderTarget) {
2306 fHWDrawState.fRenderTarget = NULL;
2307 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002308}
2309
2310void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002311 for (int s = 0; s < kNumStages; ++s) {
2312 if (fCurrDrawState.fTextures[s] == texture) {
2313 fCurrDrawState.fTextures[s] = NULL;
2314 }
2315 if (fHWDrawState.fTextures[s] == texture) {
2316 // deleting bound texture does implied bind to 0
2317 fHWDrawState.fTextures[s] = NULL;
2318 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002320}
2321
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002322bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002323 GrGLenum* internalFormat,
2324 GrGLenum* format,
2325 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002326 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002327 case kRGBA_8888_GrPixelConfig:
2328 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002329 *format = GR_GL_32BPP_COLOR_FORMAT;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002330 if (GR_GL_SUPPORT_ES) {
2331 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2332 // format for a BGRA is BGRA not RGBA (as on desktop)
2333 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2334 } else {
2335 *internalFormat = GR_GL_RGBA;
2336 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002337 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002338 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002339 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002340 *format = GR_GL_RGB;
2341 *internalFormat = GR_GL_RGB;
2342 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002343 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002344 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002345 *format = GR_GL_RGBA;
2346 *internalFormat = GR_GL_RGBA;
2347 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002348 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002349 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002350 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002351 *format = GR_GL_PALETTE8_RGBA8;
2352 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002353 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002354 } else {
2355 return false;
2356 }
2357 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002358 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002359 *format = GR_GL_ALPHA;
2360 *internalFormat = GR_GL_ALPHA;
2361 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002362 break;
2363 default:
2364 return false;
2365 }
2366 return true;
2367}
2368
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002369void GrGpuGL::setTextureUnit(int unit) {
2370 GrAssert(unit >= 0 && unit < kNumStages);
2371 if (fActiveTextureUnitIdx != unit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002372 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002373 fActiveTextureUnitIdx = unit;
2374 }
2375}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002376
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002377void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002378 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
2379 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002380 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2381 }
2382}
2383
reed@google.comac10a2d2010-12-22 21:39:39 +00002384/* On ES the internalFormat and format must match for TexImage and we use
2385 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2386 decide the internalFormat. However, on ES internalFormat for
2387 RenderBufferStorage* has to be a specific format (not a base format like
2388 GL_RGBA).
2389 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002390bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002391 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002392 case kRGBA_8888_GrPixelConfig:
2393 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002394 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002395 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002396 return true;
2397 } else {
2398 return false;
2399 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002400 case kRGB_565_GrPixelConfig:
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002401 GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it
2402 // with FBO extension desktop GL has
2403 // no such internal format
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002404 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002405 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002406 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002407 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002408 return true;
2409 default:
2410 return false;
2411 }
2412}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002413
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002414void GrGpuGL::resetDirtyFlags() {
2415 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2416}
2417
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002418void GrGpuGL::setBuffers(bool indexed,
2419 int* extraVertexOffset,
2420 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002421
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002422 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002423
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002424 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2425
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002426 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002427 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002428 case kBuffer_GeometrySrcType:
2429 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002430 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002431 break;
2432 case kArray_GeometrySrcType:
2433 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002434 this->finalizeReservedVertices();
2435 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2436 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002437 break;
2438 default:
2439 vbuf = NULL; // suppress warning
2440 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002441 }
2442
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443 GrAssert(NULL != vbuf);
2444 GrAssert(!vbuf->isLocked());
2445 if (fHWGeometryState.fVertexBuffer != vbuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002446 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002447 fHWGeometryState.fArrayPtrsDirty = true;
2448 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002449 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002450
2451 if (indexed) {
2452 GrAssert(NULL != extraIndexOffset);
2453
2454 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002455 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002456 case kBuffer_GeometrySrcType:
2457 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002458 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459 break;
2460 case kArray_GeometrySrcType:
2461 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002462 this->finalizeReservedIndices();
2463 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2464 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002465 break;
2466 default:
2467 ibuf = NULL; // suppress warning
2468 GrCrash("Unknown geometry src type!");
2469 }
2470
2471 GrAssert(NULL != ibuf);
2472 GrAssert(!ibuf->isLocked());
2473 if (fHWGeometryState.fIndexBuffer != ibuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002474 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002475 fHWGeometryState.fIndexBuffer = ibuf;
2476 }
2477 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002478}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002479
2480int GrGpuGL::getMaxEdges() const {
2481 // FIXME: This is a pessimistic estimate based on how many other things
2482 // want to add uniforms. This should be centralized somewhere.
2483 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2484}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002485