blob: bae4f7762a848239ca9b6b14cb71332c41818bbd [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
twiz@google.com0f31ca72011-03-18 17:38:11 +000027static const GrGLenum gXfermodeCoeff2Blend[] = {
28 GR_GL_ZERO,
29 GR_GL_ONE,
30 GR_GL_SRC_COLOR,
31 GR_GL_ONE_MINUS_SRC_COLOR,
32 GR_GL_DST_COLOR,
33 GR_GL_ONE_MINUS_DST_COLOR,
34 GR_GL_SRC_ALPHA,
35 GR_GL_ONE_MINUS_SRC_ALPHA,
36 GR_GL_DST_ALPHA,
37 GR_GL_ONE_MINUS_DST_ALPHA,
38 GR_GL_CONSTANT_COLOR,
39 GR_GL_ONE_MINUS_CONSTANT_COLOR,
40 GR_GL_CONSTANT_ALPHA,
41 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
43 // extended blend coeffs
44 GR_GL_SRC1_COLOR,
45 GR_GL_ONE_MINUS_SRC1_COLOR,
46 GR_GL_SRC1_ALPHA,
47 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000048};
49
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 static const bool gCoeffReferencesBlendConst[] = {
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 false,
62 true,
63 true,
64 true,
65 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000066
67 // extended blend coeffs
68 false,
69 false,
70 false,
71 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000072 };
73 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
75
76 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
77 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
78 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
79 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
80 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
81 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
82 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
83 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
84 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
85 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
86 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
87 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
88 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
89 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
90
91 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
92 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
93 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
94 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
95
96 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
97 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000098}
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
103 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104 GrMatrix* matrix) {
105 GrAssert(NULL != texture);
106 GrAssert(NULL != matrix);
107 if (GR_Scalar1 != texture->contentScaleX() ||
108 GR_Scalar1 != texture->contentScaleY()) {
109 if (GrSamplerState::kRadial_SampleMode == mode) {
110 GrMatrix scale;
111 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
112 matrix->postConcat(scale);
113 } else if (GrSamplerState::kNormal_SampleMode == mode) {
114 GrMatrix scale;
115 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
116 matrix->postConcat(scale);
117 } else {
118 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
119 }
120 }
121 GrGLTexture::Orientation orientation = texture->orientation();
122 if (GrGLTexture::kBottomUp_Orientation == orientation) {
123 GrMatrix invY;
124 invY.setAll(GR_Scalar1, 0, 0,
125 0, -GR_Scalar1, GR_Scalar1,
126 0, 0, GrMatrix::I()[8]);
127 matrix->postConcat(invY);
128 } else {
129 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
130 }
131}
132
bsalomon@google.comd302f142011-03-03 13:54:13 +0000133bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134 const GrSamplerState& sampler) {
135 GrAssert(NULL != texture);
136 if (!sampler.getMatrix().isIdentity()) {
137 return false;
138 }
139 if (GR_Scalar1 != texture->contentScaleX() ||
140 GR_Scalar1 != texture->contentScaleY()) {
141 return false;
142 }
143 GrGLTexture::Orientation orientation = texture->orientation();
144 if (GrGLTexture::kBottomUp_Orientation == orientation) {
145 return false;
146 } else {
147 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
148 }
149 return true;
150}
151
152///////////////////////////////////////////////////////////////////////////////
153
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000154static bool gPrintStartupSpew;
155
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000156static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000157
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000158 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000159
twiz@google.com0f31ca72011-03-18 17:38:11 +0000160 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
162 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000163 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000164 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
165 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000166 // some implementations require texture to be mip-map complete before
167 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000168 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
169 GR_GL_TEXTURE_MIN_FILTER,
170 GR_GL_NEAREST));
171 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
172 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
173 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
174 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
175 GR_GL_COLOR_ATTACHMENT0,
176 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000177 GrGLenum status;
178 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000179 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
180 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000181
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000182 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000183}
184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185static bool probe_for_npot_render_target_support(const GrGLInterface* gl,
186 bool hasNPOTTextureSupport) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000187
188 /* Experimentation has found that some GLs that support NPOT textures
189 do not support FBOs with a NPOT texture. They report "unsupported" FBO
190 status. I don't know how to explicitly query for this. Do an
191 experiment. Note they may support NPOT with a renderbuffer but not a
192 texture. Presumably, the implementation bloats the renderbuffer
193 internally to the next POT.
194 */
195 if (hasNPOTTextureSupport) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000196 return fbo_test(gl, 200, 200);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000197 }
198 return false;
199}
200
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000201static int probe_for_min_render_target_height(const GrGLInterface* gl,
202 bool hasNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000203 int maxRenderTargetSize) {
204 /* The iPhone 4 has a restriction that for an FBO with texture color
205 attachment with height <= 8 then the width must be <= height. Here
206 we look for such a limitation.
207 */
208 if (gPrintStartupSpew) {
209 GrPrintf("Small height FBO texture experiments\n");
210 }
211 int minRenderTargetHeight = GR_INVAL_GLINT;
212 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
213 GrGLuint w = maxRenderTargetSize;
214 GrGLuint h = i;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000215 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000216 if (gPrintStartupSpew) {
217 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
218 }
219 minRenderTargetHeight = i;
220 break;
221 } else {
222 if (gPrintStartupSpew) {
223 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
224 }
225 }
226 }
227 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
228
229 return minRenderTargetHeight;
230}
231
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000232static int probe_for_min_render_target_width(const GrGLInterface* gl,
233 bool hasNPOTRenderTargetSupport,
234 int maxRenderTargetSize) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000235
236 if (gPrintStartupSpew) {
237 GrPrintf("Small width FBO texture experiments\n");
238 }
239 int minRenderTargetWidth = GR_INVAL_GLINT;
240 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
241 GrGLuint w = i;
242 GrGLuint h = maxRenderTargetSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000243 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000244 if (gPrintStartupSpew) {
245 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
246 }
247 minRenderTargetWidth = i;
248 break;
249 } else {
250 if (gPrintStartupSpew) {
251 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
252 }
253 }
254 }
255 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
256
257 return minRenderTargetWidth;
258}
259
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000260GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000261
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000263
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000264 gl->ref();
265 fGL = gl;
266 fGLBinding = glBinding;
267 switch (glBinding) {
268 case kDesktop_GrGLBinding:
269 GrAssert(gl->supportsDesktop());
270 break;
271 case kES1_GrGLBinding:
272 GrAssert(gl->supportsES1());
273 break;
274 case kES2_GrGLBinding:
275 GrAssert(gl->supportsES2());
276 break;
277 default:
278 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000279 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000280
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000281 GrGLClearErr(fGL);
282
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000283 const GrGLubyte* ext;
284 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000285 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000286 const GrGLubyte* vendor;
287 const GrGLubyte* renderer;
288 const GrGLubyte* version;
289 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
290 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
291 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000292 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
293 this);
294 GrPrintf("------ VENDOR %s\n", vendor);
295 GrPrintf("------ RENDERER %s\n", renderer);
296 GrPrintf("------ VERSION %s\n", version);
297 GrPrintf("------ EXTENSIONS\n %s \n", ext);
298 }
299
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000300 fGLVersion = GrGLGetVersion(gl);
301 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000302 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000304 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000305
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000306 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000307
bsalomon@google.comfe676522011-06-17 18:12:21 +0000308 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000309}
310
311GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000312 // This must be called by before the GrDrawTarget destructor
313 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000314 // This subclass must do this before the base class destructor runs
315 // since we will unref the GrGLInterface.
316 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000317 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000318}
319
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000320///////////////////////////////////////////////////////////////////////////////
321
322static const GrGLuint kUnknownBitCount = ~0;
323
324void GrGpuGL::initCaps() {
325 GrGLint maxTextureUnits;
326 // check FS and fixed-function texture unit limits
327 // we only use textures in the fragment stage currently.
328 // checks are > to make sure we have a spare unit.
329 if (kES1_GrGLBinding != this->glBinding()) {
330 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000331 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000332 }
333 if (kES2_GrGLBinding != this->glBinding()) {
334 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000335 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000336 }
337 if (kES2_GrGLBinding == this->glBinding()) {
338 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
339 &fGLCaps.fMaxFragmentUniformVectors);
340 } else if (kDesktop_GrGLBinding != this->glBinding()) {
341 GrGLint max;
342 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
343 fGLCaps.fMaxFragmentUniformVectors = max / 4;
344 } else {
345 fGLCaps.fMaxFragmentUniformVectors = 16;
346 }
347
348 GrGLint numFormats;
349 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
350 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
351 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
352 for (int i = 0; i < numFormats; ++i) {
353 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
354 fCaps.f8BitPaletteSupport = true;
355 break;
356 }
357 }
358
359 if (kDesktop_GrGLBinding == this->glBinding()) {
360 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
361 this->hasExtension("GL_EXT_stencil_wrap");
362 } else {
363 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(2,0)) ||
364 this->hasExtension("GL_OES_stencil_wrap");
365 }
366
367 if (kDesktop_GrGLBinding == this->glBinding()) {
368 // we could also look for GL_ATI_separate_stencil extension or
369 // GL_EXT_stencil_two_side but they use different function signatures
370 // than GL2.0+ (and than each other).
371 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
372 // supported on GL 1.4 and higher or by extension
373 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
374 this->hasExtension("GL_EXT_stencil_wrap");
375 } else {
376 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
377 // an ES1 extension.
378 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
379 // stencil wrap support is in ES2, ES1 requires extension.
380 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(2,0)) ||
381 this->hasExtension("GL_OES_stencil_wrap");
382 }
383
384 if (kDesktop_GrGLBinding == this->glBinding()) {
385 fGLCaps.fRGBA8Renderbuffer = true;
386 } else {
387 fGLCaps.fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
388 }
389
390
391 if (kDesktop_GrGLBinding != this->glBinding()) {
392 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
393 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
394 }
395 }
396
397 if (kDesktop_GrGLBinding == this->glBinding()) {
398 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
399 // extension includes glMapBuffer.
400 } else {
401 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
402 }
403
404 if (kDesktop_GrGLBinding == this->glBinding()) {
405 if (fGLVersion >= GR_GL_VER(2,0) ||
406 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
407 fCaps.fNPOTTextureTileSupport = true;
408 fCaps.fNPOTTextureSupport = true;
409 } else {
410 fCaps.fNPOTTextureTileSupport = false;
411 fCaps.fNPOTTextureSupport = false;
412 }
413 } else {
414 if (fGLVersion >= GR_GL_VER(2,0)) {
415 fCaps.fNPOTTextureSupport = true;
416 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
417 } else {
418 fCaps.fNPOTTextureSupport =
419 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
420 fCaps.fNPOTTextureTileSupport = false;
421 }
422 }
423
424 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
425
426 ////////////////////////////////////////////////////////////////////////////
427 // Experiments to determine limitations that can't be queried.
428 // TODO: Make these a preprocess that generate some compile time constants.
429 // TODO: probe once at startup, rather than once per context creation.
430
431 int expectNPOTTargets = fGL->fNPOTRenderTargetSupport;
432 if (expectNPOTTargets == kProbe_GrGLCapability) {
433 fCaps.fNPOTRenderTargetSupport =
434 probe_for_npot_render_target_support(fGL, fCaps.fNPOTTextureSupport);
435 } else {
436 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000437 fCaps.fNPOTRenderTargetSupport = (0 != expectNPOTTargets);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000438 }
439
440 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
441 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
442 // Our render targets are always created with textures as the color
443 // attachment, hence this min:
444 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
445
446 fCaps.fMinRenderTargetHeight = fGL->fMinRenderTargetHeight;
447 if (fCaps.fMinRenderTargetHeight == kProbe_GrGLCapability) {
448 fCaps.fMinRenderTargetHeight =
449 probe_for_min_render_target_height(fGL, fCaps.fNPOTRenderTargetSupport,
450 fCaps.fMaxRenderTargetSize);
451 }
452
453 fCaps.fMinRenderTargetWidth = fGL->fMinRenderTargetWidth;
454 if (fCaps.fMinRenderTargetWidth == kProbe_GrGLCapability) {
455 fCaps.fMinRenderTargetWidth =
456 probe_for_min_render_target_width(fGL, fCaps.fNPOTRenderTargetSupport,
457 fCaps.fMaxRenderTargetSize);
458 }
459
460 this->initFSAASupport();
461 this->initStencilFormats();
462}
463
464void GrGpuGL::initFSAASupport() {
465 // TODO: Get rid of GrAALevel and use # samples directly.
466 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
467 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
468 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
469 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
470 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
471
472 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
473 if (kDesktop_GrGLBinding != this->glBinding()) {
474 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
475 // chrome's extension is equivalent to the EXT msaa
476 // and fbo_blit extensions.
477 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
478 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
479 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
480 }
481 } else {
482 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
483 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
484 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
485 this->hasExtension("GL_EXT_framebuffer_blit")) {
486 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
487 }
488 }
489
490 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
491 GrGLint maxSamples;
492 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
493 if (maxSamples > 1 ) {
494 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
495 fGLCaps.fAASamples[kLow_GrAALevel] =
496 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
497 fGLCaps.fAASamples[kMed_GrAALevel] =
498 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
499 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
500 }
501 }
502 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
503}
504
505void GrGpuGL::initStencilFormats() {
506
507 // Build up list of legal stencil formats (though perhaps not supported on
508 // the particular gpu/driver) from most preferred to least.
509
510 // these consts are in order of most preferred to least preferred
511 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
512 static const GrGLStencilBuffer::Format
513 // internal Format stencil bits total bits packed?
514 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
515 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
516 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
517 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
518 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
519 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
520
521 if (kDesktop_GrGLBinding == this->glBinding()) {
522 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
523 this->hasExtension("GL_EXT_packed_depth_stencil") ||
524 this->hasExtension("GL_ARB_framebuffer_object");
525
526 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
527 // require FBO support we can expect these are legal formats and don't
528 // check. These also all support the unsized GL_STENCIL_INDEX.
529 fGLCaps.fStencilFormats.push_back() = gS8;
530 fGLCaps.fStencilFormats.push_back() = gS16;
531 if (supportsPackedDS) {
532 fGLCaps.fStencilFormats.push_back() = gD24S8;
533 }
534 fGLCaps.fStencilFormats.push_back() = gS4;
535 if (supportsPackedDS) {
536 fGLCaps.fStencilFormats.push_back() = gDS;
537 }
538 } else {
539 // ES2 has STENCIL_INDEX8 without extensions.
540 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
541 // introduces tokens for S1 thu S8 but there are separate extensions
542 // that make them legal (GL_OES_stencil1, ...).
543 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
544 // ES doesn't support using the unsized formats.
545
546 if (fGLVersion >= GR_GL_VER(2,0) ||
547 this->hasExtension("GL_OES_stencil8")) {
548 fGLCaps.fStencilFormats.push_back() = gS8;
549 }
550 //fStencilFormats.push_back() = gS16;
551 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
552 fGLCaps.fStencilFormats.push_back() = gD24S8;
553 }
554 if (this->hasExtension("GL_OES_stencil4")) {
555 fGLCaps.fStencilFormats.push_back() = gS4;
556 }
557 // we require some stencil format.
558 GrAssert(fGLCaps.fStencilFormats.count() > 0);
559 }
560}
561
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000562void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000563 if (gPrintStartupSpew && !fPrintedCaps) {
564 fPrintedCaps = true;
565 this->getCaps().print();
566 fGLCaps.print();
567 }
568
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000569 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000570 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000572
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000573 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000574 GL_CALL(Disable(GR_GL_DEPTH_TEST));
575 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000576
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GL_CALL(Disable(GR_GL_CULL_FACE));
578 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000579 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(Disable(GR_GL_DITHER));
582 if (kDesktop_GrGLBinding == this->glBinding()) {
583 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
584 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
585 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000586 fHWAAState.fMSAAEnabled = false;
587 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000588 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000589
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000590 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000591 fHWDrawState.fFlagBits = 0;
592
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000594 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000595
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000596 // invalid
597 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000598
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000600 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
601 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000602
603 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000604 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000607
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000608 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000609
tomhudson@google.com93813632011-10-27 20:21:16 +0000610 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000611 fHWDrawState.fTextures[s] = NULL;
612 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
613 -GR_ScalarMax,
614 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000615 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000616 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000617 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000618
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000619 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000620 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000621 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000622 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000623
bsalomon@google.comd302f142011-03-03 13:54:13 +0000624 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000626 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000627
628 fHWGeometryState.fIndexBuffer = NULL;
629 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000630
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000631 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000632
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000633 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000634 fHWDrawState.fRenderTarget = NULL;
635}
636
bsalomon@google.come269f212011-11-07 13:29:52 +0000637GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
638 GrGLenum internalFormat; // we don't need this value
639 GrGLTexture::Desc glTexDesc;
640 if (!this->canBeTexture(desc.fConfig, &internalFormat,
641 &glTexDesc.fUploadFormat, &glTexDesc.fUploadType)) {
642 return NULL;
643 }
644
645 glTexDesc.fContentWidth = glTexDesc.fAllocWidth = desc.fWidth;
646 glTexDesc.fContentHeight = glTexDesc.fAllocHeight = desc.fHeight;
647 glTexDesc.fConfig = desc.fConfig;
648 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
649 glTexDesc.fOwnsID = false;
650 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
651
652 GrGLTexture* texture = NULL;
653 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
654 GrGLRenderTarget::Desc glRTDesc;
655 glRTDesc.fRTFBOID = 0;
656 glRTDesc.fTexFBOID = 0;
657 glRTDesc.fMSColorRenderbufferID = 0;
658 glRTDesc.fOwnIDs = true;
659 glRTDesc.fConfig = desc.fConfig;
660 glRTDesc.fSampleCnt = desc.fSampleCnt;
661 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
662 glTexDesc.fAllocHeight,
663 glTexDesc.fTextureID,
664 &glRTDesc)) {
665 return NULL;
666 }
667 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
668 } else {
669 texture = new GrGLTexture(this, glTexDesc);
670 }
671 if (NULL == texture) {
672 return NULL;
673 }
674
675 this->setSpareTextureUnit();
676 return texture;
677}
678
679GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
680 GrGLRenderTarget::Desc glDesc;
681 glDesc.fConfig = desc.fConfig;
682 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
683 glDesc.fMSColorRenderbufferID = 0;
684 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
685 glDesc.fSampleCnt = desc.fSampleCnt;
686 glDesc.fOwnIDs = false;
687 GrGLIRect viewport;
688 viewport.fLeft = 0;
689 viewport.fBottom = 0;
690 viewport.fWidth = desc.fWidth;
691 viewport.fHeight = desc.fHeight;
692
693 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
694 if (desc.fStencilBits) {
695 GrGLStencilBuffer::Format format;
696 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
697 format.fPacked = false;
698 format.fStencilBits = desc.fStencilBits;
699 format.fTotalBits = desc.fStencilBits;
700 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
701 0,
702 desc.fWidth,
703 desc.fHeight,
704 desc.fSampleCnt,
705 format);
706 tgt->setStencilBuffer(sb);
707 sb->unref();
708 }
709 return tgt;
710}
711
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000712GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
713
714 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
715 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
716 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
717 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
718
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000719 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000720 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000721
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000722 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000723 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000724 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000725 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000726 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000727 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000728 } else {
729 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000730 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000731 }
732 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000733 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000734 }
735 // we don't know what the RB ids are without glGets and we don't care
736 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000737 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000738 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000739 if (desc.fStencilBits) {
740 GrGLStencilBuffer::Format format;
741 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
742 format.fPacked = false;
743 format.fStencilBits = desc.fStencilBits;
744 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000745 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
746 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000747 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000748 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000749 }
750
751 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000752 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000753 GrGLenum dontCare;
754 if (!canBeTexture(desc.fConfig, &dontCare,
755 &texDesc.fUploadFormat,
756 &texDesc.fUploadType)) {
757 return NULL;
758 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000759 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
760 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
761
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000762 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000763 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000764 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000765 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000766
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000767 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000768 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000769 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000770 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000771 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000772 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000773 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000774 } else {
775 GrGLIRect viewport;
776 viewport.fLeft = 0;
777 viewport.fBottom = 0;
778 viewport.fWidth = desc.fWidth;
779 viewport.fHeight = desc.fHeight;
780
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000781 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000782 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000783 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000784 }
785}
786
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000787
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000788////////////////////////////////////////////////////////////////////////////////
789
790void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
791 GrGLenum internalFormat,
792 const void* data,
793 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000794 // we assume the texture is bound
795
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000796 size_t bpp = GrBytesPerPixel(desc.fConfig);
797 size_t trimRowBytes = desc.fContentWidth * bpp;
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000798
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000799 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000800 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000801 }
802
803 // in case we need a temporary, trimmed copy of the src pixels
804 SkAutoSMalloc<128 * 128> tempStorage;
805
806 /*
807 * check whether to allocate a temporary buffer for flipping y or
808 * because our data has extra bytes past each row. If so, we need
809 * to trim those off here, since GL ES doesn't let us specify
810 * GL_UNPACK_ROW_LENGTH.
811 */
812 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000813 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000814 if (data && rowBytes != trimRowBytes) {
815 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
816 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000817 }
818 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000819 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000820 // copy the data into our new storage, skipping the trailing bytes
821 size_t trimSize = desc.fContentHeight * trimRowBytes;
822 const char* src = (const char*)data;
823 if (flipY) {
824 src += (desc.fContentHeight - 1) * rowBytes;
825 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000826 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000827 for (int y = 0; y < desc.fContentHeight; y++) {
828 memcpy(dst, src, trimRowBytes);
829 if (flipY) {
830 src -= rowBytes;
831 } else {
832 src += rowBytes;
833 }
834 dst += trimRowBytes;
835 }
836 // now point data to our trimmed version
837 data = tempStorage.get();
838 rowBytes = trimRowBytes;
839 }
840 }
841
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000842 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000843 if (kIndex_8_GrPixelConfig == desc.fConfig &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000844 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000845 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
846 GrAssert(desc.fContentWidth == desc.fAllocWidth);
847 GrAssert(desc.fContentHeight == desc.fAllocHeight);
848 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
849 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000850 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
851 desc.fAllocWidth, desc.fAllocHeight,
852 0, imageSize, data));
853 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000854 } else {
855 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
856 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000857 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
858 desc.fAllocWidth, desc.fAllocHeight,
859 0, desc.fUploadFormat, desc.fUploadType, NULL));
860 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
861 desc.fContentHeight, desc.fUploadFormat,
862 desc.fUploadType, data));
863 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000864
865 int extraW = desc.fAllocWidth - desc.fContentWidth;
866 int extraH = desc.fAllocHeight - desc.fContentHeight;
867 int maxTexels = extraW * extraH;
868 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
869 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
870
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000871 SkAutoSMalloc<128*128> texels(bpp * maxTexels);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000872
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000873 if (extraH) {
874 uint8_t* lastRowStart = (uint8_t*) data +
875 (desc.fContentHeight - 1) * rowBytes;
876 uint8_t* extraRowStart = (uint8_t*)texels.get();
877
878 for (int i = 0; i < extraH; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000879 memcpy(extraRowStart, lastRowStart, trimRowBytes);
880 extraRowStart += trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000881 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000882 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
883 desc.fContentHeight, desc.fContentWidth,
884 extraH, desc.fUploadFormat,
885 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000886 }
887 if (extraW) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000888 uint8_t* edgeTexel = (uint8_t*)data + trimRowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000889 uint8_t* extraTexel = (uint8_t*)texels.get();
890 for (int j = 0; j < desc.fContentHeight; ++j) {
891 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000892 memcpy(extraTexel, edgeTexel, bpp);
893 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000894 }
895 edgeTexel += rowBytes;
896 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000897 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
898 0, extraW, desc.fContentHeight,
899 desc.fUploadFormat, desc.fUploadType,
900 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000901 }
902 if (extraW && extraH) {
903 uint8_t* cornerTexel = (uint8_t*)data +
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000904 desc.fContentHeight * rowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000905 uint8_t* extraTexel = (uint8_t*)texels.get();
906 for (int i = 0; i < extraW*extraH; ++i) {
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000907 memcpy(extraTexel, cornerTexel, bpp);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000908 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000909 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000910 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
911 desc.fContentHeight, extraW, extraH,
912 desc.fUploadFormat, desc.fUploadType,
913 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000914 }
915
916 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000917 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
918 desc.fAllocWidth, desc.fAllocHeight, 0,
919 desc.fUploadFormat, desc.fUploadType, data));
920 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000921 }
922 }
923}
924
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000925bool GrGpuGL::createRenderTargetObjects(int width, int height,
926 GrGLuint texID,
927 GrGLRenderTarget::Desc* desc) {
928 desc->fMSColorRenderbufferID = 0;
929 desc->fRTFBOID = 0;
930 desc->fTexFBOID = 0;
931 desc->fOwnIDs = true;
932
933 GrGLenum status;
934 GrGLint err;
935
bsalomon@google.comab15d612011-08-09 12:57:56 +0000936 GrGLenum msColorFormat = 0; // suppress warning
937
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000938 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 if (!desc->fTexFBOID) {
940 goto FAILED;
941 }
942
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943
944 // If we are using multisampling we will create two FBOS. We render
945 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000946 if (desc->fSampleCnt > 0) {
947 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
948 goto FAILED;
949 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000950 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
951 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000952 if (!desc->fRTFBOID ||
953 !desc->fMSColorRenderbufferID ||
954 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
955 goto FAILED;
956 }
957 } else {
958 desc->fRTFBOID = desc->fTexFBOID;
959 }
960
961 if (desc->fRTFBOID != desc->fTexFBOID) {
962 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000963 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000964 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GR_GL_CALL_NOERRCHECK(this->glInterface(),
966 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
967 desc->fSampleCnt,
968 msColorFormat,
969 width, height));
970 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 if (err != GR_GL_NO_ERROR) {
972 goto FAILED;
973 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000974 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
975 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000976 GR_GL_COLOR_ATTACHMENT0,
977 GR_GL_RENDERBUFFER,
978 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000979 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000980 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
981 goto FAILED;
982 }
983 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000984 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000985
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000986 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
987 GR_GL_COLOR_ATTACHMENT0,
988 GR_GL_TEXTURE_2D,
989 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000990 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000991 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
992 goto FAILED;
993 }
994
995 return true;
996
997FAILED:
998 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000999 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001000 }
1001 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001002 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001003 }
1004 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001005 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001006 }
1007 return false;
1008}
1009
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001010// good to set a break-point here to know when createTexture fails
1011static GrTexture* return_null_texture() {
1012// GrAssert(!"null texture");
1013 return NULL;
1014}
1015
1016#if GR_DEBUG
1017static size_t as_size_t(int x) {
1018 return x;
1019}
1020#endif
1021
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001022GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001023 const void* srcData,
1024 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001025
1026#if GR_COLLECT_STATS
1027 ++fStats.fTextureCreateCnt;
1028#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001029
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001030 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001031 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001032 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001033
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001034 glTexDesc.fContentWidth = desc.fWidth;
1035 glTexDesc.fContentHeight = desc.fHeight;
1036 glTexDesc.fAllocWidth = desc.fWidth;
1037 glTexDesc.fAllocHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001038 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001039 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001040
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001041 glRTDesc.fMSColorRenderbufferID = 0;
1042 glRTDesc.fRTFBOID = 0;
1043 glRTDesc.fTexFBOID = 0;
1044 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001045 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001046
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001047 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001048 if (!canBeTexture(desc.fConfig,
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001050 &glTexDesc.fUploadFormat,
1051 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 return return_null_texture();
1053 }
1054
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001055 const Caps& caps = this->getCaps();
1056
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001057 // We keep GrRenderTargets in GL's normal orientation so that they
1058 // can be drawn to by the outside world without the client having
1059 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001060 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001061 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001062
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001063 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1064 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1065 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1066 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 GrPrintf("AA RT requested but not supported on this platform.");
1068 }
1069
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001071 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001072 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1073 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001074 }
1075
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001076 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001078 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001079 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001080 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1081 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001082 return return_null_texture();
1083 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001084 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001085 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1086 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001087 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1088 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001089 return return_null_texture();
1090 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 }
1092
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001093 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001094 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001095 return return_null_texture();
1096 }
1097
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001098 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001099 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001100
1101 // Some drivers like to know these before seeing glTexImage2D. Some drivers
1102 // have a bug where an FBO won't be complete if it includes a texture that
1103 // is not complete (i.e. has mip levels or non-mip min filter).
1104 static const GrGLTexture::TexParams DEFAULT_TEX_PARAMS = {
1105 GR_GL_NEAREST,
1106 GR_GL_CLAMP_TO_EDGE,
1107 GR_GL_CLAMP_TO_EDGE
1108 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001109 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1110 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.come269f212011-11-07 13:29:52 +00001111 DEFAULT_TEX_PARAMS.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001112 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1113 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.come269f212011-11-07 13:29:52 +00001114 DEFAULT_TEX_PARAMS.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001115 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1116 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.come269f212011-11-07 13:29:52 +00001117 DEFAULT_TEX_PARAMS.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1119 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.come269f212011-11-07 13:29:52 +00001120 DEFAULT_TEX_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001121
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001122 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001123
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001124 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001125 if (renderTarget) {
1126#if GR_COLLECT_STATS
1127 ++fStats.fRenderTargetCreateCnt;
1128#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001129 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1130 glTexDesc.fAllocHeight,
1131 glTexDesc.fTextureID,
1132 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001133 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001134 return return_null_texture();
1135 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001136 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001138 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 }
bsalomon@google.come269f212011-11-07 13:29:52 +00001140 tex->setCachedTexParams(DEFAULT_TEX_PARAMS, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001141#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001142 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1143 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001144#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 return tex;
1146}
1147
1148namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001149void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1150 GrGLuint rb,
1151 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 // we shouldn't ever know one size and not the other
1153 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1154 (kUnknownBitCount == format->fTotalBits));
1155 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001156 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1158 (GrGLint*)&format->fStencilBits);
1159 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1162 (GrGLint*)&format->fTotalBits);
1163 format->fTotalBits += format->fStencilBits;
1164 } else {
1165 format->fTotalBits = format->fStencilBits;
1166 }
1167 }
1168}
1169}
1170
1171bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1172 int width, int height) {
1173
1174 // All internally created RTs are also textures. We don't create
1175 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1176 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001177 GrAssert(width >= rt->allocatedWidth());
1178 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179
1180 int samples = rt->numSamples();
1181 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001182 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 if (!sbID) {
1184 return false;
1185 }
1186
1187 GrGLStencilBuffer* sb = NULL;
1188
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001189 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 // we start with the last stencil format that succeeded in hopes
1193 // that we won't go through this loop more than once after the
1194 // first (painful) stencil creation.
1195 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001196 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001197 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 // version on a GL that doesn't have an MSAA extension.
1199 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001200 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1201 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 GR_GL_RENDERBUFFER,
1203 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001204 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 width,
1206 height));
1207 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001208 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1209 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001210 sFmt.fInternalFormat,
1211 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 }
1213
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001214 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 if (err == GR_GL_NO_ERROR) {
1216 // After sized formats we attempt an unsized format and take whatever
1217 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001218 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001220 sb = new GrGLStencilBuffer(this, sbID, width, height,
1221 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1223 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001224 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 return true;
1227 }
1228 sb->abandon(); // otherwise we lose sbID
1229 sb->unref();
1230 }
1231 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001233 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234}
1235
1236bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1237 GrRenderTarget* rt) {
1238 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1239
1240 GrGLuint fbo = glrt->renderFBOID();
1241
1242 if (NULL == sb) {
1243 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001244 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 GR_GL_STENCIL_ATTACHMENT,
1246 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001247 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248 GR_GL_DEPTH_ATTACHMENT,
1249 GR_GL_RENDERBUFFER, 0));
1250#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001251 GrGLenum status;
1252 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001253 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1254#endif
1255 }
1256 return true;
1257 } else {
1258 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1259 GrGLuint rb = glsb->renderbufferID();
1260
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001262 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1263 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001264 GR_GL_STENCIL_ATTACHMENT,
1265 GR_GL_RENDERBUFFER, rb));
1266 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001267 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 GR_GL_DEPTH_ATTACHMENT,
1269 GR_GL_RENDERBUFFER, rb));
1270 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001271 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001272 GR_GL_DEPTH_ATTACHMENT,
1273 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001275
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001276 GrGLenum status;
1277 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001278 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001279 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001280 GR_GL_STENCIL_ATTACHMENT,
1281 GR_GL_RENDERBUFFER, 0));
1282 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001283 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001284 GR_GL_DEPTH_ATTACHMENT,
1285 GR_GL_RENDERBUFFER, 0));
1286 }
1287 return false;
1288 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001289 return true;
1290 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001292}
1293
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001294////////////////////////////////////////////////////////////////////////////////
1295
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001296GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001297 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001298 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001300 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001301 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001302 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001304 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1305 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1306 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1307 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1308 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 // deleting bound buffer does implicit bind to 0
1310 fHWGeometryState.fVertexBuffer = NULL;
1311 return NULL;
1312 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001313 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 size, dynamic);
1315 fHWGeometryState.fVertexBuffer = vertexBuffer;
1316 return vertexBuffer;
1317 }
1318 return NULL;
1319}
1320
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001321GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001322 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001323 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001325 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1326 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001328 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1329 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1330 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1331 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1332 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 // deleting bound buffer does implicit bind to 0
1334 fHWGeometryState.fIndexBuffer = NULL;
1335 return NULL;
1336 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001337 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 size, dynamic);
1339 fHWGeometryState.fIndexBuffer = indexBuffer;
1340 return indexBuffer;
1341 }
1342 return NULL;
1343}
1344
reed@google.comac10a2d2010-12-22 21:39:39 +00001345void GrGpuGL::flushScissor(const GrIRect* rect) {
1346 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001347 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001348 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001349
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001350 GrGLIRect scissor;
1351 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001352 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001353 rect->width(), rect->height());
1354 if (scissor.contains(vp)) {
1355 rect = NULL;
1356 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 }
1358
1359 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001360 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001361 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001362 fHWBounds.fScissorRect = scissor;
1363 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001364 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001365 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001366 fHWBounds.fScissorEnabled = true;
1367 }
1368 } else {
1369 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001370 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001371 fHWBounds.fScissorEnabled = false;
1372 }
1373 }
1374}
1375
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001376void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001377 if (NULL == fCurrDrawState.fRenderTarget) {
1378 return;
1379 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001380 GrIRect r;
1381 if (NULL != rect) {
1382 // flushScissor expects rect to be clipped to the target.
1383 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001384 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1385 fCurrDrawState.fRenderTarget->height());
1386 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001387 rect = &r;
1388 } else {
1389 return;
1390 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001391 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001392 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001393 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001394 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001395 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001396 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1397 GrColorUnpackG(color)/255.f,
1398 GrColorUnpackB(color)/255.f,
1399 GrColorUnpackA(color)/255.f));
1400 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001401}
1402
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001403void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001404 if (NULL == fCurrDrawState.fRenderTarget) {
1405 return;
1406 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001407
1408 this->flushRenderTarget(&GrIRect::EmptyIRect());
1409
reed@google.comac10a2d2010-12-22 21:39:39 +00001410 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001411 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001412 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001414 GL_CALL(StencilMask(0xffffffff));
1415 GL_CALL(ClearStencil(0));
1416 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001417 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001418}
1419
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001420void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001421 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001422
1423 // this should only be called internally when we know we have a
1424 // stencil buffer.
1425 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001426 GrGLint stencilBitCount =
1427 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001428#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001429 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001430 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001431#else
1432 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001433 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001434 // turned into draws. Our contract on GrDrawTarget says that
1435 // changing the clip between stencil passes may or may not
1436 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001437 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001438#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001439 GrGLint value;
1440 if (insideClip) {
1441 value = (1 << (stencilBitCount - 1));
1442 } else {
1443 value = 0;
1444 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001445 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001446 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001447 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001448 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001449 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001450 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001451}
1452
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001453void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001454 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001455}
1456
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001457bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001458 int left, int top,
1459 int width, int height,
1460 GrPixelConfig config,
1461 void* buffer, size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001462 GrGLenum internalFormat; // we don't use this for glReadPixels
1463 GrGLenum format;
1464 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1466 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001467 }
1468
1469 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001470 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1471 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1472 switch (tgt->getResolveType()) {
1473 case GrGLRenderTarget::kCantResolve_ResolveType:
1474 return false;
1475 case GrGLRenderTarget::kAutoResolves_ResolveType:
1476 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1477 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001478 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001479 break;
1480 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001481 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001482 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001483 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1484 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001485 break;
1486 default:
1487 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001488 }
1489
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001490 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001491
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001492 // the read rect is viewport-relative
1493 GrGLIRect readRect;
1494 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001495
1496 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1497 if (0 == rowBytes) {
1498 rowBytes = tightRowBytes;
1499 }
1500 size_t readDstRowBytes = tightRowBytes;
1501 void* readDst = buffer;
1502
1503 // determine if GL can read using the passed rowBytes or if we need
1504 // a scratch buffer.
1505 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1506 if (rowBytes != tightRowBytes) {
1507 if (kDesktop_GrGLBinding == this->glBinding()) {
1508 GrAssert(!(rowBytes % sizeof(GrColor)));
1509 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1510 readDstRowBytes = rowBytes;
1511 } else {
1512 scratch.reset(tightRowBytes * height);
1513 readDst = scratch.get();
1514 }
1515 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001516 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1517 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001518 format, type, readDst));
1519 if (readDstRowBytes != tightRowBytes) {
1520 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1521 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001522
1523 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001524 // API presents top-to-bottom. We must preserve the padding contents. Note
1525 // that the above readPixels did not overwrite the padding.
1526 if (readDst == buffer) {
1527 GrAssert(rowBytes == readDstRowBytes);
1528 scratch.reset(tightRowBytes);
1529 void* tmpRow = scratch.get();
1530 // flip y in-place by rows
reed@google.comac10a2d2010-12-22 21:39:39 +00001531 const int halfY = height >> 1;
1532 char* top = reinterpret_cast<char*>(buffer);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001533 char* bottom = top + (height - 1) * rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001534 for (int y = 0; y < halfY; y++) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001535 memcpy(tmpRow, top, tightRowBytes);
1536 memcpy(top, bottom, tightRowBytes);
1537 memcpy(bottom, tmpRow, tightRowBytes);
1538 top += rowBytes;
1539 bottom -= rowBytes;
1540 }
1541 } else {
1542 GrAssert(readDst != buffer);
1543 // copy from readDst to buffer while flipping y
1544 const int halfY = height >> 1;
1545 const char* src = reinterpret_cast<const char*>(readDst);
1546 char* dst = reinterpret_cast<char*>(buffer) + (height-1) * rowBytes;
1547 for (int y = 0; y < height; y++) {
1548 memcpy(dst, src, tightRowBytes);
1549 src += readDstRowBytes;
1550 dst -= rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001551 }
1552 }
1553 return true;
1554}
1555
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001556void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001557
1558 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1559
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001560 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001561 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001562 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001563 #if GR_COLLECT_STATS
1564 ++fStats.fRenderTargetChngCnt;
1565 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001566 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001567 GrGLenum status;
1568 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001569 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001570 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001571 }
1572 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001573 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001574 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001575 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001576 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001577 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001578 fHWBounds.fViewportRect = vp;
1579 }
1580 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001581 if (NULL == bound || !bound->isEmpty()) {
1582 rt->flagAsNeedingResolve(bound);
1583 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001584}
1585
twiz@google.com0f31ca72011-03-18 17:38:11 +00001586GrGLenum gPrimitiveType2GLMode[] = {
1587 GR_GL_TRIANGLES,
1588 GR_GL_TRIANGLE_STRIP,
1589 GR_GL_TRIANGLE_FAN,
1590 GR_GL_POINTS,
1591 GR_GL_LINES,
1592 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001593};
1594
bsalomon@google.comd302f142011-03-03 13:54:13 +00001595#define SWAP_PER_DRAW 0
1596
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001597#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001598 #if GR_MAC_BUILD
1599 #include <AGL/agl.h>
1600 #elif GR_WIN32_BUILD
1601 void SwapBuf() {
1602 DWORD procID = GetCurrentProcessId();
1603 HWND hwnd = GetTopWindow(GetDesktopWindow());
1604 while(hwnd) {
1605 DWORD wndProcID = 0;
1606 GetWindowThreadProcessId(hwnd, &wndProcID);
1607 if(wndProcID == procID) {
1608 SwapBuffers(GetDC(hwnd));
1609 }
1610 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1611 }
1612 }
1613 #endif
1614#endif
1615
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001616void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1617 uint32_t startVertex,
1618 uint32_t startIndex,
1619 uint32_t vertexCount,
1620 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001621 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1622
twiz@google.com0f31ca72011-03-18 17:38:11 +00001623 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001624
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001625 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1626 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1627
1628 // our setupGeometry better have adjusted this to zero since
1629 // DrawElements always draws from the begining of the arrays for idx 0.
1630 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001631
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001632 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1633 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001634#if SWAP_PER_DRAW
1635 glFlush();
1636 #if GR_MAC_BUILD
1637 aglSwapBuffers(aglGetCurrentContext());
1638 int set_a_break_pt_here = 9;
1639 aglSwapBuffers(aglGetCurrentContext());
1640 #elif GR_WIN32_BUILD
1641 SwapBuf();
1642 int set_a_break_pt_here = 9;
1643 SwapBuf();
1644 #endif
1645#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001646}
1647
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001648void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1649 uint32_t startVertex,
1650 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001651 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1652
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001653 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1654
1655 // our setupGeometry better have adjusted this to zero.
1656 // DrawElements doesn't take an offset so we always adjus the startVertex.
1657 GrAssert(0 == startVertex);
1658
1659 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1660 // account for startVertex in the DrawElements case. So we always
1661 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001662 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001663#if SWAP_PER_DRAW
1664 glFlush();
1665 #if GR_MAC_BUILD
1666 aglSwapBuffers(aglGetCurrentContext());
1667 int set_a_break_pt_here = 9;
1668 aglSwapBuffers(aglGetCurrentContext());
1669 #elif GR_WIN32_BUILD
1670 SwapBuf();
1671 int set_a_break_pt_here = 9;
1672 SwapBuf();
1673 #endif
1674#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001675}
1676
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001677void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001678
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001679 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001680 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001681 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001682 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1683 rt->renderFBOID()));
1684 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1685 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001686 #if GR_COLLECT_STATS
1687 ++fStats.fRenderTargetChngCnt;
1688 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001689 // make sure we go through flushRenderTarget() since we've modified
1690 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001691 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001692 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001693 const GrIRect dirtyRect = rt->getResolveRect();
1694 GrGLIRect r;
1695 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1696 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001697
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001698 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001699 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001700 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1701 GL_CALL(Scissor(r.fLeft, r.fBottom,
1702 r.fWidth, r.fHeight));
1703 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001704 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001705 fHWBounds.fScissorEnabled = true;
1706 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001707 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001708 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001709 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1710 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001711 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001712 int right = r.fLeft + r.fWidth;
1713 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001714 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1715 r.fLeft, r.fBottom, right, top,
1716 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001717 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001718 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001719 }
1720}
1721
twiz@google.com0f31ca72011-03-18 17:38:11 +00001722static const GrGLenum grToGLStencilFunc[] = {
1723 GR_GL_ALWAYS, // kAlways_StencilFunc
1724 GR_GL_NEVER, // kNever_StencilFunc
1725 GR_GL_GREATER, // kGreater_StencilFunc
1726 GR_GL_GEQUAL, // kGEqual_StencilFunc
1727 GR_GL_LESS, // kLess_StencilFunc
1728 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1729 GR_GL_EQUAL, // kEqual_StencilFunc,
1730 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001731};
1732GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1733GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1734GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1735GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1736GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1737GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1738GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1739GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1740GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1741
twiz@google.com0f31ca72011-03-18 17:38:11 +00001742static const GrGLenum grToGLStencilOp[] = {
1743 GR_GL_KEEP, // kKeep_StencilOp
1744 GR_GL_REPLACE, // kReplace_StencilOp
1745 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1746 GR_GL_INCR, // kIncClamp_StencilOp
1747 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1748 GR_GL_DECR, // kDecClamp_StencilOp
1749 GR_GL_ZERO, // kZero_StencilOp
1750 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001751};
1752GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1753GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1754GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1755GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1756GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1757GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1758GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1759GR_STATIC_ASSERT(6 == kZero_StencilOp);
1760GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1761
reed@google.comac10a2d2010-12-22 21:39:39 +00001762void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001763 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001764
1765 // use stencil for clipping if clipping is enabled and the clip
1766 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001767 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001769 bool stencilChange = fHWStencilClip != stencilClip ||
1770 fHWDrawState.fStencilSettings != *settings ||
1771 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1772 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001773
1774 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001775
bsalomon@google.comd302f142011-03-03 13:54:13 +00001776 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1777 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001778
bsalomon@google.comd302f142011-03-03 13:54:13 +00001779 if (settings->isDisabled()) {
1780 if (stencilClip) {
1781 settings = &gClipStencilSettings;
1782 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001784
1785 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001786 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001787 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001788 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001789 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001790 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001791 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1792 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1793 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1794 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1795 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1796 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1797 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1798 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1799 }
1800 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001801 int stencilBits = 0;
1802 GrStencilBuffer* stencilBuffer =
1803 fCurrDrawState.fRenderTarget->getStencilBuffer();
1804 if (NULL != stencilBuffer) {
1805 stencilBits = stencilBuffer->bits();
1806 }
1807 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001808 GrAssert(stencilBits ||
1809 (GrStencilSettings::gDisabled ==
1810 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001811 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1812 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001813
1814 unsigned int frontRef = settings->fFrontFuncRef;
1815 unsigned int frontMask = settings->fFrontFuncMask;
1816 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001817 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001818
1819 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1820
1821 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1822 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1823 } else {
1824 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1825
1826 ConvertStencilFuncAndMask(settings->fFrontFunc,
1827 stencilClip,
1828 clipStencilMask,
1829 userStencilMask,
1830 &frontRef,
1831 &frontMask);
1832 frontWriteMask &= userStencilMask;
1833 }
1834 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001835 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001836 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001837 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001838 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001839 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001840 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001841 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001842 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001843 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001844
1845 unsigned int backRef = settings->fBackFuncRef;
1846 unsigned int backMask = settings->fBackFuncMask;
1847 unsigned int backWriteMask = settings->fBackWriteMask;
1848
1849
1850 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1851 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1852 backFunc = grToGLStencilFunc[settings->fBackFunc];
1853 } else {
1854 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1855 ConvertStencilFuncAndMask(settings->fBackFunc,
1856 stencilClip,
1857 clipStencilMask,
1858 userStencilMask,
1859 &backRef,
1860 &backMask);
1861 backWriteMask &= userStencilMask;
1862 }
1863
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001864 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1865 frontRef, frontMask));
1866 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1867 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1868 backRef, backMask));
1869 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1870 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1871 grToGLStencilOp[settings->fFrontFailOp],
1872 grToGLStencilOp[settings->fFrontPassOp],
1873 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001874
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001875 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1876 grToGLStencilOp[settings->fBackFailOp],
1877 grToGLStencilOp[settings->fBackPassOp],
1878 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001879 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001880 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1881 GL_CALL(StencilMask(frontWriteMask));
1882 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883 grToGLStencilOp[settings->fFrontPassOp],
1884 grToGLStencilOp[settings->fFrontPassOp]));
1885 }
1886 }
1887 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001888 fHWStencilClip = stencilClip;
1889 }
1890}
1891
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001892void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001893 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001894 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1895 // smooth lines.
1896
1897 // we prefer smooth lines over multisampled lines
1898 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001899 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001900 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001901 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001902 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001903 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001904 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001905 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001906 fHWAAState.fSmoothLineEnabled = false;
1907 }
1908 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1909 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001910 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001911 fHWAAState.fMSAAEnabled = false;
1912 }
1913 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001914 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001915 fHWAAState.fMSAAEnabled) {
1916 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001917 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001918 fHWAAState.fMSAAEnabled = false;
1919 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001920 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001921 fHWAAState.fMSAAEnabled = true;
1922 }
1923 }
1924 }
1925}
1926
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001927void GrGpuGL::flushBlend(GrPrimitiveType type,
1928 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001929 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001930 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001931 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001932 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001933 fHWBlendDisabled = false;
1934 }
1935 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1936 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1938 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001939 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1940 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1941 }
1942 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001943 // any optimization to disable blending should
1944 // have already been applied and tweaked the coeffs
1945 // to (1, 0).
1946 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1947 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001948 if (fHWBlendDisabled != blendOff) {
1949 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001950 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001951 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001952 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001953 }
1954 fHWBlendDisabled = blendOff;
1955 }
1956 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001957 if (fHWDrawState.fSrcBlend != srcCoeff ||
1958 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001959 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1960 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001961 fHWDrawState.fSrcBlend = srcCoeff;
1962 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001963 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001964 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1965 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001966 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1967
1968 float c[] = {
1969 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1970 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1971 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1972 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1973 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001974 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001975 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1976 }
1977 }
1978 }
1979}
1980
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001981static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1982 switch (filter) {
1983 case GrSamplerState::kBilinear_Filter:
1984 case GrSamplerState::k4x4Downsample_Filter:
1985 return GR_GL_LINEAR;
1986 case GrSamplerState::kNearest_Filter:
1987 case GrSamplerState::kConvolution_Filter:
1988 return GR_GL_NEAREST;
1989 default:
1990 GrAssert(!"Unknown filter type");
1991 return GR_GL_LINEAR;
1992 }
1993}
1994
bsalomon@google.comffca4002011-02-22 20:34:01 +00001995bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001996
1997 // GrGpu::setupClipAndFlushState should have already checked this
1998 // and bailed if not true.
1999 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002000
tomhudson@google.com93813632011-10-27 20:21:16 +00002001 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002002 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002003 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002004 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002005
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002006 // true for now, but maybe not with GrEffect.
2007 GrAssert(NULL != nextTexture);
2008 // if we created a rt/tex and rendered to it without using a
2009 // texture and now we're texuring from the rt it will still be
2010 // the last bound texture, but it needs resolving. So keep this
2011 // out of the "last != next" check.
2012 GrGLRenderTarget* texRT =
2013 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2014 if (NULL != texRT) {
2015 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002016 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002017
2018 if (fHWDrawState.fTextures[s] != nextTexture) {
2019 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002020 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002021 #if GR_COLLECT_STATS
2022 ++fStats.fTextureChngCnt;
2023 #endif
2024 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2025 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002026 // The texture matrix has to compensate for texture width/height
2027 // and NPOT-embedded-in-POT
2028 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002029 }
2030
2031 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002032 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002033 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002034 nextTexture->getCachedTexParams(&timestamp);
2035 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002036 GrGLTexture::TexParams newTexParams;
2037
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002038 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002039
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002040 const GrGLenum* wraps =
2041 GrGLTexture::WrapMode2GLWrap(this->glBinding());
2042 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2043 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002044 if (setAll) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002045 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002046 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2047 GR_GL_TEXTURE_MAG_FILTER,
2048 newTexParams.fFilter));
2049 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2050 GR_GL_TEXTURE_MIN_FILTER,
2051 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002052 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2053 GR_GL_TEXTURE_WRAP_S,
2054 newTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002055 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2056 GR_GL_TEXTURE_WRAP_T,
2057 newTexParams.fWrapT));
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002058 } else {
2059 if (newTexParams.fFilter != oldTexParams.fFilter) {
2060 setTextureUnit(s);
2061 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2062 GR_GL_TEXTURE_MAG_FILTER,
2063 newTexParams.fFilter));
2064 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2065 GR_GL_TEXTURE_MIN_FILTER,
2066 newTexParams.fFilter));
2067 }
2068 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2069 setTextureUnit(s);
2070 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2071 GR_GL_TEXTURE_WRAP_S,
2072 newTexParams.fWrapS));
2073 }
2074 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2075 setTextureUnit(s);
2076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2077 GR_GL_TEXTURE_WRAP_T,
2078 newTexParams.fWrapT));
2079 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002080 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002081 nextTexture->setCachedTexParams(newTexParams,
2082 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002083 }
2084 }
2085
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002086 GrIRect* rect = NULL;
2087 GrIRect clipBounds;
2088 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2089 fClip.hasConservativeBounds()) {
2090 fClip.getConservativeBounds().roundOut(&clipBounds);
2091 rect = &clipBounds;
2092 }
2093 this->flushRenderTarget(rect);
2094 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002095
reed@google.comac10a2d2010-12-22 21:39:39 +00002096 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2097 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2098 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002099 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002100 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002101 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002102 }
2103 }
2104
bsalomon@google.comd302f142011-03-03 13:54:13 +00002105 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2106 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002107 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002108 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002109 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002110 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002111 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002112 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002113 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002114 }
2115
bsalomon@google.comd302f142011-03-03 13:54:13 +00002116 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2117 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002118 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002119 GL_CALL(Enable(GR_GL_CULL_FACE));
2120 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002121 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002122 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002123 GL_CALL(Enable(GR_GL_CULL_FACE));
2124 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002125 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002126 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002127 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002128 break;
2129 default:
2130 GrCrash("Unknown draw face.");
2131 }
2132 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2133 }
2134
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002135#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002136 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002137 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002138 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002139 NULL == fCurrDrawState.fRenderTarget ||
2140 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002141 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002142 fCurrDrawState.fRenderTarget);
2143 }
2144#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002145
reed@google.comac10a2d2010-12-22 21:39:39 +00002146 flushStencil();
2147
bsalomon@google.comd302f142011-03-03 13:54:13 +00002148 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002149 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002150 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002151}
2152
2153void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002154 if (fHWGeometryState.fVertexBuffer != buffer) {
2155 fHWGeometryState.fArrayPtrsDirty = true;
2156 fHWGeometryState.fVertexBuffer = buffer;
2157 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002158}
2159
2160void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002161 if (fHWGeometryState.fVertexBuffer == buffer) {
2162 // deleting bound buffer does implied bind to 0
2163 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002164 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002165 }
2166}
2167
2168void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002169 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002170}
2171
2172void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002173 if (fHWGeometryState.fIndexBuffer == buffer) {
2174 // deleting bound buffer does implied bind to 0
2175 fHWGeometryState.fIndexBuffer = NULL;
2176 }
2177}
2178
reed@google.comac10a2d2010-12-22 21:39:39 +00002179void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2180 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002181 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002182 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002183 }
2184 if (fHWDrawState.fRenderTarget == renderTarget) {
2185 fHWDrawState.fRenderTarget = NULL;
2186 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002187}
2188
2189void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002190 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002191 if (fCurrDrawState.fTextures[s] == texture) {
2192 fCurrDrawState.fTextures[s] = NULL;
2193 }
2194 if (fHWDrawState.fTextures[s] == texture) {
2195 // deleting bound texture does implied bind to 0
2196 fHWDrawState.fTextures[s] = NULL;
2197 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002198 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002199}
2200
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002201bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002202 GrGLenum* internalFormat,
2203 GrGLenum* format,
2204 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002205 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002206 case kRGBA_8888_GrPixelConfig:
2207 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002208 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002209 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002210 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2211 // format for a BGRA is BGRA not RGBA (as on desktop)
2212 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2213 } else {
2214 *internalFormat = GR_GL_RGBA;
2215 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002216 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002217 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002218 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002219 *format = GR_GL_RGB;
2220 *internalFormat = GR_GL_RGB;
2221 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002222 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002223 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002224 *format = GR_GL_RGBA;
2225 *internalFormat = GR_GL_RGBA;
2226 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002227 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002228 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002229 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002230 *format = GR_GL_PALETTE8_RGBA8;
2231 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002232 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002233 } else {
2234 return false;
2235 }
2236 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002237 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002238 *format = GR_GL_ALPHA;
2239 *internalFormat = GR_GL_ALPHA;
2240 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002241 break;
2242 default:
2243 return false;
2244 }
2245 return true;
2246}
2247
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002248void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002249 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002250 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002251 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002252 fActiveTextureUnitIdx = unit;
2253 }
2254}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002255
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002256void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002257 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002258 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002259 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2260 }
2261}
2262
reed@google.comac10a2d2010-12-22 21:39:39 +00002263/* On ES the internalFormat and format must match for TexImage and we use
2264 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2265 decide the internalFormat. However, on ES internalFormat for
2266 RenderBufferStorage* has to be a specific format (not a base format like
2267 GL_RGBA).
2268 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002269bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002270 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002271 case kRGBA_8888_GrPixelConfig:
2272 case kRGBX_8888_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002273 if (fGLCaps.fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002274 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002275 return true;
2276 } else {
2277 return false;
2278 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002279 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002280 // ES2 supports 565. ES1 supports it
2281 // with FBO extension desktop GL has
2282 // no such internal format
2283 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002284 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002285 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002286 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002287 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 return true;
2289 default:
2290 return false;
2291 }
2292}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002293
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002294void GrGpuGL::resetDirtyFlags() {
2295 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2296}
2297
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002298void GrGpuGL::setBuffers(bool indexed,
2299 int* extraVertexOffset,
2300 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002301
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002302 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002303
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002304 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2305
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002306 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002307 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002308 case kBuffer_GeometrySrcType:
2309 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002310 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002311 break;
2312 case kArray_GeometrySrcType:
2313 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002314 this->finalizeReservedVertices();
2315 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2316 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002317 break;
2318 default:
2319 vbuf = NULL; // suppress warning
2320 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002321 }
2322
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002323 GrAssert(NULL != vbuf);
2324 GrAssert(!vbuf->isLocked());
2325 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002326 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002327 fHWGeometryState.fArrayPtrsDirty = true;
2328 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002329 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002330
2331 if (indexed) {
2332 GrAssert(NULL != extraIndexOffset);
2333
2334 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002335 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002336 case kBuffer_GeometrySrcType:
2337 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002338 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002339 break;
2340 case kArray_GeometrySrcType:
2341 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002342 this->finalizeReservedIndices();
2343 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2344 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002345 break;
2346 default:
2347 ibuf = NULL; // suppress warning
2348 GrCrash("Unknown geometry src type!");
2349 }
2350
2351 GrAssert(NULL != ibuf);
2352 GrAssert(!ibuf->isLocked());
2353 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002354 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002355 fHWGeometryState.fIndexBuffer = ibuf;
2356 }
2357 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002358}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002359
2360int GrGpuGL::getMaxEdges() const {
2361 // FIXME: This is a pessimistic estimate based on how many other things
2362 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002363 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2364 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002365}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002366
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002367void GrGpuGL::GLCaps::print() const {
2368 for (int i = 0; i < fStencilFormats.count(); ++i) {
2369 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2370 i,
2371 fStencilFormats[i].fStencilBits,
2372 fStencilFormats[i].fTotalBits);
2373 }
2374
2375 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2376 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2377 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2378 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2379 static const char* gMSFBOExtStr[] = {
2380 "None",
2381 "ARB",
2382 "EXT",
2383 "Apple",
2384 };
2385 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002386 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002387 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2388 }
2389 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2390 GrPrintf("Support RGBA8 Render Buffer: %s\n",
2391 (fRGBA8Renderbuffer ? "YES": "NO"));
2392}