blob: 9697a4e9819596775c4b27f520458d455f3743a5 [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)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
twiz@google.com0f31ca72011-03-18 17:38:11 +000027static const GrGLenum gXfermodeCoeff2Blend[] = {
28 GR_GL_ZERO,
29 GR_GL_ONE,
30 GR_GL_SRC_COLOR,
31 GR_GL_ONE_MINUS_SRC_COLOR,
32 GR_GL_DST_COLOR,
33 GR_GL_ONE_MINUS_DST_COLOR,
34 GR_GL_SRC_ALPHA,
35 GR_GL_ONE_MINUS_SRC_ALPHA,
36 GR_GL_DST_ALPHA,
37 GR_GL_ONE_MINUS_DST_ALPHA,
38 GR_GL_CONSTANT_COLOR,
39 GR_GL_ONE_MINUS_CONSTANT_COLOR,
40 GR_GL_CONSTANT_ALPHA,
41 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
43 // extended blend coeffs
44 GR_GL_SRC1_COLOR,
45 GR_GL_ONE_MINUS_SRC1_COLOR,
46 GR_GL_SRC1_ALPHA,
47 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000048};
49
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 static const bool gCoeffReferencesBlendConst[] = {
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 false,
62 true,
63 true,
64 true,
65 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000066
67 // extended blend coeffs
68 false,
69 false,
70 false,
71 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000072 };
73 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
75
76 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
77 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
78 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
79 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
80 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
81 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
82 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
83 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
84 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
85 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
86 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
87 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
88 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
89 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
90
91 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
92 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
93 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
94 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
95
96 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
97 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000098}
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
103 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104 GrMatrix* matrix) {
105 GrAssert(NULL != texture);
106 GrAssert(NULL != matrix);
107 if (GR_Scalar1 != texture->contentScaleX() ||
108 GR_Scalar1 != texture->contentScaleY()) {
109 if (GrSamplerState::kRadial_SampleMode == mode) {
110 GrMatrix scale;
111 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
112 matrix->postConcat(scale);
113 } else if (GrSamplerState::kNormal_SampleMode == mode) {
114 GrMatrix scale;
115 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
116 matrix->postConcat(scale);
117 } else {
118 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
119 }
120 }
121 GrGLTexture::Orientation orientation = texture->orientation();
122 if (GrGLTexture::kBottomUp_Orientation == orientation) {
123 GrMatrix invY;
124 invY.setAll(GR_Scalar1, 0, 0,
125 0, -GR_Scalar1, GR_Scalar1,
126 0, 0, GrMatrix::I()[8]);
127 matrix->postConcat(invY);
128 } else {
129 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
130 }
131}
132
bsalomon@google.comd302f142011-03-03 13:54:13 +0000133bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134 const GrSamplerState& sampler) {
135 GrAssert(NULL != texture);
136 if (!sampler.getMatrix().isIdentity()) {
137 return false;
138 }
139 if (GR_Scalar1 != texture->contentScaleX() ||
140 GR_Scalar1 != texture->contentScaleY()) {
141 return false;
142 }
143 GrGLTexture::Orientation orientation = texture->orientation();
144 if (GrGLTexture::kBottomUp_Orientation == orientation) {
145 return false;
146 } else {
147 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
148 }
149 return true;
150}
151
152///////////////////////////////////////////////////////////////////////////////
153
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000154static bool gPrintStartupSpew;
155
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000156static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000157
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000158 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000159
twiz@google.com0f31ca72011-03-18 17:38:11 +0000160 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
162 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000163 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000164 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
165 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000166 // some implementations require texture to be mip-map complete before
167 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000168 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
169 GR_GL_TEXTURE_MIN_FILTER,
170 GR_GL_NEAREST));
171 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
172 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
173 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
174 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
175 GR_GL_COLOR_ATTACHMENT0,
176 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000177 GrGLenum status;
178 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000179 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
180 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000181
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000182 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000183}
184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185static bool probe_for_npot_render_target_support(const GrGLInterface* gl,
186 bool hasNPOTTextureSupport) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000187
188 /* Experimentation has found that some GLs that support NPOT textures
189 do not support FBOs with a NPOT texture. They report "unsupported" FBO
190 status. I don't know how to explicitly query for this. Do an
191 experiment. Note they may support NPOT with a renderbuffer but not a
192 texture. Presumably, the implementation bloats the renderbuffer
193 internally to the next POT.
194 */
195 if (hasNPOTTextureSupport) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000196 return fbo_test(gl, 200, 200);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000197 }
198 return false;
199}
200
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000201static int probe_for_min_render_target_height(const GrGLInterface* gl,
202 bool hasNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000203 int maxRenderTargetSize) {
204 /* The iPhone 4 has a restriction that for an FBO with texture color
205 attachment with height <= 8 then the width must be <= height. Here
206 we look for such a limitation.
207 */
208 if (gPrintStartupSpew) {
209 GrPrintf("Small height FBO texture experiments\n");
210 }
211 int minRenderTargetHeight = GR_INVAL_GLINT;
212 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
213 GrGLuint w = maxRenderTargetSize;
214 GrGLuint h = i;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000215 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000216 if (gPrintStartupSpew) {
217 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
218 }
219 minRenderTargetHeight = i;
220 break;
221 } else {
222 if (gPrintStartupSpew) {
223 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
224 }
225 }
226 }
227 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
228
229 return minRenderTargetHeight;
230}
231
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000232static int probe_for_min_render_target_width(const GrGLInterface* gl,
233 bool hasNPOTRenderTargetSupport,
234 int maxRenderTargetSize) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000235
236 if (gPrintStartupSpew) {
237 GrPrintf("Small width FBO texture experiments\n");
238 }
239 int minRenderTargetWidth = GR_INVAL_GLINT;
240 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
241 GrGLuint w = i;
242 GrGLuint h = maxRenderTargetSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000243 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000244 if (gPrintStartupSpew) {
245 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
246 }
247 minRenderTargetWidth = i;
248 break;
249 } else {
250 if (gPrintStartupSpew) {
251 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
252 }
253 }
254 }
255 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
256
257 return minRenderTargetWidth;
258}
259
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000260GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000261
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000263
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000264 gl->ref();
265 fGL = gl;
266 fGLBinding = glBinding;
267 switch (glBinding) {
268 case kDesktop_GrGLBinding:
269 GrAssert(gl->supportsDesktop());
270 break;
271 case kES1_GrGLBinding:
272 GrAssert(gl->supportsES1());
273 break;
274 case kES2_GrGLBinding:
275 GrAssert(gl->supportsES2());
276 break;
277 default:
278 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000279 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000280
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000281 GrGLClearErr(fGL);
282
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000283 const GrGLubyte* ext;
284 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000285 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000286 const GrGLubyte* vendor;
287 const GrGLubyte* renderer;
288 const GrGLubyte* version;
289 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
290 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
291 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000292 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
293 this);
294 GrPrintf("------ VENDOR %s\n", vendor);
295 GrPrintf("------ RENDERER %s\n", renderer);
296 GrPrintf("------ VERSION %s\n", version);
297 GrPrintf("------ EXTENSIONS\n %s \n", ext);
298 }
299
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000300 fGLVersion = GrGLGetVersion(gl);
301 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000302 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000304 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000305
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000306 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000307
bsalomon@google.comfe676522011-06-17 18:12:21 +0000308 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000309}
310
311GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000312 // This must be called by before the GrDrawTarget destructor
313 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000314 // This subclass must do this before the base class destructor runs
315 // since we will unref the GrGLInterface.
316 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000317 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000318}
319
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000320///////////////////////////////////////////////////////////////////////////////
321
322static const GrGLuint kUnknownBitCount = ~0;
323
324void GrGpuGL::initCaps() {
325 GrGLint maxTextureUnits;
326 // check FS and fixed-function texture unit limits
327 // we only use textures in the fragment stage currently.
328 // checks are > to make sure we have a spare unit.
329 if (kES1_GrGLBinding != this->glBinding()) {
330 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000331 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000332 }
333 if (kES2_GrGLBinding != this->glBinding()) {
334 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000335 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000336 }
337 if (kES2_GrGLBinding == this->glBinding()) {
338 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
339 &fGLCaps.fMaxFragmentUniformVectors);
340 } else if (kDesktop_GrGLBinding != this->glBinding()) {
341 GrGLint max;
342 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
343 fGLCaps.fMaxFragmentUniformVectors = max / 4;
344 } else {
345 fGLCaps.fMaxFragmentUniformVectors = 16;
346 }
347
348 GrGLint numFormats;
349 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
350 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
351 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
352 for (int i = 0; i < numFormats; ++i) {
353 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
354 fCaps.f8BitPaletteSupport = true;
355 break;
356 }
357 }
358
359 if (kDesktop_GrGLBinding == this->glBinding()) {
360 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
361 this->hasExtension("GL_EXT_stencil_wrap");
362 } else {
363 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(2,0)) ||
364 this->hasExtension("GL_OES_stencil_wrap");
365 }
366
367 if (kDesktop_GrGLBinding == this->glBinding()) {
368 // we could also look for GL_ATI_separate_stencil extension or
369 // GL_EXT_stencil_two_side but they use different function signatures
370 // than GL2.0+ (and than each other).
371 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
372 // supported on GL 1.4 and higher or by extension
373 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
374 this->hasExtension("GL_EXT_stencil_wrap");
375 } else {
376 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
377 // an ES1 extension.
378 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
379 // stencil wrap support is in ES2, ES1 requires extension.
380 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(2,0)) ||
381 this->hasExtension("GL_OES_stencil_wrap");
382 }
383
384 if (kDesktop_GrGLBinding == this->glBinding()) {
385 fGLCaps.fRGBA8Renderbuffer = true;
386 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000387 fGLCaps.fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8") ||
388 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000389 }
390
391
bsalomon@google.comc4364992011-11-07 15:54:49 +0000392 if (kDesktop_GrGLBinding == this->glBinding()) {
393 fGLCaps.fBGRAFormat = this->glVersion() >= GR_GL_VER(1,2) ||
394 this->hasExtension("GL_EXT_bgra");
395 } else {
396 bool hasBGRAExt = false;
397 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
398 fGLCaps.fBGRAFormat = true;
399 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
400 fGLCaps.fBGRAFormat = true;
401 fGLCaps.fBGRAInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000402 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000403 GrAssert(fGLCaps.fBGRAFormat ||
404 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000405 }
406
407 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000408 fGLCaps.fTextureSwizzle = this->glVersion() >= GR_GL_VER(3,3) ||
409 this->hasExtension("GL_ARB_texture_swizzle");
410 } else {
411 fGLCaps.fTextureSwizzle = false;
412 }
413
414 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000415 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
416 // extension includes glMapBuffer.
417 } else {
418 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
419 }
420
421 if (kDesktop_GrGLBinding == this->glBinding()) {
422 if (fGLVersion >= GR_GL_VER(2,0) ||
423 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
424 fCaps.fNPOTTextureTileSupport = true;
425 fCaps.fNPOTTextureSupport = true;
426 } else {
427 fCaps.fNPOTTextureTileSupport = false;
428 fCaps.fNPOTTextureSupport = false;
429 }
430 } else {
431 if (fGLVersion >= GR_GL_VER(2,0)) {
432 fCaps.fNPOTTextureSupport = true;
433 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
434 } else {
435 fCaps.fNPOTTextureSupport =
436 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
437 fCaps.fNPOTTextureTileSupport = false;
438 }
439 }
440
441 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
442
443 ////////////////////////////////////////////////////////////////////////////
444 // Experiments to determine limitations that can't be queried.
445 // TODO: Make these a preprocess that generate some compile time constants.
446 // TODO: probe once at startup, rather than once per context creation.
447
448 int expectNPOTTargets = fGL->fNPOTRenderTargetSupport;
449 if (expectNPOTTargets == kProbe_GrGLCapability) {
450 fCaps.fNPOTRenderTargetSupport =
451 probe_for_npot_render_target_support(fGL, fCaps.fNPOTTextureSupport);
452 } else {
453 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000454 fCaps.fNPOTRenderTargetSupport = (0 != expectNPOTTargets);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000455 }
456
457 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
458 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
459 // Our render targets are always created with textures as the color
460 // attachment, hence this min:
461 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
462
463 fCaps.fMinRenderTargetHeight = fGL->fMinRenderTargetHeight;
464 if (fCaps.fMinRenderTargetHeight == kProbe_GrGLCapability) {
465 fCaps.fMinRenderTargetHeight =
466 probe_for_min_render_target_height(fGL, fCaps.fNPOTRenderTargetSupport,
467 fCaps.fMaxRenderTargetSize);
468 }
469
470 fCaps.fMinRenderTargetWidth = fGL->fMinRenderTargetWidth;
471 if (fCaps.fMinRenderTargetWidth == kProbe_GrGLCapability) {
472 fCaps.fMinRenderTargetWidth =
473 probe_for_min_render_target_width(fGL, fCaps.fNPOTRenderTargetSupport,
474 fCaps.fMaxRenderTargetSize);
475 }
476
477 this->initFSAASupport();
478 this->initStencilFormats();
479}
480
481void GrGpuGL::initFSAASupport() {
482 // TODO: Get rid of GrAALevel and use # samples directly.
483 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
484 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
485 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
486 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
487 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
488
489 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
490 if (kDesktop_GrGLBinding != this->glBinding()) {
491 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
492 // chrome's extension is equivalent to the EXT msaa
493 // and fbo_blit extensions.
494 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
495 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
496 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
497 }
498 } else {
499 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
500 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
501 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
502 this->hasExtension("GL_EXT_framebuffer_blit")) {
503 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
504 }
505 }
506
507 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
508 GrGLint maxSamples;
509 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
510 if (maxSamples > 1 ) {
511 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
512 fGLCaps.fAASamples[kLow_GrAALevel] =
513 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
514 fGLCaps.fAASamples[kMed_GrAALevel] =
515 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
516 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
517 }
518 }
519 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
520}
521
522void GrGpuGL::initStencilFormats() {
523
524 // Build up list of legal stencil formats (though perhaps not supported on
525 // the particular gpu/driver) from most preferred to least.
526
527 // these consts are in order of most preferred to least preferred
528 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
529 static const GrGLStencilBuffer::Format
530 // internal Format stencil bits total bits packed?
531 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
532 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
533 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
534 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
535 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
536 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
537
538 if (kDesktop_GrGLBinding == this->glBinding()) {
539 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
540 this->hasExtension("GL_EXT_packed_depth_stencil") ||
541 this->hasExtension("GL_ARB_framebuffer_object");
542
543 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
544 // require FBO support we can expect these are legal formats and don't
545 // check. These also all support the unsized GL_STENCIL_INDEX.
546 fGLCaps.fStencilFormats.push_back() = gS8;
547 fGLCaps.fStencilFormats.push_back() = gS16;
548 if (supportsPackedDS) {
549 fGLCaps.fStencilFormats.push_back() = gD24S8;
550 }
551 fGLCaps.fStencilFormats.push_back() = gS4;
552 if (supportsPackedDS) {
553 fGLCaps.fStencilFormats.push_back() = gDS;
554 }
555 } else {
556 // ES2 has STENCIL_INDEX8 without extensions.
557 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
558 // introduces tokens for S1 thu S8 but there are separate extensions
559 // that make them legal (GL_OES_stencil1, ...).
560 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
561 // ES doesn't support using the unsized formats.
562
563 if (fGLVersion >= GR_GL_VER(2,0) ||
564 this->hasExtension("GL_OES_stencil8")) {
565 fGLCaps.fStencilFormats.push_back() = gS8;
566 }
567 //fStencilFormats.push_back() = gS16;
568 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
569 fGLCaps.fStencilFormats.push_back() = gD24S8;
570 }
571 if (this->hasExtension("GL_OES_stencil4")) {
572 fGLCaps.fStencilFormats.push_back() = gS4;
573 }
574 // we require some stencil format.
575 GrAssert(fGLCaps.fStencilFormats.count() > 0);
576 }
577}
578
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000579void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000580 if (gPrintStartupSpew && !fPrintedCaps) {
581 fPrintedCaps = true;
582 this->getCaps().print();
583 fGLCaps.print();
584 }
585
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000586 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000588 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000589
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000590 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000591 GL_CALL(Disable(GR_GL_DEPTH_TEST));
592 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000593
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000594 GL_CALL(Disable(GR_GL_CULL_FACE));
595 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000596 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000597
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000598 GL_CALL(Disable(GR_GL_DITHER));
599 if (kDesktop_GrGLBinding == this->glBinding()) {
600 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
601 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
602 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000603 fHWAAState.fMSAAEnabled = false;
604 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000605 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000606
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000607 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000608 fHWDrawState.fFlagBits = 0;
609
reed@google.comac10a2d2010-12-22 21:39:39 +0000610 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000611 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000612
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000613 // invalid
614 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000615
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000617 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
618 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000619
620 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000621 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000622
reed@google.comac10a2d2010-12-22 21:39:39 +0000623 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000624
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000625 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000626
tomhudson@google.com93813632011-10-27 20:21:16 +0000627 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000628 fHWDrawState.fTextures[s] = NULL;
629 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
630 -GR_ScalarMax,
631 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000632 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000633 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000634 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000635
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000636 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000637 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000638 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000639 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000640
bsalomon@google.comd302f142011-03-03 13:54:13 +0000641 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000642 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000643 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000644
645 fHWGeometryState.fIndexBuffer = NULL;
646 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000647
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000648 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000649
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000650 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000651 fHWDrawState.fRenderTarget = NULL;
652}
653
bsalomon@google.come269f212011-11-07 13:29:52 +0000654GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
655 GrGLenum internalFormat; // we don't need this value
656 GrGLTexture::Desc glTexDesc;
657 if (!this->canBeTexture(desc.fConfig, &internalFormat,
658 &glTexDesc.fUploadFormat, &glTexDesc.fUploadType)) {
659 return NULL;
660 }
661
662 glTexDesc.fContentWidth = glTexDesc.fAllocWidth = desc.fWidth;
663 glTexDesc.fContentHeight = glTexDesc.fAllocHeight = desc.fHeight;
664 glTexDesc.fConfig = desc.fConfig;
665 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
666 glTexDesc.fOwnsID = false;
667 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
668
669 GrGLTexture* texture = NULL;
670 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
671 GrGLRenderTarget::Desc glRTDesc;
672 glRTDesc.fRTFBOID = 0;
673 glRTDesc.fTexFBOID = 0;
674 glRTDesc.fMSColorRenderbufferID = 0;
675 glRTDesc.fOwnIDs = true;
676 glRTDesc.fConfig = desc.fConfig;
677 glRTDesc.fSampleCnt = desc.fSampleCnt;
678 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
679 glTexDesc.fAllocHeight,
680 glTexDesc.fTextureID,
681 &glRTDesc)) {
682 return NULL;
683 }
684 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
685 } else {
686 texture = new GrGLTexture(this, glTexDesc);
687 }
688 if (NULL == texture) {
689 return NULL;
690 }
691
692 this->setSpareTextureUnit();
693 return texture;
694}
695
696GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
697 GrGLRenderTarget::Desc glDesc;
698 glDesc.fConfig = desc.fConfig;
699 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
700 glDesc.fMSColorRenderbufferID = 0;
701 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
702 glDesc.fSampleCnt = desc.fSampleCnt;
703 glDesc.fOwnIDs = false;
704 GrGLIRect viewport;
705 viewport.fLeft = 0;
706 viewport.fBottom = 0;
707 viewport.fWidth = desc.fWidth;
708 viewport.fHeight = desc.fHeight;
709
710 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
711 if (desc.fStencilBits) {
712 GrGLStencilBuffer::Format format;
713 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
714 format.fPacked = false;
715 format.fStencilBits = desc.fStencilBits;
716 format.fTotalBits = desc.fStencilBits;
717 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
718 0,
719 desc.fWidth,
720 desc.fHeight,
721 desc.fSampleCnt,
722 format);
723 tgt->setStencilBuffer(sb);
724 sb->unref();
725 }
726 return tgt;
727}
728
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000729GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
730
731 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
732 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
733 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
734 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
735
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000736 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000737 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000738
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000739 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000740 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000741 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000742 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000743 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000744 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000745 } else {
746 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000747 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000748 }
749 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000750 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000751 }
752 // we don't know what the RB ids are without glGets and we don't care
753 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000754 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000755 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000756 if (desc.fStencilBits) {
757 GrGLStencilBuffer::Format format;
758 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
759 format.fPacked = false;
760 format.fStencilBits = desc.fStencilBits;
761 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000762 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
763 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000764 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000765 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000766 }
767
768 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000769 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000770 GrGLenum dontCare;
771 if (!canBeTexture(desc.fConfig, &dontCare,
772 &texDesc.fUploadFormat,
773 &texDesc.fUploadType)) {
774 return NULL;
775 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000776 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
777 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
778
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000779 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000780 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000781 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000782 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000783
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000784 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000785 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000786 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000787 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000788 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000789 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000790 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000791 } else {
792 GrGLIRect viewport;
793 viewport.fLeft = 0;
794 viewport.fBottom = 0;
795 viewport.fWidth = desc.fWidth;
796 viewport.fHeight = desc.fHeight;
797
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000798 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000799 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000800 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000801 }
802}
803
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000804
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000805////////////////////////////////////////////////////////////////////////////////
806
807void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
808 GrGLenum internalFormat,
809 const void* data,
810 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000811 // we assume the texture is bound
812
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000813 size_t bpp = GrBytesPerPixel(desc.fConfig);
814 size_t trimRowBytes = desc.fContentWidth * bpp;
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000815
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000816 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000817 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000818 }
819
820 // in case we need a temporary, trimmed copy of the src pixels
821 SkAutoSMalloc<128 * 128> tempStorage;
822
823 /*
824 * check whether to allocate a temporary buffer for flipping y or
825 * because our data has extra bytes past each row. If so, we need
826 * to trim those off here, since GL ES doesn't let us specify
827 * GL_UNPACK_ROW_LENGTH.
828 */
829 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000830 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000831 if (data && rowBytes != trimRowBytes) {
832 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
833 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000834 }
835 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000836 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000837 // copy the data into our new storage, skipping the trailing bytes
838 size_t trimSize = desc.fContentHeight * trimRowBytes;
839 const char* src = (const char*)data;
840 if (flipY) {
841 src += (desc.fContentHeight - 1) * rowBytes;
842 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000843 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000844 for (int y = 0; y < desc.fContentHeight; y++) {
845 memcpy(dst, src, trimRowBytes);
846 if (flipY) {
847 src -= rowBytes;
848 } else {
849 src += rowBytes;
850 }
851 dst += trimRowBytes;
852 }
853 // now point data to our trimmed version
854 data = tempStorage.get();
855 rowBytes = trimRowBytes;
856 }
857 }
858
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000859 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000860 if (kIndex_8_GrPixelConfig == desc.fConfig &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000861 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000862 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
863 GrAssert(desc.fContentWidth == desc.fAllocWidth);
864 GrAssert(desc.fContentHeight == desc.fAllocHeight);
865 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
866 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000867 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
868 desc.fAllocWidth, desc.fAllocHeight,
869 0, imageSize, data));
870 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000871 } else {
872 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
873 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000874 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
875 desc.fAllocWidth, desc.fAllocHeight,
876 0, desc.fUploadFormat, desc.fUploadType, NULL));
877 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
878 desc.fContentHeight, desc.fUploadFormat,
879 desc.fUploadType, data));
880 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000881
882 int extraW = desc.fAllocWidth - desc.fContentWidth;
883 int extraH = desc.fAllocHeight - desc.fContentHeight;
884 int maxTexels = extraW * extraH;
885 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
886 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
887
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000888 SkAutoSMalloc<128*128> texels(bpp * maxTexels);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000889
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000890 if (extraH) {
891 uint8_t* lastRowStart = (uint8_t*) data +
892 (desc.fContentHeight - 1) * rowBytes;
893 uint8_t* extraRowStart = (uint8_t*)texels.get();
894
895 for (int i = 0; i < extraH; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000896 memcpy(extraRowStart, lastRowStart, trimRowBytes);
897 extraRowStart += trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000898 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000899 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
900 desc.fContentHeight, desc.fContentWidth,
901 extraH, desc.fUploadFormat,
902 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000903 }
904 if (extraW) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000905 uint8_t* edgeTexel = (uint8_t*)data + trimRowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000906 uint8_t* extraTexel = (uint8_t*)texels.get();
907 for (int j = 0; j < desc.fContentHeight; ++j) {
908 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000909 memcpy(extraTexel, edgeTexel, bpp);
910 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000911 }
912 edgeTexel += rowBytes;
913 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000914 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
915 0, extraW, desc.fContentHeight,
916 desc.fUploadFormat, desc.fUploadType,
917 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000918 }
919 if (extraW && extraH) {
920 uint8_t* cornerTexel = (uint8_t*)data +
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000921 desc.fContentHeight * rowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000922 uint8_t* extraTexel = (uint8_t*)texels.get();
923 for (int i = 0; i < extraW*extraH; ++i) {
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000924 memcpy(extraTexel, cornerTexel, bpp);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000925 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000926 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
928 desc.fContentHeight, extraW, extraH,
929 desc.fUploadFormat, desc.fUploadType,
930 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000931 }
932
933 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000934 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
935 desc.fAllocWidth, desc.fAllocHeight, 0,
936 desc.fUploadFormat, desc.fUploadType, data));
937 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000938 }
939 }
940}
941
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942bool GrGpuGL::createRenderTargetObjects(int width, int height,
943 GrGLuint texID,
944 GrGLRenderTarget::Desc* desc) {
945 desc->fMSColorRenderbufferID = 0;
946 desc->fRTFBOID = 0;
947 desc->fTexFBOID = 0;
948 desc->fOwnIDs = true;
949
950 GrGLenum status;
951 GrGLint err;
952
bsalomon@google.comab15d612011-08-09 12:57:56 +0000953 GrGLenum msColorFormat = 0; // suppress warning
954
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000955 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000956 if (!desc->fTexFBOID) {
957 goto FAILED;
958 }
959
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000960
961 // If we are using multisampling we will create two FBOS. We render
962 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000963 if (desc->fSampleCnt > 0) {
964 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
965 goto FAILED;
966 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000967 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
968 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000969 if (!desc->fRTFBOID ||
970 !desc->fMSColorRenderbufferID ||
971 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
972 goto FAILED;
973 }
974 } else {
975 desc->fRTFBOID = desc->fTexFBOID;
976 }
977
978 if (desc->fRTFBOID != desc->fTexFBOID) {
979 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000980 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000981 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000982 GR_GL_CALL_NOERRCHECK(this->glInterface(),
983 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
984 desc->fSampleCnt,
985 msColorFormat,
986 width, height));
987 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000988 if (err != GR_GL_NO_ERROR) {
989 goto FAILED;
990 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000991 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
992 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000993 GR_GL_COLOR_ATTACHMENT0,
994 GR_GL_RENDERBUFFER,
995 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000996 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000997 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
998 goto FAILED;
999 }
1000 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001001 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001002
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001003 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1004 GR_GL_COLOR_ATTACHMENT0,
1005 GR_GL_TEXTURE_2D,
1006 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001007 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001008 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1009 goto FAILED;
1010 }
1011
1012 return true;
1013
1014FAILED:
1015 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001017 }
1018 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001019 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001020 }
1021 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001022 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001023 }
1024 return false;
1025}
1026
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001027// good to set a break-point here to know when createTexture fails
1028static GrTexture* return_null_texture() {
1029// GrAssert(!"null texture");
1030 return NULL;
1031}
1032
1033#if GR_DEBUG
1034static size_t as_size_t(int x) {
1035 return x;
1036}
1037#endif
1038
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001039namespace {
1040void set_tex_swizzle(GrPixelConfig config, const GrGLInterface* gl) {
1041 // Today we always use GL_ALPHA for kAlpha_8_GrPixelConfig. However,
1042 // this format is deprecated sometimes isn't a renderable format. If we
1043 // were to spoof it in the future with GL_RED we'd want to notice that
1044 // here.
1045 // This isn't recorded in our tex params struct becauase we infer it
1046 // from the pixel config.
1047 const GrGLint* swiz;
1048 if (GrPixelConfigIsAlphaOnly(config)) {
1049 static const GrGLint gAlphaSwiz[] = {GR_GL_ALPHA, GR_GL_ALPHA,
1050 GR_GL_ALPHA, GR_GL_ALPHA};
1051 swiz = gAlphaSwiz;
1052 } else {
1053 static const GrGLint gColorSwiz[] = {GR_GL_RED, GR_GL_GREEN,
1054 GR_GL_BLUE, GR_GL_ALPHA};
1055 swiz = gColorSwiz;
1056 }
1057 // should add texparameteri to interface to make 1 instead of 4 calls here
1058 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1059 GR_GL_TEXTURE_SWIZZLE_R,
1060 swiz[0]));
1061 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1062 GR_GL_TEXTURE_SWIZZLE_G,
1063 swiz[1]));
1064 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1065 GR_GL_TEXTURE_SWIZZLE_B,
1066 swiz[2]));
1067 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1068 GR_GL_TEXTURE_SWIZZLE_A,
1069 swiz[3]));
1070}
1071}
1072
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001073GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001074 const void* srcData,
1075 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001076
1077#if GR_COLLECT_STATS
1078 ++fStats.fTextureCreateCnt;
1079#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001080
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001081 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001082 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001083 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001084
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001085 glTexDesc.fContentWidth = desc.fWidth;
1086 glTexDesc.fContentHeight = desc.fHeight;
1087 glTexDesc.fAllocWidth = desc.fWidth;
1088 glTexDesc.fAllocHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001089 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001090 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001091
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001092 glRTDesc.fMSColorRenderbufferID = 0;
1093 glRTDesc.fRTFBOID = 0;
1094 glRTDesc.fTexFBOID = 0;
1095 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001096 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001097
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001098 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001099 if (!canBeTexture(desc.fConfig,
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001101 &glTexDesc.fUploadFormat,
1102 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001103 return return_null_texture();
1104 }
1105
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001106 const Caps& caps = this->getCaps();
1107
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001108 // We keep GrRenderTargets in GL's normal orientation so that they
1109 // can be drawn to by the outside world without the client having
1110 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001111 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001113
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001114 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1115 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1116 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1117 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001118 GrPrintf("AA RT requested but not supported on this platform.");
1119 }
1120
reed@google.comac10a2d2010-12-22 21:39:39 +00001121 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001122 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001123 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1124 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001125 }
1126
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001127 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001129 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001131 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1132 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001133 return return_null_texture();
1134 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001135 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001136 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1137 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001138 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1139 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001140 return return_null_texture();
1141 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 }
1143
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001145 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001146 return return_null_texture();
1147 }
1148
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001149 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001150 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001151
1152 // Some drivers like to know these before seeing glTexImage2D. Some drivers
1153 // have a bug where an FBO won't be complete if it includes a texture that
1154 // is not complete (i.e. has mip levels or non-mip min filter).
1155 static const GrGLTexture::TexParams DEFAULT_TEX_PARAMS = {
1156 GR_GL_NEAREST,
1157 GR_GL_CLAMP_TO_EDGE,
1158 GR_GL_CLAMP_TO_EDGE
1159 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1161 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.come269f212011-11-07 13:29:52 +00001162 DEFAULT_TEX_PARAMS.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001163 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1164 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.come269f212011-11-07 13:29:52 +00001165 DEFAULT_TEX_PARAMS.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001166 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1167 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.come269f212011-11-07 13:29:52 +00001168 DEFAULT_TEX_PARAMS.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1170 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.come269f212011-11-07 13:29:52 +00001171 DEFAULT_TEX_PARAMS.fWrapT));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001172 if (fGLCaps.fTextureSwizzle) {
1173 set_tex_swizzle(desc.fConfig, this->glInterface());
1174 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001175 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001176
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001178 if (renderTarget) {
1179#if GR_COLLECT_STATS
1180 ++fStats.fRenderTargetCreateCnt;
1181#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1183 glTexDesc.fAllocHeight,
1184 glTexDesc.fTextureID,
1185 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001186 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001187 return return_null_texture();
1188 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001189 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001191 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001192 }
bsalomon@google.come269f212011-11-07 13:29:52 +00001193 tex->setCachedTexParams(DEFAULT_TEX_PARAMS, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001194#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001195 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1196 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001197#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 return tex;
1199}
1200
1201namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1203 GrGLuint rb,
1204 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 // we shouldn't ever know one size and not the other
1206 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1207 (kUnknownBitCount == format->fTotalBits));
1208 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001209 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1211 (GrGLint*)&format->fStencilBits);
1212 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001213 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001214 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1215 (GrGLint*)&format->fTotalBits);
1216 format->fTotalBits += format->fStencilBits;
1217 } else {
1218 format->fTotalBits = format->fStencilBits;
1219 }
1220 }
1221}
1222}
1223
1224bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1225 int width, int height) {
1226
1227 // All internally created RTs are also textures. We don't create
1228 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1229 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001230 GrAssert(width >= rt->allocatedWidth());
1231 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232
1233 int samples = rt->numSamples();
1234 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001235 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 if (!sbID) {
1237 return false;
1238 }
1239
1240 GrGLStencilBuffer* sb = NULL;
1241
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001242 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001244 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 // we start with the last stencil format that succeeded in hopes
1246 // that we won't go through this loop more than once after the
1247 // first (painful) stencil creation.
1248 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001249 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001250 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 // version on a GL that doesn't have an MSAA extension.
1252 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001253 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1254 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255 GR_GL_RENDERBUFFER,
1256 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001257 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001258 width,
1259 height));
1260 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1262 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001263 sFmt.fInternalFormat,
1264 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001265 }
1266
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001267 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 if (err == GR_GL_NO_ERROR) {
1269 // After sized formats we attempt an unsized format and take whatever
1270 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001271 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001273 sb = new GrGLStencilBuffer(this, sbID, width, height,
1274 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001275 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1276 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001277 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001278 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279 return true;
1280 }
1281 sb->abandon(); // otherwise we lose sbID
1282 sb->unref();
1283 }
1284 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001285 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001286 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001287}
1288
1289bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1290 GrRenderTarget* rt) {
1291 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1292
1293 GrGLuint fbo = glrt->renderFBOID();
1294
1295 if (NULL == sb) {
1296 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001297 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001298 GR_GL_STENCIL_ATTACHMENT,
1299 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001300 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001301 GR_GL_DEPTH_ATTACHMENT,
1302 GR_GL_RENDERBUFFER, 0));
1303#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001304 GrGLenum status;
1305 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001306 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1307#endif
1308 }
1309 return true;
1310 } else {
1311 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1312 GrGLuint rb = glsb->renderbufferID();
1313
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001315 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1316 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001317 GR_GL_STENCIL_ATTACHMENT,
1318 GR_GL_RENDERBUFFER, rb));
1319 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001320 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001321 GR_GL_DEPTH_ATTACHMENT,
1322 GR_GL_RENDERBUFFER, rb));
1323 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001324 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001325 GR_GL_DEPTH_ATTACHMENT,
1326 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001328
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001329 GrGLenum status;
1330 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001331 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001332 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001333 GR_GL_STENCIL_ATTACHMENT,
1334 GR_GL_RENDERBUFFER, 0));
1335 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001336 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001337 GR_GL_DEPTH_ATTACHMENT,
1338 GR_GL_RENDERBUFFER, 0));
1339 }
1340 return false;
1341 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001342 return true;
1343 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001345}
1346
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001347////////////////////////////////////////////////////////////////////////////////
1348
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001349GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001350 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001351 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001353 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001354 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001355 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001357 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1358 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1359 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1360 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1361 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001362 // deleting bound buffer does implicit bind to 0
1363 fHWGeometryState.fVertexBuffer = NULL;
1364 return NULL;
1365 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001366 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001367 size, dynamic);
1368 fHWGeometryState.fVertexBuffer = vertexBuffer;
1369 return vertexBuffer;
1370 }
1371 return NULL;
1372}
1373
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001374GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001375 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001376 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001378 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1379 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001380 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001381 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1382 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1383 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1384 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1385 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001386 // deleting bound buffer does implicit bind to 0
1387 fHWGeometryState.fIndexBuffer = NULL;
1388 return NULL;
1389 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001390 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001391 size, dynamic);
1392 fHWGeometryState.fIndexBuffer = indexBuffer;
1393 return indexBuffer;
1394 }
1395 return NULL;
1396}
1397
reed@google.comac10a2d2010-12-22 21:39:39 +00001398void GrGpuGL::flushScissor(const GrIRect* rect) {
1399 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001400 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001401 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001402
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001403 GrGLIRect scissor;
1404 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001405 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001406 rect->width(), rect->height());
1407 if (scissor.contains(vp)) {
1408 rect = NULL;
1409 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001410 }
1411
1412 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001414 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001415 fHWBounds.fScissorRect = scissor;
1416 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001417 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001418 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001419 fHWBounds.fScissorEnabled = true;
1420 }
1421 } else {
1422 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001423 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001424 fHWBounds.fScissorEnabled = false;
1425 }
1426 }
1427}
1428
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001429void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001430 if (NULL == fCurrDrawState.fRenderTarget) {
1431 return;
1432 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001433 GrIRect r;
1434 if (NULL != rect) {
1435 // flushScissor expects rect to be clipped to the target.
1436 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001437 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1438 fCurrDrawState.fRenderTarget->height());
1439 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001440 rect = &r;
1441 } else {
1442 return;
1443 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001444 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001445 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001446 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001447 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001448 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001449 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1450 GrColorUnpackG(color)/255.f,
1451 GrColorUnpackB(color)/255.f,
1452 GrColorUnpackA(color)/255.f));
1453 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001454}
1455
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001456void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001457 if (NULL == fCurrDrawState.fRenderTarget) {
1458 return;
1459 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001460
1461 this->flushRenderTarget(&GrIRect::EmptyIRect());
1462
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001464 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001465 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001466 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001467 GL_CALL(StencilMask(0xffffffff));
1468 GL_CALL(ClearStencil(0));
1469 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001470 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001471}
1472
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001473void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001474 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001475
1476 // this should only be called internally when we know we have a
1477 // stencil buffer.
1478 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001479 GrGLint stencilBitCount =
1480 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001481#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001482 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001483 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001484#else
1485 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001486 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001487 // turned into draws. Our contract on GrDrawTarget says that
1488 // changing the clip between stencil passes may or may not
1489 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001490 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001491#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001492 GrGLint value;
1493 if (insideClip) {
1494 value = (1 << (stencilBitCount - 1));
1495 } else {
1496 value = 0;
1497 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001498 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001499 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001500 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001501 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001502 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001503 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001504}
1505
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001506void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001507 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001508}
1509
bsalomon@google.comc4364992011-11-07 15:54:49 +00001510bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1511 int left, int top,
1512 int width, int height,
1513 GrPixelConfig config,
1514 size_t rowBytes) {
1515 if (kDesktop_GrGLBinding == this->glBinding()) {
1516 return false;
1517 } else {
1518 // On ES we'll have to do memcpys to handle rowByte padding. So, we
1519 // might as well flipY while we're at it.
bsalomon@google.comca08edd2011-11-07 16:56:39 +00001520 return 0 == rowBytes ||
1521 GrBytesPerPixel(config) * width == rowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001522 }
1523}
1524
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001525bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001526 int left, int top,
1527 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001528 GrPixelConfig config,
1529 void* buffer,
1530 size_t rowBytes,
1531 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001532 GrGLenum internalFormat; // we don't use this for glReadPixels
1533 GrGLenum format;
1534 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001535 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1536 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001538
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001540 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1541 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1542 switch (tgt->getResolveType()) {
1543 case GrGLRenderTarget::kCantResolve_ResolveType:
1544 return false;
1545 case GrGLRenderTarget::kAutoResolves_ResolveType:
1546 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1547 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001548 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001549 break;
1550 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001551 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001552 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001553 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1554 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001555 break;
1556 default:
1557 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001558 }
1559
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001560 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001561
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001562 // the read rect is viewport-relative
1563 GrGLIRect readRect;
1564 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001565
1566 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1567 if (0 == rowBytes) {
1568 rowBytes = tightRowBytes;
1569 }
1570 size_t readDstRowBytes = tightRowBytes;
1571 void* readDst = buffer;
1572
1573 // determine if GL can read using the passed rowBytes or if we need
1574 // a scratch buffer.
1575 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1576 if (rowBytes != tightRowBytes) {
1577 if (kDesktop_GrGLBinding == this->glBinding()) {
1578 GrAssert(!(rowBytes % sizeof(GrColor)));
1579 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1580 readDstRowBytes = rowBytes;
1581 } else {
1582 scratch.reset(tightRowBytes * height);
1583 readDst = scratch.get();
1584 }
1585 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001586 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1587 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001588 format, type, readDst));
1589 if (readDstRowBytes != tightRowBytes) {
1590 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1591 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001592
1593 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001594 // API presents top-to-bottom. We must preserve the padding contents. Note
1595 // that the above readPixels did not overwrite the padding.
1596 if (readDst == buffer) {
1597 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001598 if (!invertY) {
1599 scratch.reset(tightRowBytes);
1600 void* tmpRow = scratch.get();
1601 // flip y in-place by rows
1602 const int halfY = height >> 1;
1603 char* top = reinterpret_cast<char*>(buffer);
1604 char* bottom = top + (height - 1) * rowBytes;
1605 for (int y = 0; y < halfY; y++) {
1606 memcpy(tmpRow, top, tightRowBytes);
1607 memcpy(top, bottom, tightRowBytes);
1608 memcpy(bottom, tmpRow, tightRowBytes);
1609 top += rowBytes;
1610 bottom -= rowBytes;
1611 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001612 }
1613 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001614 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001615 // copy from readDst to buffer while flipping y
1616 const int halfY = height >> 1;
1617 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001618 char* dst = reinterpret_cast<char*>(buffer);
1619 if (!invertY) {
1620 dst += (height-1) * rowBytes;
1621 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001622 for (int y = 0; y < height; y++) {
1623 memcpy(dst, src, tightRowBytes);
1624 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001625 if (invertY) {
1626 dst += rowBytes;
1627 } else {
1628 dst -= rowBytes;
1629 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 }
1631 }
1632 return true;
1633}
1634
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001635void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001636
1637 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1638
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001639 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001641 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 #if GR_COLLECT_STATS
1643 ++fStats.fRenderTargetChngCnt;
1644 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001645 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001646 GrGLenum status;
1647 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001648 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001649 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001650 }
1651 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001652 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001653 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001654 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001655 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001656 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 fHWBounds.fViewportRect = vp;
1658 }
1659 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001660 if (NULL == bound || !bound->isEmpty()) {
1661 rt->flagAsNeedingResolve(bound);
1662 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001663}
1664
twiz@google.com0f31ca72011-03-18 17:38:11 +00001665GrGLenum gPrimitiveType2GLMode[] = {
1666 GR_GL_TRIANGLES,
1667 GR_GL_TRIANGLE_STRIP,
1668 GR_GL_TRIANGLE_FAN,
1669 GR_GL_POINTS,
1670 GR_GL_LINES,
1671 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001672};
1673
bsalomon@google.comd302f142011-03-03 13:54:13 +00001674#define SWAP_PER_DRAW 0
1675
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001676#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001677 #if GR_MAC_BUILD
1678 #include <AGL/agl.h>
1679 #elif GR_WIN32_BUILD
1680 void SwapBuf() {
1681 DWORD procID = GetCurrentProcessId();
1682 HWND hwnd = GetTopWindow(GetDesktopWindow());
1683 while(hwnd) {
1684 DWORD wndProcID = 0;
1685 GetWindowThreadProcessId(hwnd, &wndProcID);
1686 if(wndProcID == procID) {
1687 SwapBuffers(GetDC(hwnd));
1688 }
1689 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1690 }
1691 }
1692 #endif
1693#endif
1694
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001695void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1696 uint32_t startVertex,
1697 uint32_t startIndex,
1698 uint32_t vertexCount,
1699 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001700 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1701
twiz@google.com0f31ca72011-03-18 17:38:11 +00001702 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001703
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001704 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1705 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1706
1707 // our setupGeometry better have adjusted this to zero since
1708 // DrawElements always draws from the begining of the arrays for idx 0.
1709 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001710
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001711 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1712 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001713#if SWAP_PER_DRAW
1714 glFlush();
1715 #if GR_MAC_BUILD
1716 aglSwapBuffers(aglGetCurrentContext());
1717 int set_a_break_pt_here = 9;
1718 aglSwapBuffers(aglGetCurrentContext());
1719 #elif GR_WIN32_BUILD
1720 SwapBuf();
1721 int set_a_break_pt_here = 9;
1722 SwapBuf();
1723 #endif
1724#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001725}
1726
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001727void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1728 uint32_t startVertex,
1729 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1731
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001732 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1733
1734 // our setupGeometry better have adjusted this to zero.
1735 // DrawElements doesn't take an offset so we always adjus the startVertex.
1736 GrAssert(0 == startVertex);
1737
1738 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1739 // account for startVertex in the DrawElements case. So we always
1740 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001741 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001742#if SWAP_PER_DRAW
1743 glFlush();
1744 #if GR_MAC_BUILD
1745 aglSwapBuffers(aglGetCurrentContext());
1746 int set_a_break_pt_here = 9;
1747 aglSwapBuffers(aglGetCurrentContext());
1748 #elif GR_WIN32_BUILD
1749 SwapBuf();
1750 int set_a_break_pt_here = 9;
1751 SwapBuf();
1752 #endif
1753#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001754}
1755
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001756void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001757
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001758 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001759 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001760 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001761 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1762 rt->renderFBOID()));
1763 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1764 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 #if GR_COLLECT_STATS
1766 ++fStats.fRenderTargetChngCnt;
1767 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001768 // make sure we go through flushRenderTarget() since we've modified
1769 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001771 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001772 const GrIRect dirtyRect = rt->getResolveRect();
1773 GrGLIRect r;
1774 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1775 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001776
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001777 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001778 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001779 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1780 GL_CALL(Scissor(r.fLeft, r.fBottom,
1781 r.fWidth, r.fHeight));
1782 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001783 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001784 fHWBounds.fScissorEnabled = true;
1785 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001786 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001787 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001788 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1789 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001790 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001791 int right = r.fLeft + r.fWidth;
1792 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001793 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1794 r.fLeft, r.fBottom, right, top,
1795 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001796 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001797 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001798 }
1799}
1800
twiz@google.com0f31ca72011-03-18 17:38:11 +00001801static const GrGLenum grToGLStencilFunc[] = {
1802 GR_GL_ALWAYS, // kAlways_StencilFunc
1803 GR_GL_NEVER, // kNever_StencilFunc
1804 GR_GL_GREATER, // kGreater_StencilFunc
1805 GR_GL_GEQUAL, // kGEqual_StencilFunc
1806 GR_GL_LESS, // kLess_StencilFunc
1807 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1808 GR_GL_EQUAL, // kEqual_StencilFunc,
1809 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001810};
1811GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1812GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1813GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1814GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1815GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1816GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1817GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1818GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1819GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1820
twiz@google.com0f31ca72011-03-18 17:38:11 +00001821static const GrGLenum grToGLStencilOp[] = {
1822 GR_GL_KEEP, // kKeep_StencilOp
1823 GR_GL_REPLACE, // kReplace_StencilOp
1824 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1825 GR_GL_INCR, // kIncClamp_StencilOp
1826 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1827 GR_GL_DECR, // kDecClamp_StencilOp
1828 GR_GL_ZERO, // kZero_StencilOp
1829 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830};
1831GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1832GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1833GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1834GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1835GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1836GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1837GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1838GR_STATIC_ASSERT(6 == kZero_StencilOp);
1839GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1840
reed@google.comac10a2d2010-12-22 21:39:39 +00001841void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001842 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001843
1844 // use stencil for clipping if clipping is enabled and the clip
1845 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001846 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001847 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001848 bool stencilChange = fHWStencilClip != stencilClip ||
1849 fHWDrawState.fStencilSettings != *settings ||
1850 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1851 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001852
1853 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001854
bsalomon@google.comd302f142011-03-03 13:54:13 +00001855 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1856 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001857
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858 if (settings->isDisabled()) {
1859 if (stencilClip) {
1860 settings = &gClipStencilSettings;
1861 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001862 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863
1864 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001865 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001867 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001868 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001869 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001870 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1871 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1872 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1873 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1874 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1875 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1876 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1877 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1878 }
1879 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001880 int stencilBits = 0;
1881 GrStencilBuffer* stencilBuffer =
1882 fCurrDrawState.fRenderTarget->getStencilBuffer();
1883 if (NULL != stencilBuffer) {
1884 stencilBits = stencilBuffer->bits();
1885 }
1886 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887 GrAssert(stencilBits ||
1888 (GrStencilSettings::gDisabled ==
1889 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001890 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1891 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001892
1893 unsigned int frontRef = settings->fFrontFuncRef;
1894 unsigned int frontMask = settings->fFrontFuncMask;
1895 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001896 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897
1898 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1899
1900 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1901 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1902 } else {
1903 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1904
1905 ConvertStencilFuncAndMask(settings->fFrontFunc,
1906 stencilClip,
1907 clipStencilMask,
1908 userStencilMask,
1909 &frontRef,
1910 &frontMask);
1911 frontWriteMask &= userStencilMask;
1912 }
1913 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001914 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001915 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001916 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001917 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001918 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001919 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001920 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001921 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001922 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923
1924 unsigned int backRef = settings->fBackFuncRef;
1925 unsigned int backMask = settings->fBackFuncMask;
1926 unsigned int backWriteMask = settings->fBackWriteMask;
1927
1928
1929 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1930 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1931 backFunc = grToGLStencilFunc[settings->fBackFunc];
1932 } else {
1933 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1934 ConvertStencilFuncAndMask(settings->fBackFunc,
1935 stencilClip,
1936 clipStencilMask,
1937 userStencilMask,
1938 &backRef,
1939 &backMask);
1940 backWriteMask &= userStencilMask;
1941 }
1942
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001943 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1944 frontRef, frontMask));
1945 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1946 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1947 backRef, backMask));
1948 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1949 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1950 grToGLStencilOp[settings->fFrontFailOp],
1951 grToGLStencilOp[settings->fFrontPassOp],
1952 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001953
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001954 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1955 grToGLStencilOp[settings->fBackFailOp],
1956 grToGLStencilOp[settings->fBackPassOp],
1957 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001958 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001959 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1960 GL_CALL(StencilMask(frontWriteMask));
1961 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001962 grToGLStencilOp[settings->fFrontPassOp],
1963 grToGLStencilOp[settings->fFrontPassOp]));
1964 }
1965 }
1966 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001967 fHWStencilClip = stencilClip;
1968 }
1969}
1970
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001971void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001972 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001973 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1974 // smooth lines.
1975
1976 // we prefer smooth lines over multisampled lines
1977 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001978 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001979 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001980 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001981 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001982 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001983 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001984 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001985 fHWAAState.fSmoothLineEnabled = false;
1986 }
1987 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1988 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001989 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001990 fHWAAState.fMSAAEnabled = false;
1991 }
1992 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001993 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001994 fHWAAState.fMSAAEnabled) {
1995 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001996 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001997 fHWAAState.fMSAAEnabled = false;
1998 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001999 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002000 fHWAAState.fMSAAEnabled = true;
2001 }
2002 }
2003 }
2004}
2005
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002006void GrGpuGL::flushBlend(GrPrimitiveType type,
2007 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002008 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002009 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002010 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002011 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002012 fHWBlendDisabled = false;
2013 }
2014 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
2015 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002016 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2017 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002018 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
2019 fHWDrawState.fDstBlend = kISA_BlendCoeff;
2020 }
2021 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002022 // any optimization to disable blending should
2023 // have already been applied and tweaked the coeffs
2024 // to (1, 0).
2025 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2026 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002027 if (fHWBlendDisabled != blendOff) {
2028 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002029 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002030 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002031 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002032 }
2033 fHWBlendDisabled = blendOff;
2034 }
2035 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002036 if (fHWDrawState.fSrcBlend != srcCoeff ||
2037 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002038 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2039 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002040 fHWDrawState.fSrcBlend = srcCoeff;
2041 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002042 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002043 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2044 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002045 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2046
2047 float c[] = {
2048 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2049 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2050 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2051 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2052 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002053 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002054 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2055 }
2056 }
2057 }
2058}
2059
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002060static unsigned grToGLFilter(GrSamplerState::Filter filter) {
2061 switch (filter) {
2062 case GrSamplerState::kBilinear_Filter:
2063 case GrSamplerState::k4x4Downsample_Filter:
2064 return GR_GL_LINEAR;
2065 case GrSamplerState::kNearest_Filter:
2066 case GrSamplerState::kConvolution_Filter:
2067 return GR_GL_NEAREST;
2068 default:
2069 GrAssert(!"Unknown filter type");
2070 return GR_GL_LINEAR;
2071 }
2072}
2073
bsalomon@google.comffca4002011-02-22 20:34:01 +00002074bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002075
2076 // GrGpu::setupClipAndFlushState should have already checked this
2077 // and bailed if not true.
2078 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002079
tomhudson@google.com93813632011-10-27 20:21:16 +00002080 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002081 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002082 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002083 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002084
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002085 // true for now, but maybe not with GrEffect.
2086 GrAssert(NULL != nextTexture);
2087 // if we created a rt/tex and rendered to it without using a
2088 // texture and now we're texuring from the rt it will still be
2089 // the last bound texture, but it needs resolving. So keep this
2090 // out of the "last != next" check.
2091 GrGLRenderTarget* texRT =
2092 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2093 if (NULL != texRT) {
2094 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002095 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002096
2097 if (fHWDrawState.fTextures[s] != nextTexture) {
2098 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002099 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002100 #if GR_COLLECT_STATS
2101 ++fStats.fTextureChngCnt;
2102 #endif
2103 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2104 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002105 // The texture matrix has to compensate for texture width/height
2106 // and NPOT-embedded-in-POT
2107 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002108 }
2109
2110 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002111 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002112 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002113 nextTexture->getCachedTexParams(&timestamp);
2114 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002115 GrGLTexture::TexParams newTexParams;
2116
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002117 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002118
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002119 const GrGLenum* wraps =
2120 GrGLTexture::WrapMode2GLWrap(this->glBinding());
2121 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2122 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002123 if (setAll) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002124 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002125 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2126 GR_GL_TEXTURE_MAG_FILTER,
2127 newTexParams.fFilter));
2128 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2129 GR_GL_TEXTURE_MIN_FILTER,
2130 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002131 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2132 GR_GL_TEXTURE_WRAP_S,
2133 newTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002134 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2135 GR_GL_TEXTURE_WRAP_T,
2136 newTexParams.fWrapT));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002137 if (this->glCaps().fTextureSwizzle) {
2138 set_tex_swizzle(nextTexture->config(), this->glInterface());
2139 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002140 } else {
2141 if (newTexParams.fFilter != oldTexParams.fFilter) {
2142 setTextureUnit(s);
2143 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2144 GR_GL_TEXTURE_MAG_FILTER,
2145 newTexParams.fFilter));
2146 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2147 GR_GL_TEXTURE_MIN_FILTER,
2148 newTexParams.fFilter));
2149 }
2150 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2151 setTextureUnit(s);
2152 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2153 GR_GL_TEXTURE_WRAP_S,
2154 newTexParams.fWrapS));
2155 }
2156 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2157 setTextureUnit(s);
2158 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2159 GR_GL_TEXTURE_WRAP_T,
2160 newTexParams.fWrapT));
2161 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002162 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002163 nextTexture->setCachedTexParams(newTexParams,
2164 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002165 }
2166 }
2167
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002168 GrIRect* rect = NULL;
2169 GrIRect clipBounds;
2170 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2171 fClip.hasConservativeBounds()) {
2172 fClip.getConservativeBounds().roundOut(&clipBounds);
2173 rect = &clipBounds;
2174 }
2175 this->flushRenderTarget(rect);
2176 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002177
reed@google.comac10a2d2010-12-22 21:39:39 +00002178 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2179 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2180 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002181 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002182 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002183 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002184 }
2185 }
2186
bsalomon@google.comd302f142011-03-03 13:54:13 +00002187 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2188 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002189 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002190 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002191 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002192 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002193 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002194 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002195 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002196 }
2197
bsalomon@google.comd302f142011-03-03 13:54:13 +00002198 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2199 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002200 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002201 GL_CALL(Enable(GR_GL_CULL_FACE));
2202 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002203 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002204 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002205 GL_CALL(Enable(GR_GL_CULL_FACE));
2206 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002207 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002208 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002209 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002210 break;
2211 default:
2212 GrCrash("Unknown draw face.");
2213 }
2214 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2215 }
2216
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002217#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002218 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002219 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002220 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002221 NULL == fCurrDrawState.fRenderTarget ||
2222 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002223 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002224 fCurrDrawState.fRenderTarget);
2225 }
2226#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002227
reed@google.comac10a2d2010-12-22 21:39:39 +00002228 flushStencil();
2229
bsalomon@google.comd302f142011-03-03 13:54:13 +00002230 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002231 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002232 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002233}
2234
2235void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002236 if (fHWGeometryState.fVertexBuffer != buffer) {
2237 fHWGeometryState.fArrayPtrsDirty = true;
2238 fHWGeometryState.fVertexBuffer = buffer;
2239 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002240}
2241
2242void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002243 if (fHWGeometryState.fVertexBuffer == buffer) {
2244 // deleting bound buffer does implied bind to 0
2245 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002246 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002247 }
2248}
2249
2250void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002251 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002252}
2253
2254void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 if (fHWGeometryState.fIndexBuffer == buffer) {
2256 // deleting bound buffer does implied bind to 0
2257 fHWGeometryState.fIndexBuffer = NULL;
2258 }
2259}
2260
reed@google.comac10a2d2010-12-22 21:39:39 +00002261void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2262 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002263 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002264 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002265 }
2266 if (fHWDrawState.fRenderTarget == renderTarget) {
2267 fHWDrawState.fRenderTarget = NULL;
2268 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002269}
2270
2271void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002272 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002273 if (fCurrDrawState.fTextures[s] == texture) {
2274 fCurrDrawState.fTextures[s] = NULL;
2275 }
2276 if (fHWDrawState.fTextures[s] == texture) {
2277 // deleting bound texture does implied bind to 0
2278 fHWDrawState.fTextures[s] = NULL;
2279 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002281}
2282
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002283bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002284 GrGLenum* internalFormat,
2285 GrGLenum* format,
2286 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002287 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002288 case kRGBA_8888_PM_GrPixelConfig:
2289 case kRGBA_8888_UPM_GrPixelConfig:
2290 *format = GR_GL_RGBA;
2291 *internalFormat = GR_GL_RGBA;
2292 *type = GR_GL_UNSIGNED_BYTE;
2293 break;
2294 case kBGRA_8888_PM_GrPixelConfig:
2295 case kBGRA_8888_UPM_GrPixelConfig:
2296 if (!fGLCaps.fBGRAFormat) {
2297 return false;
2298 }
2299 *format = GR_GL_BGRA;
2300 if (fGLCaps.fBGRAInternalFormat) {
2301 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002302 } else {
2303 *internalFormat = GR_GL_RGBA;
2304 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002305 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002306 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002307 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002308 *format = GR_GL_RGB;
2309 *internalFormat = GR_GL_RGB;
2310 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002311 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002312 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002313 *format = GR_GL_RGBA;
2314 *internalFormat = GR_GL_RGBA;
2315 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002316 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002317 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002318 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002319 *format = GR_GL_PALETTE8_RGBA8;
2320 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002321 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002322 } else {
2323 return false;
2324 }
2325 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002326 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002327 *format = GR_GL_ALPHA;
2328 *internalFormat = GR_GL_ALPHA;
2329 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002330 break;
2331 default:
2332 return false;
2333 }
2334 return true;
2335}
2336
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002337void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002338 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002339 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002340 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002341 fActiveTextureUnitIdx = unit;
2342 }
2343}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002344
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002345void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002346 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002347 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002348 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2349 }
2350}
2351
reed@google.comac10a2d2010-12-22 21:39:39 +00002352/* On ES the internalFormat and format must match for TexImage and we use
2353 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2354 decide the internalFormat. However, on ES internalFormat for
2355 RenderBufferStorage* has to be a specific format (not a base format like
2356 GL_RGBA).
2357 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002358bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002359 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002360 // The ES story for BGRA and RenderbufferStorage appears murky. It
2361 // takes an internal format as a parameter. The OES FBO extension and
2362 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2363 // BGRA extensions adds BGRA as both an internal and external format
2364 // and the other only as an external format (like desktop GL). OES
2365 // restricts RenderbufferStorage's format to a *sized* internal format.
2366 // There is no sized BGRA internal format.
2367 // So if the texture has internal format BGRA we just hope that the
2368 // resolve blit can do RGBA->BGRA internal format conversion.
2369 case kRGBA_8888_PM_GrPixelConfig:
2370 case kRGBA_8888_UPM_GrPixelConfig:
2371 case kBGRA_8888_PM_GrPixelConfig:
2372 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002373 if (fGLCaps.fRGBA8Renderbuffer) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002374 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2375 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002376 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002377 return true;
2378 } else {
2379 return false;
2380 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002381 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002382 // ES2 supports 565. ES1 supports it
2383 // with FBO extension desktop GL has
2384 // no such internal format
bsalomon@google.comc4364992011-11-07 15:54:49 +00002385 if (kDesktop_GrGLBinding != this->glBinding()) {
2386 *format = GR_GL_RGB565;
2387 return true;
2388 } else {
2389 return false;
2390 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002391 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002392 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002393 return true;
2394 default:
2395 return false;
2396 }
2397}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002398
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002399void GrGpuGL::resetDirtyFlags() {
2400 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2401}
2402
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002403void GrGpuGL::setBuffers(bool indexed,
2404 int* extraVertexOffset,
2405 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002406
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002407 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002408
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002409 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2410
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002411 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002412 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002413 case kBuffer_GeometrySrcType:
2414 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002415 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002416 break;
2417 case kArray_GeometrySrcType:
2418 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002419 this->finalizeReservedVertices();
2420 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2421 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002422 break;
2423 default:
2424 vbuf = NULL; // suppress warning
2425 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002426 }
2427
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002428 GrAssert(NULL != vbuf);
2429 GrAssert(!vbuf->isLocked());
2430 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002431 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002432 fHWGeometryState.fArrayPtrsDirty = true;
2433 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002434 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002435
2436 if (indexed) {
2437 GrAssert(NULL != extraIndexOffset);
2438
2439 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002440 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002441 case kBuffer_GeometrySrcType:
2442 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002443 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002444 break;
2445 case kArray_GeometrySrcType:
2446 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002447 this->finalizeReservedIndices();
2448 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2449 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002450 break;
2451 default:
2452 ibuf = NULL; // suppress warning
2453 GrCrash("Unknown geometry src type!");
2454 }
2455
2456 GrAssert(NULL != ibuf);
2457 GrAssert(!ibuf->isLocked());
2458 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002459 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460 fHWGeometryState.fIndexBuffer = ibuf;
2461 }
2462 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002463}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002464
2465int GrGpuGL::getMaxEdges() const {
2466 // FIXME: This is a pessimistic estimate based on how many other things
2467 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002468 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2469 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002470}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002471
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002472void GrGpuGL::GLCaps::print() const {
2473 for (int i = 0; i < fStencilFormats.count(); ++i) {
2474 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2475 i,
2476 fStencilFormats[i].fStencilBits,
2477 fStencilFormats[i].fTotalBits);
2478 }
2479
2480 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2481 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2482 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2483 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2484 static const char* gMSFBOExtStr[] = {
2485 "None",
2486 "ARB",
2487 "EXT",
2488 "Apple",
2489 };
2490 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002491 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002492 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2493 }
2494 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2495 GrPrintf("Support RGBA8 Render Buffer: %s\n",
2496 (fRGBA8Renderbuffer ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002497 GrPrintf("BGRA is an internal format: %s\n",
2498 (fBGRAInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002499 GrPrintf("Support texture swizzle: %s\n",
2500 (fTextureSwizzle ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002501}