blob: 936ada33bc054117f062b48bb21c73dd3d55a48e [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.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
19
bsalomon@google.com316f99232011-01-13 21:28:12 +000020// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000021// mucking with the state of any of the stages.
bsalomon@google.com316f99232011-01-13 21:28:12 +000022static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
twiz@google.com0f31ca72011-03-18 17:38:11 +000026static const GrGLenum gXfermodeCoeff2Blend[] = {
27 GR_GL_ZERO,
28 GR_GL_ONE,
29 GR_GL_SRC_COLOR,
30 GR_GL_ONE_MINUS_SRC_COLOR,
31 GR_GL_DST_COLOR,
32 GR_GL_ONE_MINUS_DST_COLOR,
33 GR_GL_SRC_ALPHA,
34 GR_GL_ONE_MINUS_SRC_ALPHA,
35 GR_GL_DST_ALPHA,
36 GR_GL_ONE_MINUS_DST_ALPHA,
37 GR_GL_CONSTANT_COLOR,
38 GR_GL_ONE_MINUS_CONSTANT_COLOR,
39 GR_GL_CONSTANT_ALPHA,
40 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000041
42 // extended blend coeffs
43 GR_GL_SRC1_COLOR,
44 GR_GL_ONE_MINUS_SRC1_COLOR,
45 GR_GL_SRC1_ALPHA,
46 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000047};
48
bsalomon@google.com271cffc2011-05-20 14:13:56 +000049bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000050 static const bool gCoeffReferencesBlendConst[] = {
51 false,
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 true,
62 true,
63 true,
64 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000065
66 // extended blend coeffs
67 false,
68 false,
69 false,
70 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000071 };
72 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000073 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
74
75 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
76 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
77 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
78 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
79 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
80 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
81 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
82 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
83 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
84 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
85 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
86 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
87 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
88 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
89
90 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
91 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
92 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
93 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
94
95 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
96 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000097}
98
reed@google.comac10a2d2010-12-22 21:39:39 +000099///////////////////////////////////////////////////////////////////////////////
100
bsalomon@google.comd302f142011-03-03 13:54:13 +0000101void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
102 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000103 GrMatrix* matrix) {
104 GrAssert(NULL != texture);
105 GrAssert(NULL != matrix);
106 if (GR_Scalar1 != texture->contentScaleX() ||
107 GR_Scalar1 != texture->contentScaleY()) {
108 if (GrSamplerState::kRadial_SampleMode == mode) {
109 GrMatrix scale;
110 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
111 matrix->postConcat(scale);
112 } else if (GrSamplerState::kNormal_SampleMode == mode) {
113 GrMatrix scale;
114 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
115 matrix->postConcat(scale);
116 } else {
117 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
118 }
119 }
120 GrGLTexture::Orientation orientation = texture->orientation();
121 if (GrGLTexture::kBottomUp_Orientation == orientation) {
122 GrMatrix invY;
123 invY.setAll(GR_Scalar1, 0, 0,
124 0, -GR_Scalar1, GR_Scalar1,
125 0, 0, GrMatrix::I()[8]);
126 matrix->postConcat(invY);
127 } else {
128 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
129 }
130}
131
bsalomon@google.comd302f142011-03-03 13:54:13 +0000132bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000133 const GrSamplerState& sampler) {
134 GrAssert(NULL != texture);
135 if (!sampler.getMatrix().isIdentity()) {
136 return false;
137 }
138 if (GR_Scalar1 != texture->contentScaleX() ||
139 GR_Scalar1 != texture->contentScaleY()) {
140 return false;
141 }
142 GrGLTexture::Orientation orientation = texture->orientation();
143 if (GrGLTexture::kBottomUp_Orientation == orientation) {
144 return false;
145 } else {
146 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
147 }
148 return true;
149}
150
151///////////////////////////////////////////////////////////////////////////////
152
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000153static bool gPrintStartupSpew;
154
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000155static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000156
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000157 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000158
twiz@google.com0f31ca72011-03-18 17:38:11 +0000159 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000160 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
161 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000162 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000163 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
164 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000165 // some implementations require texture to be mip-map complete before
166 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000167 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
168 GR_GL_TEXTURE_MIN_FILTER,
169 GR_GL_NEAREST));
170 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
171 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
172 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
173 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
174 GR_GL_COLOR_ATTACHMENT0,
175 GR_GL_TEXTURE_2D, testRTTex, 0));
176 GrGLenum status = GR_GL_CALL(gl, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
177 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
178 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000179
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000180 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000181}
182
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000183static bool probe_for_npot_render_target_support(const GrGLInterface* gl,
184 bool hasNPOTTextureSupport) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000185
186 /* Experimentation has found that some GLs that support NPOT textures
187 do not support FBOs with a NPOT texture. They report "unsupported" FBO
188 status. I don't know how to explicitly query for this. Do an
189 experiment. Note they may support NPOT with a renderbuffer but not a
190 texture. Presumably, the implementation bloats the renderbuffer
191 internally to the next POT.
192 */
193 if (hasNPOTTextureSupport) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000194 return fbo_test(gl, 200, 200);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000195 }
196 return false;
197}
198
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000199static int probe_for_min_render_target_height(const GrGLInterface* gl,
200 bool hasNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000201 int maxRenderTargetSize) {
202 /* The iPhone 4 has a restriction that for an FBO with texture color
203 attachment with height <= 8 then the width must be <= height. Here
204 we look for such a limitation.
205 */
206 if (gPrintStartupSpew) {
207 GrPrintf("Small height FBO texture experiments\n");
208 }
209 int minRenderTargetHeight = GR_INVAL_GLINT;
210 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
211 GrGLuint w = maxRenderTargetSize;
212 GrGLuint h = i;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000213 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000214 if (gPrintStartupSpew) {
215 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
216 }
217 minRenderTargetHeight = i;
218 break;
219 } else {
220 if (gPrintStartupSpew) {
221 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
222 }
223 }
224 }
225 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
226
227 return minRenderTargetHeight;
228}
229
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000230static int probe_for_min_render_target_width(const GrGLInterface* gl,
231 bool hasNPOTRenderTargetSupport,
232 int maxRenderTargetSize) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000233
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;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000241 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000242 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.com0b77d682011-08-19 13:28:54 +0000259GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding)
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000260 : fStencilFormats(8) {
261
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000262 gl->ref();
263 fGL = gl;
264 fGLBinding = glBinding;
265 switch (glBinding) {
266 case kDesktop_GrGLBinding:
267 GrAssert(gl->supportsDesktop());
268 break;
269 case kES1_GrGLBinding:
270 GrAssert(gl->supportsES1());
271 break;
272 case kES2_GrGLBinding:
273 GrAssert(gl->supportsES2());
274 break;
275 default:
276 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000277 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000278
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000279 GrGLClearErr(fGL);
280
281 if (gPrintStartupSpew) {
282 const GrGLubyte* vendor = GL_CALL(GetString(GR_GL_VENDOR));
283 const GrGLubyte* renderer = GL_CALL(GetString(GR_GL_RENDERER));
284 const GrGLubyte* version = GL_CALL(GetString(GR_GL_VERSION));
285 const GrGLubyte* ext = GL_CALL(GetString(GR_GL_EXTENSIONS));
286 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
287 this);
288 GrPrintf("------ VENDOR %s\n", vendor);
289 GrPrintf("------ RENDERER %s\n", renderer);
290 GrPrintf("------ VERSION %s\n", version);
291 GrPrintf("------ EXTENSIONS\n %s \n", ext);
292 }
293
294 fGLVersion = gl_version_as_float(gl);
295 fExtensionString = (const char*) GL_CALL(GetString(GR_GL_EXTENSIONS));
reed@google.comac10a2d2010-12-22 21:39:39 +0000296
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000297 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000298
twiz@google.com0f31ca72011-03-18 17:38:11 +0000299 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000300 // check FS and fixed-function texture unit limits
301 // we only use textures in the fragment stage currently.
302 // checks are > to make sure we have a spare unit.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000303 if (kES1_GrGLBinding != this->glBinding()) {
304 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000305 GrAssert(maxTextureUnits > kNumStages);
306 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000307 if (kES2_GrGLBinding != this->glBinding()) {
308 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000309 GrAssert(maxTextureUnits > kNumStages);
310 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000311 if (kES2_GrGLBinding == this->glBinding()) {
312 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000313 &fMaxFragmentUniformVectors);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000314 } else if (kDesktop_GrGLBinding != this->glBinding()) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000315 GrGLint max;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000316 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000317 fMaxFragmentUniformVectors = max / 4;
318 } else {
319 fMaxFragmentUniformVectors = 16;
320 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000321
reed@google.comac10a2d2010-12-22 21:39:39 +0000322 ////////////////////////////////////////////////////////////////////////////
323 // Check for supported features.
324
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000325 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000326
twiz@google.com0f31ca72011-03-18 17:38:11 +0000327 GrGLint numFormats;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000328 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000329 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000330 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000331 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000332 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 f8bitPaletteSupport = true;
334 break;
335 }
336 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000337
338 if (gPrintStartupSpew) {
339 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
340 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000341
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000342 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
343 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
344 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
345 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
347 memset(fAASamples, 0, sizeof(fAASamples));
348 fMSFBOType = kNone_MSFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000349 if (kDesktop_GrGLBinding != this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000350 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000351 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000352 // and fbo_blit extensions.
353 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000354 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000355 fMSFBOType = kAppleES_MSFBO;
356 }
357 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000358 if ((fGLVersion >= 3.f) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000359 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000360 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
361 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000362 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000363 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000365 if (gPrintStartupSpew) {
366 switch (fMSFBOType) {
367 case kNone_MSFBO:
368 GrPrintf("MSAA Support: NONE\n");
369 break;
370 case kDesktopARB_MSFBO:
371 GrPrintf("MSAA Support: DESKTOP ARB.\n");
372 break;
373 case kDesktopEXT_MSFBO:
374 GrPrintf("MSAA Support: DESKTOP EXT.\n");
375 break;
376 case kAppleES_MSFBO:
377 GrPrintf("MSAA Support: APPLE ES.\n");
378 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000379 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000380 }
381
382 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000383 GrGLint maxSamples;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000384 GR_GL_GetIntegerv(gl, GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000385 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000386 fAASamples[kNone_GrAALevel] = 0;
387 fAASamples[kLow_GrAALevel] = GrMax(2,
388 GrFixedFloorToInt((GR_FixedHalf) *
389 maxSamples));
390 fAASamples[kMed_GrAALevel] = GrMax(2,
391 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
392 maxSamples));
393 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000394 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000395 if (gPrintStartupSpew) {
396 GrPrintf("\tMax Samples: %d\n", maxSamples);
397 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000398 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000399 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000400
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000401 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000402 fHasStencilWrap = (fGLVersion >= 1.4f) ||
403 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000404 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000405 fHasStencilWrap = (fGLVersion >= 2.0f) || this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000406 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000407 if (gPrintStartupSpew) {
408 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
409 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000410
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000411 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000412 // we could also look for GL_ATI_separate_stencil extension or
413 // GL_EXT_stencil_two_side but they use different function signatures
414 // than GL2.0+ (and than each other).
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000415 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000416 // supported on GL 1.4 and higher or by extension
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000417 fStencilWrapOpsSupport = (fGLVersion >= 1.4f) ||
418 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000419 } else {
420 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
421 // an ES1 extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000422 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000423 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000424 fStencilWrapOpsSupport = (fGLVersion >= 2.f) ||
425 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000426 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000427 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000428 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
429 (fTwoSidedStencilSupport ? "YES" : "NO"),
430 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000431 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000432
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000433 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000434 fRGBA8Renderbuffer = true;
435 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000436 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000437 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000438 if (gPrintStartupSpew) {
439 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
440 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000441
442
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000443 if (kDesktop_GrGLBinding != this->glBinding()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000444 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000445 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000446 }
447 }
448
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000449 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000450 fBufferLockSupport = true; // we require VBO support and the desktop VBO
451 // extension includes glMapBuffer.
452 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000453 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000454 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000455
reed@google.comeeeb5a02010-12-23 15:12:59 +0000456 if (gPrintStartupSpew) {
457 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
458 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000459
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000460 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000461 if (fGLVersion >= 2.f ||
462 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000463 fNPOTTextureTileSupport = true;
464 fNPOTTextureSupport = true;
465 } else {
466 fNPOTTextureTileSupport = false;
467 fNPOTTextureSupport = false;
468 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000469 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000470 if (fGLVersion >= 2.f) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000471 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000472 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000473 } else {
474 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000475 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000476 fNPOTTextureTileSupport = false;
477 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000478 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000479
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000480 fAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com205d4602011-04-25 12:43:45 +0000481
reed@google.comac10a2d2010-12-22 21:39:39 +0000482 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000483 // Experiments to determine limitations that can't be queried.
484 // TODO: Make these a preprocess that generate some compile time constants.
485 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000486
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000487 int expectNPOTTargets = gl->fNPOTRenderTargetSupport;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000488 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000489 fNPOTRenderTargetSupport =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000490 probe_for_npot_render_target_support(gl, fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000491 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000492 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
493 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000494 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000495
bsalomon@google.com0748f212011-02-01 22:56:16 +0000496 if (gPrintStartupSpew) {
497 if (fNPOTTextureSupport) {
498 GrPrintf("NPOT textures supported\n");
499 if (fNPOTTextureTileSupport) {
500 GrPrintf("NPOT texture tiling supported\n");
501 } else {
502 GrPrintf("NPOT texture tiling NOT supported\n");
503 }
504 if (fNPOTRenderTargetSupport) {
505 GrPrintf("NPOT render targets supported\n");
506 } else {
507 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000508 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000510 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000511 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 }
513
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000514 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
515 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000516 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000517 // attachment, hence this min:
518 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000519
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000520 fMinRenderTargetHeight = gl->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000521 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000522 fMinRenderTargetHeight =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000523 probe_for_min_render_target_height(gl,fNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000524 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000525 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000526
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000527 fMinRenderTargetWidth = gl->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000528 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000529 fMinRenderTargetWidth =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000530 probe_for_min_render_target_width(gl, fNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000531 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000532 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000533
bsalomon@google.comfe676522011-06-17 18:12:21 +0000534 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000535}
536
537GrGpuGL::~GrGpuGL() {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000538 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000539}
540
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000541void GrGpuGL::resetContext() {
542 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000543 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000544 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000545
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000546 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000547 GL_CALL(Disable(GR_GL_DEPTH_TEST));
548 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000549
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000550 GL_CALL(Disable(GR_GL_CULL_FACE));
551 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000552 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000553
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000554 GL_CALL(Disable(GR_GL_DITHER));
555 if (kDesktop_GrGLBinding == this->glBinding()) {
556 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
557 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
558 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000559 fHWAAState.fMSAAEnabled = false;
560 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000561 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000562
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000563 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000564 fHWDrawState.fFlagBits = 0;
565
reed@google.comac10a2d2010-12-22 21:39:39 +0000566 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000567 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000568
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000569 // invalid
570 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
reed@google.comac10a2d2010-12-22 21:39:39 +0000572 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000573 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
574 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000575
576 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000578
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000580
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000581 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000582
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000583 for (int s = 0; s < kNumStages; ++s) {
584 fHWDrawState.fTextures[s] = NULL;
585 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
586 -GR_ScalarMax,
587 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000588 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000589 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000590 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000591
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000592 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000594 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000595 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000596
bsalomon@google.comd302f142011-03-03 13:54:13 +0000597 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000598 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000599 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000600
601 fHWGeometryState.fIndexBuffer = NULL;
602 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000603
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000604 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000606 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000607 fHWDrawState.fRenderTarget = NULL;
608}
609
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000610GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
611
612 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
613 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
614 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
615 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
616
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000617 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000618 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000619
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000620 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000621 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000622 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000623#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
624 if (desc.fSampleCnt) {
625#else
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000626 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000627#endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000628 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000629 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000630 } else {
631 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000632 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000633 }
634 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000635 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000636 }
637 // we don't know what the RB ids are without glGets and we don't care
638 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000639 rtDesc.fMSColorRenderbufferID = 0;
640#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
641 rtDesc.fSampleCnt = desc.fSampleCnt;
642#else
643 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
644 // just guess, this code path is only compiled in WK and we aren't
645 // using MSAA anyway. This will be stripped out soon when WK sets
646 // the fSampleCnt in GrPlatformSurfaceDesc.
647 rtDesc.fSampleCnt = 4;
648 } else {
649 rtDesc.fSampleCnt = 0;
650 }
651#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000652 if (desc.fStencilBits) {
653 GrGLStencilBuffer::Format format;
654 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
655 format.fPacked = false;
656 format.fStencilBits = desc.fStencilBits;
657 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000658 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
659 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000660 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000661 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000662 }
663
664 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000665 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000666 GrGLenum dontCare;
667 if (!canBeTexture(desc.fConfig, &dontCare,
668 &texDesc.fUploadFormat,
669 &texDesc.fUploadType)) {
670 return NULL;
671 }
672
673 GrGLTexture::TexParams params;
674
675 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
676 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
677
bsalomon@google.com90405932011-06-17 15:56:55 +0000678 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000679 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000680 texDesc.fTextureID = desc.fPlatformTexture;
681 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
682 texDesc.fOwnsID = false;
683
684 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000685 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000686 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000687 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000688 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000689 } else {
690 return new GrGLTexture(this, texDesc, params);
691 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000692 } else {
693 GrGLIRect viewport;
694 viewport.fLeft = 0;
695 viewport.fBottom = 0;
696 viewport.fWidth = desc.fWidth;
697 viewport.fHeight = desc.fHeight;
698
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000699 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000700 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000701 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000702 }
703}
704
bsalomon@google.com5782d712011-01-21 21:03:59 +0000705///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000706
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000707static const GrGLuint kUnknownBitCount = ~0;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000708
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000709void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000710
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000711 // Build up list of legal stencil formats (though perhaps not supported on
712 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000713
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000714 // these consts are in order of most preferred to least preferred
715 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000716 static const GrGLStencilBuffer::Format
717 // internal Format stencil bits total bits packed?
718 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
719 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
720 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
721 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
722 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
723 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000724
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000725 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000726 bool supportsPackedDS = fGLVersion >= 3.0f ||
727 this->hasExtension("GL_EXT_packed_depth_stencil") ||
728 this->hasExtension("GL_ARB_framebuffer_object");
729
730 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
731 // require FBO support we can expect these are legal formats and don't
732 // check. These also all support the unsized GL_STENCIL_INDEX.
733 fStencilFormats.push_back() = gS8;
734 fStencilFormats.push_back() = gS16;
735 if (supportsPackedDS) {
736 fStencilFormats.push_back() = gD24S8;
737 }
738 fStencilFormats.push_back() = gS4;
739 if (supportsPackedDS) {
740 fStencilFormats.push_back() = gDS;
741 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000742 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000743 // ES2 has STENCIL_INDEX8 without extensions.
744 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
745 // introduces tokens for S1 thu S8 but there are separate extensions
746 // that make them legal (GL_OES_stencil1, ...).
747 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
748 // ES doesn't support using the unsized formats.
749
750 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
751 fStencilFormats.push_back() = gS8;
752 }
753 //fStencilFormats.push_back() = gS16;
754 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
755 fStencilFormats.push_back() = gD24S8;
756 }
757 if (this->hasExtension("GL_OES_stencil4")) {
758 fStencilFormats.push_back() = gS4;
759 }
760 // we require some stencil format.
761 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000762 }
763}
764
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000765////////////////////////////////////////////////////////////////////////////////
766
767void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
768 GrGLenum internalFormat,
769 const void* data,
770 size_t rowBytes) {
771 // we assume the texture is bound;
772 if (!rowBytes) {
773 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
774 }
775
776 // in case we need a temporary, trimmed copy of the src pixels
777 SkAutoSMalloc<128 * 128> tempStorage;
778
779 /*
780 * check whether to allocate a temporary buffer for flipping y or
781 * because our data has extra bytes past each row. If so, we need
782 * to trim those off here, since GL ES doesn't let us specify
783 * GL_UNPACK_ROW_LENGTH.
784 */
785 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000786 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000787 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000788 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
789 rowBytes / desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000790 }
791 } else {
792 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
793 if (data && (trimRowBytes < rowBytes || flipY)) {
794 // copy the data into our new storage, skipping the trailing bytes
795 size_t trimSize = desc.fContentHeight * trimRowBytes;
796 const char* src = (const char*)data;
797 if (flipY) {
798 src += (desc.fContentHeight - 1) * rowBytes;
799 }
800 char* dst = (char*)tempStorage.realloc(trimSize);
801 for (int y = 0; y < desc.fContentHeight; y++) {
802 memcpy(dst, src, trimRowBytes);
803 if (flipY) {
804 src -= rowBytes;
805 } else {
806 src += rowBytes;
807 }
808 dst += trimRowBytes;
809 }
810 // now point data to our trimmed version
811 data = tempStorage.get();
812 rowBytes = trimRowBytes;
813 }
814 }
815
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000816 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000817 if (kIndex_8_GrPixelConfig == desc.fFormat &&
818 supports8BitPalette()) {
819 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
820 GrAssert(desc.fContentWidth == desc.fAllocWidth);
821 GrAssert(desc.fContentHeight == desc.fAllocHeight);
822 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
823 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000824 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
825 desc.fAllocWidth, desc.fAllocHeight,
826 0, imageSize, data));
827 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000828 } else {
829 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
830 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000831 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
832 desc.fAllocWidth, desc.fAllocHeight,
833 0, desc.fUploadFormat, desc.fUploadType, NULL));
834 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
835 desc.fContentHeight, desc.fUploadFormat,
836 desc.fUploadType, data));
837 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000838
839 int extraW = desc.fAllocWidth - desc.fContentWidth;
840 int extraH = desc.fAllocHeight - desc.fContentHeight;
841 int maxTexels = extraW * extraH;
842 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
843 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
844
845 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
846
847 // rowBytes is actual stride between rows in data
848 // rowDataBytes is the actual amount of non-pad data in a row
849 // and the stride used for uploading extraH rows.
850 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
851 if (extraH) {
852 uint8_t* lastRowStart = (uint8_t*) data +
853 (desc.fContentHeight - 1) * rowBytes;
854 uint8_t* extraRowStart = (uint8_t*)texels.get();
855
856 for (int i = 0; i < extraH; ++i) {
857 memcpy(extraRowStart, lastRowStart, rowDataBytes);
858 extraRowStart += rowDataBytes;
859 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000860 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
861 desc.fContentHeight, desc.fContentWidth,
862 extraH, desc.fUploadFormat,
863 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000864 }
865 if (extraW) {
866 uint8_t* edgeTexel = (uint8_t*)data +
867 rowDataBytes - desc.fUploadByteCount;
868 uint8_t* extraTexel = (uint8_t*)texels.get();
869 for (int j = 0; j < desc.fContentHeight; ++j) {
870 for (int i = 0; i < extraW; ++i) {
871 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
872 extraTexel += desc.fUploadByteCount;
873 }
874 edgeTexel += rowBytes;
875 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000876 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
877 0, extraW, desc.fContentHeight,
878 desc.fUploadFormat, desc.fUploadType,
879 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000880 }
881 if (extraW && extraH) {
882 uint8_t* cornerTexel = (uint8_t*)data +
883 desc.fContentHeight * rowBytes -
884 desc.fUploadByteCount;
885 uint8_t* extraTexel = (uint8_t*)texels.get();
886 for (int i = 0; i < extraW*extraH; ++i) {
887 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
888 extraTexel += desc.fUploadByteCount;
889 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000890 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
891 desc.fContentHeight, extraW, extraH,
892 desc.fUploadFormat, desc.fUploadType,
893 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000894 }
895
896 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000897 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
898 desc.fAllocWidth, desc.fAllocHeight, 0,
899 desc.fUploadFormat, desc.fUploadType, data));
900 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000901 }
902 }
903}
904
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905bool GrGpuGL::createRenderTargetObjects(int width, int height,
906 GrGLuint texID,
907 GrGLRenderTarget::Desc* desc) {
908 desc->fMSColorRenderbufferID = 0;
909 desc->fRTFBOID = 0;
910 desc->fTexFBOID = 0;
911 desc->fOwnIDs = true;
912
913 GrGLenum status;
914 GrGLint err;
915
bsalomon@google.comab15d612011-08-09 12:57:56 +0000916 GrGLenum msColorFormat = 0; // suppress warning
917
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000918 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919 if (!desc->fTexFBOID) {
920 goto FAILED;
921 }
922
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000923
924 // If we are using multisampling we will create two FBOS. We render
925 // to one and then resolve to the texture bound to the other.
926 if (desc->fSampleCnt > 1 && kNone_MSFBO != fMSFBOType) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
928 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 if (!desc->fRTFBOID ||
930 !desc->fMSColorRenderbufferID ||
931 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
932 goto FAILED;
933 }
934 } else {
935 desc->fRTFBOID = desc->fTexFBOID;
936 }
937
938 if (desc->fRTFBOID != desc->fTexFBOID) {
939 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000940 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000941 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GR_GL_CALL_NOERRCHECK(this->glInterface(),
943 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
944 desc->fSampleCnt,
945 msColorFormat,
946 width, height));
947 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948 if (err != GR_GL_NO_ERROR) {
949 goto FAILED;
950 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000951 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
952 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000953 GR_GL_COLOR_ATTACHMENT0,
954 GR_GL_RENDERBUFFER,
955 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000956 GrGLenum status = GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000957 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
958 goto FAILED;
959 }
960 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000961 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000962
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000963 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
964 GR_GL_COLOR_ATTACHMENT0,
965 GR_GL_TEXTURE_2D,
966 texID, 0));
967 status = GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000968 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
969 goto FAILED;
970 }
971
972 return true;
973
974FAILED:
975 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000976 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000977 }
978 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000979 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000980 }
981 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000982 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000983 }
984 return false;
985}
986
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000987// good to set a break-point here to know when createTexture fails
988static GrTexture* return_null_texture() {
989// GrAssert(!"null texture");
990 return NULL;
991}
992
993#if GR_DEBUG
994static size_t as_size_t(int x) {
995 return x;
996}
997#endif
998
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000999GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001000 const void* srcData,
1001 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001002
1003#if GR_COLLECT_STATS
1004 ++fStats.fTextureCreateCnt;
1005#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001006
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001007 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001008 GR_GL_NEAREST,
1009 GR_GL_CLAMP_TO_EDGE,
1010 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001011 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001012
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001013 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001014 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001015 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001016
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001017 glTexDesc.fContentWidth = desc.fWidth;
1018 glTexDesc.fContentHeight = desc.fHeight;
1019 glTexDesc.fAllocWidth = desc.fWidth;
1020 glTexDesc.fAllocHeight = desc.fHeight;
1021 glTexDesc.fFormat = desc.fFormat;
1022 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001023
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001024 glRTDesc.fMSColorRenderbufferID = 0;
1025 glRTDesc.fRTFBOID = 0;
1026 glRTDesc.fTexFBOID = 0;
1027 glRTDesc.fOwnIDs = true;
1028 glRTDesc.fConfig = glTexDesc.fFormat;
1029
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001030 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001031 if (!canBeTexture(desc.fFormat,
1032 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001033 &glTexDesc.fUploadFormat,
1034 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001035 return return_null_texture();
1036 }
1037
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001038 // We keep GrRenderTargets in GL's normal orientation so that they
1039 // can be drawn to by the outside world without the client having
1040 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001041 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001042 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001043
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001045 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001046 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001047 GrPrintf("AA RT requested but not supported on this platform.");
1048 }
1049
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001050 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001051
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001053 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001054 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1055 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001056 }
1057
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001058 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001059 glTexDesc.fAllocWidth);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001060 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001061 glTexDesc.fAllocHeight);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001062 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1063 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001064 return return_null_texture();
1065 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001066 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001067 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1068 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1069 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1070 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001071 return return_null_texture();
1072 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001073 }
1074
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001075 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001076 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001077 return return_null_texture();
1078 }
1079
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001080 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001081 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1083 GR_GL_TEXTURE_MAG_FILTER,
1084 DEFAULT_PARAMS.fFilter));
1085 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1086 GR_GL_TEXTURE_MIN_FILTER,
1087 DEFAULT_PARAMS.fFilter));
1088 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1089 GR_GL_TEXTURE_WRAP_S,
1090 DEFAULT_PARAMS.fWrapS));
1091 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1092 GR_GL_TEXTURE_WRAP_T,
1093 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001094
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001095 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001096
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001098 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001099 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001100#if GR_COLLECT_STATS
1101 ++fStats.fRenderTargetCreateCnt;
1102#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1104 glTexDesc.fAllocHeight,
1105 glTexDesc.fTextureID,
1106 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 return return_null_texture();
1109 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1111 } else {
1112 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001113 }
1114#ifdef TRACE_TEXTURE_CREATION
1115 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1116 tex->fTextureID, width, height, tex->fUploadByteCount);
1117#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118 return tex;
1119}
1120
1121namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1123 GrGLuint rb,
1124 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 // we shouldn't ever know one size and not the other
1126 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1127 (kUnknownBitCount == format->fTotalBits));
1128 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001129 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1131 (GrGLint*)&format->fStencilBits);
1132 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001133 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1135 (GrGLint*)&format->fTotalBits);
1136 format->fTotalBits += format->fStencilBits;
1137 } else {
1138 format->fTotalBits = format->fStencilBits;
1139 }
1140 }
1141}
1142}
1143
1144bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1145 int width, int height) {
1146
1147 // All internally created RTs are also textures. We don't create
1148 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1149 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001150 GrAssert(width >= rt->allocatedWidth());
1151 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152
1153 int samples = rt->numSamples();
1154 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001155 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 if (!sbID) {
1157 return false;
1158 }
1159
1160 GrGLStencilBuffer* sb = NULL;
1161
1162 int stencilFmtCnt = fStencilFormats.count();
1163 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 // we start with the last stencil format that succeeded in hopes
1166 // that we won't go through this loop more than once after the
1167 // first (painful) stencil creation.
1168 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001169 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 // version on a GL that doesn't have an MSAA extension.
1171 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001172 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1173 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 GR_GL_RENDERBUFFER,
1175 samples,
1176 fStencilFormats[sIdx].fInternalFormat,
1177 width,
1178 height));
1179 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001180 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1181 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 fStencilFormats[sIdx].fInternalFormat,
1183 width, height));
1184 }
1185
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001186 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 if (err == GR_GL_NO_ERROR) {
1188 // After sized formats we attempt an unsized format and take whatever
1189 // sizes GL gives us. In that case we query for the size.
1190 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001192 sb = new GrGLStencilBuffer(this, sbID, width, height,
1193 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1195 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001196 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 return true;
1199 }
1200 sb->abandon(); // otherwise we lose sbID
1201 sb->unref();
1202 }
1203 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001205 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206}
1207
1208bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1209 GrRenderTarget* rt) {
1210 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1211
1212 GrGLuint fbo = glrt->renderFBOID();
1213
1214 if (NULL == sb) {
1215 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 GR_GL_STENCIL_ATTACHMENT,
1218 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 GR_GL_DEPTH_ATTACHMENT,
1221 GR_GL_RENDERBUFFER, 0));
1222#if GR_DEBUG
1223 GrGLenum status =
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001224 GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1226#endif
1227 }
1228 return true;
1229 } else {
1230 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1231 GrGLuint rb = glsb->renderbufferID();
1232
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1235 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 GR_GL_STENCIL_ATTACHMENT,
1237 GR_GL_RENDERBUFFER, rb));
1238 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001239 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 GR_GL_DEPTH_ATTACHMENT,
1241 GR_GL_RENDERBUFFER, rb));
1242 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001243 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 GR_GL_DEPTH_ATTACHMENT,
1245 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GrGLenum status = GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 GR_GL_STENCIL_ATTACHMENT,
1252 GR_GL_RENDERBUFFER, 0));
1253 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001254 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255 GR_GL_DEPTH_ATTACHMENT,
1256 GR_GL_RENDERBUFFER, 0));
1257 }
1258 return false;
1259 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001260 return true;
1261 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001263}
1264
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001265////////////////////////////////////////////////////////////////////////////////
1266
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001267GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001268 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001269 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001270 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001271 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001272 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001273 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001275 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1276 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1277 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1278 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1279 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 // deleting bound buffer does implicit bind to 0
1281 fHWGeometryState.fVertexBuffer = NULL;
1282 return NULL;
1283 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001284 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 size, dynamic);
1286 fHWGeometryState.fVertexBuffer = vertexBuffer;
1287 return vertexBuffer;
1288 }
1289 return NULL;
1290}
1291
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001292GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001293 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001294 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001296 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1297 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001299 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1300 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1301 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1302 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1303 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 // deleting bound buffer does implicit bind to 0
1305 fHWGeometryState.fIndexBuffer = NULL;
1306 return NULL;
1307 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001308 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 size, dynamic);
1310 fHWGeometryState.fIndexBuffer = indexBuffer;
1311 return indexBuffer;
1312 }
1313 return NULL;
1314}
1315
reed@google.comac10a2d2010-12-22 21:39:39 +00001316void GrGpuGL::flushScissor(const GrIRect* rect) {
1317 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001318 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001319 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001320
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001321 GrGLIRect scissor;
1322 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001323 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001324 rect->width(), rect->height());
1325 if (scissor.contains(vp)) {
1326 rect = NULL;
1327 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 }
1329
1330 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001332 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 fHWBounds.fScissorRect = scissor;
1334 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001336 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 fHWBounds.fScissorEnabled = true;
1338 }
1339 } else {
1340 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001341 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001342 fHWBounds.fScissorEnabled = false;
1343 }
1344 }
1345}
1346
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001347void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001348 if (NULL == fCurrDrawState.fRenderTarget) {
1349 return;
1350 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001351 GrIRect r;
1352 if (NULL != rect) {
1353 // flushScissor expects rect to be clipped to the target.
1354 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001355 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1356 fCurrDrawState.fRenderTarget->height());
1357 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001358 rect = &r;
1359 } else {
1360 return;
1361 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001362 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001363 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001364 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001365 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001366 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001367 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1368 GrColorUnpackG(color)/255.f,
1369 GrColorUnpackB(color)/255.f,
1370 GrColorUnpackA(color)/255.f));
1371 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001372}
1373
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001374void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001375 if (NULL == fCurrDrawState.fRenderTarget) {
1376 return;
1377 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001378
1379 this->flushRenderTarget(&GrIRect::EmptyIRect());
1380
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001382 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001383 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001384 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001385 GL_CALL(StencilMask(0xffffffff));
1386 GL_CALL(ClearStencil(0));
1387 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001388 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001389}
1390
bsalomon@google.com398109c2011-04-14 18:40:27 +00001391void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001392 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001393
1394 // this should only be called internally when we know we have a
1395 // stencil buffer.
1396 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001397#if 0
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001398 GrGLint stencilBitCount =
1399 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001400 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001401 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001402#else
1403 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001404 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001405 // turned into draws. Our contract on GrDrawTarget says that
1406 // changing the clip between stencil passes may or may not
1407 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001408 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001409#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001410 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001411 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001412 GL_CALL(StencilMask(clipStencilMask));
1413 GL_CALL(ClearStencil(0));
1414 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001415 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001416}
1417
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001418void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001419 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001420}
1421
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001422bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1423 int left, int top, int width, int height,
1424 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001425 GrGLenum internalFormat; // we don't use this for glReadPixels
1426 GrGLenum format;
1427 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001428 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1429 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001430 }
1431 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1432 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1433 switch (tgt->getResolveType()) {
1434 case GrGLRenderTarget::kCantResolve_ResolveType:
1435 return false;
1436 case GrGLRenderTarget::kAutoResolves_ResolveType:
1437 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1438 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001439 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001440 break;
1441 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001442 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001443 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001444 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1445 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001446 break;
1447 default:
1448 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001449 }
1450
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001451 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001452
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001453 // the read rect is viewport-relative
1454 GrGLIRect readRect;
1455 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001456 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1457 readRect.fWidth, readRect.fHeight,
1458 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001459
1460 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1461 // API presents top-to-bottom
1462 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001463 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001464 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 void* tmp = rowStorage.get();
1466
1467 const int halfY = height >> 1;
1468 char* top = reinterpret_cast<char*>(buffer);
1469 char* bottom = top + (height - 1) * stride;
1470 for (int y = 0; y < halfY; y++) {
1471 memcpy(tmp, top, stride);
1472 memcpy(top, bottom, stride);
1473 memcpy(bottom, tmp, stride);
1474 top += stride;
1475 bottom -= stride;
1476 }
1477 }
1478 return true;
1479}
1480
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001481void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001482
1483 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1484
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001485 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001486 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001487 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001488 #if GR_COLLECT_STATS
1489 ++fStats.fRenderTargetChngCnt;
1490 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 #if GR_DEBUG
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001492 GrGLenum status = GL_CALL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001493 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001494 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001495 }
1496 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001497 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001498 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001499 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001500 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001501 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 fHWBounds.fViewportRect = vp;
1503 }
1504 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001505 if (NULL == bound || !bound->isEmpty()) {
1506 rt->flagAsNeedingResolve(bound);
1507 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001508}
1509
twiz@google.com0f31ca72011-03-18 17:38:11 +00001510GrGLenum gPrimitiveType2GLMode[] = {
1511 GR_GL_TRIANGLES,
1512 GR_GL_TRIANGLE_STRIP,
1513 GR_GL_TRIANGLE_FAN,
1514 GR_GL_POINTS,
1515 GR_GL_LINES,
1516 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001517};
1518
bsalomon@google.comd302f142011-03-03 13:54:13 +00001519#define SWAP_PER_DRAW 0
1520
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001521#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001522 #if GR_MAC_BUILD
1523 #include <AGL/agl.h>
1524 #elif GR_WIN32_BUILD
1525 void SwapBuf() {
1526 DWORD procID = GetCurrentProcessId();
1527 HWND hwnd = GetTopWindow(GetDesktopWindow());
1528 while(hwnd) {
1529 DWORD wndProcID = 0;
1530 GetWindowThreadProcessId(hwnd, &wndProcID);
1531 if(wndProcID == procID) {
1532 SwapBuffers(GetDC(hwnd));
1533 }
1534 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1535 }
1536 }
1537 #endif
1538#endif
1539
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001540void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1541 uint32_t startVertex,
1542 uint32_t startIndex,
1543 uint32_t vertexCount,
1544 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001545 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1546
twiz@google.com0f31ca72011-03-18 17:38:11 +00001547 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001548
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001549 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1550 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1551
1552 // our setupGeometry better have adjusted this to zero since
1553 // DrawElements always draws from the begining of the arrays for idx 0.
1554 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001555
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001556 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1557 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001558#if SWAP_PER_DRAW
1559 glFlush();
1560 #if GR_MAC_BUILD
1561 aglSwapBuffers(aglGetCurrentContext());
1562 int set_a_break_pt_here = 9;
1563 aglSwapBuffers(aglGetCurrentContext());
1564 #elif GR_WIN32_BUILD
1565 SwapBuf();
1566 int set_a_break_pt_here = 9;
1567 SwapBuf();
1568 #endif
1569#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001570}
1571
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001572void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1573 uint32_t startVertex,
1574 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001575 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1576
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001577 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1578
1579 // our setupGeometry better have adjusted this to zero.
1580 // DrawElements doesn't take an offset so we always adjus the startVertex.
1581 GrAssert(0 == startVertex);
1582
1583 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1584 // account for startVertex in the DrawElements case. So we always
1585 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001586 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001587#if SWAP_PER_DRAW
1588 glFlush();
1589 #if GR_MAC_BUILD
1590 aglSwapBuffers(aglGetCurrentContext());
1591 int set_a_break_pt_here = 9;
1592 aglSwapBuffers(aglGetCurrentContext());
1593 #elif GR_WIN32_BUILD
1594 SwapBuf();
1595 int set_a_break_pt_here = 9;
1596 SwapBuf();
1597 #endif
1598#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001599}
1600
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001601void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001602
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001603 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 GrAssert(kNone_MSFBO != fMSFBOType);
1605 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001606 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1607 rt->renderFBOID()));
1608 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1609 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 #if GR_COLLECT_STATS
1611 ++fStats.fRenderTargetChngCnt;
1612 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001613 // make sure we go through flushRenderTarget() since we've modified
1614 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001615 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001616 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001617 const GrIRect dirtyRect = rt->getResolveRect();
1618 GrGLIRect r;
1619 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1620 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001621
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001622 if (kAppleES_MSFBO == fMSFBOType) {
1623 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001624 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1625 GL_CALL(Scissor(r.fLeft, r.fBottom,
1626 r.fWidth, r.fHeight));
1627 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001628 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001629 fHWBounds.fScissorEnabled = true;
1630 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001631 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001632 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001633 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1634 flushScissor(NULL);
1635 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001636 int right = r.fLeft + r.fWidth;
1637 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001638 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1639 r.fLeft, r.fBottom, right, top,
1640 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001641 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001642 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001643 }
1644}
1645
twiz@google.com0f31ca72011-03-18 17:38:11 +00001646static const GrGLenum grToGLStencilFunc[] = {
1647 GR_GL_ALWAYS, // kAlways_StencilFunc
1648 GR_GL_NEVER, // kNever_StencilFunc
1649 GR_GL_GREATER, // kGreater_StencilFunc
1650 GR_GL_GEQUAL, // kGEqual_StencilFunc
1651 GR_GL_LESS, // kLess_StencilFunc
1652 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1653 GR_GL_EQUAL, // kEqual_StencilFunc,
1654 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001655};
1656GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1657GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1658GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1659GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1660GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1661GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1662GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1663GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1664GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1665
twiz@google.com0f31ca72011-03-18 17:38:11 +00001666static const GrGLenum grToGLStencilOp[] = {
1667 GR_GL_KEEP, // kKeep_StencilOp
1668 GR_GL_REPLACE, // kReplace_StencilOp
1669 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1670 GR_GL_INCR, // kIncClamp_StencilOp
1671 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1672 GR_GL_DECR, // kDecClamp_StencilOp
1673 GR_GL_ZERO, // kZero_StencilOp
1674 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001675};
1676GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1677GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1678GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1679GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1680GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1681GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1682GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1683GR_STATIC_ASSERT(6 == kZero_StencilOp);
1684GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1685
reed@google.comac10a2d2010-12-22 21:39:39 +00001686void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001687 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001688
1689 // use stencil for clipping if clipping is enabled and the clip
1690 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001691 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001692 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001693 bool stencilChange = fHWStencilClip != stencilClip ||
1694 fHWDrawState.fStencilSettings != *settings ||
1695 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1696 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001697
1698 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001699
bsalomon@google.comd302f142011-03-03 13:54:13 +00001700 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1701 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001702
bsalomon@google.comd302f142011-03-03 13:54:13 +00001703 if (settings->isDisabled()) {
1704 if (stencilClip) {
1705 settings = &gClipStencilSettings;
1706 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001707 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001708
1709 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001710 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001711 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001712 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001713 #if GR_DEBUG
1714 if (!fStencilWrapOpsSupport) {
1715 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1716 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1717 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1718 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1719 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1720 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1721 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1722 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1723 }
1724 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001725 int stencilBits = 0;
1726 GrStencilBuffer* stencilBuffer =
1727 fCurrDrawState.fRenderTarget->getStencilBuffer();
1728 if (NULL != stencilBuffer) {
1729 stencilBits = stencilBuffer->bits();
1730 }
1731 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001732 GrAssert(stencilBits ||
1733 (GrStencilSettings::gDisabled ==
1734 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001735 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1736 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001737
1738 unsigned int frontRef = settings->fFrontFuncRef;
1739 unsigned int frontMask = settings->fFrontFuncMask;
1740 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001741 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001742
1743 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1744
1745 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1746 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1747 } else {
1748 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1749
1750 ConvertStencilFuncAndMask(settings->fFrontFunc,
1751 stencilClip,
1752 clipStencilMask,
1753 userStencilMask,
1754 &frontRef,
1755 &frontMask);
1756 frontWriteMask &= userStencilMask;
1757 }
1758 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001759 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001760 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001761 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001762 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001763 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001764 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001765 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001766 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001767 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001768
1769 unsigned int backRef = settings->fBackFuncRef;
1770 unsigned int backMask = settings->fBackFuncMask;
1771 unsigned int backWriteMask = settings->fBackWriteMask;
1772
1773
1774 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1775 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1776 backFunc = grToGLStencilFunc[settings->fBackFunc];
1777 } else {
1778 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1779 ConvertStencilFuncAndMask(settings->fBackFunc,
1780 stencilClip,
1781 clipStencilMask,
1782 userStencilMask,
1783 &backRef,
1784 &backMask);
1785 backWriteMask &= userStencilMask;
1786 }
1787
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001788 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1789 frontRef, frontMask));
1790 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1791 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1792 backRef, backMask));
1793 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1794 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1795 grToGLStencilOp[settings->fFrontFailOp],
1796 grToGLStencilOp[settings->fFrontPassOp],
1797 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001798
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001799 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1800 grToGLStencilOp[settings->fBackFailOp],
1801 grToGLStencilOp[settings->fBackPassOp],
1802 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001804 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1805 GL_CALL(StencilMask(frontWriteMask));
1806 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001807 grToGLStencilOp[settings->fFrontPassOp],
1808 grToGLStencilOp[settings->fFrontPassOp]));
1809 }
1810 }
1811 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001812 fHWStencilClip = stencilClip;
1813 }
1814}
1815
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001816void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001817 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001818 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1819 // smooth lines.
1820
1821 // we prefer smooth lines over multisampled lines
1822 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001823 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001824 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001825 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001826 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001827 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001828 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001829 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001830 fHWAAState.fSmoothLineEnabled = false;
1831 }
1832 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1833 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001834 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001835 fHWAAState.fMSAAEnabled = false;
1836 }
1837 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1838 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
1839 fHWAAState.fMSAAEnabled) {
1840 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001841 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001842 fHWAAState.fMSAAEnabled = false;
1843 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001844 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001845 fHWAAState.fMSAAEnabled = true;
1846 }
1847 }
1848 }
1849}
1850
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001851void GrGpuGL::flushBlend(GrPrimitiveType type,
1852 GrBlendCoeff srcCoeff,
1853 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001854 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001855 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001856 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001857 fHWBlendDisabled = false;
1858 }
1859 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1860 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001861 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1862 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001863 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1864 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1865 }
1866 } else {
1867 bool blendOff = canDisableBlend();
1868 if (fHWBlendDisabled != blendOff) {
1869 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001870 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001871 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001872 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001873 }
1874 fHWBlendDisabled = blendOff;
1875 }
1876 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001877 if (fHWDrawState.fSrcBlend != srcCoeff ||
1878 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001879 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1880 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001881 fHWDrawState.fSrcBlend = srcCoeff;
1882 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001883 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001884 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1885 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001886 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1887
1888 float c[] = {
1889 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1890 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1891 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1892 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1893 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001894 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001895 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1896 }
1897 }
1898 }
1899}
1900
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001901static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1902 switch (filter) {
1903 case GrSamplerState::kBilinear_Filter:
1904 case GrSamplerState::k4x4Downsample_Filter:
1905 return GR_GL_LINEAR;
1906 case GrSamplerState::kNearest_Filter:
1907 case GrSamplerState::kConvolution_Filter:
1908 return GR_GL_NEAREST;
1909 default:
1910 GrAssert(!"Unknown filter type");
1911 return GR_GL_LINEAR;
1912 }
1913}
1914
bsalomon@google.comffca4002011-02-22 20:34:01 +00001915bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001916
1917 // GrGpu::setupClipAndFlushState should have already checked this
1918 // and bailed if not true.
1919 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001920
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001921 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001922 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001923 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001924 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001925
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001926 // true for now, but maybe not with GrEffect.
1927 GrAssert(NULL != nextTexture);
1928 // if we created a rt/tex and rendered to it without using a
1929 // texture and now we're texuring from the rt it will still be
1930 // the last bound texture, but it needs resolving. So keep this
1931 // out of the "last != next" check.
1932 GrGLRenderTarget* texRT =
1933 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1934 if (NULL != texRT) {
1935 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001936 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001937
1938 if (fHWDrawState.fTextures[s] != nextTexture) {
1939 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001940 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001941 #if GR_COLLECT_STATS
1942 ++fStats.fTextureChngCnt;
1943 #endif
1944 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1945 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001946 // The texture matrix has to compensate for texture width/height
1947 // and NPOT-embedded-in-POT
1948 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001949 }
1950
1951 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
1952 const GrGLTexture::TexParams& oldTexParams =
1953 nextTexture->getTexParams();
1954 GrGLTexture::TexParams newTexParams;
1955
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001956 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001957
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 const GrGLenum* wraps =
1959 GrGLTexture::WrapMode2GLWrap(this->glBinding());
1960 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1961 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001962
1963 if (newTexParams.fFilter != oldTexParams.fFilter) {
1964 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001965 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1966 GR_GL_TEXTURE_MAG_FILTER,
1967 newTexParams.fFilter));
1968 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1969 GR_GL_TEXTURE_MIN_FILTER,
1970 newTexParams.fFilter));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001971 }
1972 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
1973 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001974 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1975 GR_GL_TEXTURE_WRAP_S,
1976 newTexParams.fWrapS));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001977 }
1978 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
1979 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001980 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1981 GR_GL_TEXTURE_WRAP_T,
1982 newTexParams.fWrapT));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001983 }
1984 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00001985 }
1986 }
1987
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001988 GrIRect* rect = NULL;
1989 GrIRect clipBounds;
1990 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
1991 fClip.hasConservativeBounds()) {
1992 fClip.getConservativeBounds().roundOut(&clipBounds);
1993 rect = &clipBounds;
1994 }
1995 this->flushRenderTarget(rect);
1996 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001997
reed@google.comac10a2d2010-12-22 21:39:39 +00001998 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
1999 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2000 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002001 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002002 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002003 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002004 }
2005 }
2006
bsalomon@google.comd302f142011-03-03 13:54:13 +00002007 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2008 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002009 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002010 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002011 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002012 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002013 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002014 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002015 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002016 }
2017
bsalomon@google.comd302f142011-03-03 13:54:13 +00002018 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2019 switch (fCurrDrawState.fDrawFace) {
2020 case kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002021 GL_CALL(Enable(GR_GL_CULL_FACE));
2022 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002023 break;
2024 case kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002025 GL_CALL(Enable(GR_GL_CULL_FACE));
2026 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002027 break;
2028 case kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002029 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002030 break;
2031 default:
2032 GrCrash("Unknown draw face.");
2033 }
2034 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2035 }
2036
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002037#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002038 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002039 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002040 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002041 NULL == fCurrDrawState.fRenderTarget ||
2042 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002043 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002044 fCurrDrawState.fRenderTarget);
2045 }
2046#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002047
reed@google.comac10a2d2010-12-22 21:39:39 +00002048 flushStencil();
2049
bsalomon@google.comd302f142011-03-03 13:54:13 +00002050 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002051 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002052 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002053}
2054
2055void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002056 if (fHWGeometryState.fVertexBuffer != buffer) {
2057 fHWGeometryState.fArrayPtrsDirty = true;
2058 fHWGeometryState.fVertexBuffer = buffer;
2059 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002060}
2061
2062void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002063 if (fHWGeometryState.fVertexBuffer == buffer) {
2064 // deleting bound buffer does implied bind to 0
2065 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002066 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002067 }
2068}
2069
2070void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002071 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002072}
2073
2074void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002075 if (fHWGeometryState.fIndexBuffer == buffer) {
2076 // deleting bound buffer does implied bind to 0
2077 fHWGeometryState.fIndexBuffer = NULL;
2078 }
2079}
2080
reed@google.comac10a2d2010-12-22 21:39:39 +00002081void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2082 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002083 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002084 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002085 }
2086 if (fHWDrawState.fRenderTarget == renderTarget) {
2087 fHWDrawState.fRenderTarget = NULL;
2088 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002089}
2090
2091void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002092 for (int s = 0; s < kNumStages; ++s) {
2093 if (fCurrDrawState.fTextures[s] == texture) {
2094 fCurrDrawState.fTextures[s] = NULL;
2095 }
2096 if (fHWDrawState.fTextures[s] == texture) {
2097 // deleting bound texture does implied bind to 0
2098 fHWDrawState.fTextures[s] = NULL;
2099 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002100 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002101}
2102
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002103bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002104 GrGLenum* internalFormat,
2105 GrGLenum* format,
2106 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002107 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002108 case kRGBA_8888_GrPixelConfig:
2109 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002110 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002111 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002112 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2113 // format for a BGRA is BGRA not RGBA (as on desktop)
2114 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2115 } else {
2116 *internalFormat = GR_GL_RGBA;
2117 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002118 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002119 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002120 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002121 *format = GR_GL_RGB;
2122 *internalFormat = GR_GL_RGB;
2123 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002124 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002125 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002126 *format = GR_GL_RGBA;
2127 *internalFormat = GR_GL_RGBA;
2128 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002129 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002130 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002131 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002132 *format = GR_GL_PALETTE8_RGBA8;
2133 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002134 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002135 } else {
2136 return false;
2137 }
2138 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002139 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002140 *format = GR_GL_ALPHA;
2141 *internalFormat = GR_GL_ALPHA;
2142 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002143 break;
2144 default:
2145 return false;
2146 }
2147 return true;
2148}
2149
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002150void GrGpuGL::setTextureUnit(int unit) {
2151 GrAssert(unit >= 0 && unit < kNumStages);
2152 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002154 fActiveTextureUnitIdx = unit;
2155 }
2156}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002157
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002158void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002159 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002160 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002161 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2162 }
2163}
2164
reed@google.comac10a2d2010-12-22 21:39:39 +00002165/* On ES the internalFormat and format must match for TexImage and we use
2166 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2167 decide the internalFormat. However, on ES internalFormat for
2168 RenderBufferStorage* has to be a specific format (not a base format like
2169 GL_RGBA).
2170 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002171bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002172 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002173 case kRGBA_8888_GrPixelConfig:
2174 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002175 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002176 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002177 return true;
2178 } else {
2179 return false;
2180 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002181 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002182 // ES2 supports 565. ES1 supports it
2183 // with FBO extension desktop GL has
2184 // no such internal format
2185 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002186 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002187 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002188 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002189 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002190 return true;
2191 default:
2192 return false;
2193 }
2194}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002195
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002196void GrGpuGL::resetDirtyFlags() {
2197 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2198}
2199
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002200void GrGpuGL::setBuffers(bool indexed,
2201 int* extraVertexOffset,
2202 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002203
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002204 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002205
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002206 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2207
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002208 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002209 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002210 case kBuffer_GeometrySrcType:
2211 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002212 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002213 break;
2214 case kArray_GeometrySrcType:
2215 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002216 this->finalizeReservedVertices();
2217 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2218 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002219 break;
2220 default:
2221 vbuf = NULL; // suppress warning
2222 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002223 }
2224
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002225 GrAssert(NULL != vbuf);
2226 GrAssert(!vbuf->isLocked());
2227 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002228 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002229 fHWGeometryState.fArrayPtrsDirty = true;
2230 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002231 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002232
2233 if (indexed) {
2234 GrAssert(NULL != extraIndexOffset);
2235
2236 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002237 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002238 case kBuffer_GeometrySrcType:
2239 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002240 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002241 break;
2242 case kArray_GeometrySrcType:
2243 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002244 this->finalizeReservedIndices();
2245 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2246 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002247 break;
2248 default:
2249 ibuf = NULL; // suppress warning
2250 GrCrash("Unknown geometry src type!");
2251 }
2252
2253 GrAssert(NULL != ibuf);
2254 GrAssert(!ibuf->isLocked());
2255 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002256 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002257 fHWGeometryState.fIndexBuffer = ibuf;
2258 }
2259 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002260}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002261
2262int GrGpuGL::getMaxEdges() const {
2263 // FIXME: This is a pessimistic estimate based on how many other things
2264 // want to add uniforms. This should be centralized somewhere.
2265 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2266}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002267