blob: dc4d78ac43626d2ca2cddabc0da843cf3f3bb186 [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 {
387 fGLCaps.fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
388 }
389
390
391 if (kDesktop_GrGLBinding != this->glBinding()) {
392 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
393 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
394 }
395 }
396
397 if (kDesktop_GrGLBinding == this->glBinding()) {
398 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
399 // extension includes glMapBuffer.
400 } else {
401 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
402 }
403
404 if (kDesktop_GrGLBinding == this->glBinding()) {
405 if (fGLVersion >= GR_GL_VER(2,0) ||
406 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
407 fCaps.fNPOTTextureTileSupport = true;
408 fCaps.fNPOTTextureSupport = true;
409 } else {
410 fCaps.fNPOTTextureTileSupport = false;
411 fCaps.fNPOTTextureSupport = false;
412 }
413 } else {
414 if (fGLVersion >= GR_GL_VER(2,0)) {
415 fCaps.fNPOTTextureSupport = true;
416 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
417 } else {
418 fCaps.fNPOTTextureSupport =
419 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
420 fCaps.fNPOTTextureTileSupport = false;
421 }
422 }
423
424 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
425
426 ////////////////////////////////////////////////////////////////////////////
427 // Experiments to determine limitations that can't be queried.
428 // TODO: Make these a preprocess that generate some compile time constants.
429 // TODO: probe once at startup, rather than once per context creation.
430
431 int expectNPOTTargets = fGL->fNPOTRenderTargetSupport;
432 if (expectNPOTTargets == kProbe_GrGLCapability) {
433 fCaps.fNPOTRenderTargetSupport =
434 probe_for_npot_render_target_support(fGL, fCaps.fNPOTTextureSupport);
435 } else {
436 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000437 fCaps.fNPOTRenderTargetSupport = (0 != expectNPOTTargets);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000438 }
439
440 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
441 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
442 // Our render targets are always created with textures as the color
443 // attachment, hence this min:
444 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
445
446 fCaps.fMinRenderTargetHeight = fGL->fMinRenderTargetHeight;
447 if (fCaps.fMinRenderTargetHeight == kProbe_GrGLCapability) {
448 fCaps.fMinRenderTargetHeight =
449 probe_for_min_render_target_height(fGL, fCaps.fNPOTRenderTargetSupport,
450 fCaps.fMaxRenderTargetSize);
451 }
452
453 fCaps.fMinRenderTargetWidth = fGL->fMinRenderTargetWidth;
454 if (fCaps.fMinRenderTargetWidth == kProbe_GrGLCapability) {
455 fCaps.fMinRenderTargetWidth =
456 probe_for_min_render_target_width(fGL, fCaps.fNPOTRenderTargetSupport,
457 fCaps.fMaxRenderTargetSize);
458 }
459
460 this->initFSAASupport();
461 this->initStencilFormats();
462}
463
464void GrGpuGL::initFSAASupport() {
465 // TODO: Get rid of GrAALevel and use # samples directly.
466 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
467 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
468 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
469 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
470 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
471
472 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
473 if (kDesktop_GrGLBinding != this->glBinding()) {
474 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
475 // chrome's extension is equivalent to the EXT msaa
476 // and fbo_blit extensions.
477 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
478 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
479 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
480 }
481 } else {
482 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
483 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
484 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
485 this->hasExtension("GL_EXT_framebuffer_blit")) {
486 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
487 }
488 }
489
490 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
491 GrGLint maxSamples;
492 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
493 if (maxSamples > 1 ) {
494 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
495 fGLCaps.fAASamples[kLow_GrAALevel] =
496 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
497 fGLCaps.fAASamples[kMed_GrAALevel] =
498 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
499 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
500 }
501 }
502 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
503}
504
505void GrGpuGL::initStencilFormats() {
506
507 // Build up list of legal stencil formats (though perhaps not supported on
508 // the particular gpu/driver) from most preferred to least.
509
510 // these consts are in order of most preferred to least preferred
511 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
512 static const GrGLStencilBuffer::Format
513 // internal Format stencil bits total bits packed?
514 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
515 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
516 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
517 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
518 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
519 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
520
521 if (kDesktop_GrGLBinding == this->glBinding()) {
522 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
523 this->hasExtension("GL_EXT_packed_depth_stencil") ||
524 this->hasExtension("GL_ARB_framebuffer_object");
525
526 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
527 // require FBO support we can expect these are legal formats and don't
528 // check. These also all support the unsized GL_STENCIL_INDEX.
529 fGLCaps.fStencilFormats.push_back() = gS8;
530 fGLCaps.fStencilFormats.push_back() = gS16;
531 if (supportsPackedDS) {
532 fGLCaps.fStencilFormats.push_back() = gD24S8;
533 }
534 fGLCaps.fStencilFormats.push_back() = gS4;
535 if (supportsPackedDS) {
536 fGLCaps.fStencilFormats.push_back() = gDS;
537 }
538 } else {
539 // ES2 has STENCIL_INDEX8 without extensions.
540 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
541 // introduces tokens for S1 thu S8 but there are separate extensions
542 // that make them legal (GL_OES_stencil1, ...).
543 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
544 // ES doesn't support using the unsized formats.
545
546 if (fGLVersion >= GR_GL_VER(2,0) ||
547 this->hasExtension("GL_OES_stencil8")) {
548 fGLCaps.fStencilFormats.push_back() = gS8;
549 }
550 //fStencilFormats.push_back() = gS16;
551 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
552 fGLCaps.fStencilFormats.push_back() = gD24S8;
553 }
554 if (this->hasExtension("GL_OES_stencil4")) {
555 fGLCaps.fStencilFormats.push_back() = gS4;
556 }
557 // we require some stencil format.
558 GrAssert(fGLCaps.fStencilFormats.count() > 0);
559 }
560}
561
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000562void GrGpuGL::resetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000563 if (gPrintStartupSpew && !fPrintedCaps) {
564 fPrintedCaps = true;
565 this->getCaps().print();
566 fGLCaps.print();
567 }
568
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000569 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000570 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000572
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000573 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000574 GL_CALL(Disable(GR_GL_DEPTH_TEST));
575 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000576
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GL_CALL(Disable(GR_GL_CULL_FACE));
578 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000579 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(Disable(GR_GL_DITHER));
582 if (kDesktop_GrGLBinding == this->glBinding()) {
583 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
584 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
585 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000586 fHWAAState.fMSAAEnabled = false;
587 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000588 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000589
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000590 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000591 fHWDrawState.fFlagBits = 0;
592
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000594 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000595
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000596 // invalid
597 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000598
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000600 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
601 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000602
603 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000604 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000607
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000608 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000609
tomhudson@google.com93813632011-10-27 20:21:16 +0000610 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000611 fHWDrawState.fTextures[s] = NULL;
612 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
613 -GR_ScalarMax,
614 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000615 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000616 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000617 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000618
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000619 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000620 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000621 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000622 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000623
bsalomon@google.comd302f142011-03-03 13:54:13 +0000624 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000626 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000627
628 fHWGeometryState.fIndexBuffer = NULL;
629 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000630
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000631 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000632
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000633 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000634 fHWDrawState.fRenderTarget = NULL;
635}
636
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000637GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
638
639 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
640 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
641 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
642 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
643
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000644 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000645 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000646
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000647 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000648 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000649 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000650 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000651 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000652 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000653 } else {
654 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000655 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000656 }
657 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000658 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000659 }
660 // we don't know what the RB ids are without glGets and we don't care
661 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000662 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000663 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000664 if (desc.fStencilBits) {
665 GrGLStencilBuffer::Format format;
666 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
667 format.fPacked = false;
668 format.fStencilBits = desc.fStencilBits;
669 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000670 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
671 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000672 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000673 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000674 }
675
676 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000677 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000678 GrGLenum dontCare;
679 if (!canBeTexture(desc.fConfig, &dontCare,
680 &texDesc.fUploadFormat,
681 &texDesc.fUploadType)) {
682 return NULL;
683 }
684
685 GrGLTexture::TexParams params;
686
687 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
688 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
689
bsalomon@google.com90405932011-06-17 15:56:55 +0000690 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000691 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000692 texDesc.fTextureID = desc.fPlatformTexture;
693 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
694 texDesc.fOwnsID = false;
695
696 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000697 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000698 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000699 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000700 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000701 } else {
702 return new GrGLTexture(this, texDesc, params);
703 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000704 } else {
705 GrGLIRect viewport;
706 viewport.fLeft = 0;
707 viewport.fBottom = 0;
708 viewport.fWidth = desc.fWidth;
709 viewport.fHeight = desc.fHeight;
710
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000711 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000712 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000713 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000714 }
715}
716
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000717
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000718////////////////////////////////////////////////////////////////////////////////
719
720void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
721 GrGLenum internalFormat,
722 const void* data,
723 size_t rowBytes) {
724 // we assume the texture is bound;
725 if (!rowBytes) {
726 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
727 }
728
729 // in case we need a temporary, trimmed copy of the src pixels
730 SkAutoSMalloc<128 * 128> tempStorage;
731
732 /*
733 * check whether to allocate a temporary buffer for flipping y or
734 * because our data has extra bytes past each row. If so, we need
735 * to trim those off here, since GL ES doesn't let us specify
736 * GL_UNPACK_ROW_LENGTH.
737 */
738 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000739 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000740 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000741 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
742 rowBytes / desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000743 }
744 } else {
745 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
746 if (data && (trimRowBytes < rowBytes || flipY)) {
747 // copy the data into our new storage, skipping the trailing bytes
748 size_t trimSize = desc.fContentHeight * trimRowBytes;
749 const char* src = (const char*)data;
750 if (flipY) {
751 src += (desc.fContentHeight - 1) * rowBytes;
752 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000753 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000754 for (int y = 0; y < desc.fContentHeight; y++) {
755 memcpy(dst, src, trimRowBytes);
756 if (flipY) {
757 src -= rowBytes;
758 } else {
759 src += rowBytes;
760 }
761 dst += trimRowBytes;
762 }
763 // now point data to our trimmed version
764 data = tempStorage.get();
765 rowBytes = trimRowBytes;
766 }
767 }
768
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000769 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 if (kIndex_8_GrPixelConfig == desc.fFormat &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000771 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000772 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
773 GrAssert(desc.fContentWidth == desc.fAllocWidth);
774 GrAssert(desc.fContentHeight == desc.fAllocHeight);
775 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
776 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000777 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
778 desc.fAllocWidth, desc.fAllocHeight,
779 0, imageSize, data));
780 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000781 } else {
782 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
783 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000784 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
785 desc.fAllocWidth, desc.fAllocHeight,
786 0, desc.fUploadFormat, desc.fUploadType, NULL));
787 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
788 desc.fContentHeight, desc.fUploadFormat,
789 desc.fUploadType, data));
790 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000791
792 int extraW = desc.fAllocWidth - desc.fContentWidth;
793 int extraH = desc.fAllocHeight - desc.fContentHeight;
794 int maxTexels = extraW * extraH;
795 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
796 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
797
798 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
799
800 // rowBytes is actual stride between rows in data
801 // rowDataBytes is the actual amount of non-pad data in a row
802 // and the stride used for uploading extraH rows.
803 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
804 if (extraH) {
805 uint8_t* lastRowStart = (uint8_t*) data +
806 (desc.fContentHeight - 1) * rowBytes;
807 uint8_t* extraRowStart = (uint8_t*)texels.get();
808
809 for (int i = 0; i < extraH; ++i) {
810 memcpy(extraRowStart, lastRowStart, rowDataBytes);
811 extraRowStart += rowDataBytes;
812 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000813 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
814 desc.fContentHeight, desc.fContentWidth,
815 extraH, desc.fUploadFormat,
816 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000817 }
818 if (extraW) {
819 uint8_t* edgeTexel = (uint8_t*)data +
820 rowDataBytes - desc.fUploadByteCount;
821 uint8_t* extraTexel = (uint8_t*)texels.get();
822 for (int j = 0; j < desc.fContentHeight; ++j) {
823 for (int i = 0; i < extraW; ++i) {
824 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
825 extraTexel += desc.fUploadByteCount;
826 }
827 edgeTexel += rowBytes;
828 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000829 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
830 0, extraW, desc.fContentHeight,
831 desc.fUploadFormat, desc.fUploadType,
832 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000833 }
834 if (extraW && extraH) {
835 uint8_t* cornerTexel = (uint8_t*)data +
836 desc.fContentHeight * rowBytes -
837 desc.fUploadByteCount;
838 uint8_t* extraTexel = (uint8_t*)texels.get();
839 for (int i = 0; i < extraW*extraH; ++i) {
840 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
841 extraTexel += desc.fUploadByteCount;
842 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000843 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
844 desc.fContentHeight, extraW, extraH,
845 desc.fUploadFormat, desc.fUploadType,
846 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000847 }
848
849 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000850 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
851 desc.fAllocWidth, desc.fAllocHeight, 0,
852 desc.fUploadFormat, desc.fUploadType, data));
853 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000854 }
855 }
856}
857
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000858bool GrGpuGL::createRenderTargetObjects(int width, int height,
859 GrGLuint texID,
860 GrGLRenderTarget::Desc* desc) {
861 desc->fMSColorRenderbufferID = 0;
862 desc->fRTFBOID = 0;
863 desc->fTexFBOID = 0;
864 desc->fOwnIDs = true;
865
866 GrGLenum status;
867 GrGLint err;
868
bsalomon@google.comab15d612011-08-09 12:57:56 +0000869 GrGLenum msColorFormat = 0; // suppress warning
870
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000871 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000872 if (!desc->fTexFBOID) {
873 goto FAILED;
874 }
875
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876
877 // If we are using multisampling we will create two FBOS. We render
878 // to one and then resolve to the texture bound to the other.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000879 if (desc->fSampleCnt > 1 && GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000880 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
881 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000882 if (!desc->fRTFBOID ||
883 !desc->fMSColorRenderbufferID ||
884 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
885 goto FAILED;
886 }
887 } else {
888 desc->fRTFBOID = desc->fTexFBOID;
889 }
890
891 if (desc->fRTFBOID != desc->fTexFBOID) {
892 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000893 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000894 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000895 GR_GL_CALL_NOERRCHECK(this->glInterface(),
896 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
897 desc->fSampleCnt,
898 msColorFormat,
899 width, height));
900 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000901 if (err != GR_GL_NO_ERROR) {
902 goto FAILED;
903 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000904 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
905 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000906 GR_GL_COLOR_ATTACHMENT0,
907 GR_GL_RENDERBUFFER,
908 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000909 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
911 goto FAILED;
912 }
913 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000914 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000916 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
917 GR_GL_COLOR_ATTACHMENT0,
918 GR_GL_TEXTURE_2D,
919 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000920 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
922 goto FAILED;
923 }
924
925 return true;
926
927FAILED:
928 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000929 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000930 }
931 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000932 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000933 }
934 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000936 }
937 return false;
938}
939
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000940// good to set a break-point here to know when createTexture fails
941static GrTexture* return_null_texture() {
942// GrAssert(!"null texture");
943 return NULL;
944}
945
946#if GR_DEBUG
947static size_t as_size_t(int x) {
948 return x;
949}
950#endif
951
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000952GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000953 const void* srcData,
954 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000955
956#if GR_COLLECT_STATS
957 ++fStats.fTextureCreateCnt;
958#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000959
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000960 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000961 GR_GL_NEAREST,
962 GR_GL_CLAMP_TO_EDGE,
963 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000964 };
reed@google.com1fcd51e2011-01-05 15:50:27 +0000965
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000966 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000967 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000968 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +0000969
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000970 glTexDesc.fContentWidth = desc.fWidth;
971 glTexDesc.fContentHeight = desc.fHeight;
972 glTexDesc.fAllocWidth = desc.fWidth;
973 glTexDesc.fAllocHeight = desc.fHeight;
974 glTexDesc.fFormat = desc.fFormat;
975 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000976
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000977 glRTDesc.fMSColorRenderbufferID = 0;
978 glRTDesc.fRTFBOID = 0;
979 glRTDesc.fTexFBOID = 0;
980 glRTDesc.fOwnIDs = true;
981 glRTDesc.fConfig = glTexDesc.fFormat;
982
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000983 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 if (!canBeTexture(desc.fFormat,
985 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000986 &glTexDesc.fUploadFormat,
987 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000988 return return_null_texture();
989 }
990
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000991 const Caps& caps = this->getCaps();
992
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000993 // We keep GrRenderTargets in GL's normal orientation so that they
994 // can be drawn to by the outside world without the client having
995 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000996 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000997 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000998
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000999 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1000 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1001 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1002 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001003 GrPrintf("AA RT requested but not supported on this platform.");
1004 }
1005
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001006 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001007
reed@google.comac10a2d2010-12-22 21:39:39 +00001008 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001009 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001010 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1011 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001012 }
1013
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001014 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001015 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001016 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001017 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001018 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1019 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001020 return return_null_texture();
1021 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001022 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001023 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1024 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001025 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1026 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001027 return return_null_texture();
1028 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 }
1030
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001031 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001032 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001033 return return_null_texture();
1034 }
1035
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001036 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001037 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1038 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1039 GR_GL_TEXTURE_MAG_FILTER,
1040 DEFAULT_PARAMS.fFilter));
1041 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1042 GR_GL_TEXTURE_MIN_FILTER,
1043 DEFAULT_PARAMS.fFilter));
1044 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1045 GR_GL_TEXTURE_WRAP_S,
1046 DEFAULT_PARAMS.fWrapS));
1047 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1048 GR_GL_TEXTURE_WRAP_T,
1049 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001050
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001051 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001052
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001053 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001055 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001056#if GR_COLLECT_STATS
1057 ++fStats.fRenderTargetCreateCnt;
1058#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001059 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1060 glTexDesc.fAllocHeight,
1061 glTexDesc.fTextureID,
1062 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001063 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 return return_null_texture();
1065 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001066 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1067 } else {
1068 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 }
1070#ifdef TRACE_TEXTURE_CREATION
1071 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1072 tex->fTextureID, width, height, tex->fUploadByteCount);
1073#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 return tex;
1075}
1076
1077namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1079 GrGLuint rb,
1080 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001081 // we shouldn't ever know one size and not the other
1082 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1083 (kUnknownBitCount == format->fTotalBits));
1084 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001085 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1087 (GrGLint*)&format->fStencilBits);
1088 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001089 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001090 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1091 (GrGLint*)&format->fTotalBits);
1092 format->fTotalBits += format->fStencilBits;
1093 } else {
1094 format->fTotalBits = format->fStencilBits;
1095 }
1096 }
1097}
1098}
1099
1100bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1101 int width, int height) {
1102
1103 // All internally created RTs are also textures. We don't create
1104 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1105 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001106 GrAssert(width >= rt->allocatedWidth());
1107 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001108
1109 int samples = rt->numSamples();
1110 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 if (!sbID) {
1113 return false;
1114 }
1115
1116 GrGLStencilBuffer* sb = NULL;
1117
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001118 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 // we start with the last stencil format that succeeded in hopes
1122 // that we won't go through this loop more than once after the
1123 // first (painful) stencil creation.
1124 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001125 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001126 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 // version on a GL that doesn't have an MSAA extension.
1128 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001129 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1130 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001131 GR_GL_RENDERBUFFER,
1132 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001133 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 width,
1135 height));
1136 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001137 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1138 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001139 sFmt.fInternalFormat,
1140 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141 }
1142
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001143 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 if (err == GR_GL_NO_ERROR) {
1145 // After sized formats we attempt an unsized format and take whatever
1146 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001147 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001148 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001149 sb = new GrGLStencilBuffer(this, sbID, width, height,
1150 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1152 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001153 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 return true;
1156 }
1157 sb->abandon(); // otherwise we lose sbID
1158 sb->unref();
1159 }
1160 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001161 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001162 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163}
1164
1165bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1166 GrRenderTarget* rt) {
1167 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1168
1169 GrGLuint fbo = glrt->renderFBOID();
1170
1171 if (NULL == sb) {
1172 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001173 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 GR_GL_STENCIL_ATTACHMENT,
1175 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001176 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 GR_GL_DEPTH_ATTACHMENT,
1178 GR_GL_RENDERBUFFER, 0));
1179#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001180 GrGLenum status;
1181 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1183#endif
1184 }
1185 return true;
1186 } else {
1187 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1188 GrGLuint rb = glsb->renderbufferID();
1189
reed@google.comac10a2d2010-12-22 21:39:39 +00001190 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1192 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 GR_GL_STENCIL_ATTACHMENT,
1194 GR_GL_RENDERBUFFER, rb));
1195 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001196 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 GR_GL_DEPTH_ATTACHMENT,
1198 GR_GL_RENDERBUFFER, rb));
1199 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001200 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 GR_GL_DEPTH_ATTACHMENT,
1202 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001203 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001205 GrGLenum status;
1206 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001208 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209 GR_GL_STENCIL_ATTACHMENT,
1210 GR_GL_RENDERBUFFER, 0));
1211 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001212 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213 GR_GL_DEPTH_ATTACHMENT,
1214 GR_GL_RENDERBUFFER, 0));
1215 }
1216 return false;
1217 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 return true;
1219 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001220 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001221}
1222
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001223////////////////////////////////////////////////////////////////////////////////
1224
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001225GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001226 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001229 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001230 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001233 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1234 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1235 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1236 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1237 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 // deleting bound buffer does implicit bind to 0
1239 fHWGeometryState.fVertexBuffer = NULL;
1240 return NULL;
1241 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001242 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001243 size, dynamic);
1244 fHWGeometryState.fVertexBuffer = vertexBuffer;
1245 return vertexBuffer;
1246 }
1247 return NULL;
1248}
1249
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001250GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001251 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001252 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001254 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1255 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001257 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1258 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1259 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1260 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1261 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 // deleting bound buffer does implicit bind to 0
1263 fHWGeometryState.fIndexBuffer = NULL;
1264 return NULL;
1265 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001266 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 size, dynamic);
1268 fHWGeometryState.fIndexBuffer = indexBuffer;
1269 return indexBuffer;
1270 }
1271 return NULL;
1272}
1273
reed@google.comac10a2d2010-12-22 21:39:39 +00001274void GrGpuGL::flushScissor(const GrIRect* rect) {
1275 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001276 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001277 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001278
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001279 GrGLIRect scissor;
1280 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001281 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001282 rect->width(), rect->height());
1283 if (scissor.contains(vp)) {
1284 rect = NULL;
1285 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 }
1287
1288 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001290 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 fHWBounds.fScissorRect = scissor;
1292 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001294 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 fHWBounds.fScissorEnabled = true;
1296 }
1297 } else {
1298 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001299 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001300 fHWBounds.fScissorEnabled = false;
1301 }
1302 }
1303}
1304
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001305void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001306 if (NULL == fCurrDrawState.fRenderTarget) {
1307 return;
1308 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001309 GrIRect r;
1310 if (NULL != rect) {
1311 // flushScissor expects rect to be clipped to the target.
1312 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001313 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1314 fCurrDrawState.fRenderTarget->height());
1315 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001316 rect = &r;
1317 } else {
1318 return;
1319 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001321 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001322 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001323 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001324 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001325 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1326 GrColorUnpackG(color)/255.f,
1327 GrColorUnpackB(color)/255.f,
1328 GrColorUnpackA(color)/255.f));
1329 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001330}
1331
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001332void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001333 if (NULL == fCurrDrawState.fRenderTarget) {
1334 return;
1335 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001336
1337 this->flushRenderTarget(&GrIRect::EmptyIRect());
1338
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001340 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001341 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001342 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001343 GL_CALL(StencilMask(0xffffffff));
1344 GL_CALL(ClearStencil(0));
1345 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001346 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001347}
1348
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001349void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001350 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001351
1352 // this should only be called internally when we know we have a
1353 // stencil buffer.
1354 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001355 GrGLint stencilBitCount =
1356 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001357#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001359 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001360#else
1361 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001362 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001363 // turned into draws. Our contract on GrDrawTarget says that
1364 // changing the clip between stencil passes may or may not
1365 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001366 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001367#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001368 GrGLint value;
1369 if (insideClip) {
1370 value = (1 << (stencilBitCount - 1));
1371 } else {
1372 value = 0;
1373 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001374 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001375 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001376 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001377 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001378 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001379 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001380}
1381
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001382void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001383 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001384}
1385
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001386bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.com1a8ddf02011-11-02 19:34:16 +00001387 int left, int top,
1388 int width, int height,
1389 GrPixelConfig config,
1390 void* buffer, size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001391 GrGLenum internalFormat; // we don't use this for glReadPixels
1392 GrGLenum format;
1393 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001394 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1395 return false;
bsalomon@google.com1a8ddf02011-11-02 19:34:16 +00001396 }
1397
1398 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001399 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1400 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1401 switch (tgt->getResolveType()) {
1402 case GrGLRenderTarget::kCantResolve_ResolveType:
1403 return false;
1404 case GrGLRenderTarget::kAutoResolves_ResolveType:
1405 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1406 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001407 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001408 break;
1409 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001410 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001411 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001412 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1413 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001414 break;
1415 default:
1416 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001417 }
1418
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001419 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001420
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001421 // the read rect is viewport-relative
1422 GrGLIRect readRect;
1423 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.com1a8ddf02011-11-02 19:34:16 +00001424
1425 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1426 if (0 == rowBytes) {
1427 rowBytes = tightRowBytes;
1428 }
1429 size_t readDstRowBytes = tightRowBytes;
1430 void* readDst = buffer;
1431
1432 // determine if GL can read using the passed rowBytes or if we need
1433 // a scratch buffer.
1434 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1435 if (rowBytes != tightRowBytes) {
1436 if (kDesktop_GrGLBinding == this->glBinding()) {
1437 GrAssert(!(rowBytes % sizeof(GrColor)));
1438 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1439 readDstRowBytes = rowBytes;
1440 } else {
1441 scratch.reset(tightRowBytes * height);
1442 readDst = scratch.get();
1443 }
1444 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001445 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1446 readRect.fWidth, readRect.fHeight,
bsalomon@google.com1a8ddf02011-11-02 19:34:16 +00001447 format, type, readDst));
1448 if (readDstRowBytes != tightRowBytes) {
1449 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1450 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001451
1452 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.com1a8ddf02011-11-02 19:34:16 +00001453 // API presents top-to-bottom. We must preserve the padding contents. Note
1454 // that the above readPixels did not overwrite the padding.
1455 if (readDst == buffer) {
1456 GrAssert(rowBytes == readDstRowBytes);
1457 scratch.reset(tightRowBytes);
1458 void* tmpRow = scratch.get();
1459 // flip y in-place by rows
reed@google.comac10a2d2010-12-22 21:39:39 +00001460 const int halfY = height >> 1;
1461 char* top = reinterpret_cast<char*>(buffer);
bsalomon@google.com1a8ddf02011-11-02 19:34:16 +00001462 char* bottom = top + (height - 1) * rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 for (int y = 0; y < halfY; y++) {
bsalomon@google.com1a8ddf02011-11-02 19:34:16 +00001464 memcpy(tmpRow, top, tightRowBytes);
1465 memcpy(top, bottom, tightRowBytes);
1466 memcpy(bottom, tmpRow, tightRowBytes);
1467 top += rowBytes;
1468 bottom -= rowBytes;
1469 }
1470 } else {
1471 GrAssert(readDst != buffer);
1472 // copy from readDst to buffer while flipping y
1473 const int halfY = height >> 1;
1474 const char* src = reinterpret_cast<const char*>(readDst);
1475 char* dst = reinterpret_cast<char*>(buffer) + (height-1) * rowBytes;
1476 for (int y = 0; y < height; y++) {
1477 memcpy(dst, src, tightRowBytes);
1478 src += readDstRowBytes;
1479 dst -= rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001480 }
1481 }
1482 return true;
1483}
1484
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001485void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001486
1487 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1488
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001489 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001490 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001491 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001492 #if GR_COLLECT_STATS
1493 ++fStats.fRenderTargetChngCnt;
1494 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001495 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001496 GrGLenum status;
1497 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001498 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001499 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 }
1501 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001502 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001503 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001504 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001505 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001506 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001507 fHWBounds.fViewportRect = vp;
1508 }
1509 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001510 if (NULL == bound || !bound->isEmpty()) {
1511 rt->flagAsNeedingResolve(bound);
1512 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001513}
1514
twiz@google.com0f31ca72011-03-18 17:38:11 +00001515GrGLenum gPrimitiveType2GLMode[] = {
1516 GR_GL_TRIANGLES,
1517 GR_GL_TRIANGLE_STRIP,
1518 GR_GL_TRIANGLE_FAN,
1519 GR_GL_POINTS,
1520 GR_GL_LINES,
1521 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001522};
1523
bsalomon@google.comd302f142011-03-03 13:54:13 +00001524#define SWAP_PER_DRAW 0
1525
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001526#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001527 #if GR_MAC_BUILD
1528 #include <AGL/agl.h>
1529 #elif GR_WIN32_BUILD
1530 void SwapBuf() {
1531 DWORD procID = GetCurrentProcessId();
1532 HWND hwnd = GetTopWindow(GetDesktopWindow());
1533 while(hwnd) {
1534 DWORD wndProcID = 0;
1535 GetWindowThreadProcessId(hwnd, &wndProcID);
1536 if(wndProcID == procID) {
1537 SwapBuffers(GetDC(hwnd));
1538 }
1539 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1540 }
1541 }
1542 #endif
1543#endif
1544
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001545void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1546 uint32_t startVertex,
1547 uint32_t startIndex,
1548 uint32_t vertexCount,
1549 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001550 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1551
twiz@google.com0f31ca72011-03-18 17:38:11 +00001552 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001553
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001554 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1555 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1556
1557 // our setupGeometry better have adjusted this to zero since
1558 // DrawElements always draws from the begining of the arrays for idx 0.
1559 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001560
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001561 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1562 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001563#if SWAP_PER_DRAW
1564 glFlush();
1565 #if GR_MAC_BUILD
1566 aglSwapBuffers(aglGetCurrentContext());
1567 int set_a_break_pt_here = 9;
1568 aglSwapBuffers(aglGetCurrentContext());
1569 #elif GR_WIN32_BUILD
1570 SwapBuf();
1571 int set_a_break_pt_here = 9;
1572 SwapBuf();
1573 #endif
1574#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001575}
1576
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001577void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1578 uint32_t startVertex,
1579 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001580 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1581
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001582 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1583
1584 // our setupGeometry better have adjusted this to zero.
1585 // DrawElements doesn't take an offset so we always adjus the startVertex.
1586 GrAssert(0 == startVertex);
1587
1588 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1589 // account for startVertex in the DrawElements case. So we always
1590 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001591 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001592#if SWAP_PER_DRAW
1593 glFlush();
1594 #if GR_MAC_BUILD
1595 aglSwapBuffers(aglGetCurrentContext());
1596 int set_a_break_pt_here = 9;
1597 aglSwapBuffers(aglGetCurrentContext());
1598 #elif GR_WIN32_BUILD
1599 SwapBuf();
1600 int set_a_break_pt_here = 9;
1601 SwapBuf();
1602 #endif
1603#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001604}
1605
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001606void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001607
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001608 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001609 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001611 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1612 rt->renderFBOID()));
1613 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1614 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001615 #if GR_COLLECT_STATS
1616 ++fStats.fRenderTargetChngCnt;
1617 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001618 // make sure we go through flushRenderTarget() since we've modified
1619 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001621 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001622 const GrIRect dirtyRect = rt->getResolveRect();
1623 GrGLIRect r;
1624 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1625 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001626
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001627 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001628 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001629 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1630 GL_CALL(Scissor(r.fLeft, r.fBottom,
1631 r.fWidth, r.fHeight));
1632 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001633 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001634 fHWBounds.fScissorEnabled = true;
1635 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001636 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001637 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001638 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1639 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001640 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001641 int right = r.fLeft + r.fWidth;
1642 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001643 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1644 r.fLeft, r.fBottom, right, top,
1645 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001647 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001648 }
1649}
1650
twiz@google.com0f31ca72011-03-18 17:38:11 +00001651static const GrGLenum grToGLStencilFunc[] = {
1652 GR_GL_ALWAYS, // kAlways_StencilFunc
1653 GR_GL_NEVER, // kNever_StencilFunc
1654 GR_GL_GREATER, // kGreater_StencilFunc
1655 GR_GL_GEQUAL, // kGEqual_StencilFunc
1656 GR_GL_LESS, // kLess_StencilFunc
1657 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1658 GR_GL_EQUAL, // kEqual_StencilFunc,
1659 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001660};
1661GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1662GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1663GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1664GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1665GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1666GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1667GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1668GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1669GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1670
twiz@google.com0f31ca72011-03-18 17:38:11 +00001671static const GrGLenum grToGLStencilOp[] = {
1672 GR_GL_KEEP, // kKeep_StencilOp
1673 GR_GL_REPLACE, // kReplace_StencilOp
1674 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1675 GR_GL_INCR, // kIncClamp_StencilOp
1676 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1677 GR_GL_DECR, // kDecClamp_StencilOp
1678 GR_GL_ZERO, // kZero_StencilOp
1679 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001680};
1681GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1682GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1683GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1684GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1685GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1686GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1687GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1688GR_STATIC_ASSERT(6 == kZero_StencilOp);
1689GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1690
reed@google.comac10a2d2010-12-22 21:39:39 +00001691void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001692 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001693
1694 // use stencil for clipping if clipping is enabled and the clip
1695 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001696 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001698 bool stencilChange = fHWStencilClip != stencilClip ||
1699 fHWDrawState.fStencilSettings != *settings ||
1700 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1701 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001702
1703 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001704
bsalomon@google.comd302f142011-03-03 13:54:13 +00001705 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1706 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001707
bsalomon@google.comd302f142011-03-03 13:54:13 +00001708 if (settings->isDisabled()) {
1709 if (stencilClip) {
1710 settings = &gClipStencilSettings;
1711 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001712 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001713
1714 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001715 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001716 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001717 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001718 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001719 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001720 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1721 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1722 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1723 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1724 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1725 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1726 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1727 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1728 }
1729 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001730 int stencilBits = 0;
1731 GrStencilBuffer* stencilBuffer =
1732 fCurrDrawState.fRenderTarget->getStencilBuffer();
1733 if (NULL != stencilBuffer) {
1734 stencilBits = stencilBuffer->bits();
1735 }
1736 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001737 GrAssert(stencilBits ||
1738 (GrStencilSettings::gDisabled ==
1739 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001740 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1741 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001742
1743 unsigned int frontRef = settings->fFrontFuncRef;
1744 unsigned int frontMask = settings->fFrontFuncMask;
1745 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001746 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001747
1748 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1749
1750 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1751 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1752 } else {
1753 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1754
1755 ConvertStencilFuncAndMask(settings->fFrontFunc,
1756 stencilClip,
1757 clipStencilMask,
1758 userStencilMask,
1759 &frontRef,
1760 &frontMask);
1761 frontWriteMask &= userStencilMask;
1762 }
1763 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001764 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001765 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001766 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001767 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001768 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001769 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001770 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001771 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001772 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001773
1774 unsigned int backRef = settings->fBackFuncRef;
1775 unsigned int backMask = settings->fBackFuncMask;
1776 unsigned int backWriteMask = settings->fBackWriteMask;
1777
1778
1779 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1780 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1781 backFunc = grToGLStencilFunc[settings->fBackFunc];
1782 } else {
1783 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1784 ConvertStencilFuncAndMask(settings->fBackFunc,
1785 stencilClip,
1786 clipStencilMask,
1787 userStencilMask,
1788 &backRef,
1789 &backMask);
1790 backWriteMask &= userStencilMask;
1791 }
1792
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001793 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1794 frontRef, frontMask));
1795 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1796 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1797 backRef, backMask));
1798 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1799 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1800 grToGLStencilOp[settings->fFrontFailOp],
1801 grToGLStencilOp[settings->fFrontPassOp],
1802 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001804 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1805 grToGLStencilOp[settings->fBackFailOp],
1806 grToGLStencilOp[settings->fBackPassOp],
1807 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001808 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001809 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1810 GL_CALL(StencilMask(frontWriteMask));
1811 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001812 grToGLStencilOp[settings->fFrontPassOp],
1813 grToGLStencilOp[settings->fFrontPassOp]));
1814 }
1815 }
1816 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001817 fHWStencilClip = stencilClip;
1818 }
1819}
1820
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001821void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001822 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001823 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1824 // smooth lines.
1825
1826 // we prefer smooth lines over multisampled lines
1827 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001828 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001829 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001830 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001831 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001832 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001833 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001834 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001835 fHWAAState.fSmoothLineEnabled = false;
1836 }
1837 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1838 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001839 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001840 fHWAAState.fMSAAEnabled = false;
1841 }
1842 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001843 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001844 fHWAAState.fMSAAEnabled) {
1845 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001846 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001847 fHWAAState.fMSAAEnabled = false;
1848 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001849 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001850 fHWAAState.fMSAAEnabled = true;
1851 }
1852 }
1853 }
1854}
1855
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001856void GrGpuGL::flushBlend(GrPrimitiveType type,
1857 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001858 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001859 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001860 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001861 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001862 fHWBlendDisabled = false;
1863 }
1864 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1865 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001866 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1867 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001868 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1869 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1870 }
1871 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001872 // any optimization to disable blending should
1873 // have already been applied and tweaked the coeffs
1874 // to (1, 0).
1875 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1876 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001877 if (fHWBlendDisabled != blendOff) {
1878 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001879 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001880 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001881 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001882 }
1883 fHWBlendDisabled = blendOff;
1884 }
1885 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001886 if (fHWDrawState.fSrcBlend != srcCoeff ||
1887 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001888 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1889 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001890 fHWDrawState.fSrcBlend = srcCoeff;
1891 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001892 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001893 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1894 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001895 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1896
1897 float c[] = {
1898 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1899 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1900 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1901 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1902 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001903 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001904 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1905 }
1906 }
1907 }
1908}
1909
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001910static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1911 switch (filter) {
1912 case GrSamplerState::kBilinear_Filter:
1913 case GrSamplerState::k4x4Downsample_Filter:
1914 return GR_GL_LINEAR;
1915 case GrSamplerState::kNearest_Filter:
1916 case GrSamplerState::kConvolution_Filter:
1917 return GR_GL_NEAREST;
1918 default:
1919 GrAssert(!"Unknown filter type");
1920 return GR_GL_LINEAR;
1921 }
1922}
1923
bsalomon@google.comffca4002011-02-22 20:34:01 +00001924bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001925
1926 // GrGpu::setupClipAndFlushState should have already checked this
1927 // and bailed if not true.
1928 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001929
tomhudson@google.com93813632011-10-27 20:21:16 +00001930 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001931 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001932 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001933 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001934
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001935 // true for now, but maybe not with GrEffect.
1936 GrAssert(NULL != nextTexture);
1937 // if we created a rt/tex and rendered to it without using a
1938 // texture and now we're texuring from the rt it will still be
1939 // the last bound texture, but it needs resolving. So keep this
1940 // out of the "last != next" check.
1941 GrGLRenderTarget* texRT =
1942 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1943 if (NULL != texRT) {
1944 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001945 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001946
1947 if (fHWDrawState.fTextures[s] != nextTexture) {
1948 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001949 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001950 #if GR_COLLECT_STATS
1951 ++fStats.fTextureChngCnt;
1952 #endif
1953 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1954 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001955 // The texture matrix has to compensate for texture width/height
1956 // and NPOT-embedded-in-POT
1957 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001958 }
1959
1960 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
1961 const GrGLTexture::TexParams& oldTexParams =
1962 nextTexture->getTexParams();
1963 GrGLTexture::TexParams newTexParams;
1964
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001965 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001966
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001967 const GrGLenum* wraps =
1968 GrGLTexture::WrapMode2GLWrap(this->glBinding());
1969 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1970 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001971
1972 if (newTexParams.fFilter != oldTexParams.fFilter) {
1973 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001974 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1975 GR_GL_TEXTURE_MAG_FILTER,
1976 newTexParams.fFilter));
1977 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1978 GR_GL_TEXTURE_MIN_FILTER,
1979 newTexParams.fFilter));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001980 }
1981 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
1982 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001983 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1984 GR_GL_TEXTURE_WRAP_S,
1985 newTexParams.fWrapS));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001986 }
1987 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
1988 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001989 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1990 GR_GL_TEXTURE_WRAP_T,
1991 newTexParams.fWrapT));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001992 }
1993 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00001994 }
1995 }
1996
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001997 GrIRect* rect = NULL;
1998 GrIRect clipBounds;
1999 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2000 fClip.hasConservativeBounds()) {
2001 fClip.getConservativeBounds().roundOut(&clipBounds);
2002 rect = &clipBounds;
2003 }
2004 this->flushRenderTarget(rect);
2005 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002006
reed@google.comac10a2d2010-12-22 21:39:39 +00002007 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2008 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2009 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002010 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002011 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002012 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002013 }
2014 }
2015
bsalomon@google.comd302f142011-03-03 13:54:13 +00002016 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2017 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002018 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002019 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002020 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002021 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002022 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002023 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002024 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002025 }
2026
bsalomon@google.comd302f142011-03-03 13:54:13 +00002027 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2028 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002029 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002030 GL_CALL(Enable(GR_GL_CULL_FACE));
2031 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002032 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002033 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002034 GL_CALL(Enable(GR_GL_CULL_FACE));
2035 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002036 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002037 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002038 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002039 break;
2040 default:
2041 GrCrash("Unknown draw face.");
2042 }
2043 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2044 }
2045
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002046#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002047 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002048 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002049 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002050 NULL == fCurrDrawState.fRenderTarget ||
2051 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002052 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002053 fCurrDrawState.fRenderTarget);
2054 }
2055#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002056
reed@google.comac10a2d2010-12-22 21:39:39 +00002057 flushStencil();
2058
bsalomon@google.comd302f142011-03-03 13:54:13 +00002059 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002060 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002061 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002062}
2063
2064void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002065 if (fHWGeometryState.fVertexBuffer != buffer) {
2066 fHWGeometryState.fArrayPtrsDirty = true;
2067 fHWGeometryState.fVertexBuffer = buffer;
2068 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002069}
2070
2071void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002072 if (fHWGeometryState.fVertexBuffer == buffer) {
2073 // deleting bound buffer does implied bind to 0
2074 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002075 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002076 }
2077}
2078
2079void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002080 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002081}
2082
2083void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002084 if (fHWGeometryState.fIndexBuffer == buffer) {
2085 // deleting bound buffer does implied bind to 0
2086 fHWGeometryState.fIndexBuffer = NULL;
2087 }
2088}
2089
reed@google.comac10a2d2010-12-22 21:39:39 +00002090void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2091 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002092 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002093 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002094 }
2095 if (fHWDrawState.fRenderTarget == renderTarget) {
2096 fHWDrawState.fRenderTarget = NULL;
2097 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002098}
2099
2100void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002101 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002102 if (fCurrDrawState.fTextures[s] == texture) {
2103 fCurrDrawState.fTextures[s] = NULL;
2104 }
2105 if (fHWDrawState.fTextures[s] == texture) {
2106 // deleting bound texture does implied bind to 0
2107 fHWDrawState.fTextures[s] = NULL;
2108 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002109 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002110}
2111
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002112bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002113 GrGLenum* internalFormat,
2114 GrGLenum* format,
2115 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002116 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002117 case kRGBA_8888_GrPixelConfig:
2118 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002119 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002120 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002121 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2122 // format for a BGRA is BGRA not RGBA (as on desktop)
2123 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2124 } else {
2125 *internalFormat = GR_GL_RGBA;
2126 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002127 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002128 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002129 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002130 *format = GR_GL_RGB;
2131 *internalFormat = GR_GL_RGB;
2132 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002133 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002134 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002135 *format = GR_GL_RGBA;
2136 *internalFormat = GR_GL_RGBA;
2137 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002138 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002139 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002140 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002141 *format = GR_GL_PALETTE8_RGBA8;
2142 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002143 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002144 } else {
2145 return false;
2146 }
2147 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002148 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002149 *format = GR_GL_ALPHA;
2150 *internalFormat = GR_GL_ALPHA;
2151 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002152 break;
2153 default:
2154 return false;
2155 }
2156 return true;
2157}
2158
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002159void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002160 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002161 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002162 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002163 fActiveTextureUnitIdx = unit;
2164 }
2165}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002166
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002167void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002168 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002169 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002170 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2171 }
2172}
2173
reed@google.comac10a2d2010-12-22 21:39:39 +00002174/* On ES the internalFormat and format must match for TexImage and we use
2175 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2176 decide the internalFormat. However, on ES internalFormat for
2177 RenderBufferStorage* has to be a specific format (not a base format like
2178 GL_RGBA).
2179 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002180bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002181 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002182 case kRGBA_8888_GrPixelConfig:
2183 case kRGBX_8888_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002184 if (fGLCaps.fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002185 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002186 return true;
2187 } else {
2188 return false;
2189 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002190 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002191 // ES2 supports 565. ES1 supports it
2192 // with FBO extension desktop GL has
2193 // no such internal format
2194 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002195 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002196 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002197 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002198 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002199 return true;
2200 default:
2201 return false;
2202 }
2203}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002204
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002205void GrGpuGL::resetDirtyFlags() {
2206 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2207}
2208
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002209void GrGpuGL::setBuffers(bool indexed,
2210 int* extraVertexOffset,
2211 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002212
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002213 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002214
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002215 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2216
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002217 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002218 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002219 case kBuffer_GeometrySrcType:
2220 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002221 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002222 break;
2223 case kArray_GeometrySrcType:
2224 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002225 this->finalizeReservedVertices();
2226 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2227 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002228 break;
2229 default:
2230 vbuf = NULL; // suppress warning
2231 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002232 }
2233
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002234 GrAssert(NULL != vbuf);
2235 GrAssert(!vbuf->isLocked());
2236 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002237 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002238 fHWGeometryState.fArrayPtrsDirty = true;
2239 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002240 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002241
2242 if (indexed) {
2243 GrAssert(NULL != extraIndexOffset);
2244
2245 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002246 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002247 case kBuffer_GeometrySrcType:
2248 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002249 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002250 break;
2251 case kArray_GeometrySrcType:
2252 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002253 this->finalizeReservedIndices();
2254 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2255 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002256 break;
2257 default:
2258 ibuf = NULL; // suppress warning
2259 GrCrash("Unknown geometry src type!");
2260 }
2261
2262 GrAssert(NULL != ibuf);
2263 GrAssert(!ibuf->isLocked());
2264 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002265 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002266 fHWGeometryState.fIndexBuffer = ibuf;
2267 }
2268 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002269}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002270
2271int GrGpuGL::getMaxEdges() const {
2272 // FIXME: This is a pessimistic estimate based on how many other things
2273 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002274 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2275 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002276}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002277
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002278void GrGpuGL::GLCaps::print() const {
2279 for (int i = 0; i < fStencilFormats.count(); ++i) {
2280 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2281 i,
2282 fStencilFormats[i].fStencilBits,
2283 fStencilFormats[i].fTotalBits);
2284 }
2285
2286 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2287 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2288 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2289 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2290 static const char* gMSFBOExtStr[] = {
2291 "None",
2292 "ARB",
2293 "EXT",
2294 "Apple",
2295 };
2296 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002297 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002298 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2299 }
2300 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2301 GrPrintf("Support RGBA8 Render Buffer: %s\n",
2302 (fRGBA8Renderbuffer ? "YES": "NO"));
2303}