blob: cfacb402eb1ea317b351538587d766253e489dc7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
twiz@google.com0f31ca72011-03-18 17:38:11 +000027static const GrGLenum gXfermodeCoeff2Blend[] = {
28 GR_GL_ZERO,
29 GR_GL_ONE,
30 GR_GL_SRC_COLOR,
31 GR_GL_ONE_MINUS_SRC_COLOR,
32 GR_GL_DST_COLOR,
33 GR_GL_ONE_MINUS_DST_COLOR,
34 GR_GL_SRC_ALPHA,
35 GR_GL_ONE_MINUS_SRC_ALPHA,
36 GR_GL_DST_ALPHA,
37 GR_GL_ONE_MINUS_DST_ALPHA,
38 GR_GL_CONSTANT_COLOR,
39 GR_GL_ONE_MINUS_CONSTANT_COLOR,
40 GR_GL_CONSTANT_ALPHA,
41 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
43 // extended blend coeffs
44 GR_GL_SRC1_COLOR,
45 GR_GL_ONE_MINUS_SRC1_COLOR,
46 GR_GL_SRC1_ALPHA,
47 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000048};
49
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 static const bool gCoeffReferencesBlendConst[] = {
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 false,
62 true,
63 true,
64 true,
65 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000066
67 // extended blend coeffs
68 false,
69 false,
70 false,
71 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000072 };
73 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
75
76 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
77 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
78 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
79 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
80 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
81 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
82 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
83 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
84 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
85 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
86 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
87 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
88 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
89 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
90
91 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
92 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
93 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
94 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
95
96 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
97 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000098}
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
103 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104 GrMatrix* matrix) {
105 GrAssert(NULL != texture);
106 GrAssert(NULL != matrix);
107 if (GR_Scalar1 != texture->contentScaleX() ||
108 GR_Scalar1 != texture->contentScaleY()) {
109 if (GrSamplerState::kRadial_SampleMode == mode) {
110 GrMatrix scale;
111 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
112 matrix->postConcat(scale);
113 } else if (GrSamplerState::kNormal_SampleMode == mode) {
114 GrMatrix scale;
115 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
116 matrix->postConcat(scale);
117 } else {
118 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
119 }
120 }
121 GrGLTexture::Orientation orientation = texture->orientation();
122 if (GrGLTexture::kBottomUp_Orientation == orientation) {
123 GrMatrix invY;
124 invY.setAll(GR_Scalar1, 0, 0,
125 0, -GR_Scalar1, GR_Scalar1,
126 0, 0, GrMatrix::I()[8]);
127 matrix->postConcat(invY);
128 } else {
129 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
130 }
131}
132
bsalomon@google.comd302f142011-03-03 13:54:13 +0000133bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134 const GrSamplerState& sampler) {
135 GrAssert(NULL != texture);
136 if (!sampler.getMatrix().isIdentity()) {
137 return false;
138 }
139 if (GR_Scalar1 != texture->contentScaleX() ||
140 GR_Scalar1 != texture->contentScaleY()) {
141 return false;
142 }
143 GrGLTexture::Orientation orientation = texture->orientation();
144 if (GrGLTexture::kBottomUp_Orientation == orientation) {
145 return false;
146 } else {
147 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
148 }
149 return true;
150}
151
152///////////////////////////////////////////////////////////////////////////////
153
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000154static bool gPrintStartupSpew;
155
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000156static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000157
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000158 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000159
twiz@google.com0f31ca72011-03-18 17:38:11 +0000160 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
162 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000163 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000164 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
165 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000166 // some implementations require texture to be mip-map complete before
167 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000168 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
169 GR_GL_TEXTURE_MIN_FILTER,
170 GR_GL_NEAREST));
171 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
172 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
173 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
174 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
175 GR_GL_COLOR_ATTACHMENT0,
176 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000177 GrGLenum status;
178 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000179 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
180 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000181
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000182 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000183}
184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185static bool probe_for_npot_render_target_support(const GrGLInterface* gl,
186 bool hasNPOTTextureSupport) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000187
188 /* Experimentation has found that some GLs that support NPOT textures
189 do not support FBOs with a NPOT texture. They report "unsupported" FBO
190 status. I don't know how to explicitly query for this. Do an
191 experiment. Note they may support NPOT with a renderbuffer but not a
192 texture. Presumably, the implementation bloats the renderbuffer
193 internally to the next POT.
194 */
195 if (hasNPOTTextureSupport) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000196 return fbo_test(gl, 200, 200);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000197 }
198 return false;
199}
200
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000201static int probe_for_min_render_target_height(const GrGLInterface* gl,
202 bool hasNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000203 int maxRenderTargetSize) {
204 /* The iPhone 4 has a restriction that for an FBO with texture color
205 attachment with height <= 8 then the width must be <= height. Here
206 we look for such a limitation.
207 */
208 if (gPrintStartupSpew) {
209 GrPrintf("Small height FBO texture experiments\n");
210 }
211 int minRenderTargetHeight = GR_INVAL_GLINT;
212 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
213 GrGLuint w = maxRenderTargetSize;
214 GrGLuint h = i;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000215 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000216 if (gPrintStartupSpew) {
217 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
218 }
219 minRenderTargetHeight = i;
220 break;
221 } else {
222 if (gPrintStartupSpew) {
223 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
224 }
225 }
226 }
227 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
228
229 return minRenderTargetHeight;
230}
231
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000232static int probe_for_min_render_target_width(const GrGLInterface* gl,
233 bool hasNPOTRenderTargetSupport,
234 int maxRenderTargetSize) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000235
236 if (gPrintStartupSpew) {
237 GrPrintf("Small width FBO texture experiments\n");
238 }
239 int minRenderTargetWidth = GR_INVAL_GLINT;
240 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
241 GrGLuint w = i;
242 GrGLuint h = maxRenderTargetSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000243 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000244 if (gPrintStartupSpew) {
245 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
246 }
247 minRenderTargetWidth = i;
248 break;
249 } else {
250 if (gPrintStartupSpew) {
251 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
252 }
253 }
254 }
255 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
256
257 return minRenderTargetWidth;
258}
259
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000260GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000261
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000263
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000264 gl->ref();
265 fGL = gl;
266 fGLBinding = glBinding;
267 switch (glBinding) {
268 case kDesktop_GrGLBinding:
269 GrAssert(gl->supportsDesktop());
270 break;
271 case kES1_GrGLBinding:
272 GrAssert(gl->supportsES1());
273 break;
274 case kES2_GrGLBinding:
275 GrAssert(gl->supportsES2());
276 break;
277 default:
278 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000279 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000280
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000281 GrGLClearErr(fGL);
282
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000283 const GrGLubyte* ext;
284 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000285 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000286 const GrGLubyte* vendor;
287 const GrGLubyte* renderer;
288 const GrGLubyte* version;
289 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
290 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
291 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000292 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
293 this);
294 GrPrintf("------ VENDOR %s\n", vendor);
295 GrPrintf("------ RENDERER %s\n", renderer);
296 GrPrintf("------ VERSION %s\n", version);
297 GrPrintf("------ EXTENSIONS\n %s \n", ext);
298 }
299
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000300 fGLVersion = GrGLGetVersion(gl);
301 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000302 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000304 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000305
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000306 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000307
bsalomon@google.comfe676522011-06-17 18:12:21 +0000308 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000309}
310
311GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000312 // This must be called by before the GrDrawTarget destructor
313 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000314 // This subclass must do this before the base class destructor runs
315 // since we will unref the GrGLInterface.
316 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000317 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000318}
319
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000320///////////////////////////////////////////////////////////////////////////////
321
322static const GrGLuint kUnknownBitCount = ~0;
323
324void GrGpuGL::initCaps() {
325 GrGLint maxTextureUnits;
326 // check FS and fixed-function texture unit limits
327 // we only use textures in the fragment stage currently.
328 // checks are > to make sure we have a spare unit.
329 if (kES1_GrGLBinding != this->glBinding()) {
330 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000331 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000332 }
333 if (kES2_GrGLBinding != this->glBinding()) {
334 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000335 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000336 }
337 if (kES2_GrGLBinding == this->glBinding()) {
338 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
339 &fGLCaps.fMaxFragmentUniformVectors);
340 } else if (kDesktop_GrGLBinding != this->glBinding()) {
341 GrGLint max;
342 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
343 fGLCaps.fMaxFragmentUniformVectors = max / 4;
344 } else {
345 fGLCaps.fMaxFragmentUniformVectors = 16;
346 }
347
348 GrGLint numFormats;
349 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
350 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
351 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
352 for (int i = 0; i < numFormats; ++i) {
353 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
354 fCaps.f8BitPaletteSupport = true;
355 break;
356 }
357 }
358
359 if (kDesktop_GrGLBinding == this->glBinding()) {
360 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
361 this->hasExtension("GL_EXT_stencil_wrap");
362 } else {
363 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(2,0)) ||
364 this->hasExtension("GL_OES_stencil_wrap");
365 }
366
367 if (kDesktop_GrGLBinding == this->glBinding()) {
368 // we could also look for GL_ATI_separate_stencil extension or
369 // GL_EXT_stencil_two_side but they use different function signatures
370 // than GL2.0+ (and than each other).
371 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
372 // supported on GL 1.4 and higher or by extension
373 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
374 this->hasExtension("GL_EXT_stencil_wrap");
375 } else {
376 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
377 // an ES1 extension.
378 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
379 // stencil wrap support is in ES2, ES1 requires extension.
380 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(2,0)) ||
381 this->hasExtension("GL_OES_stencil_wrap");
382 }
383
384 if (kDesktop_GrGLBinding == this->glBinding()) {
385 fGLCaps.fRGBA8Renderbuffer = true;
386 } else {
387 fGLCaps.fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
388 }
389
390
391 if (kDesktop_GrGLBinding != this->glBinding()) {
392 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
393 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
394 }
395 }
396
397 if (kDesktop_GrGLBinding == this->glBinding()) {
398 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
399 // extension includes glMapBuffer.
400 } else {
401 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
402 }
403
404 if (kDesktop_GrGLBinding == this->glBinding()) {
405 if (fGLVersion >= GR_GL_VER(2,0) ||
406 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
407 fCaps.fNPOTTextureTileSupport = true;
408 fCaps.fNPOTTextureSupport = true;
409 } else {
410 fCaps.fNPOTTextureTileSupport = false;
411 fCaps.fNPOTTextureSupport = false;
412 }
413 } else {
414 if (fGLVersion >= GR_GL_VER(2,0)) {
415 fCaps.fNPOTTextureSupport = true;
416 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
417 } else {
418 fCaps.fNPOTTextureSupport =
419 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
420 fCaps.fNPOTTextureTileSupport = false;
421 }
422 }
423
424 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
425
426 ////////////////////////////////////////////////////////////////////////////
427 // Experiments to determine limitations that can't be queried.
428 // TODO: Make these a preprocess that generate some compile time constants.
429 // TODO: probe once at startup, rather than once per context creation.
430
431 int expectNPOTTargets = fGL->fNPOTRenderTargetSupport;
432 if (expectNPOTTargets == kProbe_GrGLCapability) {
433 fCaps.fNPOTRenderTargetSupport =
434 probe_for_npot_render_target_support(fGL, fCaps.fNPOTTextureSupport);
435 } else {
436 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000437 fCaps.fNPOTRenderTargetSupport = (0 != expectNPOTTargets);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000438 }
439
440 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
441 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
442 // Our render targets are always created with textures as the color
443 // attachment, hence this min:
444 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
445
446 fCaps.fMinRenderTargetHeight = fGL->fMinRenderTargetHeight;
447 if (fCaps.fMinRenderTargetHeight == kProbe_GrGLCapability) {
448 fCaps.fMinRenderTargetHeight =
449 probe_for_min_render_target_height(fGL, fCaps.fNPOTRenderTargetSupport,
450 fCaps.fMaxRenderTargetSize);
451 }
452
453 fCaps.fMinRenderTargetWidth = fGL->fMinRenderTargetWidth;
454 if (fCaps.fMinRenderTargetWidth == kProbe_GrGLCapability) {
455 fCaps.fMinRenderTargetWidth =
456 probe_for_min_render_target_width(fGL, fCaps.fNPOTRenderTargetSupport,
457 fCaps.fMaxRenderTargetSize);
458 }
459
460 this->initFSAASupport();
461 this->initStencilFormats();
462}
463
464void GrGpuGL::initFSAASupport() {
465 // TODO: Get rid of GrAALevel and use # samples directly.
466 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
467 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
468 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
469 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
470 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
471
472 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
473 if (kDesktop_GrGLBinding != this->glBinding()) {
474 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
475 // chrome's extension is equivalent to the EXT msaa
476 // and fbo_blit extensions.
477 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
478 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
479 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
480 }
481 } else {
482 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
483 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
484 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
485 this->hasExtension("GL_EXT_framebuffer_blit")) {
486 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
487 }
488 }
489
490 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
491 GrGLint maxSamples;
492 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
493 if (maxSamples > 1 ) {
494 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
495 fGLCaps.fAASamples[kLow_GrAALevel] =
496 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
497 fGLCaps.fAASamples[kMed_GrAALevel] =
498 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
499 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
500 }
501 }
502 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
503}
504
505void GrGpuGL::initStencilFormats() {
506
507 // Build up list of legal stencil formats (though perhaps not supported on
508 // the particular gpu/driver) from most preferred to least.
509
510 // these consts are in order of most preferred to least preferred
511 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
512 static const GrGLStencilBuffer::Format
513 // internal Format stencil bits total bits packed?
514 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
515 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
516 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
517 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
518 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
519 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
520
521 if (kDesktop_GrGLBinding == this->glBinding()) {
522 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
523 this->hasExtension("GL_EXT_packed_depth_stencil") ||
524 this->hasExtension("GL_ARB_framebuffer_object");
525
526 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
527 // require FBO support we can expect these are legal formats and don't
528 // check. These also all support the unsized GL_STENCIL_INDEX.
529 fGLCaps.fStencilFormats.push_back() = gS8;
530 fGLCaps.fStencilFormats.push_back() = gS16;
531 if (supportsPackedDS) {
532 fGLCaps.fStencilFormats.push_back() = gD24S8;
533 }
534 fGLCaps.fStencilFormats.push_back() = gS4;
535 if (supportsPackedDS) {
536 fGLCaps.fStencilFormats.push_back() = gDS;
537 }
538 } else {
539 // ES2 has STENCIL_INDEX8 without extensions.
540 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
541 // introduces tokens for S1 thu S8 but there are separate extensions
542 // that make them legal (GL_OES_stencil1, ...).
543 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
544 // ES doesn't support using the unsized formats.
545
546 if (fGLVersion >= GR_GL_VER(2,0) ||
547 this->hasExtension("GL_OES_stencil8")) {
548 fGLCaps.fStencilFormats.push_back() = gS8;
549 }
550 //fStencilFormats.push_back() = gS16;
551 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
552 fGLCaps.fStencilFormats.push_back() = gD24S8;
553 }
554 if (this->hasExtension("GL_OES_stencil4")) {
555 fGLCaps.fStencilFormats.push_back() = gS4;
556 }
557 // we require some stencil format.
558 GrAssert(fGLCaps.fStencilFormats.count() > 0);
559 }
560}
561
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000562void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000563 if (gPrintStartupSpew && !fPrintedCaps) {
564 fPrintedCaps = true;
565 this->getCaps().print();
566 fGLCaps.print();
567 }
568
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000569 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000570 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000571 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000572
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000573 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000574 GL_CALL(Disable(GR_GL_DEPTH_TEST));
575 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000576
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GL_CALL(Disable(GR_GL_CULL_FACE));
578 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000579 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000581 GL_CALL(Disable(GR_GL_DITHER));
582 if (kDesktop_GrGLBinding == this->glBinding()) {
583 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
584 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
585 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000586 fHWAAState.fMSAAEnabled = false;
587 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000588 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000589
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000590 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000591 fHWDrawState.fFlagBits = 0;
592
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000594 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000595
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000596 // invalid
597 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000598
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000600 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
601 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000602
603 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000604 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000607
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000608 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000609
tomhudson@google.com93813632011-10-27 20:21:16 +0000610 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000611 fHWDrawState.fTextures[s] = NULL;
612 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
613 -GR_ScalarMax,
614 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000615 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000616 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000617 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000618
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000619 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000620 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000621 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000622 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000623
bsalomon@google.comd302f142011-03-03 13:54:13 +0000624 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000626 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000627
628 fHWGeometryState.fIndexBuffer = NULL;
629 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000630
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000631 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000632
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000633 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000634 fHWDrawState.fRenderTarget = NULL;
635}
636
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000637GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
638
639 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
640 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
641 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
642 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
643
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000644 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000645 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000646
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000647 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000648 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000649 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000650 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000651 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000652 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000653 } else {
654 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000655 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000656 }
657 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000658 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000659 }
660 // we don't know what the RB ids are without glGets and we don't care
661 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000662 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000663 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000664 if (desc.fStencilBits) {
665 GrGLStencilBuffer::Format format;
666 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
667 format.fPacked = false;
668 format.fStencilBits = desc.fStencilBits;
669 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000670 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
671 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000672 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000673 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000674 }
675
676 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000677 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000678 GrGLenum dontCare;
679 if (!canBeTexture(desc.fConfig, &dontCare,
680 &texDesc.fUploadFormat,
681 &texDesc.fUploadType)) {
682 return NULL;
683 }
684
685 GrGLTexture::TexParams params;
686
687 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
688 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
689
bsalomon@google.com90405932011-06-17 15:56:55 +0000690 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000691 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000692 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000693 texDesc.fOwnsID = false;
694
695 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000696 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000697 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000698 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000699 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000700 } else {
701 return new GrGLTexture(this, texDesc, params);
702 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000703 } else {
704 GrGLIRect viewport;
705 viewport.fLeft = 0;
706 viewport.fBottom = 0;
707 viewport.fWidth = desc.fWidth;
708 viewport.fHeight = desc.fHeight;
709
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000710 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000711 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000712 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000713 }
714}
715
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000716
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000717////////////////////////////////////////////////////////////////////////////////
718
719void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
720 GrGLenum internalFormat,
721 const void* data,
722 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000723 // we assume the texture is bound
724
725 size_t bpp = GrBytesPerPixel(desc.fFormat);
726 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
727
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000728 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000729 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000730 }
731
732 // in case we need a temporary, trimmed copy of the src pixels
733 SkAutoSMalloc<128 * 128> tempStorage;
734
735 /*
736 * check whether to allocate a temporary buffer for flipping y or
737 * because our data has extra bytes past each row. If so, we need
738 * to trim those off here, since GL ES doesn't let us specify
739 * GL_UNPACK_ROW_LENGTH.
740 */
741 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000742 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000743 if (data && rowBytes != trimRowBytes) {
744 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
745 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000746 }
747 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000748 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000749 // copy the data into our new storage, skipping the trailing bytes
750 size_t trimSize = desc.fContentHeight * trimRowBytes;
751 const char* src = (const char*)data;
752 if (flipY) {
753 src += (desc.fContentHeight - 1) * rowBytes;
754 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000755 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000756 for (int y = 0; y < desc.fContentHeight; y++) {
757 memcpy(dst, src, trimRowBytes);
758 if (flipY) {
759 src -= rowBytes;
760 } else {
761 src += rowBytes;
762 }
763 dst += trimRowBytes;
764 }
765 // now point data to our trimmed version
766 data = tempStorage.get();
767 rowBytes = trimRowBytes;
768 }
769 }
770
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000771 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000772 if (kIndex_8_GrPixelConfig == desc.fFormat &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000773 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000774 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
775 GrAssert(desc.fContentWidth == desc.fAllocWidth);
776 GrAssert(desc.fContentHeight == desc.fAllocHeight);
777 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
778 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000779 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
780 desc.fAllocWidth, desc.fAllocHeight,
781 0, imageSize, data));
782 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000783 } else {
784 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
785 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000786 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
787 desc.fAllocWidth, desc.fAllocHeight,
788 0, desc.fUploadFormat, desc.fUploadType, NULL));
789 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
790 desc.fContentHeight, desc.fUploadFormat,
791 desc.fUploadType, data));
792 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000793
794 int extraW = desc.fAllocWidth - desc.fContentWidth;
795 int extraH = desc.fAllocHeight - desc.fContentHeight;
796 int maxTexels = extraW * extraH;
797 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
798 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
799
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000800 SkAutoSMalloc<128*128> texels(bpp * maxTexels);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000801
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000802 if (extraH) {
803 uint8_t* lastRowStart = (uint8_t*) data +
804 (desc.fContentHeight - 1) * rowBytes;
805 uint8_t* extraRowStart = (uint8_t*)texels.get();
806
807 for (int i = 0; i < extraH; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000808 memcpy(extraRowStart, lastRowStart, trimRowBytes);
809 extraRowStart += trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000810 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000811 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
812 desc.fContentHeight, desc.fContentWidth,
813 extraH, desc.fUploadFormat,
814 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 }
816 if (extraW) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000817 uint8_t* edgeTexel = (uint8_t*)data + trimRowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000818 uint8_t* extraTexel = (uint8_t*)texels.get();
819 for (int j = 0; j < desc.fContentHeight; ++j) {
820 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000821 memcpy(extraTexel, edgeTexel, bpp);
822 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000823 }
824 edgeTexel += rowBytes;
825 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000826 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
827 0, extraW, desc.fContentHeight,
828 desc.fUploadFormat, desc.fUploadType,
829 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000830 }
831 if (extraW && extraH) {
832 uint8_t* cornerTexel = (uint8_t*)data +
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000833 desc.fContentHeight * rowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000834 uint8_t* extraTexel = (uint8_t*)texels.get();
835 for (int i = 0; i < extraW*extraH; ++i) {
836 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000837 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000838 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000839 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
840 desc.fContentHeight, extraW, extraH,
841 desc.fUploadFormat, desc.fUploadType,
842 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000843 }
844
845 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000846 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
847 desc.fAllocWidth, desc.fAllocHeight, 0,
848 desc.fUploadFormat, desc.fUploadType, data));
849 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000850 }
851 }
852}
853
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000854bool GrGpuGL::createRenderTargetObjects(int width, int height,
855 GrGLuint texID,
856 GrGLRenderTarget::Desc* desc) {
857 desc->fMSColorRenderbufferID = 0;
858 desc->fRTFBOID = 0;
859 desc->fTexFBOID = 0;
860 desc->fOwnIDs = true;
861
862 GrGLenum status;
863 GrGLint err;
864
bsalomon@google.comab15d612011-08-09 12:57:56 +0000865 GrGLenum msColorFormat = 0; // suppress warning
866
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000867 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000868 if (!desc->fTexFBOID) {
869 goto FAILED;
870 }
871
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000872
873 // If we are using multisampling we will create two FBOS. We render
874 // to one and then resolve to the texture bound to the other.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000875 if (desc->fSampleCnt > 1 && GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000876 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
877 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000878 if (!desc->fRTFBOID ||
879 !desc->fMSColorRenderbufferID ||
880 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
881 goto FAILED;
882 }
883 } else {
884 desc->fRTFBOID = desc->fTexFBOID;
885 }
886
887 if (desc->fRTFBOID != desc->fTexFBOID) {
888 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000889 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000890 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GR_GL_CALL_NOERRCHECK(this->glInterface(),
892 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
893 desc->fSampleCnt,
894 msColorFormat,
895 width, height));
896 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897 if (err != GR_GL_NO_ERROR) {
898 goto FAILED;
899 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000900 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
901 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000902 GR_GL_COLOR_ATTACHMENT0,
903 GR_GL_RENDERBUFFER,
904 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000905 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000906 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
907 goto FAILED;
908 }
909 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000910 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000911
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000912 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
913 GR_GL_COLOR_ATTACHMENT0,
914 GR_GL_TEXTURE_2D,
915 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000916 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
918 goto FAILED;
919 }
920
921 return true;
922
923FAILED:
924 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000925 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000926 }
927 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000928 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000929 }
930 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000931 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932 }
933 return false;
934}
935
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000936// good to set a break-point here to know when createTexture fails
937static GrTexture* return_null_texture() {
938// GrAssert(!"null texture");
939 return NULL;
940}
941
942#if GR_DEBUG
943static size_t as_size_t(int x) {
944 return x;
945}
946#endif
947
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000948GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000949 const void* srcData,
950 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000951
952#if GR_COLLECT_STATS
953 ++fStats.fTextureCreateCnt;
954#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000955
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000956 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000957 GR_GL_NEAREST,
958 GR_GL_CLAMP_TO_EDGE,
959 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000960 };
reed@google.com1fcd51e2011-01-05 15:50:27 +0000961
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000962 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000963 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000964 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +0000965
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000966 glTexDesc.fContentWidth = desc.fWidth;
967 glTexDesc.fContentHeight = desc.fHeight;
968 glTexDesc.fAllocWidth = desc.fWidth;
969 glTexDesc.fAllocHeight = desc.fHeight;
970 glTexDesc.fFormat = desc.fFormat;
971 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000972
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000973 glRTDesc.fMSColorRenderbufferID = 0;
974 glRTDesc.fRTFBOID = 0;
975 glRTDesc.fTexFBOID = 0;
976 glRTDesc.fOwnIDs = true;
977 glRTDesc.fConfig = glTexDesc.fFormat;
978
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000979 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 if (!canBeTexture(desc.fFormat,
981 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000982 &glTexDesc.fUploadFormat,
983 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 return return_null_texture();
985 }
986
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000987 const Caps& caps = this->getCaps();
988
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000989 // We keep GrRenderTargets in GL's normal orientation so that they
990 // can be drawn to by the outside world without the client having
991 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000992 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000993 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000994
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000995 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
996 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
997 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
998 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000999 GrPrintf("AA RT requested but not supported on this platform.");
1000 }
1001
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001002 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
reed@google.comac10a2d2010-12-22 21:39:39 +00001004 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001005 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001006 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1007 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001008 }
1009
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001010 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001011 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001012 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001013 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001014 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1015 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001016 return return_null_texture();
1017 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001018 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001019 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1020 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001021 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1022 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001023 return return_null_texture();
1024 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001025 }
1026
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001028 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001029 return return_null_texture();
1030 }
1031
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001032 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001033 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1034 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1035 GR_GL_TEXTURE_MAG_FILTER,
1036 DEFAULT_PARAMS.fFilter));
1037 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1038 GR_GL_TEXTURE_MIN_FILTER,
1039 DEFAULT_PARAMS.fFilter));
1040 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1041 GR_GL_TEXTURE_WRAP_S,
1042 DEFAULT_PARAMS.fWrapS));
1043 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1044 GR_GL_TEXTURE_WRAP_T,
1045 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001046
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001047 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001048
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001049 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001050 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001051 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001052#if GR_COLLECT_STATS
1053 ++fStats.fRenderTargetCreateCnt;
1054#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001055 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1056 glTexDesc.fAllocHeight,
1057 glTexDesc.fTextureID,
1058 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 return return_null_texture();
1061 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1063 } else {
1064 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 }
1066#ifdef TRACE_TEXTURE_CREATION
1067 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1068 tex->fTextureID, width, height, tex->fUploadByteCount);
1069#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001070 return tex;
1071}
1072
1073namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001074void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1075 GrGLuint rb,
1076 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 // we shouldn't ever know one size and not the other
1078 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1079 (kUnknownBitCount == format->fTotalBits));
1080 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001081 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1083 (GrGLint*)&format->fStencilBits);
1084 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001085 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1087 (GrGLint*)&format->fTotalBits);
1088 format->fTotalBits += format->fStencilBits;
1089 } else {
1090 format->fTotalBits = format->fStencilBits;
1091 }
1092 }
1093}
1094}
1095
1096bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1097 int width, int height) {
1098
1099 // All internally created RTs are also textures. We don't create
1100 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1101 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001102 GrAssert(width >= rt->allocatedWidth());
1103 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001104
1105 int samples = rt->numSamples();
1106 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001108 if (!sbID) {
1109 return false;
1110 }
1111
1112 GrGLStencilBuffer* sb = NULL;
1113
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001114 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001116 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117 // we start with the last stencil format that succeeded in hopes
1118 // that we won't go through this loop more than once after the
1119 // first (painful) stencil creation.
1120 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001121 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001122 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123 // version on a GL that doesn't have an MSAA extension.
1124 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001125 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1126 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 GR_GL_RENDERBUFFER,
1128 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001129 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 width,
1131 height));
1132 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001133 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1134 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001135 sFmt.fInternalFormat,
1136 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 }
1138
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001140 if (err == GR_GL_NO_ERROR) {
1141 // After sized formats we attempt an unsized format and take whatever
1142 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001143 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001145 sb = new GrGLStencilBuffer(this, sbID, width, height,
1146 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1148 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001149 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151 return true;
1152 }
1153 sb->abandon(); // otherwise we lose sbID
1154 sb->unref();
1155 }
1156 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001158 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159}
1160
1161bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1162 GrRenderTarget* rt) {
1163 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1164
1165 GrGLuint fbo = glrt->renderFBOID();
1166
1167 if (NULL == sb) {
1168 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 GR_GL_STENCIL_ATTACHMENT,
1171 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001172 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 GR_GL_DEPTH_ATTACHMENT,
1174 GR_GL_RENDERBUFFER, 0));
1175#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001176 GrGLenum status;
1177 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1179#endif
1180 }
1181 return true;
1182 } else {
1183 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1184 GrGLuint rb = glsb->renderbufferID();
1185
reed@google.comac10a2d2010-12-22 21:39:39 +00001186 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001187 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1188 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 GR_GL_STENCIL_ATTACHMENT,
1190 GR_GL_RENDERBUFFER, rb));
1191 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 GR_GL_DEPTH_ATTACHMENT,
1194 GR_GL_RENDERBUFFER, rb));
1195 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001196 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 GR_GL_DEPTH_ATTACHMENT,
1198 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001199 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001201 GrGLenum status;
1202 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 GR_GL_STENCIL_ATTACHMENT,
1206 GR_GL_RENDERBUFFER, 0));
1207 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001208 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209 GR_GL_DEPTH_ATTACHMENT,
1210 GR_GL_RENDERBUFFER, 0));
1211 }
1212 return false;
1213 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001214 return true;
1215 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001216 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001217}
1218
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001219////////////////////////////////////////////////////////////////////////////////
1220
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001221GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001222 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001223 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001224 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001226 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001229 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1230 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1231 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1232 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1233 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 // deleting bound buffer does implicit bind to 0
1235 fHWGeometryState.fVertexBuffer = NULL;
1236 return NULL;
1237 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001238 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 size, dynamic);
1240 fHWGeometryState.fVertexBuffer = vertexBuffer;
1241 return vertexBuffer;
1242 }
1243 return NULL;
1244}
1245
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001246GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001247 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1251 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001252 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001253 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1254 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1255 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1256 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1257 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 // deleting bound buffer does implicit bind to 0
1259 fHWGeometryState.fIndexBuffer = NULL;
1260 return NULL;
1261 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001262 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 size, dynamic);
1264 fHWGeometryState.fIndexBuffer = indexBuffer;
1265 return indexBuffer;
1266 }
1267 return NULL;
1268}
1269
reed@google.comac10a2d2010-12-22 21:39:39 +00001270void GrGpuGL::flushScissor(const GrIRect* rect) {
1271 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001272 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001273 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001274
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001275 GrGLIRect scissor;
1276 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001277 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001278 rect->width(), rect->height());
1279 if (scissor.contains(vp)) {
1280 rect = NULL;
1281 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 }
1283
1284 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001286 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 fHWBounds.fScissorRect = scissor;
1288 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001290 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 fHWBounds.fScissorEnabled = true;
1292 }
1293 } else {
1294 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001295 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 fHWBounds.fScissorEnabled = false;
1297 }
1298 }
1299}
1300
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001301void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001302 if (NULL == fCurrDrawState.fRenderTarget) {
1303 return;
1304 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001305 GrIRect r;
1306 if (NULL != rect) {
1307 // flushScissor expects rect to be clipped to the target.
1308 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001309 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1310 fCurrDrawState.fRenderTarget->height());
1311 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001312 rect = &r;
1313 } else {
1314 return;
1315 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001317 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001318 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001319 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001320 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001321 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1322 GrColorUnpackG(color)/255.f,
1323 GrColorUnpackB(color)/255.f,
1324 GrColorUnpackA(color)/255.f));
1325 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001326}
1327
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001328void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001329 if (NULL == fCurrDrawState.fRenderTarget) {
1330 return;
1331 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001332
1333 this->flushRenderTarget(&GrIRect::EmptyIRect());
1334
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001336 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001337 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001339 GL_CALL(StencilMask(0xffffffff));
1340 GL_CALL(ClearStencil(0));
1341 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001342 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001343}
1344
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001345void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001346 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001347
1348 // this should only be called internally when we know we have a
1349 // stencil buffer.
1350 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001351 GrGLint stencilBitCount =
1352 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001353#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001355 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001356#else
1357 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001358 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001359 // turned into draws. Our contract on GrDrawTarget says that
1360 // changing the clip between stencil passes may or may not
1361 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001362 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001363#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001364 GrGLint value;
1365 if (insideClip) {
1366 value = (1 << (stencilBitCount - 1));
1367 } else {
1368 value = 0;
1369 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001370 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001371 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001372 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001373 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001374 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001375 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001376}
1377
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001378void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001379 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001380}
1381
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001382bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001383 int left, int top,
1384 int width, int height,
1385 GrPixelConfig config,
1386 void* buffer, size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001387 GrGLenum internalFormat; // we don't use this for glReadPixels
1388 GrGLenum format;
1389 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001390 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1391 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001392 }
1393
1394 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001395 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1396 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1397 switch (tgt->getResolveType()) {
1398 case GrGLRenderTarget::kCantResolve_ResolveType:
1399 return false;
1400 case GrGLRenderTarget::kAutoResolves_ResolveType:
1401 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1402 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001403 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001404 break;
1405 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001406 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001407 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001408 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1409 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001410 break;
1411 default:
1412 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 }
1414
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001415 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001416
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001417 // the read rect is viewport-relative
1418 GrGLIRect readRect;
1419 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001420
1421 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1422 if (0 == rowBytes) {
1423 rowBytes = tightRowBytes;
1424 }
1425 size_t readDstRowBytes = tightRowBytes;
1426 void* readDst = buffer;
1427
1428 // determine if GL can read using the passed rowBytes or if we need
1429 // a scratch buffer.
1430 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1431 if (rowBytes != tightRowBytes) {
1432 if (kDesktop_GrGLBinding == this->glBinding()) {
1433 GrAssert(!(rowBytes % sizeof(GrColor)));
1434 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1435 readDstRowBytes = rowBytes;
1436 } else {
1437 scratch.reset(tightRowBytes * height);
1438 readDst = scratch.get();
1439 }
1440 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001441 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1442 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001443 format, type, readDst));
1444 if (readDstRowBytes != tightRowBytes) {
1445 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1446 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001447
1448 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001449 // API presents top-to-bottom. We must preserve the padding contents. Note
1450 // that the above readPixels did not overwrite the padding.
1451 if (readDst == buffer) {
1452 GrAssert(rowBytes == readDstRowBytes);
1453 scratch.reset(tightRowBytes);
1454 void* tmpRow = scratch.get();
1455 // flip y in-place by rows
reed@google.comac10a2d2010-12-22 21:39:39 +00001456 const int halfY = height >> 1;
1457 char* top = reinterpret_cast<char*>(buffer);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001458 char* bottom = top + (height - 1) * rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 for (int y = 0; y < halfY; y++) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001460 memcpy(tmpRow, top, tightRowBytes);
1461 memcpy(top, bottom, tightRowBytes);
1462 memcpy(bottom, tmpRow, tightRowBytes);
1463 top += rowBytes;
1464 bottom -= rowBytes;
1465 }
1466 } else {
1467 GrAssert(readDst != buffer);
1468 // copy from readDst to buffer while flipping y
1469 const int halfY = height >> 1;
1470 const char* src = reinterpret_cast<const char*>(readDst);
1471 char* dst = reinterpret_cast<char*>(buffer) + (height-1) * rowBytes;
1472 for (int y = 0; y < height; y++) {
1473 memcpy(dst, src, tightRowBytes);
1474 src += readDstRowBytes;
1475 dst -= rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001476 }
1477 }
1478 return true;
1479}
1480
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001481void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001482
1483 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1484
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001485 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001486 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001487 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001488 #if GR_COLLECT_STATS
1489 ++fStats.fRenderTargetChngCnt;
1490 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001492 GrGLenum status;
1493 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001494 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001495 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001496 }
1497 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001498 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001499 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001500 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001501 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001502 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001503 fHWBounds.fViewportRect = vp;
1504 }
1505 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001506 if (NULL == bound || !bound->isEmpty()) {
1507 rt->flagAsNeedingResolve(bound);
1508 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001509}
1510
twiz@google.com0f31ca72011-03-18 17:38:11 +00001511GrGLenum gPrimitiveType2GLMode[] = {
1512 GR_GL_TRIANGLES,
1513 GR_GL_TRIANGLE_STRIP,
1514 GR_GL_TRIANGLE_FAN,
1515 GR_GL_POINTS,
1516 GR_GL_LINES,
1517 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001518};
1519
bsalomon@google.comd302f142011-03-03 13:54:13 +00001520#define SWAP_PER_DRAW 0
1521
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001522#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001523 #if GR_MAC_BUILD
1524 #include <AGL/agl.h>
1525 #elif GR_WIN32_BUILD
1526 void SwapBuf() {
1527 DWORD procID = GetCurrentProcessId();
1528 HWND hwnd = GetTopWindow(GetDesktopWindow());
1529 while(hwnd) {
1530 DWORD wndProcID = 0;
1531 GetWindowThreadProcessId(hwnd, &wndProcID);
1532 if(wndProcID == procID) {
1533 SwapBuffers(GetDC(hwnd));
1534 }
1535 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1536 }
1537 }
1538 #endif
1539#endif
1540
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001541void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1542 uint32_t startVertex,
1543 uint32_t startIndex,
1544 uint32_t vertexCount,
1545 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001546 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1547
twiz@google.com0f31ca72011-03-18 17:38:11 +00001548 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001549
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001550 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1551 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1552
1553 // our setupGeometry better have adjusted this to zero since
1554 // DrawElements always draws from the begining of the arrays for idx 0.
1555 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001556
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001557 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1558 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001559#if SWAP_PER_DRAW
1560 glFlush();
1561 #if GR_MAC_BUILD
1562 aglSwapBuffers(aglGetCurrentContext());
1563 int set_a_break_pt_here = 9;
1564 aglSwapBuffers(aglGetCurrentContext());
1565 #elif GR_WIN32_BUILD
1566 SwapBuf();
1567 int set_a_break_pt_here = 9;
1568 SwapBuf();
1569 #endif
1570#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001571}
1572
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001573void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1574 uint32_t startVertex,
1575 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001576 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1577
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001578 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1579
1580 // our setupGeometry better have adjusted this to zero.
1581 // DrawElements doesn't take an offset so we always adjus the startVertex.
1582 GrAssert(0 == startVertex);
1583
1584 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1585 // account for startVertex in the DrawElements case. So we always
1586 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001587 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001588#if SWAP_PER_DRAW
1589 glFlush();
1590 #if GR_MAC_BUILD
1591 aglSwapBuffers(aglGetCurrentContext());
1592 int set_a_break_pt_here = 9;
1593 aglSwapBuffers(aglGetCurrentContext());
1594 #elif GR_WIN32_BUILD
1595 SwapBuf();
1596 int set_a_break_pt_here = 9;
1597 SwapBuf();
1598 #endif
1599#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001600}
1601
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001602void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001603
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001604 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001605 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001606 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001607 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1608 rt->renderFBOID()));
1609 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1610 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001611 #if GR_COLLECT_STATS
1612 ++fStats.fRenderTargetChngCnt;
1613 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001614 // make sure we go through flushRenderTarget() since we've modified
1615 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001616 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001617 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001618 const GrIRect dirtyRect = rt->getResolveRect();
1619 GrGLIRect r;
1620 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1621 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001622
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001623 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001624 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001625 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1626 GL_CALL(Scissor(r.fLeft, r.fBottom,
1627 r.fWidth, r.fHeight));
1628 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001629 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 fHWBounds.fScissorEnabled = true;
1631 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001632 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001633 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001634 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1635 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001636 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001637 int right = r.fLeft + r.fWidth;
1638 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001639 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1640 r.fLeft, r.fBottom, right, top,
1641 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001643 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 }
1645}
1646
twiz@google.com0f31ca72011-03-18 17:38:11 +00001647static const GrGLenum grToGLStencilFunc[] = {
1648 GR_GL_ALWAYS, // kAlways_StencilFunc
1649 GR_GL_NEVER, // kNever_StencilFunc
1650 GR_GL_GREATER, // kGreater_StencilFunc
1651 GR_GL_GEQUAL, // kGEqual_StencilFunc
1652 GR_GL_LESS, // kLess_StencilFunc
1653 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1654 GR_GL_EQUAL, // kEqual_StencilFunc,
1655 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001656};
1657GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1658GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1659GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1660GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1661GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1662GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1663GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1664GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1665GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1666
twiz@google.com0f31ca72011-03-18 17:38:11 +00001667static const GrGLenum grToGLStencilOp[] = {
1668 GR_GL_KEEP, // kKeep_StencilOp
1669 GR_GL_REPLACE, // kReplace_StencilOp
1670 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1671 GR_GL_INCR, // kIncClamp_StencilOp
1672 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1673 GR_GL_DECR, // kDecClamp_StencilOp
1674 GR_GL_ZERO, // kZero_StencilOp
1675 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001676};
1677GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1678GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1679GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1680GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1681GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1682GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1683GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1684GR_STATIC_ASSERT(6 == kZero_StencilOp);
1685GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1686
reed@google.comac10a2d2010-12-22 21:39:39 +00001687void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001688 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001689
1690 // use stencil for clipping if clipping is enabled and the clip
1691 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001692 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001693 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001694 bool stencilChange = fHWStencilClip != stencilClip ||
1695 fHWDrawState.fStencilSettings != *settings ||
1696 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1697 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001698
1699 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001700
bsalomon@google.comd302f142011-03-03 13:54:13 +00001701 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1702 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001703
bsalomon@google.comd302f142011-03-03 13:54:13 +00001704 if (settings->isDisabled()) {
1705 if (stencilClip) {
1706 settings = &gClipStencilSettings;
1707 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001708 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001709
1710 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001711 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001712 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001713 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001714 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001715 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001716 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1717 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1718 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1719 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1720 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1721 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1722 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1723 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1724 }
1725 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001726 int stencilBits = 0;
1727 GrStencilBuffer* stencilBuffer =
1728 fCurrDrawState.fRenderTarget->getStencilBuffer();
1729 if (NULL != stencilBuffer) {
1730 stencilBits = stencilBuffer->bits();
1731 }
1732 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001733 GrAssert(stencilBits ||
1734 (GrStencilSettings::gDisabled ==
1735 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001736 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1737 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001738
1739 unsigned int frontRef = settings->fFrontFuncRef;
1740 unsigned int frontMask = settings->fFrontFuncMask;
1741 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001742 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001743
1744 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1745
1746 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1747 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1748 } else {
1749 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1750
1751 ConvertStencilFuncAndMask(settings->fFrontFunc,
1752 stencilClip,
1753 clipStencilMask,
1754 userStencilMask,
1755 &frontRef,
1756 &frontMask);
1757 frontWriteMask &= userStencilMask;
1758 }
1759 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001760 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001761 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001762 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001763 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001764 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001765 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001766 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001767 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001768 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001769
1770 unsigned int backRef = settings->fBackFuncRef;
1771 unsigned int backMask = settings->fBackFuncMask;
1772 unsigned int backWriteMask = settings->fBackWriteMask;
1773
1774
1775 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1776 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1777 backFunc = grToGLStencilFunc[settings->fBackFunc];
1778 } else {
1779 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1780 ConvertStencilFuncAndMask(settings->fBackFunc,
1781 stencilClip,
1782 clipStencilMask,
1783 userStencilMask,
1784 &backRef,
1785 &backMask);
1786 backWriteMask &= userStencilMask;
1787 }
1788
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001789 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1790 frontRef, frontMask));
1791 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1792 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1793 backRef, backMask));
1794 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1795 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1796 grToGLStencilOp[settings->fFrontFailOp],
1797 grToGLStencilOp[settings->fFrontPassOp],
1798 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001799
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001800 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1801 grToGLStencilOp[settings->fBackFailOp],
1802 grToGLStencilOp[settings->fBackPassOp],
1803 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001804 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001805 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1806 GL_CALL(StencilMask(frontWriteMask));
1807 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001808 grToGLStencilOp[settings->fFrontPassOp],
1809 grToGLStencilOp[settings->fFrontPassOp]));
1810 }
1811 }
1812 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001813 fHWStencilClip = stencilClip;
1814 }
1815}
1816
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001817void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001818 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001819 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1820 // smooth lines.
1821
1822 // we prefer smooth lines over multisampled lines
1823 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001824 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001825 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001826 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001827 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001828 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001829 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001830 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001831 fHWAAState.fSmoothLineEnabled = false;
1832 }
1833 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1834 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001835 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001836 fHWAAState.fMSAAEnabled = false;
1837 }
1838 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001839 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001840 fHWAAState.fMSAAEnabled) {
1841 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001842 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001843 fHWAAState.fMSAAEnabled = false;
1844 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001845 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001846 fHWAAState.fMSAAEnabled = true;
1847 }
1848 }
1849 }
1850}
1851
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001852void GrGpuGL::flushBlend(GrPrimitiveType type,
1853 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001854 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001855 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001856 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001857 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001858 fHWBlendDisabled = false;
1859 }
1860 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1861 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001862 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1863 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001864 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1865 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1866 }
1867 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001868 // any optimization to disable blending should
1869 // have already been applied and tweaked the coeffs
1870 // to (1, 0).
1871 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1872 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001873 if (fHWBlendDisabled != blendOff) {
1874 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001875 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001876 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001877 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001878 }
1879 fHWBlendDisabled = blendOff;
1880 }
1881 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001882 if (fHWDrawState.fSrcBlend != srcCoeff ||
1883 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001884 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1885 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001886 fHWDrawState.fSrcBlend = srcCoeff;
1887 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001888 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001889 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1890 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001891 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1892
1893 float c[] = {
1894 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1895 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1896 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1897 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1898 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001899 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001900 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1901 }
1902 }
1903 }
1904}
1905
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001906static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1907 switch (filter) {
1908 case GrSamplerState::kBilinear_Filter:
1909 case GrSamplerState::k4x4Downsample_Filter:
1910 return GR_GL_LINEAR;
1911 case GrSamplerState::kNearest_Filter:
1912 case GrSamplerState::kConvolution_Filter:
1913 return GR_GL_NEAREST;
1914 default:
1915 GrAssert(!"Unknown filter type");
1916 return GR_GL_LINEAR;
1917 }
1918}
1919
bsalomon@google.comffca4002011-02-22 20:34:01 +00001920bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001921
1922 // GrGpu::setupClipAndFlushState should have already checked this
1923 // and bailed if not true.
1924 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001925
tomhudson@google.com93813632011-10-27 20:21:16 +00001926 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001927 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001928 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001929 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001930
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001931 // true for now, but maybe not with GrEffect.
1932 GrAssert(NULL != nextTexture);
1933 // if we created a rt/tex and rendered to it without using a
1934 // texture and now we're texuring from the rt it will still be
1935 // the last bound texture, but it needs resolving. So keep this
1936 // out of the "last != next" check.
1937 GrGLRenderTarget* texRT =
1938 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1939 if (NULL != texRT) {
1940 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001941 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001942
1943 if (fHWDrawState.fTextures[s] != nextTexture) {
1944 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001945 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001946 #if GR_COLLECT_STATS
1947 ++fStats.fTextureChngCnt;
1948 #endif
1949 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1950 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001951 // The texture matrix has to compensate for texture width/height
1952 // and NPOT-embedded-in-POT
1953 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001954 }
1955
1956 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
1957 const GrGLTexture::TexParams& oldTexParams =
1958 nextTexture->getTexParams();
1959 GrGLTexture::TexParams newTexParams;
1960
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001961 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001962
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001963 const GrGLenum* wraps =
1964 GrGLTexture::WrapMode2GLWrap(this->glBinding());
1965 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1966 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001967
1968 if (newTexParams.fFilter != oldTexParams.fFilter) {
1969 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001970 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1971 GR_GL_TEXTURE_MAG_FILTER,
1972 newTexParams.fFilter));
1973 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1974 GR_GL_TEXTURE_MIN_FILTER,
1975 newTexParams.fFilter));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001976 }
1977 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
1978 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1980 GR_GL_TEXTURE_WRAP_S,
1981 newTexParams.fWrapS));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001982 }
1983 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
1984 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001985 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1986 GR_GL_TEXTURE_WRAP_T,
1987 newTexParams.fWrapT));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001988 }
1989 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00001990 }
1991 }
1992
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001993 GrIRect* rect = NULL;
1994 GrIRect clipBounds;
1995 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
1996 fClip.hasConservativeBounds()) {
1997 fClip.getConservativeBounds().roundOut(&clipBounds);
1998 rect = &clipBounds;
1999 }
2000 this->flushRenderTarget(rect);
2001 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002002
reed@google.comac10a2d2010-12-22 21:39:39 +00002003 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2004 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2005 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002006 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002007 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002008 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002009 }
2010 }
2011
bsalomon@google.comd302f142011-03-03 13:54:13 +00002012 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2013 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002014 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002015 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002016 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002017 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002018 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002019 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002020 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002021 }
2022
bsalomon@google.comd302f142011-03-03 13:54:13 +00002023 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2024 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002025 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002026 GL_CALL(Enable(GR_GL_CULL_FACE));
2027 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002028 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002029 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002030 GL_CALL(Enable(GR_GL_CULL_FACE));
2031 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002032 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002033 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002034 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002035 break;
2036 default:
2037 GrCrash("Unknown draw face.");
2038 }
2039 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2040 }
2041
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002042#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002043 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002044 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002045 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002046 NULL == fCurrDrawState.fRenderTarget ||
2047 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002048 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002049 fCurrDrawState.fRenderTarget);
2050 }
2051#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002052
reed@google.comac10a2d2010-12-22 21:39:39 +00002053 flushStencil();
2054
bsalomon@google.comd302f142011-03-03 13:54:13 +00002055 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002056 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002057 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002058}
2059
2060void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002061 if (fHWGeometryState.fVertexBuffer != buffer) {
2062 fHWGeometryState.fArrayPtrsDirty = true;
2063 fHWGeometryState.fVertexBuffer = buffer;
2064 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002065}
2066
2067void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002068 if (fHWGeometryState.fVertexBuffer == buffer) {
2069 // deleting bound buffer does implied bind to 0
2070 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002071 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002072 }
2073}
2074
2075void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002076 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002077}
2078
2079void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002080 if (fHWGeometryState.fIndexBuffer == buffer) {
2081 // deleting bound buffer does implied bind to 0
2082 fHWGeometryState.fIndexBuffer = NULL;
2083 }
2084}
2085
reed@google.comac10a2d2010-12-22 21:39:39 +00002086void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2087 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002088 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002089 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002090 }
2091 if (fHWDrawState.fRenderTarget == renderTarget) {
2092 fHWDrawState.fRenderTarget = NULL;
2093 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002094}
2095
2096void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002097 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002098 if (fCurrDrawState.fTextures[s] == texture) {
2099 fCurrDrawState.fTextures[s] = NULL;
2100 }
2101 if (fHWDrawState.fTextures[s] == texture) {
2102 // deleting bound texture does implied bind to 0
2103 fHWDrawState.fTextures[s] = NULL;
2104 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002105 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002106}
2107
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002108bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002109 GrGLenum* internalFormat,
2110 GrGLenum* format,
2111 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002112 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002113 case kRGBA_8888_GrPixelConfig:
2114 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002115 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002116 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002117 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2118 // format for a BGRA is BGRA not RGBA (as on desktop)
2119 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2120 } else {
2121 *internalFormat = GR_GL_RGBA;
2122 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002123 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002124 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002125 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002126 *format = GR_GL_RGB;
2127 *internalFormat = GR_GL_RGB;
2128 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002129 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002130 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002131 *format = GR_GL_RGBA;
2132 *internalFormat = GR_GL_RGBA;
2133 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002134 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002135 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002136 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002137 *format = GR_GL_PALETTE8_RGBA8;
2138 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002139 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002140 } else {
2141 return false;
2142 }
2143 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002144 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002145 *format = GR_GL_ALPHA;
2146 *internalFormat = GR_GL_ALPHA;
2147 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002148 break;
2149 default:
2150 return false;
2151 }
2152 return true;
2153}
2154
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002155void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002156 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002157 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002158 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002159 fActiveTextureUnitIdx = unit;
2160 }
2161}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002162
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002163void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002164 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002166 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2167 }
2168}
2169
reed@google.comac10a2d2010-12-22 21:39:39 +00002170/* On ES the internalFormat and format must match for TexImage and we use
2171 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2172 decide the internalFormat. However, on ES internalFormat for
2173 RenderBufferStorage* has to be a specific format (not a base format like
2174 GL_RGBA).
2175 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002176bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002177 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002178 case kRGBA_8888_GrPixelConfig:
2179 case kRGBX_8888_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002180 if (fGLCaps.fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002181 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002182 return true;
2183 } else {
2184 return false;
2185 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002186 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002187 // ES2 supports 565. ES1 supports it
2188 // with FBO extension desktop GL has
2189 // no such internal format
2190 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002191 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002193 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002194 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002195 return true;
2196 default:
2197 return false;
2198 }
2199}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002200
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002201void GrGpuGL::resetDirtyFlags() {
2202 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2203}
2204
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002205void GrGpuGL::setBuffers(bool indexed,
2206 int* extraVertexOffset,
2207 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002208
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002209 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002210
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002211 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2212
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002213 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002214 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002215 case kBuffer_GeometrySrcType:
2216 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002217 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002218 break;
2219 case kArray_GeometrySrcType:
2220 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002221 this->finalizeReservedVertices();
2222 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2223 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002224 break;
2225 default:
2226 vbuf = NULL; // suppress warning
2227 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002228 }
2229
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002230 GrAssert(NULL != vbuf);
2231 GrAssert(!vbuf->isLocked());
2232 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002233 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002234 fHWGeometryState.fArrayPtrsDirty = true;
2235 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002236 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002237
2238 if (indexed) {
2239 GrAssert(NULL != extraIndexOffset);
2240
2241 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002242 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002243 case kBuffer_GeometrySrcType:
2244 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002245 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002246 break;
2247 case kArray_GeometrySrcType:
2248 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002249 this->finalizeReservedIndices();
2250 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2251 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002252 break;
2253 default:
2254 ibuf = NULL; // suppress warning
2255 GrCrash("Unknown geometry src type!");
2256 }
2257
2258 GrAssert(NULL != ibuf);
2259 GrAssert(!ibuf->isLocked());
2260 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002261 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002262 fHWGeometryState.fIndexBuffer = ibuf;
2263 }
2264 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002265}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002266
2267int GrGpuGL::getMaxEdges() const {
2268 // FIXME: This is a pessimistic estimate based on how many other things
2269 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002270 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2271 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002272}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002273
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002274void GrGpuGL::GLCaps::print() const {
2275 for (int i = 0; i < fStencilFormats.count(); ++i) {
2276 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2277 i,
2278 fStencilFormats[i].fStencilBits,
2279 fStencilFormats[i].fTotalBits);
2280 }
2281
2282 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2283 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2284 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2285 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2286 static const char* gMSFBOExtStr[] = {
2287 "None",
2288 "ARB",
2289 "EXT",
2290 "Apple",
2291 };
2292 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002293 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002294 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2295 }
2296 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2297 GrPrintf("Support RGBA8 Render Buffer: %s\n",
2298 (fRGBA8Renderbuffer ? "YES": "NO"));
2299}