blob: 9025c675f6b5440a3188a51cbda274f5b3017b9d [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);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000107 GrGLTexture::Orientation orientation = texture->orientation();
108 if (GrGLTexture::kBottomUp_Orientation == orientation) {
109 GrMatrix invY;
110 invY.setAll(GR_Scalar1, 0, 0,
111 0, -GR_Scalar1, GR_Scalar1,
112 0, 0, GrMatrix::I()[8]);
113 matrix->postConcat(invY);
114 } else {
115 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
116 }
117}
118
bsalomon@google.comd302f142011-03-03 13:54:13 +0000119bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120 const GrSamplerState& sampler) {
121 GrAssert(NULL != texture);
122 if (!sampler.getMatrix().isIdentity()) {
123 return false;
124 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000125 GrGLTexture::Orientation orientation = texture->orientation();
126 if (GrGLTexture::kBottomUp_Orientation == orientation) {
127 return false;
128 } else {
129 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
130 }
131 return true;
132}
133
134///////////////////////////////////////////////////////////////////////////////
135
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000136static bool gPrintStartupSpew;
137
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000139
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000141
twiz@google.com0f31ca72011-03-18 17:38:11 +0000142 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
144 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000145 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000146 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
147 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000148 // some implementations require texture to be mip-map complete before
149 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000150 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
151 GR_GL_TEXTURE_MIN_FILTER,
152 GR_GL_NEAREST));
153 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
154 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
155 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
156 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
157 GR_GL_COLOR_ATTACHMENT0,
158 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000159 GrGLenum status;
160 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
162 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000163
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000165}
166
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000167GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000168
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000169 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000170
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 gl->ref();
172 fGL = gl;
173 fGLBinding = glBinding;
174 switch (glBinding) {
175 case kDesktop_GrGLBinding:
176 GrAssert(gl->supportsDesktop());
177 break;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000178 case kES2_GrGLBinding:
179 GrAssert(gl->supportsES2());
180 break;
181 default:
182 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000183 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185 GrGLClearErr(fGL);
186
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000187 const GrGLubyte* ext;
188 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000189 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000190 const GrGLubyte* vendor;
191 const GrGLubyte* renderer;
192 const GrGLubyte* version;
193 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
194 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
195 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000196 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
197 this);
198 GrPrintf("------ VENDOR %s\n", vendor);
199 GrPrintf("------ RENDERER %s\n", renderer);
200 GrPrintf("------ VERSION %s\n", version);
201 GrPrintf("------ EXTENSIONS\n %s \n", ext);
202 }
203
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000204 fGLVersion = GrGLGetVersion(gl);
205 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000206 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000208 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000209
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000211
bsalomon@google.comfe676522011-06-17 18:12:21 +0000212 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213}
214
215GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000216 // This must be called by before the GrDrawTarget destructor
217 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000218 // This subclass must do this before the base class destructor runs
219 // since we will unref the GrGLInterface.
220 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000221 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000222}
223
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000224///////////////////////////////////////////////////////////////////////////////
225
226static const GrGLuint kUnknownBitCount = ~0;
227
228void GrGpuGL::initCaps() {
229 GrGLint maxTextureUnits;
230 // check FS and fixed-function texture unit limits
231 // we only use textures in the fragment stage currently.
232 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000233 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
234 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 if (kES2_GrGLBinding != this->glBinding()) {
236 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000237 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 }
239 if (kES2_GrGLBinding == this->glBinding()) {
240 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
241 &fGLCaps.fMaxFragmentUniformVectors);
242 } else if (kDesktop_GrGLBinding != this->glBinding()) {
243 GrGLint max;
244 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
245 fGLCaps.fMaxFragmentUniformVectors = max / 4;
246 } else {
247 fGLCaps.fMaxFragmentUniformVectors = 16;
248 }
249
250 GrGLint numFormats;
251 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
252 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
253 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
254 for (int i = 0; i < numFormats; ++i) {
255 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
256 fCaps.f8BitPaletteSupport = true;
257 break;
258 }
259 }
260
261 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 // we could also look for GL_ATI_separate_stencil extension or
263 // GL_EXT_stencil_two_side but they use different function signatures
264 // than GL2.0+ (and than each other).
265 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
266 // supported on GL 1.4 and higher or by extension
267 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
268 this->hasExtension("GL_EXT_stencil_wrap");
269 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000270 // ES 2 has two sided stencil and stencil wrap
271 fCaps.fTwoSidedStencilSupport = true;
272 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000273 }
274
275 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000276 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000277 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000278 fGLCaps.fRGBA8RenderbufferSupport =
279 this->hasExtension("GL_OES_rgb8_rgba8") ||
280 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000281 }
282
283
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000285 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
286 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 } else {
288 bool hasBGRAExt = false;
289 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000290 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000292 fGLCaps.fBGRAFormatSupport = true;
293 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000294 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000295 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000297 }
298
299 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000300 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000301 this->hasExtension("GL_ARB_texture_swizzle");
302 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000303 fGLCaps.fTextureSwizzleSupport = false;
304 }
305
306 if (kDesktop_GrGLBinding == this->glBinding()) {
307 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000308 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000309 fGLCaps.fPackRowLengthSupport = true;
310 } else {
311 fGLCaps.fUnpackRowLengthSupport = this->hasExtension("GL_EXT_unpack_subimage");
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000312 fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy");
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000313 // no extension for pack row length
314 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000315 }
316
317 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000318 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
319 // extension includes glMapBuffer.
320 } else {
321 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
322 }
323
324 if (kDesktop_GrGLBinding == this->glBinding()) {
325 if (fGLVersion >= GR_GL_VER(2,0) ||
326 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
327 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000328 } else {
329 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000330 }
331 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000332 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000333 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000334 }
335
336 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
337
338 ////////////////////////////////////////////////////////////////////////////
339 // Experiments to determine limitations that can't be queried.
340 // TODO: Make these a preprocess that generate some compile time constants.
341 // TODO: probe once at startup, rather than once per context creation.
342
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000343 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
344 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
345 // Our render targets are always created with textures as the color
346 // attachment, hence this min:
347 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
348
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000349 this->initFSAASupport();
350 this->initStencilFormats();
351}
352
353void GrGpuGL::initFSAASupport() {
354 // TODO: Get rid of GrAALevel and use # samples directly.
355 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
356 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
357 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
358 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
359 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
360
361 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
362 if (kDesktop_GrGLBinding != this->glBinding()) {
363 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
364 // chrome's extension is equivalent to the EXT msaa
365 // and fbo_blit extensions.
366 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
367 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
368 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
369 }
370 } else {
371 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
372 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
373 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
374 this->hasExtension("GL_EXT_framebuffer_blit")) {
375 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
376 }
377 }
378
379 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
380 GrGLint maxSamples;
381 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
382 if (maxSamples > 1 ) {
383 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
384 fGLCaps.fAASamples[kLow_GrAALevel] =
385 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
386 fGLCaps.fAASamples[kMed_GrAALevel] =
387 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
388 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
389 }
390 }
391 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
392}
393
394void GrGpuGL::initStencilFormats() {
395
396 // Build up list of legal stencil formats (though perhaps not supported on
397 // the particular gpu/driver) from most preferred to least.
398
399 // these consts are in order of most preferred to least preferred
400 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
401 static const GrGLStencilBuffer::Format
402 // internal Format stencil bits total bits packed?
403 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
404 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
405 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
406 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
407 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
408 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
409
410 if (kDesktop_GrGLBinding == this->glBinding()) {
411 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
412 this->hasExtension("GL_EXT_packed_depth_stencil") ||
413 this->hasExtension("GL_ARB_framebuffer_object");
414
415 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
416 // require FBO support we can expect these are legal formats and don't
417 // check. These also all support the unsized GL_STENCIL_INDEX.
418 fGLCaps.fStencilFormats.push_back() = gS8;
419 fGLCaps.fStencilFormats.push_back() = gS16;
420 if (supportsPackedDS) {
421 fGLCaps.fStencilFormats.push_back() = gD24S8;
422 }
423 fGLCaps.fStencilFormats.push_back() = gS4;
424 if (supportsPackedDS) {
425 fGLCaps.fStencilFormats.push_back() = gDS;
426 }
427 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000428 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
429 // for other formats.
430 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000431
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000432 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000433 //fStencilFormats.push_back() = gS16;
434 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
435 fGLCaps.fStencilFormats.push_back() = gD24S8;
436 }
437 if (this->hasExtension("GL_OES_stencil4")) {
438 fGLCaps.fStencilFormats.push_back() = gS4;
439 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000440 }
441}
442
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000443GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000444 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
445 return GrPixelConfigSwapRAndB(config);
446 } else {
447 return config;
448 }
449}
450
451GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) {
452 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000453 return GrPixelConfigSwapRAndB(config);
454 } else {
455 return config;
456 }
457}
458
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000459void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000460 if (gPrintStartupSpew && !fPrintedCaps) {
461 fPrintedCaps = true;
462 this->getCaps().print();
463 fGLCaps.print();
464 }
465
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000466 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000467 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000468 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000469
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000470 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000471 GL_CALL(Disable(GR_GL_DEPTH_TEST));
472 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000473
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000474 GL_CALL(Disable(GR_GL_CULL_FACE));
475 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000476 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000478 GL_CALL(Disable(GR_GL_DITHER));
479 if (kDesktop_GrGLBinding == this->glBinding()) {
480 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
481 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
482 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000483 fHWAAState.fMSAAEnabled = false;
484 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000485 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000486
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000487 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000488 fHWDrawState.fFlagBits = 0;
489
reed@google.comac10a2d2010-12-22 21:39:39 +0000490 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000491 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000492
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000493 // invalid
494 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 // illegal values
tomhudson@google.com62b09682011-11-09 16:39:17 +0000497 //fHWDrawState.fSrcBlend = (GrBlendCoeff)(uint8_t)-1;
498 fHWDrawState.fSrcBlend = (GrBlendCoeff)0xFF;
499 fHWDrawState.fDstBlend = (GrBlendCoeff)(uint8_t)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000500
501 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000502 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000503
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000505
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000506 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000507
tomhudson@google.com93813632011-10-27 20:21:16 +0000508 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000509 fHWDrawState.fTextures[s] = NULL;
510 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
511 -GR_ScalarMax,
512 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000513 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000514 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000515 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000516
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000517 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000518 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000519 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000520 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000521
bsalomon@google.comd302f142011-03-03 13:54:13 +0000522 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000523 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000524 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000525
526 fHWGeometryState.fIndexBuffer = NULL;
527 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000528
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000529 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000531 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000533
534 // we assume these values
535 if (this->glCaps().fUnpackRowLengthSupport) {
536 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
537 }
538 if (this->glCaps().fPackRowLengthSupport) {
539 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
540 }
541 if (this->glCaps().fUnpackFlipYSupport) {
542 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
543 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000544}
545
bsalomon@google.come269f212011-11-07 13:29:52 +0000546GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000547 GrGLenum dontCare;
bsalomon@google.come269f212011-11-07 13:29:52 +0000548 GrGLTexture::Desc glTexDesc;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000549 if (!this->canBeTexture(desc.fConfig, &glTexDesc.fInternalFormat,
550 &dontCare, &dontCare)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000551 return NULL;
552 }
553
bsalomon@google.com99621082011-11-15 16:47:16 +0000554 glTexDesc.fWidth = desc.fWidth;
555 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000556 glTexDesc.fConfig = desc.fConfig;
557 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
558 glTexDesc.fOwnsID = false;
559 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
560
561 GrGLTexture* texture = NULL;
562 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
563 GrGLRenderTarget::Desc glRTDesc;
564 glRTDesc.fRTFBOID = 0;
565 glRTDesc.fTexFBOID = 0;
566 glRTDesc.fMSColorRenderbufferID = 0;
567 glRTDesc.fOwnIDs = true;
568 glRTDesc.fConfig = desc.fConfig;
569 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000570 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
571 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000572 glTexDesc.fTextureID,
573 &glRTDesc)) {
574 return NULL;
575 }
576 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
577 } else {
578 texture = new GrGLTexture(this, glTexDesc);
579 }
580 if (NULL == texture) {
581 return NULL;
582 }
583
584 this->setSpareTextureUnit();
585 return texture;
586}
587
588GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
589 GrGLRenderTarget::Desc glDesc;
590 glDesc.fConfig = desc.fConfig;
591 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
592 glDesc.fMSColorRenderbufferID = 0;
593 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
594 glDesc.fSampleCnt = desc.fSampleCnt;
595 glDesc.fOwnIDs = false;
596 GrGLIRect viewport;
597 viewport.fLeft = 0;
598 viewport.fBottom = 0;
599 viewport.fWidth = desc.fWidth;
600 viewport.fHeight = desc.fHeight;
601
602 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
603 if (desc.fStencilBits) {
604 GrGLStencilBuffer::Format format;
605 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
606 format.fPacked = false;
607 format.fStencilBits = desc.fStencilBits;
608 format.fTotalBits = desc.fStencilBits;
609 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
610 0,
611 desc.fWidth,
612 desc.fHeight,
613 desc.fSampleCnt,
614 format);
615 tgt->setStencilBuffer(sb);
616 sb->unref();
617 }
618 return tgt;
619}
620
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000621GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
622
623 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
624 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
625 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
626 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
627
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000628 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000629 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000630
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000631 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000632 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000633 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000634 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000635 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000636 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000637 } else {
638 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000639 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000640 }
641 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000642 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000643 }
644 // we don't know what the RB ids are without glGets and we don't care
645 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000646 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000647 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000648 if (desc.fStencilBits) {
649 GrGLStencilBuffer::Format format;
650 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
651 format.fPacked = false;
652 format.fStencilBits = desc.fStencilBits;
653 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000654 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
655 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000656 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000657 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000658 }
659
660 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000661 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000662 GrGLenum dontCare;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000663 if (!canBeTexture(desc.fConfig, &texDesc.fInternalFormat,
664 &dontCare, &dontCare)) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000665 return NULL;
666 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000667 texDesc.fWidth = desc.fWidth;
668 texDesc.fHeight = desc.fHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000669
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000670 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000671 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000672 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000673 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000674
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000675 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000676 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000677 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000678 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000679 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000680 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000681 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000682 } else {
683 GrGLIRect viewport;
684 viewport.fLeft = 0;
685 viewport.fBottom = 0;
686 viewport.fWidth = desc.fWidth;
687 viewport.fHeight = desc.fHeight;
688
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000689 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000690 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000691 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000692 }
693}
694
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000695
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000696////////////////////////////////////////////////////////////////////////////////
697
bsalomon@google.com6f379512011-11-16 20:36:03 +0000698void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
699 int left, int top, int width, int height,
700 GrPixelConfig config, const void* buffer,
701 size_t rowBytes) {
702 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000703
bsalomon@google.com6f379512011-11-16 20:36:03 +0000704 this->setSpareTextureUnit();
705 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
706 GrGLTexture::Desc desc;
707 desc.fConfig = glTex->config();
708 desc.fWidth = glTex->width();
709 desc.fHeight = glTex->height();
710 desc.fOrientation = glTex->orientation();
711 desc.fTextureID = glTex->textureID();
712 desc.fInternalFormat = glTex->internalFormat();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000713
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000714 this->uploadTexData(desc, false,
715 left, top, width, height,
716 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000717}
718
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000719namespace {
720bool adjust_pixel_ops_params(int surfaceWidth,
721 int surfaceHeight,
722 size_t bpp,
723 int* left, int* top, int* width, int* height,
724 const void** data,
725 size_t* rowBytes) {
726 if (!*rowBytes) {
727 *rowBytes = *width * bpp;
728 }
729
730 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
731 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
732
733 if (!subRect.intersect(bounds)) {
734 return false;
735 }
736 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
737 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
738
739 *left = subRect.fLeft;
740 *top = subRect.fTop;
741 *width = subRect.width();
742 *height = subRect.height();
743 return true;
744}
745}
746
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000747bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
748 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000749 int left, int top, int width, int height,
750 GrPixelConfig dataConfig,
751 const void* data,
752 size_t rowBytes) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000753
754 size_t bpp = GrBytesPerPixel(dataConfig);
755 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
756 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000757 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000758 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000759 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000760
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000761 // in case we need a temporary, trimmed copy of the src pixels
762 SkAutoSMalloc<128 * 128> tempStorage;
763
bsalomon@google.com6f379512011-11-16 20:36:03 +0000764 GrGLenum dontCare;
765 GrGLenum externalFormat;
766 GrGLenum externalType;
767 if (!this->canBeTexture(dataConfig, &dontCare,
768 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000769 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000770 }
771
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000772 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000773 * check whether to allocate a temporary buffer for flipping y or
774 * because our srcData has extra bytes past each row. If so, we need
775 * to trim those off here, since GL ES may not let us specify
776 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000777 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000778 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000779 bool swFlipY = false;
780 bool glFlipY = false;
781
782 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
783 if (this->glCaps().fUnpackFlipYSupport) {
784 glFlipY = true;
785 } else {
786 swFlipY = true;
787 }
788 }
789 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000790 // can't use this for flipping, only non-neg values allowed. :(
791 if (rowBytes != trimRowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000792 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
793 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com6f379512011-11-16 20:36:03 +0000794 restoreGLRowLength = true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000795 }
796 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000797 if (trimRowBytes != rowBytes || swFlipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000798 // copy the data into our new storage, skipping the trailing bytes
bsalomon@google.com6f379512011-11-16 20:36:03 +0000799 size_t trimSize = height * trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000800 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000801 if (swFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000802 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000803 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000804 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000805 for (int y = 0; y < height; y++) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000806 memcpy(dst, src, trimRowBytes);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000807 if (swFlipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000808 src -= rowBytes;
809 } else {
810 src += rowBytes;
811 }
812 dst += trimRowBytes;
813 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000814 // now point data to our copied version
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000816 }
817 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000818 if (glFlipY) {
819 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
820 }
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000821 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000822 if (isNewTexture &&
823 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000824 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000825 GrGLClearErr(this->glInterface());
826 GR_GL_CALL_NOERRCHECK(this->glInterface(),
827 TexImage2D(GR_GL_TEXTURE_2D, 0,
828 desc.fInternalFormat,
829 desc.fWidth, desc.fHeight, 0,
830 externalFormat, externalType, data));
831 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
832 return false;
833 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000834 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000835 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000836 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000837 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000838 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, left, top, width, height,
839 externalFormat, externalType, data));
840 }
841
842 if (restoreGLRowLength) {
843 GrAssert(this->glCaps().fUnpackRowLengthSupport);
844 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000845 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000846 if (glFlipY) {
847 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
848 }
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000849 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000850}
851
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000852bool GrGpuGL::createRenderTargetObjects(int width, int height,
853 GrGLuint texID,
854 GrGLRenderTarget::Desc* desc) {
855 desc->fMSColorRenderbufferID = 0;
856 desc->fRTFBOID = 0;
857 desc->fTexFBOID = 0;
858 desc->fOwnIDs = true;
859
860 GrGLenum status;
861 GrGLint err;
862
bsalomon@google.comab15d612011-08-09 12:57:56 +0000863 GrGLenum msColorFormat = 0; // suppress warning
864
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000865 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866 if (!desc->fTexFBOID) {
867 goto FAILED;
868 }
869
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000870
871 // If we are using multisampling we will create two FBOS. We render
872 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000873 if (desc->fSampleCnt > 0) {
874 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
875 goto FAILED;
876 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000877 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
878 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000879 if (!desc->fRTFBOID ||
880 !desc->fMSColorRenderbufferID ||
881 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
882 goto FAILED;
883 }
884 } else {
885 desc->fRTFBOID = desc->fTexFBOID;
886 }
887
888 if (desc->fRTFBOID != desc->fTexFBOID) {
889 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000890 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000891 desc->fMSColorRenderbufferID));
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000892 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000893 GR_GL_CALL_NOERRCHECK(this->glInterface(),
894 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
895 desc->fSampleCnt,
896 msColorFormat,
897 width, height));
898 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 if (err != GR_GL_NO_ERROR) {
900 goto FAILED;
901 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000902 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
903 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904 GR_GL_COLOR_ATTACHMENT0,
905 GR_GL_RENDERBUFFER,
906 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000907 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
909 goto FAILED;
910 }
911 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000912 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000914 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
915 GR_GL_COLOR_ATTACHMENT0,
916 GR_GL_TEXTURE_2D,
917 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000918 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
920 goto FAILED;
921 }
922
923 return true;
924
925FAILED:
926 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 }
929 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000930 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000931 }
932 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000933 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000934 }
935 return false;
936}
937
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000938// good to set a break-point here to know when createTexture fails
939static GrTexture* return_null_texture() {
940// GrAssert(!"null texture");
941 return NULL;
942}
943
944#if GR_DEBUG
945static size_t as_size_t(int x) {
946 return x;
947}
948#endif
949
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000950GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000951 const void* srcData,
952 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000953
954#if GR_COLLECT_STATS
955 ++fStats.fTextureCreateCnt;
956#endif
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;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000960 GrGLenum externalFormat;
961 GrGLenum externalType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000962
bsalomon@google.com99621082011-11-15 16:47:16 +0000963 glTexDesc.fWidth = desc.fWidth;
964 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000965 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000966 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000967
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000968 glRTDesc.fMSColorRenderbufferID = 0;
969 glRTDesc.fRTFBOID = 0;
970 glRTDesc.fTexFBOID = 0;
971 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000972 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000973
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000974 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000975 if (!canBeTexture(desc.fConfig,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000976 &glTexDesc.fInternalFormat,
977 &externalFormat,
978 &externalType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000979 return return_null_texture();
980 }
981
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000982 const Caps& caps = this->getCaps();
983
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000984 // We keep GrRenderTargets in GL's normal orientation so that they
985 // can be drawn to by the outside world without the client having
986 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000987 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000988 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000989
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000990 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
991 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
992 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
993 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000994 GrPrintf("AA RT requested but not supported on this platform.");
995 }
996
reed@google.comac10a2d2010-12-22 21:39:39 +0000997 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +0000998 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
999 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001000 return return_null_texture();
1001 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001002 }
1003
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001004 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001005 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001006 return return_null_texture();
1007 }
1008
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001009 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001010 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001011
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001012 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1013 // drivers have a bug where an FBO won't be complete if it includes a
1014 // texture that is not mipmap complete (considering the filter in use).
1015 GrGLTexture::TexParams initialTexParams;
1016 // we only set a subset here so invalidate first
1017 initialTexParams.invalidate();
1018 initialTexParams.fFilter = GR_GL_NEAREST;
1019 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1020 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001021 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1022 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001023 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001024 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1025 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001026 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1028 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001029 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001030 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1031 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001032 initialTexParams.fWrapT));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001033 if (NULL == srcData) {
1034 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, glTexDesc.fInternalFormat,
1035 glTexDesc.fWidth, glTexDesc.fHeight, 0,
1036 externalFormat, externalType, NULL));
1037 } else {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001038 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1039 glTexDesc.fWidth, glTexDesc.fHeight,
1040 desc.fConfig, srcData, rowBytes)) {
1041 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1042 return return_null_texture();
1043 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001044 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001045
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001046 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001047 if (renderTarget) {
1048#if GR_COLLECT_STATS
1049 ++fStats.fRenderTargetCreateCnt;
1050#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001051 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1052 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001053 glTexDesc.fTextureID,
1054 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001055 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 return return_null_texture();
1057 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001058 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001059 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001060 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001062 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001063#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001064 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1065 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001066#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001067 return tex;
1068}
1069
1070namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001071void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1072 GrGLuint rb,
1073 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 // we shouldn't ever know one size and not the other
1075 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1076 (kUnknownBitCount == format->fTotalBits));
1077 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001079 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1080 (GrGLint*)&format->fStencilBits);
1081 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001082 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001083 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1084 (GrGLint*)&format->fTotalBits);
1085 format->fTotalBits += format->fStencilBits;
1086 } else {
1087 format->fTotalBits = format->fStencilBits;
1088 }
1089 }
1090}
1091}
1092
1093bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1094 int width, int height) {
1095
1096 // All internally created RTs are also textures. We don't create
1097 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1098 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001099 GrAssert(width >= rt->width());
1100 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101
1102 int samples = rt->numSamples();
1103 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001104 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 if (!sbID) {
1106 return false;
1107 }
1108
1109 GrGLStencilBuffer* sb = NULL;
1110
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001111 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 // we start with the last stencil format that succeeded in hopes
1115 // that we won't go through this loop more than once after the
1116 // first (painful) stencil creation.
1117 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001118 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001119 GrGLClearErr(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001120 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 // version on a GL that doesn't have an MSAA extension.
1122 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001123 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1124 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 GR_GL_RENDERBUFFER,
1126 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001127 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 width,
1129 height));
1130 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1132 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001133 sFmt.fInternalFormat,
1134 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135 }
1136
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001137 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138 if (err == GR_GL_NO_ERROR) {
1139 // After sized formats we attempt an unsized format and take whatever
1140 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001141 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001142 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001143 sb = new GrGLStencilBuffer(this, sbID, width, height,
1144 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1146 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001147 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 return true;
1150 }
1151 sb->abandon(); // otherwise we lose sbID
1152 sb->unref();
1153 }
1154 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001155 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001156 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157}
1158
1159bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1160 GrRenderTarget* rt) {
1161 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1162
1163 GrGLuint fbo = glrt->renderFBOID();
1164
1165 if (NULL == sb) {
1166 if (NULL != rt->getStencilBuffer()) {
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_STENCIL_ATTACHMENT,
1169 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001170 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 GR_GL_DEPTH_ATTACHMENT,
1172 GR_GL_RENDERBUFFER, 0));
1173#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001174 GrGLenum status;
1175 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1177#endif
1178 }
1179 return true;
1180 } else {
1181 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1182 GrGLuint rb = glsb->renderbufferID();
1183
reed@google.comac10a2d2010-12-22 21:39:39 +00001184 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001185 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1186 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 GR_GL_STENCIL_ATTACHMENT,
1188 GR_GL_RENDERBUFFER, rb));
1189 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001190 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 GR_GL_DEPTH_ATTACHMENT,
1192 GR_GL_RENDERBUFFER, rb));
1193 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 GR_GL_DEPTH_ATTACHMENT,
1196 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001197 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001199 GrGLenum status;
1200 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 GR_GL_STENCIL_ATTACHMENT,
1204 GR_GL_RENDERBUFFER, 0));
1205 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 GR_GL_DEPTH_ATTACHMENT,
1208 GR_GL_RENDERBUFFER, 0));
1209 }
1210 return false;
1211 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 return true;
1213 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001214 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001215}
1216
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001217////////////////////////////////////////////////////////////////////////////////
1218
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001219GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001220 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001223 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001224 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1228 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1229 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001230 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 // deleting bound buffer does implicit bind to 0
1233 fHWGeometryState.fVertexBuffer = NULL;
1234 return NULL;
1235 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001236 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001237 size, dynamic);
1238 fHWGeometryState.fVertexBuffer = vertexBuffer;
1239 return vertexBuffer;
1240 }
1241 return NULL;
1242}
1243
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001244GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001245 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001246 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001247 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1249 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001250 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001251 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1252 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1253 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001254 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001255 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 // deleting bound buffer does implicit bind to 0
1257 fHWGeometryState.fIndexBuffer = NULL;
1258 return NULL;
1259 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001260 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 size, dynamic);
1262 fHWGeometryState.fIndexBuffer = indexBuffer;
1263 return indexBuffer;
1264 }
1265 return NULL;
1266}
1267
reed@google.comac10a2d2010-12-22 21:39:39 +00001268void GrGpuGL::flushScissor(const GrIRect* rect) {
1269 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001270 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001271 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001272
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001273 GrGLIRect scissor;
1274 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001275 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001276 rect->width(), rect->height());
1277 if (scissor.contains(vp)) {
1278 rect = NULL;
1279 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 }
1281
1282 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001284 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 fHWBounds.fScissorRect = scissor;
1286 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 fHWBounds.fScissorEnabled = true;
1290 }
1291 } else {
1292 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 fHWBounds.fScissorEnabled = false;
1295 }
1296 }
1297}
1298
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001299void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001300 // parent class should never let us get here with no RT
1301 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1302
bsalomon@google.com74b98712011-11-11 19:46:16 +00001303 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001304 if (NULL != rect) {
1305 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001306 clippedRect = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001307 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1308 fCurrDrawState.fRenderTarget->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001309 if (clippedRect.intersect(rtRect)) {
1310 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001311 } else {
1312 return;
1313 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001315 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001316 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001317
1318 GrGLfloat r, g, b, a;
1319 static const GrGLfloat scale255 = 1.f / 255.f;
1320 a = GrColorUnpackA(color) * scale255;
1321 GrGLfloat scaleRGB = scale255;
1322 if (GrPixelConfigIsUnpremultiplied(fCurrDrawState.fRenderTarget->config())) {
1323 scaleRGB *= a;
1324 }
1325 r = GrColorUnpackR(color) * scaleRGB;
1326 g = GrColorUnpackG(color) * scaleRGB;
1327 b = GrColorUnpackB(color) * scaleRGB;
1328
1329 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001330 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001331 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001332 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001333}
1334
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001335void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001336 if (NULL == fCurrDrawState.fRenderTarget) {
1337 return;
1338 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001339
1340 this->flushRenderTarget(&GrIRect::EmptyIRect());
1341
reed@google.comac10a2d2010-12-22 21:39:39 +00001342 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001343 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001344 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001346 GL_CALL(StencilMask(0xffffffff));
1347 GL_CALL(ClearStencil(0));
1348 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001349 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001350}
1351
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001352void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001353 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001354
1355 // this should only be called internally when we know we have a
1356 // stencil buffer.
1357 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001358 GrGLint stencilBitCount =
1359 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001360#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001361 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001362 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001363#else
1364 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001365 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001366 // turned into draws. Our contract on GrDrawTarget says that
1367 // changing the clip between stencil passes may or may not
1368 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001369 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001370#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001371 GrGLint value;
1372 if (insideClip) {
1373 value = (1 << (stencilBitCount - 1));
1374 } else {
1375 value = 0;
1376 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001377 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001378 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001379 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001380 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001381 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001382 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001383}
1384
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001385void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001386 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001387}
1388
bsalomon@google.comc4364992011-11-07 15:54:49 +00001389bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1390 int left, int top,
1391 int width, int height,
1392 GrPixelConfig config,
1393 size_t rowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001394 // if we have to do memcpy to handle non-trim rowBytes then we
1395 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001396 if (this->glCaps().fPackRowLengthSupport) {
1397 return true;
1398 }
1399 // If we have to do memcpys to handle rowBytes then y-flip is free
1400 // Note the rowBytes might be tight to the passed in data, but if data
1401 // gets clipped in x to the target the rowBytes will no longer be tight.
1402 if (left >= 0 && (left + width) < renderTarget->width()) {
1403 return 0 == rowBytes ||
1404 GrBytesPerPixel(config) * width == rowBytes;
1405 } else {
1406 return false;
1407 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001408}
1409
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001410bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001411 int left, int top,
1412 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001413 GrPixelConfig config,
1414 void* buffer,
1415 size_t rowBytes,
1416 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001417 GrGLenum internalFormat; // we don't use this for glReadPixels
1418 GrGLenum format;
1419 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001420 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1421 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001422 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001423 size_t bpp = GrBytesPerPixel(config);
1424 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1425 &left, &top, &width, &height,
1426 const_cast<const void**>(&buffer),
1427 &rowBytes)) {
1428 return false;
1429 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001430
bsalomon@google.comc6980972011-11-02 19:57:21 +00001431 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001432 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1433 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1434 switch (tgt->getResolveType()) {
1435 case GrGLRenderTarget::kCantResolve_ResolveType:
1436 return false;
1437 case GrGLRenderTarget::kAutoResolves_ResolveType:
1438 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1439 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001440 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001441 break;
1442 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001443 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001444 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001445 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1446 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001447 break;
1448 default:
1449 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001450 }
1451
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001452 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001453
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001454 // the read rect is viewport-relative
1455 GrGLIRect readRect;
1456 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001457
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001458 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001459 if (0 == rowBytes) {
1460 rowBytes = tightRowBytes;
1461 }
1462 size_t readDstRowBytes = tightRowBytes;
1463 void* readDst = buffer;
1464
1465 // determine if GL can read using the passed rowBytes or if we need
1466 // a scratch buffer.
1467 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1468 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001469 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001470 GrAssert(!(rowBytes % sizeof(GrColor)));
1471 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1472 readDstRowBytes = rowBytes;
1473 } else {
1474 scratch.reset(tightRowBytes * height);
1475 readDst = scratch.get();
1476 }
1477 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001478 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1479 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001480 format, type, readDst));
1481 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001482 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001483 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1484 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001485
1486 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001487 // API presents top-to-bottom. We must preserve the padding contents. Note
1488 // that the above readPixels did not overwrite the padding.
1489 if (readDst == buffer) {
1490 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001491 if (!invertY) {
1492 scratch.reset(tightRowBytes);
1493 void* tmpRow = scratch.get();
1494 // flip y in-place by rows
1495 const int halfY = height >> 1;
1496 char* top = reinterpret_cast<char*>(buffer);
1497 char* bottom = top + (height - 1) * rowBytes;
1498 for (int y = 0; y < halfY; y++) {
1499 memcpy(tmpRow, top, tightRowBytes);
1500 memcpy(top, bottom, tightRowBytes);
1501 memcpy(bottom, tmpRow, tightRowBytes);
1502 top += rowBytes;
1503 bottom -= rowBytes;
1504 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001505 }
1506 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001507 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001508 // copy from readDst to buffer while flipping y
1509 const int halfY = height >> 1;
1510 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001511 char* dst = reinterpret_cast<char*>(buffer);
1512 if (!invertY) {
1513 dst += (height-1) * rowBytes;
1514 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001515 for (int y = 0; y < height; y++) {
1516 memcpy(dst, src, tightRowBytes);
1517 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001518 if (invertY) {
1519 dst += rowBytes;
1520 } else {
1521 dst -= rowBytes;
1522 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001523 }
1524 }
1525 return true;
1526}
1527
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001528void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001529
1530 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1531
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001532 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001533 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001534 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001535 #if GR_COLLECT_STATS
1536 ++fStats.fRenderTargetChngCnt;
1537 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001538 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001539 GrGLenum status;
1540 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001541 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001542 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001543 }
1544 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001545 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001546 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001547 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001548 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001549 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001550 fHWBounds.fViewportRect = vp;
1551 }
1552 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001553 if (NULL == bound || !bound->isEmpty()) {
1554 rt->flagAsNeedingResolve(bound);
1555 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001556}
1557
twiz@google.com0f31ca72011-03-18 17:38:11 +00001558GrGLenum gPrimitiveType2GLMode[] = {
1559 GR_GL_TRIANGLES,
1560 GR_GL_TRIANGLE_STRIP,
1561 GR_GL_TRIANGLE_FAN,
1562 GR_GL_POINTS,
1563 GR_GL_LINES,
1564 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001565};
1566
bsalomon@google.comd302f142011-03-03 13:54:13 +00001567#define SWAP_PER_DRAW 0
1568
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001569#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001570 #if GR_MAC_BUILD
1571 #include <AGL/agl.h>
1572 #elif GR_WIN32_BUILD
1573 void SwapBuf() {
1574 DWORD procID = GetCurrentProcessId();
1575 HWND hwnd = GetTopWindow(GetDesktopWindow());
1576 while(hwnd) {
1577 DWORD wndProcID = 0;
1578 GetWindowThreadProcessId(hwnd, &wndProcID);
1579 if(wndProcID == procID) {
1580 SwapBuffers(GetDC(hwnd));
1581 }
1582 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1583 }
1584 }
1585 #endif
1586#endif
1587
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001588void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1589 uint32_t startVertex,
1590 uint32_t startIndex,
1591 uint32_t vertexCount,
1592 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001593 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1594
twiz@google.com0f31ca72011-03-18 17:38:11 +00001595 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001596
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001597 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1598 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1599
1600 // our setupGeometry better have adjusted this to zero since
1601 // DrawElements always draws from the begining of the arrays for idx 0.
1602 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001603
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001604 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1605 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001606#if SWAP_PER_DRAW
1607 glFlush();
1608 #if GR_MAC_BUILD
1609 aglSwapBuffers(aglGetCurrentContext());
1610 int set_a_break_pt_here = 9;
1611 aglSwapBuffers(aglGetCurrentContext());
1612 #elif GR_WIN32_BUILD
1613 SwapBuf();
1614 int set_a_break_pt_here = 9;
1615 SwapBuf();
1616 #endif
1617#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001618}
1619
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001620void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1621 uint32_t startVertex,
1622 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1624
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001625 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1626
1627 // our setupGeometry better have adjusted this to zero.
1628 // DrawElements doesn't take an offset so we always adjus the startVertex.
1629 GrAssert(0 == startVertex);
1630
1631 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1632 // account for startVertex in the DrawElements case. So we always
1633 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001634 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001635#if SWAP_PER_DRAW
1636 glFlush();
1637 #if GR_MAC_BUILD
1638 aglSwapBuffers(aglGetCurrentContext());
1639 int set_a_break_pt_here = 9;
1640 aglSwapBuffers(aglGetCurrentContext());
1641 #elif GR_WIN32_BUILD
1642 SwapBuf();
1643 int set_a_break_pt_here = 9;
1644 SwapBuf();
1645 #endif
1646#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001647}
1648
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001649void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001650
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001651 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001652 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001654 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1655 rt->renderFBOID()));
1656 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1657 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001658 #if GR_COLLECT_STATS
1659 ++fStats.fRenderTargetChngCnt;
1660 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001661 // make sure we go through flushRenderTarget() since we've modified
1662 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001663 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001664 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001665 const GrIRect dirtyRect = rt->getResolveRect();
1666 GrGLIRect r;
1667 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1668 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001669
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001670 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001671 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001672 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1673 GL_CALL(Scissor(r.fLeft, r.fBottom,
1674 r.fWidth, r.fHeight));
1675 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001676 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001677 fHWBounds.fScissorEnabled = true;
1678 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001679 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001680 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001681 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1682 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001683 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001684 int right = r.fLeft + r.fWidth;
1685 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001686 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1687 r.fLeft, r.fBottom, right, top,
1688 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001689 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001690 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001691 }
1692}
1693
twiz@google.com0f31ca72011-03-18 17:38:11 +00001694static const GrGLenum grToGLStencilFunc[] = {
1695 GR_GL_ALWAYS, // kAlways_StencilFunc
1696 GR_GL_NEVER, // kNever_StencilFunc
1697 GR_GL_GREATER, // kGreater_StencilFunc
1698 GR_GL_GEQUAL, // kGEqual_StencilFunc
1699 GR_GL_LESS, // kLess_StencilFunc
1700 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1701 GR_GL_EQUAL, // kEqual_StencilFunc,
1702 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001703};
1704GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1705GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1706GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1707GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1708GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1709GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1710GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1711GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1712GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1713
twiz@google.com0f31ca72011-03-18 17:38:11 +00001714static const GrGLenum grToGLStencilOp[] = {
1715 GR_GL_KEEP, // kKeep_StencilOp
1716 GR_GL_REPLACE, // kReplace_StencilOp
1717 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1718 GR_GL_INCR, // kIncClamp_StencilOp
1719 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1720 GR_GL_DECR, // kDecClamp_StencilOp
1721 GR_GL_ZERO, // kZero_StencilOp
1722 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001723};
1724GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1725GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1726GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1727GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1728GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1729GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1730GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1731GR_STATIC_ASSERT(6 == kZero_StencilOp);
1732GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1733
reed@google.comac10a2d2010-12-22 21:39:39 +00001734void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001735 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001736
1737 // use stencil for clipping if clipping is enabled and the clip
1738 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001739 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001741 bool stencilChange = fHWStencilClip != stencilClip ||
1742 fHWDrawState.fStencilSettings != *settings ||
1743 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1744 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001745
1746 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001747
bsalomon@google.comd302f142011-03-03 13:54:13 +00001748 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1749 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001750
bsalomon@google.comd302f142011-03-03 13:54:13 +00001751 if (settings->isDisabled()) {
1752 if (stencilClip) {
1753 settings = &gClipStencilSettings;
1754 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001755 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001756
1757 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001758 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001759 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001760 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001761 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001762 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001763 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1764 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1765 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1766 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1767 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1768 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1769 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1770 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1771 }
1772 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001773 int stencilBits = 0;
1774 GrStencilBuffer* stencilBuffer =
1775 fCurrDrawState.fRenderTarget->getStencilBuffer();
1776 if (NULL != stencilBuffer) {
1777 stencilBits = stencilBuffer->bits();
1778 }
1779 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001780 GrAssert(stencilBits ||
1781 (GrStencilSettings::gDisabled ==
1782 fCurrDrawState.fStencilSettings));
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001783
1784 GrGLuint clipStencilMask = 0;
1785 GrGLuint userStencilMask = ~0;
1786 if (stencilBits > 0) {
1787 clipStencilMask = 1 << (stencilBits - 1);
1788 userStencilMask = clipStencilMask - 1;
1789 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001790
1791 unsigned int frontRef = settings->fFrontFuncRef;
1792 unsigned int frontMask = settings->fFrontFuncMask;
1793 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001794 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001795
1796 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1797
1798 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1799 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1800 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001801 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
1802 stencilClip, settings->fFrontFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803
1804 ConvertStencilFuncAndMask(settings->fFrontFunc,
1805 stencilClip,
1806 clipStencilMask,
1807 userStencilMask,
1808 &frontRef,
1809 &frontMask);
1810 frontWriteMask &= userStencilMask;
1811 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001812 GrAssert((size_t)
1813 settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1814 GrAssert((size_t)
1815 settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
1816 GrAssert((size_t)
1817 settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1818 GrAssert((size_t)
1819 settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001820 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001821 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822
1823 unsigned int backRef = settings->fBackFuncRef;
1824 unsigned int backMask = settings->fBackFuncMask;
1825 unsigned int backWriteMask = settings->fBackWriteMask;
1826
1827
1828 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1829 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1830 backFunc = grToGLStencilFunc[settings->fBackFunc];
1831 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001832 backFunc = grToGLStencilFunc[ConvertStencilFunc(
1833 stencilClip, settings->fBackFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001834 ConvertStencilFuncAndMask(settings->fBackFunc,
1835 stencilClip,
1836 clipStencilMask,
1837 userStencilMask,
1838 &backRef,
1839 &backMask);
1840 backWriteMask &= userStencilMask;
1841 }
1842
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001843 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1844 frontRef, frontMask));
1845 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1846 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1847 backRef, backMask));
1848 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1849 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1850 grToGLStencilOp[settings->fFrontFailOp],
1851 grToGLStencilOp[settings->fFrontPassOp],
1852 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001853
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001854 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1855 grToGLStencilOp[settings->fBackFailOp],
1856 grToGLStencilOp[settings->fBackPassOp],
1857 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001859 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1860 GL_CALL(StencilMask(frontWriteMask));
1861 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001862 grToGLStencilOp[settings->fFrontPassOp],
1863 grToGLStencilOp[settings->fFrontPassOp]));
1864 }
1865 }
1866 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001867 fHWStencilClip = stencilClip;
1868 }
1869}
1870
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001871void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001872 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001873 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1874 // smooth lines.
1875
1876 // we prefer smooth lines over multisampled lines
1877 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001878 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001879 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001880 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001881 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001882 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001883 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001884 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001885 fHWAAState.fSmoothLineEnabled = false;
1886 }
1887 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1888 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001889 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001890 fHWAAState.fMSAAEnabled = false;
1891 }
1892 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001893 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001894 fHWAAState.fMSAAEnabled) {
1895 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001896 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001897 fHWAAState.fMSAAEnabled = false;
1898 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001899 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001900 fHWAAState.fMSAAEnabled = true;
1901 }
1902 }
1903 }
1904}
1905
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001906void GrGpuGL::flushBlend(GrPrimitiveType type,
1907 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001908 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001909 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001910 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001911 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001912 fHWBlendDisabled = false;
1913 }
1914 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1915 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001916 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1917 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001918 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1919 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1920 }
1921 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001922 // any optimization to disable blending should
1923 // have already been applied and tweaked the coeffs
1924 // to (1, 0).
1925 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1926 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001927 if (fHWBlendDisabled != blendOff) {
1928 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001929 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001930 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001931 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001932 }
1933 fHWBlendDisabled = blendOff;
1934 }
1935 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001936 if (fHWDrawState.fSrcBlend != srcCoeff ||
1937 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001938 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1939 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001940 fHWDrawState.fSrcBlend = srcCoeff;
1941 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001942 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001943 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1944 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001945 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1946
1947 float c[] = {
1948 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1949 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1950 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1951 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1952 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001953 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001954 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1955 }
1956 }
1957 }
1958}
1959
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001960namespace {
1961
1962unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001963 switch (filter) {
1964 case GrSamplerState::kBilinear_Filter:
1965 case GrSamplerState::k4x4Downsample_Filter:
1966 return GR_GL_LINEAR;
1967 case GrSamplerState::kNearest_Filter:
1968 case GrSamplerState::kConvolution_Filter:
1969 return GR_GL_NEAREST;
1970 default:
1971 GrAssert(!"Unknown filter type");
1972 return GR_GL_LINEAR;
1973 }
1974}
1975
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001976const GrGLenum* get_swizzle(GrPixelConfig config,
1977 const GrSamplerState& sampler) {
1978 if (GrPixelConfigIsAlphaOnly(config)) {
1979 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
1980 GR_GL_ALPHA, GR_GL_ALPHA };
1981 return gAlphaSmear;
1982 } else if (sampler.swapsRAndB()) {
1983 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
1984 GR_GL_RED, GR_GL_ALPHA };
1985 return gRedBlueSwap;
1986 } else {
1987 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
1988 GR_GL_BLUE, GR_GL_ALPHA };
1989 return gStraight;
1990 }
1991}
1992
1993void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
1994 // should add texparameteri to interface to make 1 instead of 4 calls here
1995 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1996 GR_GL_TEXTURE_SWIZZLE_R,
1997 swizzle[0]));
1998 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1999 GR_GL_TEXTURE_SWIZZLE_G,
2000 swizzle[1]));
2001 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2002 GR_GL_TEXTURE_SWIZZLE_B,
2003 swizzle[2]));
2004 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2005 GR_GL_TEXTURE_SWIZZLE_A,
2006 swizzle[3]));
2007}
2008}
2009
bsalomon@google.comffca4002011-02-22 20:34:01 +00002010bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002011
2012 // GrGpu::setupClipAndFlushState should have already checked this
2013 // and bailed if not true.
2014 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002015
tomhudson@google.com93813632011-10-27 20:21:16 +00002016 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002017 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002018 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002019 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002020
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002021 // true for now, but maybe not with GrEffect.
2022 GrAssert(NULL != nextTexture);
2023 // if we created a rt/tex and rendered to it without using a
2024 // texture and now we're texuring from the rt it will still be
2025 // the last bound texture, but it needs resolving. So keep this
2026 // out of the "last != next" check.
2027 GrGLRenderTarget* texRT =
2028 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2029 if (NULL != texRT) {
2030 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002031 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002032
2033 if (fHWDrawState.fTextures[s] != nextTexture) {
2034 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002035 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002036 #if GR_COLLECT_STATS
2037 ++fStats.fTextureChngCnt;
2038 #endif
2039 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2040 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002041 // The texture matrix has to compensate for texture width/height
2042 // and NPOT-embedded-in-POT
2043 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002044 }
2045
2046 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002047 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002048 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002049 nextTexture->getCachedTexParams(&timestamp);
2050 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002051 GrGLTexture::TexParams newTexParams;
2052
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002053 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002054
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002055 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002056 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2057 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002058 memcpy(newTexParams.fSwizzleRGBA,
2059 get_swizzle(nextTexture->config(), sampler),
2060 sizeof(newTexParams.fSwizzleRGBA));
2061 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002062 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002063 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002064 GR_GL_TEXTURE_MAG_FILTER,
2065 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002066 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002067 GR_GL_TEXTURE_MIN_FILTER,
2068 newTexParams.fFilter));
2069 }
2070 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2071 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002072 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002073 GR_GL_TEXTURE_WRAP_S,
2074 newTexParams.fWrapS));
2075 }
2076 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2077 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002078 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002079 GR_GL_TEXTURE_WRAP_T,
2080 newTexParams.fWrapT));
2081 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002082 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002083 (setAll ||
2084 memcmp(newTexParams.fSwizzleRGBA,
2085 oldTexParams.fSwizzleRGBA,
2086 sizeof(newTexParams.fSwizzleRGBA)))) {
2087 setTextureUnit(s);
2088 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2089 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002090 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002091 nextTexture->setCachedTexParams(newTexParams,
2092 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002093 }
2094 }
2095
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002096 GrIRect* rect = NULL;
2097 GrIRect clipBounds;
2098 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2099 fClip.hasConservativeBounds()) {
2100 fClip.getConservativeBounds().roundOut(&clipBounds);
2101 rect = &clipBounds;
2102 }
2103 this->flushRenderTarget(rect);
2104 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002105
reed@google.comac10a2d2010-12-22 21:39:39 +00002106 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2107 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2108 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002109 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002110 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002111 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002112 }
2113 }
2114
bsalomon@google.comd302f142011-03-03 13:54:13 +00002115 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2116 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002117 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002118 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002119 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002120 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002121 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002122 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002123 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002124 }
2125
bsalomon@google.comd302f142011-03-03 13:54:13 +00002126 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2127 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002128 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002129 GL_CALL(Enable(GR_GL_CULL_FACE));
2130 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002131 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002132 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002133 GL_CALL(Enable(GR_GL_CULL_FACE));
2134 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002135 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002136 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002137 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002138 break;
2139 default:
2140 GrCrash("Unknown draw face.");
2141 }
2142 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2143 }
2144
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002145#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002146 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002147 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002148 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002149 NULL == fCurrDrawState.fRenderTarget ||
2150 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002151 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002152 fCurrDrawState.fRenderTarget);
2153 }
2154#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002155
reed@google.comac10a2d2010-12-22 21:39:39 +00002156 flushStencil();
2157
bsalomon@google.comd302f142011-03-03 13:54:13 +00002158 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002159 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002160 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002161}
2162
2163void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002164 if (fHWGeometryState.fVertexBuffer != buffer) {
2165 fHWGeometryState.fArrayPtrsDirty = true;
2166 fHWGeometryState.fVertexBuffer = buffer;
2167 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002168}
2169
2170void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002171 if (fHWGeometryState.fVertexBuffer == buffer) {
2172 // deleting bound buffer does implied bind to 0
2173 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002174 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002175 }
2176}
2177
2178void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002179 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002180}
2181
2182void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002183 if (fHWGeometryState.fIndexBuffer == buffer) {
2184 // deleting bound buffer does implied bind to 0
2185 fHWGeometryState.fIndexBuffer = NULL;
2186 }
2187}
2188
reed@google.comac10a2d2010-12-22 21:39:39 +00002189void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2190 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002191 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002192 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002193 }
2194 if (fHWDrawState.fRenderTarget == renderTarget) {
2195 fHWDrawState.fRenderTarget = NULL;
2196 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002197}
2198
2199void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002200 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002201 if (fCurrDrawState.fTextures[s] == texture) {
2202 fCurrDrawState.fTextures[s] = NULL;
2203 }
2204 if (fHWDrawState.fTextures[s] == texture) {
2205 // deleting bound texture does implied bind to 0
2206 fHWDrawState.fTextures[s] = NULL;
2207 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002208 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002209}
2210
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002211bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002212 GrGLenum* internalFormat,
bsalomon@google.com6f379512011-11-16 20:36:03 +00002213 GrGLenum* externalFormat,
2214 GrGLenum* externalType) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002216 case kRGBA_8888_PM_GrPixelConfig:
2217 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002218 *externalFormat = GR_GL_RGBA;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002219 *internalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002220 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002221 break;
2222 case kBGRA_8888_PM_GrPixelConfig:
2223 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002224 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002225 return false;
2226 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002227 *externalFormat = GR_GL_BGRA;
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002228 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002229 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002230 } else {
2231 *internalFormat = GR_GL_RGBA;
2232 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002233 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002234 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002235 case kRGB_565_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002236 *externalFormat = GR_GL_RGB;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002237 *internalFormat = GR_GL_RGB;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002238 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002239 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002240 case kRGBA_4444_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002241 *externalFormat = GR_GL_RGBA;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002242 *internalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002243 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002244 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002245 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002246 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00002247 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002248 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002249 *externalType = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002250 } else {
2251 return false;
2252 }
2253 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002254 case kAlpha_8_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002255 *externalFormat = GR_GL_ALPHA;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002256 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002257 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002258 break;
2259 default:
2260 return false;
2261 }
2262 return true;
2263}
2264
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002265void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002266 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002267 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002268 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002269 fActiveTextureUnitIdx = unit;
2270 }
2271}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002272
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002273void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002274 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002275 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002276 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2277 }
2278}
2279
reed@google.comac10a2d2010-12-22 21:39:39 +00002280/* On ES the internalFormat and format must match for TexImage and we use
2281 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2282 decide the internalFormat. However, on ES internalFormat for
2283 RenderBufferStorage* has to be a specific format (not a base format like
2284 GL_RGBA).
2285 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002286bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002287 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002288 // The ES story for BGRA and RenderbufferStorage appears murky. It
2289 // takes an internal format as a parameter. The OES FBO extension and
2290 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2291 // BGRA extensions adds BGRA as both an internal and external format
2292 // and the other only as an external format (like desktop GL). OES
2293 // restricts RenderbufferStorage's format to a *sized* internal format.
2294 // There is no sized BGRA internal format.
2295 // So if the texture has internal format BGRA we just hope that the
2296 // resolve blit can do RGBA->BGRA internal format conversion.
2297 case kRGBA_8888_PM_GrPixelConfig:
2298 case kRGBA_8888_UPM_GrPixelConfig:
2299 case kBGRA_8888_PM_GrPixelConfig:
2300 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002301 if (fGLCaps.fRGBA8RenderbufferSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002302 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2303 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002304 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002305 return true;
2306 } else {
2307 return false;
2308 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002309 case kRGB_565_GrPixelConfig:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002310 // ES2 supports 565, but desktop GL does not.
bsalomon@google.comc4364992011-11-07 15:54:49 +00002311 if (kDesktop_GrGLBinding != this->glBinding()) {
2312 *format = GR_GL_RGB565;
2313 return true;
2314 } else {
2315 return false;
2316 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002317 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002318 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 return true;
2320 default:
2321 return false;
2322 }
2323}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002324
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002325void GrGpuGL::resetDirtyFlags() {
2326 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2327}
2328
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002329void GrGpuGL::setBuffers(bool indexed,
2330 int* extraVertexOffset,
2331 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002332
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002333 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002334
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002335 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2336
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002337 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002338 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002339 case kBuffer_GeometrySrcType:
2340 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002341 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002342 break;
2343 case kArray_GeometrySrcType:
2344 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002345 this->finalizeReservedVertices();
2346 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2347 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002348 break;
2349 default:
2350 vbuf = NULL; // suppress warning
2351 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002352 }
2353
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002354 GrAssert(NULL != vbuf);
2355 GrAssert(!vbuf->isLocked());
2356 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002357 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002358 fHWGeometryState.fArrayPtrsDirty = true;
2359 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002360 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002361
2362 if (indexed) {
2363 GrAssert(NULL != extraIndexOffset);
2364
2365 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002366 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002367 case kBuffer_GeometrySrcType:
2368 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002369 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002370 break;
2371 case kArray_GeometrySrcType:
2372 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002373 this->finalizeReservedIndices();
2374 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2375 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002376 break;
2377 default:
2378 ibuf = NULL; // suppress warning
2379 GrCrash("Unknown geometry src type!");
2380 }
2381
2382 GrAssert(NULL != ibuf);
2383 GrAssert(!ibuf->isLocked());
2384 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002385 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002386 fHWGeometryState.fIndexBuffer = ibuf;
2387 }
2388 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002389}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002390
2391int GrGpuGL::getMaxEdges() const {
2392 // FIXME: This is a pessimistic estimate based on how many other things
2393 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002394 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2395 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002396}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002397
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002398void GrGpuGL::GLCaps::print() const {
2399 for (int i = 0; i < fStencilFormats.count(); ++i) {
2400 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2401 i,
2402 fStencilFormats[i].fStencilBits,
2403 fStencilFormats[i].fTotalBits);
2404 }
2405
2406 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2407 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2408 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2409 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2410 static const char* gMSFBOExtStr[] = {
2411 "None",
2412 "ARB",
2413 "EXT",
2414 "Apple",
2415 };
2416 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002417 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002418 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2419 }
2420 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2421 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002422 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002423 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002424 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002425 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002426 (fTextureSwizzleSupport ? "YES": "NO"));
2427 GrPrintf("Unpack Row length support: %s\n",
2428 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002429 GrPrintf("Unpack Flip Y support: %s\n",
2430 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002431 GrPrintf("Pack Row length support: %s\n",
2432 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002433}