blob: 86a95a973c49b0f99f25c62b80ee9baec6ae3205 [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;
308 fGLCaps.fPackRowLengthSupport = true;
309 } else {
310 fGLCaps.fUnpackRowLengthSupport = this->hasExtension("GL_EXT_unpack_subimage");
311 // no extension for pack row length
312 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000313 }
314
315 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000316 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
317 // extension includes glMapBuffer.
318 } else {
319 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
320 }
321
322 if (kDesktop_GrGLBinding == this->glBinding()) {
323 if (fGLVersion >= GR_GL_VER(2,0) ||
324 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
325 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000326 } else {
327 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000328 }
329 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000330 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000331 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000332 }
333
334 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
335
336 ////////////////////////////////////////////////////////////////////////////
337 // Experiments to determine limitations that can't be queried.
338 // TODO: Make these a preprocess that generate some compile time constants.
339 // TODO: probe once at startup, rather than once per context creation.
340
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000341 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
342 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
343 // Our render targets are always created with textures as the color
344 // attachment, hence this min:
345 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
346
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000347 this->initFSAASupport();
348 this->initStencilFormats();
349}
350
351void GrGpuGL::initFSAASupport() {
352 // TODO: Get rid of GrAALevel and use # samples directly.
353 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
354 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
355 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
356 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
357 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
358
359 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
360 if (kDesktop_GrGLBinding != this->glBinding()) {
361 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
362 // chrome's extension is equivalent to the EXT msaa
363 // and fbo_blit extensions.
364 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
365 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
366 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
367 }
368 } else {
369 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
370 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
371 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
372 this->hasExtension("GL_EXT_framebuffer_blit")) {
373 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
374 }
375 }
376
377 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
378 GrGLint maxSamples;
379 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
380 if (maxSamples > 1 ) {
381 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
382 fGLCaps.fAASamples[kLow_GrAALevel] =
383 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
384 fGLCaps.fAASamples[kMed_GrAALevel] =
385 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
386 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
387 }
388 }
389 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
390}
391
392void GrGpuGL::initStencilFormats() {
393
394 // Build up list of legal stencil formats (though perhaps not supported on
395 // the particular gpu/driver) from most preferred to least.
396
397 // these consts are in order of most preferred to least preferred
398 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
399 static const GrGLStencilBuffer::Format
400 // internal Format stencil bits total bits packed?
401 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
402 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
403 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
404 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
405 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
406 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
407
408 if (kDesktop_GrGLBinding == this->glBinding()) {
409 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
410 this->hasExtension("GL_EXT_packed_depth_stencil") ||
411 this->hasExtension("GL_ARB_framebuffer_object");
412
413 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
414 // require FBO support we can expect these are legal formats and don't
415 // check. These also all support the unsized GL_STENCIL_INDEX.
416 fGLCaps.fStencilFormats.push_back() = gS8;
417 fGLCaps.fStencilFormats.push_back() = gS16;
418 if (supportsPackedDS) {
419 fGLCaps.fStencilFormats.push_back() = gD24S8;
420 }
421 fGLCaps.fStencilFormats.push_back() = gS4;
422 if (supportsPackedDS) {
423 fGLCaps.fStencilFormats.push_back() = gDS;
424 }
425 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000426 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
427 // for other formats.
428 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000429
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000430 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000431 //fStencilFormats.push_back() = gS16;
432 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
433 fGLCaps.fStencilFormats.push_back() = gD24S8;
434 }
435 if (this->hasExtension("GL_OES_stencil4")) {
436 fGLCaps.fStencilFormats.push_back() = gS4;
437 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000438 }
439}
440
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000441GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) {
442 if (GR_GL_RGBA_8888_READBACK_SLOW && GrPixelConfigIsRGBA8888(config)) {
443 return GrPixelConfigSwapRAndB(config);
444 } else {
445 return config;
446 }
447}
448
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000449void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000450 if (gPrintStartupSpew && !fPrintedCaps) {
451 fPrintedCaps = true;
452 this->getCaps().print();
453 fGLCaps.print();
454 }
455
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000456 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000458 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000459
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000460 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000461 GL_CALL(Disable(GR_GL_DEPTH_TEST));
462 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000463
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000464 GL_CALL(Disable(GR_GL_CULL_FACE));
465 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000466 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000468 GL_CALL(Disable(GR_GL_DITHER));
469 if (kDesktop_GrGLBinding == this->glBinding()) {
470 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
471 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
472 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000473 fHWAAState.fMSAAEnabled = false;
474 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000475 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000476
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000477 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000478 fHWDrawState.fFlagBits = 0;
479
reed@google.comac10a2d2010-12-22 21:39:39 +0000480 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000481 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000482
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000483 // invalid
484 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
reed@google.comac10a2d2010-12-22 21:39:39 +0000486 // illegal values
tomhudson@google.com62b09682011-11-09 16:39:17 +0000487 //fHWDrawState.fSrcBlend = (GrBlendCoeff)(uint8_t)-1;
488 fHWDrawState.fSrcBlend = (GrBlendCoeff)0xFF;
489 fHWDrawState.fDstBlend = (GrBlendCoeff)(uint8_t)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000490
491 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000492 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000493
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000495
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000496 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000497
tomhudson@google.com93813632011-10-27 20:21:16 +0000498 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000499 fHWDrawState.fTextures[s] = NULL;
500 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
501 -GR_ScalarMax,
502 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000503 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000504 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000505 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000506
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000507 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000509 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000510 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
bsalomon@google.comd302f142011-03-03 13:54:13 +0000512 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000513 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000514 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000515
516 fHWGeometryState.fIndexBuffer = NULL;
517 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000518
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000519 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000520
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000521 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 fHWDrawState.fRenderTarget = NULL;
523}
524
bsalomon@google.come269f212011-11-07 13:29:52 +0000525GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
526 GrGLenum internalFormat; // we don't need this value
527 GrGLTexture::Desc glTexDesc;
528 if (!this->canBeTexture(desc.fConfig, &internalFormat,
529 &glTexDesc.fUploadFormat, &glTexDesc.fUploadType)) {
530 return NULL;
531 }
532
bsalomon@google.com99621082011-11-15 16:47:16 +0000533 glTexDesc.fWidth = desc.fWidth;
534 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000535 glTexDesc.fConfig = desc.fConfig;
536 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
537 glTexDesc.fOwnsID = false;
538 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
539
540 GrGLTexture* texture = NULL;
541 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
542 GrGLRenderTarget::Desc glRTDesc;
543 glRTDesc.fRTFBOID = 0;
544 glRTDesc.fTexFBOID = 0;
545 glRTDesc.fMSColorRenderbufferID = 0;
546 glRTDesc.fOwnIDs = true;
547 glRTDesc.fConfig = desc.fConfig;
548 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000549 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
550 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000551 glTexDesc.fTextureID,
552 &glRTDesc)) {
553 return NULL;
554 }
555 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
556 } else {
557 texture = new GrGLTexture(this, glTexDesc);
558 }
559 if (NULL == texture) {
560 return NULL;
561 }
562
563 this->setSpareTextureUnit();
564 return texture;
565}
566
567GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
568 GrGLRenderTarget::Desc glDesc;
569 glDesc.fConfig = desc.fConfig;
570 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
571 glDesc.fMSColorRenderbufferID = 0;
572 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
573 glDesc.fSampleCnt = desc.fSampleCnt;
574 glDesc.fOwnIDs = false;
575 GrGLIRect viewport;
576 viewport.fLeft = 0;
577 viewport.fBottom = 0;
578 viewport.fWidth = desc.fWidth;
579 viewport.fHeight = desc.fHeight;
580
581 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
582 if (desc.fStencilBits) {
583 GrGLStencilBuffer::Format format;
584 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
585 format.fPacked = false;
586 format.fStencilBits = desc.fStencilBits;
587 format.fTotalBits = desc.fStencilBits;
588 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
589 0,
590 desc.fWidth,
591 desc.fHeight,
592 desc.fSampleCnt,
593 format);
594 tgt->setStencilBuffer(sb);
595 sb->unref();
596 }
597 return tgt;
598}
599
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000600GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
601
602 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
603 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
604 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
605 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
606
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000607 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000608 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000609
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000610 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000611 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000612 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000613 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000614 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000615 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000616 } else {
617 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000618 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000619 }
620 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000621 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000622 }
623 // we don't know what the RB ids are without glGets and we don't care
624 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000625 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000626 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000627 if (desc.fStencilBits) {
628 GrGLStencilBuffer::Format format;
629 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
630 format.fPacked = false;
631 format.fStencilBits = desc.fStencilBits;
632 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000633 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
634 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000635 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000636 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000637 }
638
639 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000640 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000641 GrGLenum dontCare;
642 if (!canBeTexture(desc.fConfig, &dontCare,
643 &texDesc.fUploadFormat,
644 &texDesc.fUploadType)) {
645 return NULL;
646 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000647 texDesc.fWidth = desc.fWidth;
648 texDesc.fHeight = desc.fHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000649
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000650 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000651 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000652 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000653 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000654
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000655 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000656 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000657 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000658 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000659 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000660 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000661 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000662 } else {
663 GrGLIRect viewport;
664 viewport.fLeft = 0;
665 viewport.fBottom = 0;
666 viewport.fWidth = desc.fWidth;
667 viewport.fHeight = desc.fHeight;
668
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000669 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000670 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000671 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000672 }
673}
674
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000675
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000676////////////////////////////////////////////////////////////////////////////////
677
678void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
679 GrGLenum internalFormat,
680 const void* data,
681 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000682 // we assume the texture is bound
683
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000684 size_t bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com99621082011-11-15 16:47:16 +0000685 size_t trimRowBytes = desc.fWidth * bpp;
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000686
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000687 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000688 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000689 }
690
691 // in case we need a temporary, trimmed copy of the src pixels
692 SkAutoSMalloc<128 * 128> tempStorage;
693
694 /*
695 * check whether to allocate a temporary buffer for flipping y or
696 * because our data has extra bytes past each row. If so, we need
697 * to trim those off here, since GL ES doesn't let us specify
698 * GL_UNPACK_ROW_LENGTH.
699 */
700 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000701 if (this->glCaps().fUnpackRowLengthSupport && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000702 if (data && rowBytes != trimRowBytes) {
703 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
704 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000705 }
706 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000707 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000708 // copy the data into our new storage, skipping the trailing bytes
bsalomon@google.com99621082011-11-15 16:47:16 +0000709 size_t trimSize = desc.fHeight * trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000710 const char* src = (const char*)data;
711 if (flipY) {
bsalomon@google.com99621082011-11-15 16:47:16 +0000712 src += (desc.fHeight - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000713 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000714 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com99621082011-11-15 16:47:16 +0000715 for (int y = 0; y < desc.fHeight; y++) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000716 memcpy(dst, src, trimRowBytes);
717 if (flipY) {
718 src -= rowBytes;
719 } else {
720 src += rowBytes;
721 }
722 dst += trimRowBytes;
723 }
724 // now point data to our trimmed version
725 data = tempStorage.get();
726 rowBytes = trimRowBytes;
727 }
728 }
729
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000730 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000731 if (kIndex_8_GrPixelConfig == desc.fConfig &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000732 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000733 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
bsalomon@google.com99621082011-11-15 16:47:16 +0000734 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000735 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000736 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
bsalomon@google.com99621082011-11-15 16:47:16 +0000737 desc.fWidth, desc.fHeight,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000738 0, imageSize, data));
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000739 if (this->glCaps().fUnpackRowLengthSupport) {
740 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
741 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000742 } else {
bsalomon@google.com99621082011-11-15 16:47:16 +0000743 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
744 desc.fWidth, desc.fHeight, 0,
745 desc.fUploadFormat, desc.fUploadType, data));
746 if (this->glCaps().fUnpackRowLengthSupport) {
747 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000748 }
749 }
750}
751
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000752bool GrGpuGL::createRenderTargetObjects(int width, int height,
753 GrGLuint texID,
754 GrGLRenderTarget::Desc* desc) {
755 desc->fMSColorRenderbufferID = 0;
756 desc->fRTFBOID = 0;
757 desc->fTexFBOID = 0;
758 desc->fOwnIDs = true;
759
760 GrGLenum status;
761 GrGLint err;
762
bsalomon@google.comab15d612011-08-09 12:57:56 +0000763 GrGLenum msColorFormat = 0; // suppress warning
764
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000765 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000766 if (!desc->fTexFBOID) {
767 goto FAILED;
768 }
769
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000770
771 // If we are using multisampling we will create two FBOS. We render
772 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000773 if (desc->fSampleCnt > 0) {
774 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
775 goto FAILED;
776 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000777 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
778 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000779 if (!desc->fRTFBOID ||
780 !desc->fMSColorRenderbufferID ||
781 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
782 goto FAILED;
783 }
784 } else {
785 desc->fRTFBOID = desc->fTexFBOID;
786 }
787
788 if (desc->fRTFBOID != desc->fTexFBOID) {
789 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000790 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000791 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000792 GR_GL_CALL_NOERRCHECK(this->glInterface(),
793 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
794 desc->fSampleCnt,
795 msColorFormat,
796 width, height));
797 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000798 if (err != GR_GL_NO_ERROR) {
799 goto FAILED;
800 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000801 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
802 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000803 GR_GL_COLOR_ATTACHMENT0,
804 GR_GL_RENDERBUFFER,
805 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000806 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000807 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
808 goto FAILED;
809 }
810 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000811 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000812
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000813 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
814 GR_GL_COLOR_ATTACHMENT0,
815 GR_GL_TEXTURE_2D,
816 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000817 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000818 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
819 goto FAILED;
820 }
821
822 return true;
823
824FAILED:
825 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000826 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000827 }
828 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000829 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000830 }
831 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000832 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000833 }
834 return false;
835}
836
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000837// good to set a break-point here to know when createTexture fails
838static GrTexture* return_null_texture() {
839// GrAssert(!"null texture");
840 return NULL;
841}
842
843#if GR_DEBUG
844static size_t as_size_t(int x) {
845 return x;
846}
847#endif
848
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000849GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000850 const void* srcData,
851 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000852
853#if GR_COLLECT_STATS
854 ++fStats.fTextureCreateCnt;
855#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000856
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000857 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000858 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000859 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +0000860
bsalomon@google.com99621082011-11-15 16:47:16 +0000861 glTexDesc.fWidth = desc.fWidth;
862 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000863 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000864 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000865
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000866 glRTDesc.fMSColorRenderbufferID = 0;
867 glRTDesc.fRTFBOID = 0;
868 glRTDesc.fTexFBOID = 0;
869 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000870 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000871
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000872 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000873 if (!canBeTexture(desc.fConfig,
reed@google.comac10a2d2010-12-22 21:39:39 +0000874 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000875 &glTexDesc.fUploadFormat,
876 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000877 return return_null_texture();
878 }
879
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000880 const Caps& caps = this->getCaps();
881
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000882 // We keep GrRenderTargets in GL's normal orientation so that they
883 // can be drawn to by the outside world without the client having
884 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000885 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000886 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000887
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000888 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
889 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
890 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
891 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000892 GrPrintf("AA RT requested but not supported on this platform.");
893 }
894
reed@google.comac10a2d2010-12-22 21:39:39 +0000895 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +0000896 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
897 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000898 return return_null_texture();
899 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000900 }
901
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000902 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000903 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000904 return return_null_texture();
905 }
906
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000907 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000908 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000909
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000910 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
911 // drivers have a bug where an FBO won't be complete if it includes a
912 // texture that is not mipmap complete (considering the filter in use).
913 GrGLTexture::TexParams initialTexParams;
914 // we only set a subset here so invalidate first
915 initialTexParams.invalidate();
916 initialTexParams.fFilter = GR_GL_NEAREST;
917 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
918 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
920 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000921 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
923 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000924 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000925 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
926 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000927 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000928 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
929 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000930 initialTexParams.fWrapT));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000931 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000932
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000933 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000934 if (renderTarget) {
935#if GR_COLLECT_STATS
936 ++fStats.fRenderTargetCreateCnt;
937#endif
bsalomon@google.com99621082011-11-15 16:47:16 +0000938 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
939 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940 glTexDesc.fTextureID,
941 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000943 return return_null_texture();
944 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000945 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000946 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000947 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000948 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000949 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +0000950#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000951 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
952 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000953#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000954 return tex;
955}
956
957namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000958void inline get_stencil_rb_sizes(const GrGLInterface* gl,
959 GrGLuint rb,
960 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000961 // we shouldn't ever know one size and not the other
962 GrAssert((kUnknownBitCount == format->fStencilBits) ==
963 (kUnknownBitCount == format->fTotalBits));
964 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000966 GR_GL_RENDERBUFFER_STENCIL_SIZE,
967 (GrGLint*)&format->fStencilBits);
968 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000969 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000970 GR_GL_RENDERBUFFER_DEPTH_SIZE,
971 (GrGLint*)&format->fTotalBits);
972 format->fTotalBits += format->fStencilBits;
973 } else {
974 format->fTotalBits = format->fStencilBits;
975 }
976 }
977}
978}
979
980bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
981 int width, int height) {
982
983 // All internally created RTs are also textures. We don't create
984 // SBs for a client's standalone RT (that is RT that isnt also a texture).
985 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +0000986 GrAssert(width >= rt->width());
987 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000988
989 int samples = rt->numSamples();
990 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000991 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000992 if (!sbID) {
993 return false;
994 }
995
996 GrGLStencilBuffer* sb = NULL;
997
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000998 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000999 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001000 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001001 // we start with the last stencil format that succeeded in hopes
1002 // that we won't go through this loop more than once after the
1003 // first (painful) stencil creation.
1004 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001005 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001006 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007 // version on a GL that doesn't have an MSAA extension.
1008 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001009 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1010 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001011 GR_GL_RENDERBUFFER,
1012 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001013 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001014 width,
1015 height));
1016 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001017 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1018 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001019 sFmt.fInternalFormat,
1020 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001021 }
1022
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001023 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001024 if (err == GR_GL_NO_ERROR) {
1025 // After sized formats we attempt an unsized format and take whatever
1026 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001027 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001028 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001029 sb = new GrGLStencilBuffer(this, sbID, width, height,
1030 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001031 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1032 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001033 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001034 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001035 return true;
1036 }
1037 sb->abandon(); // otherwise we lose sbID
1038 sb->unref();
1039 }
1040 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001041 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001042 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001043}
1044
1045bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1046 GrRenderTarget* rt) {
1047 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1048
1049 GrGLuint fbo = glrt->renderFBOID();
1050
1051 if (NULL == sb) {
1052 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001053 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001054 GR_GL_STENCIL_ATTACHMENT,
1055 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001056 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001057 GR_GL_DEPTH_ATTACHMENT,
1058 GR_GL_RENDERBUFFER, 0));
1059#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001060 GrGLenum status;
1061 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1063#endif
1064 }
1065 return true;
1066 } else {
1067 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1068 GrGLuint rb = glsb->renderbufferID();
1069
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001071 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1072 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001073 GR_GL_STENCIL_ATTACHMENT,
1074 GR_GL_RENDERBUFFER, rb));
1075 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001076 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 GR_GL_DEPTH_ATTACHMENT,
1078 GR_GL_RENDERBUFFER, rb));
1079 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001080 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001081 GR_GL_DEPTH_ATTACHMENT,
1082 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001083 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001084
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001085 GrGLenum status;
1086 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001087 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001088 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089 GR_GL_STENCIL_ATTACHMENT,
1090 GR_GL_RENDERBUFFER, 0));
1091 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001092 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001093 GR_GL_DEPTH_ATTACHMENT,
1094 GR_GL_RENDERBUFFER, 0));
1095 }
1096 return false;
1097 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001098 return true;
1099 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001101}
1102
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001103////////////////////////////////////////////////////////////////////////////////
1104
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001105GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001106 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001109 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001110 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001112 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1114 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1115 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1116 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1117 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001118 // deleting bound buffer does implicit bind to 0
1119 fHWGeometryState.fVertexBuffer = NULL;
1120 return NULL;
1121 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001122 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001123 size, dynamic);
1124 fHWGeometryState.fVertexBuffer = vertexBuffer;
1125 return vertexBuffer;
1126 }
1127 return NULL;
1128}
1129
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001130GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001131 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001132 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001133 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001134 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1135 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001136 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001137 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1138 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1139 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1140 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1141 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 // deleting bound buffer does implicit bind to 0
1143 fHWGeometryState.fIndexBuffer = NULL;
1144 return NULL;
1145 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001146 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 size, dynamic);
1148 fHWGeometryState.fIndexBuffer = indexBuffer;
1149 return indexBuffer;
1150 }
1151 return NULL;
1152}
1153
reed@google.comac10a2d2010-12-22 21:39:39 +00001154void GrGpuGL::flushScissor(const GrIRect* rect) {
1155 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001156 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001157 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001158
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001159 GrGLIRect scissor;
1160 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001161 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001162 rect->width(), rect->height());
1163 if (scissor.contains(vp)) {
1164 rect = NULL;
1165 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001166 }
1167
1168 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001169 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001170 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001171 fHWBounds.fScissorRect = scissor;
1172 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001173 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001174 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001175 fHWBounds.fScissorEnabled = true;
1176 }
1177 } else {
1178 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001179 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001180 fHWBounds.fScissorEnabled = false;
1181 }
1182 }
1183}
1184
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001185void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001186 // parent class should never let us get here with no RT
1187 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1188
bsalomon@google.com74b98712011-11-11 19:46:16 +00001189 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001190 if (NULL != rect) {
1191 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001192 clippedRect = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001193 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1194 fCurrDrawState.fRenderTarget->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001195 if (clippedRect.intersect(rtRect)) {
1196 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001197 } else {
1198 return;
1199 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001200 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001201 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001202 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001203
1204 GrGLfloat r, g, b, a;
1205 static const GrGLfloat scale255 = 1.f / 255.f;
1206 a = GrColorUnpackA(color) * scale255;
1207 GrGLfloat scaleRGB = scale255;
1208 if (GrPixelConfigIsUnpremultiplied(fCurrDrawState.fRenderTarget->config())) {
1209 scaleRGB *= a;
1210 }
1211 r = GrColorUnpackR(color) * scaleRGB;
1212 g = GrColorUnpackG(color) * scaleRGB;
1213 b = GrColorUnpackB(color) * scaleRGB;
1214
1215 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001216 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001217 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001218 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001219}
1220
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001221void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001222 if (NULL == fCurrDrawState.fRenderTarget) {
1223 return;
1224 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001225
1226 this->flushRenderTarget(&GrIRect::EmptyIRect());
1227
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001229 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001230 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 GL_CALL(StencilMask(0xffffffff));
1233 GL_CALL(ClearStencil(0));
1234 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001235 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001236}
1237
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001238void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001239 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240
1241 // this should only be called internally when we know we have a
1242 // stencil buffer.
1243 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 GrGLint stencilBitCount =
1245 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001246#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001247 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001248 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001249#else
1250 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001252 // turned into draws. Our contract on GrDrawTarget says that
1253 // changing the clip between stencil passes may or may not
1254 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001255 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001256#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001257 GrGLint value;
1258 if (insideClip) {
1259 value = (1 << (stencilBitCount - 1));
1260 } else {
1261 value = 0;
1262 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001263 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001264 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001265 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001266 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001267 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001268 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001269}
1270
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001271void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001272 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001273}
1274
bsalomon@google.comc4364992011-11-07 15:54:49 +00001275bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1276 int left, int top,
1277 int width, int height,
1278 GrPixelConfig config,
1279 size_t rowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001280 // if we have to do memcpy to handle non-trim rowBytes then we
1281 // get the flip for free. Otherwise it costs.
1282 return this->glCaps().fPackRowLengthSupport ||
1283 0 == rowBytes ||
1284 GrBytesPerPixel(config) * width == rowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001285}
1286
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001287bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001288 int left, int top,
1289 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001290 GrPixelConfig config,
1291 void* buffer,
1292 size_t rowBytes,
1293 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001294 GrGLenum internalFormat; // we don't use this for glReadPixels
1295 GrGLenum format;
1296 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1298 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001299 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001300
bsalomon@google.comc6980972011-11-02 19:57:21 +00001301 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001302 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1303 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1304 switch (tgt->getResolveType()) {
1305 case GrGLRenderTarget::kCantResolve_ResolveType:
1306 return false;
1307 case GrGLRenderTarget::kAutoResolves_ResolveType:
1308 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1309 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001310 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001311 break;
1312 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001313 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001314 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001315 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1316 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001317 break;
1318 default:
1319 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 }
1321
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001322 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001323
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001324 // the read rect is viewport-relative
1325 GrGLIRect readRect;
1326 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001327
1328 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1329 if (0 == rowBytes) {
1330 rowBytes = tightRowBytes;
1331 }
1332 size_t readDstRowBytes = tightRowBytes;
1333 void* readDst = buffer;
1334
1335 // determine if GL can read using the passed rowBytes or if we need
1336 // a scratch buffer.
1337 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1338 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001339 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001340 GrAssert(!(rowBytes % sizeof(GrColor)));
1341 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1342 readDstRowBytes = rowBytes;
1343 } else {
1344 scratch.reset(tightRowBytes * height);
1345 readDst = scratch.get();
1346 }
1347 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001348 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1349 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001350 format, type, readDst));
1351 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001352 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001353 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1354 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001355
1356 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001357 // API presents top-to-bottom. We must preserve the padding contents. Note
1358 // that the above readPixels did not overwrite the padding.
1359 if (readDst == buffer) {
1360 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001361 if (!invertY) {
1362 scratch.reset(tightRowBytes);
1363 void* tmpRow = scratch.get();
1364 // flip y in-place by rows
1365 const int halfY = height >> 1;
1366 char* top = reinterpret_cast<char*>(buffer);
1367 char* bottom = top + (height - 1) * rowBytes;
1368 for (int y = 0; y < halfY; y++) {
1369 memcpy(tmpRow, top, tightRowBytes);
1370 memcpy(top, bottom, tightRowBytes);
1371 memcpy(bottom, tmpRow, tightRowBytes);
1372 top += rowBytes;
1373 bottom -= rowBytes;
1374 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001375 }
1376 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001377 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001378 // copy from readDst to buffer while flipping y
1379 const int halfY = height >> 1;
1380 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001381 char* dst = reinterpret_cast<char*>(buffer);
1382 if (!invertY) {
1383 dst += (height-1) * rowBytes;
1384 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001385 for (int y = 0; y < height; y++) {
1386 memcpy(dst, src, tightRowBytes);
1387 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001388 if (invertY) {
1389 dst += rowBytes;
1390 } else {
1391 dst -= rowBytes;
1392 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 }
1394 }
1395 return true;
1396}
1397
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001398void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001399
1400 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1401
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001402 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001403 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001404 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001405 #if GR_COLLECT_STATS
1406 ++fStats.fRenderTargetChngCnt;
1407 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001409 GrGLenum status;
1410 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001411 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001412 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 }
1414 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001415 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001416 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001417 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001418 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001419 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001420 fHWBounds.fViewportRect = vp;
1421 }
1422 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001423 if (NULL == bound || !bound->isEmpty()) {
1424 rt->flagAsNeedingResolve(bound);
1425 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001426}
1427
twiz@google.com0f31ca72011-03-18 17:38:11 +00001428GrGLenum gPrimitiveType2GLMode[] = {
1429 GR_GL_TRIANGLES,
1430 GR_GL_TRIANGLE_STRIP,
1431 GR_GL_TRIANGLE_FAN,
1432 GR_GL_POINTS,
1433 GR_GL_LINES,
1434 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001435};
1436
bsalomon@google.comd302f142011-03-03 13:54:13 +00001437#define SWAP_PER_DRAW 0
1438
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001439#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001440 #if GR_MAC_BUILD
1441 #include <AGL/agl.h>
1442 #elif GR_WIN32_BUILD
1443 void SwapBuf() {
1444 DWORD procID = GetCurrentProcessId();
1445 HWND hwnd = GetTopWindow(GetDesktopWindow());
1446 while(hwnd) {
1447 DWORD wndProcID = 0;
1448 GetWindowThreadProcessId(hwnd, &wndProcID);
1449 if(wndProcID == procID) {
1450 SwapBuffers(GetDC(hwnd));
1451 }
1452 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1453 }
1454 }
1455 #endif
1456#endif
1457
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001458void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1459 uint32_t startVertex,
1460 uint32_t startIndex,
1461 uint32_t vertexCount,
1462 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1464
twiz@google.com0f31ca72011-03-18 17:38:11 +00001465 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001466
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001467 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1468 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1469
1470 // our setupGeometry better have adjusted this to zero since
1471 // DrawElements always draws from the begining of the arrays for idx 0.
1472 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001473
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001474 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1475 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001476#if SWAP_PER_DRAW
1477 glFlush();
1478 #if GR_MAC_BUILD
1479 aglSwapBuffers(aglGetCurrentContext());
1480 int set_a_break_pt_here = 9;
1481 aglSwapBuffers(aglGetCurrentContext());
1482 #elif GR_WIN32_BUILD
1483 SwapBuf();
1484 int set_a_break_pt_here = 9;
1485 SwapBuf();
1486 #endif
1487#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001488}
1489
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001490void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1491 uint32_t startVertex,
1492 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001493 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1494
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001495 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1496
1497 // our setupGeometry better have adjusted this to zero.
1498 // DrawElements doesn't take an offset so we always adjus the startVertex.
1499 GrAssert(0 == startVertex);
1500
1501 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1502 // account for startVertex in the DrawElements case. So we always
1503 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001504 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001505#if SWAP_PER_DRAW
1506 glFlush();
1507 #if GR_MAC_BUILD
1508 aglSwapBuffers(aglGetCurrentContext());
1509 int set_a_break_pt_here = 9;
1510 aglSwapBuffers(aglGetCurrentContext());
1511 #elif GR_WIN32_BUILD
1512 SwapBuf();
1513 int set_a_break_pt_here = 9;
1514 SwapBuf();
1515 #endif
1516#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001517}
1518
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001519void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001520
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001521 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001522 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001523 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001524 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1525 rt->renderFBOID()));
1526 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1527 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001528 #if GR_COLLECT_STATS
1529 ++fStats.fRenderTargetChngCnt;
1530 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001531 // make sure we go through flushRenderTarget() since we've modified
1532 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001533 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001534 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001535 const GrIRect dirtyRect = rt->getResolveRect();
1536 GrGLIRect r;
1537 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1538 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001539
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001540 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001541 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001542 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1543 GL_CALL(Scissor(r.fLeft, r.fBottom,
1544 r.fWidth, r.fHeight));
1545 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001546 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001547 fHWBounds.fScissorEnabled = true;
1548 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001549 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001550 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001551 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1552 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001553 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001554 int right = r.fLeft + r.fWidth;
1555 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001556 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1557 r.fLeft, r.fBottom, right, top,
1558 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001559 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001560 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001561 }
1562}
1563
twiz@google.com0f31ca72011-03-18 17:38:11 +00001564static const GrGLenum grToGLStencilFunc[] = {
1565 GR_GL_ALWAYS, // kAlways_StencilFunc
1566 GR_GL_NEVER, // kNever_StencilFunc
1567 GR_GL_GREATER, // kGreater_StencilFunc
1568 GR_GL_GEQUAL, // kGEqual_StencilFunc
1569 GR_GL_LESS, // kLess_StencilFunc
1570 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1571 GR_GL_EQUAL, // kEqual_StencilFunc,
1572 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001573};
1574GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1575GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1576GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1577GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1578GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1579GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1580GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1581GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1582GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1583
twiz@google.com0f31ca72011-03-18 17:38:11 +00001584static const GrGLenum grToGLStencilOp[] = {
1585 GR_GL_KEEP, // kKeep_StencilOp
1586 GR_GL_REPLACE, // kReplace_StencilOp
1587 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1588 GR_GL_INCR, // kIncClamp_StencilOp
1589 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1590 GR_GL_DECR, // kDecClamp_StencilOp
1591 GR_GL_ZERO, // kZero_StencilOp
1592 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001593};
1594GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1595GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1596GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1597GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1598GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1599GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1600GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1601GR_STATIC_ASSERT(6 == kZero_StencilOp);
1602GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1603
reed@google.comac10a2d2010-12-22 21:39:39 +00001604void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001605 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001606
1607 // use stencil for clipping if clipping is enabled and the clip
1608 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001609 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001611 bool stencilChange = fHWStencilClip != stencilClip ||
1612 fHWDrawState.fStencilSettings != *settings ||
1613 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1614 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001615
1616 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001617
bsalomon@google.comd302f142011-03-03 13:54:13 +00001618 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1619 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001620
bsalomon@google.comd302f142011-03-03 13:54:13 +00001621 if (settings->isDisabled()) {
1622 if (stencilClip) {
1623 settings = &gClipStencilSettings;
1624 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001625 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001626
1627 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001628 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001629 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001630 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001631 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001632 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001633 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1634 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1635 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1636 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1637 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1638 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1639 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1640 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1641 }
1642 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001643 int stencilBits = 0;
1644 GrStencilBuffer* stencilBuffer =
1645 fCurrDrawState.fRenderTarget->getStencilBuffer();
1646 if (NULL != stencilBuffer) {
1647 stencilBits = stencilBuffer->bits();
1648 }
1649 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001650 GrAssert(stencilBits ||
1651 (GrStencilSettings::gDisabled ==
1652 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001653 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1654 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001655
1656 unsigned int frontRef = settings->fFrontFuncRef;
1657 unsigned int frontMask = settings->fFrontFuncMask;
1658 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001659 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001660
1661 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1662
1663 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1664 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1665 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001666 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
1667 stencilClip, settings->fFrontFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001668
1669 ConvertStencilFuncAndMask(settings->fFrontFunc,
1670 stencilClip,
1671 clipStencilMask,
1672 userStencilMask,
1673 &frontRef,
1674 &frontMask);
1675 frontWriteMask &= userStencilMask;
1676 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001677 GrAssert((size_t)
1678 settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1679 GrAssert((size_t)
1680 settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
1681 GrAssert((size_t)
1682 settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1683 GrAssert((size_t)
1684 settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001685 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001686 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001687
1688 unsigned int backRef = settings->fBackFuncRef;
1689 unsigned int backMask = settings->fBackFuncMask;
1690 unsigned int backWriteMask = settings->fBackWriteMask;
1691
1692
1693 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1694 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1695 backFunc = grToGLStencilFunc[settings->fBackFunc];
1696 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001697 backFunc = grToGLStencilFunc[ConvertStencilFunc(
1698 stencilClip, settings->fBackFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001699 ConvertStencilFuncAndMask(settings->fBackFunc,
1700 stencilClip,
1701 clipStencilMask,
1702 userStencilMask,
1703 &backRef,
1704 &backMask);
1705 backWriteMask &= userStencilMask;
1706 }
1707
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001708 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1709 frontRef, frontMask));
1710 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1711 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1712 backRef, backMask));
1713 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1714 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1715 grToGLStencilOp[settings->fFrontFailOp],
1716 grToGLStencilOp[settings->fFrontPassOp],
1717 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001718
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001719 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1720 grToGLStencilOp[settings->fBackFailOp],
1721 grToGLStencilOp[settings->fBackPassOp],
1722 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001723 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001724 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1725 GL_CALL(StencilMask(frontWriteMask));
1726 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001727 grToGLStencilOp[settings->fFrontPassOp],
1728 grToGLStencilOp[settings->fFrontPassOp]));
1729 }
1730 }
1731 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001732 fHWStencilClip = stencilClip;
1733 }
1734}
1735
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001736void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001737 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001738 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1739 // smooth lines.
1740
1741 // we prefer smooth lines over multisampled lines
1742 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001743 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001744 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001745 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001746 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001747 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001748 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001749 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001750 fHWAAState.fSmoothLineEnabled = false;
1751 }
1752 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1753 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001754 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001755 fHWAAState.fMSAAEnabled = false;
1756 }
1757 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001758 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001759 fHWAAState.fMSAAEnabled) {
1760 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001761 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001762 fHWAAState.fMSAAEnabled = false;
1763 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001764 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001765 fHWAAState.fMSAAEnabled = true;
1766 }
1767 }
1768 }
1769}
1770
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001771void GrGpuGL::flushBlend(GrPrimitiveType type,
1772 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001773 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001774 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001775 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001776 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001777 fHWBlendDisabled = false;
1778 }
1779 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1780 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001781 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1782 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001783 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1784 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1785 }
1786 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001787 // any optimization to disable blending should
1788 // have already been applied and tweaked the coeffs
1789 // to (1, 0).
1790 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1791 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001792 if (fHWBlendDisabled != blendOff) {
1793 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001794 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001795 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001796 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001797 }
1798 fHWBlendDisabled = blendOff;
1799 }
1800 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001801 if (fHWDrawState.fSrcBlend != srcCoeff ||
1802 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001803 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1804 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001805 fHWDrawState.fSrcBlend = srcCoeff;
1806 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001807 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001808 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1809 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001810 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1811
1812 float c[] = {
1813 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1814 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1815 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1816 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1817 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001818 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001819 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1820 }
1821 }
1822 }
1823}
1824
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001825namespace {
1826
1827unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001828 switch (filter) {
1829 case GrSamplerState::kBilinear_Filter:
1830 case GrSamplerState::k4x4Downsample_Filter:
1831 return GR_GL_LINEAR;
1832 case GrSamplerState::kNearest_Filter:
1833 case GrSamplerState::kConvolution_Filter:
1834 return GR_GL_NEAREST;
1835 default:
1836 GrAssert(!"Unknown filter type");
1837 return GR_GL_LINEAR;
1838 }
1839}
1840
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001841const GrGLenum* get_swizzle(GrPixelConfig config,
1842 const GrSamplerState& sampler) {
1843 if (GrPixelConfigIsAlphaOnly(config)) {
1844 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
1845 GR_GL_ALPHA, GR_GL_ALPHA };
1846 return gAlphaSmear;
1847 } else if (sampler.swapsRAndB()) {
1848 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
1849 GR_GL_RED, GR_GL_ALPHA };
1850 return gRedBlueSwap;
1851 } else {
1852 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
1853 GR_GL_BLUE, GR_GL_ALPHA };
1854 return gStraight;
1855 }
1856}
1857
1858void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
1859 // should add texparameteri to interface to make 1 instead of 4 calls here
1860 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1861 GR_GL_TEXTURE_SWIZZLE_R,
1862 swizzle[0]));
1863 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1864 GR_GL_TEXTURE_SWIZZLE_G,
1865 swizzle[1]));
1866 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1867 GR_GL_TEXTURE_SWIZZLE_B,
1868 swizzle[2]));
1869 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1870 GR_GL_TEXTURE_SWIZZLE_A,
1871 swizzle[3]));
1872}
1873}
1874
bsalomon@google.comffca4002011-02-22 20:34:01 +00001875bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001876
1877 // GrGpu::setupClipAndFlushState should have already checked this
1878 // and bailed if not true.
1879 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001880
tomhudson@google.com93813632011-10-27 20:21:16 +00001881 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001882 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001883 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001884 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001885
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001886 // true for now, but maybe not with GrEffect.
1887 GrAssert(NULL != nextTexture);
1888 // if we created a rt/tex and rendered to it without using a
1889 // texture and now we're texuring from the rt it will still be
1890 // the last bound texture, but it needs resolving. So keep this
1891 // out of the "last != next" check.
1892 GrGLRenderTarget* texRT =
1893 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1894 if (NULL != texRT) {
1895 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001896 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001897
1898 if (fHWDrawState.fTextures[s] != nextTexture) {
1899 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001900 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001901 #if GR_COLLECT_STATS
1902 ++fStats.fTextureChngCnt;
1903 #endif
1904 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1905 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001906 // The texture matrix has to compensate for texture width/height
1907 // and NPOT-embedded-in-POT
1908 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001909 }
1910
1911 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001912 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001913 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001914 nextTexture->getCachedTexParams(&timestamp);
1915 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001916 GrGLTexture::TexParams newTexParams;
1917
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001918 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001919
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00001920 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001921 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1922 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001923 memcpy(newTexParams.fSwizzleRGBA,
1924 get_swizzle(nextTexture->config(), sampler),
1925 sizeof(newTexParams.fSwizzleRGBA));
1926 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001927 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001928 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001929 GR_GL_TEXTURE_MAG_FILTER,
1930 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001931 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001932 GR_GL_TEXTURE_MIN_FILTER,
1933 newTexParams.fFilter));
1934 }
1935 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
1936 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001938 GR_GL_TEXTURE_WRAP_S,
1939 newTexParams.fWrapS));
1940 }
1941 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
1942 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001943 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001944 GR_GL_TEXTURE_WRAP_T,
1945 newTexParams.fWrapT));
1946 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001947 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001948 (setAll ||
1949 memcmp(newTexParams.fSwizzleRGBA,
1950 oldTexParams.fSwizzleRGBA,
1951 sizeof(newTexParams.fSwizzleRGBA)))) {
1952 setTextureUnit(s);
1953 set_tex_swizzle(newTexParams.fSwizzleRGBA,
1954 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001955 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001956 nextTexture->setCachedTexParams(newTexParams,
1957 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001958 }
1959 }
1960
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001961 GrIRect* rect = NULL;
1962 GrIRect clipBounds;
1963 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
1964 fClip.hasConservativeBounds()) {
1965 fClip.getConservativeBounds().roundOut(&clipBounds);
1966 rect = &clipBounds;
1967 }
1968 this->flushRenderTarget(rect);
1969 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001970
reed@google.comac10a2d2010-12-22 21:39:39 +00001971 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
1972 (fHWDrawState.fFlagBits & kDither_StateBit)) {
1973 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001974 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00001975 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001976 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00001977 }
1978 }
1979
bsalomon@google.comd302f142011-03-03 13:54:13 +00001980 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
1981 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001982 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001983 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001984 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001985 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001986 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001987 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001988 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001989 }
1990
bsalomon@google.comd302f142011-03-03 13:54:13 +00001991 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
1992 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00001993 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001994 GL_CALL(Enable(GR_GL_CULL_FACE));
1995 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001996 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00001997 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001998 GL_CALL(Enable(GR_GL_CULL_FACE));
1999 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002000 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002001 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002002 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002003 break;
2004 default:
2005 GrCrash("Unknown draw face.");
2006 }
2007 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2008 }
2009
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002010#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002011 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002012 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002013 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002014 NULL == fCurrDrawState.fRenderTarget ||
2015 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002016 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002017 fCurrDrawState.fRenderTarget);
2018 }
2019#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002020
reed@google.comac10a2d2010-12-22 21:39:39 +00002021 flushStencil();
2022
bsalomon@google.comd302f142011-03-03 13:54:13 +00002023 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002024 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002025 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002026}
2027
2028void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002029 if (fHWGeometryState.fVertexBuffer != buffer) {
2030 fHWGeometryState.fArrayPtrsDirty = true;
2031 fHWGeometryState.fVertexBuffer = buffer;
2032 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002033}
2034
2035void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002036 if (fHWGeometryState.fVertexBuffer == buffer) {
2037 // deleting bound buffer does implied bind to 0
2038 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002039 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002040 }
2041}
2042
2043void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002044 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002045}
2046
2047void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002048 if (fHWGeometryState.fIndexBuffer == buffer) {
2049 // deleting bound buffer does implied bind to 0
2050 fHWGeometryState.fIndexBuffer = NULL;
2051 }
2052}
2053
reed@google.comac10a2d2010-12-22 21:39:39 +00002054void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2055 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002056 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002057 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002058 }
2059 if (fHWDrawState.fRenderTarget == renderTarget) {
2060 fHWDrawState.fRenderTarget = NULL;
2061 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002062}
2063
2064void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002065 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002066 if (fCurrDrawState.fTextures[s] == texture) {
2067 fCurrDrawState.fTextures[s] = NULL;
2068 }
2069 if (fHWDrawState.fTextures[s] == texture) {
2070 // deleting bound texture does implied bind to 0
2071 fHWDrawState.fTextures[s] = NULL;
2072 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002073 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002074}
2075
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002076bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002077 GrGLenum* internalFormat,
2078 GrGLenum* format,
2079 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002080 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002081 case kRGBA_8888_PM_GrPixelConfig:
2082 case kRGBA_8888_UPM_GrPixelConfig:
2083 *format = GR_GL_RGBA;
2084 *internalFormat = GR_GL_RGBA;
2085 *type = GR_GL_UNSIGNED_BYTE;
2086 break;
2087 case kBGRA_8888_PM_GrPixelConfig:
2088 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002089 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002090 return false;
2091 }
2092 *format = GR_GL_BGRA;
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002093 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002094 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002095 } else {
2096 *internalFormat = GR_GL_RGBA;
2097 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002098 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002099 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002100 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002101 *format = GR_GL_RGB;
2102 *internalFormat = GR_GL_RGB;
2103 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002104 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002105 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002106 *format = GR_GL_RGBA;
2107 *internalFormat = GR_GL_RGBA;
2108 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002109 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002110 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002111 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002112 *format = GR_GL_PALETTE8_RGBA8;
2113 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002114 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002115 } else {
2116 return false;
2117 }
2118 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002119 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002120 *format = GR_GL_ALPHA;
2121 *internalFormat = GR_GL_ALPHA;
2122 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002123 break;
2124 default:
2125 return false;
2126 }
2127 return true;
2128}
2129
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002130void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002131 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002132 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002133 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002134 fActiveTextureUnitIdx = unit;
2135 }
2136}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002137
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002138void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002139 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002140 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002141 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2142 }
2143}
2144
reed@google.comac10a2d2010-12-22 21:39:39 +00002145/* On ES the internalFormat and format must match for TexImage and we use
2146 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2147 decide the internalFormat. However, on ES internalFormat for
2148 RenderBufferStorage* has to be a specific format (not a base format like
2149 GL_RGBA).
2150 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002151bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002152 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002153 // The ES story for BGRA and RenderbufferStorage appears murky. It
2154 // takes an internal format as a parameter. The OES FBO extension and
2155 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2156 // BGRA extensions adds BGRA as both an internal and external format
2157 // and the other only as an external format (like desktop GL). OES
2158 // restricts RenderbufferStorage's format to a *sized* internal format.
2159 // There is no sized BGRA internal format.
2160 // So if the texture has internal format BGRA we just hope that the
2161 // resolve blit can do RGBA->BGRA internal format conversion.
2162 case kRGBA_8888_PM_GrPixelConfig:
2163 case kRGBA_8888_UPM_GrPixelConfig:
2164 case kBGRA_8888_PM_GrPixelConfig:
2165 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002166 if (fGLCaps.fRGBA8RenderbufferSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002167 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2168 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002169 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002170 return true;
2171 } else {
2172 return false;
2173 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002174 case kRGB_565_GrPixelConfig:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002175 // ES2 supports 565, but desktop GL does not.
bsalomon@google.comc4364992011-11-07 15:54:49 +00002176 if (kDesktop_GrGLBinding != this->glBinding()) {
2177 *format = GR_GL_RGB565;
2178 return true;
2179 } else {
2180 return false;
2181 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002182 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002183 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002184 return true;
2185 default:
2186 return false;
2187 }
2188}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002189
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002190void GrGpuGL::resetDirtyFlags() {
2191 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2192}
2193
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002194void GrGpuGL::setBuffers(bool indexed,
2195 int* extraVertexOffset,
2196 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002197
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002198 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002199
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002200 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2201
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002202 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002203 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002204 case kBuffer_GeometrySrcType:
2205 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002206 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002207 break;
2208 case kArray_GeometrySrcType:
2209 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002210 this->finalizeReservedVertices();
2211 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2212 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002213 break;
2214 default:
2215 vbuf = NULL; // suppress warning
2216 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002217 }
2218
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002219 GrAssert(NULL != vbuf);
2220 GrAssert(!vbuf->isLocked());
2221 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002222 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002223 fHWGeometryState.fArrayPtrsDirty = true;
2224 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002225 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002226
2227 if (indexed) {
2228 GrAssert(NULL != extraIndexOffset);
2229
2230 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002231 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002232 case kBuffer_GeometrySrcType:
2233 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002234 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002235 break;
2236 case kArray_GeometrySrcType:
2237 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002238 this->finalizeReservedIndices();
2239 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2240 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002241 break;
2242 default:
2243 ibuf = NULL; // suppress warning
2244 GrCrash("Unknown geometry src type!");
2245 }
2246
2247 GrAssert(NULL != ibuf);
2248 GrAssert(!ibuf->isLocked());
2249 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002250 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002251 fHWGeometryState.fIndexBuffer = ibuf;
2252 }
2253 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002254}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002255
2256int GrGpuGL::getMaxEdges() const {
2257 // FIXME: This is a pessimistic estimate based on how many other things
2258 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002259 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2260 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002261}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002262
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002263void GrGpuGL::GLCaps::print() const {
2264 for (int i = 0; i < fStencilFormats.count(); ++i) {
2265 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2266 i,
2267 fStencilFormats[i].fStencilBits,
2268 fStencilFormats[i].fTotalBits);
2269 }
2270
2271 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2272 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2273 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2274 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2275 static const char* gMSFBOExtStr[] = {
2276 "None",
2277 "ARB",
2278 "EXT",
2279 "Apple",
2280 };
2281 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002282 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002283 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2284 }
2285 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2286 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002287 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002288 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002289 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002290 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002291 (fTextureSwizzleSupport ? "YES": "NO"));
2292 GrPrintf("Unpack Row length support: %s\n",
2293 (fUnpackRowLengthSupport ? "YES": "NO"));
2294 GrPrintf("Pack Row length support: %s\n",
2295 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002296}