blob: 83087f46945964e573c2289b7cf17f8b793e60bd [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
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
31#else
32 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
twiz@google.com0f31ca72011-03-18 17:38:11 +000037static const GrGLenum gXfermodeCoeff2Blend[] = {
38 GR_GL_ZERO,
39 GR_GL_ONE,
40 GR_GL_SRC_COLOR,
41 GR_GL_ONE_MINUS_SRC_COLOR,
42 GR_GL_DST_COLOR,
43 GR_GL_ONE_MINUS_DST_COLOR,
44 GR_GL_SRC_ALPHA,
45 GR_GL_ONE_MINUS_SRC_ALPHA,
46 GR_GL_DST_ALPHA,
47 GR_GL_ONE_MINUS_DST_ALPHA,
48 GR_GL_CONSTANT_COLOR,
49 GR_GL_ONE_MINUS_CONSTANT_COLOR,
50 GR_GL_CONSTANT_ALPHA,
51 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000052
53 // extended blend coeffs
54 GR_GL_SRC1_COLOR,
55 GR_GL_ONE_MINUS_SRC1_COLOR,
56 GR_GL_SRC1_ALPHA,
57 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000058};
59
bsalomon@google.com271cffc2011-05-20 14:13:56 +000060bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000061 static const bool gCoeffReferencesBlendConst[] = {
62 false,
63 false,
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 true,
73 true,
74 true,
75 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000076
77 // extended blend coeffs
78 false,
79 false,
80 false,
81 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000082 };
83 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000084 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
85
86 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
87 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
88 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
89 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
90 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
91 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
92 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
93 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
94 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
95 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
96 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
97 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
98 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
99 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
100
101 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
102 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
103 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
104 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
105
106 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
107 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000108}
109
reed@google.comac10a2d2010-12-22 21:39:39 +0000110///////////////////////////////////////////////////////////////////////////////
111
bsalomon@google.comd302f142011-03-03 13:54:13 +0000112void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
113 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000114 GrMatrix* matrix) {
115 GrAssert(NULL != texture);
116 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000117 GrGLTexture::Orientation orientation = texture->orientation();
118 if (GrGLTexture::kBottomUp_Orientation == orientation) {
119 GrMatrix invY;
120 invY.setAll(GR_Scalar1, 0, 0,
121 0, -GR_Scalar1, GR_Scalar1,
122 0, 0, GrMatrix::I()[8]);
123 matrix->postConcat(invY);
124 } else {
125 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
126 }
127}
128
bsalomon@google.comd302f142011-03-03 13:54:13 +0000129bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000130 const GrSamplerState& sampler) {
131 GrAssert(NULL != texture);
132 if (!sampler.getMatrix().isIdentity()) {
133 return false;
134 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000135 GrGLTexture::Orientation orientation = texture->orientation();
136 if (GrGLTexture::kBottomUp_Orientation == orientation) {
137 return false;
138 } else {
139 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
140 }
141 return true;
142}
143
144///////////////////////////////////////////////////////////////////////////////
145
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000146static bool gPrintStartupSpew;
147
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000148static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000149
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000150 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000151
twiz@google.com0f31ca72011-03-18 17:38:11 +0000152 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000153 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
154 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000155 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000156 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
157 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000158 // some implementations require texture to be mip-map complete before
159 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000160 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_TEXTURE_MIN_FILTER,
162 GR_GL_NEAREST));
163 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
164 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
165 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
166 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
167 GR_GL_COLOR_ATTACHMENT0,
168 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000169 GrGLenum status;
170 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
172 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000173
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000174 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000175}
176
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000177GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000178
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000179 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000180
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000181 gl->ref();
182 fGL = gl;
183 fGLBinding = glBinding;
184 switch (glBinding) {
185 case kDesktop_GrGLBinding:
186 GrAssert(gl->supportsDesktop());
187 break;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000188 case kES2_GrGLBinding:
189 GrAssert(gl->supportsES2());
190 break;
191 default:
192 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000193 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000194
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000195 GrGLClearErr(fGL);
196
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000197 const GrGLubyte* ext;
198 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000199 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000200 const GrGLubyte* vendor;
201 const GrGLubyte* renderer;
202 const GrGLubyte* version;
203 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
204 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
205 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000206 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
207 this);
208 GrPrintf("------ VENDOR %s\n", vendor);
209 GrPrintf("------ RENDERER %s\n", renderer);
210 GrPrintf("------ VERSION %s\n", version);
211 GrPrintf("------ EXTENSIONS\n %s \n", ext);
212 }
213
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000214 fGLVersion = GrGLGetVersion(gl);
215 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000216 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000217
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000218 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000219
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000220 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000221
bsalomon@google.comfe676522011-06-17 18:12:21 +0000222 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000223}
224
225GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000226 // This must be called by before the GrDrawTarget destructor
227 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000228 // This subclass must do this before the base class destructor runs
229 // since we will unref the GrGLInterface.
230 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000231 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000232}
233
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234///////////////////////////////////////////////////////////////////////////////
235
236static const GrGLuint kUnknownBitCount = ~0;
237
238void GrGpuGL::initCaps() {
239 GrGLint maxTextureUnits;
240 // check FS and fixed-function texture unit limits
241 // we only use textures in the fragment stage currently.
242 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000243 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
244 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000245 if (kES2_GrGLBinding != this->glBinding()) {
246 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000247 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000248 }
249 if (kES2_GrGLBinding == this->glBinding()) {
250 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
251 &fGLCaps.fMaxFragmentUniformVectors);
252 } else if (kDesktop_GrGLBinding != this->glBinding()) {
253 GrGLint max;
254 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
255 fGLCaps.fMaxFragmentUniformVectors = max / 4;
256 } else {
257 fGLCaps.fMaxFragmentUniformVectors = 16;
258 }
259
260 GrGLint numFormats;
261 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
262 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
263 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
264 for (int i = 0; i < numFormats; ++i) {
265 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
266 fCaps.f8BitPaletteSupport = true;
267 break;
268 }
269 }
270
271 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000272 // we could also look for GL_ATI_separate_stencil extension or
273 // GL_EXT_stencil_two_side but they use different function signatures
274 // than GL2.0+ (and than each other).
275 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
276 // supported on GL 1.4 and higher or by extension
277 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
278 this->hasExtension("GL_EXT_stencil_wrap");
279 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000280 // ES 2 has two sided stencil and stencil wrap
281 fCaps.fTwoSidedStencilSupport = true;
282 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000283 }
284
285 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000286 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000287 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000288 fGLCaps.fRGBA8RenderbufferSupport =
289 this->hasExtension("GL_OES_rgb8_rgba8") ||
290 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000291 }
292
293
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000295 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
296 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 } else {
298 bool hasBGRAExt = false;
299 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000300 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000302 fGLCaps.fBGRAFormatSupport = true;
303 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000304 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000305 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000306 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000307 }
308
309 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000310 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000311 this->hasExtension("GL_ARB_texture_swizzle");
312 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000313 fGLCaps.fTextureSwizzleSupport = false;
314 }
315
316 if (kDesktop_GrGLBinding == this->glBinding()) {
317 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000318 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000319 fGLCaps.fPackRowLengthSupport = true;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000320 fGLCaps.fPackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000321 } else {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000322 fGLCaps.fUnpackRowLengthSupport =this->hasExtension("GL_EXT_unpack_subimage");
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000323 fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy");
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000324 // no extension for pack row length
325 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000326 fGLCaps.fPackFlipYSupport =
327 this->hasExtension("GL_ANGLE_pack_reverse_row_order");
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000328 }
329
330 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000331 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
332 // extension includes glMapBuffer.
333 } else {
334 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
335 }
336
337 if (kDesktop_GrGLBinding == this->glBinding()) {
338 if (fGLVersion >= GR_GL_VER(2,0) ||
339 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
340 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000341 } else {
342 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000343 }
344 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000345 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000346 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000347 }
348
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000349 fGLCaps.fTextureUsageSupport = (kES2_GrGLBinding == this->glBinding()) &&
350 this->hasExtension("GL_ANGLE_texture_usage");
351
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000352 // Tex storage is in desktop 4.2 and can be an extension to desktop or ES.
353 fGLCaps.fTexStorageSupport = (kDesktop_GrGLBinding == this->glBinding() &&
354 fGLVersion >= GR_GL_VER(4,2)) ||
355 this->hasExtension("GL_ARB_texture_storage") ||
356 this->hasExtension("GL_EXT_texture_storage");
357
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000358 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
359
360 ////////////////////////////////////////////////////////////////////////////
361 // Experiments to determine limitations that can't be queried.
362 // TODO: Make these a preprocess that generate some compile time constants.
363 // TODO: probe once at startup, rather than once per context creation.
364
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000365 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
366 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
367 // Our render targets are always created with textures as the color
368 // attachment, hence this min:
369 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
370
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000371 this->initFSAASupport();
372 this->initStencilFormats();
373}
374
375void GrGpuGL::initFSAASupport() {
376 // TODO: Get rid of GrAALevel and use # samples directly.
377 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
378 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
379 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
380 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
381 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
382
383 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
384 if (kDesktop_GrGLBinding != this->glBinding()) {
385 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
386 // chrome's extension is equivalent to the EXT msaa
387 // and fbo_blit extensions.
388 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
389 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
390 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
391 }
392 } else {
393 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
394 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
395 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
396 this->hasExtension("GL_EXT_framebuffer_blit")) {
397 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
398 }
399 }
400
401 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
402 GrGLint maxSamples;
403 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
404 if (maxSamples > 1 ) {
405 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
406 fGLCaps.fAASamples[kLow_GrAALevel] =
407 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
408 fGLCaps.fAASamples[kMed_GrAALevel] =
409 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
410 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
411 }
412 }
413 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
414}
415
416void GrGpuGL::initStencilFormats() {
417
418 // Build up list of legal stencil formats (though perhaps not supported on
419 // the particular gpu/driver) from most preferred to least.
420
421 // these consts are in order of most preferred to least preferred
422 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
423 static const GrGLStencilBuffer::Format
424 // internal Format stencil bits total bits packed?
425 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
426 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
427 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
428 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
429 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
430 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
431
432 if (kDesktop_GrGLBinding == this->glBinding()) {
433 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
434 this->hasExtension("GL_EXT_packed_depth_stencil") ||
435 this->hasExtension("GL_ARB_framebuffer_object");
436
437 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
438 // require FBO support we can expect these are legal formats and don't
439 // check. These also all support the unsized GL_STENCIL_INDEX.
440 fGLCaps.fStencilFormats.push_back() = gS8;
441 fGLCaps.fStencilFormats.push_back() = gS16;
442 if (supportsPackedDS) {
443 fGLCaps.fStencilFormats.push_back() = gD24S8;
444 }
445 fGLCaps.fStencilFormats.push_back() = gS4;
446 if (supportsPackedDS) {
447 fGLCaps.fStencilFormats.push_back() = gDS;
448 }
449 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000450 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
451 // for other formats.
452 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000453
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000454 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000455 //fStencilFormats.push_back() = gS16;
456 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
457 fGLCaps.fStencilFormats.push_back() = gD24S8;
458 }
459 if (this->hasExtension("GL_OES_stencil4")) {
460 fGLCaps.fStencilFormats.push_back() = gS4;
461 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000462 }
463}
464
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000465GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000466 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
467 return GrPixelConfigSwapRAndB(config);
468 } else {
469 return config;
470 }
471}
472
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000473GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000474 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000475 return GrPixelConfigSwapRAndB(config);
476 } else {
477 return config;
478 }
479}
480
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000481bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
482 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
483}
484
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000485void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000486 if (gPrintStartupSpew && !fPrintedCaps) {
487 fPrintedCaps = true;
488 this->getCaps().print();
489 fGLCaps.print();
490 }
491
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000492 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000494 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000496 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000497 GL_CALL(Disable(GR_GL_DEPTH_TEST));
498 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000499
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000500 GL_CALL(Disable(GR_GL_CULL_FACE));
501 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000504 GL_CALL(Disable(GR_GL_DITHER));
505 if (kDesktop_GrGLBinding == this->glBinding()) {
506 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
507 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
508 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000509 fHWAAState.fMSAAEnabled = false;
510 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000511 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000512
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000513 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000514 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000515
reed@google.comac10a2d2010-12-22 21:39:39 +0000516 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000517 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000518
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000519 // invalid
520 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000521
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000523 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000524
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000525 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000526 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000527
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000528 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000529
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000530 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000531
tomhudson@google.com93813632011-10-27 20:21:16 +0000532 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000533 fHWDrawState.setTexture(s, NULL);
534 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
535 -GR_ScalarMax,
536 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000537 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000538 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000539 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000540
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000541 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000542 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000543 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000544 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000545
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000546 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000547 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000548 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000549
550 fHWGeometryState.fIndexBuffer = NULL;
551 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000552
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000553 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000555 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000556 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000557
558 // we assume these values
559 if (this->glCaps().fUnpackRowLengthSupport) {
560 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
561 }
562 if (this->glCaps().fPackRowLengthSupport) {
563 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
564 }
565 if (this->glCaps().fUnpackFlipYSupport) {
566 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
567 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000568 if (this->glCaps().fPackFlipYSupport) {
569 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
570 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000571}
572
bsalomon@google.come269f212011-11-07 13:29:52 +0000573GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000574 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000575 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000576 return NULL;
577 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000578
bsalomon@google.com99621082011-11-15 16:47:16 +0000579 glTexDesc.fWidth = desc.fWidth;
580 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000581 glTexDesc.fConfig = desc.fConfig;
582 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
583 glTexDesc.fOwnsID = false;
584 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
585
586 GrGLTexture* texture = NULL;
587 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
588 GrGLRenderTarget::Desc glRTDesc;
589 glRTDesc.fRTFBOID = 0;
590 glRTDesc.fTexFBOID = 0;
591 glRTDesc.fMSColorRenderbufferID = 0;
592 glRTDesc.fOwnIDs = true;
593 glRTDesc.fConfig = desc.fConfig;
594 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000595 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
596 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000597 glTexDesc.fTextureID,
598 &glRTDesc)) {
599 return NULL;
600 }
601 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
602 } else {
603 texture = new GrGLTexture(this, glTexDesc);
604 }
605 if (NULL == texture) {
606 return NULL;
607 }
608
609 this->setSpareTextureUnit();
610 return texture;
611}
612
613GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
614 GrGLRenderTarget::Desc glDesc;
615 glDesc.fConfig = desc.fConfig;
616 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
617 glDesc.fMSColorRenderbufferID = 0;
618 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
619 glDesc.fSampleCnt = desc.fSampleCnt;
620 glDesc.fOwnIDs = false;
621 GrGLIRect viewport;
622 viewport.fLeft = 0;
623 viewport.fBottom = 0;
624 viewport.fWidth = desc.fWidth;
625 viewport.fHeight = desc.fHeight;
626
627 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
628 if (desc.fStencilBits) {
629 GrGLStencilBuffer::Format format;
630 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
631 format.fPacked = false;
632 format.fStencilBits = desc.fStencilBits;
633 format.fTotalBits = desc.fStencilBits;
634 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
635 0,
636 desc.fWidth,
637 desc.fHeight,
638 desc.fSampleCnt,
639 format);
640 tgt->setStencilBuffer(sb);
641 sb->unref();
642 }
643 return tgt;
644}
645
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000646////////////////////////////////////////////////////////////////////////////////
647
bsalomon@google.com6f379512011-11-16 20:36:03 +0000648void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
649 int left, int top, int width, int height,
650 GrPixelConfig config, const void* buffer,
651 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000652 if (NULL == buffer) {
653 return;
654 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000655 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
656
bsalomon@google.com6f379512011-11-16 20:36:03 +0000657 this->setSpareTextureUnit();
658 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
659 GrGLTexture::Desc desc;
660 desc.fConfig = glTex->config();
661 desc.fWidth = glTex->width();
662 desc.fHeight = glTex->height();
663 desc.fOrientation = glTex->orientation();
664 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000665
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000666 this->uploadTexData(desc, false,
667 left, top, width, height,
668 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000669}
670
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000671namespace {
672bool adjust_pixel_ops_params(int surfaceWidth,
673 int surfaceHeight,
674 size_t bpp,
675 int* left, int* top, int* width, int* height,
676 const void** data,
677 size_t* rowBytes) {
678 if (!*rowBytes) {
679 *rowBytes = *width * bpp;
680 }
681
682 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
683 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
684
685 if (!subRect.intersect(bounds)) {
686 return false;
687 }
688 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
689 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
690
691 *left = subRect.fLeft;
692 *top = subRect.fTop;
693 *width = subRect.width();
694 *height = subRect.height();
695 return true;
696}
697}
698
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000699bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
700 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000701 int left, int top, int width, int height,
702 GrPixelConfig dataConfig,
703 const void* data,
704 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000705 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000706
707 size_t bpp = GrBytesPerPixel(dataConfig);
708 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
709 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000710 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000711 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000712 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000713
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000714 // in case we need a temporary, trimmed copy of the src pixels
715 SkAutoSMalloc<128 * 128> tempStorage;
716
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000717 bool useTexStorage = isNewTexture &&
718 this->glCaps().fTexStorageSupport;
719 if (useTexStorage) {
720 if (kDesktop_GrGLBinding == this->glBinding()) {
721 // 565 is not a sized internal format on desktop GL. So on desktop
722 // with 565 we always use an unsized internal format to let the
723 // system pick the best sized format to convert the 565 data to.
724 // Since glTexStorage only allows sized internal formats we will
725 // instead fallback to glTexImage2D.
726 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
727 } else {
728 // ES doesn't allow paletted textures to be used with tex storage
729 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
730 }
731 }
732
733 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000734 GrGLenum externalFormat;
735 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000736 // glTexStorage requires sized internal formats on both desktop and ES. ES
737 // glTexImage requires an unsized format.
738 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
739 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000740 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000741 }
742
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000743 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000744 // paletted textures cannot be updated
745 return false;
746 }
747
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000748 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000749 * check whether to allocate a temporary buffer for flipping y or
750 * because our srcData has extra bytes past each row. If so, we need
751 * to trim those off here, since GL ES may not let us specify
752 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000753 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000754 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000755 bool swFlipY = false;
756 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000757 if (NULL != data) {
758 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
759 if (this->glCaps().fUnpackFlipYSupport) {
760 glFlipY = true;
761 } else {
762 swFlipY = true;
763 }
764 }
765 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
766 // can't use this for flipping, only non-neg values allowed. :(
767 if (rowBytes != trimRowBytes) {
768 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
769 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
770 restoreGLRowLength = true;
771 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000772 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000773 if (trimRowBytes != rowBytes || swFlipY) {
774 // copy data into our new storage, skipping the trailing bytes
775 size_t trimSize = height * trimRowBytes;
776 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000777 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000778 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000779 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000780 char* dst = (char*)tempStorage.reset(trimSize);
781 for (int y = 0; y < height; y++) {
782 memcpy(dst, src, trimRowBytes);
783 if (swFlipY) {
784 src -= rowBytes;
785 } else {
786 src += rowBytes;
787 }
788 dst += trimRowBytes;
789 }
790 // now point data to our copied version
791 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000792 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000793 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000794 if (glFlipY) {
795 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
796 }
797 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000798 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000799 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000800 if (isNewTexture &&
801 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000802 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000803 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000804 if (useTexStorage) {
805 // We never resize or change formats of textures. We don't use
806 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000807 GL_ALLOC_CALL(this->glInterface(),
808 TexStorage2D(GR_GL_TEXTURE_2D,
809 1, // levels
810 internalFormat,
811 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000812 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000813 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
814 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
815 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000816 GL_ALLOC_CALL(this->glInterface(),
817 CompressedTexImage2D(GR_GL_TEXTURE_2D,
818 0, // level
819 internalFormat,
820 desc.fWidth, desc.fHeight,
821 0, // border
822 imageSize,
823 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000824 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000825 GL_ALLOC_CALL(this->glInterface(),
826 TexImage2D(GR_GL_TEXTURE_2D,
827 0, // level
828 internalFormat,
829 desc.fWidth, desc.fHeight,
830 0, // border
831 externalFormat, externalType,
832 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000833 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000834 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000835 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000836 if (error != GR_GL_NO_ERROR) {
837 succeeded = false;
838 } else {
839 // if we have data and we used TexStorage to create the texture, we
840 // now upload with TexSubImage.
841 if (NULL != data && useTexStorage) {
842 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
843 0, // level
844 left, top,
845 width, height,
846 externalFormat, externalType,
847 data));
848 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000849 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000850 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000851 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000852 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000853 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000854 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
855 0, // level
856 left, top,
857 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000858 externalFormat, externalType, data));
859 }
860
861 if (restoreGLRowLength) {
862 GrAssert(this->glCaps().fUnpackRowLengthSupport);
863 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000864 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000865 if (glFlipY) {
866 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
867 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000868 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000869}
870
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000871bool GrGpuGL::createRenderTargetObjects(int width, int height,
872 GrGLuint texID,
873 GrGLRenderTarget::Desc* desc) {
874 desc->fMSColorRenderbufferID = 0;
875 desc->fRTFBOID = 0;
876 desc->fTexFBOID = 0;
877 desc->fOwnIDs = true;
878
879 GrGLenum status;
880 GrGLint err;
881
bsalomon@google.comab15d612011-08-09 12:57:56 +0000882 GrGLenum msColorFormat = 0; // suppress warning
883
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000884 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000885 if (!desc->fTexFBOID) {
886 goto FAILED;
887 }
888
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000889
890 // If we are using multisampling we will create two FBOS. We render
891 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000892 if (desc->fSampleCnt > 0) {
893 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
894 goto FAILED;
895 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000896 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
897 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000898 if (!desc->fRTFBOID ||
899 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000900 !this->configToGLFormats(desc->fConfig,
901 // GLES requires sized internal formats
902 kES2_GrGLBinding == this->glBinding(),
903 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904 goto FAILED;
905 }
906 } else {
907 desc->fRTFBOID = desc->fTexFBOID;
908 }
909
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000910 // below here we may bind the FBO
911 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 if (desc->fRTFBOID != desc->fTexFBOID) {
913 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000914 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915 desc->fMSColorRenderbufferID));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000916 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
917 GL_ALLOC_CALL(this->glInterface(),
918 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
919 desc->fSampleCnt,
920 msColorFormat,
921 width, height));
922 err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000923 if (err != GR_GL_NO_ERROR) {
924 goto FAILED;
925 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000926 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
927 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 GR_GL_COLOR_ATTACHMENT0,
929 GR_GL_RENDERBUFFER,
930 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000931 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
933 goto FAILED;
934 }
935 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000937
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000938 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
939 GR_GL_COLOR_ATTACHMENT0,
940 GR_GL_TEXTURE_2D,
941 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000942 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
944 goto FAILED;
945 }
946
947 return true;
948
949FAILED:
950 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000951 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000952 }
953 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000954 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000955 }
956 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000957 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000958 }
959 return false;
960}
961
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000962// good to set a break-point here to know when createTexture fails
963static GrTexture* return_null_texture() {
964// GrAssert(!"null texture");
965 return NULL;
966}
967
968#if GR_DEBUG
969static size_t as_size_t(int x) {
970 return x;
971}
972#endif
973
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000974GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000975 const void* srcData,
976 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000977
978#if GR_COLLECT_STATS
979 ++fStats.fTextureCreateCnt;
980#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000981
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000982 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000983 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000984
bsalomon@google.com99621082011-11-15 16:47:16 +0000985 glTexDesc.fWidth = desc.fWidth;
986 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000987 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000988 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000989
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000990 glRTDesc.fMSColorRenderbufferID = 0;
991 glRTDesc.fRTFBOID = 0;
992 glRTDesc.fTexFBOID = 0;
993 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000994 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000995
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000996 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000997
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000998 const Caps& caps = this->getCaps();
999
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001000 // We keep GrRenderTargets in GL's normal orientation so that they
1001 // can be drawn to by the outside world without the client having
1002 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001003 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001004 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001005
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001006 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1007 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1008 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1009 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 GrPrintf("AA RT requested but not supported on this platform.");
1011 }
1012
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001014 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1015 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001016 return return_null_texture();
1017 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001018 }
1019
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001020 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001021 if (renderTarget && this->glCaps().fTextureUsageSupport) {
1022 // provides a hint about how this texture will be used
1023 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1024 GR_GL_TEXTURE_USAGE,
1025 GR_GL_FRAMEBUFFER_ATTACHMENT));
1026 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001027 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001028 return return_null_texture();
1029 }
1030
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001031 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001032 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001033
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001034 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1035 // drivers have a bug where an FBO won't be complete if it includes a
1036 // texture that is not mipmap complete (considering the filter in use).
1037 GrGLTexture::TexParams initialTexParams;
1038 // we only set a subset here so invalidate first
1039 initialTexParams.invalidate();
1040 initialTexParams.fFilter = GR_GL_NEAREST;
1041 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1042 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001043 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1044 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001045 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1047 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001048 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001049 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1050 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001051 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001052 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1053 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001054 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001055 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1056 glTexDesc.fWidth, glTexDesc.fHeight,
1057 desc.fConfig, srcData, rowBytes)) {
1058 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1059 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001060 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001061
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 if (renderTarget) {
1064#if GR_COLLECT_STATS
1065 ++fStats.fRenderTargetCreateCnt;
1066#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001067 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1068 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001069 glTexDesc.fTextureID,
1070 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001071 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 return return_null_texture();
1073 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001074 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001076 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001078 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001079#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001080 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1081 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001082#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001083 return tex;
1084}
1085
1086namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001087void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1088 GrGLuint rb,
1089 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001090 // we shouldn't ever know one size and not the other
1091 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1092 (kUnknownBitCount == format->fTotalBits));
1093 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001094 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001095 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1096 (GrGLint*)&format->fStencilBits);
1097 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001098 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001099 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1100 (GrGLint*)&format->fTotalBits);
1101 format->fTotalBits += format->fStencilBits;
1102 } else {
1103 format->fTotalBits = format->fStencilBits;
1104 }
1105 }
1106}
1107}
1108
1109bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1110 int width, int height) {
1111
1112 // All internally created RTs are also textures. We don't create
1113 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1114 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001115 GrAssert(width >= rt->width());
1116 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117
1118 int samples = rt->numSamples();
1119 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 if (!sbID) {
1122 return false;
1123 }
1124
1125 GrGLStencilBuffer* sb = NULL;
1126
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001127 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001129 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 // we start with the last stencil format that succeeded in hopes
1131 // that we won't go through this loop more than once after the
1132 // first (painful) stencil creation.
1133 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001134 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001135 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001136 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 // version on a GL that doesn't have an MSAA extension.
1138 if (samples > 1) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001139 GL_ALLOC_CALL(this->glInterface(),
1140 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1141 samples,
1142 sFmt.fInternalFormat,
1143 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001145 GL_ALLOC_CALL(this->glInterface(),
1146 RenderbufferStorage(GR_GL_RENDERBUFFER,
1147 sFmt.fInternalFormat,
1148 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 }
1150
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001151 GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 if (err == GR_GL_NO_ERROR) {
1153 // After sized formats we attempt an unsized format and take whatever
1154 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001155 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001156 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001157 sb = new GrGLStencilBuffer(this, sbID, width, height,
1158 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1160 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001161 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 return true;
1164 }
1165 sb->abandon(); // otherwise we lose sbID
1166 sb->unref();
1167 }
1168 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001170 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171}
1172
1173bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1174 GrRenderTarget* rt) {
1175 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1176
1177 GrGLuint fbo = glrt->renderFBOID();
1178
1179 if (NULL == sb) {
1180 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001181 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 GR_GL_STENCIL_ATTACHMENT,
1183 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001184 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 GR_GL_DEPTH_ATTACHMENT,
1186 GR_GL_RENDERBUFFER, 0));
1187#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001188 GrGLenum status;
1189 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1191#endif
1192 }
1193 return true;
1194 } else {
1195 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1196 GrGLuint rb = glsb->renderbufferID();
1197
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001198 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1200 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 GR_GL_STENCIL_ATTACHMENT,
1202 GR_GL_RENDERBUFFER, rb));
1203 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 GR_GL_DEPTH_ATTACHMENT,
1206 GR_GL_RENDERBUFFER, rb));
1207 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001208 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209 GR_GL_DEPTH_ATTACHMENT,
1210 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001211 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001213 GrGLenum status;
1214 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 GR_GL_STENCIL_ATTACHMENT,
1218 GR_GL_RENDERBUFFER, 0));
1219 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 GR_GL_DEPTH_ATTACHMENT,
1222 GR_GL_RENDERBUFFER, 0));
1223 }
1224 return false;
1225 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 return true;
1227 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001229}
1230
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001231////////////////////////////////////////////////////////////////////////////////
1232
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001233GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001234 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001235 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001236 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001238 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001239 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001241 GL_ALLOC_CALL(this->glInterface(),
1242 BufferData(GR_GL_ARRAY_BUFFER,
1243 size,
1244 NULL, // data ptr
1245 dynamic ? GR_GL_DYNAMIC_DRAW :
1246 GR_GL_STATIC_DRAW));
1247 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 // deleting bound buffer does implicit bind to 0
1250 fHWGeometryState.fVertexBuffer = NULL;
1251 return NULL;
1252 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001253 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 size, dynamic);
1255 fHWGeometryState.fVertexBuffer = vertexBuffer;
1256 return vertexBuffer;
1257 }
1258 return NULL;
1259}
1260
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001261GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001262 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001265 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001266 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001268 GL_ALLOC_CALL(this->glInterface(),
1269 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1270 size,
1271 NULL, // data ptr
1272 dynamic ? GR_GL_DYNAMIC_DRAW :
1273 GR_GL_STATIC_DRAW));
1274 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001275 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001276 // deleting bound buffer does implicit bind to 0
1277 fHWGeometryState.fIndexBuffer = NULL;
1278 return NULL;
1279 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001280 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 size, dynamic);
1282 fHWGeometryState.fIndexBuffer = indexBuffer;
1283 return indexBuffer;
1284 }
1285 return NULL;
1286}
1287
reed@google.comac10a2d2010-12-22 21:39:39 +00001288void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001289 const GrDrawState& drawState = this->getDrawState();
1290 const GrGLRenderTarget* rt =
1291 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1292
1293 GrAssert(NULL != rt);
1294 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001295
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001296 GrGLIRect scissor;
1297 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001298 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001299 rect->width(), rect->height());
1300 if (scissor.contains(vp)) {
1301 rect = NULL;
1302 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 }
1304
1305 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001306 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001307 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001308 fHWBounds.fScissorRect = scissor;
1309 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001311 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 fHWBounds.fScissorEnabled = true;
1313 }
1314 } else {
1315 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001316 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 fHWBounds.fScissorEnabled = false;
1318 }
1319 }
1320}
1321
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001322void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001323 const GrDrawState& drawState = this->getDrawState();
1324 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001325 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001326 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001327
bsalomon@google.com74b98712011-11-11 19:46:16 +00001328 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001329 if (NULL != rect) {
1330 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001331 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001332 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001333 if (clippedRect.intersect(rtRect)) {
1334 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001335 } else {
1336 return;
1337 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001339 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001340 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001341
1342 GrGLfloat r, g, b, a;
1343 static const GrGLfloat scale255 = 1.f / 255.f;
1344 a = GrColorUnpackA(color) * scale255;
1345 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001346 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001347 scaleRGB *= a;
1348 }
1349 r = GrColorUnpackR(color) * scaleRGB;
1350 g = GrColorUnpackG(color) * scaleRGB;
1351 b = GrColorUnpackB(color) * scaleRGB;
1352
1353 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001354 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001355 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001356 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001357}
1358
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001359void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001360 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001361 return;
1362 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001363
1364 this->flushRenderTarget(&GrIRect::EmptyIRect());
1365
reed@google.comac10a2d2010-12-22 21:39:39 +00001366 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001367 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001368 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001369 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001370 GL_CALL(StencilMask(0xffffffff));
1371 GL_CALL(ClearStencil(0));
1372 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001373 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001374}
1375
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001376void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001377 const GrDrawState& drawState = this->getDrawState();
1378 const GrRenderTarget* rt = drawState.getRenderTarget();
1379 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001380
1381 // this should only be called internally when we know we have a
1382 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001383 GrAssert(NULL != rt->getStencilBuffer());
1384 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001385#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001386 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001387 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001388#else
1389 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001390 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001391 // turned into draws. Our contract on GrDrawTarget says that
1392 // changing the clip between stencil passes may or may not
1393 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001394 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001395#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001396 GrGLint value;
1397 if (insideClip) {
1398 value = (1 << (stencilBitCount - 1));
1399 } else {
1400 value = 0;
1401 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001402 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001403 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001404 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001405 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001406 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001407 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001408}
1409
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001410void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001411 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001412}
1413
bsalomon@google.comc4364992011-11-07 15:54:49 +00001414bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1415 int left, int top,
1416 int width, int height,
1417 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001418 size_t rowBytes) const {
1419 // if GL can do the flip then we'll never pay for it.
1420 if (this->glCaps().fPackFlipYSupport) {
1421 return false;
1422 }
1423
1424 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001425 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001426 if (this->glCaps().fPackRowLengthSupport) {
1427 return true;
1428 }
1429 // If we have to do memcpys to handle rowBytes then y-flip is free
1430 // Note the rowBytes might be tight to the passed in data, but if data
1431 // gets clipped in x to the target the rowBytes will no longer be tight.
1432 if (left >= 0 && (left + width) < renderTarget->width()) {
1433 return 0 == rowBytes ||
1434 GrBytesPerPixel(config) * width == rowBytes;
1435 } else {
1436 return false;
1437 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001438}
1439
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001440bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001441 int left, int top,
1442 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001443 GrPixelConfig config,
1444 void* buffer,
1445 size_t rowBytes,
1446 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001447 GrGLenum format;
1448 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001449 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001450 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001451 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001452 size_t bpp = GrBytesPerPixel(config);
1453 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1454 &left, &top, &width, &height,
1455 const_cast<const void**>(&buffer),
1456 &rowBytes)) {
1457 return false;
1458 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001459
bsalomon@google.comc6980972011-11-02 19:57:21 +00001460 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001461 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001462 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001463 switch (tgt->getResolveType()) {
1464 case GrGLRenderTarget::kCantResolve_ResolveType:
1465 return false;
1466 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001467 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001468 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001469 break;
1470 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001471 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001472 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001473 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1474 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001475 break;
1476 default:
1477 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001478 }
1479
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001480 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001481
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001482 // the read rect is viewport-relative
1483 GrGLIRect readRect;
1484 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001485
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001486 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001487 if (0 == rowBytes) {
1488 rowBytes = tightRowBytes;
1489 }
1490 size_t readDstRowBytes = tightRowBytes;
1491 void* readDst = buffer;
1492
1493 // determine if GL can read using the passed rowBytes or if we need
1494 // a scratch buffer.
1495 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1496 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001497 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498 GrAssert(!(rowBytes % sizeof(GrColor)));
1499 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1500 readDstRowBytes = rowBytes;
1501 } else {
1502 scratch.reset(tightRowBytes * height);
1503 readDst = scratch.get();
1504 }
1505 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001506 if (!invertY && this->glCaps().fPackFlipYSupport) {
1507 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1508 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001509 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1510 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511 format, type, readDst));
1512 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001513 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001514 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1515 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001516 if (!invertY && this->glCaps().fPackFlipYSupport) {
1517 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1518 invertY = true;
1519 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001520
1521 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001522 // API presents top-to-bottom. We must preserve the padding contents. Note
1523 // that the above readPixels did not overwrite the padding.
1524 if (readDst == buffer) {
1525 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001526 if (!invertY) {
1527 scratch.reset(tightRowBytes);
1528 void* tmpRow = scratch.get();
1529 // flip y in-place by rows
1530 const int halfY = height >> 1;
1531 char* top = reinterpret_cast<char*>(buffer);
1532 char* bottom = top + (height - 1) * rowBytes;
1533 for (int y = 0; y < halfY; y++) {
1534 memcpy(tmpRow, top, tightRowBytes);
1535 memcpy(top, bottom, tightRowBytes);
1536 memcpy(bottom, tmpRow, tightRowBytes);
1537 top += rowBytes;
1538 bottom -= rowBytes;
1539 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001540 }
1541 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001542 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001543 // copy from readDst to buffer while flipping y
1544 const int halfY = height >> 1;
1545 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001546 char* dst = reinterpret_cast<char*>(buffer);
1547 if (!invertY) {
1548 dst += (height-1) * rowBytes;
1549 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001550 for (int y = 0; y < height; y++) {
1551 memcpy(dst, src, tightRowBytes);
1552 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001553 if (invertY) {
1554 dst += rowBytes;
1555 } else {
1556 dst -= rowBytes;
1557 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001558 }
1559 }
1560 return true;
1561}
1562
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001563void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001564
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001565 GrGLRenderTarget* rt =
1566 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1567 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001568
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001569 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001570 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001571 #if GR_COLLECT_STATS
1572 ++fStats.fRenderTargetChngCnt;
1573 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001574 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001575 GrGLenum status;
1576 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001577 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001578 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001579 }
1580 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001581 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001582 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001583 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001584 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001585 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001586 fHWBounds.fViewportRect = vp;
1587 }
1588 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001589 if (NULL == bound || !bound->isEmpty()) {
1590 rt->flagAsNeedingResolve(bound);
1591 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001592}
1593
twiz@google.com0f31ca72011-03-18 17:38:11 +00001594GrGLenum gPrimitiveType2GLMode[] = {
1595 GR_GL_TRIANGLES,
1596 GR_GL_TRIANGLE_STRIP,
1597 GR_GL_TRIANGLE_FAN,
1598 GR_GL_POINTS,
1599 GR_GL_LINES,
1600 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001601};
1602
bsalomon@google.comd302f142011-03-03 13:54:13 +00001603#define SWAP_PER_DRAW 0
1604
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001605#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001606 #if GR_MAC_BUILD
1607 #include <AGL/agl.h>
1608 #elif GR_WIN32_BUILD
1609 void SwapBuf() {
1610 DWORD procID = GetCurrentProcessId();
1611 HWND hwnd = GetTopWindow(GetDesktopWindow());
1612 while(hwnd) {
1613 DWORD wndProcID = 0;
1614 GetWindowThreadProcessId(hwnd, &wndProcID);
1615 if(wndProcID == procID) {
1616 SwapBuffers(GetDC(hwnd));
1617 }
1618 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1619 }
1620 }
1621 #endif
1622#endif
1623
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001624void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1625 uint32_t startVertex,
1626 uint32_t startIndex,
1627 uint32_t vertexCount,
1628 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001629 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1630
twiz@google.com0f31ca72011-03-18 17:38:11 +00001631 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001632
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001633 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1634 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1635
1636 // our setupGeometry better have adjusted this to zero since
1637 // DrawElements always draws from the begining of the arrays for idx 0.
1638 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001639
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001640 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1641 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001642#if SWAP_PER_DRAW
1643 glFlush();
1644 #if GR_MAC_BUILD
1645 aglSwapBuffers(aglGetCurrentContext());
1646 int set_a_break_pt_here = 9;
1647 aglSwapBuffers(aglGetCurrentContext());
1648 #elif GR_WIN32_BUILD
1649 SwapBuf();
1650 int set_a_break_pt_here = 9;
1651 SwapBuf();
1652 #endif
1653#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001654}
1655
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001656void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1657 uint32_t startVertex,
1658 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1660
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001661 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1662
1663 // our setupGeometry better have adjusted this to zero.
1664 // DrawElements doesn't take an offset so we always adjus the startVertex.
1665 GrAssert(0 == startVertex);
1666
1667 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1668 // account for startVertex in the DrawElements case. So we always
1669 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001670 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001671#if SWAP_PER_DRAW
1672 glFlush();
1673 #if GR_MAC_BUILD
1674 aglSwapBuffers(aglGetCurrentContext());
1675 int set_a_break_pt_here = 9;
1676 aglSwapBuffers(aglGetCurrentContext());
1677 #elif GR_WIN32_BUILD
1678 SwapBuf();
1679 int set_a_break_pt_here = 9;
1680 SwapBuf();
1681 #endif
1682#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001683}
1684
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001685void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001686
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001687 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001688 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001689 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001690 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1691 rt->renderFBOID()));
1692 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1693 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001694 #if GR_COLLECT_STATS
1695 ++fStats.fRenderTargetChngCnt;
1696 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001697 // make sure we go through flushRenderTarget() since we've modified
1698 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001699 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001700 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001701 const GrIRect dirtyRect = rt->getResolveRect();
1702 GrGLIRect r;
1703 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1704 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001705
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001706 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001707 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001708 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1709 GL_CALL(Scissor(r.fLeft, r.fBottom,
1710 r.fWidth, r.fHeight));
1711 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001712 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001713 fHWBounds.fScissorEnabled = true;
1714 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001715 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001716 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001717 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1718 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001719 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001720 int right = r.fLeft + r.fWidth;
1721 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001722 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1723 r.fLeft, r.fBottom, right, top,
1724 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001725 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001726 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001727 }
1728}
1729
twiz@google.com0f31ca72011-03-18 17:38:11 +00001730static const GrGLenum grToGLStencilFunc[] = {
1731 GR_GL_ALWAYS, // kAlways_StencilFunc
1732 GR_GL_NEVER, // kNever_StencilFunc
1733 GR_GL_GREATER, // kGreater_StencilFunc
1734 GR_GL_GEQUAL, // kGEqual_StencilFunc
1735 GR_GL_LESS, // kLess_StencilFunc
1736 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1737 GR_GL_EQUAL, // kEqual_StencilFunc,
1738 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001739};
1740GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1741GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1742GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1743GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1744GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1745GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1746GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1747GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1748GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1749
twiz@google.com0f31ca72011-03-18 17:38:11 +00001750static const GrGLenum grToGLStencilOp[] = {
1751 GR_GL_KEEP, // kKeep_StencilOp
1752 GR_GL_REPLACE, // kReplace_StencilOp
1753 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1754 GR_GL_INCR, // kIncClamp_StencilOp
1755 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1756 GR_GL_DECR, // kDecClamp_StencilOp
1757 GR_GL_ZERO, // kZero_StencilOp
1758 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001759};
1760GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1761GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1762GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1763GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1764GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1765GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1766GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1767GR_STATIC_ASSERT(6 == kZero_StencilOp);
1768GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1769
reed@google.comac10a2d2010-12-22 21:39:39 +00001770void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001771 const GrDrawState& drawState = this->getDrawState();
1772
1773 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001774
1775 // use stencil for clipping if clipping is enabled and the clip
1776 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001777 bool stencilClip = fClipInStencil && drawState.isClipState();
1778 bool drawClipToStencil =
1779 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001780 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1781 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001782 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1783 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001784
1785 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001786
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001787 // we can't simultaneously perform stencil-clipping and
1788 // modify the stencil clip
1789 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001790
bsalomon@google.comd302f142011-03-03 13:54:13 +00001791 if (settings->isDisabled()) {
1792 if (stencilClip) {
1793 settings = &gClipStencilSettings;
1794 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001795 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001796
1797 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001798 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001799 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001800 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001801 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001802 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001803 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1804 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1805 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1806 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1807 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1808 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1809 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1810 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001811 }
1812 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001813 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001814 GrStencilBuffer* stencilBuffer =
1815 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001816 if (NULL != stencilBuffer) {
1817 stencilBits = stencilBuffer->bits();
1818 }
1819 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001820 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001821
1822 GrGLuint clipStencilMask = 0;
1823 GrGLuint userStencilMask = ~0;
1824 if (stencilBits > 0) {
1825 clipStencilMask = 1 << (stencilBits - 1);
1826 userStencilMask = clipStencilMask - 1;
1827 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001829 unsigned int frontRef = settings->frontFuncRef();
1830 unsigned int frontMask = settings->frontFuncMask();
1831 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001832 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001833
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001834 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001835 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1836 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001837 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001838 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001839 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001840
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001841 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001842 stencilClip,
1843 clipStencilMask,
1844 userStencilMask,
1845 &frontRef,
1846 &frontMask);
1847 frontWriteMask &= userStencilMask;
1848 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001849 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001850 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001851 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001852 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001853 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001854 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001855 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001856 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001857 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001858 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001860 unsigned int backRef = settings->backFuncRef();
1861 unsigned int backMask = settings->backFuncMask();
1862 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863
1864
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001865 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001866 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1867 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001868 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001869 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001870 stencilClip, settings->backFunc())];
1871 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001872 stencilClip,
1873 clipStencilMask,
1874 userStencilMask,
1875 &backRef,
1876 &backMask);
1877 backWriteMask &= userStencilMask;
1878 }
1879
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001880 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1881 frontRef, frontMask));
1882 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1883 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1884 backRef, backMask));
1885 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1886 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001887 grToGLStencilOp[settings->frontFailOp()],
1888 grToGLStencilOp[settings->frontPassOp()],
1889 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001890
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001891 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001892 grToGLStencilOp[settings->backFailOp()],
1893 grToGLStencilOp[settings->backPassOp()],
1894 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001895 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001896 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1897 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001898 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1899 grToGLStencilOp[settings->frontPassOp()],
1900 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001901 }
1902 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001903 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001904 fHWStencilClip = stencilClip;
1905 }
1906}
1907
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001908void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001909 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001910 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001911 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1912 // smooth lines.
1913
1914 // we prefer smooth lines over multisampled lines
1915 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001916 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001917 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001918 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001919 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001920 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001921 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001922 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001923 fHWAAState.fSmoothLineEnabled = false;
1924 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001925 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001926 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001927 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001928 fHWAAState.fMSAAEnabled = false;
1929 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001930 } else if (rt->isMultisampled() &&
1931 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001932 fHWAAState.fMSAAEnabled) {
1933 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001934 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001935 fHWAAState.fMSAAEnabled = false;
1936 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001938 fHWAAState.fMSAAEnabled = true;
1939 }
1940 }
1941 }
1942}
1943
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001944void GrGpuGL::flushBlend(GrPrimitiveType type,
1945 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001946 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001947 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001948 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001949 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001950 fHWBlendDisabled = false;
1951 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001952 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1953 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001954 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1955 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001956 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001957 }
1958 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001959 // any optimization to disable blending should
1960 // have already been applied and tweaked the coeffs
1961 // to (1, 0).
1962 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1963 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001964 if (fHWBlendDisabled != blendOff) {
1965 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001966 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001967 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001968 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001969 }
1970 fHWBlendDisabled = blendOff;
1971 }
1972 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001973 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
1974 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001975 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1976 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001977 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001978 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001979 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001980 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1981 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001982 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001983
1984 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001985 GrColorUnpackR(blendConst) / 255.f,
1986 GrColorUnpackG(blendConst) / 255.f,
1987 GrColorUnpackB(blendConst) / 255.f,
1988 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001989 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001990 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001991 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001992 }
1993 }
1994 }
1995}
1996
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001997namespace {
1998
1999unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002000 switch (filter) {
2001 case GrSamplerState::kBilinear_Filter:
2002 case GrSamplerState::k4x4Downsample_Filter:
2003 return GR_GL_LINEAR;
2004 case GrSamplerState::kNearest_Filter:
2005 case GrSamplerState::kConvolution_Filter:
2006 return GR_GL_NEAREST;
2007 default:
2008 GrAssert(!"Unknown filter type");
2009 return GR_GL_LINEAR;
2010 }
2011}
2012
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002013const GrGLenum* get_swizzle(GrPixelConfig config,
2014 const GrSamplerState& sampler) {
2015 if (GrPixelConfigIsAlphaOnly(config)) {
2016 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2017 GR_GL_ALPHA, GR_GL_ALPHA };
2018 return gAlphaSmear;
2019 } else if (sampler.swapsRAndB()) {
2020 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2021 GR_GL_RED, GR_GL_ALPHA };
2022 return gRedBlueSwap;
2023 } else {
2024 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2025 GR_GL_BLUE, GR_GL_ALPHA };
2026 return gStraight;
2027 }
2028}
2029
2030void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2031 // should add texparameteri to interface to make 1 instead of 4 calls here
2032 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2033 GR_GL_TEXTURE_SWIZZLE_R,
2034 swizzle[0]));
2035 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2036 GR_GL_TEXTURE_SWIZZLE_G,
2037 swizzle[1]));
2038 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2039 GR_GL_TEXTURE_SWIZZLE_B,
2040 swizzle[2]));
2041 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2042 GR_GL_TEXTURE_SWIZZLE_A,
2043 swizzle[3]));
2044}
2045}
2046
bsalomon@google.comffca4002011-02-22 20:34:01 +00002047bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002048
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002049 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002050 // GrGpu::setupClipAndFlushState should have already checked this
2051 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002052 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002053
tomhudson@google.com93813632011-10-27 20:21:16 +00002054 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002055 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002056 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002057 GrGLTexture* nextTexture =
2058 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002059
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002060 // true for now, but maybe not with GrEffect.
2061 GrAssert(NULL != nextTexture);
2062 // if we created a rt/tex and rendered to it without using a
2063 // texture and now we're texuring from the rt it will still be
2064 // the last bound texture, but it needs resolving. So keep this
2065 // out of the "last != next" check.
2066 GrGLRenderTarget* texRT =
2067 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2068 if (NULL != texRT) {
2069 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002070 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002071
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002072 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002073 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002074 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002075 #if GR_COLLECT_STATS
2076 ++fStats.fTextureChngCnt;
2077 #endif
2078 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002079 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002080 // The texture matrix has to compensate for texture width/height
2081 // and NPOT-embedded-in-POT
2082 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002083 }
2084
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002085 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002086 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002087 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002088 nextTexture->getCachedTexParams(&timestamp);
2089 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002090 GrGLTexture::TexParams newTexParams;
2091
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002092 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002093
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002094 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002095 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2096 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002097 memcpy(newTexParams.fSwizzleRGBA,
2098 get_swizzle(nextTexture->config(), sampler),
2099 sizeof(newTexParams.fSwizzleRGBA));
2100 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002101 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002102 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002103 GR_GL_TEXTURE_MAG_FILTER,
2104 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002105 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002106 GR_GL_TEXTURE_MIN_FILTER,
2107 newTexParams.fFilter));
2108 }
2109 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2110 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002111 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002112 GR_GL_TEXTURE_WRAP_S,
2113 newTexParams.fWrapS));
2114 }
2115 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2116 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002117 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002118 GR_GL_TEXTURE_WRAP_T,
2119 newTexParams.fWrapT));
2120 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002121 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002122 (setAll ||
2123 memcmp(newTexParams.fSwizzleRGBA,
2124 oldTexParams.fSwizzleRGBA,
2125 sizeof(newTexParams.fSwizzleRGBA)))) {
2126 setTextureUnit(s);
2127 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2128 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002129 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002130 nextTexture->setCachedTexParams(newTexParams,
2131 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002132 }
2133 }
2134
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002135 GrIRect* rect = NULL;
2136 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002137 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002138 fClip.hasConservativeBounds()) {
2139 fClip.getConservativeBounds().roundOut(&clipBounds);
2140 rect = &clipBounds;
2141 }
2142 this->flushRenderTarget(rect);
2143 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002144
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002145 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2146 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002147 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002148 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002149 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002150 }
2151 }
2152
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002153 if (drawState->isColorWriteDisabled() !=
2154 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002155 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002156 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002157 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002158 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002159 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002160 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002161 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002162 }
2163
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002164 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2165 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002166 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002167 GL_CALL(Enable(GR_GL_CULL_FACE));
2168 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002169 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002170 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002171 GL_CALL(Enable(GR_GL_CULL_FACE));
2172 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002173 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002174 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002175 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002176 break;
2177 default:
2178 GrCrash("Unknown draw face.");
2179 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002180 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002181 }
2182
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002183#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002184 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002185 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002186 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002187 NULL == drawState->getRenderTarget() ||
2188 NULL == drawState->getTexture(s) ||
2189 drawState->getTexture(s)->asRenderTarget() !=
2190 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002191 }
2192#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002193
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002194 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002195
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002196 // This copy must happen after flushStencil() is called. flushStencil()
2197 // relies on detecting when the kModifyStencilClip_StateBit state has
2198 // changed since the last draw.
2199 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002200 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002201}
2202
2203void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002204 if (fHWGeometryState.fVertexBuffer != buffer) {
2205 fHWGeometryState.fArrayPtrsDirty = true;
2206 fHWGeometryState.fVertexBuffer = buffer;
2207 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002208}
2209
2210void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002211 if (fHWGeometryState.fVertexBuffer == buffer) {
2212 // deleting bound buffer does implied bind to 0
2213 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002214 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 }
2216}
2217
2218void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002219 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002220}
2221
2222void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002223 if (fHWGeometryState.fIndexBuffer == buffer) {
2224 // deleting bound buffer does implied bind to 0
2225 fHWGeometryState.fIndexBuffer = NULL;
2226 }
2227}
2228
reed@google.comac10a2d2010-12-22 21:39:39 +00002229void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2230 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002231 GrDrawState* drawState = this->drawState();
2232 if (drawState->getRenderTarget() == renderTarget) {
2233 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002234 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002235 if (fHWDrawState.getRenderTarget() == renderTarget) {
2236 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002237 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002238}
2239
2240void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002241 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002242 GrDrawState* drawState = this->drawState();
2243 if (drawState->getTexture(s) == texture) {
2244 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002245 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002246 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002247 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002248 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002249 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002250 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002251}
2252
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002253bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2254 bool getSizedInternalFormat,
2255 GrGLenum* internalFormat,
2256 GrGLenum* externalFormat,
2257 GrGLenum* externalType) {
2258 GrGLenum dontCare;
2259 if (NULL == internalFormat) {
2260 internalFormat = &dontCare;
2261 }
2262 if (NULL == externalFormat) {
2263 externalFormat = &dontCare;
2264 }
2265 if (NULL == externalType) {
2266 externalType = &dontCare;
2267 }
2268
reed@google.comac10a2d2010-12-22 21:39:39 +00002269 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002270 case kRGBA_8888_PM_GrPixelConfig:
2271 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002272 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002273 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002274 if (getSizedInternalFormat) {
2275 *internalFormat = GR_GL_RGBA8;
2276 } else {
2277 *internalFormat = GR_GL_RGBA;
2278 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002279 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002280 break;
2281 case kBGRA_8888_PM_GrPixelConfig:
2282 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002283 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002284 return false;
2285 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002286 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002287 if (getSizedInternalFormat) {
2288 *internalFormat = GR_GL_BGRA8;
2289 } else {
2290 *internalFormat = GR_GL_BGRA;
2291 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002292 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002293 if (getSizedInternalFormat) {
2294 *internalFormat = GR_GL_RGBA8;
2295 } else {
2296 *internalFormat = GR_GL_RGBA;
2297 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002298 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002299 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002300 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002301 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002302 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002303 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002304 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002305 if (getSizedInternalFormat) {
2306 if (this->glBinding() == kDesktop_GrGLBinding) {
2307 return false;
2308 } else {
2309 *internalFormat = GR_GL_RGB565;
2310 }
2311 } else {
2312 *internalFormat = GR_GL_RGB;
2313 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002314 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002315 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002316 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002317 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002318 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002319 if (getSizedInternalFormat) {
2320 *internalFormat = GR_GL_RGBA4;
2321 } else {
2322 *internalFormat = GR_GL_RGBA;
2323 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002324 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002325 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002326 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002327 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002328 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002329 // glCompressedTexImage doesn't take external params
2330 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002331 // no sized/unsized internal format distinction here
2332 *internalFormat = GR_GL_PALETTE8_RGBA8;
2333 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002334 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002335 } else {
2336 return false;
2337 }
2338 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002339 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002340 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002341 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002342 if (getSizedInternalFormat) {
2343 *internalFormat = GR_GL_ALPHA8;
2344 } else {
2345 *internalFormat = GR_GL_ALPHA;
2346 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002347 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002348 break;
2349 default:
2350 return false;
2351 }
2352 return true;
2353}
2354
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002355void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002356 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002357 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002358 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002359 fActiveTextureUnitIdx = unit;
2360 }
2361}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002362
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002363void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002364 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002365 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002366 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2367 }
2368}
2369
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002370void GrGpuGL::resetDirtyFlags() {
2371 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2372}
2373
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002374void GrGpuGL::setBuffers(bool indexed,
2375 int* extraVertexOffset,
2376 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002377
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002378 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002379
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002380 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2381
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002382 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002383 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002384 case kBuffer_GeometrySrcType:
2385 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002386 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002387 break;
2388 case kArray_GeometrySrcType:
2389 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002390 this->finalizeReservedVertices();
2391 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2392 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002393 break;
2394 default:
2395 vbuf = NULL; // suppress warning
2396 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002397 }
2398
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002399 GrAssert(NULL != vbuf);
2400 GrAssert(!vbuf->isLocked());
2401 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002402 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002403 fHWGeometryState.fArrayPtrsDirty = true;
2404 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002405 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002406
2407 if (indexed) {
2408 GrAssert(NULL != extraIndexOffset);
2409
2410 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002411 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002412 case kBuffer_GeometrySrcType:
2413 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002414 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002415 break;
2416 case kArray_GeometrySrcType:
2417 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002418 this->finalizeReservedIndices();
2419 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2420 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002421 break;
2422 default:
2423 ibuf = NULL; // suppress warning
2424 GrCrash("Unknown geometry src type!");
2425 }
2426
2427 GrAssert(NULL != ibuf);
2428 GrAssert(!ibuf->isLocked());
2429 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002430 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002431 fHWGeometryState.fIndexBuffer = ibuf;
2432 }
2433 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002434}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002435
2436int GrGpuGL::getMaxEdges() const {
2437 // FIXME: This is a pessimistic estimate based on how many other things
2438 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002439 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2440 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002441}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002442
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002443void GrGpuGL::GLCaps::print() const {
2444 for (int i = 0; i < fStencilFormats.count(); ++i) {
2445 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2446 i,
2447 fStencilFormats[i].fStencilBits,
2448 fStencilFormats[i].fTotalBits);
2449 }
2450
2451 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2452 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2453 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2454 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2455 static const char* gMSFBOExtStr[] = {
2456 "None",
2457 "ARB",
2458 "EXT",
2459 "Apple",
2460 };
2461 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002462 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002463 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2464 }
2465 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2466 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002467 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002468 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002469 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002470 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002471 (fTextureSwizzleSupport ? "YES": "NO"));
2472 GrPrintf("Unpack Row length support: %s\n",
2473 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002474 GrPrintf("Unpack Flip Y support: %s\n",
2475 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002476 GrPrintf("Pack Row length support: %s\n",
2477 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002478 GrPrintf("Pack Flip Y support: %s\n",
2479 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002480}