blob: c162e11c9453260af961d4271c202f9473c9eb60 [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
tomhudson@google.com62b09682011-11-09 16:39:17 +0000625 //fHWDrawState.fSrcBlend = (GrBlendCoeff)(uint8_t)-1;
626 fHWDrawState.fSrcBlend = (GrBlendCoeff)0xFF;
627 fHWDrawState.fDstBlend = (GrBlendCoeff)(uint8_t)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000628
629 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000630 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000631
reed@google.comac10a2d2010-12-22 21:39:39 +0000632 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000633
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000634 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000635
tomhudson@google.com93813632011-10-27 20:21:16 +0000636 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000637 fHWDrawState.fTextures[s] = NULL;
638 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
639 -GR_ScalarMax,
640 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000641 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000642 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000643 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000644
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000645 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000646 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000647 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000648 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000649
bsalomon@google.comd302f142011-03-03 13:54:13 +0000650 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000651 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000652 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000653
654 fHWGeometryState.fIndexBuffer = NULL;
655 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000656
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000657 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000658
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000659 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000660 fHWDrawState.fRenderTarget = NULL;
661}
662
bsalomon@google.come269f212011-11-07 13:29:52 +0000663GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
664 GrGLenum internalFormat; // we don't need this value
665 GrGLTexture::Desc glTexDesc;
666 if (!this->canBeTexture(desc.fConfig, &internalFormat,
667 &glTexDesc.fUploadFormat, &glTexDesc.fUploadType)) {
668 return NULL;
669 }
670
671 glTexDesc.fContentWidth = glTexDesc.fAllocWidth = desc.fWidth;
672 glTexDesc.fContentHeight = glTexDesc.fAllocHeight = desc.fHeight;
673 glTexDesc.fConfig = desc.fConfig;
674 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
675 glTexDesc.fOwnsID = false;
676 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
677
678 GrGLTexture* texture = NULL;
679 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
680 GrGLRenderTarget::Desc glRTDesc;
681 glRTDesc.fRTFBOID = 0;
682 glRTDesc.fTexFBOID = 0;
683 glRTDesc.fMSColorRenderbufferID = 0;
684 glRTDesc.fOwnIDs = true;
685 glRTDesc.fConfig = desc.fConfig;
686 glRTDesc.fSampleCnt = desc.fSampleCnt;
687 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
688 glTexDesc.fAllocHeight,
689 glTexDesc.fTextureID,
690 &glRTDesc)) {
691 return NULL;
692 }
693 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
694 } else {
695 texture = new GrGLTexture(this, glTexDesc);
696 }
697 if (NULL == texture) {
698 return NULL;
699 }
700
701 this->setSpareTextureUnit();
702 return texture;
703}
704
705GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
706 GrGLRenderTarget::Desc glDesc;
707 glDesc.fConfig = desc.fConfig;
708 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
709 glDesc.fMSColorRenderbufferID = 0;
710 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
711 glDesc.fSampleCnt = desc.fSampleCnt;
712 glDesc.fOwnIDs = false;
713 GrGLIRect viewport;
714 viewport.fLeft = 0;
715 viewport.fBottom = 0;
716 viewport.fWidth = desc.fWidth;
717 viewport.fHeight = desc.fHeight;
718
719 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
720 if (desc.fStencilBits) {
721 GrGLStencilBuffer::Format format;
722 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
723 format.fPacked = false;
724 format.fStencilBits = desc.fStencilBits;
725 format.fTotalBits = desc.fStencilBits;
726 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
727 0,
728 desc.fWidth,
729 desc.fHeight,
730 desc.fSampleCnt,
731 format);
732 tgt->setStencilBuffer(sb);
733 sb->unref();
734 }
735 return tgt;
736}
737
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000738GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
739
740 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
741 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
742 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
743 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
744
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000745 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000746 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000747
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000748 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000749 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000750 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000751 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000752 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000753 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000754 } else {
755 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000756 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000757 }
758 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000759 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000760 }
761 // we don't know what the RB ids are without glGets and we don't care
762 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000763 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000764 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000765 if (desc.fStencilBits) {
766 GrGLStencilBuffer::Format format;
767 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
768 format.fPacked = false;
769 format.fStencilBits = desc.fStencilBits;
770 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000771 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
772 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000773 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000774 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000775 }
776
777 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000778 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000779 GrGLenum dontCare;
780 if (!canBeTexture(desc.fConfig, &dontCare,
781 &texDesc.fUploadFormat,
782 &texDesc.fUploadType)) {
783 return NULL;
784 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000785 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
786 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
787
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000788 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000789 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000790 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000791 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000792
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000793 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000794 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000795 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000796 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000797 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000798 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000799 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000800 } else {
801 GrGLIRect viewport;
802 viewport.fLeft = 0;
803 viewport.fBottom = 0;
804 viewport.fWidth = desc.fWidth;
805 viewport.fHeight = desc.fHeight;
806
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000807 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000808 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000809 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000810 }
811}
812
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000813
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000814////////////////////////////////////////////////////////////////////////////////
815
816void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
817 GrGLenum internalFormat,
818 const void* data,
819 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000820 // we assume the texture is bound
821
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000822 size_t bpp = GrBytesPerPixel(desc.fConfig);
823 size_t trimRowBytes = desc.fContentWidth * bpp;
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000824
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000825 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000826 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000827 }
828
829 // in case we need a temporary, trimmed copy of the src pixels
830 SkAutoSMalloc<128 * 128> tempStorage;
831
832 /*
833 * check whether to allocate a temporary buffer for flipping y or
834 * because our data has extra bytes past each row. If so, we need
835 * to trim those off here, since GL ES doesn't let us specify
836 * GL_UNPACK_ROW_LENGTH.
837 */
838 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000839 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000840 if (data && rowBytes != trimRowBytes) {
841 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
842 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000843 }
844 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000845 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846 // copy the data into our new storage, skipping the trailing bytes
847 size_t trimSize = desc.fContentHeight * trimRowBytes;
848 const char* src = (const char*)data;
849 if (flipY) {
850 src += (desc.fContentHeight - 1) * rowBytes;
851 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000852 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000853 for (int y = 0; y < desc.fContentHeight; y++) {
854 memcpy(dst, src, trimRowBytes);
855 if (flipY) {
856 src -= rowBytes;
857 } else {
858 src += rowBytes;
859 }
860 dst += trimRowBytes;
861 }
862 // now point data to our trimmed version
863 data = tempStorage.get();
864 rowBytes = trimRowBytes;
865 }
866 }
867
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000868 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000869 if (kIndex_8_GrPixelConfig == desc.fConfig &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000870 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000871 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
872 GrAssert(desc.fContentWidth == desc.fAllocWidth);
873 GrAssert(desc.fContentHeight == desc.fAllocHeight);
874 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
875 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000876 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
877 desc.fAllocWidth, desc.fAllocHeight,
878 0, imageSize, data));
879 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000880 } else {
881 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
882 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000883 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
884 desc.fAllocWidth, desc.fAllocHeight,
885 0, desc.fUploadFormat, desc.fUploadType, NULL));
886 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
887 desc.fContentHeight, desc.fUploadFormat,
888 desc.fUploadType, data));
889 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000890
891 int extraW = desc.fAllocWidth - desc.fContentWidth;
892 int extraH = desc.fAllocHeight - desc.fContentHeight;
893 int maxTexels = extraW * extraH;
894 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
895 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
896
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000897 SkAutoSMalloc<128*128> texels(bpp * maxTexels);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000898
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000899 if (extraH) {
900 uint8_t* lastRowStart = (uint8_t*) data +
901 (desc.fContentHeight - 1) * rowBytes;
902 uint8_t* extraRowStart = (uint8_t*)texels.get();
903
904 for (int i = 0; i < extraH; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000905 memcpy(extraRowStart, lastRowStart, trimRowBytes);
906 extraRowStart += trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000907 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000908 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
909 desc.fContentHeight, desc.fContentWidth,
910 extraH, desc.fUploadFormat,
911 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000912 }
913 if (extraW) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000914 uint8_t* edgeTexel = (uint8_t*)data + trimRowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000915 uint8_t* extraTexel = (uint8_t*)texels.get();
916 for (int j = 0; j < desc.fContentHeight; ++j) {
917 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000918 memcpy(extraTexel, edgeTexel, bpp);
919 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000920 }
921 edgeTexel += rowBytes;
922 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000923 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
924 0, extraW, desc.fContentHeight,
925 desc.fUploadFormat, desc.fUploadType,
926 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000927 }
928 if (extraW && extraH) {
929 uint8_t* cornerTexel = (uint8_t*)data +
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000930 desc.fContentHeight * rowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000931 uint8_t* extraTexel = (uint8_t*)texels.get();
932 for (int i = 0; i < extraW*extraH; ++i) {
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000933 memcpy(extraTexel, cornerTexel, bpp);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000934 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000935 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
937 desc.fContentHeight, extraW, extraH,
938 desc.fUploadFormat, desc.fUploadType,
939 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000940 }
941
942 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000943 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
944 desc.fAllocWidth, desc.fAllocHeight, 0,
945 desc.fUploadFormat, desc.fUploadType, data));
946 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000947 }
948 }
949}
950
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000951bool GrGpuGL::createRenderTargetObjects(int width, int height,
952 GrGLuint texID,
953 GrGLRenderTarget::Desc* desc) {
954 desc->fMSColorRenderbufferID = 0;
955 desc->fRTFBOID = 0;
956 desc->fTexFBOID = 0;
957 desc->fOwnIDs = true;
958
959 GrGLenum status;
960 GrGLint err;
961
bsalomon@google.comab15d612011-08-09 12:57:56 +0000962 GrGLenum msColorFormat = 0; // suppress warning
963
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000964 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000965 if (!desc->fTexFBOID) {
966 goto FAILED;
967 }
968
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000969
970 // If we are using multisampling we will create two FBOS. We render
971 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000972 if (desc->fSampleCnt > 0) {
973 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
974 goto FAILED;
975 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000976 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
977 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000978 if (!desc->fRTFBOID ||
979 !desc->fMSColorRenderbufferID ||
980 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
981 goto FAILED;
982 }
983 } else {
984 desc->fRTFBOID = desc->fTexFBOID;
985 }
986
987 if (desc->fRTFBOID != desc->fTexFBOID) {
988 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000989 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000990 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000991 GR_GL_CALL_NOERRCHECK(this->glInterface(),
992 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
993 desc->fSampleCnt,
994 msColorFormat,
995 width, height));
996 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000997 if (err != GR_GL_NO_ERROR) {
998 goto FAILED;
999 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001000 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
1001 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001002 GR_GL_COLOR_ATTACHMENT0,
1003 GR_GL_RENDERBUFFER,
1004 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001005 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001006 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1007 goto FAILED;
1008 }
1009 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001010 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001011
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001012 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1013 GR_GL_COLOR_ATTACHMENT0,
1014 GR_GL_TEXTURE_2D,
1015 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001016 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001017 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1018 goto FAILED;
1019 }
1020
1021 return true;
1022
1023FAILED:
1024 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001025 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001026 }
1027 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001028 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001029 }
1030 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001031 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001032 }
1033 return false;
1034}
1035
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001036// good to set a break-point here to know when createTexture fails
1037static GrTexture* return_null_texture() {
1038// GrAssert(!"null texture");
1039 return NULL;
1040}
1041
1042#if GR_DEBUG
1043static size_t as_size_t(int x) {
1044 return x;
1045}
1046#endif
1047
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001048GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001049 const void* srcData,
1050 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001051
1052#if GR_COLLECT_STATS
1053 ++fStats.fTextureCreateCnt;
1054#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001055
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001056 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001057 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001058 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001059
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001060 glTexDesc.fContentWidth = desc.fWidth;
1061 glTexDesc.fContentHeight = desc.fHeight;
1062 glTexDesc.fAllocWidth = desc.fWidth;
1063 glTexDesc.fAllocHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001064 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001065 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001066
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001067 glRTDesc.fMSColorRenderbufferID = 0;
1068 glRTDesc.fRTFBOID = 0;
1069 glRTDesc.fTexFBOID = 0;
1070 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001071 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001072
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001073 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001074 if (!canBeTexture(desc.fConfig,
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001076 &glTexDesc.fUploadFormat,
1077 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001078 return return_null_texture();
1079 }
1080
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001081 const Caps& caps = this->getCaps();
1082
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001083 // We keep GrRenderTargets in GL's normal orientation so that they
1084 // can be drawn to by the outside world without the client having
1085 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001086 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001087 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001088
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001089 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1090 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1091 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1092 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 GrPrintf("AA RT requested but not supported on this platform.");
1094 }
1095
reed@google.comac10a2d2010-12-22 21:39:39 +00001096 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001097 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001098 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1099 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001100 }
1101
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001102 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001104 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001106 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1107 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001108 return return_null_texture();
1109 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001110 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001111 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1112 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001113 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1114 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001115 return return_null_texture();
1116 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001117 }
1118
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001119 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001120 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001121 return return_null_texture();
1122 }
1123
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001124 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001125 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001126
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001127 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1128 // drivers have a bug where an FBO won't be complete if it includes a
1129 // texture that is not mipmap complete (considering the filter in use).
1130 GrGLTexture::TexParams initialTexParams;
1131 // we only set a subset here so invalidate first
1132 initialTexParams.invalidate();
1133 initialTexParams.fFilter = GR_GL_NEAREST;
1134 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1135 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001136 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1137 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001138 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1140 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001141 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001142 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1143 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001144 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001145 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1146 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001147 initialTexParams.fWrapT));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001148 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001149
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 if (renderTarget) {
1152#if GR_COLLECT_STATS
1153 ++fStats.fRenderTargetCreateCnt;
1154#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1156 glTexDesc.fAllocHeight,
1157 glTexDesc.fTextureID,
1158 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001159 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001160 return return_null_texture();
1161 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001162 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001164 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001165 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001166 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001167#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001168 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1169 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001170#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 return tex;
1172}
1173
1174namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001175void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1176 GrGLuint rb,
1177 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 // we shouldn't ever know one size and not the other
1179 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1180 (kUnknownBitCount == format->fTotalBits));
1181 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001182 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1184 (GrGLint*)&format->fStencilBits);
1185 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001186 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1188 (GrGLint*)&format->fTotalBits);
1189 format->fTotalBits += format->fStencilBits;
1190 } else {
1191 format->fTotalBits = format->fStencilBits;
1192 }
1193 }
1194}
1195}
1196
1197bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1198 int width, int height) {
1199
1200 // All internally created RTs are also textures. We don't create
1201 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1202 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001203 GrAssert(width >= rt->allocatedWidth());
1204 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205
1206 int samples = rt->numSamples();
1207 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001208 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209 if (!sbID) {
1210 return false;
1211 }
1212
1213 GrGLStencilBuffer* sb = NULL;
1214
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001215 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 // we start with the last stencil format that succeeded in hopes
1219 // that we won't go through this loop more than once after the
1220 // first (painful) stencil creation.
1221 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001222 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001223 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 // version on a GL that doesn't have an MSAA extension.
1225 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001226 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1227 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228 GR_GL_RENDERBUFFER,
1229 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001230 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231 width,
1232 height));
1233 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1235 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001236 sFmt.fInternalFormat,
1237 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 }
1239
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 if (err == GR_GL_NO_ERROR) {
1242 // After sized formats we attempt an unsized format and take whatever
1243 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001244 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001245 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001246 sb = new GrGLStencilBuffer(this, sbID, width, height,
1247 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1249 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001250 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 return true;
1253 }
1254 sb->abandon(); // otherwise we lose sbID
1255 sb->unref();
1256 }
1257 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001258 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001259 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001260}
1261
1262bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1263 GrRenderTarget* rt) {
1264 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1265
1266 GrGLuint fbo = glrt->renderFBOID();
1267
1268 if (NULL == sb) {
1269 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001270 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001271 GR_GL_STENCIL_ATTACHMENT,
1272 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001273 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001274 GR_GL_DEPTH_ATTACHMENT,
1275 GR_GL_RENDERBUFFER, 0));
1276#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001277 GrGLenum status;
1278 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1280#endif
1281 }
1282 return true;
1283 } else {
1284 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1285 GrGLuint rb = glsb->renderbufferID();
1286
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1289 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290 GR_GL_STENCIL_ATTACHMENT,
1291 GR_GL_RENDERBUFFER, rb));
1292 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001294 GR_GL_DEPTH_ATTACHMENT,
1295 GR_GL_RENDERBUFFER, rb));
1296 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001297 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001298 GR_GL_DEPTH_ATTACHMENT,
1299 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001300 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001301
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001302 GrGLenum status;
1303 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001304 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001305 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001306 GR_GL_STENCIL_ATTACHMENT,
1307 GR_GL_RENDERBUFFER, 0));
1308 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001309 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001310 GR_GL_DEPTH_ATTACHMENT,
1311 GR_GL_RENDERBUFFER, 0));
1312 }
1313 return false;
1314 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001315 return true;
1316 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001318}
1319
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001320////////////////////////////////////////////////////////////////////////////////
1321
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001322GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001323 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001324 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001326 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001327 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001328 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001329 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001330 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1331 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1332 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1333 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1334 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 // deleting bound buffer does implicit bind to 0
1336 fHWGeometryState.fVertexBuffer = NULL;
1337 return NULL;
1338 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001339 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 size, dynamic);
1341 fHWGeometryState.fVertexBuffer = vertexBuffer;
1342 return vertexBuffer;
1343 }
1344 return NULL;
1345}
1346
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001347GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001348 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001349 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001351 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1352 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001354 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1355 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1356 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1357 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1358 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001359 // deleting bound buffer does implicit bind to 0
1360 fHWGeometryState.fIndexBuffer = NULL;
1361 return NULL;
1362 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001363 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001364 size, dynamic);
1365 fHWGeometryState.fIndexBuffer = indexBuffer;
1366 return indexBuffer;
1367 }
1368 return NULL;
1369}
1370
reed@google.comac10a2d2010-12-22 21:39:39 +00001371void GrGpuGL::flushScissor(const GrIRect* rect) {
1372 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001373 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001374 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001375
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001376 GrGLIRect scissor;
1377 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001378 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001379 rect->width(), rect->height());
1380 if (scissor.contains(vp)) {
1381 rect = NULL;
1382 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001383 }
1384
1385 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001386 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001387 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001388 fHWBounds.fScissorRect = scissor;
1389 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001390 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001391 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001392 fHWBounds.fScissorEnabled = true;
1393 }
1394 } else {
1395 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001396 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001397 fHWBounds.fScissorEnabled = false;
1398 }
1399 }
1400}
1401
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001402void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001403 if (NULL == fCurrDrawState.fRenderTarget) {
1404 return;
1405 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001406 GrIRect r;
1407 if (NULL != rect) {
1408 // flushScissor expects rect to be clipped to the target.
1409 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001410 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1411 fCurrDrawState.fRenderTarget->height());
1412 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001413 rect = &r;
1414 } else {
1415 return;
1416 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001417 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001418 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001419 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001420 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001421 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001422 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1423 GrColorUnpackG(color)/255.f,
1424 GrColorUnpackB(color)/255.f,
1425 GrColorUnpackA(color)/255.f));
1426 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001427}
1428
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001429void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001430 if (NULL == fCurrDrawState.fRenderTarget) {
1431 return;
1432 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001433
1434 this->flushRenderTarget(&GrIRect::EmptyIRect());
1435
reed@google.comac10a2d2010-12-22 21:39:39 +00001436 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001437 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001438 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001439 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001440 GL_CALL(StencilMask(0xffffffff));
1441 GL_CALL(ClearStencil(0));
1442 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001443 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001444}
1445
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001446void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001447 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001448
1449 // this should only be called internally when we know we have a
1450 // stencil buffer.
1451 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001452 GrGLint stencilBitCount =
1453 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001454#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001455 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001456 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001457#else
1458 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001459 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001460 // turned into draws. Our contract on GrDrawTarget says that
1461 // changing the clip between stencil passes may or may not
1462 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001463 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001464#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001465 GrGLint value;
1466 if (insideClip) {
1467 value = (1 << (stencilBitCount - 1));
1468 } else {
1469 value = 0;
1470 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001471 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001472 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001473 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001474 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001475 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001476 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001477}
1478
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001479void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001480 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001481}
1482
bsalomon@google.comc4364992011-11-07 15:54:49 +00001483bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1484 int left, int top,
1485 int width, int height,
1486 GrPixelConfig config,
1487 size_t rowBytes) {
1488 if (kDesktop_GrGLBinding == this->glBinding()) {
1489 return false;
1490 } else {
1491 // On ES we'll have to do memcpys to handle rowByte padding. So, we
1492 // might as well flipY while we're at it.
bsalomon@google.comca08edd2011-11-07 16:56:39 +00001493 return 0 == rowBytes ||
1494 GrBytesPerPixel(config) * width == rowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001495 }
1496}
1497
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001498bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001499 int left, int top,
1500 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001501 GrPixelConfig config,
1502 void* buffer,
1503 size_t rowBytes,
1504 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001505 GrGLenum internalFormat; // we don't use this for glReadPixels
1506 GrGLenum format;
1507 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1509 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001511
bsalomon@google.comc6980972011-11-02 19:57:21 +00001512 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001513 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1514 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1515 switch (tgt->getResolveType()) {
1516 case GrGLRenderTarget::kCantResolve_ResolveType:
1517 return false;
1518 case GrGLRenderTarget::kAutoResolves_ResolveType:
1519 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1520 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001521 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001522 break;
1523 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001524 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001525 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001526 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1527 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001528 break;
1529 default:
1530 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001531 }
1532
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001533 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001534
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001535 // the read rect is viewport-relative
1536 GrGLIRect readRect;
1537 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001538
1539 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1540 if (0 == rowBytes) {
1541 rowBytes = tightRowBytes;
1542 }
1543 size_t readDstRowBytes = tightRowBytes;
1544 void* readDst = buffer;
1545
1546 // determine if GL can read using the passed rowBytes or if we need
1547 // a scratch buffer.
1548 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1549 if (rowBytes != tightRowBytes) {
1550 if (kDesktop_GrGLBinding == this->glBinding()) {
1551 GrAssert(!(rowBytes % sizeof(GrColor)));
1552 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1553 readDstRowBytes = rowBytes;
1554 } else {
1555 scratch.reset(tightRowBytes * height);
1556 readDst = scratch.get();
1557 }
1558 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001559 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1560 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001561 format, type, readDst));
1562 if (readDstRowBytes != tightRowBytes) {
1563 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1564 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001565
1566 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001567 // API presents top-to-bottom. We must preserve the padding contents. Note
1568 // that the above readPixels did not overwrite the padding.
1569 if (readDst == buffer) {
1570 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001571 if (!invertY) {
1572 scratch.reset(tightRowBytes);
1573 void* tmpRow = scratch.get();
1574 // flip y in-place by rows
1575 const int halfY = height >> 1;
1576 char* top = reinterpret_cast<char*>(buffer);
1577 char* bottom = top + (height - 1) * rowBytes;
1578 for (int y = 0; y < halfY; y++) {
1579 memcpy(tmpRow, top, tightRowBytes);
1580 memcpy(top, bottom, tightRowBytes);
1581 memcpy(bottom, tmpRow, tightRowBytes);
1582 top += rowBytes;
1583 bottom -= rowBytes;
1584 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001585 }
1586 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001587 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001588 // copy from readDst to buffer while flipping y
1589 const int halfY = height >> 1;
1590 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001591 char* dst = reinterpret_cast<char*>(buffer);
1592 if (!invertY) {
1593 dst += (height-1) * rowBytes;
1594 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001595 for (int y = 0; y < height; y++) {
1596 memcpy(dst, src, tightRowBytes);
1597 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001598 if (invertY) {
1599 dst += rowBytes;
1600 } else {
1601 dst -= rowBytes;
1602 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 }
1604 }
1605 return true;
1606}
1607
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001608void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001609
1610 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1611
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001612 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001614 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001615 #if GR_COLLECT_STATS
1616 ++fStats.fRenderTargetChngCnt;
1617 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001618 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001619 GrGLenum status;
1620 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001621 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001622 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 }
1624 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001625 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001626 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001627 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001628 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001629 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 fHWBounds.fViewportRect = vp;
1631 }
1632 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001633 if (NULL == bound || !bound->isEmpty()) {
1634 rt->flagAsNeedingResolve(bound);
1635 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001636}
1637
twiz@google.com0f31ca72011-03-18 17:38:11 +00001638GrGLenum gPrimitiveType2GLMode[] = {
1639 GR_GL_TRIANGLES,
1640 GR_GL_TRIANGLE_STRIP,
1641 GR_GL_TRIANGLE_FAN,
1642 GR_GL_POINTS,
1643 GR_GL_LINES,
1644 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001645};
1646
bsalomon@google.comd302f142011-03-03 13:54:13 +00001647#define SWAP_PER_DRAW 0
1648
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001649#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001650 #if GR_MAC_BUILD
1651 #include <AGL/agl.h>
1652 #elif GR_WIN32_BUILD
1653 void SwapBuf() {
1654 DWORD procID = GetCurrentProcessId();
1655 HWND hwnd = GetTopWindow(GetDesktopWindow());
1656 while(hwnd) {
1657 DWORD wndProcID = 0;
1658 GetWindowThreadProcessId(hwnd, &wndProcID);
1659 if(wndProcID == procID) {
1660 SwapBuffers(GetDC(hwnd));
1661 }
1662 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1663 }
1664 }
1665 #endif
1666#endif
1667
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001668void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1669 uint32_t startVertex,
1670 uint32_t startIndex,
1671 uint32_t vertexCount,
1672 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001673 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1674
twiz@google.com0f31ca72011-03-18 17:38:11 +00001675 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001676
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001677 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1678 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1679
1680 // our setupGeometry better have adjusted this to zero since
1681 // DrawElements always draws from the begining of the arrays for idx 0.
1682 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001683
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001684 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1685 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001686#if SWAP_PER_DRAW
1687 glFlush();
1688 #if GR_MAC_BUILD
1689 aglSwapBuffers(aglGetCurrentContext());
1690 int set_a_break_pt_here = 9;
1691 aglSwapBuffers(aglGetCurrentContext());
1692 #elif GR_WIN32_BUILD
1693 SwapBuf();
1694 int set_a_break_pt_here = 9;
1695 SwapBuf();
1696 #endif
1697#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001698}
1699
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001700void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1701 uint32_t startVertex,
1702 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1704
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001705 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1706
1707 // our setupGeometry better have adjusted this to zero.
1708 // DrawElements doesn't take an offset so we always adjus the startVertex.
1709 GrAssert(0 == startVertex);
1710
1711 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1712 // account for startVertex in the DrawElements case. So we always
1713 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001714 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001715#if SWAP_PER_DRAW
1716 glFlush();
1717 #if GR_MAC_BUILD
1718 aglSwapBuffers(aglGetCurrentContext());
1719 int set_a_break_pt_here = 9;
1720 aglSwapBuffers(aglGetCurrentContext());
1721 #elif GR_WIN32_BUILD
1722 SwapBuf();
1723 int set_a_break_pt_here = 9;
1724 SwapBuf();
1725 #endif
1726#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001727}
1728
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001729void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001730
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001731 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001732 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001734 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1735 rt->renderFBOID()));
1736 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1737 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001738 #if GR_COLLECT_STATS
1739 ++fStats.fRenderTargetChngCnt;
1740 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001741 // make sure we go through flushRenderTarget() since we've modified
1742 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001743 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001744 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001745 const GrIRect dirtyRect = rt->getResolveRect();
1746 GrGLIRect r;
1747 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1748 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001749
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001750 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001751 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001752 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1753 GL_CALL(Scissor(r.fLeft, r.fBottom,
1754 r.fWidth, r.fHeight));
1755 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001756 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 fHWBounds.fScissorEnabled = true;
1758 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001759 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001760 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001761 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1762 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001763 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001764 int right = r.fLeft + r.fWidth;
1765 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001766 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1767 r.fLeft, r.fBottom, right, top,
1768 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001770 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001771 }
1772}
1773
twiz@google.com0f31ca72011-03-18 17:38:11 +00001774static const GrGLenum grToGLStencilFunc[] = {
1775 GR_GL_ALWAYS, // kAlways_StencilFunc
1776 GR_GL_NEVER, // kNever_StencilFunc
1777 GR_GL_GREATER, // kGreater_StencilFunc
1778 GR_GL_GEQUAL, // kGEqual_StencilFunc
1779 GR_GL_LESS, // kLess_StencilFunc
1780 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1781 GR_GL_EQUAL, // kEqual_StencilFunc,
1782 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001783};
1784GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1785GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1786GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1787GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1788GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1789GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1790GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1791GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1792GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1793
twiz@google.com0f31ca72011-03-18 17:38:11 +00001794static const GrGLenum grToGLStencilOp[] = {
1795 GR_GL_KEEP, // kKeep_StencilOp
1796 GR_GL_REPLACE, // kReplace_StencilOp
1797 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1798 GR_GL_INCR, // kIncClamp_StencilOp
1799 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1800 GR_GL_DECR, // kDecClamp_StencilOp
1801 GR_GL_ZERO, // kZero_StencilOp
1802 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803};
1804GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1805GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1806GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1807GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1808GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1809GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1810GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1811GR_STATIC_ASSERT(6 == kZero_StencilOp);
1812GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1813
reed@google.comac10a2d2010-12-22 21:39:39 +00001814void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001815 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001816
1817 // use stencil for clipping if clipping is enabled and the clip
1818 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001819 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001820 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001821 bool stencilChange = fHWStencilClip != stencilClip ||
1822 fHWDrawState.fStencilSettings != *settings ||
1823 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1824 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001825
1826 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001827
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1829 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001830
bsalomon@google.comd302f142011-03-03 13:54:13 +00001831 if (settings->isDisabled()) {
1832 if (stencilClip) {
1833 settings = &gClipStencilSettings;
1834 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001835 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001836
1837 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001838 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001839 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001840 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001841 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001842 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001843 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1844 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1845 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1846 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1847 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1848 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1849 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1850 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1851 }
1852 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001853 int stencilBits = 0;
1854 GrStencilBuffer* stencilBuffer =
1855 fCurrDrawState.fRenderTarget->getStencilBuffer();
1856 if (NULL != stencilBuffer) {
1857 stencilBits = stencilBuffer->bits();
1858 }
1859 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001860 GrAssert(stencilBits ||
1861 (GrStencilSettings::gDisabled ==
1862 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001863 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1864 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001865
1866 unsigned int frontRef = settings->fFrontFuncRef;
1867 unsigned int frontMask = settings->fFrontFuncMask;
1868 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001869 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001870
1871 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1872
1873 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1874 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1875 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001876 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
1877 stencilClip, settings->fFrontFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001878
1879 ConvertStencilFuncAndMask(settings->fFrontFunc,
1880 stencilClip,
1881 clipStencilMask,
1882 userStencilMask,
1883 &frontRef,
1884 &frontMask);
1885 frontWriteMask &= userStencilMask;
1886 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001887 GrAssert((size_t)
1888 settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1889 GrAssert((size_t)
1890 settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
1891 GrAssert((size_t)
1892 settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1893 GrAssert((size_t)
1894 settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001895 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001896 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897
1898 unsigned int backRef = settings->fBackFuncRef;
1899 unsigned int backMask = settings->fBackFuncMask;
1900 unsigned int backWriteMask = settings->fBackWriteMask;
1901
1902
1903 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1904 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1905 backFunc = grToGLStencilFunc[settings->fBackFunc];
1906 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001907 backFunc = grToGLStencilFunc[ConvertStencilFunc(
1908 stencilClip, settings->fBackFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001909 ConvertStencilFuncAndMask(settings->fBackFunc,
1910 stencilClip,
1911 clipStencilMask,
1912 userStencilMask,
1913 &backRef,
1914 &backMask);
1915 backWriteMask &= userStencilMask;
1916 }
1917
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001918 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1919 frontRef, frontMask));
1920 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1921 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1922 backRef, backMask));
1923 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1924 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1925 grToGLStencilOp[settings->fFrontFailOp],
1926 grToGLStencilOp[settings->fFrontPassOp],
1927 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001928
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001929 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1930 grToGLStencilOp[settings->fBackFailOp],
1931 grToGLStencilOp[settings->fBackPassOp],
1932 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001933 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001934 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1935 GL_CALL(StencilMask(frontWriteMask));
1936 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001937 grToGLStencilOp[settings->fFrontPassOp],
1938 grToGLStencilOp[settings->fFrontPassOp]));
1939 }
1940 }
1941 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001942 fHWStencilClip = stencilClip;
1943 }
1944}
1945
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001946void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001947 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001948 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1949 // smooth lines.
1950
1951 // we prefer smooth lines over multisampled lines
1952 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001953 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001954 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001955 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001956 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001957 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001958 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001959 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001960 fHWAAState.fSmoothLineEnabled = false;
1961 }
1962 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1963 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001964 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001965 fHWAAState.fMSAAEnabled = false;
1966 }
1967 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001968 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001969 fHWAAState.fMSAAEnabled) {
1970 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001971 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001972 fHWAAState.fMSAAEnabled = false;
1973 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001974 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001975 fHWAAState.fMSAAEnabled = true;
1976 }
1977 }
1978 }
1979}
1980
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001981void GrGpuGL::flushBlend(GrPrimitiveType type,
1982 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001983 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001984 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001985 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001986 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001987 fHWBlendDisabled = false;
1988 }
1989 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1990 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001991 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1992 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001993 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1994 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1995 }
1996 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001997 // any optimization to disable blending should
1998 // have already been applied and tweaked the coeffs
1999 // to (1, 0).
2000 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2001 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002002 if (fHWBlendDisabled != blendOff) {
2003 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002004 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002005 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002006 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002007 }
2008 fHWBlendDisabled = blendOff;
2009 }
2010 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002011 if (fHWDrawState.fSrcBlend != srcCoeff ||
2012 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002013 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2014 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002015 fHWDrawState.fSrcBlend = srcCoeff;
2016 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002017 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002018 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2019 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002020 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2021
2022 float c[] = {
2023 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2024 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2025 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2026 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2027 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002028 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002029 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2030 }
2031 }
2032 }
2033}
2034
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002035namespace {
2036
2037unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002038 switch (filter) {
2039 case GrSamplerState::kBilinear_Filter:
2040 case GrSamplerState::k4x4Downsample_Filter:
2041 return GR_GL_LINEAR;
2042 case GrSamplerState::kNearest_Filter:
2043 case GrSamplerState::kConvolution_Filter:
2044 return GR_GL_NEAREST;
2045 default:
2046 GrAssert(!"Unknown filter type");
2047 return GR_GL_LINEAR;
2048 }
2049}
2050
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002051const GrGLenum* get_swizzle(GrPixelConfig config,
2052 const GrSamplerState& sampler) {
2053 if (GrPixelConfigIsAlphaOnly(config)) {
2054 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2055 GR_GL_ALPHA, GR_GL_ALPHA };
2056 return gAlphaSmear;
2057 } else if (sampler.swapsRAndB()) {
2058 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2059 GR_GL_RED, GR_GL_ALPHA };
2060 return gRedBlueSwap;
2061 } else {
2062 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2063 GR_GL_BLUE, GR_GL_ALPHA };
2064 return gStraight;
2065 }
2066}
2067
2068void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2069 // should add texparameteri to interface to make 1 instead of 4 calls here
2070 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2071 GR_GL_TEXTURE_SWIZZLE_R,
2072 swizzle[0]));
2073 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2074 GR_GL_TEXTURE_SWIZZLE_G,
2075 swizzle[1]));
2076 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2077 GR_GL_TEXTURE_SWIZZLE_B,
2078 swizzle[2]));
2079 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2080 GR_GL_TEXTURE_SWIZZLE_A,
2081 swizzle[3]));
2082}
2083}
2084
bsalomon@google.comffca4002011-02-22 20:34:01 +00002085bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002086
2087 // GrGpu::setupClipAndFlushState should have already checked this
2088 // and bailed if not true.
2089 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002090
tomhudson@google.com93813632011-10-27 20:21:16 +00002091 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002092 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002093 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002094 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002095
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002096 // true for now, but maybe not with GrEffect.
2097 GrAssert(NULL != nextTexture);
2098 // if we created a rt/tex and rendered to it without using a
2099 // texture and now we're texuring from the rt it will still be
2100 // the last bound texture, but it needs resolving. So keep this
2101 // out of the "last != next" check.
2102 GrGLRenderTarget* texRT =
2103 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2104 if (NULL != texRT) {
2105 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002106 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002107
2108 if (fHWDrawState.fTextures[s] != nextTexture) {
2109 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002110 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002111 #if GR_COLLECT_STATS
2112 ++fStats.fTextureChngCnt;
2113 #endif
2114 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2115 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002116 // The texture matrix has to compensate for texture width/height
2117 // and NPOT-embedded-in-POT
2118 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002119 }
2120
2121 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002122 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002123 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002124 nextTexture->getCachedTexParams(&timestamp);
2125 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002126 GrGLTexture::TexParams newTexParams;
2127
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002128 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002129
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002130 const GrGLenum* wraps =
2131 GrGLTexture::WrapMode2GLWrap(this->glBinding());
2132 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2133 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002134 memcpy(newTexParams.fSwizzleRGBA,
2135 get_swizzle(nextTexture->config(), sampler),
2136 sizeof(newTexParams.fSwizzleRGBA));
2137 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002138 setTextureUnit(s);
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_MAG_FILTER,
2141 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002142 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002143 GR_GL_TEXTURE_MIN_FILTER,
2144 newTexParams.fFilter));
2145 }
2146 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2147 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002148 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002149 GR_GL_TEXTURE_WRAP_S,
2150 newTexParams.fWrapS));
2151 }
2152 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2153 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002154 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002155 GR_GL_TEXTURE_WRAP_T,
2156 newTexParams.fWrapT));
2157 }
2158 if (this->glCaps().fTextureSwizzle &&
2159 (setAll ||
2160 memcmp(newTexParams.fSwizzleRGBA,
2161 oldTexParams.fSwizzleRGBA,
2162 sizeof(newTexParams.fSwizzleRGBA)))) {
2163 setTextureUnit(s);
2164 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2165 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002166 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002167 nextTexture->setCachedTexParams(newTexParams,
2168 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002169 }
2170 }
2171
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002172 GrIRect* rect = NULL;
2173 GrIRect clipBounds;
2174 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2175 fClip.hasConservativeBounds()) {
2176 fClip.getConservativeBounds().roundOut(&clipBounds);
2177 rect = &clipBounds;
2178 }
2179 this->flushRenderTarget(rect);
2180 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002181
reed@google.comac10a2d2010-12-22 21:39:39 +00002182 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2183 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2184 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002185 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002186 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002187 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002188 }
2189 }
2190
bsalomon@google.comd302f142011-03-03 13:54:13 +00002191 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2192 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002193 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002194 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002195 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002196 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002197 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002198 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002199 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002200 }
2201
bsalomon@google.comd302f142011-03-03 13:54:13 +00002202 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2203 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002204 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002205 GL_CALL(Enable(GR_GL_CULL_FACE));
2206 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002207 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002208 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002209 GL_CALL(Enable(GR_GL_CULL_FACE));
2210 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002211 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002212 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002213 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002214 break;
2215 default:
2216 GrCrash("Unknown draw face.");
2217 }
2218 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2219 }
2220
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002221#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002222 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002223 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002224 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002225 NULL == fCurrDrawState.fRenderTarget ||
2226 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002227 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002228 fCurrDrawState.fRenderTarget);
2229 }
2230#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002231
reed@google.comac10a2d2010-12-22 21:39:39 +00002232 flushStencil();
2233
bsalomon@google.comd302f142011-03-03 13:54:13 +00002234 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002235 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002236 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002237}
2238
2239void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002240 if (fHWGeometryState.fVertexBuffer != buffer) {
2241 fHWGeometryState.fArrayPtrsDirty = true;
2242 fHWGeometryState.fVertexBuffer = buffer;
2243 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002244}
2245
2246void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002247 if (fHWGeometryState.fVertexBuffer == buffer) {
2248 // deleting bound buffer does implied bind to 0
2249 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002250 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 }
2252}
2253
2254void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002255 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002256}
2257
2258void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 if (fHWGeometryState.fIndexBuffer == buffer) {
2260 // deleting bound buffer does implied bind to 0
2261 fHWGeometryState.fIndexBuffer = NULL;
2262 }
2263}
2264
reed@google.comac10a2d2010-12-22 21:39:39 +00002265void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2266 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002267 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002268 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002269 }
2270 if (fHWDrawState.fRenderTarget == renderTarget) {
2271 fHWDrawState.fRenderTarget = NULL;
2272 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002273}
2274
2275void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002276 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002277 if (fCurrDrawState.fTextures[s] == texture) {
2278 fCurrDrawState.fTextures[s] = NULL;
2279 }
2280 if (fHWDrawState.fTextures[s] == texture) {
2281 // deleting bound texture does implied bind to 0
2282 fHWDrawState.fTextures[s] = NULL;
2283 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002284 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002285}
2286
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002287bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002288 GrGLenum* internalFormat,
2289 GrGLenum* format,
2290 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002291 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002292 case kRGBA_8888_PM_GrPixelConfig:
2293 case kRGBA_8888_UPM_GrPixelConfig:
2294 *format = GR_GL_RGBA;
2295 *internalFormat = GR_GL_RGBA;
2296 *type = GR_GL_UNSIGNED_BYTE;
2297 break;
2298 case kBGRA_8888_PM_GrPixelConfig:
2299 case kBGRA_8888_UPM_GrPixelConfig:
2300 if (!fGLCaps.fBGRAFormat) {
2301 return false;
2302 }
2303 *format = GR_GL_BGRA;
2304 if (fGLCaps.fBGRAInternalFormat) {
2305 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002306 } else {
2307 *internalFormat = GR_GL_RGBA;
2308 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002309 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002310 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002311 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002312 *format = GR_GL_RGB;
2313 *internalFormat = GR_GL_RGB;
2314 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002315 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002316 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002317 *format = GR_GL_RGBA;
2318 *internalFormat = GR_GL_RGBA;
2319 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002320 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002321 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002322 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002323 *format = GR_GL_PALETTE8_RGBA8;
2324 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002325 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002326 } else {
2327 return false;
2328 }
2329 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002330 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002331 *format = GR_GL_ALPHA;
2332 *internalFormat = GR_GL_ALPHA;
2333 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002334 break;
2335 default:
2336 return false;
2337 }
2338 return true;
2339}
2340
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002341void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002342 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002343 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002344 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002345 fActiveTextureUnitIdx = unit;
2346 }
2347}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002348
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002349void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002350 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002351 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002352 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2353 }
2354}
2355
reed@google.comac10a2d2010-12-22 21:39:39 +00002356/* On ES the internalFormat and format must match for TexImage and we use
2357 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2358 decide the internalFormat. However, on ES internalFormat for
2359 RenderBufferStorage* has to be a specific format (not a base format like
2360 GL_RGBA).
2361 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002362bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002363 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002364 // The ES story for BGRA and RenderbufferStorage appears murky. It
2365 // takes an internal format as a parameter. The OES FBO extension and
2366 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2367 // BGRA extensions adds BGRA as both an internal and external format
2368 // and the other only as an external format (like desktop GL). OES
2369 // restricts RenderbufferStorage's format to a *sized* internal format.
2370 // There is no sized BGRA internal format.
2371 // So if the texture has internal format BGRA we just hope that the
2372 // resolve blit can do RGBA->BGRA internal format conversion.
2373 case kRGBA_8888_PM_GrPixelConfig:
2374 case kRGBA_8888_UPM_GrPixelConfig:
2375 case kBGRA_8888_PM_GrPixelConfig:
2376 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002377 if (fGLCaps.fRGBA8Renderbuffer) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002378 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2379 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002380 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002381 return true;
2382 } else {
2383 return false;
2384 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002385 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002386 // ES2 supports 565. ES1 supports it
2387 // with FBO extension desktop GL has
2388 // no such internal format
bsalomon@google.comc4364992011-11-07 15:54:49 +00002389 if (kDesktop_GrGLBinding != this->glBinding()) {
2390 *format = GR_GL_RGB565;
2391 return true;
2392 } else {
2393 return false;
2394 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002395 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002396 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002397 return true;
2398 default:
2399 return false;
2400 }
2401}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002402
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002403void GrGpuGL::resetDirtyFlags() {
2404 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2405}
2406
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002407void GrGpuGL::setBuffers(bool indexed,
2408 int* extraVertexOffset,
2409 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002410
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002411 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002412
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002413 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2414
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002415 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002416 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002417 case kBuffer_GeometrySrcType:
2418 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002419 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002420 break;
2421 case kArray_GeometrySrcType:
2422 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002423 this->finalizeReservedVertices();
2424 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2425 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002426 break;
2427 default:
2428 vbuf = NULL; // suppress warning
2429 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002430 }
2431
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002432 GrAssert(NULL != vbuf);
2433 GrAssert(!vbuf->isLocked());
2434 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002435 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002436 fHWGeometryState.fArrayPtrsDirty = true;
2437 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002438 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439
2440 if (indexed) {
2441 GrAssert(NULL != extraIndexOffset);
2442
2443 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002444 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 case kBuffer_GeometrySrcType:
2446 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002447 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002448 break;
2449 case kArray_GeometrySrcType:
2450 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002451 this->finalizeReservedIndices();
2452 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2453 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002454 break;
2455 default:
2456 ibuf = NULL; // suppress warning
2457 GrCrash("Unknown geometry src type!");
2458 }
2459
2460 GrAssert(NULL != ibuf);
2461 GrAssert(!ibuf->isLocked());
2462 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002463 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002464 fHWGeometryState.fIndexBuffer = ibuf;
2465 }
2466 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002467}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002468
2469int GrGpuGL::getMaxEdges() const {
2470 // FIXME: This is a pessimistic estimate based on how many other things
2471 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002472 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2473 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002474}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002475
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002476void GrGpuGL::GLCaps::print() const {
2477 for (int i = 0; i < fStencilFormats.count(); ++i) {
2478 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2479 i,
2480 fStencilFormats[i].fStencilBits,
2481 fStencilFormats[i].fTotalBits);
2482 }
2483
2484 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2485 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2486 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2487 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2488 static const char* gMSFBOExtStr[] = {
2489 "None",
2490 "ARB",
2491 "EXT",
2492 "Apple",
2493 };
2494 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002495 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002496 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2497 }
2498 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2499 GrPrintf("Support RGBA8 Render Buffer: %s\n",
2500 (fRGBA8Renderbuffer ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002501 GrPrintf("BGRA is an internal format: %s\n",
2502 (fBGRAInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002503 GrPrintf("Support texture swizzle: %s\n",
2504 (fTextureSwizzle ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002505}