blob: ebf33f42dfcd4073bf05478311f2213868ad4a69 [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.com7d34d2e2011-01-24 17:41:47 +00001419 if (NULL == fCurrDrawState.fRenderTarget) {
1420 return;
1421 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001422 GrIRect r;
1423 if (NULL != rect) {
1424 // flushScissor expects rect to be clipped to the target.
1425 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001426 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1427 fCurrDrawState.fRenderTarget->height());
1428 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001429 rect = &r;
1430 } 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.com0b77d682011-08-19 13:28:54 +00001436 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001437 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001438 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1439 GrColorUnpackG(color)/255.f,
1440 GrColorUnpackB(color)/255.f,
1441 GrColorUnpackA(color)/255.f));
1442 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001443}
1444
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001445void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001446 if (NULL == fCurrDrawState.fRenderTarget) {
1447 return;
1448 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001449
1450 this->flushRenderTarget(&GrIRect::EmptyIRect());
1451
reed@google.comac10a2d2010-12-22 21:39:39 +00001452 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001453 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001454 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001455 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001456 GL_CALL(StencilMask(0xffffffff));
1457 GL_CALL(ClearStencil(0));
1458 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001459 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001460}
1461
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001462void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001463 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001464
1465 // this should only be called internally when we know we have a
1466 // stencil buffer.
1467 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001468 GrGLint stencilBitCount =
1469 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001470#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001471 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001472 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001473#else
1474 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001475 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001476 // turned into draws. Our contract on GrDrawTarget says that
1477 // changing the clip between stencil passes may or may not
1478 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001479 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001480#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001481 GrGLint value;
1482 if (insideClip) {
1483 value = (1 << (stencilBitCount - 1));
1484 } else {
1485 value = 0;
1486 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001487 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001488 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001489 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001490 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001491 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001492 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001493}
1494
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001495void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001496 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001497}
1498
bsalomon@google.comc4364992011-11-07 15:54:49 +00001499bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1500 int left, int top,
1501 int width, int height,
1502 GrPixelConfig config,
1503 size_t rowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001504 // if we have to do memcpy to handle non-trim rowBytes then we
1505 // get the flip for free. Otherwise it costs.
1506 return this->glCaps().fPackRowLengthSupport ||
1507 0 == rowBytes ||
1508 GrBytesPerPixel(config) * width == rowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001509}
1510
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001511bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001512 int left, int top,
1513 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001514 GrPixelConfig config,
1515 void* buffer,
1516 size_t rowBytes,
1517 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001518 GrGLenum internalFormat; // we don't use this for glReadPixels
1519 GrGLenum format;
1520 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1522 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001523 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001524
bsalomon@google.comc6980972011-11-02 19:57:21 +00001525 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001526 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1527 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1528 switch (tgt->getResolveType()) {
1529 case GrGLRenderTarget::kCantResolve_ResolveType:
1530 return false;
1531 case GrGLRenderTarget::kAutoResolves_ResolveType:
1532 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1533 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001534 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001535 break;
1536 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001537 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001538 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001539 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1540 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001541 break;
1542 default:
1543 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001544 }
1545
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001546 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001547
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001548 // the read rect is viewport-relative
1549 GrGLIRect readRect;
1550 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001551
1552 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1553 if (0 == rowBytes) {
1554 rowBytes = tightRowBytes;
1555 }
1556 size_t readDstRowBytes = tightRowBytes;
1557 void* readDst = buffer;
1558
1559 // determine if GL can read using the passed rowBytes or if we need
1560 // a scratch buffer.
1561 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1562 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001563 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001564 GrAssert(!(rowBytes % sizeof(GrColor)));
1565 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1566 readDstRowBytes = rowBytes;
1567 } else {
1568 scratch.reset(tightRowBytes * height);
1569 readDst = scratch.get();
1570 }
1571 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001572 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1573 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001574 format, type, readDst));
1575 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001576 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001577 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1578 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001579
1580 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001581 // API presents top-to-bottom. We must preserve the padding contents. Note
1582 // that the above readPixels did not overwrite the padding.
1583 if (readDst == buffer) {
1584 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001585 if (!invertY) {
1586 scratch.reset(tightRowBytes);
1587 void* tmpRow = scratch.get();
1588 // flip y in-place by rows
1589 const int halfY = height >> 1;
1590 char* top = reinterpret_cast<char*>(buffer);
1591 char* bottom = top + (height - 1) * rowBytes;
1592 for (int y = 0; y < halfY; y++) {
1593 memcpy(tmpRow, top, tightRowBytes);
1594 memcpy(top, bottom, tightRowBytes);
1595 memcpy(bottom, tmpRow, tightRowBytes);
1596 top += rowBytes;
1597 bottom -= rowBytes;
1598 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001599 }
1600 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001601 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001602 // copy from readDst to buffer while flipping y
1603 const int halfY = height >> 1;
1604 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001605 char* dst = reinterpret_cast<char*>(buffer);
1606 if (!invertY) {
1607 dst += (height-1) * rowBytes;
1608 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001609 for (int y = 0; y < height; y++) {
1610 memcpy(dst, src, tightRowBytes);
1611 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001612 if (invertY) {
1613 dst += rowBytes;
1614 } else {
1615 dst -= rowBytes;
1616 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 }
1618 }
1619 return true;
1620}
1621
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001622void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001623
1624 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1625
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001626 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001627 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001628 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001629 #if GR_COLLECT_STATS
1630 ++fStats.fRenderTargetChngCnt;
1631 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001632 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001633 GrGLenum status;
1634 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001635 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001636 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001637 }
1638 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001639 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001640 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001641 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001642 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001643 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 fHWBounds.fViewportRect = vp;
1645 }
1646 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001647 if (NULL == bound || !bound->isEmpty()) {
1648 rt->flagAsNeedingResolve(bound);
1649 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001650}
1651
twiz@google.com0f31ca72011-03-18 17:38:11 +00001652GrGLenum gPrimitiveType2GLMode[] = {
1653 GR_GL_TRIANGLES,
1654 GR_GL_TRIANGLE_STRIP,
1655 GR_GL_TRIANGLE_FAN,
1656 GR_GL_POINTS,
1657 GR_GL_LINES,
1658 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001659};
1660
bsalomon@google.comd302f142011-03-03 13:54:13 +00001661#define SWAP_PER_DRAW 0
1662
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001663#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001664 #if GR_MAC_BUILD
1665 #include <AGL/agl.h>
1666 #elif GR_WIN32_BUILD
1667 void SwapBuf() {
1668 DWORD procID = GetCurrentProcessId();
1669 HWND hwnd = GetTopWindow(GetDesktopWindow());
1670 while(hwnd) {
1671 DWORD wndProcID = 0;
1672 GetWindowThreadProcessId(hwnd, &wndProcID);
1673 if(wndProcID == procID) {
1674 SwapBuffers(GetDC(hwnd));
1675 }
1676 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1677 }
1678 }
1679 #endif
1680#endif
1681
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001682void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1683 uint32_t startVertex,
1684 uint32_t startIndex,
1685 uint32_t vertexCount,
1686 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001687 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1688
twiz@google.com0f31ca72011-03-18 17:38:11 +00001689 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001690
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001691 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1692 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1693
1694 // our setupGeometry better have adjusted this to zero since
1695 // DrawElements always draws from the begining of the arrays for idx 0.
1696 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001697
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001698 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1699 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001700#if SWAP_PER_DRAW
1701 glFlush();
1702 #if GR_MAC_BUILD
1703 aglSwapBuffers(aglGetCurrentContext());
1704 int set_a_break_pt_here = 9;
1705 aglSwapBuffers(aglGetCurrentContext());
1706 #elif GR_WIN32_BUILD
1707 SwapBuf();
1708 int set_a_break_pt_here = 9;
1709 SwapBuf();
1710 #endif
1711#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001712}
1713
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001714void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1715 uint32_t startVertex,
1716 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001717 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1718
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001719 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1720
1721 // our setupGeometry better have adjusted this to zero.
1722 // DrawElements doesn't take an offset so we always adjus the startVertex.
1723 GrAssert(0 == startVertex);
1724
1725 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1726 // account for startVertex in the DrawElements case. So we always
1727 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001728 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001729#if SWAP_PER_DRAW
1730 glFlush();
1731 #if GR_MAC_BUILD
1732 aglSwapBuffers(aglGetCurrentContext());
1733 int set_a_break_pt_here = 9;
1734 aglSwapBuffers(aglGetCurrentContext());
1735 #elif GR_WIN32_BUILD
1736 SwapBuf();
1737 int set_a_break_pt_here = 9;
1738 SwapBuf();
1739 #endif
1740#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001741}
1742
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001743void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001744
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001745 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001746 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001748 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1749 rt->renderFBOID()));
1750 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1751 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 #if GR_COLLECT_STATS
1753 ++fStats.fRenderTargetChngCnt;
1754 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001755 // make sure we go through flushRenderTarget() since we've modified
1756 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001758 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001759 const GrIRect dirtyRect = rt->getResolveRect();
1760 GrGLIRect r;
1761 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1762 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001763
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001764 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001765 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001766 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1767 GL_CALL(Scissor(r.fLeft, r.fBottom,
1768 r.fWidth, r.fHeight));
1769 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001770 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001771 fHWBounds.fScissorEnabled = true;
1772 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001773 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001774 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001775 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1776 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001777 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001778 int right = r.fLeft + r.fWidth;
1779 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001780 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1781 r.fLeft, r.fBottom, right, top,
1782 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001784 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001785 }
1786}
1787
twiz@google.com0f31ca72011-03-18 17:38:11 +00001788static const GrGLenum grToGLStencilFunc[] = {
1789 GR_GL_ALWAYS, // kAlways_StencilFunc
1790 GR_GL_NEVER, // kNever_StencilFunc
1791 GR_GL_GREATER, // kGreater_StencilFunc
1792 GR_GL_GEQUAL, // kGEqual_StencilFunc
1793 GR_GL_LESS, // kLess_StencilFunc
1794 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1795 GR_GL_EQUAL, // kEqual_StencilFunc,
1796 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001797};
1798GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1799GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1800GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1801GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1802GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1803GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1804GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1805GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1806GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1807
twiz@google.com0f31ca72011-03-18 17:38:11 +00001808static const GrGLenum grToGLStencilOp[] = {
1809 GR_GL_KEEP, // kKeep_StencilOp
1810 GR_GL_REPLACE, // kReplace_StencilOp
1811 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1812 GR_GL_INCR, // kIncClamp_StencilOp
1813 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1814 GR_GL_DECR, // kDecClamp_StencilOp
1815 GR_GL_ZERO, // kZero_StencilOp
1816 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001817};
1818GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1819GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1820GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1821GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1822GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1823GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1824GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1825GR_STATIC_ASSERT(6 == kZero_StencilOp);
1826GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1827
reed@google.comac10a2d2010-12-22 21:39:39 +00001828void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001829 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001830
1831 // use stencil for clipping if clipping is enabled and the clip
1832 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001833 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001834 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835 bool stencilChange = fHWStencilClip != stencilClip ||
1836 fHWDrawState.fStencilSettings != *settings ||
1837 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1838 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001839
1840 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001841
bsalomon@google.comd302f142011-03-03 13:54:13 +00001842 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1843 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001844
bsalomon@google.comd302f142011-03-03 13:54:13 +00001845 if (settings->isDisabled()) {
1846 if (stencilClip) {
1847 settings = &gClipStencilSettings;
1848 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001849 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001850
1851 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001852 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001853 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001854 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001855 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001856 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001857 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1858 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1859 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1860 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1861 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1862 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1863 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1864 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1865 }
1866 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001867 int stencilBits = 0;
1868 GrStencilBuffer* stencilBuffer =
1869 fCurrDrawState.fRenderTarget->getStencilBuffer();
1870 if (NULL != stencilBuffer) {
1871 stencilBits = stencilBuffer->bits();
1872 }
1873 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001874 GrAssert(stencilBits ||
1875 (GrStencilSettings::gDisabled ==
1876 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001877 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1878 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001879
1880 unsigned int frontRef = settings->fFrontFuncRef;
1881 unsigned int frontMask = settings->fFrontFuncMask;
1882 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001883 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001884
1885 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1886
1887 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1888 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1889 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001890 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
1891 stencilClip, settings->fFrontFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001892
1893 ConvertStencilFuncAndMask(settings->fFrontFunc,
1894 stencilClip,
1895 clipStencilMask,
1896 userStencilMask,
1897 &frontRef,
1898 &frontMask);
1899 frontWriteMask &= userStencilMask;
1900 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001901 GrAssert((size_t)
1902 settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1903 GrAssert((size_t)
1904 settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
1905 GrAssert((size_t)
1906 settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1907 GrAssert((size_t)
1908 settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001909 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001910 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001911
1912 unsigned int backRef = settings->fBackFuncRef;
1913 unsigned int backMask = settings->fBackFuncMask;
1914 unsigned int backWriteMask = settings->fBackWriteMask;
1915
1916
1917 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1918 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1919 backFunc = grToGLStencilFunc[settings->fBackFunc];
1920 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001921 backFunc = grToGLStencilFunc[ConvertStencilFunc(
1922 stencilClip, settings->fBackFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923 ConvertStencilFuncAndMask(settings->fBackFunc,
1924 stencilClip,
1925 clipStencilMask,
1926 userStencilMask,
1927 &backRef,
1928 &backMask);
1929 backWriteMask &= userStencilMask;
1930 }
1931
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001932 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1933 frontRef, frontMask));
1934 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1935 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1936 backRef, backMask));
1937 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1938 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1939 grToGLStencilOp[settings->fFrontFailOp],
1940 grToGLStencilOp[settings->fFrontPassOp],
1941 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001942
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001943 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1944 grToGLStencilOp[settings->fBackFailOp],
1945 grToGLStencilOp[settings->fBackPassOp],
1946 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001947 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001948 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1949 GL_CALL(StencilMask(frontWriteMask));
1950 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001951 grToGLStencilOp[settings->fFrontPassOp],
1952 grToGLStencilOp[settings->fFrontPassOp]));
1953 }
1954 }
1955 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001956 fHWStencilClip = stencilClip;
1957 }
1958}
1959
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001960void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001961 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001962 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1963 // smooth lines.
1964
1965 // we prefer smooth lines over multisampled lines
1966 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001967 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001968 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001969 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001970 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001971 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001972 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001973 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001974 fHWAAState.fSmoothLineEnabled = false;
1975 }
1976 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1977 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001978 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001979 fHWAAState.fMSAAEnabled = false;
1980 }
1981 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001982 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001983 fHWAAState.fMSAAEnabled) {
1984 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001985 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001986 fHWAAState.fMSAAEnabled = false;
1987 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001988 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001989 fHWAAState.fMSAAEnabled = true;
1990 }
1991 }
1992 }
1993}
1994
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001995void GrGpuGL::flushBlend(GrPrimitiveType type,
1996 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001997 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001998 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001999 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002000 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002001 fHWBlendDisabled = false;
2002 }
2003 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
2004 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002005 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2006 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002007 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
2008 fHWDrawState.fDstBlend = kISA_BlendCoeff;
2009 }
2010 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002011 // any optimization to disable blending should
2012 // have already been applied and tweaked the coeffs
2013 // to (1, 0).
2014 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2015 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002016 if (fHWBlendDisabled != blendOff) {
2017 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002018 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002019 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002020 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002021 }
2022 fHWBlendDisabled = blendOff;
2023 }
2024 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002025 if (fHWDrawState.fSrcBlend != srcCoeff ||
2026 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002027 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2028 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002029 fHWDrawState.fSrcBlend = srcCoeff;
2030 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002031 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002032 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2033 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002034 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2035
2036 float c[] = {
2037 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2038 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2039 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2040 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2041 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002042 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002043 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2044 }
2045 }
2046 }
2047}
2048
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002049namespace {
2050
2051unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002052 switch (filter) {
2053 case GrSamplerState::kBilinear_Filter:
2054 case GrSamplerState::k4x4Downsample_Filter:
2055 return GR_GL_LINEAR;
2056 case GrSamplerState::kNearest_Filter:
2057 case GrSamplerState::kConvolution_Filter:
2058 return GR_GL_NEAREST;
2059 default:
2060 GrAssert(!"Unknown filter type");
2061 return GR_GL_LINEAR;
2062 }
2063}
2064
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002065const GrGLenum* get_swizzle(GrPixelConfig config,
2066 const GrSamplerState& sampler) {
2067 if (GrPixelConfigIsAlphaOnly(config)) {
2068 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2069 GR_GL_ALPHA, GR_GL_ALPHA };
2070 return gAlphaSmear;
2071 } else if (sampler.swapsRAndB()) {
2072 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2073 GR_GL_RED, GR_GL_ALPHA };
2074 return gRedBlueSwap;
2075 } else {
2076 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2077 GR_GL_BLUE, GR_GL_ALPHA };
2078 return gStraight;
2079 }
2080}
2081
2082void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2083 // should add texparameteri to interface to make 1 instead of 4 calls here
2084 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2085 GR_GL_TEXTURE_SWIZZLE_R,
2086 swizzle[0]));
2087 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2088 GR_GL_TEXTURE_SWIZZLE_G,
2089 swizzle[1]));
2090 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2091 GR_GL_TEXTURE_SWIZZLE_B,
2092 swizzle[2]));
2093 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2094 GR_GL_TEXTURE_SWIZZLE_A,
2095 swizzle[3]));
2096}
2097}
2098
bsalomon@google.comffca4002011-02-22 20:34:01 +00002099bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002100
2101 // GrGpu::setupClipAndFlushState should have already checked this
2102 // and bailed if not true.
2103 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002104
tomhudson@google.com93813632011-10-27 20:21:16 +00002105 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002106 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002107 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002108 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002109
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002110 // true for now, but maybe not with GrEffect.
2111 GrAssert(NULL != nextTexture);
2112 // if we created a rt/tex and rendered to it without using a
2113 // texture and now we're texuring from the rt it will still be
2114 // the last bound texture, but it needs resolving. So keep this
2115 // out of the "last != next" check.
2116 GrGLRenderTarget* texRT =
2117 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2118 if (NULL != texRT) {
2119 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002120 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002121
2122 if (fHWDrawState.fTextures[s] != nextTexture) {
2123 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002124 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002125 #if GR_COLLECT_STATS
2126 ++fStats.fTextureChngCnt;
2127 #endif
2128 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2129 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002130 // The texture matrix has to compensate for texture width/height
2131 // and NPOT-embedded-in-POT
2132 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002133 }
2134
2135 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002136 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002137 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002138 nextTexture->getCachedTexParams(&timestamp);
2139 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002140 GrGLTexture::TexParams newTexParams;
2141
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002142 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002143
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002144 const GrGLenum* wraps =
2145 GrGLTexture::WrapMode2GLWrap(this->glBinding());
2146 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2147 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002148 memcpy(newTexParams.fSwizzleRGBA,
2149 get_swizzle(nextTexture->config(), sampler),
2150 sizeof(newTexParams.fSwizzleRGBA));
2151 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002152 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002154 GR_GL_TEXTURE_MAG_FILTER,
2155 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002156 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002157 GR_GL_TEXTURE_MIN_FILTER,
2158 newTexParams.fFilter));
2159 }
2160 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2161 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_WRAP_S,
2164 newTexParams.fWrapS));
2165 }
2166 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2167 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002168 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002169 GR_GL_TEXTURE_WRAP_T,
2170 newTexParams.fWrapT));
2171 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002172 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002173 (setAll ||
2174 memcmp(newTexParams.fSwizzleRGBA,
2175 oldTexParams.fSwizzleRGBA,
2176 sizeof(newTexParams.fSwizzleRGBA)))) {
2177 setTextureUnit(s);
2178 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2179 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002180 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002181 nextTexture->setCachedTexParams(newTexParams,
2182 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002183 }
2184 }
2185
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002186 GrIRect* rect = NULL;
2187 GrIRect clipBounds;
2188 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2189 fClip.hasConservativeBounds()) {
2190 fClip.getConservativeBounds().roundOut(&clipBounds);
2191 rect = &clipBounds;
2192 }
2193 this->flushRenderTarget(rect);
2194 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002195
reed@google.comac10a2d2010-12-22 21:39:39 +00002196 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2197 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2198 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002199 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002200 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002201 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002202 }
2203 }
2204
bsalomon@google.comd302f142011-03-03 13:54:13 +00002205 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2206 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002207 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002208 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002209 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002210 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002211 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002212 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002213 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002214 }
2215
bsalomon@google.comd302f142011-03-03 13:54:13 +00002216 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2217 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002218 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(Enable(GR_GL_CULL_FACE));
2220 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002221 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002222 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002223 GL_CALL(Enable(GR_GL_CULL_FACE));
2224 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002225 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002226 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002227 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002228 break;
2229 default:
2230 GrCrash("Unknown draw face.");
2231 }
2232 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2233 }
2234
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002235#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002236 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002237 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002238 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002239 NULL == fCurrDrawState.fRenderTarget ||
2240 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002241 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002242 fCurrDrawState.fRenderTarget);
2243 }
2244#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002245
reed@google.comac10a2d2010-12-22 21:39:39 +00002246 flushStencil();
2247
bsalomon@google.comd302f142011-03-03 13:54:13 +00002248 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002249 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002250 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002251}
2252
2253void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002254 if (fHWGeometryState.fVertexBuffer != buffer) {
2255 fHWGeometryState.fArrayPtrsDirty = true;
2256 fHWGeometryState.fVertexBuffer = buffer;
2257 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002258}
2259
2260void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002261 if (fHWGeometryState.fVertexBuffer == buffer) {
2262 // deleting bound buffer does implied bind to 0
2263 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002264 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002265 }
2266}
2267
2268void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002269 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002270}
2271
2272void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002273 if (fHWGeometryState.fIndexBuffer == buffer) {
2274 // deleting bound buffer does implied bind to 0
2275 fHWGeometryState.fIndexBuffer = NULL;
2276 }
2277}
2278
reed@google.comac10a2d2010-12-22 21:39:39 +00002279void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2280 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002282 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002283 }
2284 if (fHWDrawState.fRenderTarget == renderTarget) {
2285 fHWDrawState.fRenderTarget = NULL;
2286 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002287}
2288
2289void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002290 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002291 if (fCurrDrawState.fTextures[s] == texture) {
2292 fCurrDrawState.fTextures[s] = NULL;
2293 }
2294 if (fHWDrawState.fTextures[s] == texture) {
2295 // deleting bound texture does implied bind to 0
2296 fHWDrawState.fTextures[s] = NULL;
2297 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002298 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002299}
2300
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002301bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002302 GrGLenum* internalFormat,
2303 GrGLenum* format,
2304 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002305 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002306 case kRGBA_8888_PM_GrPixelConfig:
2307 case kRGBA_8888_UPM_GrPixelConfig:
2308 *format = GR_GL_RGBA;
2309 *internalFormat = GR_GL_RGBA;
2310 *type = GR_GL_UNSIGNED_BYTE;
2311 break;
2312 case kBGRA_8888_PM_GrPixelConfig:
2313 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002314 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002315 return false;
2316 }
2317 *format = GR_GL_BGRA;
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002318 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002319 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002320 } else {
2321 *internalFormat = GR_GL_RGBA;
2322 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002323 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002324 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002325 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002326 *format = GR_GL_RGB;
2327 *internalFormat = GR_GL_RGB;
2328 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002329 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002330 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002331 *format = GR_GL_RGBA;
2332 *internalFormat = GR_GL_RGBA;
2333 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002334 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002335 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002336 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002337 *format = GR_GL_PALETTE8_RGBA8;
2338 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002339 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002340 } else {
2341 return false;
2342 }
2343 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002344 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002345 *format = GR_GL_ALPHA;
2346 *internalFormat = GR_GL_ALPHA;
2347 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002348 break;
2349 default:
2350 return false;
2351 }
2352 return true;
2353}
2354
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002355void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002356 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002357 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002358 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002359 fActiveTextureUnitIdx = unit;
2360 }
2361}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002362
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002363void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002364 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002365 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002366 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2367 }
2368}
2369
reed@google.comac10a2d2010-12-22 21:39:39 +00002370/* On ES the internalFormat and format must match for TexImage and we use
2371 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2372 decide the internalFormat. However, on ES internalFormat for
2373 RenderBufferStorage* has to be a specific format (not a base format like
2374 GL_RGBA).
2375 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002376bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002377 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002378 // The ES story for BGRA and RenderbufferStorage appears murky. It
2379 // takes an internal format as a parameter. The OES FBO extension and
2380 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2381 // BGRA extensions adds BGRA as both an internal and external format
2382 // and the other only as an external format (like desktop GL). OES
2383 // restricts RenderbufferStorage's format to a *sized* internal format.
2384 // There is no sized BGRA internal format.
2385 // So if the texture has internal format BGRA we just hope that the
2386 // resolve blit can do RGBA->BGRA internal format conversion.
2387 case kRGBA_8888_PM_GrPixelConfig:
2388 case kRGBA_8888_UPM_GrPixelConfig:
2389 case kBGRA_8888_PM_GrPixelConfig:
2390 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002391 if (fGLCaps.fRGBA8RenderbufferSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002392 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2393 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002394 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002395 return true;
2396 } else {
2397 return false;
2398 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002399 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002400 // ES2 supports 565. ES1 supports it
2401 // with FBO extension desktop GL has
2402 // no such internal format
bsalomon@google.comc4364992011-11-07 15:54:49 +00002403 if (kDesktop_GrGLBinding != this->glBinding()) {
2404 *format = GR_GL_RGB565;
2405 return true;
2406 } else {
2407 return false;
2408 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002409 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002410 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002411 return true;
2412 default:
2413 return false;
2414 }
2415}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002416
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002417void GrGpuGL::resetDirtyFlags() {
2418 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2419}
2420
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002421void GrGpuGL::setBuffers(bool indexed,
2422 int* extraVertexOffset,
2423 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002424
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002425 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002426
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002427 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2428
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002429 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002430 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002431 case kBuffer_GeometrySrcType:
2432 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002433 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434 break;
2435 case kArray_GeometrySrcType:
2436 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002437 this->finalizeReservedVertices();
2438 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2439 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002440 break;
2441 default:
2442 vbuf = NULL; // suppress warning
2443 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002444 }
2445
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002446 GrAssert(NULL != vbuf);
2447 GrAssert(!vbuf->isLocked());
2448 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002449 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002450 fHWGeometryState.fArrayPtrsDirty = true;
2451 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002452 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002453
2454 if (indexed) {
2455 GrAssert(NULL != extraIndexOffset);
2456
2457 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002458 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459 case kBuffer_GeometrySrcType:
2460 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002461 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462 break;
2463 case kArray_GeometrySrcType:
2464 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002465 this->finalizeReservedIndices();
2466 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2467 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002468 break;
2469 default:
2470 ibuf = NULL; // suppress warning
2471 GrCrash("Unknown geometry src type!");
2472 }
2473
2474 GrAssert(NULL != ibuf);
2475 GrAssert(!ibuf->isLocked());
2476 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002477 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002478 fHWGeometryState.fIndexBuffer = ibuf;
2479 }
2480 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002481}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002482
2483int GrGpuGL::getMaxEdges() const {
2484 // FIXME: This is a pessimistic estimate based on how many other things
2485 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002486 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2487 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002488}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002489
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002490void GrGpuGL::GLCaps::print() const {
2491 for (int i = 0; i < fStencilFormats.count(); ++i) {
2492 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2493 i,
2494 fStencilFormats[i].fStencilBits,
2495 fStencilFormats[i].fTotalBits);
2496 }
2497
2498 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2499 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2500 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2501 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2502 static const char* gMSFBOExtStr[] = {
2503 "None",
2504 "ARB",
2505 "EXT",
2506 "Apple",
2507 };
2508 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002509 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002510 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2511 }
2512 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2513 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002514 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002515 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002516 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002517 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002518 (fTextureSwizzleSupport ? "YES": "NO"));
2519 GrPrintf("Unpack Row length support: %s\n",
2520 (fUnpackRowLengthSupport ? "YES": "NO"));
2521 GrPrintf("Pack Row length support: %s\n",
2522 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002523}