blob: 7162f35afcf8fc8a065aa9ead1167eac0c0fea34 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com316f99232011-01-13 21:28:12 +000018// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000019// mucking with the state of any of the stages.
bsalomon@google.com316f99232011-01-13 21:28:12 +000020static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022#define SKIP_CACHE_CHECK true
23
twiz@google.com0f31ca72011-03-18 17:38:11 +000024static const GrGLenum gXfermodeCoeff2Blend[] = {
25 GR_GL_ZERO,
26 GR_GL_ONE,
27 GR_GL_SRC_COLOR,
28 GR_GL_ONE_MINUS_SRC_COLOR,
29 GR_GL_DST_COLOR,
30 GR_GL_ONE_MINUS_DST_COLOR,
31 GR_GL_SRC_ALPHA,
32 GR_GL_ONE_MINUS_SRC_ALPHA,
33 GR_GL_DST_ALPHA,
34 GR_GL_ONE_MINUS_DST_ALPHA,
35 GR_GL_CONSTANT_COLOR,
36 GR_GL_ONE_MINUS_CONSTANT_COLOR,
37 GR_GL_CONSTANT_ALPHA,
38 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000039
40 // extended blend coeffs
41 GR_GL_SRC1_COLOR,
42 GR_GL_ONE_MINUS_SRC1_COLOR,
43 GR_GL_SRC1_ALPHA,
44 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000045};
46
bsalomon@google.com271cffc2011-05-20 14:13:56 +000047bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000048 static const bool gCoeffReferencesBlendConst[] = {
49 false,
50 false,
51 false,
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 true,
60 true,
61 true,
62 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063
64 // extended blend coeffs
65 false,
66 false,
67 false,
68 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000069 };
70 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000071 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
72
73 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
74 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
75 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
76 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
77 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
78 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
79 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
80 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
81 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
82 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
83 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
84 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
85 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
86 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
87
88 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
89 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
90 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
91 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
92
93 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
94 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000095}
96
reed@google.comac10a2d2010-12-22 21:39:39 +000097///////////////////////////////////////////////////////////////////////////////
98
bsalomon@google.comd302f142011-03-03 13:54:13 +000099void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
100 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000101 GrMatrix* matrix) {
102 GrAssert(NULL != texture);
103 GrAssert(NULL != matrix);
104 if (GR_Scalar1 != texture->contentScaleX() ||
105 GR_Scalar1 != texture->contentScaleY()) {
106 if (GrSamplerState::kRadial_SampleMode == mode) {
107 GrMatrix scale;
108 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
109 matrix->postConcat(scale);
110 } else if (GrSamplerState::kNormal_SampleMode == mode) {
111 GrMatrix scale;
112 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
113 matrix->postConcat(scale);
114 } else {
115 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
116 }
117 }
118 GrGLTexture::Orientation orientation = texture->orientation();
119 if (GrGLTexture::kBottomUp_Orientation == orientation) {
120 GrMatrix invY;
121 invY.setAll(GR_Scalar1, 0, 0,
122 0, -GR_Scalar1, GR_Scalar1,
123 0, 0, GrMatrix::I()[8]);
124 matrix->postConcat(invY);
125 } else {
126 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
127 }
128}
129
bsalomon@google.comd302f142011-03-03 13:54:13 +0000130bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000131 const GrSamplerState& sampler) {
132 GrAssert(NULL != texture);
133 if (!sampler.getMatrix().isIdentity()) {
134 return false;
135 }
136 if (GR_Scalar1 != texture->contentScaleX() ||
137 GR_Scalar1 != texture->contentScaleY()) {
138 return false;
139 }
140 GrGLTexture::Orientation orientation = texture->orientation();
141 if (GrGLTexture::kBottomUp_Orientation == orientation) {
142 return false;
143 } else {
144 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
145 }
146 return true;
147}
148
149///////////////////////////////////////////////////////////////////////////////
150
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000151static bool gPrintStartupSpew;
152
twiz@google.com59a190b2011-03-14 21:23:01 +0000153static bool fbo_test(int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000154
twiz@google.com0f31ca72011-03-18 17:38:11 +0000155 GrGLint savedFBO;
156 GrGLint savedTexUnit;
157 GR_GL_GetIntegerv(GR_GL_ACTIVE_TEXTURE, &savedTexUnit);
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000158 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &savedFBO);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000159
twiz@google.com0f31ca72011-03-18 17:38:11 +0000160 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000161
twiz@google.com0f31ca72011-03-18 17:38:11 +0000162 GrGLuint testFBO;
twiz@google.com59a190b2011-03-14 21:23:01 +0000163 GR_GL(GenFramebuffers(1, &testFBO));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000165 GrGLuint testRTTex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 GR_GL(GenTextures(1, &testRTTex));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000167 GR_GL(BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000168 // some implementations require texture to be mip-map complete before
169 // FBO with level 0 bound as color attachment will be framebuffer complete.
twiz@google.com0f31ca72011-03-18 17:38:11 +0000170 GR_GL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
171 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
172 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
173 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000174 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000175 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000176 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000177 GR_GL(DeleteFramebuffers(1, &testFBO));
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 GR_GL(DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000179
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000180 GR_GL(ActiveTexture(savedTexUnit));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000181 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, savedFBO));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000182
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000183 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000184}
185
tomhudson@google.com747bf292011-06-14 18:16:52 +0000186static bool probe_for_npot_render_target_support(bool hasNPOTTextureSupport) {
187
188 /* Experimentation has found that some GLs that support NPOT textures
189 do not support FBOs with a NPOT texture. They report "unsupported" FBO
190 status. I don't know how to explicitly query for this. Do an
191 experiment. Note they may support NPOT with a renderbuffer but not a
192 texture. Presumably, the implementation bloats the renderbuffer
193 internally to the next POT.
194 */
195 if (hasNPOTTextureSupport) {
196 return fbo_test(200, 200);
197 }
198 return false;
199}
200
201static int probe_for_min_render_target_height(bool hasNPOTRenderTargetSupport,
202 int maxRenderTargetSize) {
203 /* The iPhone 4 has a restriction that for an FBO with texture color
204 attachment with height <= 8 then the width must be <= height. Here
205 we look for such a limitation.
206 */
207 if (gPrintStartupSpew) {
208 GrPrintf("Small height FBO texture experiments\n");
209 }
210 int minRenderTargetHeight = GR_INVAL_GLINT;
211 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
212 GrGLuint w = maxRenderTargetSize;
213 GrGLuint h = i;
214 if (fbo_test(w, h)) {
215 if (gPrintStartupSpew) {
216 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
217 }
218 minRenderTargetHeight = i;
219 break;
220 } else {
221 if (gPrintStartupSpew) {
222 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
223 }
224 }
225 }
226 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
227
228 return minRenderTargetHeight;
229}
230
231static int probe_for_min_render_target_width(bool hasNPOTRenderTargetSupport,
232 int maxRenderTargetSize) {
233
234 if (gPrintStartupSpew) {
235 GrPrintf("Small width FBO texture experiments\n");
236 }
237 int minRenderTargetWidth = GR_INVAL_GLINT;
238 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
239 GrGLuint w = i;
240 GrGLuint h = maxRenderTargetSize;
241 if (fbo_test(w, h)) {
242 if (gPrintStartupSpew) {
243 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
244 }
245 minRenderTargetWidth = i;
246 break;
247 } else {
248 if (gPrintStartupSpew) {
249 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
250 }
251 }
252 }
253 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
254
255 return minRenderTargetWidth;
256}
257
258
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000259GrGpuGL::GrGpuGL()
260 : fStencilFormats(8) {
261
262 GrGLClearErr();
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000263
reed@google.comeeeb5a02010-12-23 15:12:59 +0000264 if (gPrintStartupSpew) {
265 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
266 this);
twiz@google.com59a190b2011-03-14 21:23:01 +0000267 GrPrintf("------ VENDOR %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000268 GrGLGetGLInterface()->fGetString(GR_GL_VENDOR));
twiz@google.com59a190b2011-03-14 21:23:01 +0000269 GrPrintf("------ RENDERER %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000270 GrGLGetGLInterface()->fGetString(GR_GL_RENDERER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000271 GrPrintf("------ VERSION %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000272 GrGLGetGLInterface()->fGetString(GR_GL_VERSION));
twiz@google.com59a190b2011-03-14 21:23:01 +0000273 GrPrintf("------ EXTENSIONS\n %s \n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000274 GrGLGetGLInterface()->fGetString(GR_GL_EXTENSIONS));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000275 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000277 fGLVersion = gl_version_as_float();
278 fExtensionString = (const char*) GR_GL(GetString(GR_GL_EXTENSIONS));
reed@google.comac10a2d2010-12-22 21:39:39 +0000279
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000280 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000281
twiz@google.com0f31ca72011-03-18 17:38:11 +0000282 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000283 // check FS and fixed-function texture unit limits
284 // we only use textures in the fragment stage currently.
285 // checks are > to make sure we have a spare unit.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000286 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES2) {
287 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
288 GrAssert(maxTextureUnits > kNumStages);
289 }
290 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES1) {
291 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
292 GrAssert(maxTextureUnits > kNumStages);
293 }
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000294 if (GR_GL_SUPPORT_ES2) {
295 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
296 &fMaxFragmentUniformVectors);
297 } else if (GR_GL_SUPPORT_DESKTOP) {
298 GrGLint max;
299 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
300 fMaxFragmentUniformVectors = max / 4;
301 } else {
302 fMaxFragmentUniformVectors = 16;
303 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000304
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 ////////////////////////////////////////////////////////////////////////////
306 // Check for supported features.
307
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000308 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
twiz@google.com0f31ca72011-03-18 17:38:11 +0000310 GrGLint numFormats;
311 GR_GL_GetIntegerv(GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000312 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
twiz@google.com0f31ca72011-03-18 17:38:11 +0000313 GR_GL_GetIntegerv(GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000315 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000316 f8bitPaletteSupport = true;
317 break;
318 }
319 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000320
321 if (gPrintStartupSpew) {
322 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
323 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000324
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000325 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
326 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
327 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
328 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000329
330 memset(fAASamples, 0, sizeof(fAASamples));
331 fMSFBOType = kNone_MSFBO;
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000332 if (GR_GL_SUPPORT_ES) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000333 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000334 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000335 // and fbo_blit extensions.
336 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000337 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000338 fMSFBOType = kAppleES_MSFBO;
339 }
340 } else {
341 GrAssert(GR_GL_SUPPORT_DESKTOP);
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000342 if ((fGLVersion >= 3.f) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000343 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000344 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
345 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000346 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000347 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000349 if (gPrintStartupSpew) {
350 switch (fMSFBOType) {
351 case kNone_MSFBO:
352 GrPrintf("MSAA Support: NONE\n");
353 break;
354 case kDesktopARB_MSFBO:
355 GrPrintf("MSAA Support: DESKTOP ARB.\n");
356 break;
357 case kDesktopEXT_MSFBO:
358 GrPrintf("MSAA Support: DESKTOP EXT.\n");
359 break;
360 case kAppleES_MSFBO:
361 GrPrintf("MSAA Support: APPLE ES.\n");
362 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000363 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 }
365
366 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000367 GrGLint maxSamples;
bsalomon@google.comd1e433532011-03-21 21:38:40 +0000368 GR_GL_GetIntegerv(GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000370 fAASamples[kNone_GrAALevel] = 0;
371 fAASamples[kLow_GrAALevel] = GrMax(2,
372 GrFixedFloorToInt((GR_FixedHalf) *
373 maxSamples));
374 fAASamples[kMed_GrAALevel] = GrMax(2,
375 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
376 maxSamples));
377 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000379 if (gPrintStartupSpew) {
380 GrPrintf("\tMax Samples: %d\n", maxSamples);
381 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000382 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000383 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000384
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000385 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000386 fHasStencilWrap = (fGLVersion >= 1.4f) ||
387 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000388 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000389 fHasStencilWrap = (fGLVersion >= 2.0f) || this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000390 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000391 if (gPrintStartupSpew) {
392 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
393 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000395 if (GR_GL_SUPPORT_DESKTOP) {
396 // we could also look for GL_ATI_separate_stencil extension or
397 // GL_EXT_stencil_two_side but they use different function signatures
398 // than GL2.0+ (and than each other).
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000399 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000400 // supported on GL 1.4 and higher or by extension
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000401 fStencilWrapOpsSupport = (fGLVersion >= 1.4f) ||
402 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000403 } else {
404 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
405 // an ES1 extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000406 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000407 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000408 fStencilWrapOpsSupport = (fGLVersion >= 2.f) ||
409 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000410 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000411 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000412 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
413 (fTwoSidedStencilSupport ? "YES" : "NO"),
414 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000415 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000416
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000417 if (GR_GL_SUPPORT_DESKTOP) {
418 fRGBA8Renderbuffer = true;
419 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000420 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000421 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000422 if (gPrintStartupSpew) {
423 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
424 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000425
426
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000427 if (GR_GL_SUPPORT_ES) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000428 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000429 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000430 }
431 }
432
433 if (GR_GL_SUPPORT_DESKTOP) {
434 fBufferLockSupport = true; // we require VBO support and the desktop VBO
435 // extension includes glMapBuffer.
436 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000437 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000438 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000439
reed@google.comeeeb5a02010-12-23 15:12:59 +0000440 if (gPrintStartupSpew) {
441 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
442 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000443
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000444 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000445 if (fGLVersion >= 2.f ||
446 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000447 fNPOTTextureTileSupport = true;
448 fNPOTTextureSupport = true;
449 } else {
450 fNPOTTextureTileSupport = false;
451 fNPOTTextureSupport = false;
452 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000453 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000454 if (fGLVersion >= 2.f) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000455 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000456 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000457 } else {
458 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000459 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000460 fNPOTTextureTileSupport = false;
461 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000462 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000463
bsalomon@google.com205d4602011-04-25 12:43:45 +0000464 fAALineSupport = GR_GL_SUPPORT_DESKTOP;
465
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000467 // Experiments to determine limitations that can't be queried.
468 // TODO: Make these a preprocess that generate some compile time constants.
469 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000471 int expectNPOTTargets = GrGLGetGLInterface()->fNPOTRenderTargetSupport;
472 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000473 fNPOTRenderTargetSupport =
474 probe_for_npot_render_target_support(fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000475 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000476 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
477 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000478 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000479
bsalomon@google.com0748f212011-02-01 22:56:16 +0000480 if (gPrintStartupSpew) {
481 if (fNPOTTextureSupport) {
482 GrPrintf("NPOT textures supported\n");
483 if (fNPOTTextureTileSupport) {
484 GrPrintf("NPOT texture tiling supported\n");
485 } else {
486 GrPrintf("NPOT texture tiling NOT supported\n");
487 }
488 if (fNPOTRenderTargetSupport) {
489 GrPrintf("NPOT render targets supported\n");
490 } else {
491 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000492 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000494 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000495 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 }
497
bsalomon@google.com91958362011-06-13 17:58:13 +0000498 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
499 GR_GL_GetIntegerv(GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000500 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000501 // attachment, hence this min:
502 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000503
tomhudson@google.com747bf292011-06-14 18:16:52 +0000504 fMinRenderTargetHeight = GrGLGetGLInterface()->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000505 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000506 fMinRenderTargetHeight =
507 probe_for_min_render_target_height(fNPOTRenderTargetSupport,
508 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000509 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000510
tomhudson@google.com747bf292011-06-14 18:16:52 +0000511 fMinRenderTargetWidth = GrGLGetGLInterface()->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000512 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000513 fMinRenderTargetWidth =
514 probe_for_min_render_target_width(fNPOTRenderTargetSupport,
515 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000516 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000517
bsalomon@google.comfe676522011-06-17 18:12:21 +0000518 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000519}
520
521GrGpuGL::~GrGpuGL() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000522}
523
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000524void GrGpuGL::resetContext() {
525 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000526 fHWBlendDisabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000527 GR_GL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000528
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000529 // we don't use the zb at all
twiz@google.com0f31ca72011-03-18 17:38:11 +0000530 GR_GL(Disable(GR_GL_DEPTH_TEST));
531 GR_GL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000532
twiz@google.com0f31ca72011-03-18 17:38:11 +0000533 GR_GL(Disable(GR_GL_CULL_FACE));
534 GR_GL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000535 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000536
twiz@google.com0f31ca72011-03-18 17:38:11 +0000537 GR_GL(Disable(GR_GL_DITHER));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000538 if (GR_GL_SUPPORT_DESKTOP) {
539 GR_GL(Disable(GR_GL_LINE_SMOOTH));
540 GR_GL(Disable(GR_GL_POINT_SMOOTH));
541 GR_GL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000542 fHWAAState.fMSAAEnabled = false;
543 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000544 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000545
twiz@google.com0f31ca72011-03-18 17:38:11 +0000546 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000547 fHWDrawState.fFlagBits = 0;
548
reed@google.comac10a2d2010-12-22 21:39:39 +0000549 // we only ever use lines in hairline mode
550 GR_GL(LineWidth(1));
551
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000552 // invalid
553 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000556 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
557 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000558
559 fHWDrawState.fBlendConstant = 0x00000000;
560 GR_GL(BlendColor(0,0,0,0));
561
reed@google.comac10a2d2010-12-22 21:39:39 +0000562 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000563
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000564 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000565
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000566 for (int s = 0; s < kNumStages; ++s) {
567 fHWDrawState.fTextures[s] = NULL;
568 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
569 -GR_ScalarMax,
570 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000571 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000572 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000573 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000574
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000575 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000576 fHWBounds.fScissorEnabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000577 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000578 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000579
bsalomon@google.comd302f142011-03-03 13:54:13 +0000580 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000582 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000583
584 fHWGeometryState.fIndexBuffer = NULL;
585 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000586
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000587 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
twiz@google.com0f31ca72011-03-18 17:38:11 +0000589 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 fHWDrawState.fRenderTarget = NULL;
591}
592
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000593GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
594
595 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
596 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
597 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
598 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
599
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000600 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000601 GrGLStencilBuffer* sb = NULL;
602
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000603 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000604 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
605#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
606 if (desc.fSampleCnt) {
607#else
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000608 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000609#endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000610 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000611 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000612 } else {
613 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000614 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000615 }
616 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000617 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000618 }
619 // we don't know what the RB ids are without glGets and we don't care
620 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000621 rtDesc.fMSColorRenderbufferID = 0;
622#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
623 rtDesc.fSampleCnt = desc.fSampleCnt;
624#else
625 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
626 // just guess, this code path is only compiled in WK and we aren't
627 // using MSAA anyway. This will be stripped out soon when WK sets
628 // the fSampleCnt in GrPlatformSurfaceDesc.
629 rtDesc.fSampleCnt = 4;
630 } else {
631 rtDesc.fSampleCnt = 0;
632 }
633#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000634 if (desc.fStencilBits) {
635 GrGLStencilBuffer::Format format;
636 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
637 format.fPacked = false;
638 format.fStencilBits = desc.fStencilBits;
639 format.fTotalBits = desc.fStencilBits;
640 sb = new GrGLStencilBuffer(this, 0, desc.fWidth,
641 desc.fHeight, format);
642 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000643 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000644 }
645
646 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000647 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000648 GrGLenum dontCare;
649 if (!canBeTexture(desc.fConfig, &dontCare,
650 &texDesc.fUploadFormat,
651 &texDesc.fUploadType)) {
652 return NULL;
653 }
654
655 GrGLTexture::TexParams params;
656
657 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
658 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
659
bsalomon@google.com90405932011-06-17 15:56:55 +0000660 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000661 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000662 texDesc.fTextureID = desc.fPlatformTexture;
663 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
664 texDesc.fOwnsID = false;
665
666 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000667 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000668 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
669 tex->asRenderTarget()->setStencilBuffer(sb);
670 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000671 } else {
672 return new GrGLTexture(this, texDesc, params);
673 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000674 } else {
675 GrGLIRect viewport;
676 viewport.fLeft = 0;
677 viewport.fBottom = 0;
678 viewport.fWidth = desc.fWidth;
679 viewport.fHeight = desc.fHeight;
680
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000681 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
682 rt->setStencilBuffer(sb);
683 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000684 }
685}
686
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000687namespace {
688
689static const GrGLenum kUnknownGLFormat = ~0;
690
691GrGLenum get_fbo_color_format() {
692 GrGLint cbType;
693 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
694 GR_GL_COLOR_ATTACHMENT0,
695 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
696 &cbType);
697 GrGLint cbID;
698 GrGLint cbFormat;
699 switch (cbType) {
700 case GR_GL_RENDERBUFFER:
701 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
702 GR_GL_COLOR_ATTACHMENT0,
703 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
704 &cbID);
705 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
706 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
707 GR_GL_RENDERBUFFER_INTERNAL_FORMAT,
708 &cbFormat);
709 return cbFormat;
710 break;
711 case GR_GL_TEXTURE:
712 // ES doesn't have glGetTexLevelParameter
713 if (GR_GL_SUPPORT_DESKTOP) {
714 GrGLint cbLevel;
715 GrGLint cbFace;
716 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
717 GR_GL_COLOR_ATTACHMENT0,
718 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
719 &cbID);
720 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
721 GR_GL_COLOR_ATTACHMENT0,
722 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
723 &cbLevel);
724 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
725 GR_GL_COLOR_ATTACHMENT0,
726 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
727 &cbFace);
728 GrGLenum bind;
729 GrGLenum target;
730 if (cbFace) {
731 bind = GR_GL_TEXTURE_CUBE_MAP;
732 target = cbFace;
733 } else {
734 bind = GR_GL_TEXTURE_2D;
735 target = GR_GL_TEXTURE_2D;
736 }
737 GR_GL(BindTexture(bind, cbID));
738 GR_GL_GetTexLevelParameteriv(target, cbLevel,
739 GR_GL_TEXTURE_INTERNAL_FORMAT, &cbFormat);
740 return cbFormat;
741 } else {
742 return kUnknownGLFormat;
743 }
744 break;
745 default:
746 // we can get here with FBO 0, not a render buffer or a texture
747 return kUnknownGLFormat;
748 }
749}
750
751GrPixelConfig internal_color_format_to_config(GrGLenum iFormat) {
752 switch (iFormat) {
753 case GR_GL_RGB565:
754 return kRGB_565_GrPixelConfig;
755 case GR_GL_RGBA4:
756 return kRGBA_4444_GrPixelConfig;
757 case GR_GL_RGBA8:
758 case GR_GL_SRGB8_ALPHA8:
759 case GR_GL_SRGB_ALPHA:
760 case GR_GL_RGBA:
761 case GR_GL_BGRA:
762 return kRGBA_8888_GrPixelConfig;
763 case GR_GL_RGB8:
764 case GR_GL_SRGB8:
765 case GR_GL_SRGB:
766 return kRGBX_8888_GrPixelConfig;
767 default:
768 // there are many GL formats we don't have enums
769 // for. We should still render to them if the client
770 // asks us.
771 return kUnknown_GrPixelConfig;
772 }
773}
774
775GrPixelConfig get_implied_color_config(bool arbFBOExtension) {
776 GrGLint rSize, bSize, gSize, aSize;
777 if (arbFBOExtension) {
778 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
779 GR_GL_COLOR_ATTACHMENT0,
780 GR_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &rSize);
781 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
782 GR_GL_COLOR_ATTACHMENT0,
783 GR_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &gSize);
784 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
785 GR_GL_COLOR_ATTACHMENT0,
786 GR_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &bSize);
787 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
788 GR_GL_COLOR_ATTACHMENT0,
789 GR_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &aSize);
790 } else {
791 GR_GL_GetIntegerv(GR_GL_RED_BITS, &rSize);
792 GR_GL_GetIntegerv(GR_GL_GREEN_BITS, &gSize);
793 GR_GL_GetIntegerv(GR_GL_BLUE_BITS, &bSize);
794 GR_GL_GetIntegerv(GR_GL_ALPHA_BITS, &aSize);
795 }
796
797 if(8 == rSize && 8 == gSize && 8 == bSize) {
798 if (0 == aSize) {
799 return kRGBX_8888_GrPixelConfig;
800 } else if (8 == aSize) {
801 return kRGBA_8888_GrPixelConfig;
802 }
803 } else if (4 == rSize && 4 == gSize && 4 == bSize && 4 == aSize) {
804 return kRGBA_4444_GrPixelConfig;
805 } else if (5 == rSize && 6 == gSize && 5 == bSize && 0 == aSize) {
806 return kRGB_565_GrPixelConfig;
807 }
808 return kUnknown_GrPixelConfig;
809}
810
811int get_fbo_stencil_bits(bool arbFBOExtension) {
812 GrGLint stencilBits;
813 if (arbFBOExtension) {
814 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
815 GR_GL_STENCIL_ATTACHMENT,
816 GR_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
817 &stencilBits);
818 } else {
819 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &stencilBits);
820 }
821 return stencilBits;
822}
823}
824
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000825GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() {
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000826
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000827 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000828
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000829 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, (GrGLint*)&rtDesc.fRTFBOID);
830 rtDesc.fTexFBOID = rtDesc.fRTFBOID;
831 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000832
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000833 bool arbFBO = (GR_GL_SUPPORT_DESKTOP && (fGLVersion > 3.0 ||
834 this->hasExtension("GL_ARB_framebuffer_object")));
835
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000836 GrGLIRect viewport;
837 viewport.setFromGLViewport();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000838 int stencilBits = get_fbo_stencil_bits(arbFBO);
839
840 GrGLStencilBuffer* sb = NULL;
841 if (stencilBits) {
842 GrGLStencilBuffer::Format format;
843 // we could query this but we don't really need it
844 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
845 format.fPacked = false;
846 format.fStencilBits = stencilBits;
847 format.fTotalBits = stencilBits;
848 sb = new GrGLStencilBuffer(this, 0, viewport.fWidth,
849 viewport.fHeight, format);
850 }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000851
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000852 GR_GL_GetIntegerv(GR_GL_SAMPLES, &rtDesc.fSampleCnt);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000853 GrGLenum fmat = get_fbo_color_format();
854 if (kUnknownGLFormat == fmat) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000855 rtDesc.fConfig = get_implied_color_config(arbFBO);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000856 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000857 rtDesc.fConfig = internal_color_format_to_config(fmat);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000858 }
859
860 // may have to bind a texture to gets its format
861 this->setSpareTextureUnit();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000862
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000863 rtDesc.fOwnIDs = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000864
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000865 GrGLRenderTarget* target = new GrGLRenderTarget(this, rtDesc, viewport);
866 target->setStencilBuffer(sb);
867 return target;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000868}
869
bsalomon@google.com5782d712011-01-21 21:03:59 +0000870///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000871static const GrGLuint kUnknownBitCount = ~0;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000872
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000873void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000874
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000875 // Build up list of legal stencil formats (though perhaps not supported on
876 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000877
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000878 // these consts are in order of most preferred to least preferred
879 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880 static const GrGLStencilBuffer::Format
881 // internal Format stencil bits total bits packed?
882 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
883 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
884 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
885 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
886 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
887 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000888
889 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000890 bool supportsPackedDS = fGLVersion >= 3.0f ||
891 this->hasExtension("GL_EXT_packed_depth_stencil") ||
892 this->hasExtension("GL_ARB_framebuffer_object");
893
894 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
895 // require FBO support we can expect these are legal formats and don't
896 // check. These also all support the unsized GL_STENCIL_INDEX.
897 fStencilFormats.push_back() = gS8;
898 fStencilFormats.push_back() = gS16;
899 if (supportsPackedDS) {
900 fStencilFormats.push_back() = gD24S8;
901 }
902 fStencilFormats.push_back() = gS4;
903 if (supportsPackedDS) {
904 fStencilFormats.push_back() = gDS;
905 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000906 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000907 // ES2 has STENCIL_INDEX8 without extensions.
908 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
909 // introduces tokens for S1 thu S8 but there are separate extensions
910 // that make them legal (GL_OES_stencil1, ...).
911 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
912 // ES doesn't support using the unsized formats.
913
914 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
915 fStencilFormats.push_back() = gS8;
916 }
917 //fStencilFormats.push_back() = gS16;
918 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
919 fStencilFormats.push_back() = gD24S8;
920 }
921 if (this->hasExtension("GL_OES_stencil4")) {
922 fStencilFormats.push_back() = gS4;
923 }
924 // we require some stencil format.
925 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000926 }
927}
928
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000929////////////////////////////////////////////////////////////////////////////////
930
931void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
932 GrGLenum internalFormat,
933 const void* data,
934 size_t rowBytes) {
935 // we assume the texture is bound;
936 if (!rowBytes) {
937 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
938 }
939
940 // in case we need a temporary, trimmed copy of the src pixels
941 SkAutoSMalloc<128 * 128> tempStorage;
942
943 /*
944 * check whether to allocate a temporary buffer for flipping y or
945 * because our data has extra bytes past each row. If so, we need
946 * to trim those off here, since GL ES doesn't let us specify
947 * GL_UNPACK_ROW_LENGTH.
948 */
949 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
950 if (GR_GL_SUPPORT_DESKTOP && !flipY) {
951 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
952 GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
953 rowBytes / desc.fUploadByteCount));
954 }
955 } else {
956 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
957 if (data && (trimRowBytes < rowBytes || flipY)) {
958 // copy the data into our new storage, skipping the trailing bytes
959 size_t trimSize = desc.fContentHeight * trimRowBytes;
960 const char* src = (const char*)data;
961 if (flipY) {
962 src += (desc.fContentHeight - 1) * rowBytes;
963 }
964 char* dst = (char*)tempStorage.realloc(trimSize);
965 for (int y = 0; y < desc.fContentHeight; y++) {
966 memcpy(dst, src, trimRowBytes);
967 if (flipY) {
968 src -= rowBytes;
969 } else {
970 src += rowBytes;
971 }
972 dst += trimRowBytes;
973 }
974 // now point data to our trimmed version
975 data = tempStorage.get();
976 rowBytes = trimRowBytes;
977 }
978 }
979
980 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
981 if (kIndex_8_GrPixelConfig == desc.fFormat &&
982 supports8BitPalette()) {
983 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
984 GrAssert(desc.fContentWidth == desc.fAllocWidth);
985 GrAssert(desc.fContentHeight == desc.fAllocHeight);
986 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
987 kGrColorTableSize;
988 GR_GL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
989 desc.fAllocWidth, desc.fAllocHeight,
990 0, imageSize, data));
991 GrGLRestoreResetRowLength();
992 } else {
993 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
994 desc.fAllocHeight != desc.fContentHeight)) {
995 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
996 desc.fAllocWidth, desc.fAllocHeight,
997 0, desc.fUploadFormat, desc.fUploadType, NULL));
998 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
999 desc.fContentHeight, desc.fUploadFormat,
1000 desc.fUploadType, data));
1001 GrGLRestoreResetRowLength();
1002
1003 int extraW = desc.fAllocWidth - desc.fContentWidth;
1004 int extraH = desc.fAllocHeight - desc.fContentHeight;
1005 int maxTexels = extraW * extraH;
1006 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
1007 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
1008
1009 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
1010
1011 // rowBytes is actual stride between rows in data
1012 // rowDataBytes is the actual amount of non-pad data in a row
1013 // and the stride used for uploading extraH rows.
1014 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
1015 if (extraH) {
1016 uint8_t* lastRowStart = (uint8_t*) data +
1017 (desc.fContentHeight - 1) * rowBytes;
1018 uint8_t* extraRowStart = (uint8_t*)texels.get();
1019
1020 for (int i = 0; i < extraH; ++i) {
1021 memcpy(extraRowStart, lastRowStart, rowDataBytes);
1022 extraRowStart += rowDataBytes;
1023 }
1024 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, desc.fContentHeight,
1025 desc.fContentWidth, extraH,
1026 desc.fUploadFormat, desc.fUploadType,
1027 texels.get()));
1028 }
1029 if (extraW) {
1030 uint8_t* edgeTexel = (uint8_t*)data +
1031 rowDataBytes - desc.fUploadByteCount;
1032 uint8_t* extraTexel = (uint8_t*)texels.get();
1033 for (int j = 0; j < desc.fContentHeight; ++j) {
1034 for (int i = 0; i < extraW; ++i) {
1035 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
1036 extraTexel += desc.fUploadByteCount;
1037 }
1038 edgeTexel += rowBytes;
1039 }
1040 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth, 0,
1041 extraW, desc.fContentHeight,
1042 desc.fUploadFormat, desc.fUploadType,
1043 texels.get()));
1044 }
1045 if (extraW && extraH) {
1046 uint8_t* cornerTexel = (uint8_t*)data +
1047 desc.fContentHeight * rowBytes -
1048 desc.fUploadByteCount;
1049 uint8_t* extraTexel = (uint8_t*)texels.get();
1050 for (int i = 0; i < extraW*extraH; ++i) {
1051 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
1052 extraTexel += desc.fUploadByteCount;
1053 }
1054 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
1055 desc.fContentHeight, extraW, extraH,
1056 desc.fUploadFormat, desc.fUploadType,
1057 texels.get()));
1058 }
1059
1060 } else {
1061 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
1062 desc.fAllocWidth, desc.fAllocHeight, 0,
1063 desc.fUploadFormat, desc.fUploadType, data));
1064 GrGLRestoreResetRowLength();
1065 }
1066 }
1067}
1068
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001069bool GrGpuGL::createRenderTargetObjects(int width, int height,
1070 GrGLuint texID,
1071 GrGLRenderTarget::Desc* desc) {
1072 desc->fMSColorRenderbufferID = 0;
1073 desc->fRTFBOID = 0;
1074 desc->fTexFBOID = 0;
1075 desc->fOwnIDs = true;
1076
1077 GrGLenum status;
1078 GrGLint err;
1079
1080 GR_GL(GenFramebuffers(1, &desc->fTexFBOID));
1081 if (!desc->fTexFBOID) {
1082 goto FAILED;
1083 }
1084
1085 GrGLenum msColorFormat;
1086
1087 // If we are using multisampling we will create two FBOS. We render
1088 // to one and then resolve to the texture bound to the other.
1089 if (desc->fSampleCnt > 1 && kNone_MSFBO != fMSFBOType) {
1090 GR_GL(GenFramebuffers(1, &desc->fRTFBOID));
1091 GR_GL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
1092 if (!desc->fRTFBOID ||
1093 !desc->fMSColorRenderbufferID ||
1094 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
1095 goto FAILED;
1096 }
1097 } else {
1098 desc->fRTFBOID = desc->fTexFBOID;
1099 }
1100
1101 if (desc->fRTFBOID != desc->fTexFBOID) {
1102 GrAssert(desc->fSampleCnt > 1);
1103 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
1104 desc->fMSColorRenderbufferID));
1105 GR_GL_NO_ERR(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1106 desc->fSampleCnt,
1107 msColorFormat,
1108 width, height));
1109 err = GrGLGetGLInterface()->fGetError();
1110 if (err != GR_GL_NO_ERROR) {
1111 goto FAILED;
1112 }
1113 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
1114 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1115 GR_GL_COLOR_ATTACHMENT0,
1116 GR_GL_RENDERBUFFER,
1117 desc->fMSColorRenderbufferID));
1118 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1119 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1120 goto FAILED;
1121 }
1122 }
1123 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
1124
1125 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1126 GR_GL_COLOR_ATTACHMENT0,
1127 GR_GL_TEXTURE_2D,
1128 texID, 0));
1129 status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1130 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1131 goto FAILED;
1132 }
1133
1134 return true;
1135
1136FAILED:
1137 if (desc->fMSColorRenderbufferID) {
1138 GR_GL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
1139 }
1140 if (desc->fRTFBOID != desc->fTexFBOID) {
1141 GR_GL(DeleteFramebuffers(1, &desc->fRTFBOID));
1142 }
1143 if (desc->fTexFBOID) {
1144 GR_GL(DeleteFramebuffers(1, &desc->fTexFBOID));
1145 }
1146 return false;
1147}
1148
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001149// good to set a break-point here to know when createTexture fails
1150static GrTexture* return_null_texture() {
1151// GrAssert(!"null texture");
1152 return NULL;
1153}
1154
1155#if GR_DEBUG
1156static size_t as_size_t(int x) {
1157 return x;
1158}
1159#endif
1160
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001161GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001162 const void* srcData,
1163 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001164
1165#if GR_COLLECT_STATS
1166 ++fStats.fTextureCreateCnt;
1167#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001168
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001169 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001170 GR_GL_NEAREST,
1171 GR_GL_CLAMP_TO_EDGE,
1172 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001173 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001174
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001175 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001176 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001177 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001178
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001179 glTexDesc.fContentWidth = desc.fWidth;
1180 glTexDesc.fContentHeight = desc.fHeight;
1181 glTexDesc.fAllocWidth = desc.fWidth;
1182 glTexDesc.fAllocHeight = desc.fHeight;
1183 glTexDesc.fFormat = desc.fFormat;
1184 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001185
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001186 glRTDesc.fMSColorRenderbufferID = 0;
1187 glRTDesc.fRTFBOID = 0;
1188 glRTDesc.fTexFBOID = 0;
1189 glRTDesc.fOwnIDs = true;
1190 glRTDesc.fConfig = glTexDesc.fFormat;
1191
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001192 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001193 if (!canBeTexture(desc.fFormat,
1194 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001195 &glTexDesc.fUploadFormat,
1196 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001197 return return_null_texture();
1198 }
1199
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001200 // We keep GrRenderTargets in GL's normal orientation so that they
1201 // can be drawn to by the outside world without the client having
1202 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001203 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001205
reed@google.comac10a2d2010-12-22 21:39:39 +00001206 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001207 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001208 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001209 GrPrintf("AA RT requested but not supported on this platform.");
1210 }
1211
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001212 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001213
reed@google.comac10a2d2010-12-22 21:39:39 +00001214 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001215 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001216 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1217 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001218 }
1219
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001220 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 glTexDesc.fAllocWidth);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001222 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 glTexDesc.fAllocHeight);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001224 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1225 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001226 return return_null_texture();
1227 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001228 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001229 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1230 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1231 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1232 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001233 return return_null_texture();
1234 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 }
1236
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001237 GR_GL(GenTextures(1, &glTexDesc.fTextureID));
1238 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001239 return return_null_texture();
1240 }
1241
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001242 this->setSpareTextureUnit();
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001243 GR_GL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001244 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1245 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001246 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001247 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1248 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001249 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001250 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1251 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001252 DEFAULT_PARAMS.fWrapS));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001253 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1254 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001255 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001256
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001257 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001258
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001259 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001261 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001262#if GR_COLLECT_STATS
1263 ++fStats.fRenderTargetCreateCnt;
1264#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001265 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1266 glTexDesc.fAllocHeight,
1267 glTexDesc.fTextureID,
1268 &glRTDesc)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001269 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001270 return return_null_texture();
1271 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001272 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1273 } else {
1274 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001275 }
1276#ifdef TRACE_TEXTURE_CREATION
1277 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1278 tex->fTextureID, width, height, tex->fUploadByteCount);
1279#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001280 return tex;
1281}
1282
1283namespace {
1284void inline get_stencil_rb_sizes(GrGLuint rb, GrGLStencilBuffer::Format* format) {
1285 // we shouldn't ever know one size and not the other
1286 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1287 (kUnknownBitCount == format->fTotalBits));
1288 if (kUnknownBitCount == format->fStencilBits) {
1289 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
1290 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1291 (GrGLint*)&format->fStencilBits);
1292 if (format->fPacked) {
1293 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
1294 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1295 (GrGLint*)&format->fTotalBits);
1296 format->fTotalBits += format->fStencilBits;
1297 } else {
1298 format->fTotalBits = format->fStencilBits;
1299 }
1300 }
1301}
1302}
1303
1304bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1305 int width, int height) {
1306
1307 // All internally created RTs are also textures. We don't create
1308 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1309 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001310 GrAssert(width >= rt->allocatedWidth());
1311 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001312
1313 int samples = rt->numSamples();
1314 GrGLuint sbID;
1315 GR_GL(GenRenderbuffers(1, &sbID));
1316 if (!sbID) {
1317 return false;
1318 }
1319
1320 GrGLStencilBuffer* sb = NULL;
1321
1322 int stencilFmtCnt = fStencilFormats.count();
1323 for (int i = 0; i < stencilFmtCnt; ++i) {
1324 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
1325 // we start with the last stencil format that succeeded in hopes
1326 // that we won't go through this loop more than once after the
1327 // first (painful) stencil creation.
1328 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
1329 // we do this if so that we don't call the multisample
1330 // version on a GL that doesn't have an MSAA extension.
1331 if (samples > 1) {
1332 GR_GL_NO_ERR(RenderbufferStorageMultisample(
1333 GR_GL_RENDERBUFFER,
1334 samples,
1335 fStencilFormats[sIdx].fInternalFormat,
1336 width,
1337 height));
1338 } else {
1339 GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER,
1340 fStencilFormats[sIdx].fInternalFormat,
1341 width, height));
1342 }
1343
1344 GrGLenum err = GrGLGetGLInterface()->fGetError();
1345 if (err == GR_GL_NO_ERROR) {
1346 // After sized formats we attempt an unsized format and take whatever
1347 // sizes GL gives us. In that case we query for the size.
1348 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
1349 get_stencil_rb_sizes(sbID, &format);
1350 sb = new GrGLStencilBuffer(this, sbID, width, height, format);
1351 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1352 fLastSuccessfulStencilFmtIdx = sIdx;
1353 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001354 return true;
1355 }
1356 sb->abandon(); // otherwise we lose sbID
1357 sb->unref();
1358 }
1359 }
1360 GR_GL(DeleteRenderbuffers(1, &sbID));
1361 return NULL;
1362}
1363
1364bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1365 GrRenderTarget* rt) {
1366 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1367
1368 GrGLuint fbo = glrt->renderFBOID();
1369
1370 if (NULL == sb) {
1371 if (NULL != rt->getStencilBuffer()) {
1372 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1373 GR_GL_STENCIL_ATTACHMENT,
1374 GR_GL_RENDERBUFFER, 0));
1375 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1376 GR_GL_DEPTH_ATTACHMENT,
1377 GR_GL_RENDERBUFFER, 0));
1378#if GR_DEBUG
1379 GrGLenum status =
1380 GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1381 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1382#endif
1383 }
1384 return true;
1385 } else {
1386 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1387 GrGLuint rb = glsb->renderbufferID();
1388
reed@google.comac10a2d2010-12-22 21:39:39 +00001389 fHWDrawState.fRenderTarget = NULL;
1390
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001391 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1392 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1393 GR_GL_STENCIL_ATTACHMENT,
1394 GR_GL_RENDERBUFFER, rb));
1395 if (glsb->format().fPacked) {
1396 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1397 GR_GL_DEPTH_ATTACHMENT,
1398 GR_GL_RENDERBUFFER, rb));
1399 } else {
1400 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1401 GR_GL_DEPTH_ATTACHMENT,
1402 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001403 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001404
1405 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1406 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1407 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1408 GR_GL_STENCIL_ATTACHMENT,
1409 GR_GL_RENDERBUFFER, 0));
1410 if (glsb->format().fPacked) {
1411 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1412 GR_GL_DEPTH_ATTACHMENT,
1413 GR_GL_RENDERBUFFER, 0));
1414 }
1415 return false;
1416 } else {
1417 rt->setStencilBuffer(sb);
1418 return true;
1419 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001420 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001421}
1422
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001423////////////////////////////////////////////////////////////////////////////////
1424
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001425GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001426 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001427 GR_GL(GenBuffers(1, &id));
1428 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001429 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001430 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001431 GrGLClearErr();
1432 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001433 GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1434 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1435 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001436 GR_GL(DeleteBuffers(1, &id));
1437 // deleting bound buffer does implicit bind to 0
1438 fHWGeometryState.fVertexBuffer = NULL;
1439 return NULL;
1440 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001441 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001442 size, dynamic);
1443 fHWGeometryState.fVertexBuffer = vertexBuffer;
1444 return vertexBuffer;
1445 }
1446 return NULL;
1447}
1448
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001449GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001450 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001451 GR_GL(GenBuffers(1, &id));
1452 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001453 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001454 GrGLClearErr();
1455 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001456 GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1457 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1458 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 GR_GL(DeleteBuffers(1, &id));
1460 // deleting bound buffer does implicit bind to 0
1461 fHWGeometryState.fIndexBuffer = NULL;
1462 return NULL;
1463 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001464 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 size, dynamic);
1466 fHWGeometryState.fIndexBuffer = indexBuffer;
1467 return indexBuffer;
1468 }
1469 return NULL;
1470}
1471
reed@google.comac10a2d2010-12-22 21:39:39 +00001472void GrGpuGL::flushScissor(const GrIRect* rect) {
1473 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001474 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001475 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001476
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001477 GrGLIRect scissor;
1478 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001479 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001480 rect->width(), rect->height());
1481 if (scissor.contains(vp)) {
1482 rect = NULL;
1483 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001484 }
1485
1486 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001488 scissor.pushToGLScissor();
reed@google.comac10a2d2010-12-22 21:39:39 +00001489 fHWBounds.fScissorRect = scissor;
1490 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 if (!fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001492 GR_GL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001493 fHWBounds.fScissorEnabled = true;
1494 }
1495 } else {
1496 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001497 GR_GL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001498 fHWBounds.fScissorEnabled = false;
1499 }
1500 }
1501}
1502
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001503void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001504 if (NULL == fCurrDrawState.fRenderTarget) {
1505 return;
1506 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001507 GrIRect r;
1508 if (NULL != rect) {
1509 // flushScissor expects rect to be clipped to the target.
1510 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001511 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1512 fCurrDrawState.fRenderTarget->height());
1513 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001514 rect = &r;
1515 } else {
1516 return;
1517 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001518 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001519 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001520 this->flushScissor(rect);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001521 GR_GL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001522 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
reed@google.comac10a2d2010-12-22 21:39:39 +00001523 GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
1524 GrColorUnpackG(color)/255.f,
1525 GrColorUnpackB(color)/255.f,
1526 GrColorUnpackA(color)/255.f));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001527 GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001528}
1529
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001530void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001531 if (NULL == fCurrDrawState.fRenderTarget) {
1532 return;
1533 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001534
1535 this->flushRenderTarget(&GrIRect::EmptyIRect());
1536
reed@google.comac10a2d2010-12-22 21:39:39 +00001537 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001538 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001539 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001540 }
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001541 GR_GL(StencilMask(0xffffffff));
1542 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001543 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001544 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001545}
1546
bsalomon@google.com398109c2011-04-14 18:40:27 +00001547void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001548 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001549
1550 // this should only be called internally when we know we have a
1551 // stencil buffer.
1552 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001553#if 0
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001554 GrGLint stencilBitCount =
1555 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001556 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001557 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001558#else
1559 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001560 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001561 // turned into draws. Our contract on GrDrawTarget says that
1562 // changing the clip between stencil passes may or may not
1563 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001564 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001565#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001566 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001567 this->flushScissor(&rect);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001568 GR_GL(StencilMask(clipStencilMask));
1569 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001570 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001571 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001572}
1573
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001574void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001575 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001576}
1577
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001578bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1579 int left, int top, int width, int height,
1580 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001581 GrGLenum internalFormat; // we don't use this for glReadPixels
1582 GrGLenum format;
1583 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001584 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1585 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001586 }
1587 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1588 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1589 switch (tgt->getResolveType()) {
1590 case GrGLRenderTarget::kCantResolve_ResolveType:
1591 return false;
1592 case GrGLRenderTarget::kAutoResolves_ResolveType:
1593 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1594 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001595 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001596 break;
1597 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001598 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001599 // we don't track the state of the READ FBO ID.
1600 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
1601 break;
1602 default:
1603 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 }
1605
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001606 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001607
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001608 // the read rect is viewport-relative
1609 GrGLIRect readRect;
1610 readRect.setRelativeTo(glvp, left, top, width, height);
1611 GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001612 readRect.fWidth, readRect.fHeight,
bsalomon@google.com316f99232011-01-13 21:28:12 +00001613 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001614
1615 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1616 // API presents top-to-bottom
1617 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001618 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001619 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 void* tmp = rowStorage.get();
1621
1622 const int halfY = height >> 1;
1623 char* top = reinterpret_cast<char*>(buffer);
1624 char* bottom = top + (height - 1) * stride;
1625 for (int y = 0; y < halfY; y++) {
1626 memcpy(tmp, top, stride);
1627 memcpy(top, bottom, stride);
1628 memcpy(bottom, tmp, stride);
1629 top += stride;
1630 bottom -= stride;
1631 }
1632 }
1633 return true;
1634}
1635
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001636void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001637
1638 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1639
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001640 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001641 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001642 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001643 #if GR_COLLECT_STATS
1644 ++fStats.fRenderTargetChngCnt;
1645 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 #if GR_DEBUG
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001647 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1648 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001649 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001650 }
1651 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001652 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001653 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001654 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001655 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001656 vp.pushToGLViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 fHWBounds.fViewportRect = vp;
1658 }
1659 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001660 if (NULL == bound || !bound->isEmpty()) {
1661 rt->flagAsNeedingResolve(bound);
1662 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001663}
1664
twiz@google.com0f31ca72011-03-18 17:38:11 +00001665GrGLenum gPrimitiveType2GLMode[] = {
1666 GR_GL_TRIANGLES,
1667 GR_GL_TRIANGLE_STRIP,
1668 GR_GL_TRIANGLE_FAN,
1669 GR_GL_POINTS,
1670 GR_GL_LINES,
1671 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001672};
1673
bsalomon@google.comd302f142011-03-03 13:54:13 +00001674#define SWAP_PER_DRAW 0
1675
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001676#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001677 #if GR_MAC_BUILD
1678 #include <AGL/agl.h>
1679 #elif GR_WIN32_BUILD
1680 void SwapBuf() {
1681 DWORD procID = GetCurrentProcessId();
1682 HWND hwnd = GetTopWindow(GetDesktopWindow());
1683 while(hwnd) {
1684 DWORD wndProcID = 0;
1685 GetWindowThreadProcessId(hwnd, &wndProcID);
1686 if(wndProcID == procID) {
1687 SwapBuffers(GetDC(hwnd));
1688 }
1689 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1690 }
1691 }
1692 #endif
1693#endif
1694
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001695void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1696 uint32_t startVertex,
1697 uint32_t startIndex,
1698 uint32_t vertexCount,
1699 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001700 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1701
twiz@google.com0f31ca72011-03-18 17:38:11 +00001702 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001703
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001704 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1705 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1706
1707 // our setupGeometry better have adjusted this to zero since
1708 // DrawElements always draws from the begining of the arrays for idx 0.
1709 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001710
1711 GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
twiz@google.com0f31ca72011-03-18 17:38:11 +00001712 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001713#if SWAP_PER_DRAW
1714 glFlush();
1715 #if GR_MAC_BUILD
1716 aglSwapBuffers(aglGetCurrentContext());
1717 int set_a_break_pt_here = 9;
1718 aglSwapBuffers(aglGetCurrentContext());
1719 #elif GR_WIN32_BUILD
1720 SwapBuf();
1721 int set_a_break_pt_here = 9;
1722 SwapBuf();
1723 #endif
1724#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001725}
1726
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001727void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1728 uint32_t startVertex,
1729 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1731
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001732 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1733
1734 // our setupGeometry better have adjusted this to zero.
1735 // DrawElements doesn't take an offset so we always adjus the startVertex.
1736 GrAssert(0 == startVertex);
1737
1738 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1739 // account for startVertex in the DrawElements case. So we always
1740 // rely on setupGeometry to have accounted for startVertex.
reed@google.comac10a2d2010-12-22 21:39:39 +00001741 GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001742#if SWAP_PER_DRAW
1743 glFlush();
1744 #if GR_MAC_BUILD
1745 aglSwapBuffers(aglGetCurrentContext());
1746 int set_a_break_pt_here = 9;
1747 aglSwapBuffers(aglGetCurrentContext());
1748 #elif GR_WIN32_BUILD
1749 SwapBuf();
1750 int set_a_break_pt_here = 9;
1751 SwapBuf();
1752 #endif
1753#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001754}
1755
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001756void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001757
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001758 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001759 GrAssert(kNone_MSFBO != fMSFBOType);
1760 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001761 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 rt->renderFBOID()));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001763 GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 rt->textureFBOID()));
1765 #if GR_COLLECT_STATS
1766 ++fStats.fRenderTargetChngCnt;
1767 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001768 // make sure we go through flushRenderTarget() since we've modified
1769 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001771 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001772 const GrIRect dirtyRect = rt->getResolveRect();
1773 GrGLIRect r;
1774 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1775 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001776
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001777 if (kAppleES_MSFBO == fMSFBOType) {
1778 // Apple's extension uses the scissor as the blit bounds.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001779 GR_GL(Enable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001780 GR_GL(Scissor(r.fLeft, r.fBottom,
1781 r.fWidth, r.fHeight));
twiz@google.com59a190b2011-03-14 21:23:01 +00001782 GR_GL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001783 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001784 fHWBounds.fScissorEnabled = true;
1785 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001786 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001787 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001788 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1789 flushScissor(NULL);
1790 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001791 int right = r.fLeft + r.fWidth;
1792 int top = r.fBottom + r.fHeight;
1793 GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1794 r.fLeft, r.fBottom, right, top,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001795 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001796 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001797 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001798 }
1799}
1800
twiz@google.com0f31ca72011-03-18 17:38:11 +00001801static const GrGLenum grToGLStencilFunc[] = {
1802 GR_GL_ALWAYS, // kAlways_StencilFunc
1803 GR_GL_NEVER, // kNever_StencilFunc
1804 GR_GL_GREATER, // kGreater_StencilFunc
1805 GR_GL_GEQUAL, // kGEqual_StencilFunc
1806 GR_GL_LESS, // kLess_StencilFunc
1807 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1808 GR_GL_EQUAL, // kEqual_StencilFunc,
1809 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001810};
1811GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1812GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1813GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1814GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1815GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1816GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1817GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1818GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1819GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1820
twiz@google.com0f31ca72011-03-18 17:38:11 +00001821static const GrGLenum grToGLStencilOp[] = {
1822 GR_GL_KEEP, // kKeep_StencilOp
1823 GR_GL_REPLACE, // kReplace_StencilOp
1824 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1825 GR_GL_INCR, // kIncClamp_StencilOp
1826 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1827 GR_GL_DECR, // kDecClamp_StencilOp
1828 GR_GL_ZERO, // kZero_StencilOp
1829 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830};
1831GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1832GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1833GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1834GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1835GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1836GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1837GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1838GR_STATIC_ASSERT(6 == kZero_StencilOp);
1839GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1840
reed@google.comac10a2d2010-12-22 21:39:39 +00001841void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001842 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001843
1844 // use stencil for clipping if clipping is enabled and the clip
1845 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001846 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001847 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001848 bool stencilChange = fHWStencilClip != stencilClip ||
1849 fHWDrawState.fStencilSettings != *settings ||
1850 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1851 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001852
1853 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001854
bsalomon@google.comd302f142011-03-03 13:54:13 +00001855 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1856 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001857
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858 if (settings->isDisabled()) {
1859 if (stencilClip) {
1860 settings = &gClipStencilSettings;
1861 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001862 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863
1864 if (settings->isDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001865 GR_GL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001867 GR_GL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001868 #if GR_DEBUG
1869 if (!fStencilWrapOpsSupport) {
1870 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1871 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1872 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1873 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1874 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1875 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1876 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1877 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1878 }
1879 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001880 int stencilBits = 0;
1881 GrStencilBuffer* stencilBuffer =
1882 fCurrDrawState.fRenderTarget->getStencilBuffer();
1883 if (NULL != stencilBuffer) {
1884 stencilBits = stencilBuffer->bits();
1885 }
1886 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887 GrAssert(stencilBits ||
1888 (GrStencilSettings::gDisabled ==
1889 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001890 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1891 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001892
1893 unsigned int frontRef = settings->fFrontFuncRef;
1894 unsigned int frontMask = settings->fFrontFuncMask;
1895 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001896 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897
1898 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1899
1900 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1901 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1902 } else {
1903 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1904
1905 ConvertStencilFuncAndMask(settings->fFrontFunc,
1906 stencilClip,
1907 clipStencilMask,
1908 userStencilMask,
1909 &frontRef,
1910 &frontMask);
1911 frontWriteMask &= userStencilMask;
1912 }
1913 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001914 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001915 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001916 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001917 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001918 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001919 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001920 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001921 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001922 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923
1924 unsigned int backRef = settings->fBackFuncRef;
1925 unsigned int backMask = settings->fBackFuncMask;
1926 unsigned int backWriteMask = settings->fBackWriteMask;
1927
1928
1929 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1930 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1931 backFunc = grToGLStencilFunc[settings->fBackFunc];
1932 } else {
1933 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1934 ConvertStencilFuncAndMask(settings->fBackFunc,
1935 stencilClip,
1936 clipStencilMask,
1937 userStencilMask,
1938 &backRef,
1939 &backMask);
1940 backWriteMask &= userStencilMask;
1941 }
1942
twiz@google.com0f31ca72011-03-18 17:38:11 +00001943 GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask));
1944 GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1945 GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask));
1946 GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1947 GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001948 grToGLStencilOp[settings->fFrontPassOp],
1949 grToGLStencilOp[settings->fFrontPassOp]));
1950
twiz@google.com0f31ca72011-03-18 17:38:11 +00001951 GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001952 grToGLStencilOp[settings->fBackPassOp],
1953 grToGLStencilOp[settings->fBackPassOp]));
1954 } else {
1955 GR_GL(StencilFunc(frontFunc, frontRef, frontMask));
1956 GR_GL(StencilMask(frontWriteMask));
1957 GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
1958 grToGLStencilOp[settings->fFrontPassOp],
1959 grToGLStencilOp[settings->fFrontPassOp]));
1960 }
1961 }
1962 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001963 fHWStencilClip = stencilClip;
1964 }
1965}
1966
bsalomon@google.com0650e812011-04-08 18:07:53 +00001967bool GrGpuGL::useSmoothLines() {
1968 // there is a conflict between using smooth lines and our use of
1969 // premultiplied alpha. Smooth lines tweak the incoming alpha value
1970 // but not in a premul-alpha way. So we only use them when our alpha
1971 // is 0xff.
1972
1973 // TODO: write a smarter line frag shader.
1974
1975 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
1976 canDisableBlend();
1977}
1978
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001979void GrGpuGL::flushAAState(GrPrimitiveType type) {
1980 if (GR_GL_SUPPORT_DESKTOP) {
1981 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1982 // smooth lines.
1983
1984 // we prefer smooth lines over multisampled lines
1985 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001986 if (GrIsPrimTypeLines(type)) {
1987 bool smooth = useSmoothLines();
1988 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001989 GR_GL(Enable(GR_GL_LINE_SMOOTH));
1990 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001991 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001992 GR_GL(Disable(GR_GL_LINE_SMOOTH));
1993 fHWAAState.fSmoothLineEnabled = false;
1994 }
1995 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1996 fHWAAState.fMSAAEnabled) {
1997 GR_GL(Disable(GR_GL_MULTISAMPLE));
1998 fHWAAState.fMSAAEnabled = false;
1999 }
2000 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2001 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
2002 fHWAAState.fMSAAEnabled) {
2003 if (fHWAAState.fMSAAEnabled) {
2004 GR_GL(Disable(GR_GL_MULTISAMPLE));
2005 fHWAAState.fMSAAEnabled = false;
2006 } else {
2007 GR_GL(Enable(GR_GL_MULTISAMPLE));
2008 fHWAAState.fMSAAEnabled = true;
2009 }
2010 }
2011 }
2012}
2013
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002014void GrGpuGL::flushBlend(GrPrimitiveType type,
2015 GrBlendCoeff srcCoeff,
2016 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002017 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
2018 if (fHWBlendDisabled) {
2019 GR_GL(Enable(GR_GL_BLEND));
2020 fHWBlendDisabled = false;
2021 }
2022 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
2023 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
2024 GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2025 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
2026 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
2027 fHWDrawState.fDstBlend = kISA_BlendCoeff;
2028 }
2029 } else {
2030 bool blendOff = canDisableBlend();
2031 if (fHWBlendDisabled != blendOff) {
2032 if (blendOff) {
2033 GR_GL(Disable(GR_GL_BLEND));
2034 } else {
2035 GR_GL(Enable(GR_GL_BLEND));
2036 }
2037 fHWBlendDisabled = blendOff;
2038 }
2039 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002040 if (fHWDrawState.fSrcBlend != srcCoeff ||
2041 fHWDrawState.fDstBlend != dstCoeff) {
2042 GR_GL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2043 gXfermodeCoeff2Blend[dstCoeff]));
2044 fHWDrawState.fSrcBlend = srcCoeff;
2045 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002046 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002047 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2048 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002049 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2050
2051 float c[] = {
2052 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2053 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2054 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2055 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2056 };
2057 GR_GL(BlendColor(c[0], c[1], c[2], c[3]));
2058 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2059 }
2060 }
2061 }
2062}
2063
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002064static unsigned grToGLFilter(GrSamplerState::Filter filter) {
2065 switch (filter) {
2066 case GrSamplerState::kBilinear_Filter:
2067 case GrSamplerState::k4x4Downsample_Filter:
2068 return GR_GL_LINEAR;
2069 case GrSamplerState::kNearest_Filter:
2070 case GrSamplerState::kConvolution_Filter:
2071 return GR_GL_NEAREST;
2072 default:
2073 GrAssert(!"Unknown filter type");
2074 return GR_GL_LINEAR;
2075 }
2076}
2077
bsalomon@google.comffca4002011-02-22 20:34:01 +00002078bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002079
2080 // GrGpu::setupClipAndFlushState should have already checked this
2081 // and bailed if not true.
2082 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002083
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002084 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002085 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002086 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002087 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002088
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002089 // true for now, but maybe not with GrEffect.
2090 GrAssert(NULL != nextTexture);
2091 // if we created a rt/tex and rendered to it without using a
2092 // texture and now we're texuring from the rt it will still be
2093 // the last bound texture, but it needs resolving. So keep this
2094 // out of the "last != next" check.
2095 GrGLRenderTarget* texRT =
2096 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2097 if (NULL != texRT) {
2098 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002099 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002100
2101 if (fHWDrawState.fTextures[s] != nextTexture) {
2102 setTextureUnit(s);
2103 GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
2104 #if GR_COLLECT_STATS
2105 ++fStats.fTextureChngCnt;
2106 #endif
2107 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2108 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002109 // The texture matrix has to compensate for texture width/height
2110 // and NPOT-embedded-in-POT
2111 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002112 }
2113
2114 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2115 const GrGLTexture::TexParams& oldTexParams =
2116 nextTexture->getTexParams();
2117 GrGLTexture::TexParams newTexParams;
2118
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002119 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002120
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002121 newTexParams.fWrapS =
2122 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()];
2123 newTexParams.fWrapT =
2124 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()];
2125
2126 if (newTexParams.fFilter != oldTexParams.fFilter) {
2127 setTextureUnit(s);
2128 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2129 GR_GL_TEXTURE_MAG_FILTER,
2130 newTexParams.fFilter));
2131 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2132 GR_GL_TEXTURE_MIN_FILTER,
2133 newTexParams.fFilter));
2134 }
2135 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2136 setTextureUnit(s);
2137 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2138 GR_GL_TEXTURE_WRAP_S,
2139 newTexParams.fWrapS));
2140 }
2141 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2142 setTextureUnit(s);
2143 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2144 GR_GL_TEXTURE_WRAP_T,
2145 newTexParams.fWrapT));
2146 }
2147 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002148 }
2149 }
2150
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002151 GrIRect* rect = NULL;
2152 GrIRect clipBounds;
2153 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2154 fClip.hasConservativeBounds()) {
2155 fClip.getConservativeBounds().roundOut(&clipBounds);
2156 rect = &clipBounds;
2157 }
2158 this->flushRenderTarget(rect);
2159 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002160
reed@google.comac10a2d2010-12-22 21:39:39 +00002161 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2162 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2163 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002164 GR_GL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002165 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002166 GR_GL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002167 }
2168 }
2169
bsalomon@google.comd302f142011-03-03 13:54:13 +00002170 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2171 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002172 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002173 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002174 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002175 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002176 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002177 }
2178 GR_GL(ColorMask(mask, mask, mask, mask));
2179 }
2180
bsalomon@google.comd302f142011-03-03 13:54:13 +00002181 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2182 switch (fCurrDrawState.fDrawFace) {
2183 case kCCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002184 GR_GL(Enable(GR_GL_CULL_FACE));
2185 GR_GL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002186 break;
2187 case kCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002188 GR_GL(Enable(GR_GL_CULL_FACE));
2189 GR_GL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002190 break;
2191 case kBoth_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002192 GR_GL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002193 break;
2194 default:
2195 GrCrash("Unknown draw face.");
2196 }
2197 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2198 }
2199
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002200#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002201 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002202 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002203 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002204 NULL == fCurrDrawState.fRenderTarget ||
2205 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002206 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002207 fCurrDrawState.fRenderTarget);
2208 }
2209#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002210
reed@google.comac10a2d2010-12-22 21:39:39 +00002211 flushStencil();
2212
bsalomon@google.comd302f142011-03-03 13:54:13 +00002213 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002214 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002215 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002216}
2217
2218void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002219 if (fHWGeometryState.fVertexBuffer != buffer) {
2220 fHWGeometryState.fArrayPtrsDirty = true;
2221 fHWGeometryState.fVertexBuffer = buffer;
2222 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002223}
2224
2225void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002226 if (fHWGeometryState.fVertexBuffer == buffer) {
2227 // deleting bound buffer does implied bind to 0
2228 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002229 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002230 }
2231}
2232
2233void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002234 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002235}
2236
2237void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002238 if (fHWGeometryState.fIndexBuffer == buffer) {
2239 // deleting bound buffer does implied bind to 0
2240 fHWGeometryState.fIndexBuffer = NULL;
2241 }
2242}
2243
reed@google.comac10a2d2010-12-22 21:39:39 +00002244void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2245 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002246 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002247 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002248 }
2249 if (fHWDrawState.fRenderTarget == renderTarget) {
2250 fHWDrawState.fRenderTarget = NULL;
2251 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002252}
2253
2254void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002255 for (int s = 0; s < kNumStages; ++s) {
2256 if (fCurrDrawState.fTextures[s] == texture) {
2257 fCurrDrawState.fTextures[s] = NULL;
2258 }
2259 if (fHWDrawState.fTextures[s] == texture) {
2260 // deleting bound texture does implied bind to 0
2261 fHWDrawState.fTextures[s] = NULL;
2262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002263 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002264}
2265
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002266bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002267 GrGLenum* internalFormat,
2268 GrGLenum* format,
2269 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002270 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002271 case kRGBA_8888_GrPixelConfig:
2272 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002273 *format = GR_GL_32BPP_COLOR_FORMAT;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002274 if (GR_GL_SUPPORT_ES) {
2275 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2276 // format for a BGRA is BGRA not RGBA (as on desktop)
2277 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2278 } else {
2279 *internalFormat = GR_GL_RGBA;
2280 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002281 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002282 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002283 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002284 *format = GR_GL_RGB;
2285 *internalFormat = GR_GL_RGB;
2286 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002287 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002288 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002289 *format = GR_GL_RGBA;
2290 *internalFormat = GR_GL_RGBA;
2291 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002292 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002293 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002294 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002295 *format = GR_GL_PALETTE8_RGBA8;
2296 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002297 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002298 } else {
2299 return false;
2300 }
2301 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002302 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002303 *format = GR_GL_ALPHA;
2304 *internalFormat = GR_GL_ALPHA;
2305 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002306 break;
2307 default:
2308 return false;
2309 }
2310 return true;
2311}
2312
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002313void GrGpuGL::setTextureUnit(int unit) {
2314 GrAssert(unit >= 0 && unit < kNumStages);
2315 if (fActiveTextureUnitIdx != unit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002316 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002317 fActiveTextureUnitIdx = unit;
2318 }
2319}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002320
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002321void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002322 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
2323 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002324 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2325 }
2326}
2327
reed@google.comac10a2d2010-12-22 21:39:39 +00002328/* On ES the internalFormat and format must match for TexImage and we use
2329 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2330 decide the internalFormat. However, on ES internalFormat for
2331 RenderBufferStorage* has to be a specific format (not a base format like
2332 GL_RGBA).
2333 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002334bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002335 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002336 case kRGBA_8888_GrPixelConfig:
2337 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002338 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002339 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002340 return true;
2341 } else {
2342 return false;
2343 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002344 case kRGB_565_GrPixelConfig:
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002345 GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it
2346 // with FBO extension desktop GL has
2347 // no such internal format
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002348 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002349 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002350 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002351 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002352 return true;
2353 default:
2354 return false;
2355 }
2356}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002357
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002358void GrGpuGL::resetDirtyFlags() {
2359 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2360}
2361
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002362void GrGpuGL::setBuffers(bool indexed,
2363 int* extraVertexOffset,
2364 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002365
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002366 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002367
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002368 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2369
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002370 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002371 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002372 case kBuffer_GeometrySrcType:
2373 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002374 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002375 break;
2376 case kArray_GeometrySrcType:
2377 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002378 this->finalizeReservedVertices();
2379 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2380 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002381 break;
2382 default:
2383 vbuf = NULL; // suppress warning
2384 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002385 }
2386
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002387 GrAssert(NULL != vbuf);
2388 GrAssert(!vbuf->isLocked());
2389 if (fHWGeometryState.fVertexBuffer != vbuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002390 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002391 fHWGeometryState.fArrayPtrsDirty = true;
2392 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002393 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002394
2395 if (indexed) {
2396 GrAssert(NULL != extraIndexOffset);
2397
2398 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002399 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002400 case kBuffer_GeometrySrcType:
2401 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002402 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002403 break;
2404 case kArray_GeometrySrcType:
2405 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002406 this->finalizeReservedIndices();
2407 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2408 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002409 break;
2410 default:
2411 ibuf = NULL; // suppress warning
2412 GrCrash("Unknown geometry src type!");
2413 }
2414
2415 GrAssert(NULL != ibuf);
2416 GrAssert(!ibuf->isLocked());
2417 if (fHWGeometryState.fIndexBuffer != ibuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002418 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002419 fHWGeometryState.fIndexBuffer = ibuf;
2420 }
2421 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002422}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002423
2424int GrGpuGL::getMaxEdges() const {
2425 // FIXME: This is a pessimistic estimate based on how many other things
2426 // want to add uniforms. This should be centralized somewhere.
2427 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2428}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002429