blob: 10c0fa61578c89754baafac42f2cc14ebcf14078 [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 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000684 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
685 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
686
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000687 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000688 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000689 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000690 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000691
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000692 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000693 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000694 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000695 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000696 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000697 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000698 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000699 } else {
700 GrGLIRect viewport;
701 viewport.fLeft = 0;
702 viewport.fBottom = 0;
703 viewport.fWidth = desc.fWidth;
704 viewport.fHeight = desc.fHeight;
705
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000706 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000707 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000708 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000709 }
710}
711
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000712
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000713////////////////////////////////////////////////////////////////////////////////
714
715void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
716 GrGLenum internalFormat,
717 const void* data,
718 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000719 // we assume the texture is bound
720
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000721 size_t bpp = GrBytesPerPixel(desc.fConfig);
722 size_t trimRowBytes = desc.fContentWidth * bpp;
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000723
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000724 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000725 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000726 }
727
728 // in case we need a temporary, trimmed copy of the src pixels
729 SkAutoSMalloc<128 * 128> tempStorage;
730
731 /*
732 * check whether to allocate a temporary buffer for flipping y or
733 * because our data has extra bytes past each row. If so, we need
734 * to trim those off here, since GL ES doesn't let us specify
735 * GL_UNPACK_ROW_LENGTH.
736 */
737 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000738 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000739 if (data && rowBytes != trimRowBytes) {
740 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
741 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000742 }
743 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000744 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000745 // copy the data into our new storage, skipping the trailing bytes
746 size_t trimSize = desc.fContentHeight * trimRowBytes;
747 const char* src = (const char*)data;
748 if (flipY) {
749 src += (desc.fContentHeight - 1) * rowBytes;
750 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000751 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000752 for (int y = 0; y < desc.fContentHeight; y++) {
753 memcpy(dst, src, trimRowBytes);
754 if (flipY) {
755 src -= rowBytes;
756 } else {
757 src += rowBytes;
758 }
759 dst += trimRowBytes;
760 }
761 // now point data to our trimmed version
762 data = tempStorage.get();
763 rowBytes = trimRowBytes;
764 }
765 }
766
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000767 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000768 if (kIndex_8_GrPixelConfig == desc.fConfig &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000769 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
771 GrAssert(desc.fContentWidth == desc.fAllocWidth);
772 GrAssert(desc.fContentHeight == desc.fAllocHeight);
773 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
774 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000775 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
776 desc.fAllocWidth, desc.fAllocHeight,
777 0, imageSize, data));
778 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000779 } else {
780 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
781 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000782 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
783 desc.fAllocWidth, desc.fAllocHeight,
784 0, desc.fUploadFormat, desc.fUploadType, NULL));
785 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
786 desc.fContentHeight, desc.fUploadFormat,
787 desc.fUploadType, data));
788 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000789
790 int extraW = desc.fAllocWidth - desc.fContentWidth;
791 int extraH = desc.fAllocHeight - desc.fContentHeight;
792 int maxTexels = extraW * extraH;
793 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
794 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
795
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000796 SkAutoSMalloc<128*128> texels(bpp * maxTexels);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000797
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000798 if (extraH) {
799 uint8_t* lastRowStart = (uint8_t*) data +
800 (desc.fContentHeight - 1) * rowBytes;
801 uint8_t* extraRowStart = (uint8_t*)texels.get();
802
803 for (int i = 0; i < extraH; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000804 memcpy(extraRowStart, lastRowStart, trimRowBytes);
805 extraRowStart += trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000806 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000807 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
808 desc.fContentHeight, desc.fContentWidth,
809 extraH, desc.fUploadFormat,
810 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000811 }
812 if (extraW) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000813 uint8_t* edgeTexel = (uint8_t*)data + trimRowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000814 uint8_t* extraTexel = (uint8_t*)texels.get();
815 for (int j = 0; j < desc.fContentHeight; ++j) {
816 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000817 memcpy(extraTexel, edgeTexel, bpp);
818 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000819 }
820 edgeTexel += rowBytes;
821 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000822 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
823 0, extraW, desc.fContentHeight,
824 desc.fUploadFormat, desc.fUploadType,
825 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000826 }
827 if (extraW && extraH) {
828 uint8_t* cornerTexel = (uint8_t*)data +
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000829 desc.fContentHeight * rowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000830 uint8_t* extraTexel = (uint8_t*)texels.get();
831 for (int i = 0; i < extraW*extraH; ++i) {
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000832 memcpy(extraTexel, cornerTexel, bpp);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000833 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000834 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000835 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
836 desc.fContentHeight, extraW, extraH,
837 desc.fUploadFormat, desc.fUploadType,
838 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000839 }
840
841 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000842 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
843 desc.fAllocWidth, desc.fAllocHeight, 0,
844 desc.fUploadFormat, desc.fUploadType, data));
845 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846 }
847 }
848}
849
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000850bool GrGpuGL::createRenderTargetObjects(int width, int height,
851 GrGLuint texID,
852 GrGLRenderTarget::Desc* desc) {
853 desc->fMSColorRenderbufferID = 0;
854 desc->fRTFBOID = 0;
855 desc->fTexFBOID = 0;
856 desc->fOwnIDs = true;
857
858 GrGLenum status;
859 GrGLint err;
860
bsalomon@google.comab15d612011-08-09 12:57:56 +0000861 GrGLenum msColorFormat = 0; // suppress warning
862
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000863 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000864 if (!desc->fTexFBOID) {
865 goto FAILED;
866 }
867
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000868
869 // If we are using multisampling we will create two FBOS. We render
870 // to one and then resolve to the texture bound to the other.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000871 if (desc->fSampleCnt > 1 && GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000872 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
873 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874 if (!desc->fRTFBOID ||
875 !desc->fMSColorRenderbufferID ||
876 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
877 goto FAILED;
878 }
879 } else {
880 desc->fRTFBOID = desc->fTexFBOID;
881 }
882
883 if (desc->fRTFBOID != desc->fTexFBOID) {
884 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000885 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000886 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000887 GR_GL_CALL_NOERRCHECK(this->glInterface(),
888 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
889 desc->fSampleCnt,
890 msColorFormat,
891 width, height));
892 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893 if (err != GR_GL_NO_ERROR) {
894 goto FAILED;
895 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000896 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
897 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000898 GR_GL_COLOR_ATTACHMENT0,
899 GR_GL_RENDERBUFFER,
900 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000901 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000902 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
903 goto FAILED;
904 }
905 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000906 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000908 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
909 GR_GL_COLOR_ATTACHMENT0,
910 GR_GL_TEXTURE_2D,
911 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000912 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
914 goto FAILED;
915 }
916
917 return true;
918
919FAILED:
920 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000921 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000922 }
923 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000924 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000925 }
926 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 }
929 return false;
930}
931
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000932// good to set a break-point here to know when createTexture fails
933static GrTexture* return_null_texture() {
934// GrAssert(!"null texture");
935 return NULL;
936}
937
938#if GR_DEBUG
939static size_t as_size_t(int x) {
940 return x;
941}
942#endif
943
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000944GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000945 const void* srcData,
946 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000947
948#if GR_COLLECT_STATS
949 ++fStats.fTextureCreateCnt;
950#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000951
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000952 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000953 GR_GL_NEAREST,
954 GR_GL_CLAMP_TO_EDGE,
955 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000956 };
reed@google.com1fcd51e2011-01-05 15:50:27 +0000957
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000958 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000959 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000960 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +0000961
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000962 glTexDesc.fContentWidth = desc.fWidth;
963 glTexDesc.fContentHeight = desc.fHeight;
964 glTexDesc.fAllocWidth = desc.fWidth;
965 glTexDesc.fAllocHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000966 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000967 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000968
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000969 glRTDesc.fMSColorRenderbufferID = 0;
970 glRTDesc.fRTFBOID = 0;
971 glRTDesc.fTexFBOID = 0;
972 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000973 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000974
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000975 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000976 if (!canBeTexture(desc.fConfig,
reed@google.comac10a2d2010-12-22 21:39:39 +0000977 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000978 &glTexDesc.fUploadFormat,
979 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 return return_null_texture();
981 }
982
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000983 const Caps& caps = this->getCaps();
984
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000985 // We keep GrRenderTargets in GL's normal orientation so that they
986 // can be drawn to by the outside world without the client having
987 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000988 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000989 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000990
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000991 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
992 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
993 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
994 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000995 GrPrintf("AA RT requested but not supported on this platform.");
996 }
997
reed@google.comac10a2d2010-12-22 21:39:39 +0000998 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000999 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001000 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1001 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001002 }
1003
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001004 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001005 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001006 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001008 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1009 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001010 return return_null_texture();
1011 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001012 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001013 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1014 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001015 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1016 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001017 return return_null_texture();
1018 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 }
1020
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001021 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001022 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001023 return return_null_texture();
1024 }
1025
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001026 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1028 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1029 GR_GL_TEXTURE_MAG_FILTER,
1030 DEFAULT_PARAMS.fFilter));
1031 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1032 GR_GL_TEXTURE_MIN_FILTER,
1033 DEFAULT_PARAMS.fFilter));
1034 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1035 GR_GL_TEXTURE_WRAP_S,
1036 DEFAULT_PARAMS.fWrapS));
1037 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1038 GR_GL_TEXTURE_WRAP_T,
1039 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001040
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001041 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001042
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001043 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001045 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001046#if GR_COLLECT_STATS
1047 ++fStats.fRenderTargetCreateCnt;
1048#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001049 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1050 glTexDesc.fAllocHeight,
1051 glTexDesc.fTextureID,
1052 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001053 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 return return_null_texture();
1055 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001056 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001057 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001058 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001060 tex->setCachedTexParams(DEFAULT_PARAMS, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001061#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001062 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1063 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001064#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001065 return tex;
1066}
1067
1068namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001069void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1070 GrGLuint rb,
1071 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001072 // we shouldn't ever know one size and not the other
1073 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1074 (kUnknownBitCount == format->fTotalBits));
1075 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001076 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1078 (GrGLint*)&format->fStencilBits);
1079 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001080 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001081 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1082 (GrGLint*)&format->fTotalBits);
1083 format->fTotalBits += format->fStencilBits;
1084 } else {
1085 format->fTotalBits = format->fStencilBits;
1086 }
1087 }
1088}
1089}
1090
1091bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1092 int width, int height) {
1093
1094 // All internally created RTs are also textures. We don't create
1095 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1096 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001097 GrAssert(width >= rt->allocatedWidth());
1098 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001099
1100 int samples = rt->numSamples();
1101 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001102 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103 if (!sbID) {
1104 return false;
1105 }
1106
1107 GrGLStencilBuffer* sb = NULL;
1108
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001109 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 // we start with the last stencil format that succeeded in hopes
1113 // that we won't go through this loop more than once after the
1114 // first (painful) stencil creation.
1115 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001116 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001117 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118 // version on a GL that doesn't have an MSAA extension.
1119 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1121 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001122 GR_GL_RENDERBUFFER,
1123 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001124 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 width,
1126 height));
1127 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001128 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1129 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001130 sFmt.fInternalFormat,
1131 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 }
1133
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001134 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135 if (err == GR_GL_NO_ERROR) {
1136 // After sized formats we attempt an unsized format and take whatever
1137 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001138 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001140 sb = new GrGLStencilBuffer(this, sbID, width, height,
1141 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1143 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001144 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 return true;
1147 }
1148 sb->abandon(); // otherwise we lose sbID
1149 sb->unref();
1150 }
1151 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001152 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001153 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154}
1155
1156bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1157 GrRenderTarget* rt) {
1158 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1159
1160 GrGLuint fbo = glrt->renderFBOID();
1161
1162 if (NULL == sb) {
1163 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 GR_GL_STENCIL_ATTACHMENT,
1166 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001167 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 GR_GL_DEPTH_ATTACHMENT,
1169 GR_GL_RENDERBUFFER, 0));
1170#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001171 GrGLenum status;
1172 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1174#endif
1175 }
1176 return true;
1177 } else {
1178 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1179 GrGLuint rb = glsb->renderbufferID();
1180
reed@google.comac10a2d2010-12-22 21:39:39 +00001181 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001182 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1183 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 GR_GL_STENCIL_ATTACHMENT,
1185 GR_GL_RENDERBUFFER, rb));
1186 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001187 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 GR_GL_DEPTH_ATTACHMENT,
1189 GR_GL_RENDERBUFFER, rb));
1190 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 GR_GL_DEPTH_ATTACHMENT,
1193 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001194 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001196 GrGLenum status;
1197 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 GR_GL_STENCIL_ATTACHMENT,
1201 GR_GL_RENDERBUFFER, 0));
1202 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001203 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 GR_GL_DEPTH_ATTACHMENT,
1205 GR_GL_RENDERBUFFER, 0));
1206 }
1207 return false;
1208 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209 return true;
1210 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001211 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001212}
1213
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001214////////////////////////////////////////////////////////////////////////////////
1215
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001216GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001217 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001218 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001219 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001220 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001221 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001222 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001223 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001224 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1225 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1226 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1227 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1228 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 // deleting bound buffer does implicit bind to 0
1230 fHWGeometryState.fVertexBuffer = NULL;
1231 return NULL;
1232 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001233 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 size, dynamic);
1235 fHWGeometryState.fVertexBuffer = vertexBuffer;
1236 return vertexBuffer;
1237 }
1238 return NULL;
1239}
1240
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001241GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001242 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001243 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001244 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001245 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1246 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001247 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1249 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1250 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1251 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1252 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 // deleting bound buffer does implicit bind to 0
1254 fHWGeometryState.fIndexBuffer = NULL;
1255 return NULL;
1256 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001257 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 size, dynamic);
1259 fHWGeometryState.fIndexBuffer = indexBuffer;
1260 return indexBuffer;
1261 }
1262 return NULL;
1263}
1264
reed@google.comac10a2d2010-12-22 21:39:39 +00001265void GrGpuGL::flushScissor(const GrIRect* rect) {
1266 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001267 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001268 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001269
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001270 GrGLIRect scissor;
1271 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001272 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001273 rect->width(), rect->height());
1274 if (scissor.contains(vp)) {
1275 rect = NULL;
1276 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 }
1278
1279 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001281 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 fHWBounds.fScissorRect = scissor;
1283 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001285 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 fHWBounds.fScissorEnabled = true;
1287 }
1288 } else {
1289 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001290 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 fHWBounds.fScissorEnabled = false;
1292 }
1293 }
1294}
1295
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001296void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001297 if (NULL == fCurrDrawState.fRenderTarget) {
1298 return;
1299 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001300 GrIRect r;
1301 if (NULL != rect) {
1302 // flushScissor expects rect to be clipped to the target.
1303 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001304 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1305 fCurrDrawState.fRenderTarget->height());
1306 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001307 rect = &r;
1308 } else {
1309 return;
1310 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001312 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001313 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001314 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001315 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001316 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1317 GrColorUnpackG(color)/255.f,
1318 GrColorUnpackB(color)/255.f,
1319 GrColorUnpackA(color)/255.f));
1320 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001321}
1322
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001323void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001324 if (NULL == fCurrDrawState.fRenderTarget) {
1325 return;
1326 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001327
1328 this->flushRenderTarget(&GrIRect::EmptyIRect());
1329
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001331 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001332 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001334 GL_CALL(StencilMask(0xffffffff));
1335 GL_CALL(ClearStencil(0));
1336 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001337 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001338}
1339
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001340void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001341 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001342
1343 // this should only be called internally when we know we have a
1344 // stencil buffer.
1345 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001346 GrGLint stencilBitCount =
1347 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001348#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001350 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001351#else
1352 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001353 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001354 // turned into draws. Our contract on GrDrawTarget says that
1355 // changing the clip between stencil passes may or may not
1356 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001357 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001358#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001359 GrGLint value;
1360 if (insideClip) {
1361 value = (1 << (stencilBitCount - 1));
1362 } else {
1363 value = 0;
1364 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001365 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001366 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001367 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001368 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001369 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001370 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001371}
1372
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001373void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001374 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001375}
1376
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001377bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001378 int left, int top,
1379 int width, int height,
1380 GrPixelConfig config,
1381 void* buffer, size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001382 GrGLenum internalFormat; // we don't use this for glReadPixels
1383 GrGLenum format;
1384 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001385 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1386 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001387 }
1388
1389 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001390 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1391 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1392 switch (tgt->getResolveType()) {
1393 case GrGLRenderTarget::kCantResolve_ResolveType:
1394 return false;
1395 case GrGLRenderTarget::kAutoResolves_ResolveType:
1396 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1397 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001398 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001399 break;
1400 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001401 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001402 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001403 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1404 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001405 break;
1406 default:
1407 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 }
1409
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001410 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001411
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001412 // the read rect is viewport-relative
1413 GrGLIRect readRect;
1414 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001415
1416 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1417 if (0 == rowBytes) {
1418 rowBytes = tightRowBytes;
1419 }
1420 size_t readDstRowBytes = tightRowBytes;
1421 void* readDst = buffer;
1422
1423 // determine if GL can read using the passed rowBytes or if we need
1424 // a scratch buffer.
1425 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1426 if (rowBytes != tightRowBytes) {
1427 if (kDesktop_GrGLBinding == this->glBinding()) {
1428 GrAssert(!(rowBytes % sizeof(GrColor)));
1429 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1430 readDstRowBytes = rowBytes;
1431 } else {
1432 scratch.reset(tightRowBytes * height);
1433 readDst = scratch.get();
1434 }
1435 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001436 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1437 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001438 format, type, readDst));
1439 if (readDstRowBytes != tightRowBytes) {
1440 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1441 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001442
1443 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001444 // API presents top-to-bottom. We must preserve the padding contents. Note
1445 // that the above readPixels did not overwrite the padding.
1446 if (readDst == buffer) {
1447 GrAssert(rowBytes == readDstRowBytes);
1448 scratch.reset(tightRowBytes);
1449 void* tmpRow = scratch.get();
1450 // flip y in-place by rows
reed@google.comac10a2d2010-12-22 21:39:39 +00001451 const int halfY = height >> 1;
1452 char* top = reinterpret_cast<char*>(buffer);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001453 char* bottom = top + (height - 1) * rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001454 for (int y = 0; y < halfY; y++) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001455 memcpy(tmpRow, top, tightRowBytes);
1456 memcpy(top, bottom, tightRowBytes);
1457 memcpy(bottom, tmpRow, tightRowBytes);
1458 top += rowBytes;
1459 bottom -= rowBytes;
1460 }
1461 } else {
1462 GrAssert(readDst != buffer);
1463 // copy from readDst to buffer while flipping y
1464 const int halfY = height >> 1;
1465 const char* src = reinterpret_cast<const char*>(readDst);
1466 char* dst = reinterpret_cast<char*>(buffer) + (height-1) * rowBytes;
1467 for (int y = 0; y < height; y++) {
1468 memcpy(dst, src, tightRowBytes);
1469 src += readDstRowBytes;
1470 dst -= rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001471 }
1472 }
1473 return true;
1474}
1475
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001476void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001477
1478 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1479
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001480 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001481 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001482 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001483 #if GR_COLLECT_STATS
1484 ++fStats.fRenderTargetChngCnt;
1485 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001486 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001487 GrGLenum status;
1488 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001489 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001490 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 }
1492 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001493 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001494 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001495 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001496 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001497 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001498 fHWBounds.fViewportRect = vp;
1499 }
1500 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001501 if (NULL == bound || !bound->isEmpty()) {
1502 rt->flagAsNeedingResolve(bound);
1503 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001504}
1505
twiz@google.com0f31ca72011-03-18 17:38:11 +00001506GrGLenum gPrimitiveType2GLMode[] = {
1507 GR_GL_TRIANGLES,
1508 GR_GL_TRIANGLE_STRIP,
1509 GR_GL_TRIANGLE_FAN,
1510 GR_GL_POINTS,
1511 GR_GL_LINES,
1512 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001513};
1514
bsalomon@google.comd302f142011-03-03 13:54:13 +00001515#define SWAP_PER_DRAW 0
1516
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001517#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001518 #if GR_MAC_BUILD
1519 #include <AGL/agl.h>
1520 #elif GR_WIN32_BUILD
1521 void SwapBuf() {
1522 DWORD procID = GetCurrentProcessId();
1523 HWND hwnd = GetTopWindow(GetDesktopWindow());
1524 while(hwnd) {
1525 DWORD wndProcID = 0;
1526 GetWindowThreadProcessId(hwnd, &wndProcID);
1527 if(wndProcID == procID) {
1528 SwapBuffers(GetDC(hwnd));
1529 }
1530 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1531 }
1532 }
1533 #endif
1534#endif
1535
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001536void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1537 uint32_t startVertex,
1538 uint32_t startIndex,
1539 uint32_t vertexCount,
1540 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001541 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1542
twiz@google.com0f31ca72011-03-18 17:38:11 +00001543 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001544
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001545 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1546 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1547
1548 // our setupGeometry better have adjusted this to zero since
1549 // DrawElements always draws from the begining of the arrays for idx 0.
1550 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001551
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001552 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1553 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001554#if SWAP_PER_DRAW
1555 glFlush();
1556 #if GR_MAC_BUILD
1557 aglSwapBuffers(aglGetCurrentContext());
1558 int set_a_break_pt_here = 9;
1559 aglSwapBuffers(aglGetCurrentContext());
1560 #elif GR_WIN32_BUILD
1561 SwapBuf();
1562 int set_a_break_pt_here = 9;
1563 SwapBuf();
1564 #endif
1565#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001566}
1567
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001568void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1569 uint32_t startVertex,
1570 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001571 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1572
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001573 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1574
1575 // our setupGeometry better have adjusted this to zero.
1576 // DrawElements doesn't take an offset so we always adjus the startVertex.
1577 GrAssert(0 == startVertex);
1578
1579 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1580 // account for startVertex in the DrawElements case. So we always
1581 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001582 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001583#if SWAP_PER_DRAW
1584 glFlush();
1585 #if GR_MAC_BUILD
1586 aglSwapBuffers(aglGetCurrentContext());
1587 int set_a_break_pt_here = 9;
1588 aglSwapBuffers(aglGetCurrentContext());
1589 #elif GR_WIN32_BUILD
1590 SwapBuf();
1591 int set_a_break_pt_here = 9;
1592 SwapBuf();
1593 #endif
1594#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001595}
1596
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001597void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001598
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001599 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001600 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001601 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001602 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1603 rt->renderFBOID()));
1604 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1605 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001606 #if GR_COLLECT_STATS
1607 ++fStats.fRenderTargetChngCnt;
1608 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001609 // make sure we go through flushRenderTarget() since we've modified
1610 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001611 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001612 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001613 const GrIRect dirtyRect = rt->getResolveRect();
1614 GrGLIRect r;
1615 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1616 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001617
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001618 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001619 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001620 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1621 GL_CALL(Scissor(r.fLeft, r.fBottom,
1622 r.fWidth, r.fHeight));
1623 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001624 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001625 fHWBounds.fScissorEnabled = true;
1626 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001627 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001628 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001629 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1630 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001631 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001632 int right = r.fLeft + r.fWidth;
1633 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001634 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1635 r.fLeft, r.fBottom, right, top,
1636 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001637 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001638 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001639 }
1640}
1641
twiz@google.com0f31ca72011-03-18 17:38:11 +00001642static const GrGLenum grToGLStencilFunc[] = {
1643 GR_GL_ALWAYS, // kAlways_StencilFunc
1644 GR_GL_NEVER, // kNever_StencilFunc
1645 GR_GL_GREATER, // kGreater_StencilFunc
1646 GR_GL_GEQUAL, // kGEqual_StencilFunc
1647 GR_GL_LESS, // kLess_StencilFunc
1648 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1649 GR_GL_EQUAL, // kEqual_StencilFunc,
1650 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001651};
1652GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1653GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1654GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1655GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1656GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1657GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1658GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1659GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1660GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1661
twiz@google.com0f31ca72011-03-18 17:38:11 +00001662static const GrGLenum grToGLStencilOp[] = {
1663 GR_GL_KEEP, // kKeep_StencilOp
1664 GR_GL_REPLACE, // kReplace_StencilOp
1665 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1666 GR_GL_INCR, // kIncClamp_StencilOp
1667 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1668 GR_GL_DECR, // kDecClamp_StencilOp
1669 GR_GL_ZERO, // kZero_StencilOp
1670 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001671};
1672GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1673GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1674GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1675GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1676GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1677GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1678GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1679GR_STATIC_ASSERT(6 == kZero_StencilOp);
1680GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1681
reed@google.comac10a2d2010-12-22 21:39:39 +00001682void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001683 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001684
1685 // use stencil for clipping if clipping is enabled and the clip
1686 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001687 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001688 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001689 bool stencilChange = fHWStencilClip != stencilClip ||
1690 fHWDrawState.fStencilSettings != *settings ||
1691 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1692 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001693
1694 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001695
bsalomon@google.comd302f142011-03-03 13:54:13 +00001696 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1697 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001698
bsalomon@google.comd302f142011-03-03 13:54:13 +00001699 if (settings->isDisabled()) {
1700 if (stencilClip) {
1701 settings = &gClipStencilSettings;
1702 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001704
1705 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001706 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001707 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001708 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001709 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001710 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001711 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1712 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1713 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1714 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1715 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1716 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1717 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1718 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1719 }
1720 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001721 int stencilBits = 0;
1722 GrStencilBuffer* stencilBuffer =
1723 fCurrDrawState.fRenderTarget->getStencilBuffer();
1724 if (NULL != stencilBuffer) {
1725 stencilBits = stencilBuffer->bits();
1726 }
1727 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001728 GrAssert(stencilBits ||
1729 (GrStencilSettings::gDisabled ==
1730 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001731 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1732 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001733
1734 unsigned int frontRef = settings->fFrontFuncRef;
1735 unsigned int frontMask = settings->fFrontFuncMask;
1736 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001737 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001738
1739 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1740
1741 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1742 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1743 } else {
1744 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1745
1746 ConvertStencilFuncAndMask(settings->fFrontFunc,
1747 stencilClip,
1748 clipStencilMask,
1749 userStencilMask,
1750 &frontRef,
1751 &frontMask);
1752 frontWriteMask &= userStencilMask;
1753 }
1754 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001755 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001756 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001757 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001758 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001759 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001760 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001761 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001762 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001763 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001764
1765 unsigned int backRef = settings->fBackFuncRef;
1766 unsigned int backMask = settings->fBackFuncMask;
1767 unsigned int backWriteMask = settings->fBackWriteMask;
1768
1769
1770 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1771 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1772 backFunc = grToGLStencilFunc[settings->fBackFunc];
1773 } else {
1774 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1775 ConvertStencilFuncAndMask(settings->fBackFunc,
1776 stencilClip,
1777 clipStencilMask,
1778 userStencilMask,
1779 &backRef,
1780 &backMask);
1781 backWriteMask &= userStencilMask;
1782 }
1783
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001784 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1785 frontRef, frontMask));
1786 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1787 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1788 backRef, backMask));
1789 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1790 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1791 grToGLStencilOp[settings->fFrontFailOp],
1792 grToGLStencilOp[settings->fFrontPassOp],
1793 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001794
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001795 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1796 grToGLStencilOp[settings->fBackFailOp],
1797 grToGLStencilOp[settings->fBackPassOp],
1798 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001799 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001800 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1801 GL_CALL(StencilMask(frontWriteMask));
1802 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803 grToGLStencilOp[settings->fFrontPassOp],
1804 grToGLStencilOp[settings->fFrontPassOp]));
1805 }
1806 }
1807 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001808 fHWStencilClip = stencilClip;
1809 }
1810}
1811
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001812void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001813 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001814 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1815 // smooth lines.
1816
1817 // we prefer smooth lines over multisampled lines
1818 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001819 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001820 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001821 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001822 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001823 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001824 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001825 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001826 fHWAAState.fSmoothLineEnabled = false;
1827 }
1828 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1829 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001830 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001831 fHWAAState.fMSAAEnabled = false;
1832 }
1833 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001834 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001835 fHWAAState.fMSAAEnabled) {
1836 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001837 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001838 fHWAAState.fMSAAEnabled = false;
1839 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001840 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001841 fHWAAState.fMSAAEnabled = true;
1842 }
1843 }
1844 }
1845}
1846
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001847void GrGpuGL::flushBlend(GrPrimitiveType type,
1848 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001849 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001850 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001851 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001852 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001853 fHWBlendDisabled = false;
1854 }
1855 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1856 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001857 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1858 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001859 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1860 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1861 }
1862 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001863 // any optimization to disable blending should
1864 // have already been applied and tweaked the coeffs
1865 // to (1, 0).
1866 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1867 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001868 if (fHWBlendDisabled != blendOff) {
1869 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001870 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001871 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001872 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001873 }
1874 fHWBlendDisabled = blendOff;
1875 }
1876 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001877 if (fHWDrawState.fSrcBlend != srcCoeff ||
1878 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001879 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1880 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001881 fHWDrawState.fSrcBlend = srcCoeff;
1882 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001883 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001884 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1885 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001886 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1887
1888 float c[] = {
1889 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1890 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1891 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1892 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1893 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001894 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001895 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1896 }
1897 }
1898 }
1899}
1900
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001901static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1902 switch (filter) {
1903 case GrSamplerState::kBilinear_Filter:
1904 case GrSamplerState::k4x4Downsample_Filter:
1905 return GR_GL_LINEAR;
1906 case GrSamplerState::kNearest_Filter:
1907 case GrSamplerState::kConvolution_Filter:
1908 return GR_GL_NEAREST;
1909 default:
1910 GrAssert(!"Unknown filter type");
1911 return GR_GL_LINEAR;
1912 }
1913}
1914
bsalomon@google.comffca4002011-02-22 20:34:01 +00001915bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001916
1917 // GrGpu::setupClipAndFlushState should have already checked this
1918 // and bailed if not true.
1919 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001920
tomhudson@google.com93813632011-10-27 20:21:16 +00001921 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001922 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001923 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001924 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001925
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001926 // true for now, but maybe not with GrEffect.
1927 GrAssert(NULL != nextTexture);
1928 // if we created a rt/tex and rendered to it without using a
1929 // texture and now we're texuring from the rt it will still be
1930 // the last bound texture, but it needs resolving. So keep this
1931 // out of the "last != next" check.
1932 GrGLRenderTarget* texRT =
1933 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1934 if (NULL != texRT) {
1935 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001936 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001937
1938 if (fHWDrawState.fTextures[s] != nextTexture) {
1939 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001940 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001941 #if GR_COLLECT_STATS
1942 ++fStats.fTextureChngCnt;
1943 #endif
1944 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1945 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001946 // The texture matrix has to compensate for texture width/height
1947 // and NPOT-embedded-in-POT
1948 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001949 }
1950
1951 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001952 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001953 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001954 nextTexture->getCachedTexParams(&timestamp);
1955 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001956 GrGLTexture::TexParams newTexParams;
1957
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001958 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001959
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001960 const GrGLenum* wraps =
1961 GrGLTexture::WrapMode2GLWrap(this->glBinding());
1962 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1963 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001964 if (setAll) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001965 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001966 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1967 GR_GL_TEXTURE_MAG_FILTER,
1968 newTexParams.fFilter));
1969 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1970 GR_GL_TEXTURE_MIN_FILTER,
1971 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001972 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1973 GR_GL_TEXTURE_WRAP_S,
1974 newTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001975 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1976 GR_GL_TEXTURE_WRAP_T,
1977 newTexParams.fWrapT));
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001978 } else {
1979 if (newTexParams.fFilter != oldTexParams.fFilter) {
1980 setTextureUnit(s);
1981 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1982 GR_GL_TEXTURE_MAG_FILTER,
1983 newTexParams.fFilter));
1984 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1985 GR_GL_TEXTURE_MIN_FILTER,
1986 newTexParams.fFilter));
1987 }
1988 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
1989 setTextureUnit(s);
1990 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1991 GR_GL_TEXTURE_WRAP_S,
1992 newTexParams.fWrapS));
1993 }
1994 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
1995 setTextureUnit(s);
1996 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1997 GR_GL_TEXTURE_WRAP_T,
1998 newTexParams.fWrapT));
1999 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002000 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002001 nextTexture->setCachedTexParams(newTexParams,
2002 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002003 }
2004 }
2005
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002006 GrIRect* rect = NULL;
2007 GrIRect clipBounds;
2008 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2009 fClip.hasConservativeBounds()) {
2010 fClip.getConservativeBounds().roundOut(&clipBounds);
2011 rect = &clipBounds;
2012 }
2013 this->flushRenderTarget(rect);
2014 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002015
reed@google.comac10a2d2010-12-22 21:39:39 +00002016 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2017 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2018 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002019 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002020 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002021 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002022 }
2023 }
2024
bsalomon@google.comd302f142011-03-03 13:54:13 +00002025 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2026 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002027 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002028 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002029 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002030 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002031 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002032 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002033 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002034 }
2035
bsalomon@google.comd302f142011-03-03 13:54:13 +00002036 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2037 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002038 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002039 GL_CALL(Enable(GR_GL_CULL_FACE));
2040 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002041 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002042 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002043 GL_CALL(Enable(GR_GL_CULL_FACE));
2044 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002045 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002046 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002047 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002048 break;
2049 default:
2050 GrCrash("Unknown draw face.");
2051 }
2052 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2053 }
2054
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002055#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002056 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002057 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002058 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002059 NULL == fCurrDrawState.fRenderTarget ||
2060 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002061 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002062 fCurrDrawState.fRenderTarget);
2063 }
2064#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002065
reed@google.comac10a2d2010-12-22 21:39:39 +00002066 flushStencil();
2067
bsalomon@google.comd302f142011-03-03 13:54:13 +00002068 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002069 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002070 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002071}
2072
2073void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002074 if (fHWGeometryState.fVertexBuffer != buffer) {
2075 fHWGeometryState.fArrayPtrsDirty = true;
2076 fHWGeometryState.fVertexBuffer = buffer;
2077 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002078}
2079
2080void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002081 if (fHWGeometryState.fVertexBuffer == buffer) {
2082 // deleting bound buffer does implied bind to 0
2083 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002084 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002085 }
2086}
2087
2088void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002089 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002090}
2091
2092void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002093 if (fHWGeometryState.fIndexBuffer == buffer) {
2094 // deleting bound buffer does implied bind to 0
2095 fHWGeometryState.fIndexBuffer = NULL;
2096 }
2097}
2098
reed@google.comac10a2d2010-12-22 21:39:39 +00002099void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2100 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002101 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002102 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002103 }
2104 if (fHWDrawState.fRenderTarget == renderTarget) {
2105 fHWDrawState.fRenderTarget = NULL;
2106 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002107}
2108
2109void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002110 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002111 if (fCurrDrawState.fTextures[s] == texture) {
2112 fCurrDrawState.fTextures[s] = NULL;
2113 }
2114 if (fHWDrawState.fTextures[s] == texture) {
2115 // deleting bound texture does implied bind to 0
2116 fHWDrawState.fTextures[s] = NULL;
2117 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002118 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002119}
2120
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002121bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002122 GrGLenum* internalFormat,
2123 GrGLenum* format,
2124 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002125 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002126 case kRGBA_8888_GrPixelConfig:
2127 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002128 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002129 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002130 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2131 // format for a BGRA is BGRA not RGBA (as on desktop)
2132 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2133 } else {
2134 *internalFormat = GR_GL_RGBA;
2135 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002136 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002137 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002138 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002139 *format = GR_GL_RGB;
2140 *internalFormat = GR_GL_RGB;
2141 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002142 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002143 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002144 *format = GR_GL_RGBA;
2145 *internalFormat = GR_GL_RGBA;
2146 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002147 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002148 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002149 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002150 *format = GR_GL_PALETTE8_RGBA8;
2151 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002152 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002153 } else {
2154 return false;
2155 }
2156 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002157 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002158 *format = GR_GL_ALPHA;
2159 *internalFormat = GR_GL_ALPHA;
2160 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002161 break;
2162 default:
2163 return false;
2164 }
2165 return true;
2166}
2167
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002168void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002169 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002170 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002171 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002172 fActiveTextureUnitIdx = unit;
2173 }
2174}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002175
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002176void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002177 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002178 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002179 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2180 }
2181}
2182
reed@google.comac10a2d2010-12-22 21:39:39 +00002183/* On ES the internalFormat and format must match for TexImage and we use
2184 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2185 decide the internalFormat. However, on ES internalFormat for
2186 RenderBufferStorage* has to be a specific format (not a base format like
2187 GL_RGBA).
2188 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002189bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002190 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002191 case kRGBA_8888_GrPixelConfig:
2192 case kRGBX_8888_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002193 if (fGLCaps.fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002194 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002195 return true;
2196 } else {
2197 return false;
2198 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002199 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002200 // ES2 supports 565. ES1 supports it
2201 // with FBO extension desktop GL has
2202 // no such internal format
2203 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002204 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002205 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002206 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002207 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002208 return true;
2209 default:
2210 return false;
2211 }
2212}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002213
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002214void GrGpuGL::resetDirtyFlags() {
2215 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2216}
2217
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002218void GrGpuGL::setBuffers(bool indexed,
2219 int* extraVertexOffset,
2220 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002221
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002222 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002223
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002224 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2225
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002226 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002227 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002228 case kBuffer_GeometrySrcType:
2229 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002230 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002231 break;
2232 case kArray_GeometrySrcType:
2233 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002234 this->finalizeReservedVertices();
2235 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2236 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002237 break;
2238 default:
2239 vbuf = NULL; // suppress warning
2240 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002241 }
2242
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002243 GrAssert(NULL != vbuf);
2244 GrAssert(!vbuf->isLocked());
2245 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002246 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002247 fHWGeometryState.fArrayPtrsDirty = true;
2248 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002249 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002250
2251 if (indexed) {
2252 GrAssert(NULL != extraIndexOffset);
2253
2254 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002255 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002256 case kBuffer_GeometrySrcType:
2257 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002258 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002259 break;
2260 case kArray_GeometrySrcType:
2261 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002262 this->finalizeReservedIndices();
2263 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2264 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002265 break;
2266 default:
2267 ibuf = NULL; // suppress warning
2268 GrCrash("Unknown geometry src type!");
2269 }
2270
2271 GrAssert(NULL != ibuf);
2272 GrAssert(!ibuf->isLocked());
2273 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002274 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002275 fHWGeometryState.fIndexBuffer = ibuf;
2276 }
2277 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002278}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002279
2280int GrGpuGL::getMaxEdges() const {
2281 // FIXME: This is a pessimistic estimate based on how many other things
2282 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002283 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2284 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002285}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002286
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002287void GrGpuGL::GLCaps::print() const {
2288 for (int i = 0; i < fStencilFormats.count(); ++i) {
2289 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2290 i,
2291 fStencilFormats[i].fStencilBits,
2292 fStencilFormats[i].fTotalBits);
2293 }
2294
2295 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2296 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2297 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2298 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2299 static const char* gMSFBOExtStr[] = {
2300 "None",
2301 "ARB",
2302 "EXT",
2303 "Apple",
2304 };
2305 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002306 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002307 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2308 }
2309 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2310 GrPrintf("Support RGBA8 Render Buffer: %s\n",
2311 (fRGBA8Renderbuffer ? "YES": "NO"));
2312}