blob: 4c6c60e821508ce5ba4e6ac6a52be1f6b45d1e46 [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());
1310 // if this thing is bloated for NPOT reasons we'll have to bloat the SB
1311 // as well.
1312 GrGLTexture* tex = (GrGLTexture*) rt->asTexture();
1313 width = GrMax(width, tex->allocWidth());
bsalomon@google.comc7e6d082011-08-05 15:38:49 +00001314 height = GrMax(height, tex->allocHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001315
1316 int samples = rt->numSamples();
1317 GrGLuint sbID;
1318 GR_GL(GenRenderbuffers(1, &sbID));
1319 if (!sbID) {
1320 return false;
1321 }
1322
1323 GrGLStencilBuffer* sb = NULL;
1324
1325 int stencilFmtCnt = fStencilFormats.count();
1326 for (int i = 0; i < stencilFmtCnt; ++i) {
1327 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
1328 // we start with the last stencil format that succeeded in hopes
1329 // that we won't go through this loop more than once after the
1330 // first (painful) stencil creation.
1331 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
1332 // we do this if so that we don't call the multisample
1333 // version on a GL that doesn't have an MSAA extension.
1334 if (samples > 1) {
1335 GR_GL_NO_ERR(RenderbufferStorageMultisample(
1336 GR_GL_RENDERBUFFER,
1337 samples,
1338 fStencilFormats[sIdx].fInternalFormat,
1339 width,
1340 height));
1341 } else {
1342 GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER,
1343 fStencilFormats[sIdx].fInternalFormat,
1344 width, height));
1345 }
1346
1347 GrGLenum err = GrGLGetGLInterface()->fGetError();
1348 if (err == GR_GL_NO_ERROR) {
1349 // After sized formats we attempt an unsized format and take whatever
1350 // sizes GL gives us. In that case we query for the size.
1351 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
1352 get_stencil_rb_sizes(sbID, &format);
1353 sb = new GrGLStencilBuffer(this, sbID, width, height, format);
1354 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1355 fLastSuccessfulStencilFmtIdx = sIdx;
1356 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001357 return true;
1358 }
1359 sb->abandon(); // otherwise we lose sbID
1360 sb->unref();
1361 }
1362 }
1363 GR_GL(DeleteRenderbuffers(1, &sbID));
1364 return NULL;
1365}
1366
1367bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1368 GrRenderTarget* rt) {
1369 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1370
1371 GrGLuint fbo = glrt->renderFBOID();
1372
1373 if (NULL == sb) {
1374 if (NULL != rt->getStencilBuffer()) {
1375 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1376 GR_GL_STENCIL_ATTACHMENT,
1377 GR_GL_RENDERBUFFER, 0));
1378 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1379 GR_GL_DEPTH_ATTACHMENT,
1380 GR_GL_RENDERBUFFER, 0));
1381#if GR_DEBUG
1382 GrGLenum status =
1383 GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1384 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1385#endif
1386 }
1387 return true;
1388 } else {
1389 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1390 GrGLuint rb = glsb->renderbufferID();
1391
reed@google.comac10a2d2010-12-22 21:39:39 +00001392 fHWDrawState.fRenderTarget = NULL;
1393
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001394 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1395 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1396 GR_GL_STENCIL_ATTACHMENT,
1397 GR_GL_RENDERBUFFER, rb));
1398 if (glsb->format().fPacked) {
1399 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1400 GR_GL_DEPTH_ATTACHMENT,
1401 GR_GL_RENDERBUFFER, rb));
1402 } else {
1403 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1404 GR_GL_DEPTH_ATTACHMENT,
1405 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001406 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001407
1408 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1409 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1410 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1411 GR_GL_STENCIL_ATTACHMENT,
1412 GR_GL_RENDERBUFFER, 0));
1413 if (glsb->format().fPacked) {
1414 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1415 GR_GL_DEPTH_ATTACHMENT,
1416 GR_GL_RENDERBUFFER, 0));
1417 }
1418 return false;
1419 } else {
1420 rt->setStencilBuffer(sb);
1421 return true;
1422 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001423 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001424}
1425
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001426////////////////////////////////////////////////////////////////////////////////
1427
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001428GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001429 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001430 GR_GL(GenBuffers(1, &id));
1431 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001432 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001433 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001434 GrGLClearErr();
1435 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001436 GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1437 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1438 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001439 GR_GL(DeleteBuffers(1, &id));
1440 // deleting bound buffer does implicit bind to 0
1441 fHWGeometryState.fVertexBuffer = NULL;
1442 return NULL;
1443 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001444 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001445 size, dynamic);
1446 fHWGeometryState.fVertexBuffer = vertexBuffer;
1447 return vertexBuffer;
1448 }
1449 return NULL;
1450}
1451
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001452GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001453 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001454 GR_GL(GenBuffers(1, &id));
1455 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001456 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001457 GrGLClearErr();
1458 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001459 GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1460 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1461 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001462 GR_GL(DeleteBuffers(1, &id));
1463 // deleting bound buffer does implicit bind to 0
1464 fHWGeometryState.fIndexBuffer = NULL;
1465 return NULL;
1466 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001467 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001468 size, dynamic);
1469 fHWGeometryState.fIndexBuffer = indexBuffer;
1470 return indexBuffer;
1471 }
1472 return NULL;
1473}
1474
reed@google.comac10a2d2010-12-22 21:39:39 +00001475void GrGpuGL::flushScissor(const GrIRect* rect) {
1476 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001477 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001478 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001479
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001480 GrGLIRect scissor;
1481 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001482 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001483 rect->width(), rect->height());
1484 if (scissor.contains(vp)) {
1485 rect = NULL;
1486 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 }
1488
1489 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001490 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001491 scissor.pushToGLScissor();
reed@google.comac10a2d2010-12-22 21:39:39 +00001492 fHWBounds.fScissorRect = scissor;
1493 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001494 if (!fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001495 GR_GL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001496 fHWBounds.fScissorEnabled = true;
1497 }
1498 } else {
1499 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001500 GR_GL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001501 fHWBounds.fScissorEnabled = false;
1502 }
1503 }
1504}
1505
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001506void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001507 if (NULL == fCurrDrawState.fRenderTarget) {
1508 return;
1509 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001510 GrIRect r;
1511 if (NULL != rect) {
1512 // flushScissor expects rect to be clipped to the target.
1513 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001514 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1515 fCurrDrawState.fRenderTarget->height());
1516 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001517 rect = &r;
1518 } else {
1519 return;
1520 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001522 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001523 this->flushScissor(rect);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001524 GR_GL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001525 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
reed@google.comac10a2d2010-12-22 21:39:39 +00001526 GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
1527 GrColorUnpackG(color)/255.f,
1528 GrColorUnpackB(color)/255.f,
1529 GrColorUnpackA(color)/255.f));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001530 GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001531}
1532
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001533void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001534 if (NULL == fCurrDrawState.fRenderTarget) {
1535 return;
1536 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001537
1538 this->flushRenderTarget(&GrIRect::EmptyIRect());
1539
reed@google.comac10a2d2010-12-22 21:39:39 +00001540 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001541 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001542 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001543 }
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001544 GR_GL(StencilMask(0xffffffff));
1545 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001546 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001547 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001548}
1549
bsalomon@google.com398109c2011-04-14 18:40:27 +00001550void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001551 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001552
1553 // this should only be called internally when we know we have a
1554 // stencil buffer.
1555 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001556#if 0
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001557 GrGLint stencilBitCount =
1558 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001559 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001560 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001561#else
1562 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001563 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001564 // turned into draws. Our contract on GrDrawTarget says that
1565 // changing the clip between stencil passes may or may not
1566 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001567 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001568#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001569 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001570 this->flushScissor(&rect);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001571 GR_GL(StencilMask(clipStencilMask));
1572 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001573 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001574 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001575}
1576
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001577void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001578 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001579}
1580
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001581bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1582 int left, int top, int width, int height,
1583 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001584 GrGLenum internalFormat; // we don't use this for glReadPixels
1585 GrGLenum format;
1586 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001587 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1588 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001589 }
1590 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1591 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1592 switch (tgt->getResolveType()) {
1593 case GrGLRenderTarget::kCantResolve_ResolveType:
1594 return false;
1595 case GrGLRenderTarget::kAutoResolves_ResolveType:
1596 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1597 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001598 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001599 break;
1600 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001601 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001602 // we don't track the state of the READ FBO ID.
1603 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
1604 break;
1605 default:
1606 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001607 }
1608
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001609 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001610
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001611 // the read rect is viewport-relative
1612 GrGLIRect readRect;
1613 readRect.setRelativeTo(glvp, left, top, width, height);
1614 GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001615 readRect.fWidth, readRect.fHeight,
bsalomon@google.com316f99232011-01-13 21:28:12 +00001616 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001617
1618 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1619 // API presents top-to-bottom
1620 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001621 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001622 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 void* tmp = rowStorage.get();
1624
1625 const int halfY = height >> 1;
1626 char* top = reinterpret_cast<char*>(buffer);
1627 char* bottom = top + (height - 1) * stride;
1628 for (int y = 0; y < halfY; y++) {
1629 memcpy(tmp, top, stride);
1630 memcpy(top, bottom, stride);
1631 memcpy(bottom, tmp, stride);
1632 top += stride;
1633 bottom -= stride;
1634 }
1635 }
1636 return true;
1637}
1638
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001639void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001640
1641 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1642
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001643 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001645 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 #if GR_COLLECT_STATS
1647 ++fStats.fRenderTargetChngCnt;
1648 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001649 #if GR_DEBUG
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001650 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1651 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001652 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 }
1654 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001655 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001656 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001657 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001658 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001659 vp.pushToGLViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001660 fHWBounds.fViewportRect = vp;
1661 }
1662 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001663 if (NULL == bound || !bound->isEmpty()) {
1664 rt->flagAsNeedingResolve(bound);
1665 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001666}
1667
twiz@google.com0f31ca72011-03-18 17:38:11 +00001668GrGLenum gPrimitiveType2GLMode[] = {
1669 GR_GL_TRIANGLES,
1670 GR_GL_TRIANGLE_STRIP,
1671 GR_GL_TRIANGLE_FAN,
1672 GR_GL_POINTS,
1673 GR_GL_LINES,
1674 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001675};
1676
bsalomon@google.comd302f142011-03-03 13:54:13 +00001677#define SWAP_PER_DRAW 0
1678
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001679#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001680 #if GR_MAC_BUILD
1681 #include <AGL/agl.h>
1682 #elif GR_WIN32_BUILD
1683 void SwapBuf() {
1684 DWORD procID = GetCurrentProcessId();
1685 HWND hwnd = GetTopWindow(GetDesktopWindow());
1686 while(hwnd) {
1687 DWORD wndProcID = 0;
1688 GetWindowThreadProcessId(hwnd, &wndProcID);
1689 if(wndProcID == procID) {
1690 SwapBuffers(GetDC(hwnd));
1691 }
1692 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1693 }
1694 }
1695 #endif
1696#endif
1697
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001698void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1699 uint32_t startVertex,
1700 uint32_t startIndex,
1701 uint32_t vertexCount,
1702 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1704
twiz@google.com0f31ca72011-03-18 17:38:11 +00001705 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001706
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001707 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1708 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1709
1710 // our setupGeometry better have adjusted this to zero since
1711 // DrawElements always draws from the begining of the arrays for idx 0.
1712 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001713
1714 GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
twiz@google.com0f31ca72011-03-18 17:38:11 +00001715 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001716#if SWAP_PER_DRAW
1717 glFlush();
1718 #if GR_MAC_BUILD
1719 aglSwapBuffers(aglGetCurrentContext());
1720 int set_a_break_pt_here = 9;
1721 aglSwapBuffers(aglGetCurrentContext());
1722 #elif GR_WIN32_BUILD
1723 SwapBuf();
1724 int set_a_break_pt_here = 9;
1725 SwapBuf();
1726 #endif
1727#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001728}
1729
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001730void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1731 uint32_t startVertex,
1732 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1734
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001735 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1736
1737 // our setupGeometry better have adjusted this to zero.
1738 // DrawElements doesn't take an offset so we always adjus the startVertex.
1739 GrAssert(0 == startVertex);
1740
1741 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1742 // account for startVertex in the DrawElements case. So we always
1743 // rely on setupGeometry to have accounted for startVertex.
reed@google.comac10a2d2010-12-22 21:39:39 +00001744 GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001745#if SWAP_PER_DRAW
1746 glFlush();
1747 #if GR_MAC_BUILD
1748 aglSwapBuffers(aglGetCurrentContext());
1749 int set_a_break_pt_here = 9;
1750 aglSwapBuffers(aglGetCurrentContext());
1751 #elif GR_WIN32_BUILD
1752 SwapBuf();
1753 int set_a_break_pt_here = 9;
1754 SwapBuf();
1755 #endif
1756#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001757}
1758
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001759void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001760
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001761 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 GrAssert(kNone_MSFBO != fMSFBOType);
1763 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001764 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 rt->renderFBOID()));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001766 GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001767 rt->textureFBOID()));
1768 #if GR_COLLECT_STATS
1769 ++fStats.fRenderTargetChngCnt;
1770 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001771 // make sure we go through flushRenderTarget() since we've modified
1772 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001773 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001774 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001775 const GrIRect dirtyRect = rt->getResolveRect();
1776 GrGLIRect r;
1777 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1778 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001779
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001780 if (kAppleES_MSFBO == fMSFBOType) {
1781 // Apple's extension uses the scissor as the blit bounds.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001782 GR_GL(Enable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001783 GR_GL(Scissor(r.fLeft, r.fBottom,
1784 r.fWidth, r.fHeight));
twiz@google.com59a190b2011-03-14 21:23:01 +00001785 GR_GL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001786 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001787 fHWBounds.fScissorEnabled = true;
1788 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001789 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001790 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001791 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1792 flushScissor(NULL);
1793 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001794 int right = r.fLeft + r.fWidth;
1795 int top = r.fBottom + r.fHeight;
1796 GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1797 r.fLeft, r.fBottom, right, top,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001798 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001799 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001800 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001801 }
1802}
1803
twiz@google.com0f31ca72011-03-18 17:38:11 +00001804static const GrGLenum grToGLStencilFunc[] = {
1805 GR_GL_ALWAYS, // kAlways_StencilFunc
1806 GR_GL_NEVER, // kNever_StencilFunc
1807 GR_GL_GREATER, // kGreater_StencilFunc
1808 GR_GL_GEQUAL, // kGEqual_StencilFunc
1809 GR_GL_LESS, // kLess_StencilFunc
1810 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1811 GR_GL_EQUAL, // kEqual_StencilFunc,
1812 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001813};
1814GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1815GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1816GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1817GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1818GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1819GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1820GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1821GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1822GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1823
twiz@google.com0f31ca72011-03-18 17:38:11 +00001824static const GrGLenum grToGLStencilOp[] = {
1825 GR_GL_KEEP, // kKeep_StencilOp
1826 GR_GL_REPLACE, // kReplace_StencilOp
1827 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1828 GR_GL_INCR, // kIncClamp_StencilOp
1829 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1830 GR_GL_DECR, // kDecClamp_StencilOp
1831 GR_GL_ZERO, // kZero_StencilOp
1832 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001833};
1834GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1835GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1836GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1837GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1838GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1839GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1840GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1841GR_STATIC_ASSERT(6 == kZero_StencilOp);
1842GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1843
reed@google.comac10a2d2010-12-22 21:39:39 +00001844void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001845 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001846
1847 // use stencil for clipping if clipping is enabled and the clip
1848 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001849 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001850 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001851 bool stencilChange = fHWStencilClip != stencilClip ||
1852 fHWDrawState.fStencilSettings != *settings ||
1853 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1854 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001855
1856 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001857
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1859 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001860
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861 if (settings->isDisabled()) {
1862 if (stencilClip) {
1863 settings = &gClipStencilSettings;
1864 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001865 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866
1867 if (settings->isDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001868 GR_GL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001870 GR_GL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001871 #if GR_DEBUG
1872 if (!fStencilWrapOpsSupport) {
1873 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1874 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1875 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1876 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1877 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1878 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1879 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1880 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1881 }
1882 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001883 int stencilBits = 0;
1884 GrStencilBuffer* stencilBuffer =
1885 fCurrDrawState.fRenderTarget->getStencilBuffer();
1886 if (NULL != stencilBuffer) {
1887 stencilBits = stencilBuffer->bits();
1888 }
1889 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001890 GrAssert(stencilBits ||
1891 (GrStencilSettings::gDisabled ==
1892 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001893 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1894 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001895
1896 unsigned int frontRef = settings->fFrontFuncRef;
1897 unsigned int frontMask = settings->fFrontFuncMask;
1898 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001899 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900
1901 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1902
1903 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1904 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1905 } else {
1906 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1907
1908 ConvertStencilFuncAndMask(settings->fFrontFunc,
1909 stencilClip,
1910 clipStencilMask,
1911 userStencilMask,
1912 &frontRef,
1913 &frontMask);
1914 frontWriteMask &= userStencilMask;
1915 }
1916 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001917 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001918 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001919 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001920 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001921 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001923 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001924 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001925 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001926
1927 unsigned int backRef = settings->fBackFuncRef;
1928 unsigned int backMask = settings->fBackFuncMask;
1929 unsigned int backWriteMask = settings->fBackWriteMask;
1930
1931
1932 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1933 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1934 backFunc = grToGLStencilFunc[settings->fBackFunc];
1935 } else {
1936 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1937 ConvertStencilFuncAndMask(settings->fBackFunc,
1938 stencilClip,
1939 clipStencilMask,
1940 userStencilMask,
1941 &backRef,
1942 &backMask);
1943 backWriteMask &= userStencilMask;
1944 }
1945
twiz@google.com0f31ca72011-03-18 17:38:11 +00001946 GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask));
1947 GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1948 GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask));
1949 GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1950 GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001951 grToGLStencilOp[settings->fFrontPassOp],
1952 grToGLStencilOp[settings->fFrontPassOp]));
1953
twiz@google.com0f31ca72011-03-18 17:38:11 +00001954 GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001955 grToGLStencilOp[settings->fBackPassOp],
1956 grToGLStencilOp[settings->fBackPassOp]));
1957 } else {
1958 GR_GL(StencilFunc(frontFunc, frontRef, frontMask));
1959 GR_GL(StencilMask(frontWriteMask));
1960 GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
1961 grToGLStencilOp[settings->fFrontPassOp],
1962 grToGLStencilOp[settings->fFrontPassOp]));
1963 }
1964 }
1965 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001966 fHWStencilClip = stencilClip;
1967 }
1968}
1969
bsalomon@google.com0650e812011-04-08 18:07:53 +00001970bool GrGpuGL::useSmoothLines() {
1971 // there is a conflict between using smooth lines and our use of
1972 // premultiplied alpha. Smooth lines tweak the incoming alpha value
1973 // but not in a premul-alpha way. So we only use them when our alpha
1974 // is 0xff.
1975
1976 // TODO: write a smarter line frag shader.
1977
1978 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
1979 canDisableBlend();
1980}
1981
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001982void GrGpuGL::flushAAState(GrPrimitiveType type) {
1983 if (GR_GL_SUPPORT_DESKTOP) {
1984 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1985 // smooth lines.
1986
1987 // we prefer smooth lines over multisampled lines
1988 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001989 if (GrIsPrimTypeLines(type)) {
1990 bool smooth = useSmoothLines();
1991 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001992 GR_GL(Enable(GR_GL_LINE_SMOOTH));
1993 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001994 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001995 GR_GL(Disable(GR_GL_LINE_SMOOTH));
1996 fHWAAState.fSmoothLineEnabled = false;
1997 }
1998 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1999 fHWAAState.fMSAAEnabled) {
2000 GR_GL(Disable(GR_GL_MULTISAMPLE));
2001 fHWAAState.fMSAAEnabled = false;
2002 }
2003 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
2004 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
2005 fHWAAState.fMSAAEnabled) {
2006 if (fHWAAState.fMSAAEnabled) {
2007 GR_GL(Disable(GR_GL_MULTISAMPLE));
2008 fHWAAState.fMSAAEnabled = false;
2009 } else {
2010 GR_GL(Enable(GR_GL_MULTISAMPLE));
2011 fHWAAState.fMSAAEnabled = true;
2012 }
2013 }
2014 }
2015}
2016
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002017void GrGpuGL::flushBlend(GrPrimitiveType type,
2018 GrBlendCoeff srcCoeff,
2019 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002020 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
2021 if (fHWBlendDisabled) {
2022 GR_GL(Enable(GR_GL_BLEND));
2023 fHWBlendDisabled = false;
2024 }
2025 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
2026 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
2027 GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2028 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
2029 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
2030 fHWDrawState.fDstBlend = kISA_BlendCoeff;
2031 }
2032 } else {
2033 bool blendOff = canDisableBlend();
2034 if (fHWBlendDisabled != blendOff) {
2035 if (blendOff) {
2036 GR_GL(Disable(GR_GL_BLEND));
2037 } else {
2038 GR_GL(Enable(GR_GL_BLEND));
2039 }
2040 fHWBlendDisabled = blendOff;
2041 }
2042 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002043 if (fHWDrawState.fSrcBlend != srcCoeff ||
2044 fHWDrawState.fDstBlend != dstCoeff) {
2045 GR_GL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2046 gXfermodeCoeff2Blend[dstCoeff]));
2047 fHWDrawState.fSrcBlend = srcCoeff;
2048 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002049 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002050 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2051 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002052 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2053
2054 float c[] = {
2055 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2056 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2057 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2058 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2059 };
2060 GR_GL(BlendColor(c[0], c[1], c[2], c[3]));
2061 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2062 }
2063 }
2064 }
2065}
2066
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002067static unsigned grToGLFilter(GrSamplerState::Filter filter) {
2068 switch (filter) {
2069 case GrSamplerState::kBilinear_Filter:
2070 case GrSamplerState::k4x4Downsample_Filter:
2071 return GR_GL_LINEAR;
2072 case GrSamplerState::kNearest_Filter:
2073 case GrSamplerState::kConvolution_Filter:
2074 return GR_GL_NEAREST;
2075 default:
2076 GrAssert(!"Unknown filter type");
2077 return GR_GL_LINEAR;
2078 }
2079}
2080
bsalomon@google.comffca4002011-02-22 20:34:01 +00002081bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002082
2083 // GrGpu::setupClipAndFlushState should have already checked this
2084 // and bailed if not true.
2085 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002086
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002087 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002088 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002089 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002090 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002091
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002092 // true for now, but maybe not with GrEffect.
2093 GrAssert(NULL != nextTexture);
2094 // if we created a rt/tex and rendered to it without using a
2095 // texture and now we're texuring from the rt it will still be
2096 // the last bound texture, but it needs resolving. So keep this
2097 // out of the "last != next" check.
2098 GrGLRenderTarget* texRT =
2099 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2100 if (NULL != texRT) {
2101 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002102 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002103
2104 if (fHWDrawState.fTextures[s] != nextTexture) {
2105 setTextureUnit(s);
2106 GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
2107 #if GR_COLLECT_STATS
2108 ++fStats.fTextureChngCnt;
2109 #endif
2110 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2111 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002112 // The texture matrix has to compensate for texture width/height
2113 // and NPOT-embedded-in-POT
2114 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002115 }
2116
2117 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2118 const GrGLTexture::TexParams& oldTexParams =
2119 nextTexture->getTexParams();
2120 GrGLTexture::TexParams newTexParams;
2121
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002122 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002123
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002124 newTexParams.fWrapS =
2125 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()];
2126 newTexParams.fWrapT =
2127 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()];
2128
2129 if (newTexParams.fFilter != oldTexParams.fFilter) {
2130 setTextureUnit(s);
2131 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2132 GR_GL_TEXTURE_MAG_FILTER,
2133 newTexParams.fFilter));
2134 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2135 GR_GL_TEXTURE_MIN_FILTER,
2136 newTexParams.fFilter));
2137 }
2138 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2139 setTextureUnit(s);
2140 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2141 GR_GL_TEXTURE_WRAP_S,
2142 newTexParams.fWrapS));
2143 }
2144 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2145 setTextureUnit(s);
2146 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2147 GR_GL_TEXTURE_WRAP_T,
2148 newTexParams.fWrapT));
2149 }
2150 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002151 }
2152 }
2153
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002154 GrIRect* rect = NULL;
2155 GrIRect clipBounds;
2156 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2157 fClip.hasConservativeBounds()) {
2158 fClip.getConservativeBounds().roundOut(&clipBounds);
2159 rect = &clipBounds;
2160 }
2161 this->flushRenderTarget(rect);
2162 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002163
reed@google.comac10a2d2010-12-22 21:39:39 +00002164 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2165 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2166 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002167 GR_GL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002168 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002169 GR_GL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002170 }
2171 }
2172
bsalomon@google.comd302f142011-03-03 13:54:13 +00002173 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2174 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002175 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002176 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002177 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002178 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002179 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002180 }
2181 GR_GL(ColorMask(mask, mask, mask, mask));
2182 }
2183
bsalomon@google.comd302f142011-03-03 13:54:13 +00002184 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2185 switch (fCurrDrawState.fDrawFace) {
2186 case kCCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002187 GR_GL(Enable(GR_GL_CULL_FACE));
2188 GR_GL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002189 break;
2190 case kCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002191 GR_GL(Enable(GR_GL_CULL_FACE));
2192 GR_GL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002193 break;
2194 case kBoth_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002195 GR_GL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002196 break;
2197 default:
2198 GrCrash("Unknown draw face.");
2199 }
2200 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2201 }
2202
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002203#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002204 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002205 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002206 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002207 NULL == fCurrDrawState.fRenderTarget ||
2208 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002209 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002210 fCurrDrawState.fRenderTarget);
2211 }
2212#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002213
reed@google.comac10a2d2010-12-22 21:39:39 +00002214 flushStencil();
2215
bsalomon@google.comd302f142011-03-03 13:54:13 +00002216 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002217 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002218 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002219}
2220
2221void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002222 if (fHWGeometryState.fVertexBuffer != buffer) {
2223 fHWGeometryState.fArrayPtrsDirty = true;
2224 fHWGeometryState.fVertexBuffer = buffer;
2225 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002226}
2227
2228void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002229 if (fHWGeometryState.fVertexBuffer == buffer) {
2230 // deleting bound buffer does implied bind to 0
2231 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002232 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002233 }
2234}
2235
2236void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002237 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002238}
2239
2240void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002241 if (fHWGeometryState.fIndexBuffer == buffer) {
2242 // deleting bound buffer does implied bind to 0
2243 fHWGeometryState.fIndexBuffer = NULL;
2244 }
2245}
2246
reed@google.comac10a2d2010-12-22 21:39:39 +00002247void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2248 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002249 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002250 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 }
2252 if (fHWDrawState.fRenderTarget == renderTarget) {
2253 fHWDrawState.fRenderTarget = NULL;
2254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002255}
2256
2257void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002258 for (int s = 0; s < kNumStages; ++s) {
2259 if (fCurrDrawState.fTextures[s] == texture) {
2260 fCurrDrawState.fTextures[s] = NULL;
2261 }
2262 if (fHWDrawState.fTextures[s] == texture) {
2263 // deleting bound texture does implied bind to 0
2264 fHWDrawState.fTextures[s] = NULL;
2265 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002266 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002267}
2268
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002269bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002270 GrGLenum* internalFormat,
2271 GrGLenum* format,
2272 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002273 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002274 case kRGBA_8888_GrPixelConfig:
2275 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002276 *format = GR_GL_32BPP_COLOR_FORMAT;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002277 if (GR_GL_SUPPORT_ES) {
2278 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2279 // format for a BGRA is BGRA not RGBA (as on desktop)
2280 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2281 } else {
2282 *internalFormat = GR_GL_RGBA;
2283 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002284 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002285 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002286 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002287 *format = GR_GL_RGB;
2288 *internalFormat = GR_GL_RGB;
2289 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002291 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002292 *format = GR_GL_RGBA;
2293 *internalFormat = GR_GL_RGBA;
2294 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002295 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002296 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002297 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002298 *format = GR_GL_PALETTE8_RGBA8;
2299 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002300 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002301 } else {
2302 return false;
2303 }
2304 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002305 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002306 *format = GR_GL_ALPHA;
2307 *internalFormat = GR_GL_ALPHA;
2308 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002309 break;
2310 default:
2311 return false;
2312 }
2313 return true;
2314}
2315
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002316void GrGpuGL::setTextureUnit(int unit) {
2317 GrAssert(unit >= 0 && unit < kNumStages);
2318 if (fActiveTextureUnitIdx != unit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002319 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002320 fActiveTextureUnitIdx = unit;
2321 }
2322}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002323
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002324void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002325 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
2326 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002327 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2328 }
2329}
2330
reed@google.comac10a2d2010-12-22 21:39:39 +00002331/* On ES the internalFormat and format must match for TexImage and we use
2332 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2333 decide the internalFormat. However, on ES internalFormat for
2334 RenderBufferStorage* has to be a specific format (not a base format like
2335 GL_RGBA).
2336 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002337bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002338 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002339 case kRGBA_8888_GrPixelConfig:
2340 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002341 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002342 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002343 return true;
2344 } else {
2345 return false;
2346 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002347 case kRGB_565_GrPixelConfig:
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002348 GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it
2349 // with FBO extension desktop GL has
2350 // no such internal format
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002351 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002352 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002353 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002354 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002355 return true;
2356 default:
2357 return false;
2358 }
2359}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002360
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002361void GrGpuGL::resetDirtyFlags() {
2362 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2363}
2364
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002365void GrGpuGL::setBuffers(bool indexed,
2366 int* extraVertexOffset,
2367 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002368
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002369 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002370
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002371 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2372
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002373 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002374 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002375 case kBuffer_GeometrySrcType:
2376 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002377 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002378 break;
2379 case kArray_GeometrySrcType:
2380 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002381 this->finalizeReservedVertices();
2382 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2383 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002384 break;
2385 default:
2386 vbuf = NULL; // suppress warning
2387 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002388 }
2389
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002390 GrAssert(NULL != vbuf);
2391 GrAssert(!vbuf->isLocked());
2392 if (fHWGeometryState.fVertexBuffer != vbuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002393 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002394 fHWGeometryState.fArrayPtrsDirty = true;
2395 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002396 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002397
2398 if (indexed) {
2399 GrAssert(NULL != extraIndexOffset);
2400
2401 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002402 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002403 case kBuffer_GeometrySrcType:
2404 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002405 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002406 break;
2407 case kArray_GeometrySrcType:
2408 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002409 this->finalizeReservedIndices();
2410 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2411 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002412 break;
2413 default:
2414 ibuf = NULL; // suppress warning
2415 GrCrash("Unknown geometry src type!");
2416 }
2417
2418 GrAssert(NULL != ibuf);
2419 GrAssert(!ibuf->isLocked());
2420 if (fHWGeometryState.fIndexBuffer != ibuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002421 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002422 fHWGeometryState.fIndexBuffer = ibuf;
2423 }
2424 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002425}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002426
2427int GrGpuGL::getMaxEdges() const {
2428 // FIXME: This is a pessimistic estimate based on how many other things
2429 // want to add uniforms. This should be centralized somewhere.
2430 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2431}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002432