blob: 774b8d955362aaeb95f9321b08472255c178b955 [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()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000385 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000386 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000387 fGLCaps.fRGBA8RenderbufferSupport =
388 this->hasExtension("GL_OES_rgb8_rgba8") ||
389 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000390 }
391
392
bsalomon@google.comc4364992011-11-07 15:54:49 +0000393 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000394 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
395 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000396 } else {
397 bool hasBGRAExt = false;
398 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000399 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000400 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000401 fGLCaps.fBGRAFormatSupport = true;
402 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000403 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000404 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000405 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000406 }
407
408 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000409 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000410 this->hasExtension("GL_ARB_texture_swizzle");
411 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000412 fGLCaps.fTextureSwizzleSupport = false;
413 }
414
415 if (kDesktop_GrGLBinding == this->glBinding()) {
416 fGLCaps.fUnpackRowLengthSupport = true;
417 fGLCaps.fPackRowLengthSupport = true;
418 } else {
419 fGLCaps.fUnpackRowLengthSupport = this->hasExtension("GL_EXT_unpack_subimage");
420 // no extension for pack row length
421 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000422 }
423
424 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000425 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
426 // extension includes glMapBuffer.
427 } else {
428 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
429 }
430
431 if (kDesktop_GrGLBinding == this->glBinding()) {
432 if (fGLVersion >= GR_GL_VER(2,0) ||
433 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
434 fCaps.fNPOTTextureTileSupport = true;
435 fCaps.fNPOTTextureSupport = true;
436 } else {
437 fCaps.fNPOTTextureTileSupport = false;
438 fCaps.fNPOTTextureSupport = false;
439 }
440 } else {
441 if (fGLVersion >= GR_GL_VER(2,0)) {
442 fCaps.fNPOTTextureSupport = true;
443 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
444 } else {
445 fCaps.fNPOTTextureSupport =
446 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
447 fCaps.fNPOTTextureTileSupport = false;
448 }
449 }
450
451 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
452
453 ////////////////////////////////////////////////////////////////////////////
454 // Experiments to determine limitations that can't be queried.
455 // TODO: Make these a preprocess that generate some compile time constants.
456 // TODO: probe once at startup, rather than once per context creation.
457
458 int expectNPOTTargets = fGL->fNPOTRenderTargetSupport;
459 if (expectNPOTTargets == kProbe_GrGLCapability) {
460 fCaps.fNPOTRenderTargetSupport =
461 probe_for_npot_render_target_support(fGL, fCaps.fNPOTTextureSupport);
462 } else {
463 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000464 fCaps.fNPOTRenderTargetSupport = (0 != expectNPOTTargets);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000465 }
466
467 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
468 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
469 // Our render targets are always created with textures as the color
470 // attachment, hence this min:
471 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
472
473 fCaps.fMinRenderTargetHeight = fGL->fMinRenderTargetHeight;
474 if (fCaps.fMinRenderTargetHeight == kProbe_GrGLCapability) {
475 fCaps.fMinRenderTargetHeight =
476 probe_for_min_render_target_height(fGL, fCaps.fNPOTRenderTargetSupport,
477 fCaps.fMaxRenderTargetSize);
478 }
479
480 fCaps.fMinRenderTargetWidth = fGL->fMinRenderTargetWidth;
481 if (fCaps.fMinRenderTargetWidth == kProbe_GrGLCapability) {
482 fCaps.fMinRenderTargetWidth =
483 probe_for_min_render_target_width(fGL, fCaps.fNPOTRenderTargetSupport,
484 fCaps.fMaxRenderTargetSize);
485 }
486
487 this->initFSAASupport();
488 this->initStencilFormats();
489}
490
491void GrGpuGL::initFSAASupport() {
492 // TODO: Get rid of GrAALevel and use # samples directly.
493 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
494 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
495 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
496 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
497 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
498
499 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
500 if (kDesktop_GrGLBinding != this->glBinding()) {
501 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
502 // chrome's extension is equivalent to the EXT msaa
503 // and fbo_blit extensions.
504 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
505 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
506 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
507 }
508 } else {
509 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
510 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
511 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
512 this->hasExtension("GL_EXT_framebuffer_blit")) {
513 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
514 }
515 }
516
517 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
518 GrGLint maxSamples;
519 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
520 if (maxSamples > 1 ) {
521 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
522 fGLCaps.fAASamples[kLow_GrAALevel] =
523 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
524 fGLCaps.fAASamples[kMed_GrAALevel] =
525 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
526 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
527 }
528 }
529 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
530}
531
532void GrGpuGL::initStencilFormats() {
533
534 // Build up list of legal stencil formats (though perhaps not supported on
535 // the particular gpu/driver) from most preferred to least.
536
537 // these consts are in order of most preferred to least preferred
538 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
539 static const GrGLStencilBuffer::Format
540 // internal Format stencil bits total bits packed?
541 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
542 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
543 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
544 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
545 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
546 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
547
548 if (kDesktop_GrGLBinding == this->glBinding()) {
549 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
550 this->hasExtension("GL_EXT_packed_depth_stencil") ||
551 this->hasExtension("GL_ARB_framebuffer_object");
552
553 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
554 // require FBO support we can expect these are legal formats and don't
555 // check. These also all support the unsized GL_STENCIL_INDEX.
556 fGLCaps.fStencilFormats.push_back() = gS8;
557 fGLCaps.fStencilFormats.push_back() = gS16;
558 if (supportsPackedDS) {
559 fGLCaps.fStencilFormats.push_back() = gD24S8;
560 }
561 fGLCaps.fStencilFormats.push_back() = gS4;
562 if (supportsPackedDS) {
563 fGLCaps.fStencilFormats.push_back() = gDS;
564 }
565 } else {
566 // ES2 has STENCIL_INDEX8 without extensions.
567 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
568 // introduces tokens for S1 thu S8 but there are separate extensions
569 // that make them legal (GL_OES_stencil1, ...).
570 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
571 // ES doesn't support using the unsized formats.
572
573 if (fGLVersion >= GR_GL_VER(2,0) ||
574 this->hasExtension("GL_OES_stencil8")) {
575 fGLCaps.fStencilFormats.push_back() = gS8;
576 }
577 //fStencilFormats.push_back() = gS16;
578 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
579 fGLCaps.fStencilFormats.push_back() = gD24S8;
580 }
581 if (this->hasExtension("GL_OES_stencil4")) {
582 fGLCaps.fStencilFormats.push_back() = gS4;
583 }
584 // we require some stencil format.
585 GrAssert(fGLCaps.fStencilFormats.count() > 0);
586 }
587}
588
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000589GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) {
590 if (GR_GL_RGBA_8888_READBACK_SLOW && GrPixelConfigIsRGBA8888(config)) {
591 return GrPixelConfigSwapRAndB(config);
592 } else {
593 return config;
594 }
595}
596
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000597void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000598 if (gPrintStartupSpew && !fPrintedCaps) {
599 fPrintedCaps = true;
600 this->getCaps().print();
601 fGLCaps.print();
602 }
603
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000604 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000605 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000606 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000607
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000608 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000609 GL_CALL(Disable(GR_GL_DEPTH_TEST));
610 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000611
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000612 GL_CALL(Disable(GR_GL_CULL_FACE));
613 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000614 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000615
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000616 GL_CALL(Disable(GR_GL_DITHER));
617 if (kDesktop_GrGLBinding == this->glBinding()) {
618 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
619 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
620 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000621 fHWAAState.fMSAAEnabled = false;
622 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000623 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000624
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000625 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000626 fHWDrawState.fFlagBits = 0;
627
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000629 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000630
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000631 // invalid
632 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000633
reed@google.comac10a2d2010-12-22 21:39:39 +0000634 // illegal values
tomhudson@google.com62b09682011-11-09 16:39:17 +0000635 //fHWDrawState.fSrcBlend = (GrBlendCoeff)(uint8_t)-1;
636 fHWDrawState.fSrcBlend = (GrBlendCoeff)0xFF;
637 fHWDrawState.fDstBlend = (GrBlendCoeff)(uint8_t)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000638
639 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000640 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000641
reed@google.comac10a2d2010-12-22 21:39:39 +0000642 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000643
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000644 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000645
tomhudson@google.com93813632011-10-27 20:21:16 +0000646 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000647 fHWDrawState.fTextures[s] = NULL;
648 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
649 -GR_ScalarMax,
650 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000651 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000652 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000653 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000654
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000655 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000656 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000657 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000658 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000659
bsalomon@google.comd302f142011-03-03 13:54:13 +0000660 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000661 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000662 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000663
664 fHWGeometryState.fIndexBuffer = NULL;
665 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000666
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000667 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000668
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000669 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000670 fHWDrawState.fRenderTarget = NULL;
671}
672
bsalomon@google.come269f212011-11-07 13:29:52 +0000673GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
674 GrGLenum internalFormat; // we don't need this value
675 GrGLTexture::Desc glTexDesc;
676 if (!this->canBeTexture(desc.fConfig, &internalFormat,
677 &glTexDesc.fUploadFormat, &glTexDesc.fUploadType)) {
678 return NULL;
679 }
680
681 glTexDesc.fContentWidth = glTexDesc.fAllocWidth = desc.fWidth;
682 glTexDesc.fContentHeight = glTexDesc.fAllocHeight = desc.fHeight;
683 glTexDesc.fConfig = desc.fConfig;
684 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
685 glTexDesc.fOwnsID = false;
686 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
687
688 GrGLTexture* texture = NULL;
689 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
690 GrGLRenderTarget::Desc glRTDesc;
691 glRTDesc.fRTFBOID = 0;
692 glRTDesc.fTexFBOID = 0;
693 glRTDesc.fMSColorRenderbufferID = 0;
694 glRTDesc.fOwnIDs = true;
695 glRTDesc.fConfig = desc.fConfig;
696 glRTDesc.fSampleCnt = desc.fSampleCnt;
697 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
698 glTexDesc.fAllocHeight,
699 glTexDesc.fTextureID,
700 &glRTDesc)) {
701 return NULL;
702 }
703 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
704 } else {
705 texture = new GrGLTexture(this, glTexDesc);
706 }
707 if (NULL == texture) {
708 return NULL;
709 }
710
711 this->setSpareTextureUnit();
712 return texture;
713}
714
715GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
716 GrGLRenderTarget::Desc glDesc;
717 glDesc.fConfig = desc.fConfig;
718 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
719 glDesc.fMSColorRenderbufferID = 0;
720 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
721 glDesc.fSampleCnt = desc.fSampleCnt;
722 glDesc.fOwnIDs = false;
723 GrGLIRect viewport;
724 viewport.fLeft = 0;
725 viewport.fBottom = 0;
726 viewport.fWidth = desc.fWidth;
727 viewport.fHeight = desc.fHeight;
728
729 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
730 if (desc.fStencilBits) {
731 GrGLStencilBuffer::Format format;
732 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
733 format.fPacked = false;
734 format.fStencilBits = desc.fStencilBits;
735 format.fTotalBits = desc.fStencilBits;
736 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
737 0,
738 desc.fWidth,
739 desc.fHeight,
740 desc.fSampleCnt,
741 format);
742 tgt->setStencilBuffer(sb);
743 sb->unref();
744 }
745 return tgt;
746}
747
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000748GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
749
750 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
751 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
752 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
753 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
754
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000755 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000756 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000757
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000758 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000759 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000760 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000761 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000762 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000763 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000764 } else {
765 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000766 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000767 }
768 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000769 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000770 }
771 // we don't know what the RB ids are without glGets and we don't care
772 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000773 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000774 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000775 if (desc.fStencilBits) {
776 GrGLStencilBuffer::Format format;
777 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
778 format.fPacked = false;
779 format.fStencilBits = desc.fStencilBits;
780 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000781 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
782 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000783 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000784 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000785 }
786
787 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000788 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000789 GrGLenum dontCare;
790 if (!canBeTexture(desc.fConfig, &dontCare,
791 &texDesc.fUploadFormat,
792 &texDesc.fUploadType)) {
793 return NULL;
794 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000795 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
796 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
797
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000798 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000799 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000800 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000801 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000802
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000803 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000804 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000805 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000806 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000807 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000808 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000809 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000810 } else {
811 GrGLIRect viewport;
812 viewport.fLeft = 0;
813 viewport.fBottom = 0;
814 viewport.fWidth = desc.fWidth;
815 viewport.fHeight = desc.fHeight;
816
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000817 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000818 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000819 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000820 }
821}
822
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000823
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000824////////////////////////////////////////////////////////////////////////////////
825
826void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
827 GrGLenum internalFormat,
828 const void* data,
829 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000830 // we assume the texture is bound
831
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000832 size_t bpp = GrBytesPerPixel(desc.fConfig);
833 size_t trimRowBytes = desc.fContentWidth * bpp;
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000834
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000835 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000836 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000837 }
838
839 // in case we need a temporary, trimmed copy of the src pixels
840 SkAutoSMalloc<128 * 128> tempStorage;
841
842 /*
843 * check whether to allocate a temporary buffer for flipping y or
844 * because our data has extra bytes past each row. If so, we need
845 * to trim those off here, since GL ES doesn't let us specify
846 * GL_UNPACK_ROW_LENGTH.
847 */
848 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000849 if (this->glCaps().fUnpackRowLengthSupport && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000850 if (data && rowBytes != trimRowBytes) {
851 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
852 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000853 }
854 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000855 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000856 // copy the data into our new storage, skipping the trailing bytes
857 size_t trimSize = desc.fContentHeight * trimRowBytes;
858 const char* src = (const char*)data;
859 if (flipY) {
860 src += (desc.fContentHeight - 1) * rowBytes;
861 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000862 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000863 for (int y = 0; y < desc.fContentHeight; y++) {
864 memcpy(dst, src, trimRowBytes);
865 if (flipY) {
866 src -= rowBytes;
867 } else {
868 src += rowBytes;
869 }
870 dst += trimRowBytes;
871 }
872 // now point data to our trimmed version
873 data = tempStorage.get();
874 rowBytes = trimRowBytes;
875 }
876 }
877
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000878 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000879 if (kIndex_8_GrPixelConfig == desc.fConfig &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000880 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000881 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
882 GrAssert(desc.fContentWidth == desc.fAllocWidth);
883 GrAssert(desc.fContentHeight == desc.fAllocHeight);
884 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
885 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000886 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
887 desc.fAllocWidth, desc.fAllocHeight,
888 0, imageSize, data));
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000889 if (this->glCaps().fUnpackRowLengthSupport) {
890 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
891 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000892 } else {
893 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
894 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000895 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
896 desc.fAllocWidth, desc.fAllocHeight,
897 0, desc.fUploadFormat, desc.fUploadType, NULL));
898 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
899 desc.fContentHeight, desc.fUploadFormat,
900 desc.fUploadType, data));
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000901 if (this->glCaps().fUnpackRowLengthSupport) {
902 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
903 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000904
905 int extraW = desc.fAllocWidth - desc.fContentWidth;
906 int extraH = desc.fAllocHeight - desc.fContentHeight;
907 int maxTexels = extraW * extraH;
908 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
909 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
910
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000911 SkAutoSMalloc<128*128> texels(bpp * maxTexels);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000912
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000913 if (extraH) {
914 uint8_t* lastRowStart = (uint8_t*) data +
915 (desc.fContentHeight - 1) * rowBytes;
916 uint8_t* extraRowStart = (uint8_t*)texels.get();
917
918 for (int i = 0; i < extraH; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000919 memcpy(extraRowStart, lastRowStart, trimRowBytes);
920 extraRowStart += trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000921 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
923 desc.fContentHeight, desc.fContentWidth,
924 extraH, desc.fUploadFormat,
925 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000926 }
927 if (extraW) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000928 uint8_t* edgeTexel = (uint8_t*)data + trimRowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000929 uint8_t* extraTexel = (uint8_t*)texels.get();
930 for (int j = 0; j < desc.fContentHeight; ++j) {
931 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000932 memcpy(extraTexel, edgeTexel, bpp);
933 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000934 }
935 edgeTexel += rowBytes;
936 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000937 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
938 0, extraW, desc.fContentHeight,
939 desc.fUploadFormat, desc.fUploadType,
940 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000941 }
942 if (extraW && extraH) {
943 uint8_t* cornerTexel = (uint8_t*)data +
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000944 desc.fContentHeight * rowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000945 uint8_t* extraTexel = (uint8_t*)texels.get();
946 for (int i = 0; i < extraW*extraH; ++i) {
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000947 memcpy(extraTexel, cornerTexel, bpp);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000948 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000949 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000950 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
951 desc.fContentHeight, extraW, extraH,
952 desc.fUploadFormat, desc.fUploadType,
953 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000954 }
955
956 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000957 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
958 desc.fAllocWidth, desc.fAllocHeight, 0,
959 desc.fUploadFormat, desc.fUploadType, data));
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000960 if (this->glCaps().fUnpackRowLengthSupport) {
961 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
962 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000963 }
964 }
965}
966
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000967bool GrGpuGL::createRenderTargetObjects(int width, int height,
968 GrGLuint texID,
969 GrGLRenderTarget::Desc* desc) {
970 desc->fMSColorRenderbufferID = 0;
971 desc->fRTFBOID = 0;
972 desc->fTexFBOID = 0;
973 desc->fOwnIDs = true;
974
975 GrGLenum status;
976 GrGLint err;
977
bsalomon@google.comab15d612011-08-09 12:57:56 +0000978 GrGLenum msColorFormat = 0; // suppress warning
979
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000980 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000981 if (!desc->fTexFBOID) {
982 goto FAILED;
983 }
984
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000985
986 // If we are using multisampling we will create two FBOS. We render
987 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000988 if (desc->fSampleCnt > 0) {
989 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
990 goto FAILED;
991 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000992 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
993 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000994 if (!desc->fRTFBOID ||
995 !desc->fMSColorRenderbufferID ||
996 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
997 goto FAILED;
998 }
999 } else {
1000 desc->fRTFBOID = desc->fTexFBOID;
1001 }
1002
1003 if (desc->fRTFBOID != desc->fTexFBOID) {
1004 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001005 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001006 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001007 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1008 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1009 desc->fSampleCnt,
1010 msColorFormat,
1011 width, height));
1012 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001013 if (err != GR_GL_NO_ERROR) {
1014 goto FAILED;
1015 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
1017 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001018 GR_GL_COLOR_ATTACHMENT0,
1019 GR_GL_RENDERBUFFER,
1020 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001021 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001022 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1023 goto FAILED;
1024 }
1025 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001026 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001027
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001028 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1029 GR_GL_COLOR_ATTACHMENT0,
1030 GR_GL_TEXTURE_2D,
1031 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001032 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001033 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1034 goto FAILED;
1035 }
1036
1037 return true;
1038
1039FAILED:
1040 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001041 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001042 }
1043 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001044 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001045 }
1046 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001047 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001048 }
1049 return false;
1050}
1051
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001052// good to set a break-point here to know when createTexture fails
1053static GrTexture* return_null_texture() {
1054// GrAssert(!"null texture");
1055 return NULL;
1056}
1057
1058#if GR_DEBUG
1059static size_t as_size_t(int x) {
1060 return x;
1061}
1062#endif
1063
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001064GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001065 const void* srcData,
1066 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001067
1068#if GR_COLLECT_STATS
1069 ++fStats.fTextureCreateCnt;
1070#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001071
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001072 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001073 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001074 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001075
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001076 glTexDesc.fContentWidth = desc.fWidth;
1077 glTexDesc.fContentHeight = desc.fHeight;
1078 glTexDesc.fAllocWidth = desc.fWidth;
1079 glTexDesc.fAllocHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001080 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001081 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001082
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001083 glRTDesc.fMSColorRenderbufferID = 0;
1084 glRTDesc.fRTFBOID = 0;
1085 glRTDesc.fTexFBOID = 0;
1086 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001087 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001088
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001089 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001090 if (!canBeTexture(desc.fConfig,
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001092 &glTexDesc.fUploadFormat,
1093 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 return return_null_texture();
1095 }
1096
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001097 const Caps& caps = this->getCaps();
1098
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001099 // We keep GrRenderTargets in GL's normal orientation so that they
1100 // can be drawn to by the outside world without the client having
1101 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001102 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001104
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001105 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1106 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1107 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1108 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001109 GrPrintf("AA RT requested but not supported on this platform.");
1110 }
1111
reed@google.comac10a2d2010-12-22 21:39:39 +00001112 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001113 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001114 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1115 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001116 }
1117
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001118 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001120 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001122 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1123 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001124 return return_null_texture();
1125 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001126 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001127 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1128 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001129 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1130 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001131 return return_null_texture();
1132 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001133 }
1134
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001136 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001137 return return_null_texture();
1138 }
1139
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001140 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001141 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001142
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001143 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1144 // drivers have a bug where an FBO won't be complete if it includes a
1145 // texture that is not mipmap complete (considering the filter in use).
1146 GrGLTexture::TexParams initialTexParams;
1147 // we only set a subset here so invalidate first
1148 initialTexParams.invalidate();
1149 initialTexParams.fFilter = GR_GL_NEAREST;
1150 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1151 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001152 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1153 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001154 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001155 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1156 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001157 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001158 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1159 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001160 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001161 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1162 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001163 initialTexParams.fWrapT));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001164 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001165
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001167 if (renderTarget) {
1168#if GR_COLLECT_STATS
1169 ++fStats.fRenderTargetCreateCnt;
1170#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1172 glTexDesc.fAllocHeight,
1173 glTexDesc.fTextureID,
1174 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001175 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001176 return return_null_texture();
1177 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001178 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001180 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001181 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001182 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001183#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001184 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1185 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001186#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 return tex;
1188}
1189
1190namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1192 GrGLuint rb,
1193 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 // we shouldn't ever know one size and not the other
1195 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1196 (kUnknownBitCount == format->fTotalBits));
1197 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001198 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001199 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1200 (GrGLint*)&format->fStencilBits);
1201 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1204 (GrGLint*)&format->fTotalBits);
1205 format->fTotalBits += format->fStencilBits;
1206 } else {
1207 format->fTotalBits = format->fStencilBits;
1208 }
1209 }
1210}
1211}
1212
1213bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1214 int width, int height) {
1215
1216 // All internally created RTs are also textures. We don't create
1217 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1218 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001219 GrAssert(width >= rt->allocatedWidth());
1220 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221
1222 int samples = rt->numSamples();
1223 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001224 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 if (!sbID) {
1226 return false;
1227 }
1228
1229 GrGLStencilBuffer* sb = NULL;
1230
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001231 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001233 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 // we start with the last stencil format that succeeded in hopes
1235 // that we won't go through this loop more than once after the
1236 // first (painful) stencil creation.
1237 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001238 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001239 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 // version on a GL that doesn't have an MSAA extension.
1241 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001242 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1243 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 GR_GL_RENDERBUFFER,
1245 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001246 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247 width,
1248 height));
1249 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1251 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001252 sFmt.fInternalFormat,
1253 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001254 }
1255
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001256 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001257 if (err == GR_GL_NO_ERROR) {
1258 // After sized formats we attempt an unsized format and take whatever
1259 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001260 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001262 sb = new GrGLStencilBuffer(this, sbID, width, height,
1263 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001264 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1265 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001266 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001267 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 return true;
1269 }
1270 sb->abandon(); // otherwise we lose sbID
1271 sb->unref();
1272 }
1273 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001274 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001275 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001276}
1277
1278bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1279 GrRenderTarget* rt) {
1280 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1281
1282 GrGLuint fbo = glrt->renderFBOID();
1283
1284 if (NULL == sb) {
1285 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001286 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001287 GR_GL_STENCIL_ATTACHMENT,
1288 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290 GR_GL_DEPTH_ATTACHMENT,
1291 GR_GL_RENDERBUFFER, 0));
1292#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001293 GrGLenum status;
1294 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001295 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1296#endif
1297 }
1298 return true;
1299 } else {
1300 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1301 GrGLuint rb = glsb->renderbufferID();
1302
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001304 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1305 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001306 GR_GL_STENCIL_ATTACHMENT,
1307 GR_GL_RENDERBUFFER, rb));
1308 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001309 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001310 GR_GL_DEPTH_ATTACHMENT,
1311 GR_GL_RENDERBUFFER, rb));
1312 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001313 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001314 GR_GL_DEPTH_ATTACHMENT,
1315 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001317
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001318 GrGLenum status;
1319 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001320 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001321 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001322 GR_GL_STENCIL_ATTACHMENT,
1323 GR_GL_RENDERBUFFER, 0));
1324 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001325 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001326 GR_GL_DEPTH_ATTACHMENT,
1327 GR_GL_RENDERBUFFER, 0));
1328 }
1329 return false;
1330 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001331 return true;
1332 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001334}
1335
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001336////////////////////////////////////////////////////////////////////////////////
1337
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001338GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001339 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001340 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001342 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001343 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001344 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001346 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1347 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1348 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1349 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1350 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 // deleting bound buffer does implicit bind to 0
1352 fHWGeometryState.fVertexBuffer = NULL;
1353 return NULL;
1354 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001355 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 size, dynamic);
1357 fHWGeometryState.fVertexBuffer = vertexBuffer;
1358 return vertexBuffer;
1359 }
1360 return NULL;
1361}
1362
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001363GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001364 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001365 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001366 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001367 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1368 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001369 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001370 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1371 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1372 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1373 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1374 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001375 // deleting bound buffer does implicit bind to 0
1376 fHWGeometryState.fIndexBuffer = NULL;
1377 return NULL;
1378 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001379 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001380 size, dynamic);
1381 fHWGeometryState.fIndexBuffer = indexBuffer;
1382 return indexBuffer;
1383 }
1384 return NULL;
1385}
1386
reed@google.comac10a2d2010-12-22 21:39:39 +00001387void GrGpuGL::flushScissor(const GrIRect* rect) {
1388 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001389 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001390 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001391
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001392 GrGLIRect scissor;
1393 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001394 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001395 rect->width(), rect->height());
1396 if (scissor.contains(vp)) {
1397 rect = NULL;
1398 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001399 }
1400
1401 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001402 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001403 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001404 fHWBounds.fScissorRect = scissor;
1405 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001406 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001407 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 fHWBounds.fScissorEnabled = true;
1409 }
1410 } else {
1411 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001412 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 fHWBounds.fScissorEnabled = false;
1414 }
1415 }
1416}
1417
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001418void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001419 // parent class should never let us get here with no RT
1420 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1421
bsalomon@google.com74b98712011-11-11 19:46:16 +00001422 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001423 if (NULL != rect) {
1424 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001425 clippedRect = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001426 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1427 fCurrDrawState.fRenderTarget->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001428 if (clippedRect.intersect(rtRect)) {
1429 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001430 } else {
1431 return;
1432 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001433 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001434 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001435 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001436
1437 GrGLfloat r, g, b, a;
1438 static const GrGLfloat scale255 = 1.f / 255.f;
1439 a = GrColorUnpackA(color) * scale255;
1440 GrGLfloat scaleRGB = scale255;
1441 if (GrPixelConfigIsUnpremultiplied(fCurrDrawState.fRenderTarget->config())) {
1442 scaleRGB *= a;
1443 }
1444 r = GrColorUnpackR(color) * scaleRGB;
1445 g = GrColorUnpackG(color) * scaleRGB;
1446 b = GrColorUnpackB(color) * scaleRGB;
1447
1448 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001449 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001450 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001451 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001452}
1453
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001454void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001455 if (NULL == fCurrDrawState.fRenderTarget) {
1456 return;
1457 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001458
1459 this->flushRenderTarget(&GrIRect::EmptyIRect());
1460
reed@google.comac10a2d2010-12-22 21:39:39 +00001461 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001462 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001463 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001464 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001465 GL_CALL(StencilMask(0xffffffff));
1466 GL_CALL(ClearStencil(0));
1467 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001468 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001469}
1470
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001471void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001472 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001473
1474 // this should only be called internally when we know we have a
1475 // stencil buffer.
1476 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001477 GrGLint stencilBitCount =
1478 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001479#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001480 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001481 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001482#else
1483 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001484 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001485 // turned into draws. Our contract on GrDrawTarget says that
1486 // changing the clip between stencil passes may or may not
1487 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001488 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001489#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001490 GrGLint value;
1491 if (insideClip) {
1492 value = (1 << (stencilBitCount - 1));
1493 } else {
1494 value = 0;
1495 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001496 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001497 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001498 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001499 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001500 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001501 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001502}
1503
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001504void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001505 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001506}
1507
bsalomon@google.comc4364992011-11-07 15:54:49 +00001508bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1509 int left, int top,
1510 int width, int height,
1511 GrPixelConfig config,
1512 size_t rowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001513 // if we have to do memcpy to handle non-trim rowBytes then we
1514 // get the flip for free. Otherwise it costs.
1515 return this->glCaps().fPackRowLengthSupport ||
1516 0 == rowBytes ||
1517 GrBytesPerPixel(config) * width == rowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001518}
1519
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001520bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001521 int left, int top,
1522 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001523 GrPixelConfig config,
1524 void* buffer,
1525 size_t rowBytes,
1526 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001527 GrGLenum internalFormat; // we don't use this for glReadPixels
1528 GrGLenum format;
1529 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001530 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1531 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001532 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001533
bsalomon@google.comc6980972011-11-02 19:57:21 +00001534 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001535 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1536 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1537 switch (tgt->getResolveType()) {
1538 case GrGLRenderTarget::kCantResolve_ResolveType:
1539 return false;
1540 case GrGLRenderTarget::kAutoResolves_ResolveType:
1541 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1542 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001543 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001544 break;
1545 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001546 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001547 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001548 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1549 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001550 break;
1551 default:
1552 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001553 }
1554
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001555 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001556
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001557 // the read rect is viewport-relative
1558 GrGLIRect readRect;
1559 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001560
1561 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1562 if (0 == rowBytes) {
1563 rowBytes = tightRowBytes;
1564 }
1565 size_t readDstRowBytes = tightRowBytes;
1566 void* readDst = buffer;
1567
1568 // determine if GL can read using the passed rowBytes or if we need
1569 // a scratch buffer.
1570 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1571 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001572 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001573 GrAssert(!(rowBytes % sizeof(GrColor)));
1574 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1575 readDstRowBytes = rowBytes;
1576 } else {
1577 scratch.reset(tightRowBytes * height);
1578 readDst = scratch.get();
1579 }
1580 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001581 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1582 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001583 format, type, readDst));
1584 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001585 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001586 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1587 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001588
1589 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001590 // API presents top-to-bottom. We must preserve the padding contents. Note
1591 // that the above readPixels did not overwrite the padding.
1592 if (readDst == buffer) {
1593 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001594 if (!invertY) {
1595 scratch.reset(tightRowBytes);
1596 void* tmpRow = scratch.get();
1597 // flip y in-place by rows
1598 const int halfY = height >> 1;
1599 char* top = reinterpret_cast<char*>(buffer);
1600 char* bottom = top + (height - 1) * rowBytes;
1601 for (int y = 0; y < halfY; y++) {
1602 memcpy(tmpRow, top, tightRowBytes);
1603 memcpy(top, bottom, tightRowBytes);
1604 memcpy(bottom, tmpRow, tightRowBytes);
1605 top += rowBytes;
1606 bottom -= rowBytes;
1607 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001608 }
1609 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001610 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001611 // copy from readDst to buffer while flipping y
1612 const int halfY = height >> 1;
1613 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001614 char* dst = reinterpret_cast<char*>(buffer);
1615 if (!invertY) {
1616 dst += (height-1) * rowBytes;
1617 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001618 for (int y = 0; y < height; y++) {
1619 memcpy(dst, src, tightRowBytes);
1620 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001621 if (invertY) {
1622 dst += rowBytes;
1623 } else {
1624 dst -= rowBytes;
1625 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001626 }
1627 }
1628 return true;
1629}
1630
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001631void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001632
1633 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1634
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001635 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001636 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001637 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001638 #if GR_COLLECT_STATS
1639 ++fStats.fRenderTargetChngCnt;
1640 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001641 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001642 GrGLenum status;
1643 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001644 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001645 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 }
1647 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001648 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001649 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001650 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001651 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001652 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 fHWBounds.fViewportRect = vp;
1654 }
1655 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001656 if (NULL == bound || !bound->isEmpty()) {
1657 rt->flagAsNeedingResolve(bound);
1658 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001659}
1660
twiz@google.com0f31ca72011-03-18 17:38:11 +00001661GrGLenum gPrimitiveType2GLMode[] = {
1662 GR_GL_TRIANGLES,
1663 GR_GL_TRIANGLE_STRIP,
1664 GR_GL_TRIANGLE_FAN,
1665 GR_GL_POINTS,
1666 GR_GL_LINES,
1667 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001668};
1669
bsalomon@google.comd302f142011-03-03 13:54:13 +00001670#define SWAP_PER_DRAW 0
1671
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001672#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001673 #if GR_MAC_BUILD
1674 #include <AGL/agl.h>
1675 #elif GR_WIN32_BUILD
1676 void SwapBuf() {
1677 DWORD procID = GetCurrentProcessId();
1678 HWND hwnd = GetTopWindow(GetDesktopWindow());
1679 while(hwnd) {
1680 DWORD wndProcID = 0;
1681 GetWindowThreadProcessId(hwnd, &wndProcID);
1682 if(wndProcID == procID) {
1683 SwapBuffers(GetDC(hwnd));
1684 }
1685 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1686 }
1687 }
1688 #endif
1689#endif
1690
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001691void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1692 uint32_t startVertex,
1693 uint32_t startIndex,
1694 uint32_t vertexCount,
1695 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001696 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1697
twiz@google.com0f31ca72011-03-18 17:38:11 +00001698 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001699
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001700 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1701 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1702
1703 // our setupGeometry better have adjusted this to zero since
1704 // DrawElements always draws from the begining of the arrays for idx 0.
1705 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001706
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001707 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1708 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001709#if SWAP_PER_DRAW
1710 glFlush();
1711 #if GR_MAC_BUILD
1712 aglSwapBuffers(aglGetCurrentContext());
1713 int set_a_break_pt_here = 9;
1714 aglSwapBuffers(aglGetCurrentContext());
1715 #elif GR_WIN32_BUILD
1716 SwapBuf();
1717 int set_a_break_pt_here = 9;
1718 SwapBuf();
1719 #endif
1720#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001721}
1722
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001723void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1724 uint32_t startVertex,
1725 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001726 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1727
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001728 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1729
1730 // our setupGeometry better have adjusted this to zero.
1731 // DrawElements doesn't take an offset so we always adjus the startVertex.
1732 GrAssert(0 == startVertex);
1733
1734 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1735 // account for startVertex in the DrawElements case. So we always
1736 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001737 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001738#if SWAP_PER_DRAW
1739 glFlush();
1740 #if GR_MAC_BUILD
1741 aglSwapBuffers(aglGetCurrentContext());
1742 int set_a_break_pt_here = 9;
1743 aglSwapBuffers(aglGetCurrentContext());
1744 #elif GR_WIN32_BUILD
1745 SwapBuf();
1746 int set_a_break_pt_here = 9;
1747 SwapBuf();
1748 #endif
1749#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001750}
1751
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001752void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001753
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001754 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001755 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001757 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1758 rt->renderFBOID()));
1759 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1760 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 #if GR_COLLECT_STATS
1762 ++fStats.fRenderTargetChngCnt;
1763 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001764 // make sure we go through flushRenderTarget() since we've modified
1765 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001767 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001768 const GrIRect dirtyRect = rt->getResolveRect();
1769 GrGLIRect r;
1770 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1771 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001772
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001773 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001774 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001775 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1776 GL_CALL(Scissor(r.fLeft, r.fBottom,
1777 r.fWidth, r.fHeight));
1778 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001779 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 fHWBounds.fScissorEnabled = true;
1781 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001782 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001783 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001784 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1785 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001786 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001787 int right = r.fLeft + r.fWidth;
1788 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001789 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1790 r.fLeft, r.fBottom, right, top,
1791 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001792 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001793 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001794 }
1795}
1796
twiz@google.com0f31ca72011-03-18 17:38:11 +00001797static const GrGLenum grToGLStencilFunc[] = {
1798 GR_GL_ALWAYS, // kAlways_StencilFunc
1799 GR_GL_NEVER, // kNever_StencilFunc
1800 GR_GL_GREATER, // kGreater_StencilFunc
1801 GR_GL_GEQUAL, // kGEqual_StencilFunc
1802 GR_GL_LESS, // kLess_StencilFunc
1803 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1804 GR_GL_EQUAL, // kEqual_StencilFunc,
1805 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001806};
1807GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1808GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1809GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1810GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1811GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1812GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1813GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1814GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1815GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1816
twiz@google.com0f31ca72011-03-18 17:38:11 +00001817static const GrGLenum grToGLStencilOp[] = {
1818 GR_GL_KEEP, // kKeep_StencilOp
1819 GR_GL_REPLACE, // kReplace_StencilOp
1820 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1821 GR_GL_INCR, // kIncClamp_StencilOp
1822 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1823 GR_GL_DECR, // kDecClamp_StencilOp
1824 GR_GL_ZERO, // kZero_StencilOp
1825 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001826};
1827GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1828GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1829GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1830GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1831GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1832GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1833GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1834GR_STATIC_ASSERT(6 == kZero_StencilOp);
1835GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1836
reed@google.comac10a2d2010-12-22 21:39:39 +00001837void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001838 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001839
1840 // use stencil for clipping if clipping is enabled and the clip
1841 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001842 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001843 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001844 bool stencilChange = fHWStencilClip != stencilClip ||
1845 fHWDrawState.fStencilSettings != *settings ||
1846 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1847 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001848
1849 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001850
bsalomon@google.comd302f142011-03-03 13:54:13 +00001851 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1852 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001853
bsalomon@google.comd302f142011-03-03 13:54:13 +00001854 if (settings->isDisabled()) {
1855 if (stencilClip) {
1856 settings = &gClipStencilSettings;
1857 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001858 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859
1860 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001861 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001862 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001863 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001865 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1867 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1868 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1869 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1870 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1871 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1872 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1873 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1874 }
1875 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001876 int stencilBits = 0;
1877 GrStencilBuffer* stencilBuffer =
1878 fCurrDrawState.fRenderTarget->getStencilBuffer();
1879 if (NULL != stencilBuffer) {
1880 stencilBits = stencilBuffer->bits();
1881 }
1882 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883 GrAssert(stencilBits ||
1884 (GrStencilSettings::gDisabled ==
1885 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001886 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1887 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001888
1889 unsigned int frontRef = settings->fFrontFuncRef;
1890 unsigned int frontMask = settings->fFrontFuncMask;
1891 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001892 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001893
1894 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1895
1896 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1897 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1898 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001899 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
1900 stencilClip, settings->fFrontFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001901
1902 ConvertStencilFuncAndMask(settings->fFrontFunc,
1903 stencilClip,
1904 clipStencilMask,
1905 userStencilMask,
1906 &frontRef,
1907 &frontMask);
1908 frontWriteMask &= userStencilMask;
1909 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001910 GrAssert((size_t)
1911 settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1912 GrAssert((size_t)
1913 settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
1914 GrAssert((size_t)
1915 settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1916 GrAssert((size_t)
1917 settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001918 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001919 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001920
1921 unsigned int backRef = settings->fBackFuncRef;
1922 unsigned int backMask = settings->fBackFuncMask;
1923 unsigned int backWriteMask = settings->fBackWriteMask;
1924
1925
1926 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1927 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1928 backFunc = grToGLStencilFunc[settings->fBackFunc];
1929 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001930 backFunc = grToGLStencilFunc[ConvertStencilFunc(
1931 stencilClip, settings->fBackFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001932 ConvertStencilFuncAndMask(settings->fBackFunc,
1933 stencilClip,
1934 clipStencilMask,
1935 userStencilMask,
1936 &backRef,
1937 &backMask);
1938 backWriteMask &= userStencilMask;
1939 }
1940
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001941 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1942 frontRef, frontMask));
1943 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1944 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1945 backRef, backMask));
1946 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1947 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1948 grToGLStencilOp[settings->fFrontFailOp],
1949 grToGLStencilOp[settings->fFrontPassOp],
1950 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001951
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001952 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1953 grToGLStencilOp[settings->fBackFailOp],
1954 grToGLStencilOp[settings->fBackPassOp],
1955 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001956 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001957 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1958 GL_CALL(StencilMask(frontWriteMask));
1959 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001960 grToGLStencilOp[settings->fFrontPassOp],
1961 grToGLStencilOp[settings->fFrontPassOp]));
1962 }
1963 }
1964 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001965 fHWStencilClip = stencilClip;
1966 }
1967}
1968
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001969void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001970 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001971 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1972 // smooth lines.
1973
1974 // we prefer smooth lines over multisampled lines
1975 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001976 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001977 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001978 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001980 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001981 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001982 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001983 fHWAAState.fSmoothLineEnabled = false;
1984 }
1985 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1986 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001987 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001988 fHWAAState.fMSAAEnabled = false;
1989 }
1990 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001991 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001992 fHWAAState.fMSAAEnabled) {
1993 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001994 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001995 fHWAAState.fMSAAEnabled = false;
1996 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001997 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001998 fHWAAState.fMSAAEnabled = true;
1999 }
2000 }
2001 }
2002}
2003
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002004void GrGpuGL::flushBlend(GrPrimitiveType type,
2005 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002006 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002007 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002008 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002009 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002010 fHWBlendDisabled = false;
2011 }
2012 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
2013 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002014 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2015 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002016 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
2017 fHWDrawState.fDstBlend = kISA_BlendCoeff;
2018 }
2019 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002020 // any optimization to disable blending should
2021 // have already been applied and tweaked the coeffs
2022 // to (1, 0).
2023 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2024 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002025 if (fHWBlendDisabled != blendOff) {
2026 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002027 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002028 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002029 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002030 }
2031 fHWBlendDisabled = blendOff;
2032 }
2033 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002034 if (fHWDrawState.fSrcBlend != srcCoeff ||
2035 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002036 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2037 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002038 fHWDrawState.fSrcBlend = srcCoeff;
2039 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002040 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002041 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2042 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002043 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2044
2045 float c[] = {
2046 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2047 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2048 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2049 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2050 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002051 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002052 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2053 }
2054 }
2055 }
2056}
2057
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002058namespace {
2059
2060unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002061 switch (filter) {
2062 case GrSamplerState::kBilinear_Filter:
2063 case GrSamplerState::k4x4Downsample_Filter:
2064 return GR_GL_LINEAR;
2065 case GrSamplerState::kNearest_Filter:
2066 case GrSamplerState::kConvolution_Filter:
2067 return GR_GL_NEAREST;
2068 default:
2069 GrAssert(!"Unknown filter type");
2070 return GR_GL_LINEAR;
2071 }
2072}
2073
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002074const GrGLenum* get_swizzle(GrPixelConfig config,
2075 const GrSamplerState& sampler) {
2076 if (GrPixelConfigIsAlphaOnly(config)) {
2077 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2078 GR_GL_ALPHA, GR_GL_ALPHA };
2079 return gAlphaSmear;
2080 } else if (sampler.swapsRAndB()) {
2081 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2082 GR_GL_RED, GR_GL_ALPHA };
2083 return gRedBlueSwap;
2084 } else {
2085 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2086 GR_GL_BLUE, GR_GL_ALPHA };
2087 return gStraight;
2088 }
2089}
2090
2091void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2092 // should add texparameteri to interface to make 1 instead of 4 calls here
2093 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2094 GR_GL_TEXTURE_SWIZZLE_R,
2095 swizzle[0]));
2096 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2097 GR_GL_TEXTURE_SWIZZLE_G,
2098 swizzle[1]));
2099 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2100 GR_GL_TEXTURE_SWIZZLE_B,
2101 swizzle[2]));
2102 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2103 GR_GL_TEXTURE_SWIZZLE_A,
2104 swizzle[3]));
2105}
2106}
2107
bsalomon@google.comffca4002011-02-22 20:34:01 +00002108bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002109
2110 // GrGpu::setupClipAndFlushState should have already checked this
2111 // and bailed if not true.
2112 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002113
tomhudson@google.com93813632011-10-27 20:21:16 +00002114 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002115 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002116 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002117 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002118
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002119 // true for now, but maybe not with GrEffect.
2120 GrAssert(NULL != nextTexture);
2121 // if we created a rt/tex and rendered to it without using a
2122 // texture and now we're texuring from the rt it will still be
2123 // the last bound texture, but it needs resolving. So keep this
2124 // out of the "last != next" check.
2125 GrGLRenderTarget* texRT =
2126 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2127 if (NULL != texRT) {
2128 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002129 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002130
2131 if (fHWDrawState.fTextures[s] != nextTexture) {
2132 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002133 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002134 #if GR_COLLECT_STATS
2135 ++fStats.fTextureChngCnt;
2136 #endif
2137 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2138 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002139 // The texture matrix has to compensate for texture width/height
2140 // and NPOT-embedded-in-POT
2141 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002142 }
2143
2144 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002145 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002146 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002147 nextTexture->getCachedTexParams(&timestamp);
2148 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002149 GrGLTexture::TexParams newTexParams;
2150
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002151 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002152
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 const GrGLenum* wraps =
2154 GrGLTexture::WrapMode2GLWrap(this->glBinding());
2155 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2156 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002157 memcpy(newTexParams.fSwizzleRGBA,
2158 get_swizzle(nextTexture->config(), sampler),
2159 sizeof(newTexParams.fSwizzleRGBA));
2160 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002161 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002162 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002163 GR_GL_TEXTURE_MAG_FILTER,
2164 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002166 GR_GL_TEXTURE_MIN_FILTER,
2167 newTexParams.fFilter));
2168 }
2169 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2170 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002171 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002172 GR_GL_TEXTURE_WRAP_S,
2173 newTexParams.fWrapS));
2174 }
2175 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2176 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002177 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002178 GR_GL_TEXTURE_WRAP_T,
2179 newTexParams.fWrapT));
2180 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002181 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002182 (setAll ||
2183 memcmp(newTexParams.fSwizzleRGBA,
2184 oldTexParams.fSwizzleRGBA,
2185 sizeof(newTexParams.fSwizzleRGBA)))) {
2186 setTextureUnit(s);
2187 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2188 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002189 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002190 nextTexture->setCachedTexParams(newTexParams,
2191 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 }
2193 }
2194
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002195 GrIRect* rect = NULL;
2196 GrIRect clipBounds;
2197 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2198 fClip.hasConservativeBounds()) {
2199 fClip.getConservativeBounds().roundOut(&clipBounds);
2200 rect = &clipBounds;
2201 }
2202 this->flushRenderTarget(rect);
2203 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002204
reed@google.comac10a2d2010-12-22 21:39:39 +00002205 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2206 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2207 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002208 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002209 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002210 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002211 }
2212 }
2213
bsalomon@google.comd302f142011-03-03 13:54:13 +00002214 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2215 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002216 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002218 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002219 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002220 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002221 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002222 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002223 }
2224
bsalomon@google.comd302f142011-03-03 13:54:13 +00002225 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2226 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002227 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002228 GL_CALL(Enable(GR_GL_CULL_FACE));
2229 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002230 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002231 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002232 GL_CALL(Enable(GR_GL_CULL_FACE));
2233 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002234 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002235 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002236 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002237 break;
2238 default:
2239 GrCrash("Unknown draw face.");
2240 }
2241 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2242 }
2243
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002244#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002245 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002246 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002247 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002248 NULL == fCurrDrawState.fRenderTarget ||
2249 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002250 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002251 fCurrDrawState.fRenderTarget);
2252 }
2253#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002254
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 flushStencil();
2256
bsalomon@google.comd302f142011-03-03 13:54:13 +00002257 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002258 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002259 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002260}
2261
2262void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002263 if (fHWGeometryState.fVertexBuffer != buffer) {
2264 fHWGeometryState.fArrayPtrsDirty = true;
2265 fHWGeometryState.fVertexBuffer = buffer;
2266 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002267}
2268
2269void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002270 if (fHWGeometryState.fVertexBuffer == buffer) {
2271 // deleting bound buffer does implied bind to 0
2272 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002273 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002274 }
2275}
2276
2277void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002278 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002279}
2280
2281void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002282 if (fHWGeometryState.fIndexBuffer == buffer) {
2283 // deleting bound buffer does implied bind to 0
2284 fHWGeometryState.fIndexBuffer = NULL;
2285 }
2286}
2287
reed@google.comac10a2d2010-12-22 21:39:39 +00002288void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2289 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002291 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002292 }
2293 if (fHWDrawState.fRenderTarget == renderTarget) {
2294 fHWDrawState.fRenderTarget = NULL;
2295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002296}
2297
2298void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002299 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002300 if (fCurrDrawState.fTextures[s] == texture) {
2301 fCurrDrawState.fTextures[s] = NULL;
2302 }
2303 if (fHWDrawState.fTextures[s] == texture) {
2304 // deleting bound texture does implied bind to 0
2305 fHWDrawState.fTextures[s] = NULL;
2306 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002307 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002308}
2309
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002310bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002311 GrGLenum* internalFormat,
2312 GrGLenum* format,
2313 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002314 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002315 case kRGBA_8888_PM_GrPixelConfig:
2316 case kRGBA_8888_UPM_GrPixelConfig:
2317 *format = GR_GL_RGBA;
2318 *internalFormat = GR_GL_RGBA;
2319 *type = GR_GL_UNSIGNED_BYTE;
2320 break;
2321 case kBGRA_8888_PM_GrPixelConfig:
2322 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002323 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002324 return false;
2325 }
2326 *format = GR_GL_BGRA;
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002327 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002328 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002329 } else {
2330 *internalFormat = GR_GL_RGBA;
2331 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002332 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002333 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002334 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002335 *format = GR_GL_RGB;
2336 *internalFormat = GR_GL_RGB;
2337 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002338 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002339 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002340 *format = GR_GL_RGBA;
2341 *internalFormat = GR_GL_RGBA;
2342 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002343 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002344 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002345 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002346 *format = GR_GL_PALETTE8_RGBA8;
2347 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002348 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002349 } else {
2350 return false;
2351 }
2352 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002353 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002354 *format = GR_GL_ALPHA;
2355 *internalFormat = GR_GL_ALPHA;
2356 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002357 break;
2358 default:
2359 return false;
2360 }
2361 return true;
2362}
2363
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002364void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002365 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002366 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002367 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002368 fActiveTextureUnitIdx = unit;
2369 }
2370}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002371
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002372void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002373 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002374 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002375 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2376 }
2377}
2378
reed@google.comac10a2d2010-12-22 21:39:39 +00002379/* On ES the internalFormat and format must match for TexImage and we use
2380 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2381 decide the internalFormat. However, on ES internalFormat for
2382 RenderBufferStorage* has to be a specific format (not a base format like
2383 GL_RGBA).
2384 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002385bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002386 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002387 // The ES story for BGRA and RenderbufferStorage appears murky. It
2388 // takes an internal format as a parameter. The OES FBO extension and
2389 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2390 // BGRA extensions adds BGRA as both an internal and external format
2391 // and the other only as an external format (like desktop GL). OES
2392 // restricts RenderbufferStorage's format to a *sized* internal format.
2393 // There is no sized BGRA internal format.
2394 // So if the texture has internal format BGRA we just hope that the
2395 // resolve blit can do RGBA->BGRA internal format conversion.
2396 case kRGBA_8888_PM_GrPixelConfig:
2397 case kRGBA_8888_UPM_GrPixelConfig:
2398 case kBGRA_8888_PM_GrPixelConfig:
2399 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002400 if (fGLCaps.fRGBA8RenderbufferSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002401 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2402 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002403 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002404 return true;
2405 } else {
2406 return false;
2407 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002408 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002409 // ES2 supports 565. ES1 supports it
2410 // with FBO extension desktop GL has
2411 // no such internal format
bsalomon@google.comc4364992011-11-07 15:54:49 +00002412 if (kDesktop_GrGLBinding != this->glBinding()) {
2413 *format = GR_GL_RGB565;
2414 return true;
2415 } else {
2416 return false;
2417 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002418 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002419 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002420 return true;
2421 default:
2422 return false;
2423 }
2424}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002425
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002426void GrGpuGL::resetDirtyFlags() {
2427 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2428}
2429
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002430void GrGpuGL::setBuffers(bool indexed,
2431 int* extraVertexOffset,
2432 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002433
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002435
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002436 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2437
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002438 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002439 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002440 case kBuffer_GeometrySrcType:
2441 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002442 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443 break;
2444 case kArray_GeometrySrcType:
2445 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002446 this->finalizeReservedVertices();
2447 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2448 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002449 break;
2450 default:
2451 vbuf = NULL; // suppress warning
2452 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002453 }
2454
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002455 GrAssert(NULL != vbuf);
2456 GrAssert(!vbuf->isLocked());
2457 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002458 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459 fHWGeometryState.fArrayPtrsDirty = true;
2460 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002461 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462
2463 if (indexed) {
2464 GrAssert(NULL != extraIndexOffset);
2465
2466 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002467 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002468 case kBuffer_GeometrySrcType:
2469 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002470 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002471 break;
2472 case kArray_GeometrySrcType:
2473 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002474 this->finalizeReservedIndices();
2475 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2476 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002477 break;
2478 default:
2479 ibuf = NULL; // suppress warning
2480 GrCrash("Unknown geometry src type!");
2481 }
2482
2483 GrAssert(NULL != ibuf);
2484 GrAssert(!ibuf->isLocked());
2485 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002486 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002487 fHWGeometryState.fIndexBuffer = ibuf;
2488 }
2489 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002490}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002491
2492int GrGpuGL::getMaxEdges() const {
2493 // FIXME: This is a pessimistic estimate based on how many other things
2494 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002495 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2496 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002497}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002498
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002499void GrGpuGL::GLCaps::print() const {
2500 for (int i = 0; i < fStencilFormats.count(); ++i) {
2501 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2502 i,
2503 fStencilFormats[i].fStencilBits,
2504 fStencilFormats[i].fTotalBits);
2505 }
2506
2507 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2508 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2509 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2510 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2511 static const char* gMSFBOExtStr[] = {
2512 "None",
2513 "ARB",
2514 "EXT",
2515 "Apple",
2516 };
2517 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002518 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002519 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2520 }
2521 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2522 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002523 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002524 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002525 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002526 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002527 (fTextureSwizzleSupport ? "YES": "NO"));
2528 GrPrintf("Unpack Row length support: %s\n",
2529 (fUnpackRowLengthSupport ? "YES": "NO"));
2530 GrPrintf("Pack Row length support: %s\n",
2531 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002532}