blob: a3562b25ef0dd961a641164ecaa4f7f00e3c65bf [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()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000398 fGLCaps.fTextureSwizzle = this->glVersion() >= GR_GL_VER(3,3) ||
399 this->hasExtension("GL_ARB_texture_swizzle");
400 } else {
401 fGLCaps.fTextureSwizzle = false;
402 }
403
404 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000405 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
406 // extension includes glMapBuffer.
407 } else {
408 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
409 }
410
411 if (kDesktop_GrGLBinding == this->glBinding()) {
412 if (fGLVersion >= GR_GL_VER(2,0) ||
413 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
414 fCaps.fNPOTTextureTileSupport = true;
415 fCaps.fNPOTTextureSupport = true;
416 } else {
417 fCaps.fNPOTTextureTileSupport = false;
418 fCaps.fNPOTTextureSupport = false;
419 }
420 } else {
421 if (fGLVersion >= GR_GL_VER(2,0)) {
422 fCaps.fNPOTTextureSupport = true;
423 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
424 } else {
425 fCaps.fNPOTTextureSupport =
426 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
427 fCaps.fNPOTTextureTileSupport = false;
428 }
429 }
430
431 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
432
433 ////////////////////////////////////////////////////////////////////////////
434 // Experiments to determine limitations that can't be queried.
435 // TODO: Make these a preprocess that generate some compile time constants.
436 // TODO: probe once at startup, rather than once per context creation.
437
438 int expectNPOTTargets = fGL->fNPOTRenderTargetSupport;
439 if (expectNPOTTargets == kProbe_GrGLCapability) {
440 fCaps.fNPOTRenderTargetSupport =
441 probe_for_npot_render_target_support(fGL, fCaps.fNPOTTextureSupport);
442 } else {
443 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000444 fCaps.fNPOTRenderTargetSupport = (0 != expectNPOTTargets);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000445 }
446
447 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
448 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
449 // Our render targets are always created with textures as the color
450 // attachment, hence this min:
451 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
452
453 fCaps.fMinRenderTargetHeight = fGL->fMinRenderTargetHeight;
454 if (fCaps.fMinRenderTargetHeight == kProbe_GrGLCapability) {
455 fCaps.fMinRenderTargetHeight =
456 probe_for_min_render_target_height(fGL, fCaps.fNPOTRenderTargetSupport,
457 fCaps.fMaxRenderTargetSize);
458 }
459
460 fCaps.fMinRenderTargetWidth = fGL->fMinRenderTargetWidth;
461 if (fCaps.fMinRenderTargetWidth == kProbe_GrGLCapability) {
462 fCaps.fMinRenderTargetWidth =
463 probe_for_min_render_target_width(fGL, fCaps.fNPOTRenderTargetSupport,
464 fCaps.fMaxRenderTargetSize);
465 }
466
467 this->initFSAASupport();
468 this->initStencilFormats();
469}
470
471void GrGpuGL::initFSAASupport() {
472 // TODO: Get rid of GrAALevel and use # samples directly.
473 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
474 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
475 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
476 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
477 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
478
479 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
480 if (kDesktop_GrGLBinding != this->glBinding()) {
481 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
482 // chrome's extension is equivalent to the EXT msaa
483 // and fbo_blit extensions.
484 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
485 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
486 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
487 }
488 } else {
489 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
490 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
491 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
492 this->hasExtension("GL_EXT_framebuffer_blit")) {
493 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
494 }
495 }
496
497 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
498 GrGLint maxSamples;
499 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
500 if (maxSamples > 1 ) {
501 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
502 fGLCaps.fAASamples[kLow_GrAALevel] =
503 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
504 fGLCaps.fAASamples[kMed_GrAALevel] =
505 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
506 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
507 }
508 }
509 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
510}
511
512void GrGpuGL::initStencilFormats() {
513
514 // Build up list of legal stencil formats (though perhaps not supported on
515 // the particular gpu/driver) from most preferred to least.
516
517 // these consts are in order of most preferred to least preferred
518 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
519 static const GrGLStencilBuffer::Format
520 // internal Format stencil bits total bits packed?
521 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
522 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
523 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
524 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
525 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
526 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
527
528 if (kDesktop_GrGLBinding == this->glBinding()) {
529 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
530 this->hasExtension("GL_EXT_packed_depth_stencil") ||
531 this->hasExtension("GL_ARB_framebuffer_object");
532
533 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
534 // require FBO support we can expect these are legal formats and don't
535 // check. These also all support the unsized GL_STENCIL_INDEX.
536 fGLCaps.fStencilFormats.push_back() = gS8;
537 fGLCaps.fStencilFormats.push_back() = gS16;
538 if (supportsPackedDS) {
539 fGLCaps.fStencilFormats.push_back() = gD24S8;
540 }
541 fGLCaps.fStencilFormats.push_back() = gS4;
542 if (supportsPackedDS) {
543 fGLCaps.fStencilFormats.push_back() = gDS;
544 }
545 } else {
546 // ES2 has STENCIL_INDEX8 without extensions.
547 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
548 // introduces tokens for S1 thu S8 but there are separate extensions
549 // that make them legal (GL_OES_stencil1, ...).
550 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
551 // ES doesn't support using the unsized formats.
552
553 if (fGLVersion >= GR_GL_VER(2,0) ||
554 this->hasExtension("GL_OES_stencil8")) {
555 fGLCaps.fStencilFormats.push_back() = gS8;
556 }
557 //fStencilFormats.push_back() = gS16;
558 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
559 fGLCaps.fStencilFormats.push_back() = gD24S8;
560 }
561 if (this->hasExtension("GL_OES_stencil4")) {
562 fGLCaps.fStencilFormats.push_back() = gS4;
563 }
564 // we require some stencil format.
565 GrAssert(fGLCaps.fStencilFormats.count() > 0);
566 }
567}
568
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000569void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000570 if (gPrintStartupSpew && !fPrintedCaps) {
571 fPrintedCaps = true;
572 this->getCaps().print();
573 fGLCaps.print();
574 }
575
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000576 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000577 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000578 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000579
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000580 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(Disable(GR_GL_DEPTH_TEST));
582 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000583
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000584 GL_CALL(Disable(GR_GL_CULL_FACE));
585 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000586 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000587
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000588 GL_CALL(Disable(GR_GL_DITHER));
589 if (kDesktop_GrGLBinding == this->glBinding()) {
590 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
591 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
592 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000593 fHWAAState.fMSAAEnabled = false;
594 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000595 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000596
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000597 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000598 fHWDrawState.fFlagBits = 0;
599
reed@google.comac10a2d2010-12-22 21:39:39 +0000600 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000601 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000602
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000603 // invalid
604 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000607 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
608 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000609
610 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000611 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000612
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000614
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000615 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000616
tomhudson@google.com93813632011-10-27 20:21:16 +0000617 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000618 fHWDrawState.fTextures[s] = NULL;
619 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
620 -GR_ScalarMax,
621 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000622 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000623 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000624 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000625
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000626 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000627 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000628 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000629 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000630
bsalomon@google.comd302f142011-03-03 13:54:13 +0000631 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000632 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000633 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000634
635 fHWGeometryState.fIndexBuffer = NULL;
636 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000637
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000638 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000639
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000640 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000641 fHWDrawState.fRenderTarget = NULL;
642}
643
bsalomon@google.come269f212011-11-07 13:29:52 +0000644GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
645 GrGLenum internalFormat; // we don't need this value
646 GrGLTexture::Desc glTexDesc;
647 if (!this->canBeTexture(desc.fConfig, &internalFormat,
648 &glTexDesc.fUploadFormat, &glTexDesc.fUploadType)) {
649 return NULL;
650 }
651
652 glTexDesc.fContentWidth = glTexDesc.fAllocWidth = desc.fWidth;
653 glTexDesc.fContentHeight = glTexDesc.fAllocHeight = desc.fHeight;
654 glTexDesc.fConfig = desc.fConfig;
655 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
656 glTexDesc.fOwnsID = false;
657 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
658
659 GrGLTexture* texture = NULL;
660 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
661 GrGLRenderTarget::Desc glRTDesc;
662 glRTDesc.fRTFBOID = 0;
663 glRTDesc.fTexFBOID = 0;
664 glRTDesc.fMSColorRenderbufferID = 0;
665 glRTDesc.fOwnIDs = true;
666 glRTDesc.fConfig = desc.fConfig;
667 glRTDesc.fSampleCnt = desc.fSampleCnt;
668 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
669 glTexDesc.fAllocHeight,
670 glTexDesc.fTextureID,
671 &glRTDesc)) {
672 return NULL;
673 }
674 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
675 } else {
676 texture = new GrGLTexture(this, glTexDesc);
677 }
678 if (NULL == texture) {
679 return NULL;
680 }
681
682 this->setSpareTextureUnit();
683 return texture;
684}
685
686GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
687 GrGLRenderTarget::Desc glDesc;
688 glDesc.fConfig = desc.fConfig;
689 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
690 glDesc.fMSColorRenderbufferID = 0;
691 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
692 glDesc.fSampleCnt = desc.fSampleCnt;
693 glDesc.fOwnIDs = false;
694 GrGLIRect viewport;
695 viewport.fLeft = 0;
696 viewport.fBottom = 0;
697 viewport.fWidth = desc.fWidth;
698 viewport.fHeight = desc.fHeight;
699
700 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
701 if (desc.fStencilBits) {
702 GrGLStencilBuffer::Format format;
703 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
704 format.fPacked = false;
705 format.fStencilBits = desc.fStencilBits;
706 format.fTotalBits = desc.fStencilBits;
707 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
708 0,
709 desc.fWidth,
710 desc.fHeight,
711 desc.fSampleCnt,
712 format);
713 tgt->setStencilBuffer(sb);
714 sb->unref();
715 }
716 return tgt;
717}
718
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000719GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
720
721 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
722 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
723 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
724 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
725
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000726 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000727 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000728
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000729 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000730 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000731 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000732 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000733 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000734 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000735 } else {
736 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000737 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000738 }
739 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000740 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000741 }
742 // we don't know what the RB ids are without glGets and we don't care
743 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000744 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000745 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000746 if (desc.fStencilBits) {
747 GrGLStencilBuffer::Format format;
748 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
749 format.fPacked = false;
750 format.fStencilBits = desc.fStencilBits;
751 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000752 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
753 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000754 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000755 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000756 }
757
758 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000759 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000760 GrGLenum dontCare;
761 if (!canBeTexture(desc.fConfig, &dontCare,
762 &texDesc.fUploadFormat,
763 &texDesc.fUploadType)) {
764 return NULL;
765 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000766 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
767 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
768
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000769 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000770 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000771 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000772 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000773
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000774 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000775 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000776 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000777 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000778 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000779 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000780 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000781 } else {
782 GrGLIRect viewport;
783 viewport.fLeft = 0;
784 viewport.fBottom = 0;
785 viewport.fWidth = desc.fWidth;
786 viewport.fHeight = desc.fHeight;
787
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000788 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000789 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000790 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000791 }
792}
793
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000794
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000795////////////////////////////////////////////////////////////////////////////////
796
797void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
798 GrGLenum internalFormat,
799 const void* data,
800 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000801 // we assume the texture is bound
802
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000803 size_t bpp = GrBytesPerPixel(desc.fConfig);
804 size_t trimRowBytes = desc.fContentWidth * bpp;
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000805
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000806 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000807 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000808 }
809
810 // in case we need a temporary, trimmed copy of the src pixels
811 SkAutoSMalloc<128 * 128> tempStorage;
812
813 /*
814 * check whether to allocate a temporary buffer for flipping y or
815 * because our data has extra bytes past each row. If so, we need
816 * to trim those off here, since GL ES doesn't let us specify
817 * GL_UNPACK_ROW_LENGTH.
818 */
819 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000820 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000821 if (data && rowBytes != trimRowBytes) {
822 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
823 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000824 }
825 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000826 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000827 // copy the data into our new storage, skipping the trailing bytes
828 size_t trimSize = desc.fContentHeight * trimRowBytes;
829 const char* src = (const char*)data;
830 if (flipY) {
831 src += (desc.fContentHeight - 1) * rowBytes;
832 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000833 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000834 for (int y = 0; y < desc.fContentHeight; y++) {
835 memcpy(dst, src, trimRowBytes);
836 if (flipY) {
837 src -= rowBytes;
838 } else {
839 src += rowBytes;
840 }
841 dst += trimRowBytes;
842 }
843 // now point data to our trimmed version
844 data = tempStorage.get();
845 rowBytes = trimRowBytes;
846 }
847 }
848
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000849 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000850 if (kIndex_8_GrPixelConfig == desc.fConfig &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000851 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000852 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
853 GrAssert(desc.fContentWidth == desc.fAllocWidth);
854 GrAssert(desc.fContentHeight == desc.fAllocHeight);
855 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
856 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000857 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
858 desc.fAllocWidth, desc.fAllocHeight,
859 0, imageSize, data));
860 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000861 } else {
862 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
863 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000864 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
865 desc.fAllocWidth, desc.fAllocHeight,
866 0, desc.fUploadFormat, desc.fUploadType, NULL));
867 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
868 desc.fContentHeight, desc.fUploadFormat,
869 desc.fUploadType, data));
870 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000871
872 int extraW = desc.fAllocWidth - desc.fContentWidth;
873 int extraH = desc.fAllocHeight - desc.fContentHeight;
874 int maxTexels = extraW * extraH;
875 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
876 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
877
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000878 SkAutoSMalloc<128*128> texels(bpp * maxTexels);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000879
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000880 if (extraH) {
881 uint8_t* lastRowStart = (uint8_t*) data +
882 (desc.fContentHeight - 1) * rowBytes;
883 uint8_t* extraRowStart = (uint8_t*)texels.get();
884
885 for (int i = 0; i < extraH; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000886 memcpy(extraRowStart, lastRowStart, trimRowBytes);
887 extraRowStart += trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000888 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000889 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
890 desc.fContentHeight, desc.fContentWidth,
891 extraH, desc.fUploadFormat,
892 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000893 }
894 if (extraW) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000895 uint8_t* edgeTexel = (uint8_t*)data + trimRowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000896 uint8_t* extraTexel = (uint8_t*)texels.get();
897 for (int j = 0; j < desc.fContentHeight; ++j) {
898 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000899 memcpy(extraTexel, edgeTexel, bpp);
900 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000901 }
902 edgeTexel += rowBytes;
903 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000904 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
905 0, extraW, desc.fContentHeight,
906 desc.fUploadFormat, desc.fUploadType,
907 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000908 }
909 if (extraW && extraH) {
910 uint8_t* cornerTexel = (uint8_t*)data +
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000911 desc.fContentHeight * rowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000912 uint8_t* extraTexel = (uint8_t*)texels.get();
913 for (int i = 0; i < extraW*extraH; ++i) {
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000914 memcpy(extraTexel, cornerTexel, bpp);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000915 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000916 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000917 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
918 desc.fContentHeight, extraW, extraH,
919 desc.fUploadFormat, desc.fUploadType,
920 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000921 }
922
923 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000924 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
925 desc.fAllocWidth, desc.fAllocHeight, 0,
926 desc.fUploadFormat, desc.fUploadType, data));
927 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000928 }
929 }
930}
931
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932bool GrGpuGL::createRenderTargetObjects(int width, int height,
933 GrGLuint texID,
934 GrGLRenderTarget::Desc* desc) {
935 desc->fMSColorRenderbufferID = 0;
936 desc->fRTFBOID = 0;
937 desc->fTexFBOID = 0;
938 desc->fOwnIDs = true;
939
940 GrGLenum status;
941 GrGLint err;
942
bsalomon@google.comab15d612011-08-09 12:57:56 +0000943 GrGLenum msColorFormat = 0; // suppress warning
944
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000945 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000946 if (!desc->fTexFBOID) {
947 goto FAILED;
948 }
949
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950
951 // If we are using multisampling we will create two FBOS. We render
952 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000953 if (desc->fSampleCnt > 0) {
954 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
955 goto FAILED;
956 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000957 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
958 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000959 if (!desc->fRTFBOID ||
960 !desc->fMSColorRenderbufferID ||
961 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
962 goto FAILED;
963 }
964 } else {
965 desc->fRTFBOID = desc->fTexFBOID;
966 }
967
968 if (desc->fRTFBOID != desc->fTexFBOID) {
969 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000970 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000972 GR_GL_CALL_NOERRCHECK(this->glInterface(),
973 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
974 desc->fSampleCnt,
975 msColorFormat,
976 width, height));
977 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000978 if (err != GR_GL_NO_ERROR) {
979 goto FAILED;
980 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000981 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
982 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000983 GR_GL_COLOR_ATTACHMENT0,
984 GR_GL_RENDERBUFFER,
985 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000986 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000987 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
988 goto FAILED;
989 }
990 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000991 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000992
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000993 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
994 GR_GL_COLOR_ATTACHMENT0,
995 GR_GL_TEXTURE_2D,
996 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000997 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000998 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
999 goto FAILED;
1000 }
1001
1002 return true;
1003
1004FAILED:
1005 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001006 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007 }
1008 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001009 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001010 }
1011 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001012 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001013 }
1014 return false;
1015}
1016
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001017// good to set a break-point here to know when createTexture fails
1018static GrTexture* return_null_texture() {
1019// GrAssert(!"null texture");
1020 return NULL;
1021}
1022
1023#if GR_DEBUG
1024static size_t as_size_t(int x) {
1025 return x;
1026}
1027#endif
1028
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001029namespace {
1030void set_tex_swizzle(GrPixelConfig config, const GrGLInterface* gl) {
1031 // Today we always use GL_ALPHA for kAlpha_8_GrPixelConfig. However,
1032 // this format is deprecated sometimes isn't a renderable format. If we
1033 // were to spoof it in the future with GL_RED we'd want to notice that
1034 // here.
1035 // This isn't recorded in our tex params struct becauase we infer it
1036 // from the pixel config.
1037 const GrGLint* swiz;
1038 if (GrPixelConfigIsAlphaOnly(config)) {
1039 static const GrGLint gAlphaSwiz[] = {GR_GL_ALPHA, GR_GL_ALPHA,
1040 GR_GL_ALPHA, GR_GL_ALPHA};
1041 swiz = gAlphaSwiz;
1042 } else {
1043 static const GrGLint gColorSwiz[] = {GR_GL_RED, GR_GL_GREEN,
1044 GR_GL_BLUE, GR_GL_ALPHA};
1045 swiz = gColorSwiz;
1046 }
1047 // should add texparameteri to interface to make 1 instead of 4 calls here
1048 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1049 GR_GL_TEXTURE_SWIZZLE_R,
1050 swiz[0]));
1051 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1052 GR_GL_TEXTURE_SWIZZLE_G,
1053 swiz[1]));
1054 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1055 GR_GL_TEXTURE_SWIZZLE_B,
1056 swiz[2]));
1057 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1058 GR_GL_TEXTURE_SWIZZLE_A,
1059 swiz[3]));
1060}
1061}
1062
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001063GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001064 const void* srcData,
1065 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001066
1067#if GR_COLLECT_STATS
1068 ++fStats.fTextureCreateCnt;
1069#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001070
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001071 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001072 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001073 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001074
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001075 glTexDesc.fContentWidth = desc.fWidth;
1076 glTexDesc.fContentHeight = desc.fHeight;
1077 glTexDesc.fAllocWidth = desc.fWidth;
1078 glTexDesc.fAllocHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001079 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001080 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001081
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001082 glRTDesc.fMSColorRenderbufferID = 0;
1083 glRTDesc.fRTFBOID = 0;
1084 glRTDesc.fTexFBOID = 0;
1085 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001086 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001087
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001088 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001089 if (!canBeTexture(desc.fConfig,
reed@google.comac10a2d2010-12-22 21:39:39 +00001090 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001091 &glTexDesc.fUploadFormat,
1092 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 return return_null_texture();
1094 }
1095
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001096 const Caps& caps = this->getCaps();
1097
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001098 // We keep GrRenderTargets in GL's normal orientation so that they
1099 // can be drawn to by the outside world without the client having
1100 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001101 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001102 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001103
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001104 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1105 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1106 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1107 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 GrPrintf("AA RT requested but not supported on this platform.");
1109 }
1110
reed@google.comac10a2d2010-12-22 21:39:39 +00001111 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001112 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001113 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1114 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001115 }
1116
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001117 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001119 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001120 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001121 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1122 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001123 return return_null_texture();
1124 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001125 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001126 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1127 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001128 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1129 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001130 return return_null_texture();
1131 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001132 }
1133
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001134 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001135 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001136 return return_null_texture();
1137 }
1138
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001139 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001140 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001141
1142 // Some drivers like to know these before seeing glTexImage2D. Some drivers
1143 // have a bug where an FBO won't be complete if it includes a texture that
1144 // is not complete (i.e. has mip levels or non-mip min filter).
1145 static const GrGLTexture::TexParams DEFAULT_TEX_PARAMS = {
1146 GR_GL_NEAREST,
1147 GR_GL_CLAMP_TO_EDGE,
1148 GR_GL_CLAMP_TO_EDGE
1149 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001150 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1151 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.come269f212011-11-07 13:29:52 +00001152 DEFAULT_TEX_PARAMS.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001153 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1154 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.come269f212011-11-07 13:29:52 +00001155 DEFAULT_TEX_PARAMS.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001156 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1157 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.come269f212011-11-07 13:29:52 +00001158 DEFAULT_TEX_PARAMS.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001159 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1160 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.come269f212011-11-07 13:29:52 +00001161 DEFAULT_TEX_PARAMS.fWrapT));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001162 if (fGLCaps.fTextureSwizzle) {
1163 set_tex_swizzle(desc.fConfig, this->glInterface());
1164 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001165 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001166
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001167 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001168 if (renderTarget) {
1169#if GR_COLLECT_STATS
1170 ++fStats.fRenderTargetCreateCnt;
1171#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1173 glTexDesc.fAllocHeight,
1174 glTexDesc.fTextureID,
1175 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001176 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001177 return return_null_texture();
1178 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001179 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001181 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001182 }
bsalomon@google.come269f212011-11-07 13:29:52 +00001183 tex->setCachedTexParams(DEFAULT_TEX_PARAMS, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001184#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001185 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1186 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001187#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 return tex;
1189}
1190
1191namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1193 GrGLuint rb,
1194 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 // we shouldn't ever know one size and not the other
1196 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1197 (kUnknownBitCount == format->fTotalBits));
1198 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1201 (GrGLint*)&format->fStencilBits);
1202 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001203 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1205 (GrGLint*)&format->fTotalBits);
1206 format->fTotalBits += format->fStencilBits;
1207 } else {
1208 format->fTotalBits = format->fStencilBits;
1209 }
1210 }
1211}
1212}
1213
1214bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1215 int width, int height) {
1216
1217 // All internally created RTs are also textures. We don't create
1218 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1219 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001220 GrAssert(width >= rt->allocatedWidth());
1221 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222
1223 int samples = rt->numSamples();
1224 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 if (!sbID) {
1227 return false;
1228 }
1229
1230 GrGLStencilBuffer* sb = NULL;
1231
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001232 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 // we start with the last stencil format that succeeded in hopes
1236 // that we won't go through this loop more than once after the
1237 // first (painful) stencil creation.
1238 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001239 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001240 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 // version on a GL that doesn't have an MSAA extension.
1242 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001243 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1244 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 GR_GL_RENDERBUFFER,
1246 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001247 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248 width,
1249 height));
1250 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001251 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1252 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001253 sFmt.fInternalFormat,
1254 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255 }
1256
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001257 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001258 if (err == GR_GL_NO_ERROR) {
1259 // After sized formats we attempt an unsized format and take whatever
1260 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001261 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001262 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001263 sb = new GrGLStencilBuffer(this, sbID, width, height,
1264 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001265 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1266 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001267 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001269 return true;
1270 }
1271 sb->abandon(); // otherwise we lose sbID
1272 sb->unref();
1273 }
1274 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001275 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001276 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001277}
1278
1279bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1280 GrRenderTarget* rt) {
1281 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1282
1283 GrGLuint fbo = glrt->renderFBOID();
1284
1285 if (NULL == sb) {
1286 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001287 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001288 GR_GL_STENCIL_ATTACHMENT,
1289 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001290 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001291 GR_GL_DEPTH_ATTACHMENT,
1292 GR_GL_RENDERBUFFER, 0));
1293#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001294 GrGLenum status;
1295 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001296 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1297#endif
1298 }
1299 return true;
1300 } else {
1301 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1302 GrGLuint rb = glsb->renderbufferID();
1303
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001305 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1306 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001307 GR_GL_STENCIL_ATTACHMENT,
1308 GR_GL_RENDERBUFFER, rb));
1309 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001310 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001311 GR_GL_DEPTH_ATTACHMENT,
1312 GR_GL_RENDERBUFFER, rb));
1313 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001314 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001315 GR_GL_DEPTH_ATTACHMENT,
1316 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001318
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001319 GrGLenum status;
1320 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001321 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001322 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001323 GR_GL_STENCIL_ATTACHMENT,
1324 GR_GL_RENDERBUFFER, 0));
1325 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001326 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001327 GR_GL_DEPTH_ATTACHMENT,
1328 GR_GL_RENDERBUFFER, 0));
1329 }
1330 return false;
1331 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001332 return true;
1333 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001335}
1336
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001337////////////////////////////////////////////////////////////////////////////////
1338
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001339GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001340 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001341 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001342 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001343 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001344 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001345 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001347 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1348 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1349 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1350 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1351 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 // deleting bound buffer does implicit bind to 0
1353 fHWGeometryState.fVertexBuffer = NULL;
1354 return NULL;
1355 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001356 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 size, dynamic);
1358 fHWGeometryState.fVertexBuffer = vertexBuffer;
1359 return vertexBuffer;
1360 }
1361 return NULL;
1362}
1363
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001364GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001365 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001366 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001367 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001368 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1369 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001371 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1372 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1373 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1374 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1375 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 // deleting bound buffer does implicit bind to 0
1377 fHWGeometryState.fIndexBuffer = NULL;
1378 return NULL;
1379 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001380 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 size, dynamic);
1382 fHWGeometryState.fIndexBuffer = indexBuffer;
1383 return indexBuffer;
1384 }
1385 return NULL;
1386}
1387
reed@google.comac10a2d2010-12-22 21:39:39 +00001388void GrGpuGL::flushScissor(const GrIRect* rect) {
1389 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001390 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001391 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001392
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001393 GrGLIRect scissor;
1394 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001395 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001396 rect->width(), rect->height());
1397 if (scissor.contains(vp)) {
1398 rect = NULL;
1399 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001400 }
1401
1402 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001403 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001404 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001405 fHWBounds.fScissorRect = scissor;
1406 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001407 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001408 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001409 fHWBounds.fScissorEnabled = true;
1410 }
1411 } else {
1412 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001413 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001414 fHWBounds.fScissorEnabled = false;
1415 }
1416 }
1417}
1418
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001419void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001420 if (NULL == fCurrDrawState.fRenderTarget) {
1421 return;
1422 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001423 GrIRect r;
1424 if (NULL != rect) {
1425 // flushScissor expects rect to be clipped to the target.
1426 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001427 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1428 fCurrDrawState.fRenderTarget->height());
1429 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001430 rect = &r;
1431 } else {
1432 return;
1433 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001434 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001435 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001436 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001437 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001438 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001439 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1440 GrColorUnpackG(color)/255.f,
1441 GrColorUnpackB(color)/255.f,
1442 GrColorUnpackA(color)/255.f));
1443 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001444}
1445
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001446void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001447 if (NULL == fCurrDrawState.fRenderTarget) {
1448 return;
1449 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001450
1451 this->flushRenderTarget(&GrIRect::EmptyIRect());
1452
reed@google.comac10a2d2010-12-22 21:39:39 +00001453 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001454 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001455 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001456 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001457 GL_CALL(StencilMask(0xffffffff));
1458 GL_CALL(ClearStencil(0));
1459 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001460 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001461}
1462
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001463void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001464 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001465
1466 // this should only be called internally when we know we have a
1467 // stencil buffer.
1468 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001469 GrGLint stencilBitCount =
1470 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001471#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001472 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001473 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001474#else
1475 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001476 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001477 // turned into draws. Our contract on GrDrawTarget says that
1478 // changing the clip between stencil passes may or may not
1479 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001480 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001481#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001482 GrGLint value;
1483 if (insideClip) {
1484 value = (1 << (stencilBitCount - 1));
1485 } else {
1486 value = 0;
1487 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001488 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001489 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001490 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001491 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001492 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001493 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001494}
1495
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001496void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001497 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001498}
1499
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001500bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001501 int left, int top,
1502 int width, int height,
1503 GrPixelConfig config,
1504 void* buffer, size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001505 GrGLenum internalFormat; // we don't use this for glReadPixels
1506 GrGLenum format;
1507 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1509 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 }
1511
1512 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001513 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1514 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1515 switch (tgt->getResolveType()) {
1516 case GrGLRenderTarget::kCantResolve_ResolveType:
1517 return false;
1518 case GrGLRenderTarget::kAutoResolves_ResolveType:
1519 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1520 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001521 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001522 break;
1523 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001524 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001525 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001526 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1527 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001528 break;
1529 default:
1530 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001531 }
1532
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001533 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001534
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001535 // the read rect is viewport-relative
1536 GrGLIRect readRect;
1537 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001538
1539 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1540 if (0 == rowBytes) {
1541 rowBytes = tightRowBytes;
1542 }
1543 size_t readDstRowBytes = tightRowBytes;
1544 void* readDst = buffer;
1545
1546 // determine if GL can read using the passed rowBytes or if we need
1547 // a scratch buffer.
1548 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1549 if (rowBytes != tightRowBytes) {
1550 if (kDesktop_GrGLBinding == this->glBinding()) {
1551 GrAssert(!(rowBytes % sizeof(GrColor)));
1552 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1553 readDstRowBytes = rowBytes;
1554 } else {
1555 scratch.reset(tightRowBytes * height);
1556 readDst = scratch.get();
1557 }
1558 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001559 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1560 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001561 format, type, readDst));
1562 if (readDstRowBytes != tightRowBytes) {
1563 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1564 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001565
1566 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001567 // API presents top-to-bottom. We must preserve the padding contents. Note
1568 // that the above readPixels did not overwrite the padding.
1569 if (readDst == buffer) {
1570 GrAssert(rowBytes == readDstRowBytes);
1571 scratch.reset(tightRowBytes);
1572 void* tmpRow = scratch.get();
1573 // flip y in-place by rows
reed@google.comac10a2d2010-12-22 21:39:39 +00001574 const int halfY = height >> 1;
1575 char* top = reinterpret_cast<char*>(buffer);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001576 char* bottom = top + (height - 1) * rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001577 for (int y = 0; y < halfY; y++) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001578 memcpy(tmpRow, top, tightRowBytes);
1579 memcpy(top, bottom, tightRowBytes);
1580 memcpy(bottom, tmpRow, tightRowBytes);
1581 top += rowBytes;
1582 bottom -= rowBytes;
1583 }
1584 } else {
1585 GrAssert(readDst != buffer);
1586 // copy from readDst to buffer while flipping y
1587 const int halfY = height >> 1;
1588 const char* src = reinterpret_cast<const char*>(readDst);
1589 char* dst = reinterpret_cast<char*>(buffer) + (height-1) * rowBytes;
1590 for (int y = 0; y < height; y++) {
1591 memcpy(dst, src, tightRowBytes);
1592 src += readDstRowBytes;
1593 dst -= rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001594 }
1595 }
1596 return true;
1597}
1598
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001599void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001600
1601 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1602
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001603 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001605 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001606 #if GR_COLLECT_STATS
1607 ++fStats.fRenderTargetChngCnt;
1608 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001610 GrGLenum status;
1611 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001612 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001613 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 }
1615 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001616 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001617 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001618 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001619 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001620 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001621 fHWBounds.fViewportRect = vp;
1622 }
1623 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001624 if (NULL == bound || !bound->isEmpty()) {
1625 rt->flagAsNeedingResolve(bound);
1626 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001627}
1628
twiz@google.com0f31ca72011-03-18 17:38:11 +00001629GrGLenum gPrimitiveType2GLMode[] = {
1630 GR_GL_TRIANGLES,
1631 GR_GL_TRIANGLE_STRIP,
1632 GR_GL_TRIANGLE_FAN,
1633 GR_GL_POINTS,
1634 GR_GL_LINES,
1635 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001636};
1637
bsalomon@google.comd302f142011-03-03 13:54:13 +00001638#define SWAP_PER_DRAW 0
1639
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001640#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001641 #if GR_MAC_BUILD
1642 #include <AGL/agl.h>
1643 #elif GR_WIN32_BUILD
1644 void SwapBuf() {
1645 DWORD procID = GetCurrentProcessId();
1646 HWND hwnd = GetTopWindow(GetDesktopWindow());
1647 while(hwnd) {
1648 DWORD wndProcID = 0;
1649 GetWindowThreadProcessId(hwnd, &wndProcID);
1650 if(wndProcID == procID) {
1651 SwapBuffers(GetDC(hwnd));
1652 }
1653 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1654 }
1655 }
1656 #endif
1657#endif
1658
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001659void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1660 uint32_t startVertex,
1661 uint32_t startIndex,
1662 uint32_t vertexCount,
1663 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001664 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1665
twiz@google.com0f31ca72011-03-18 17:38:11 +00001666 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001667
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001668 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1669 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1670
1671 // our setupGeometry better have adjusted this to zero since
1672 // DrawElements always draws from the begining of the arrays for idx 0.
1673 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001674
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001675 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1676 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001677#if SWAP_PER_DRAW
1678 glFlush();
1679 #if GR_MAC_BUILD
1680 aglSwapBuffers(aglGetCurrentContext());
1681 int set_a_break_pt_here = 9;
1682 aglSwapBuffers(aglGetCurrentContext());
1683 #elif GR_WIN32_BUILD
1684 SwapBuf();
1685 int set_a_break_pt_here = 9;
1686 SwapBuf();
1687 #endif
1688#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001689}
1690
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001691void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1692 uint32_t startVertex,
1693 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001694 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1695
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001696 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1697
1698 // our setupGeometry better have adjusted this to zero.
1699 // DrawElements doesn't take an offset so we always adjus the startVertex.
1700 GrAssert(0 == startVertex);
1701
1702 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1703 // account for startVertex in the DrawElements case. So we always
1704 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001705 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001706#if SWAP_PER_DRAW
1707 glFlush();
1708 #if GR_MAC_BUILD
1709 aglSwapBuffers(aglGetCurrentContext());
1710 int set_a_break_pt_here = 9;
1711 aglSwapBuffers(aglGetCurrentContext());
1712 #elif GR_WIN32_BUILD
1713 SwapBuf();
1714 int set_a_break_pt_here = 9;
1715 SwapBuf();
1716 #endif
1717#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001718}
1719
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001720void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001721
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001722 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001723 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001724 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001725 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1726 rt->renderFBOID()));
1727 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1728 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001729 #if GR_COLLECT_STATS
1730 ++fStats.fRenderTargetChngCnt;
1731 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001732 // make sure we go through flushRenderTarget() since we've modified
1733 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001735 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001736 const GrIRect dirtyRect = rt->getResolveRect();
1737 GrGLIRect r;
1738 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1739 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001740
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001741 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001742 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001743 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1744 GL_CALL(Scissor(r.fLeft, r.fBottom,
1745 r.fWidth, r.fHeight));
1746 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001747 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001748 fHWBounds.fScissorEnabled = true;
1749 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001750 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001751 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001752 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1753 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001754 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001755 int right = r.fLeft + r.fWidth;
1756 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001757 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1758 r.fLeft, r.fBottom, right, top,
1759 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001760 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001761 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 }
1763}
1764
twiz@google.com0f31ca72011-03-18 17:38:11 +00001765static const GrGLenum grToGLStencilFunc[] = {
1766 GR_GL_ALWAYS, // kAlways_StencilFunc
1767 GR_GL_NEVER, // kNever_StencilFunc
1768 GR_GL_GREATER, // kGreater_StencilFunc
1769 GR_GL_GEQUAL, // kGEqual_StencilFunc
1770 GR_GL_LESS, // kLess_StencilFunc
1771 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1772 GR_GL_EQUAL, // kEqual_StencilFunc,
1773 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001774};
1775GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1776GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1777GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1778GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1779GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1780GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1781GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1782GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1783GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1784
twiz@google.com0f31ca72011-03-18 17:38:11 +00001785static const GrGLenum grToGLStencilOp[] = {
1786 GR_GL_KEEP, // kKeep_StencilOp
1787 GR_GL_REPLACE, // kReplace_StencilOp
1788 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1789 GR_GL_INCR, // kIncClamp_StencilOp
1790 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1791 GR_GL_DECR, // kDecClamp_StencilOp
1792 GR_GL_ZERO, // kZero_StencilOp
1793 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001794};
1795GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1796GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1797GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1798GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1799GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1800GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1801GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1802GR_STATIC_ASSERT(6 == kZero_StencilOp);
1803GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1804
reed@google.comac10a2d2010-12-22 21:39:39 +00001805void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001806 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001807
1808 // use stencil for clipping if clipping is enabled and the clip
1809 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001810 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001811 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001812 bool stencilChange = fHWStencilClip != stencilClip ||
1813 fHWDrawState.fStencilSettings != *settings ||
1814 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1815 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001816
1817 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001818
bsalomon@google.comd302f142011-03-03 13:54:13 +00001819 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1820 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001821
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822 if (settings->isDisabled()) {
1823 if (stencilClip) {
1824 settings = &gClipStencilSettings;
1825 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001826 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001827
1828 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001829 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001831 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001832 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001833 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001834 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1835 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1836 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1837 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1838 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1839 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1840 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1841 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1842 }
1843 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001844 int stencilBits = 0;
1845 GrStencilBuffer* stencilBuffer =
1846 fCurrDrawState.fRenderTarget->getStencilBuffer();
1847 if (NULL != stencilBuffer) {
1848 stencilBits = stencilBuffer->bits();
1849 }
1850 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001851 GrAssert(stencilBits ||
1852 (GrStencilSettings::gDisabled ==
1853 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001854 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1855 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001856
1857 unsigned int frontRef = settings->fFrontFuncRef;
1858 unsigned int frontMask = settings->fFrontFuncMask;
1859 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001860 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861
1862 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1863
1864 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1865 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1866 } else {
1867 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1868
1869 ConvertStencilFuncAndMask(settings->fFrontFunc,
1870 stencilClip,
1871 clipStencilMask,
1872 userStencilMask,
1873 &frontRef,
1874 &frontMask);
1875 frontWriteMask &= userStencilMask;
1876 }
1877 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001878 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001879 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001880 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001881 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001882 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001884 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001885 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001886 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887
1888 unsigned int backRef = settings->fBackFuncRef;
1889 unsigned int backMask = settings->fBackFuncMask;
1890 unsigned int backWriteMask = settings->fBackWriteMask;
1891
1892
1893 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1894 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1895 backFunc = grToGLStencilFunc[settings->fBackFunc];
1896 } else {
1897 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1898 ConvertStencilFuncAndMask(settings->fBackFunc,
1899 stencilClip,
1900 clipStencilMask,
1901 userStencilMask,
1902 &backRef,
1903 &backMask);
1904 backWriteMask &= userStencilMask;
1905 }
1906
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001907 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1908 frontRef, frontMask));
1909 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1910 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1911 backRef, backMask));
1912 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1913 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1914 grToGLStencilOp[settings->fFrontFailOp],
1915 grToGLStencilOp[settings->fFrontPassOp],
1916 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001917
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001918 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1919 grToGLStencilOp[settings->fBackFailOp],
1920 grToGLStencilOp[settings->fBackPassOp],
1921 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001923 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1924 GL_CALL(StencilMask(frontWriteMask));
1925 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001926 grToGLStencilOp[settings->fFrontPassOp],
1927 grToGLStencilOp[settings->fFrontPassOp]));
1928 }
1929 }
1930 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001931 fHWStencilClip = stencilClip;
1932 }
1933}
1934
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001935void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001936 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001937 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1938 // smooth lines.
1939
1940 // we prefer smooth lines over multisampled lines
1941 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001942 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001943 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001944 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001945 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001946 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001947 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001948 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001949 fHWAAState.fSmoothLineEnabled = false;
1950 }
1951 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1952 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001953 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001954 fHWAAState.fMSAAEnabled = false;
1955 }
1956 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001957 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001958 fHWAAState.fMSAAEnabled) {
1959 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001960 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001961 fHWAAState.fMSAAEnabled = false;
1962 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001963 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001964 fHWAAState.fMSAAEnabled = true;
1965 }
1966 }
1967 }
1968}
1969
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001970void GrGpuGL::flushBlend(GrPrimitiveType type,
1971 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001972 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001973 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001974 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001975 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001976 fHWBlendDisabled = false;
1977 }
1978 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1979 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001980 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1981 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001982 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1983 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1984 }
1985 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001986 // any optimization to disable blending should
1987 // have already been applied and tweaked the coeffs
1988 // to (1, 0).
1989 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1990 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001991 if (fHWBlendDisabled != blendOff) {
1992 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001993 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001994 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001995 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001996 }
1997 fHWBlendDisabled = blendOff;
1998 }
1999 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002000 if (fHWDrawState.fSrcBlend != srcCoeff ||
2001 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002002 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2003 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002004 fHWDrawState.fSrcBlend = srcCoeff;
2005 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002006 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002007 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2008 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002009 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2010
2011 float c[] = {
2012 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2013 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2014 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2015 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2016 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002017 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002018 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2019 }
2020 }
2021 }
2022}
2023
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002024static unsigned grToGLFilter(GrSamplerState::Filter filter) {
2025 switch (filter) {
2026 case GrSamplerState::kBilinear_Filter:
2027 case GrSamplerState::k4x4Downsample_Filter:
2028 return GR_GL_LINEAR;
2029 case GrSamplerState::kNearest_Filter:
2030 case GrSamplerState::kConvolution_Filter:
2031 return GR_GL_NEAREST;
2032 default:
2033 GrAssert(!"Unknown filter type");
2034 return GR_GL_LINEAR;
2035 }
2036}
2037
bsalomon@google.comffca4002011-02-22 20:34:01 +00002038bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002039
2040 // GrGpu::setupClipAndFlushState should have already checked this
2041 // and bailed if not true.
2042 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002043
tomhudson@google.com93813632011-10-27 20:21:16 +00002044 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002045 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002046 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002047 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002048
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002049 // true for now, but maybe not with GrEffect.
2050 GrAssert(NULL != nextTexture);
2051 // if we created a rt/tex and rendered to it without using a
2052 // texture and now we're texuring from the rt it will still be
2053 // the last bound texture, but it needs resolving. So keep this
2054 // out of the "last != next" check.
2055 GrGLRenderTarget* texRT =
2056 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2057 if (NULL != texRT) {
2058 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002059 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002060
2061 if (fHWDrawState.fTextures[s] != nextTexture) {
2062 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002063 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002064 #if GR_COLLECT_STATS
2065 ++fStats.fTextureChngCnt;
2066 #endif
2067 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2068 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002069 // The texture matrix has to compensate for texture width/height
2070 // and NPOT-embedded-in-POT
2071 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002072 }
2073
2074 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002075 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002076 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002077 nextTexture->getCachedTexParams(&timestamp);
2078 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002079 GrGLTexture::TexParams newTexParams;
2080
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002081 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002082
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002083 const GrGLenum* wraps =
2084 GrGLTexture::WrapMode2GLWrap(this->glBinding());
2085 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2086 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002087 if (setAll) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002088 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002089 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2090 GR_GL_TEXTURE_MAG_FILTER,
2091 newTexParams.fFilter));
2092 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2093 GR_GL_TEXTURE_MIN_FILTER,
2094 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002095 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2096 GR_GL_TEXTURE_WRAP_S,
2097 newTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002098 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2099 GR_GL_TEXTURE_WRAP_T,
2100 newTexParams.fWrapT));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002101 if (this->glCaps().fTextureSwizzle) {
2102 set_tex_swizzle(nextTexture->config(), this->glInterface());
2103 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002104 } else {
2105 if (newTexParams.fFilter != oldTexParams.fFilter) {
2106 setTextureUnit(s);
2107 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2108 GR_GL_TEXTURE_MAG_FILTER,
2109 newTexParams.fFilter));
2110 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2111 GR_GL_TEXTURE_MIN_FILTER,
2112 newTexParams.fFilter));
2113 }
2114 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2115 setTextureUnit(s);
2116 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2117 GR_GL_TEXTURE_WRAP_S,
2118 newTexParams.fWrapS));
2119 }
2120 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2121 setTextureUnit(s);
2122 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2123 GR_GL_TEXTURE_WRAP_T,
2124 newTexParams.fWrapT));
2125 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002126 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002127 nextTexture->setCachedTexParams(newTexParams,
2128 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002129 }
2130 }
2131
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002132 GrIRect* rect = NULL;
2133 GrIRect clipBounds;
2134 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2135 fClip.hasConservativeBounds()) {
2136 fClip.getConservativeBounds().roundOut(&clipBounds);
2137 rect = &clipBounds;
2138 }
2139 this->flushRenderTarget(rect);
2140 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002141
reed@google.comac10a2d2010-12-22 21:39:39 +00002142 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2143 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2144 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002145 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002146 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002147 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002148 }
2149 }
2150
bsalomon@google.comd302f142011-03-03 13:54:13 +00002151 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2152 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002153 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002154 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002155 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002156 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002157 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002158 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002159 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002160 }
2161
bsalomon@google.comd302f142011-03-03 13:54:13 +00002162 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2163 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002164 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(Enable(GR_GL_CULL_FACE));
2166 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002167 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002168 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002169 GL_CALL(Enable(GR_GL_CULL_FACE));
2170 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002171 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002172 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002173 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002174 break;
2175 default:
2176 GrCrash("Unknown draw face.");
2177 }
2178 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2179 }
2180
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002181#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002182 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002183 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002184 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002185 NULL == fCurrDrawState.fRenderTarget ||
2186 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002187 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002188 fCurrDrawState.fRenderTarget);
2189 }
2190#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002191
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 flushStencil();
2193
bsalomon@google.comd302f142011-03-03 13:54:13 +00002194 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002195 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002196 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002197}
2198
2199void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002200 if (fHWGeometryState.fVertexBuffer != buffer) {
2201 fHWGeometryState.fArrayPtrsDirty = true;
2202 fHWGeometryState.fVertexBuffer = buffer;
2203 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002204}
2205
2206void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002207 if (fHWGeometryState.fVertexBuffer == buffer) {
2208 // deleting bound buffer does implied bind to 0
2209 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002210 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002211 }
2212}
2213
2214void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002215 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002216}
2217
2218void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002219 if (fHWGeometryState.fIndexBuffer == buffer) {
2220 // deleting bound buffer does implied bind to 0
2221 fHWGeometryState.fIndexBuffer = NULL;
2222 }
2223}
2224
reed@google.comac10a2d2010-12-22 21:39:39 +00002225void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2226 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002227 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002228 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002229 }
2230 if (fHWDrawState.fRenderTarget == renderTarget) {
2231 fHWDrawState.fRenderTarget = NULL;
2232 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002233}
2234
2235void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002236 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002237 if (fCurrDrawState.fTextures[s] == texture) {
2238 fCurrDrawState.fTextures[s] = NULL;
2239 }
2240 if (fHWDrawState.fTextures[s] == texture) {
2241 // deleting bound texture does implied bind to 0
2242 fHWDrawState.fTextures[s] = NULL;
2243 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002244 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002245}
2246
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002247bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002248 GrGLenum* internalFormat,
2249 GrGLenum* format,
2250 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002252 case kRGBA_8888_GrPixelConfig:
2253 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002254 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002255 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002256 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2257 // format for a BGRA is BGRA not RGBA (as on desktop)
2258 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2259 } else {
2260 *internalFormat = GR_GL_RGBA;
2261 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002262 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002263 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002264 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002265 *format = GR_GL_RGB;
2266 *internalFormat = GR_GL_RGB;
2267 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002268 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002269 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002270 *format = GR_GL_RGBA;
2271 *internalFormat = GR_GL_RGBA;
2272 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002273 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002274 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002275 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002276 *format = GR_GL_PALETTE8_RGBA8;
2277 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002278 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002279 } else {
2280 return false;
2281 }
2282 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002283 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002284 *format = GR_GL_ALPHA;
2285 *internalFormat = GR_GL_ALPHA;
2286 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002287 break;
2288 default:
2289 return false;
2290 }
2291 return true;
2292}
2293
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002294void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002295 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002296 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002297 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002298 fActiveTextureUnitIdx = unit;
2299 }
2300}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002301
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002302void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002303 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002304 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002305 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2306 }
2307}
2308
reed@google.comac10a2d2010-12-22 21:39:39 +00002309/* On ES the internalFormat and format must match for TexImage and we use
2310 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2311 decide the internalFormat. However, on ES internalFormat for
2312 RenderBufferStorage* has to be a specific format (not a base format like
2313 GL_RGBA).
2314 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002315bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002316 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002317 case kRGBA_8888_GrPixelConfig:
2318 case kRGBX_8888_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002319 if (fGLCaps.fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002320 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002321 return true;
2322 } else {
2323 return false;
2324 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002325 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002326 // ES2 supports 565. ES1 supports it
2327 // with FBO extension desktop GL has
2328 // no such internal format
2329 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002330 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002331 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002332 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002333 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002334 return true;
2335 default:
2336 return false;
2337 }
2338}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002339
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002340void GrGpuGL::resetDirtyFlags() {
2341 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2342}
2343
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002344void GrGpuGL::setBuffers(bool indexed,
2345 int* extraVertexOffset,
2346 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002347
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002348 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002349
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002350 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2351
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002352 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002353 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002354 case kBuffer_GeometrySrcType:
2355 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002356 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002357 break;
2358 case kArray_GeometrySrcType:
2359 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002360 this->finalizeReservedVertices();
2361 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2362 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002363 break;
2364 default:
2365 vbuf = NULL; // suppress warning
2366 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002367 }
2368
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002369 GrAssert(NULL != vbuf);
2370 GrAssert(!vbuf->isLocked());
2371 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002372 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002373 fHWGeometryState.fArrayPtrsDirty = true;
2374 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002375 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002376
2377 if (indexed) {
2378 GrAssert(NULL != extraIndexOffset);
2379
2380 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002381 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002382 case kBuffer_GeometrySrcType:
2383 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002384 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002385 break;
2386 case kArray_GeometrySrcType:
2387 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002388 this->finalizeReservedIndices();
2389 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2390 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002391 break;
2392 default:
2393 ibuf = NULL; // suppress warning
2394 GrCrash("Unknown geometry src type!");
2395 }
2396
2397 GrAssert(NULL != ibuf);
2398 GrAssert(!ibuf->isLocked());
2399 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002400 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002401 fHWGeometryState.fIndexBuffer = ibuf;
2402 }
2403 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002404}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002405
2406int GrGpuGL::getMaxEdges() const {
2407 // FIXME: This is a pessimistic estimate based on how many other things
2408 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002409 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2410 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002411}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002412
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002413void GrGpuGL::GLCaps::print() const {
2414 for (int i = 0; i < fStencilFormats.count(); ++i) {
2415 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2416 i,
2417 fStencilFormats[i].fStencilBits,
2418 fStencilFormats[i].fTotalBits);
2419 }
2420
2421 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2422 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2423 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2424 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2425 static const char* gMSFBOExtStr[] = {
2426 "None",
2427 "ARB",
2428 "EXT",
2429 "Apple",
2430 };
2431 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002432 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002433 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2434 }
2435 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2436 GrPrintf("Support RGBA8 Render Buffer: %s\n",
2437 (fRGBA8Renderbuffer ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002438 GrPrintf("Support texture swizzle: %s\n",
2439 (fTextureSwizzle ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002440}