blob: d768a24f89041a13c2a7f9a8ec25456039d4e6ef [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.com0a97be22011-11-08 19:20:57 +0000579GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) {
580 if (GR_GL_RGBA_8888_READBACK_SLOW && GrPixelConfigIsRGBA8888(config)) {
581 return GrPixelConfigSwapRAndB(config);
582 } else {
583 return config;
584 }
585}
586
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000587void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000588 if (gPrintStartupSpew && !fPrintedCaps) {
589 fPrintedCaps = true;
590 this->getCaps().print();
591 fGLCaps.print();
592 }
593
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000594 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000595 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000596 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000597
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000598 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000599 GL_CALL(Disable(GR_GL_DEPTH_TEST));
600 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000601
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000602 GL_CALL(Disable(GR_GL_CULL_FACE));
603 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000604 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000606 GL_CALL(Disable(GR_GL_DITHER));
607 if (kDesktop_GrGLBinding == this->glBinding()) {
608 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
609 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
610 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000611 fHWAAState.fMSAAEnabled = false;
612 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000613 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000614
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000615 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000616 fHWDrawState.fFlagBits = 0;
617
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000619 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000620
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000621 // invalid
622 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000623
reed@google.comac10a2d2010-12-22 21:39:39 +0000624 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000625 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
626 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000627
628 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000629 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000630
reed@google.comac10a2d2010-12-22 21:39:39 +0000631 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000632
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000633 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000634
tomhudson@google.com93813632011-10-27 20:21:16 +0000635 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000636 fHWDrawState.fTextures[s] = NULL;
637 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
638 -GR_ScalarMax,
639 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000640 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000641 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000642 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000643
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000644 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000645 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000646 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000647 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000648
bsalomon@google.comd302f142011-03-03 13:54:13 +0000649 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000650 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000651 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000652
653 fHWGeometryState.fIndexBuffer = NULL;
654 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000655
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000656 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000657
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000658 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000659 fHWDrawState.fRenderTarget = NULL;
660}
661
bsalomon@google.come269f212011-11-07 13:29:52 +0000662GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
663 GrGLenum internalFormat; // we don't need this value
664 GrGLTexture::Desc glTexDesc;
665 if (!this->canBeTexture(desc.fConfig, &internalFormat,
666 &glTexDesc.fUploadFormat, &glTexDesc.fUploadType)) {
667 return NULL;
668 }
669
670 glTexDesc.fContentWidth = glTexDesc.fAllocWidth = desc.fWidth;
671 glTexDesc.fContentHeight = glTexDesc.fAllocHeight = desc.fHeight;
672 glTexDesc.fConfig = desc.fConfig;
673 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
674 glTexDesc.fOwnsID = false;
675 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
676
677 GrGLTexture* texture = NULL;
678 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
679 GrGLRenderTarget::Desc glRTDesc;
680 glRTDesc.fRTFBOID = 0;
681 glRTDesc.fTexFBOID = 0;
682 glRTDesc.fMSColorRenderbufferID = 0;
683 glRTDesc.fOwnIDs = true;
684 glRTDesc.fConfig = desc.fConfig;
685 glRTDesc.fSampleCnt = desc.fSampleCnt;
686 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
687 glTexDesc.fAllocHeight,
688 glTexDesc.fTextureID,
689 &glRTDesc)) {
690 return NULL;
691 }
692 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
693 } else {
694 texture = new GrGLTexture(this, glTexDesc);
695 }
696 if (NULL == texture) {
697 return NULL;
698 }
699
700 this->setSpareTextureUnit();
701 return texture;
702}
703
704GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
705 GrGLRenderTarget::Desc glDesc;
706 glDesc.fConfig = desc.fConfig;
707 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
708 glDesc.fMSColorRenderbufferID = 0;
709 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
710 glDesc.fSampleCnt = desc.fSampleCnt;
711 glDesc.fOwnIDs = false;
712 GrGLIRect viewport;
713 viewport.fLeft = 0;
714 viewport.fBottom = 0;
715 viewport.fWidth = desc.fWidth;
716 viewport.fHeight = desc.fHeight;
717
718 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
719 if (desc.fStencilBits) {
720 GrGLStencilBuffer::Format format;
721 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
722 format.fPacked = false;
723 format.fStencilBits = desc.fStencilBits;
724 format.fTotalBits = desc.fStencilBits;
725 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
726 0,
727 desc.fWidth,
728 desc.fHeight,
729 desc.fSampleCnt,
730 format);
731 tgt->setStencilBuffer(sb);
732 sb->unref();
733 }
734 return tgt;
735}
736
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000737GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
738
739 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
740 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
741 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
742 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
743
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000744 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000745 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000746
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000747 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000748 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000749 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000750 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000751 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000752 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000753 } else {
754 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000755 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000756 }
757 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000758 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000759 }
760 // we don't know what the RB ids are without glGets and we don't care
761 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000762 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000763 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000764 if (desc.fStencilBits) {
765 GrGLStencilBuffer::Format format;
766 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
767 format.fPacked = false;
768 format.fStencilBits = desc.fStencilBits;
769 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000770 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
771 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000772 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000773 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000774 }
775
776 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000777 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000778 GrGLenum dontCare;
779 if (!canBeTexture(desc.fConfig, &dontCare,
780 &texDesc.fUploadFormat,
781 &texDesc.fUploadType)) {
782 return NULL;
783 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000784 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
785 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
786
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000787 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000788 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000789 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000790 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000791
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000792 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000793 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000794 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000795 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000796 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000797 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000798 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000799 } else {
800 GrGLIRect viewport;
801 viewport.fLeft = 0;
802 viewport.fBottom = 0;
803 viewport.fWidth = desc.fWidth;
804 viewport.fHeight = desc.fHeight;
805
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000806 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000807 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000808 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000809 }
810}
811
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000812
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000813////////////////////////////////////////////////////////////////////////////////
814
815void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
816 GrGLenum internalFormat,
817 const void* data,
818 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000819 // we assume the texture is bound
820
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000821 size_t bpp = GrBytesPerPixel(desc.fConfig);
822 size_t trimRowBytes = desc.fContentWidth * bpp;
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000823
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000824 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000825 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000826 }
827
828 // in case we need a temporary, trimmed copy of the src pixels
829 SkAutoSMalloc<128 * 128> tempStorage;
830
831 /*
832 * check whether to allocate a temporary buffer for flipping y or
833 * because our data has extra bytes past each row. If so, we need
834 * to trim those off here, since GL ES doesn't let us specify
835 * GL_UNPACK_ROW_LENGTH.
836 */
837 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000838 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000839 if (data && rowBytes != trimRowBytes) {
840 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
841 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000842 }
843 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000844 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000845 // copy the data into our new storage, skipping the trailing bytes
846 size_t trimSize = desc.fContentHeight * trimRowBytes;
847 const char* src = (const char*)data;
848 if (flipY) {
849 src += (desc.fContentHeight - 1) * rowBytes;
850 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000851 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000852 for (int y = 0; y < desc.fContentHeight; y++) {
853 memcpy(dst, src, trimRowBytes);
854 if (flipY) {
855 src -= rowBytes;
856 } else {
857 src += rowBytes;
858 }
859 dst += trimRowBytes;
860 }
861 // now point data to our trimmed version
862 data = tempStorage.get();
863 rowBytes = trimRowBytes;
864 }
865 }
866
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000867 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000868 if (kIndex_8_GrPixelConfig == desc.fConfig &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000869 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000870 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
871 GrAssert(desc.fContentWidth == desc.fAllocWidth);
872 GrAssert(desc.fContentHeight == desc.fAllocHeight);
873 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
874 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000875 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
876 desc.fAllocWidth, desc.fAllocHeight,
877 0, imageSize, data));
878 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000879 } else {
880 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
881 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000882 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
883 desc.fAllocWidth, desc.fAllocHeight,
884 0, desc.fUploadFormat, desc.fUploadType, NULL));
885 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
886 desc.fContentHeight, desc.fUploadFormat,
887 desc.fUploadType, data));
888 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000889
890 int extraW = desc.fAllocWidth - desc.fContentWidth;
891 int extraH = desc.fAllocHeight - desc.fContentHeight;
892 int maxTexels = extraW * extraH;
893 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
894 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
895
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000896 SkAutoSMalloc<128*128> texels(bpp * maxTexels);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000897
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000898 if (extraH) {
899 uint8_t* lastRowStart = (uint8_t*) data +
900 (desc.fContentHeight - 1) * rowBytes;
901 uint8_t* extraRowStart = (uint8_t*)texels.get();
902
903 for (int i = 0; i < extraH; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000904 memcpy(extraRowStart, lastRowStart, trimRowBytes);
905 extraRowStart += trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000906 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000907 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
908 desc.fContentHeight, desc.fContentWidth,
909 extraH, desc.fUploadFormat,
910 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000911 }
912 if (extraW) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000913 uint8_t* edgeTexel = (uint8_t*)data + trimRowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000914 uint8_t* extraTexel = (uint8_t*)texels.get();
915 for (int j = 0; j < desc.fContentHeight; ++j) {
916 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000917 memcpy(extraTexel, edgeTexel, bpp);
918 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000919 }
920 edgeTexel += rowBytes;
921 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
923 0, extraW, desc.fContentHeight,
924 desc.fUploadFormat, desc.fUploadType,
925 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000926 }
927 if (extraW && extraH) {
928 uint8_t* cornerTexel = (uint8_t*)data +
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000929 desc.fContentHeight * rowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000930 uint8_t* extraTexel = (uint8_t*)texels.get();
931 for (int i = 0; i < extraW*extraH; ++i) {
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000932 memcpy(extraTexel, cornerTexel, bpp);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000933 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000934 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
936 desc.fContentHeight, extraW, extraH,
937 desc.fUploadFormat, desc.fUploadType,
938 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000939 }
940
941 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
943 desc.fAllocWidth, desc.fAllocHeight, 0,
944 desc.fUploadFormat, desc.fUploadType, data));
945 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000946 }
947 }
948}
949
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950bool GrGpuGL::createRenderTargetObjects(int width, int height,
951 GrGLuint texID,
952 GrGLRenderTarget::Desc* desc) {
953 desc->fMSColorRenderbufferID = 0;
954 desc->fRTFBOID = 0;
955 desc->fTexFBOID = 0;
956 desc->fOwnIDs = true;
957
958 GrGLenum status;
959 GrGLint err;
960
bsalomon@google.comab15d612011-08-09 12:57:56 +0000961 GrGLenum msColorFormat = 0; // suppress warning
962
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000963 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000964 if (!desc->fTexFBOID) {
965 goto FAILED;
966 }
967
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000968
969 // If we are using multisampling we will create two FBOS. We render
970 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000971 if (desc->fSampleCnt > 0) {
972 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
973 goto FAILED;
974 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000975 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
976 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000977 if (!desc->fRTFBOID ||
978 !desc->fMSColorRenderbufferID ||
979 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
980 goto FAILED;
981 }
982 } else {
983 desc->fRTFBOID = desc->fTexFBOID;
984 }
985
986 if (desc->fRTFBOID != desc->fTexFBOID) {
987 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000988 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000989 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000990 GR_GL_CALL_NOERRCHECK(this->glInterface(),
991 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
992 desc->fSampleCnt,
993 msColorFormat,
994 width, height));
995 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000996 if (err != GR_GL_NO_ERROR) {
997 goto FAILED;
998 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000999 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
1000 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001001 GR_GL_COLOR_ATTACHMENT0,
1002 GR_GL_RENDERBUFFER,
1003 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001004 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001005 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1006 goto FAILED;
1007 }
1008 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001009 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001010
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001011 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1012 GR_GL_COLOR_ATTACHMENT0,
1013 GR_GL_TEXTURE_2D,
1014 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001015 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001016 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1017 goto FAILED;
1018 }
1019
1020 return true;
1021
1022FAILED:
1023 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001024 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001025 }
1026 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001028 }
1029 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001030 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001031 }
1032 return false;
1033}
1034
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001035// good to set a break-point here to know when createTexture fails
1036static GrTexture* return_null_texture() {
1037// GrAssert(!"null texture");
1038 return NULL;
1039}
1040
1041#if GR_DEBUG
1042static size_t as_size_t(int x) {
1043 return x;
1044}
1045#endif
1046
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001047GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001048 const void* srcData,
1049 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001050
1051#if GR_COLLECT_STATS
1052 ++fStats.fTextureCreateCnt;
1053#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001054
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001055 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001056 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001057 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001058
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001059 glTexDesc.fContentWidth = desc.fWidth;
1060 glTexDesc.fContentHeight = desc.fHeight;
1061 glTexDesc.fAllocWidth = desc.fWidth;
1062 glTexDesc.fAllocHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001063 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001064 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001065
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001066 glRTDesc.fMSColorRenderbufferID = 0;
1067 glRTDesc.fRTFBOID = 0;
1068 glRTDesc.fTexFBOID = 0;
1069 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001070 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001071
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001072 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001073 if (!canBeTexture(desc.fConfig,
reed@google.comac10a2d2010-12-22 21:39:39 +00001074 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001075 &glTexDesc.fUploadFormat,
1076 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 return return_null_texture();
1078 }
1079
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001080 const Caps& caps = this->getCaps();
1081
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001082 // We keep GrRenderTargets in GL's normal orientation so that they
1083 // can be drawn to by the outside world without the client having
1084 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001085 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001087
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001088 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1089 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1090 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1091 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001092 GrPrintf("AA RT requested but not supported on this platform.");
1093 }
1094
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001096 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001097 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1098 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001099 }
1100
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001101 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001102 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001103 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001104 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001105 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1106 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001107 return return_null_texture();
1108 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001109 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001110 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1111 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001112 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1113 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001114 return return_null_texture();
1115 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001116 }
1117
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001119 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001120 return return_null_texture();
1121 }
1122
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001123 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001124 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001125
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001126 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1127 // drivers have a bug where an FBO won't be complete if it includes a
1128 // texture that is not mipmap complete (considering the filter in use).
1129 GrGLTexture::TexParams initialTexParams;
1130 // we only set a subset here so invalidate first
1131 initialTexParams.invalidate();
1132 initialTexParams.fFilter = GR_GL_NEAREST;
1133 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1134 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1136 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001137 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001138 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1139 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001140 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001141 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1142 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001143 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1145 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001146 initialTexParams.fWrapT));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001147 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001148
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001150 if (renderTarget) {
1151#if GR_COLLECT_STATS
1152 ++fStats.fRenderTargetCreateCnt;
1153#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1155 glTexDesc.fAllocHeight,
1156 glTexDesc.fTextureID,
1157 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001158 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001159 return return_null_texture();
1160 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001161 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001163 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001164 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001165 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001166#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001167 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1168 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001169#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 return tex;
1171}
1172
1173namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001174void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1175 GrGLuint rb,
1176 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 // we shouldn't ever know one size and not the other
1178 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1179 (kUnknownBitCount == format->fTotalBits));
1180 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001181 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1183 (GrGLint*)&format->fStencilBits);
1184 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001185 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001186 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1187 (GrGLint*)&format->fTotalBits);
1188 format->fTotalBits += format->fStencilBits;
1189 } else {
1190 format->fTotalBits = format->fStencilBits;
1191 }
1192 }
1193}
1194}
1195
1196bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1197 int width, int height) {
1198
1199 // All internally created RTs are also textures. We don't create
1200 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1201 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001202 GrAssert(width >= rt->allocatedWidth());
1203 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204
1205 int samples = rt->numSamples();
1206 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001207 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 if (!sbID) {
1209 return false;
1210 }
1211
1212 GrGLStencilBuffer* sb = NULL;
1213
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001214 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 // we start with the last stencil format that succeeded in hopes
1218 // that we won't go through this loop more than once after the
1219 // first (painful) stencil creation.
1220 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001221 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001222 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 // version on a GL that doesn't have an MSAA extension.
1224 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1226 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 GR_GL_RENDERBUFFER,
1228 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001229 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001230 width,
1231 height));
1232 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001233 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1234 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001235 sFmt.fInternalFormat,
1236 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237 }
1238
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001239 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 if (err == GR_GL_NO_ERROR) {
1241 // After sized formats we attempt an unsized format and take whatever
1242 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001243 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001244 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001245 sb = new GrGLStencilBuffer(this, sbID, width, height,
1246 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1248 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001249 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 return true;
1252 }
1253 sb->abandon(); // otherwise we lose sbID
1254 sb->unref();
1255 }
1256 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001257 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001258 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001259}
1260
1261bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1262 GrRenderTarget* rt) {
1263 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1264
1265 GrGLuint fbo = glrt->renderFBOID();
1266
1267 if (NULL == sb) {
1268 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001269 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001270 GR_GL_STENCIL_ATTACHMENT,
1271 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001273 GR_GL_DEPTH_ATTACHMENT,
1274 GR_GL_RENDERBUFFER, 0));
1275#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001276 GrGLenum status;
1277 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001278 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1279#endif
1280 }
1281 return true;
1282 } else {
1283 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1284 GrGLuint rb = glsb->renderbufferID();
1285
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001287 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1288 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001289 GR_GL_STENCIL_ATTACHMENT,
1290 GR_GL_RENDERBUFFER, rb));
1291 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001292 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001293 GR_GL_DEPTH_ATTACHMENT,
1294 GR_GL_RENDERBUFFER, rb));
1295 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001296 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001297 GR_GL_DEPTH_ATTACHMENT,
1298 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001300
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001301 GrGLenum status;
1302 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001303 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001304 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001305 GR_GL_STENCIL_ATTACHMENT,
1306 GR_GL_RENDERBUFFER, 0));
1307 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001308 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001309 GR_GL_DEPTH_ATTACHMENT,
1310 GR_GL_RENDERBUFFER, 0));
1311 }
1312 return false;
1313 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001314 return true;
1315 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001317}
1318
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001319////////////////////////////////////////////////////////////////////////////////
1320
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001321GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001322 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001323 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001325 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001326 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001327 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001329 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1330 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1331 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1332 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1333 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 // deleting bound buffer does implicit bind to 0
1335 fHWGeometryState.fVertexBuffer = NULL;
1336 return NULL;
1337 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001338 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 size, dynamic);
1340 fHWGeometryState.fVertexBuffer = vertexBuffer;
1341 return vertexBuffer;
1342 }
1343 return NULL;
1344}
1345
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001346GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001347 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001348 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001350 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1351 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001353 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1354 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1355 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1356 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1357 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 // deleting bound buffer does implicit bind to 0
1359 fHWGeometryState.fIndexBuffer = NULL;
1360 return NULL;
1361 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001362 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 size, dynamic);
1364 fHWGeometryState.fIndexBuffer = indexBuffer;
1365 return indexBuffer;
1366 }
1367 return NULL;
1368}
1369
reed@google.comac10a2d2010-12-22 21:39:39 +00001370void GrGpuGL::flushScissor(const GrIRect* rect) {
1371 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001372 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001373 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001374
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001375 GrGLIRect scissor;
1376 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001377 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001378 rect->width(), rect->height());
1379 if (scissor.contains(vp)) {
1380 rect = NULL;
1381 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 }
1383
1384 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001385 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001386 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001387 fHWBounds.fScissorRect = scissor;
1388 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001389 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001390 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001391 fHWBounds.fScissorEnabled = true;
1392 }
1393 } else {
1394 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001395 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001396 fHWBounds.fScissorEnabled = false;
1397 }
1398 }
1399}
1400
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001401void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001402 if (NULL == fCurrDrawState.fRenderTarget) {
1403 return;
1404 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001405 GrIRect r;
1406 if (NULL != rect) {
1407 // flushScissor expects rect to be clipped to the target.
1408 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001409 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1410 fCurrDrawState.fRenderTarget->height());
1411 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001412 rect = &r;
1413 } else {
1414 return;
1415 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001416 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001417 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001418 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001419 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001420 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001421 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1422 GrColorUnpackG(color)/255.f,
1423 GrColorUnpackB(color)/255.f,
1424 GrColorUnpackA(color)/255.f));
1425 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001426}
1427
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001428void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001429 if (NULL == fCurrDrawState.fRenderTarget) {
1430 return;
1431 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001432
1433 this->flushRenderTarget(&GrIRect::EmptyIRect());
1434
reed@google.comac10a2d2010-12-22 21:39:39 +00001435 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001436 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001437 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001438 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001439 GL_CALL(StencilMask(0xffffffff));
1440 GL_CALL(ClearStencil(0));
1441 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001442 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001443}
1444
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001445void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001446 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001447
1448 // this should only be called internally when we know we have a
1449 // stencil buffer.
1450 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001451 GrGLint stencilBitCount =
1452 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001453#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001454 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001455 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001456#else
1457 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001458 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001459 // turned into draws. Our contract on GrDrawTarget says that
1460 // changing the clip between stencil passes may or may not
1461 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001462 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001463#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001464 GrGLint value;
1465 if (insideClip) {
1466 value = (1 << (stencilBitCount - 1));
1467 } else {
1468 value = 0;
1469 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001470 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001471 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001472 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001473 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001474 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001475 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001476}
1477
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001478void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001479 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001480}
1481
bsalomon@google.comc4364992011-11-07 15:54:49 +00001482bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1483 int left, int top,
1484 int width, int height,
1485 GrPixelConfig config,
1486 size_t rowBytes) {
1487 if (kDesktop_GrGLBinding == this->glBinding()) {
1488 return false;
1489 } else {
1490 // On ES we'll have to do memcpys to handle rowByte padding. So, we
1491 // might as well flipY while we're at it.
bsalomon@google.comca08edd2011-11-07 16:56:39 +00001492 return 0 == rowBytes ||
1493 GrBytesPerPixel(config) * width == rowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001494 }
1495}
1496
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001497bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498 int left, int top,
1499 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001500 GrPixelConfig config,
1501 void* buffer,
1502 size_t rowBytes,
1503 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001504 GrGLenum internalFormat; // we don't use this for glReadPixels
1505 GrGLenum format;
1506 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001507 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1508 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001509 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001510
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001512 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1513 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1514 switch (tgt->getResolveType()) {
1515 case GrGLRenderTarget::kCantResolve_ResolveType:
1516 return false;
1517 case GrGLRenderTarget::kAutoResolves_ResolveType:
1518 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1519 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001520 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001521 break;
1522 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001523 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001524 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001525 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1526 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001527 break;
1528 default:
1529 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001530 }
1531
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001532 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001533
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001534 // the read rect is viewport-relative
1535 GrGLIRect readRect;
1536 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537
1538 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1539 if (0 == rowBytes) {
1540 rowBytes = tightRowBytes;
1541 }
1542 size_t readDstRowBytes = tightRowBytes;
1543 void* readDst = buffer;
1544
1545 // determine if GL can read using the passed rowBytes or if we need
1546 // a scratch buffer.
1547 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1548 if (rowBytes != tightRowBytes) {
1549 if (kDesktop_GrGLBinding == this->glBinding()) {
1550 GrAssert(!(rowBytes % sizeof(GrColor)));
1551 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1552 readDstRowBytes = rowBytes;
1553 } else {
1554 scratch.reset(tightRowBytes * height);
1555 readDst = scratch.get();
1556 }
1557 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001558 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1559 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001560 format, type, readDst));
1561 if (readDstRowBytes != tightRowBytes) {
1562 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1563 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001564
1565 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001566 // API presents top-to-bottom. We must preserve the padding contents. Note
1567 // that the above readPixels did not overwrite the padding.
1568 if (readDst == buffer) {
1569 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001570 if (!invertY) {
1571 scratch.reset(tightRowBytes);
1572 void* tmpRow = scratch.get();
1573 // flip y in-place by rows
1574 const int halfY = height >> 1;
1575 char* top = reinterpret_cast<char*>(buffer);
1576 char* bottom = top + (height - 1) * rowBytes;
1577 for (int y = 0; y < halfY; y++) {
1578 memcpy(tmpRow, top, tightRowBytes);
1579 memcpy(top, bottom, tightRowBytes);
1580 memcpy(bottom, tmpRow, tightRowBytes);
1581 top += rowBytes;
1582 bottom -= rowBytes;
1583 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001584 }
1585 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001586 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001587 // copy from readDst to buffer while flipping y
1588 const int halfY = height >> 1;
1589 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001590 char* dst = reinterpret_cast<char*>(buffer);
1591 if (!invertY) {
1592 dst += (height-1) * rowBytes;
1593 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001594 for (int y = 0; y < height; y++) {
1595 memcpy(dst, src, tightRowBytes);
1596 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001597 if (invertY) {
1598 dst += rowBytes;
1599 } else {
1600 dst -= rowBytes;
1601 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001602 }
1603 }
1604 return true;
1605}
1606
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001607void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001608
1609 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1610
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001611 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001613 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 #if GR_COLLECT_STATS
1615 ++fStats.fRenderTargetChngCnt;
1616 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001618 GrGLenum status;
1619 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001620 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001621 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001622 }
1623 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001624 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001625 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001626 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001627 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001628 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001629 fHWBounds.fViewportRect = vp;
1630 }
1631 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001632 if (NULL == bound || !bound->isEmpty()) {
1633 rt->flagAsNeedingResolve(bound);
1634 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001635}
1636
twiz@google.com0f31ca72011-03-18 17:38:11 +00001637GrGLenum gPrimitiveType2GLMode[] = {
1638 GR_GL_TRIANGLES,
1639 GR_GL_TRIANGLE_STRIP,
1640 GR_GL_TRIANGLE_FAN,
1641 GR_GL_POINTS,
1642 GR_GL_LINES,
1643 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001644};
1645
bsalomon@google.comd302f142011-03-03 13:54:13 +00001646#define SWAP_PER_DRAW 0
1647
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001648#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001649 #if GR_MAC_BUILD
1650 #include <AGL/agl.h>
1651 #elif GR_WIN32_BUILD
1652 void SwapBuf() {
1653 DWORD procID = GetCurrentProcessId();
1654 HWND hwnd = GetTopWindow(GetDesktopWindow());
1655 while(hwnd) {
1656 DWORD wndProcID = 0;
1657 GetWindowThreadProcessId(hwnd, &wndProcID);
1658 if(wndProcID == procID) {
1659 SwapBuffers(GetDC(hwnd));
1660 }
1661 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1662 }
1663 }
1664 #endif
1665#endif
1666
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001667void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1668 uint32_t startVertex,
1669 uint32_t startIndex,
1670 uint32_t vertexCount,
1671 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001672 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1673
twiz@google.com0f31ca72011-03-18 17:38:11 +00001674 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001675
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001676 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1677 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1678
1679 // our setupGeometry better have adjusted this to zero since
1680 // DrawElements always draws from the begining of the arrays for idx 0.
1681 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001682
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001683 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1684 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001685#if SWAP_PER_DRAW
1686 glFlush();
1687 #if GR_MAC_BUILD
1688 aglSwapBuffers(aglGetCurrentContext());
1689 int set_a_break_pt_here = 9;
1690 aglSwapBuffers(aglGetCurrentContext());
1691 #elif GR_WIN32_BUILD
1692 SwapBuf();
1693 int set_a_break_pt_here = 9;
1694 SwapBuf();
1695 #endif
1696#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001697}
1698
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001699void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1700 uint32_t startVertex,
1701 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1703
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001704 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1705
1706 // our setupGeometry better have adjusted this to zero.
1707 // DrawElements doesn't take an offset so we always adjus the startVertex.
1708 GrAssert(0 == startVertex);
1709
1710 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1711 // account for startVertex in the DrawElements case. So we always
1712 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001713 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001714#if SWAP_PER_DRAW
1715 glFlush();
1716 #if GR_MAC_BUILD
1717 aglSwapBuffers(aglGetCurrentContext());
1718 int set_a_break_pt_here = 9;
1719 aglSwapBuffers(aglGetCurrentContext());
1720 #elif GR_WIN32_BUILD
1721 SwapBuf();
1722 int set_a_break_pt_here = 9;
1723 SwapBuf();
1724 #endif
1725#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001726}
1727
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001728void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001729
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001730 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001731 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001732 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001733 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1734 rt->renderFBOID()));
1735 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1736 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001737 #if GR_COLLECT_STATS
1738 ++fStats.fRenderTargetChngCnt;
1739 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001740 // make sure we go through flushRenderTarget() since we've modified
1741 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001743 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001744 const GrIRect dirtyRect = rt->getResolveRect();
1745 GrGLIRect r;
1746 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1747 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001748
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001749 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001750 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001751 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1752 GL_CALL(Scissor(r.fLeft, r.fBottom,
1753 r.fWidth, r.fHeight));
1754 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001755 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 fHWBounds.fScissorEnabled = true;
1757 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001758 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001759 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001760 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1761 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001762 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001763 int right = r.fLeft + r.fWidth;
1764 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001765 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1766 r.fLeft, r.fBottom, right, top,
1767 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001769 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 }
1771}
1772
twiz@google.com0f31ca72011-03-18 17:38:11 +00001773static const GrGLenum grToGLStencilFunc[] = {
1774 GR_GL_ALWAYS, // kAlways_StencilFunc
1775 GR_GL_NEVER, // kNever_StencilFunc
1776 GR_GL_GREATER, // kGreater_StencilFunc
1777 GR_GL_GEQUAL, // kGEqual_StencilFunc
1778 GR_GL_LESS, // kLess_StencilFunc
1779 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1780 GR_GL_EQUAL, // kEqual_StencilFunc,
1781 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001782};
1783GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1784GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1785GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1786GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1787GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1788GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1789GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1790GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1791GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1792
twiz@google.com0f31ca72011-03-18 17:38:11 +00001793static const GrGLenum grToGLStencilOp[] = {
1794 GR_GL_KEEP, // kKeep_StencilOp
1795 GR_GL_REPLACE, // kReplace_StencilOp
1796 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1797 GR_GL_INCR, // kIncClamp_StencilOp
1798 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1799 GR_GL_DECR, // kDecClamp_StencilOp
1800 GR_GL_ZERO, // kZero_StencilOp
1801 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001802};
1803GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1804GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1805GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1806GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1807GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1808GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1809GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1810GR_STATIC_ASSERT(6 == kZero_StencilOp);
1811GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1812
reed@google.comac10a2d2010-12-22 21:39:39 +00001813void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001814 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001815
1816 // use stencil for clipping if clipping is enabled and the clip
1817 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001818 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001819 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001820 bool stencilChange = fHWStencilClip != stencilClip ||
1821 fHWDrawState.fStencilSettings != *settings ||
1822 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1823 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001824
1825 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001826
bsalomon@google.comd302f142011-03-03 13:54:13 +00001827 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1828 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001829
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830 if (settings->isDisabled()) {
1831 if (stencilClip) {
1832 settings = &gClipStencilSettings;
1833 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001834 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835
1836 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001837 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001838 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001839 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001840 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001841 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001842 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1843 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1844 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1845 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1846 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1847 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1848 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1849 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1850 }
1851 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001852 int stencilBits = 0;
1853 GrStencilBuffer* stencilBuffer =
1854 fCurrDrawState.fRenderTarget->getStencilBuffer();
1855 if (NULL != stencilBuffer) {
1856 stencilBits = stencilBuffer->bits();
1857 }
1858 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859 GrAssert(stencilBits ||
1860 (GrStencilSettings::gDisabled ==
1861 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001862 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1863 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864
1865 unsigned int frontRef = settings->fFrontFuncRef;
1866 unsigned int frontMask = settings->fFrontFuncMask;
1867 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001868 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869
1870 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1871
1872 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1873 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1874 } else {
1875 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1876
1877 ConvertStencilFuncAndMask(settings->fFrontFunc,
1878 stencilClip,
1879 clipStencilMask,
1880 userStencilMask,
1881 &frontRef,
1882 &frontMask);
1883 frontWriteMask &= userStencilMask;
1884 }
1885 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001886 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001888 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001889 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001890 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001891 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001892 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001893 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001894 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001895
1896 unsigned int backRef = settings->fBackFuncRef;
1897 unsigned int backMask = settings->fBackFuncMask;
1898 unsigned int backWriteMask = settings->fBackWriteMask;
1899
1900
1901 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1902 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1903 backFunc = grToGLStencilFunc[settings->fBackFunc];
1904 } else {
1905 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1906 ConvertStencilFuncAndMask(settings->fBackFunc,
1907 stencilClip,
1908 clipStencilMask,
1909 userStencilMask,
1910 &backRef,
1911 &backMask);
1912 backWriteMask &= userStencilMask;
1913 }
1914
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001915 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1916 frontRef, frontMask));
1917 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1918 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1919 backRef, backMask));
1920 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1921 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1922 grToGLStencilOp[settings->fFrontFailOp],
1923 grToGLStencilOp[settings->fFrontPassOp],
1924 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001925
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001926 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1927 grToGLStencilOp[settings->fBackFailOp],
1928 grToGLStencilOp[settings->fBackPassOp],
1929 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001931 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1932 GL_CALL(StencilMask(frontWriteMask));
1933 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001934 grToGLStencilOp[settings->fFrontPassOp],
1935 grToGLStencilOp[settings->fFrontPassOp]));
1936 }
1937 }
1938 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001939 fHWStencilClip = stencilClip;
1940 }
1941}
1942
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001943void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001944 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001945 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1946 // smooth lines.
1947
1948 // we prefer smooth lines over multisampled lines
1949 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001950 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001951 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001952 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001953 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001954 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001955 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001956 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001957 fHWAAState.fSmoothLineEnabled = false;
1958 }
1959 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1960 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001961 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001962 fHWAAState.fMSAAEnabled = false;
1963 }
1964 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001965 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001966 fHWAAState.fMSAAEnabled) {
1967 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001968 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001969 fHWAAState.fMSAAEnabled = false;
1970 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001971 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001972 fHWAAState.fMSAAEnabled = true;
1973 }
1974 }
1975 }
1976}
1977
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001978void GrGpuGL::flushBlend(GrPrimitiveType type,
1979 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001980 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001981 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001982 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001983 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001984 fHWBlendDisabled = false;
1985 }
1986 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1987 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001988 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1989 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001990 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1991 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1992 }
1993 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001994 // any optimization to disable blending should
1995 // have already been applied and tweaked the coeffs
1996 // to (1, 0).
1997 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1998 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001999 if (fHWBlendDisabled != blendOff) {
2000 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002001 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002002 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002003 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002004 }
2005 fHWBlendDisabled = blendOff;
2006 }
2007 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002008 if (fHWDrawState.fSrcBlend != srcCoeff ||
2009 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002010 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2011 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002012 fHWDrawState.fSrcBlend = srcCoeff;
2013 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002014 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002015 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2016 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002017 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2018
2019 float c[] = {
2020 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2021 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2022 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2023 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2024 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002025 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002026 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2027 }
2028 }
2029 }
2030}
2031
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002032namespace {
2033
2034unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002035 switch (filter) {
2036 case GrSamplerState::kBilinear_Filter:
2037 case GrSamplerState::k4x4Downsample_Filter:
2038 return GR_GL_LINEAR;
2039 case GrSamplerState::kNearest_Filter:
2040 case GrSamplerState::kConvolution_Filter:
2041 return GR_GL_NEAREST;
2042 default:
2043 GrAssert(!"Unknown filter type");
2044 return GR_GL_LINEAR;
2045 }
2046}
2047
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002048const GrGLenum* get_swizzle(GrPixelConfig config,
2049 const GrSamplerState& sampler) {
2050 if (GrPixelConfigIsAlphaOnly(config)) {
2051 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2052 GR_GL_ALPHA, GR_GL_ALPHA };
2053 return gAlphaSmear;
2054 } else if (sampler.swapsRAndB()) {
2055 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2056 GR_GL_RED, GR_GL_ALPHA };
2057 return gRedBlueSwap;
2058 } else {
2059 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2060 GR_GL_BLUE, GR_GL_ALPHA };
2061 return gStraight;
2062 }
2063}
2064
2065void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2066 // should add texparameteri to interface to make 1 instead of 4 calls here
2067 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2068 GR_GL_TEXTURE_SWIZZLE_R,
2069 swizzle[0]));
2070 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2071 GR_GL_TEXTURE_SWIZZLE_G,
2072 swizzle[1]));
2073 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2074 GR_GL_TEXTURE_SWIZZLE_B,
2075 swizzle[2]));
2076 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2077 GR_GL_TEXTURE_SWIZZLE_A,
2078 swizzle[3]));
2079}
2080}
2081
bsalomon@google.comffca4002011-02-22 20:34:01 +00002082bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002083
2084 // GrGpu::setupClipAndFlushState should have already checked this
2085 // and bailed if not true.
2086 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002087
tomhudson@google.com93813632011-10-27 20:21:16 +00002088 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002089 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002090 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002091 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002092
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002093 // true for now, but maybe not with GrEffect.
2094 GrAssert(NULL != nextTexture);
2095 // if we created a rt/tex and rendered to it without using a
2096 // texture and now we're texuring from the rt it will still be
2097 // the last bound texture, but it needs resolving. So keep this
2098 // out of the "last != next" check.
2099 GrGLRenderTarget* texRT =
2100 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2101 if (NULL != texRT) {
2102 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002103 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002104
2105 if (fHWDrawState.fTextures[s] != nextTexture) {
2106 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002107 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002108 #if GR_COLLECT_STATS
2109 ++fStats.fTextureChngCnt;
2110 #endif
2111 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2112 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002113 // The texture matrix has to compensate for texture width/height
2114 // and NPOT-embedded-in-POT
2115 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002116 }
2117
2118 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002119 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002120 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002121 nextTexture->getCachedTexParams(&timestamp);
2122 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002123 GrGLTexture::TexParams newTexParams;
2124
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002125 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002126
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002127 const GrGLenum* wraps =
2128 GrGLTexture::WrapMode2GLWrap(this->glBinding());
2129 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2130 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002131 memcpy(newTexParams.fSwizzleRGBA,
2132 get_swizzle(nextTexture->config(), sampler),
2133 sizeof(newTexParams.fSwizzleRGBA));
2134 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002135 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002136 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002137 GR_GL_TEXTURE_MAG_FILTER,
2138 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002139 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002140 GR_GL_TEXTURE_MIN_FILTER,
2141 newTexParams.fFilter));
2142 }
2143 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2144 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002145 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002146 GR_GL_TEXTURE_WRAP_S,
2147 newTexParams.fWrapS));
2148 }
2149 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2150 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002151 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002152 GR_GL_TEXTURE_WRAP_T,
2153 newTexParams.fWrapT));
2154 }
2155 if (this->glCaps().fTextureSwizzle &&
2156 (setAll ||
2157 memcmp(newTexParams.fSwizzleRGBA,
2158 oldTexParams.fSwizzleRGBA,
2159 sizeof(newTexParams.fSwizzleRGBA)))) {
2160 setTextureUnit(s);
2161 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2162 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002163 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002164 nextTexture->setCachedTexParams(newTexParams,
2165 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002166 }
2167 }
2168
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002169 GrIRect* rect = NULL;
2170 GrIRect clipBounds;
2171 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2172 fClip.hasConservativeBounds()) {
2173 fClip.getConservativeBounds().roundOut(&clipBounds);
2174 rect = &clipBounds;
2175 }
2176 this->flushRenderTarget(rect);
2177 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002178
reed@google.comac10a2d2010-12-22 21:39:39 +00002179 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2180 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2181 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002182 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002183 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002184 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002185 }
2186 }
2187
bsalomon@google.comd302f142011-03-03 13:54:13 +00002188 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2189 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002190 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002191 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002192 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002193 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002194 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002195 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002196 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002197 }
2198
bsalomon@google.comd302f142011-03-03 13:54:13 +00002199 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2200 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002201 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002202 GL_CALL(Enable(GR_GL_CULL_FACE));
2203 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002204 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002205 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002206 GL_CALL(Enable(GR_GL_CULL_FACE));
2207 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002208 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002209 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002210 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002211 break;
2212 default:
2213 GrCrash("Unknown draw face.");
2214 }
2215 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2216 }
2217
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002218#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002219 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002220 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002221 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002222 NULL == fCurrDrawState.fRenderTarget ||
2223 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002224 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002225 fCurrDrawState.fRenderTarget);
2226 }
2227#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002228
reed@google.comac10a2d2010-12-22 21:39:39 +00002229 flushStencil();
2230
bsalomon@google.comd302f142011-03-03 13:54:13 +00002231 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002232 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002233 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002234}
2235
2236void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002237 if (fHWGeometryState.fVertexBuffer != buffer) {
2238 fHWGeometryState.fArrayPtrsDirty = true;
2239 fHWGeometryState.fVertexBuffer = buffer;
2240 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002241}
2242
2243void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002244 if (fHWGeometryState.fVertexBuffer == buffer) {
2245 // deleting bound buffer does implied bind to 0
2246 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002247 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002248 }
2249}
2250
2251void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002252 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002253}
2254
2255void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002256 if (fHWGeometryState.fIndexBuffer == buffer) {
2257 // deleting bound buffer does implied bind to 0
2258 fHWGeometryState.fIndexBuffer = NULL;
2259 }
2260}
2261
reed@google.comac10a2d2010-12-22 21:39:39 +00002262void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2263 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002264 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002265 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002266 }
2267 if (fHWDrawState.fRenderTarget == renderTarget) {
2268 fHWDrawState.fRenderTarget = NULL;
2269 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002270}
2271
2272void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002273 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002274 if (fCurrDrawState.fTextures[s] == texture) {
2275 fCurrDrawState.fTextures[s] = NULL;
2276 }
2277 if (fHWDrawState.fTextures[s] == texture) {
2278 // deleting bound texture does implied bind to 0
2279 fHWDrawState.fTextures[s] = NULL;
2280 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002282}
2283
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002284bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002285 GrGLenum* internalFormat,
2286 GrGLenum* format,
2287 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002289 case kRGBA_8888_PM_GrPixelConfig:
2290 case kRGBA_8888_UPM_GrPixelConfig:
2291 *format = GR_GL_RGBA;
2292 *internalFormat = GR_GL_RGBA;
2293 *type = GR_GL_UNSIGNED_BYTE;
2294 break;
2295 case kBGRA_8888_PM_GrPixelConfig:
2296 case kBGRA_8888_UPM_GrPixelConfig:
2297 if (!fGLCaps.fBGRAFormat) {
2298 return false;
2299 }
2300 *format = GR_GL_BGRA;
2301 if (fGLCaps.fBGRAInternalFormat) {
2302 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002303 } else {
2304 *internalFormat = GR_GL_RGBA;
2305 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002306 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002307 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002308 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002309 *format = GR_GL_RGB;
2310 *internalFormat = GR_GL_RGB;
2311 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002312 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002313 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002314 *format = GR_GL_RGBA;
2315 *internalFormat = GR_GL_RGBA;
2316 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002317 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002318 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002319 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002320 *format = GR_GL_PALETTE8_RGBA8;
2321 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002322 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002323 } else {
2324 return false;
2325 }
2326 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002327 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002328 *format = GR_GL_ALPHA;
2329 *internalFormat = GR_GL_ALPHA;
2330 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002331 break;
2332 default:
2333 return false;
2334 }
2335 return true;
2336}
2337
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002338void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002339 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002340 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002341 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002342 fActiveTextureUnitIdx = unit;
2343 }
2344}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002345
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002346void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002347 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002348 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002349 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2350 }
2351}
2352
reed@google.comac10a2d2010-12-22 21:39:39 +00002353/* On ES the internalFormat and format must match for TexImage and we use
2354 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2355 decide the internalFormat. However, on ES internalFormat for
2356 RenderBufferStorage* has to be a specific format (not a base format like
2357 GL_RGBA).
2358 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002359bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002360 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002361 // The ES story for BGRA and RenderbufferStorage appears murky. It
2362 // takes an internal format as a parameter. The OES FBO extension and
2363 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2364 // BGRA extensions adds BGRA as both an internal and external format
2365 // and the other only as an external format (like desktop GL). OES
2366 // restricts RenderbufferStorage's format to a *sized* internal format.
2367 // There is no sized BGRA internal format.
2368 // So if the texture has internal format BGRA we just hope that the
2369 // resolve blit can do RGBA->BGRA internal format conversion.
2370 case kRGBA_8888_PM_GrPixelConfig:
2371 case kRGBA_8888_UPM_GrPixelConfig:
2372 case kBGRA_8888_PM_GrPixelConfig:
2373 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002374 if (fGLCaps.fRGBA8Renderbuffer) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002375 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2376 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002377 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002378 return true;
2379 } else {
2380 return false;
2381 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002382 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002383 // ES2 supports 565. ES1 supports it
2384 // with FBO extension desktop GL has
2385 // no such internal format
bsalomon@google.comc4364992011-11-07 15:54:49 +00002386 if (kDesktop_GrGLBinding != this->glBinding()) {
2387 *format = GR_GL_RGB565;
2388 return true;
2389 } else {
2390 return false;
2391 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002392 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002393 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002394 return true;
2395 default:
2396 return false;
2397 }
2398}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002399
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002400void GrGpuGL::resetDirtyFlags() {
2401 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2402}
2403
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002404void GrGpuGL::setBuffers(bool indexed,
2405 int* extraVertexOffset,
2406 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002407
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002408 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002409
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002410 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2411
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002412 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002413 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002414 case kBuffer_GeometrySrcType:
2415 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002416 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002417 break;
2418 case kArray_GeometrySrcType:
2419 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002420 this->finalizeReservedVertices();
2421 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2422 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002423 break;
2424 default:
2425 vbuf = NULL; // suppress warning
2426 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002427 }
2428
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002429 GrAssert(NULL != vbuf);
2430 GrAssert(!vbuf->isLocked());
2431 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002432 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002433 fHWGeometryState.fArrayPtrsDirty = true;
2434 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002435 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002436
2437 if (indexed) {
2438 GrAssert(NULL != extraIndexOffset);
2439
2440 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002441 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002442 case kBuffer_GeometrySrcType:
2443 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002444 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 break;
2446 case kArray_GeometrySrcType:
2447 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002448 this->finalizeReservedIndices();
2449 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2450 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002451 break;
2452 default:
2453 ibuf = NULL; // suppress warning
2454 GrCrash("Unknown geometry src type!");
2455 }
2456
2457 GrAssert(NULL != ibuf);
2458 GrAssert(!ibuf->isLocked());
2459 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002460 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002461 fHWGeometryState.fIndexBuffer = ibuf;
2462 }
2463 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002464}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002465
2466int GrGpuGL::getMaxEdges() const {
2467 // FIXME: This is a pessimistic estimate based on how many other things
2468 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002469 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2470 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002471}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002472
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002473void GrGpuGL::GLCaps::print() const {
2474 for (int i = 0; i < fStencilFormats.count(); ++i) {
2475 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2476 i,
2477 fStencilFormats[i].fStencilBits,
2478 fStencilFormats[i].fTotalBits);
2479 }
2480
2481 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2482 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2483 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2484 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2485 static const char* gMSFBOExtStr[] = {
2486 "None",
2487 "ARB",
2488 "EXT",
2489 "Apple",
2490 };
2491 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002492 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002493 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2494 }
2495 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2496 GrPrintf("Support RGBA8 Render Buffer: %s\n",
2497 (fRGBA8Renderbuffer ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002498 GrPrintf("BGRA is an internal format: %s\n",
2499 (fBGRAInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002500 GrPrintf("Support texture swizzle: %s\n",
2501 (fTextureSwizzle ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002502}