blob: ffbb557170689e55c57eac3c19f6d73c0f4ea501 [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.
bsalomon@google.com316f99232011-01-13 21:28:12 +000023static const int SPARE_TEX_UNIT = GrGpuGL::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.com15b11df2011-09-16 21:18:29 +0000312 // This subclass must do this before the base class destructor runs
313 // since we will unref the GrGLInterface.
314 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000315 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000316}
317
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000318///////////////////////////////////////////////////////////////////////////////
319
320static const GrGLuint kUnknownBitCount = ~0;
321
322void GrGpuGL::initCaps() {
323 GrGLint maxTextureUnits;
324 // check FS and fixed-function texture unit limits
325 // we only use textures in the fragment stage currently.
326 // checks are > to make sure we have a spare unit.
327 if (kES1_GrGLBinding != this->glBinding()) {
328 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
329 GrAssert(maxTextureUnits > kNumStages);
330 }
331 if (kES2_GrGLBinding != this->glBinding()) {
332 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
333 GrAssert(maxTextureUnits > kNumStages);
334 }
335 if (kES2_GrGLBinding == this->glBinding()) {
336 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
337 &fGLCaps.fMaxFragmentUniformVectors);
338 } else if (kDesktop_GrGLBinding != this->glBinding()) {
339 GrGLint max;
340 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
341 fGLCaps.fMaxFragmentUniformVectors = max / 4;
342 } else {
343 fGLCaps.fMaxFragmentUniformVectors = 16;
344 }
345
346 GrGLint numFormats;
347 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
348 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
349 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
350 for (int i = 0; i < numFormats; ++i) {
351 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
352 fCaps.f8BitPaletteSupport = true;
353 break;
354 }
355 }
356
357 if (kDesktop_GrGLBinding == this->glBinding()) {
358 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
359 this->hasExtension("GL_EXT_stencil_wrap");
360 } else {
361 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(2,0)) ||
362 this->hasExtension("GL_OES_stencil_wrap");
363 }
364
365 if (kDesktop_GrGLBinding == this->glBinding()) {
366 // we could also look for GL_ATI_separate_stencil extension or
367 // GL_EXT_stencil_two_side but they use different function signatures
368 // than GL2.0+ (and than each other).
369 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
370 // supported on GL 1.4 and higher or by extension
371 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
372 this->hasExtension("GL_EXT_stencil_wrap");
373 } else {
374 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
375 // an ES1 extension.
376 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
377 // stencil wrap support is in ES2, ES1 requires extension.
378 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(2,0)) ||
379 this->hasExtension("GL_OES_stencil_wrap");
380 }
381
382 if (kDesktop_GrGLBinding == this->glBinding()) {
383 fGLCaps.fRGBA8Renderbuffer = true;
384 } else {
385 fGLCaps.fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
386 }
387
388
389 if (kDesktop_GrGLBinding != this->glBinding()) {
390 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
391 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
392 }
393 }
394
395 if (kDesktop_GrGLBinding == this->glBinding()) {
396 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
397 // extension includes glMapBuffer.
398 } else {
399 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
400 }
401
402 if (kDesktop_GrGLBinding == this->glBinding()) {
403 if (fGLVersion >= GR_GL_VER(2,0) ||
404 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
405 fCaps.fNPOTTextureTileSupport = true;
406 fCaps.fNPOTTextureSupport = true;
407 } else {
408 fCaps.fNPOTTextureTileSupport = false;
409 fCaps.fNPOTTextureSupport = false;
410 }
411 } else {
412 if (fGLVersion >= GR_GL_VER(2,0)) {
413 fCaps.fNPOTTextureSupport = true;
414 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
415 } else {
416 fCaps.fNPOTTextureSupport =
417 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
418 fCaps.fNPOTTextureTileSupport = false;
419 }
420 }
421
422 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
423
424 ////////////////////////////////////////////////////////////////////////////
425 // Experiments to determine limitations that can't be queried.
426 // TODO: Make these a preprocess that generate some compile time constants.
427 // TODO: probe once at startup, rather than once per context creation.
428
429 int expectNPOTTargets = fGL->fNPOTRenderTargetSupport;
430 if (expectNPOTTargets == kProbe_GrGLCapability) {
431 fCaps.fNPOTRenderTargetSupport =
432 probe_for_npot_render_target_support(fGL, fCaps.fNPOTTextureSupport);
433 } else {
434 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000435 fCaps.fNPOTRenderTargetSupport = (0 != expectNPOTTargets);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000436 }
437
438 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
439 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
440 // Our render targets are always created with textures as the color
441 // attachment, hence this min:
442 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
443
444 fCaps.fMinRenderTargetHeight = fGL->fMinRenderTargetHeight;
445 if (fCaps.fMinRenderTargetHeight == kProbe_GrGLCapability) {
446 fCaps.fMinRenderTargetHeight =
447 probe_for_min_render_target_height(fGL, fCaps.fNPOTRenderTargetSupport,
448 fCaps.fMaxRenderTargetSize);
449 }
450
451 fCaps.fMinRenderTargetWidth = fGL->fMinRenderTargetWidth;
452 if (fCaps.fMinRenderTargetWidth == kProbe_GrGLCapability) {
453 fCaps.fMinRenderTargetWidth =
454 probe_for_min_render_target_width(fGL, fCaps.fNPOTRenderTargetSupport,
455 fCaps.fMaxRenderTargetSize);
456 }
457
458 this->initFSAASupport();
459 this->initStencilFormats();
460}
461
462void GrGpuGL::initFSAASupport() {
463 // TODO: Get rid of GrAALevel and use # samples directly.
464 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
465 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
466 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
467 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
468 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
469
470 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
471 if (kDesktop_GrGLBinding != this->glBinding()) {
472 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
473 // chrome's extension is equivalent to the EXT msaa
474 // and fbo_blit extensions.
475 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
476 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
477 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
478 }
479 } else {
480 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
481 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
482 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
483 this->hasExtension("GL_EXT_framebuffer_blit")) {
484 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
485 }
486 }
487
488 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
489 GrGLint maxSamples;
490 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
491 if (maxSamples > 1 ) {
492 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
493 fGLCaps.fAASamples[kLow_GrAALevel] =
494 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
495 fGLCaps.fAASamples[kMed_GrAALevel] =
496 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
497 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
498 }
499 }
500 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
501}
502
503void GrGpuGL::initStencilFormats() {
504
505 // Build up list of legal stencil formats (though perhaps not supported on
506 // the particular gpu/driver) from most preferred to least.
507
508 // these consts are in order of most preferred to least preferred
509 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
510 static const GrGLStencilBuffer::Format
511 // internal Format stencil bits total bits packed?
512 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
513 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
514 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
515 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
516 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
517 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
518
519 if (kDesktop_GrGLBinding == this->glBinding()) {
520 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
521 this->hasExtension("GL_EXT_packed_depth_stencil") ||
522 this->hasExtension("GL_ARB_framebuffer_object");
523
524 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
525 // require FBO support we can expect these are legal formats and don't
526 // check. These also all support the unsized GL_STENCIL_INDEX.
527 fGLCaps.fStencilFormats.push_back() = gS8;
528 fGLCaps.fStencilFormats.push_back() = gS16;
529 if (supportsPackedDS) {
530 fGLCaps.fStencilFormats.push_back() = gD24S8;
531 }
532 fGLCaps.fStencilFormats.push_back() = gS4;
533 if (supportsPackedDS) {
534 fGLCaps.fStencilFormats.push_back() = gDS;
535 }
536 } else {
537 // ES2 has STENCIL_INDEX8 without extensions.
538 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
539 // introduces tokens for S1 thu S8 but there are separate extensions
540 // that make them legal (GL_OES_stencil1, ...).
541 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
542 // ES doesn't support using the unsized formats.
543
544 if (fGLVersion >= GR_GL_VER(2,0) ||
545 this->hasExtension("GL_OES_stencil8")) {
546 fGLCaps.fStencilFormats.push_back() = gS8;
547 }
548 //fStencilFormats.push_back() = gS16;
549 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
550 fGLCaps.fStencilFormats.push_back() = gD24S8;
551 }
552 if (this->hasExtension("GL_OES_stencil4")) {
553 fGLCaps.fStencilFormats.push_back() = gS4;
554 }
555 // we require some stencil format.
556 GrAssert(fGLCaps.fStencilFormats.count() > 0);
557 }
558}
559
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000560void GrGpuGL::resetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000561 if (gPrintStartupSpew && !fPrintedCaps) {
562 fPrintedCaps = true;
563 this->getCaps().print();
564 fGLCaps.print();
565 }
566
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000567 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000569 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000571 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000572 GL_CALL(Disable(GR_GL_DEPTH_TEST));
573 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000574
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000575 GL_CALL(Disable(GR_GL_CULL_FACE));
576 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000577 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000578
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000579 GL_CALL(Disable(GR_GL_DITHER));
580 if (kDesktop_GrGLBinding == this->glBinding()) {
581 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
582 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
583 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000584 fHWAAState.fMSAAEnabled = false;
585 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000586 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000587
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000588 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000589 fHWDrawState.fFlagBits = 0;
590
reed@google.comac10a2d2010-12-22 21:39:39 +0000591 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000592 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000593
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000594 // invalid
595 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000596
reed@google.comac10a2d2010-12-22 21:39:39 +0000597 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000598 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
599 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000600
601 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000602 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000603
reed@google.comac10a2d2010-12-22 21:39:39 +0000604 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000605
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000606 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000607
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000608 for (int s = 0; s < kNumStages; ++s) {
609 fHWDrawState.fTextures[s] = NULL;
610 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
611 -GR_ScalarMax,
612 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000613 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000614 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000615 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000616
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000617 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000619 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000620 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
bsalomon@google.comd302f142011-03-03 13:54:13 +0000622 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000623 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000624 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000625
626 fHWGeometryState.fIndexBuffer = NULL;
627 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000628
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000629 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000630
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000631 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000632 fHWDrawState.fRenderTarget = NULL;
633}
634
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000635GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
636
637 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
638 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
639 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
640 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
641
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000642 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000643 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000644
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000645 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000646 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000647 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000648 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000649 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000650 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000651 } else {
652 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000653 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000654 }
655 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000656 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000657 }
658 // we don't know what the RB ids are without glGets and we don't care
659 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000660 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000661 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000662 if (desc.fStencilBits) {
663 GrGLStencilBuffer::Format format;
664 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
665 format.fPacked = false;
666 format.fStencilBits = desc.fStencilBits;
667 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000668 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
669 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000670 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000671 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000672 }
673
674 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000675 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000676 GrGLenum dontCare;
677 if (!canBeTexture(desc.fConfig, &dontCare,
678 &texDesc.fUploadFormat,
679 &texDesc.fUploadType)) {
680 return NULL;
681 }
682
683 GrGLTexture::TexParams params;
684
685 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
686 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
687
bsalomon@google.com90405932011-06-17 15:56:55 +0000688 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000689 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000690 texDesc.fTextureID = desc.fPlatformTexture;
691 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
692 texDesc.fOwnsID = false;
693
694 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000695 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000696 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000697 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000698 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000699 } else {
700 return new GrGLTexture(this, texDesc, params);
701 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000702 } else {
703 GrGLIRect viewport;
704 viewport.fLeft = 0;
705 viewport.fBottom = 0;
706 viewport.fWidth = desc.fWidth;
707 viewport.fHeight = desc.fHeight;
708
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000709 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000710 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000711 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000712 }
713}
714
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000715
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000716////////////////////////////////////////////////////////////////////////////////
717
718void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
719 GrGLenum internalFormat,
720 const void* data,
721 size_t rowBytes) {
722 // we assume the texture is bound;
723 if (!rowBytes) {
724 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
725 }
726
727 // in case we need a temporary, trimmed copy of the src pixels
728 SkAutoSMalloc<128 * 128> tempStorage;
729
730 /*
731 * check whether to allocate a temporary buffer for flipping y or
732 * because our data has extra bytes past each row. If so, we need
733 * to trim those off here, since GL ES doesn't let us specify
734 * GL_UNPACK_ROW_LENGTH.
735 */
736 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000737 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000738 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000739 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
740 rowBytes / desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000741 }
742 } else {
743 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
744 if (data && (trimRowBytes < rowBytes || flipY)) {
745 // copy the data into our new storage, skipping the trailing bytes
746 size_t trimSize = desc.fContentHeight * trimRowBytes;
747 const char* src = (const char*)data;
748 if (flipY) {
749 src += (desc.fContentHeight - 1) * rowBytes;
750 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000751 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000752 for (int y = 0; y < desc.fContentHeight; y++) {
753 memcpy(dst, src, trimRowBytes);
754 if (flipY) {
755 src -= rowBytes;
756 } else {
757 src += rowBytes;
758 }
759 dst += trimRowBytes;
760 }
761 // now point data to our trimmed version
762 data = tempStorage.get();
763 rowBytes = trimRowBytes;
764 }
765 }
766
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000767 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000768 if (kIndex_8_GrPixelConfig == desc.fFormat &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000769 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
771 GrAssert(desc.fContentWidth == desc.fAllocWidth);
772 GrAssert(desc.fContentHeight == desc.fAllocHeight);
773 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
774 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000775 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
776 desc.fAllocWidth, desc.fAllocHeight,
777 0, imageSize, data));
778 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000779 } else {
780 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
781 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000782 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
783 desc.fAllocWidth, desc.fAllocHeight,
784 0, desc.fUploadFormat, desc.fUploadType, NULL));
785 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
786 desc.fContentHeight, desc.fUploadFormat,
787 desc.fUploadType, data));
788 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000789
790 int extraW = desc.fAllocWidth - desc.fContentWidth;
791 int extraH = desc.fAllocHeight - desc.fContentHeight;
792 int maxTexels = extraW * extraH;
793 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
794 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
795
796 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
797
798 // rowBytes is actual stride between rows in data
799 // rowDataBytes is the actual amount of non-pad data in a row
800 // and the stride used for uploading extraH rows.
801 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
802 if (extraH) {
803 uint8_t* lastRowStart = (uint8_t*) data +
804 (desc.fContentHeight - 1) * rowBytes;
805 uint8_t* extraRowStart = (uint8_t*)texels.get();
806
807 for (int i = 0; i < extraH; ++i) {
808 memcpy(extraRowStart, lastRowStart, rowDataBytes);
809 extraRowStart += rowDataBytes;
810 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000811 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
812 desc.fContentHeight, desc.fContentWidth,
813 extraH, desc.fUploadFormat,
814 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 }
816 if (extraW) {
817 uint8_t* edgeTexel = (uint8_t*)data +
818 rowDataBytes - desc.fUploadByteCount;
819 uint8_t* extraTexel = (uint8_t*)texels.get();
820 for (int j = 0; j < desc.fContentHeight; ++j) {
821 for (int i = 0; i < extraW; ++i) {
822 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
823 extraTexel += desc.fUploadByteCount;
824 }
825 edgeTexel += rowBytes;
826 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000827 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
828 0, extraW, desc.fContentHeight,
829 desc.fUploadFormat, desc.fUploadType,
830 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000831 }
832 if (extraW && extraH) {
833 uint8_t* cornerTexel = (uint8_t*)data +
834 desc.fContentHeight * rowBytes -
835 desc.fUploadByteCount;
836 uint8_t* extraTexel = (uint8_t*)texels.get();
837 for (int i = 0; i < extraW*extraH; ++i) {
838 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
839 extraTexel += desc.fUploadByteCount;
840 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000841 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
842 desc.fContentHeight, extraW, extraH,
843 desc.fUploadFormat, desc.fUploadType,
844 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000845 }
846
847 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000848 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
849 desc.fAllocWidth, desc.fAllocHeight, 0,
850 desc.fUploadFormat, desc.fUploadType, data));
851 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000852 }
853 }
854}
855
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000856bool GrGpuGL::createRenderTargetObjects(int width, int height,
857 GrGLuint texID,
858 GrGLRenderTarget::Desc* desc) {
859 desc->fMSColorRenderbufferID = 0;
860 desc->fRTFBOID = 0;
861 desc->fTexFBOID = 0;
862 desc->fOwnIDs = true;
863
864 GrGLenum status;
865 GrGLint err;
866
bsalomon@google.comab15d612011-08-09 12:57:56 +0000867 GrGLenum msColorFormat = 0; // suppress warning
868
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000869 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000870 if (!desc->fTexFBOID) {
871 goto FAILED;
872 }
873
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874
875 // If we are using multisampling we will create two FBOS. We render
876 // to one and then resolve to the texture bound to the other.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000877 if (desc->fSampleCnt > 1 && GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000878 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
879 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880 if (!desc->fRTFBOID ||
881 !desc->fMSColorRenderbufferID ||
882 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
883 goto FAILED;
884 }
885 } else {
886 desc->fRTFBOID = desc->fTexFBOID;
887 }
888
889 if (desc->fRTFBOID != desc->fTexFBOID) {
890 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000892 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000893 GR_GL_CALL_NOERRCHECK(this->glInterface(),
894 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
895 desc->fSampleCnt,
896 msColorFormat,
897 width, height));
898 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 if (err != GR_GL_NO_ERROR) {
900 goto FAILED;
901 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000902 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
903 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904 GR_GL_COLOR_ATTACHMENT0,
905 GR_GL_RENDERBUFFER,
906 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000907 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
909 goto FAILED;
910 }
911 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000912 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000914 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
915 GR_GL_COLOR_ATTACHMENT0,
916 GR_GL_TEXTURE_2D,
917 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000918 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
920 goto FAILED;
921 }
922
923 return true;
924
925FAILED:
926 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 }
929 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000930 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000931 }
932 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000933 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000934 }
935 return false;
936}
937
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000938// good to set a break-point here to know when createTexture fails
939static GrTexture* return_null_texture() {
940// GrAssert(!"null texture");
941 return NULL;
942}
943
944#if GR_DEBUG
945static size_t as_size_t(int x) {
946 return x;
947}
948#endif
949
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000950GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000951 const void* srcData,
952 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000953
954#if GR_COLLECT_STATS
955 ++fStats.fTextureCreateCnt;
956#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000957
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000958 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000959 GR_GL_NEAREST,
960 GR_GL_CLAMP_TO_EDGE,
961 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000962 };
reed@google.com1fcd51e2011-01-05 15:50:27 +0000963
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000964 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000965 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000966 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +0000967
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000968 glTexDesc.fContentWidth = desc.fWidth;
969 glTexDesc.fContentHeight = desc.fHeight;
970 glTexDesc.fAllocWidth = desc.fWidth;
971 glTexDesc.fAllocHeight = desc.fHeight;
972 glTexDesc.fFormat = desc.fFormat;
973 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000974
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000975 glRTDesc.fMSColorRenderbufferID = 0;
976 glRTDesc.fRTFBOID = 0;
977 glRTDesc.fTexFBOID = 0;
978 glRTDesc.fOwnIDs = true;
979 glRTDesc.fConfig = glTexDesc.fFormat;
980
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000981 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000982 if (!canBeTexture(desc.fFormat,
983 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000984 &glTexDesc.fUploadFormat,
985 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000986 return return_null_texture();
987 }
988
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000989 const Caps& caps = this->getCaps();
990
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000991 // We keep GrRenderTargets in GL's normal orientation so that they
992 // can be drawn to by the outside world without the client having
993 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000994 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000995 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000996
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000997 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
998 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
999 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1000 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001001 GrPrintf("AA RT requested but not supported on this platform.");
1002 }
1003
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001004 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001005
reed@google.comac10a2d2010-12-22 21:39:39 +00001006 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001007 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001008 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1009 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001010 }
1011
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001012 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001013 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001014 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001015 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001016 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1017 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001018 return return_null_texture();
1019 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001020 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001021 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1022 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001023 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1024 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001025 return return_null_texture();
1026 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001027 }
1028
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001029 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001030 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001031 return return_null_texture();
1032 }
1033
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001034 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001035 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1036 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1037 GR_GL_TEXTURE_MAG_FILTER,
1038 DEFAULT_PARAMS.fFilter));
1039 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1040 GR_GL_TEXTURE_MIN_FILTER,
1041 DEFAULT_PARAMS.fFilter));
1042 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1043 GR_GL_TEXTURE_WRAP_S,
1044 DEFAULT_PARAMS.fWrapS));
1045 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1046 GR_GL_TEXTURE_WRAP_T,
1047 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001048
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001049 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001050
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001051 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001053 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001054#if GR_COLLECT_STATS
1055 ++fStats.fRenderTargetCreateCnt;
1056#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001057 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1058 glTexDesc.fAllocHeight,
1059 glTexDesc.fTextureID,
1060 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001061 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 return return_null_texture();
1063 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001064 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1065 } else {
1066 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 }
1068#ifdef TRACE_TEXTURE_CREATION
1069 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1070 tex->fTextureID, width, height, tex->fUploadByteCount);
1071#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001072 return tex;
1073}
1074
1075namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001076void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1077 GrGLuint rb,
1078 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001079 // we shouldn't ever know one size and not the other
1080 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1081 (kUnknownBitCount == format->fTotalBits));
1082 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001083 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001084 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1085 (GrGLint*)&format->fStencilBits);
1086 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001087 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1089 (GrGLint*)&format->fTotalBits);
1090 format->fTotalBits += format->fStencilBits;
1091 } else {
1092 format->fTotalBits = format->fStencilBits;
1093 }
1094 }
1095}
1096}
1097
1098bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1099 int width, int height) {
1100
1101 // All internally created RTs are also textures. We don't create
1102 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1103 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001104 GrAssert(width >= rt->allocatedWidth());
1105 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106
1107 int samples = rt->numSamples();
1108 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001109 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 if (!sbID) {
1111 return false;
1112 }
1113
1114 GrGLStencilBuffer* sb = NULL;
1115
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001116 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 // we start with the last stencil format that succeeded in hopes
1120 // that we won't go through this loop more than once after the
1121 // first (painful) stencil creation.
1122 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001123 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001124 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 // version on a GL that doesn't have an MSAA extension.
1126 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1128 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001129 GR_GL_RENDERBUFFER,
1130 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001131 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 width,
1133 height));
1134 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1136 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001137 sFmt.fInternalFormat,
1138 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139 }
1140
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001141 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 if (err == GR_GL_NO_ERROR) {
1143 // After sized formats we attempt an unsized format and take whatever
1144 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001145 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001147 sb = new GrGLStencilBuffer(this, sbID, width, height,
1148 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1150 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001151 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001153 return true;
1154 }
1155 sb->abandon(); // otherwise we lose sbID
1156 sb->unref();
1157 }
1158 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001159 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001160 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161}
1162
1163bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1164 GrRenderTarget* rt) {
1165 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1166
1167 GrGLuint fbo = glrt->renderFBOID();
1168
1169 if (NULL == sb) {
1170 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001171 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172 GR_GL_STENCIL_ATTACHMENT,
1173 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001174 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 GR_GL_DEPTH_ATTACHMENT,
1176 GR_GL_RENDERBUFFER, 0));
1177#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001178 GrGLenum status;
1179 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1181#endif
1182 }
1183 return true;
1184 } else {
1185 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1186 GrGLuint rb = glsb->renderbufferID();
1187
reed@google.comac10a2d2010-12-22 21:39:39 +00001188 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001189 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1190 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 GR_GL_STENCIL_ATTACHMENT,
1192 GR_GL_RENDERBUFFER, rb));
1193 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 GR_GL_DEPTH_ATTACHMENT,
1196 GR_GL_RENDERBUFFER, rb));
1197 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001198 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001199 GR_GL_DEPTH_ATTACHMENT,
1200 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001201 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001203 GrGLenum status;
1204 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 GR_GL_STENCIL_ATTACHMENT,
1208 GR_GL_RENDERBUFFER, 0));
1209 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001210 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 GR_GL_DEPTH_ATTACHMENT,
1212 GR_GL_RENDERBUFFER, 0));
1213 }
1214 return false;
1215 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 return true;
1217 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001219}
1220
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001221////////////////////////////////////////////////////////////////////////////////
1222
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001223GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001224 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001228 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001229 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1232 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1233 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1234 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1235 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001236 // deleting bound buffer does implicit bind to 0
1237 fHWGeometryState.fVertexBuffer = NULL;
1238 return NULL;
1239 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001240 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 size, dynamic);
1242 fHWGeometryState.fVertexBuffer = vertexBuffer;
1243 return vertexBuffer;
1244 }
1245 return NULL;
1246}
1247
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001248GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001249 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001251 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001252 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1253 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001255 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1256 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1257 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1258 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1259 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 // deleting bound buffer does implicit bind to 0
1261 fHWGeometryState.fIndexBuffer = NULL;
1262 return NULL;
1263 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001264 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 size, dynamic);
1266 fHWGeometryState.fIndexBuffer = indexBuffer;
1267 return indexBuffer;
1268 }
1269 return NULL;
1270}
1271
reed@google.comac10a2d2010-12-22 21:39:39 +00001272void GrGpuGL::flushScissor(const GrIRect* rect) {
1273 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001274 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001275 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001276
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001277 GrGLIRect scissor;
1278 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001279 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001280 rect->width(), rect->height());
1281 if (scissor.contains(vp)) {
1282 rect = NULL;
1283 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 }
1285
1286 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 fHWBounds.fScissorRect = scissor;
1290 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001292 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 fHWBounds.fScissorEnabled = true;
1294 }
1295 } else {
1296 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001297 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 fHWBounds.fScissorEnabled = false;
1299 }
1300 }
1301}
1302
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001303void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001304 if (NULL == fCurrDrawState.fRenderTarget) {
1305 return;
1306 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001307 GrIRect r;
1308 if (NULL != rect) {
1309 // flushScissor expects rect to be clipped to the target.
1310 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001311 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1312 fCurrDrawState.fRenderTarget->height());
1313 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001314 rect = &r;
1315 } else {
1316 return;
1317 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001318 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001319 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001320 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001321 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001322 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001323 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1324 GrColorUnpackG(color)/255.f,
1325 GrColorUnpackB(color)/255.f,
1326 GrColorUnpackA(color)/255.f));
1327 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001328}
1329
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001330void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001331 if (NULL == fCurrDrawState.fRenderTarget) {
1332 return;
1333 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001334
1335 this->flushRenderTarget(&GrIRect::EmptyIRect());
1336
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001338 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001339 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001341 GL_CALL(StencilMask(0xffffffff));
1342 GL_CALL(ClearStencil(0));
1343 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001344 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001345}
1346
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001347void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001348 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001349
1350 // this should only be called internally when we know we have a
1351 // stencil buffer.
1352 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001353 GrGLint stencilBitCount =
1354 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001355#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001357 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001358#else
1359 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001360 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001361 // turned into draws. Our contract on GrDrawTarget says that
1362 // changing the clip between stencil passes may or may not
1363 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001364 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001365#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001366 GrGLint value;
1367 if (insideClip) {
1368 value = (1 << (stencilBitCount - 1));
1369 } else {
1370 value = 0;
1371 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001372 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001373 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001374 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001375 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001376 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001377 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001378}
1379
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001380void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001381 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001382}
1383
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001384bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1385 int left, int top, int width, int height,
1386 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001387 GrGLenum internalFormat; // we don't use this for glReadPixels
1388 GrGLenum format;
1389 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001390 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1391 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001392 }
1393 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1394 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1395 switch (tgt->getResolveType()) {
1396 case GrGLRenderTarget::kCantResolve_ResolveType:
1397 return false;
1398 case GrGLRenderTarget::kAutoResolves_ResolveType:
1399 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1400 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001401 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001402 break;
1403 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001404 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001405 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001406 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1407 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001408 break;
1409 default:
1410 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 }
1412
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001413 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001414
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001415 // the read rect is viewport-relative
1416 GrGLIRect readRect;
1417 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001418 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1419 readRect.fWidth, readRect.fHeight,
1420 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001421
1422 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1423 // API presents top-to-bottom
1424 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001425 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001426 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001427 void* tmp = rowStorage.get();
1428
1429 const int halfY = height >> 1;
1430 char* top = reinterpret_cast<char*>(buffer);
1431 char* bottom = top + (height - 1) * stride;
1432 for (int y = 0; y < halfY; y++) {
1433 memcpy(tmp, top, stride);
1434 memcpy(top, bottom, stride);
1435 memcpy(bottom, tmp, stride);
1436 top += stride;
1437 bottom -= stride;
1438 }
1439 }
1440 return true;
1441}
1442
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001443void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001444
1445 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1446
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001447 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001448 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001449 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001450 #if GR_COLLECT_STATS
1451 ++fStats.fRenderTargetChngCnt;
1452 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001453 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001454 GrGLenum status;
1455 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001456 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001457 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001458 }
1459 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001460 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001461 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001462 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001463 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001464 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 fHWBounds.fViewportRect = vp;
1466 }
1467 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001468 if (NULL == bound || !bound->isEmpty()) {
1469 rt->flagAsNeedingResolve(bound);
1470 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001471}
1472
twiz@google.com0f31ca72011-03-18 17:38:11 +00001473GrGLenum gPrimitiveType2GLMode[] = {
1474 GR_GL_TRIANGLES,
1475 GR_GL_TRIANGLE_STRIP,
1476 GR_GL_TRIANGLE_FAN,
1477 GR_GL_POINTS,
1478 GR_GL_LINES,
1479 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001480};
1481
bsalomon@google.comd302f142011-03-03 13:54:13 +00001482#define SWAP_PER_DRAW 0
1483
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001484#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001485 #if GR_MAC_BUILD
1486 #include <AGL/agl.h>
1487 #elif GR_WIN32_BUILD
1488 void SwapBuf() {
1489 DWORD procID = GetCurrentProcessId();
1490 HWND hwnd = GetTopWindow(GetDesktopWindow());
1491 while(hwnd) {
1492 DWORD wndProcID = 0;
1493 GetWindowThreadProcessId(hwnd, &wndProcID);
1494 if(wndProcID == procID) {
1495 SwapBuffers(GetDC(hwnd));
1496 }
1497 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1498 }
1499 }
1500 #endif
1501#endif
1502
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001503void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1504 uint32_t startVertex,
1505 uint32_t startIndex,
1506 uint32_t vertexCount,
1507 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1509
twiz@google.com0f31ca72011-03-18 17:38:11 +00001510 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001511
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001512 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1513 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1514
1515 // our setupGeometry better have adjusted this to zero since
1516 // DrawElements always draws from the begining of the arrays for idx 0.
1517 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001518
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001519 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1520 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001521#if SWAP_PER_DRAW
1522 glFlush();
1523 #if GR_MAC_BUILD
1524 aglSwapBuffers(aglGetCurrentContext());
1525 int set_a_break_pt_here = 9;
1526 aglSwapBuffers(aglGetCurrentContext());
1527 #elif GR_WIN32_BUILD
1528 SwapBuf();
1529 int set_a_break_pt_here = 9;
1530 SwapBuf();
1531 #endif
1532#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001533}
1534
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001535void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1536 uint32_t startVertex,
1537 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001538 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1539
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001540 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1541
1542 // our setupGeometry better have adjusted this to zero.
1543 // DrawElements doesn't take an offset so we always adjus the startVertex.
1544 GrAssert(0 == startVertex);
1545
1546 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1547 // account for startVertex in the DrawElements case. So we always
1548 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001549 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001550#if SWAP_PER_DRAW
1551 glFlush();
1552 #if GR_MAC_BUILD
1553 aglSwapBuffers(aglGetCurrentContext());
1554 int set_a_break_pt_here = 9;
1555 aglSwapBuffers(aglGetCurrentContext());
1556 #elif GR_WIN32_BUILD
1557 SwapBuf();
1558 int set_a_break_pt_here = 9;
1559 SwapBuf();
1560 #endif
1561#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001562}
1563
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001564void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001565
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001566 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001567 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001568 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001569 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1570 rt->renderFBOID()));
1571 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1572 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001573 #if GR_COLLECT_STATS
1574 ++fStats.fRenderTargetChngCnt;
1575 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001576 // make sure we go through flushRenderTarget() since we've modified
1577 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001578 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001579 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001580 const GrIRect dirtyRect = rt->getResolveRect();
1581 GrGLIRect r;
1582 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1583 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001584
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001585 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001586 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001587 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1588 GL_CALL(Scissor(r.fLeft, r.fBottom,
1589 r.fWidth, r.fHeight));
1590 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001591 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001592 fHWBounds.fScissorEnabled = true;
1593 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001594 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001595 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001596 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1597 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001598 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001599 int right = r.fLeft + r.fWidth;
1600 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001601 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1602 r.fLeft, r.fBottom, right, top,
1603 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001605 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001606 }
1607}
1608
twiz@google.com0f31ca72011-03-18 17:38:11 +00001609static const GrGLenum grToGLStencilFunc[] = {
1610 GR_GL_ALWAYS, // kAlways_StencilFunc
1611 GR_GL_NEVER, // kNever_StencilFunc
1612 GR_GL_GREATER, // kGreater_StencilFunc
1613 GR_GL_GEQUAL, // kGEqual_StencilFunc
1614 GR_GL_LESS, // kLess_StencilFunc
1615 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1616 GR_GL_EQUAL, // kEqual_StencilFunc,
1617 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001618};
1619GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1620GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1621GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1622GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1623GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1624GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1625GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1626GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1627GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1628
twiz@google.com0f31ca72011-03-18 17:38:11 +00001629static const GrGLenum grToGLStencilOp[] = {
1630 GR_GL_KEEP, // kKeep_StencilOp
1631 GR_GL_REPLACE, // kReplace_StencilOp
1632 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1633 GR_GL_INCR, // kIncClamp_StencilOp
1634 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1635 GR_GL_DECR, // kDecClamp_StencilOp
1636 GR_GL_ZERO, // kZero_StencilOp
1637 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001638};
1639GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1640GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1641GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1642GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1643GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1644GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1645GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1646GR_STATIC_ASSERT(6 == kZero_StencilOp);
1647GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1648
reed@google.comac10a2d2010-12-22 21:39:39 +00001649void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001650 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001651
1652 // use stencil for clipping if clipping is enabled and the clip
1653 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001654 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001656 bool stencilChange = fHWStencilClip != stencilClip ||
1657 fHWDrawState.fStencilSettings != *settings ||
1658 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1659 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001660
1661 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001662
bsalomon@google.comd302f142011-03-03 13:54:13 +00001663 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1664 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001665
bsalomon@google.comd302f142011-03-03 13:54:13 +00001666 if (settings->isDisabled()) {
1667 if (stencilClip) {
1668 settings = &gClipStencilSettings;
1669 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001670 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001671
1672 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001673 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001674 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001675 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001676 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001677 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001678 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1679 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1680 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1681 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1682 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1683 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1684 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1685 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1686 }
1687 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001688 int stencilBits = 0;
1689 GrStencilBuffer* stencilBuffer =
1690 fCurrDrawState.fRenderTarget->getStencilBuffer();
1691 if (NULL != stencilBuffer) {
1692 stencilBits = stencilBuffer->bits();
1693 }
1694 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001695 GrAssert(stencilBits ||
1696 (GrStencilSettings::gDisabled ==
1697 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001698 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1699 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001700
1701 unsigned int frontRef = settings->fFrontFuncRef;
1702 unsigned int frontMask = settings->fFrontFuncMask;
1703 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001704 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001705
1706 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1707
1708 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1709 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1710 } else {
1711 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1712
1713 ConvertStencilFuncAndMask(settings->fFrontFunc,
1714 stencilClip,
1715 clipStencilMask,
1716 userStencilMask,
1717 &frontRef,
1718 &frontMask);
1719 frontWriteMask &= userStencilMask;
1720 }
1721 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001722 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001723 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001724 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001725 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001726 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001727 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001728 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001729 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001730 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001731
1732 unsigned int backRef = settings->fBackFuncRef;
1733 unsigned int backMask = settings->fBackFuncMask;
1734 unsigned int backWriteMask = settings->fBackWriteMask;
1735
1736
1737 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1738 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1739 backFunc = grToGLStencilFunc[settings->fBackFunc];
1740 } else {
1741 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1742 ConvertStencilFuncAndMask(settings->fBackFunc,
1743 stencilClip,
1744 clipStencilMask,
1745 userStencilMask,
1746 &backRef,
1747 &backMask);
1748 backWriteMask &= userStencilMask;
1749 }
1750
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001751 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1752 frontRef, frontMask));
1753 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1754 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1755 backRef, backMask));
1756 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1757 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1758 grToGLStencilOp[settings->fFrontFailOp],
1759 grToGLStencilOp[settings->fFrontPassOp],
1760 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001761
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001762 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1763 grToGLStencilOp[settings->fBackFailOp],
1764 grToGLStencilOp[settings->fBackPassOp],
1765 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001766 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001767 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1768 GL_CALL(StencilMask(frontWriteMask));
1769 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001770 grToGLStencilOp[settings->fFrontPassOp],
1771 grToGLStencilOp[settings->fFrontPassOp]));
1772 }
1773 }
1774 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001775 fHWStencilClip = stencilClip;
1776 }
1777}
1778
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001779void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001780 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001781 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1782 // smooth lines.
1783
1784 // we prefer smooth lines over multisampled lines
1785 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001786 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001787 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001788 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001789 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001790 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001791 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001792 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001793 fHWAAState.fSmoothLineEnabled = false;
1794 }
1795 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1796 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001797 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001798 fHWAAState.fMSAAEnabled = false;
1799 }
1800 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1801 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
1802 fHWAAState.fMSAAEnabled) {
1803 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001804 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001805 fHWAAState.fMSAAEnabled = false;
1806 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001807 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001808 fHWAAState.fMSAAEnabled = true;
1809 }
1810 }
1811 }
1812}
1813
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001814void GrGpuGL::flushBlend(GrPrimitiveType type,
1815 GrBlendCoeff srcCoeff,
1816 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001817 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001818 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001819 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001820 fHWBlendDisabled = false;
1821 }
1822 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1823 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001824 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1825 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001826 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1827 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1828 }
1829 } else {
1830 bool blendOff = canDisableBlend();
1831 if (fHWBlendDisabled != blendOff) {
1832 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001833 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001834 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001835 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001836 }
1837 fHWBlendDisabled = blendOff;
1838 }
1839 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001840 if (fHWDrawState.fSrcBlend != srcCoeff ||
1841 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001842 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1843 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001844 fHWDrawState.fSrcBlend = srcCoeff;
1845 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001846 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001847 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1848 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001849 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1850
1851 float c[] = {
1852 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1853 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1854 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1855 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1856 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001857 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001858 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1859 }
1860 }
1861 }
1862}
1863
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001864static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1865 switch (filter) {
1866 case GrSamplerState::kBilinear_Filter:
1867 case GrSamplerState::k4x4Downsample_Filter:
1868 return GR_GL_LINEAR;
1869 case GrSamplerState::kNearest_Filter:
1870 case GrSamplerState::kConvolution_Filter:
1871 return GR_GL_NEAREST;
1872 default:
1873 GrAssert(!"Unknown filter type");
1874 return GR_GL_LINEAR;
1875 }
1876}
1877
bsalomon@google.comffca4002011-02-22 20:34:01 +00001878bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001879
1880 // GrGpu::setupClipAndFlushState should have already checked this
1881 // and bailed if not true.
1882 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001883
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001884 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001885 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001886 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001887 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001888
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001889 // true for now, but maybe not with GrEffect.
1890 GrAssert(NULL != nextTexture);
1891 // if we created a rt/tex and rendered to it without using a
1892 // texture and now we're texuring from the rt it will still be
1893 // the last bound texture, but it needs resolving. So keep this
1894 // out of the "last != next" check.
1895 GrGLRenderTarget* texRT =
1896 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1897 if (NULL != texRT) {
1898 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001899 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001900
1901 if (fHWDrawState.fTextures[s] != nextTexture) {
1902 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001903 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001904 #if GR_COLLECT_STATS
1905 ++fStats.fTextureChngCnt;
1906 #endif
1907 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1908 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001909 // The texture matrix has to compensate for texture width/height
1910 // and NPOT-embedded-in-POT
1911 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001912 }
1913
1914 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
1915 const GrGLTexture::TexParams& oldTexParams =
1916 nextTexture->getTexParams();
1917 GrGLTexture::TexParams newTexParams;
1918
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001919 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001920
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001921 const GrGLenum* wraps =
1922 GrGLTexture::WrapMode2GLWrap(this->glBinding());
1923 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1924 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001925
1926 if (newTexParams.fFilter != oldTexParams.fFilter) {
1927 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001928 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1929 GR_GL_TEXTURE_MAG_FILTER,
1930 newTexParams.fFilter));
1931 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1932 GR_GL_TEXTURE_MIN_FILTER,
1933 newTexParams.fFilter));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001934 }
1935 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
1936 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1938 GR_GL_TEXTURE_WRAP_S,
1939 newTexParams.fWrapS));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001940 }
1941 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
1942 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001943 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1944 GR_GL_TEXTURE_WRAP_T,
1945 newTexParams.fWrapT));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001946 }
1947 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00001948 }
1949 }
1950
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001951 GrIRect* rect = NULL;
1952 GrIRect clipBounds;
1953 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
1954 fClip.hasConservativeBounds()) {
1955 fClip.getConservativeBounds().roundOut(&clipBounds);
1956 rect = &clipBounds;
1957 }
1958 this->flushRenderTarget(rect);
1959 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001960
reed@google.comac10a2d2010-12-22 21:39:39 +00001961 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
1962 (fHWDrawState.fFlagBits & kDither_StateBit)) {
1963 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001964 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00001965 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001966 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00001967 }
1968 }
1969
bsalomon@google.comd302f142011-03-03 13:54:13 +00001970 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
1971 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001972 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001973 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001974 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001975 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001976 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001977 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001978 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001979 }
1980
bsalomon@google.comd302f142011-03-03 13:54:13 +00001981 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
1982 switch (fCurrDrawState.fDrawFace) {
1983 case kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001984 GL_CALL(Enable(GR_GL_CULL_FACE));
1985 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001986 break;
1987 case kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001988 GL_CALL(Enable(GR_GL_CULL_FACE));
1989 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001990 break;
1991 case kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001992 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001993 break;
1994 default:
1995 GrCrash("Unknown draw face.");
1996 }
1997 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
1998 }
1999
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002000#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002001 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002002 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002003 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002004 NULL == fCurrDrawState.fRenderTarget ||
2005 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002006 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002007 fCurrDrawState.fRenderTarget);
2008 }
2009#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002010
reed@google.comac10a2d2010-12-22 21:39:39 +00002011 flushStencil();
2012
bsalomon@google.comd302f142011-03-03 13:54:13 +00002013 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002014 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002015 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002016}
2017
2018void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002019 if (fHWGeometryState.fVertexBuffer != buffer) {
2020 fHWGeometryState.fArrayPtrsDirty = true;
2021 fHWGeometryState.fVertexBuffer = buffer;
2022 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002023}
2024
2025void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002026 if (fHWGeometryState.fVertexBuffer == buffer) {
2027 // deleting bound buffer does implied bind to 0
2028 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002029 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002030 }
2031}
2032
2033void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002034 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002035}
2036
2037void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002038 if (fHWGeometryState.fIndexBuffer == buffer) {
2039 // deleting bound buffer does implied bind to 0
2040 fHWGeometryState.fIndexBuffer = NULL;
2041 }
2042}
2043
reed@google.comac10a2d2010-12-22 21:39:39 +00002044void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2045 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002046 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002047 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002048 }
2049 if (fHWDrawState.fRenderTarget == renderTarget) {
2050 fHWDrawState.fRenderTarget = NULL;
2051 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002052}
2053
2054void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002055 for (int s = 0; s < kNumStages; ++s) {
2056 if (fCurrDrawState.fTextures[s] == texture) {
2057 fCurrDrawState.fTextures[s] = NULL;
2058 }
2059 if (fHWDrawState.fTextures[s] == texture) {
2060 // deleting bound texture does implied bind to 0
2061 fHWDrawState.fTextures[s] = NULL;
2062 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002063 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002064}
2065
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002066bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002067 GrGLenum* internalFormat,
2068 GrGLenum* format,
2069 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002070 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002071 case kRGBA_8888_GrPixelConfig:
2072 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002073 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002074 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002075 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2076 // format for a BGRA is BGRA not RGBA (as on desktop)
2077 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2078 } else {
2079 *internalFormat = GR_GL_RGBA;
2080 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002081 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002082 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002083 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002084 *format = GR_GL_RGB;
2085 *internalFormat = GR_GL_RGB;
2086 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002087 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002088 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002089 *format = GR_GL_RGBA;
2090 *internalFormat = GR_GL_RGBA;
2091 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002092 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002093 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002094 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002095 *format = GR_GL_PALETTE8_RGBA8;
2096 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002097 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002098 } else {
2099 return false;
2100 }
2101 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002102 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002103 *format = GR_GL_ALPHA;
2104 *internalFormat = GR_GL_ALPHA;
2105 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002106 break;
2107 default:
2108 return false;
2109 }
2110 return true;
2111}
2112
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002113void GrGpuGL::setTextureUnit(int unit) {
2114 GrAssert(unit >= 0 && unit < kNumStages);
2115 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002116 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002117 fActiveTextureUnitIdx = unit;
2118 }
2119}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002120
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002121void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002122 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002123 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002124 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2125 }
2126}
2127
reed@google.comac10a2d2010-12-22 21:39:39 +00002128/* On ES the internalFormat and format must match for TexImage and we use
2129 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2130 decide the internalFormat. However, on ES internalFormat for
2131 RenderBufferStorage* has to be a specific format (not a base format like
2132 GL_RGBA).
2133 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002134bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002135 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002136 case kRGBA_8888_GrPixelConfig:
2137 case kRGBX_8888_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002138 if (fGLCaps.fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002139 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002140 return true;
2141 } else {
2142 return false;
2143 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002144 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002145 // ES2 supports 565. ES1 supports it
2146 // with FBO extension desktop GL has
2147 // no such internal format
2148 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002149 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002150 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002151 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002152 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002153 return true;
2154 default:
2155 return false;
2156 }
2157}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002158
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002159void GrGpuGL::resetDirtyFlags() {
2160 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2161}
2162
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002163void GrGpuGL::setBuffers(bool indexed,
2164 int* extraVertexOffset,
2165 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002166
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002167 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002168
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002169 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2170
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002171 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002172 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002173 case kBuffer_GeometrySrcType:
2174 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002175 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002176 break;
2177 case kArray_GeometrySrcType:
2178 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002179 this->finalizeReservedVertices();
2180 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2181 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002182 break;
2183 default:
2184 vbuf = NULL; // suppress warning
2185 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002186 }
2187
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002188 GrAssert(NULL != vbuf);
2189 GrAssert(!vbuf->isLocked());
2190 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002191 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002192 fHWGeometryState.fArrayPtrsDirty = true;
2193 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002194 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002195
2196 if (indexed) {
2197 GrAssert(NULL != extraIndexOffset);
2198
2199 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002200 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002201 case kBuffer_GeometrySrcType:
2202 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002203 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002204 break;
2205 case kArray_GeometrySrcType:
2206 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002207 this->finalizeReservedIndices();
2208 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2209 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002210 break;
2211 default:
2212 ibuf = NULL; // suppress warning
2213 GrCrash("Unknown geometry src type!");
2214 }
2215
2216 GrAssert(NULL != ibuf);
2217 GrAssert(!ibuf->isLocked());
2218 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002220 fHWGeometryState.fIndexBuffer = ibuf;
2221 }
2222 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002223}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002224
2225int GrGpuGL::getMaxEdges() const {
2226 // FIXME: This is a pessimistic estimate based on how many other things
2227 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002228 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8, kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002229}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002230
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002231void GrGpuGL::GLCaps::print() const {
2232 for (int i = 0; i < fStencilFormats.count(); ++i) {
2233 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2234 i,
2235 fStencilFormats[i].fStencilBits,
2236 fStencilFormats[i].fTotalBits);
2237 }
2238
2239 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2240 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2241 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2242 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2243 static const char* gMSFBOExtStr[] = {
2244 "None",
2245 "ARB",
2246 "EXT",
2247 "Apple",
2248 };
2249 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002250 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002251 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2252 }
2253 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2254 GrPrintf("Support RGBA8 Render Buffer: %s\n",
2255 (fRGBA8Renderbuffer ? "YES": "NO"));
2256}