blob: 85defc55f8a3351e0b05619cc67e09e0e21306c8 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
twiz@google.com0f31ca72011-03-18 17:38:11 +000027static const GrGLenum gXfermodeCoeff2Blend[] = {
28 GR_GL_ZERO,
29 GR_GL_ONE,
30 GR_GL_SRC_COLOR,
31 GR_GL_ONE_MINUS_SRC_COLOR,
32 GR_GL_DST_COLOR,
33 GR_GL_ONE_MINUS_DST_COLOR,
34 GR_GL_SRC_ALPHA,
35 GR_GL_ONE_MINUS_SRC_ALPHA,
36 GR_GL_DST_ALPHA,
37 GR_GL_ONE_MINUS_DST_ALPHA,
38 GR_GL_CONSTANT_COLOR,
39 GR_GL_ONE_MINUS_CONSTANT_COLOR,
40 GR_GL_CONSTANT_ALPHA,
41 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
43 // extended blend coeffs
44 GR_GL_SRC1_COLOR,
45 GR_GL_ONE_MINUS_SRC1_COLOR,
46 GR_GL_SRC1_ALPHA,
47 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000048};
49
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 static const bool gCoeffReferencesBlendConst[] = {
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 false,
62 true,
63 true,
64 true,
65 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000066
67 // extended blend coeffs
68 false,
69 false,
70 false,
71 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000072 };
73 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
75
76 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
77 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
78 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
79 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
80 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
81 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
82 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
83 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
84 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
85 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
86 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
87 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
88 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
89 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
90
91 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
92 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
93 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
94 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
95
96 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
97 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000098}
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
103 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104 GrMatrix* matrix) {
105 GrAssert(NULL != texture);
106 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000107 GrGLTexture::Orientation orientation = texture->orientation();
108 if (GrGLTexture::kBottomUp_Orientation == orientation) {
109 GrMatrix invY;
110 invY.setAll(GR_Scalar1, 0, 0,
111 0, -GR_Scalar1, GR_Scalar1,
112 0, 0, GrMatrix::I()[8]);
113 matrix->postConcat(invY);
114 } else {
115 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
116 }
117}
118
bsalomon@google.comd302f142011-03-03 13:54:13 +0000119bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120 const GrSamplerState& sampler) {
121 GrAssert(NULL != texture);
122 if (!sampler.getMatrix().isIdentity()) {
123 return false;
124 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000125 GrGLTexture::Orientation orientation = texture->orientation();
126 if (GrGLTexture::kBottomUp_Orientation == orientation) {
127 return false;
128 } else {
129 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
130 }
131 return true;
132}
133
134///////////////////////////////////////////////////////////////////////////////
135
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000136static bool gPrintStartupSpew;
137
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000139
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000141
twiz@google.com0f31ca72011-03-18 17:38:11 +0000142 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
144 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000145 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000146 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
147 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000148 // some implementations require texture to be mip-map complete before
149 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000150 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000151 GR_GL_TEXTURE_MIN_FILTER,
152 GR_GL_NEAREST));
153 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
154 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
155 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
156 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
157 GR_GL_COLOR_ATTACHMENT0,
158 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000159 GrGLenum status;
160 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
162 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000163
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000165}
166
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000167GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000168
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000169 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000170
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 gl->ref();
172 fGL = gl;
173 fGLBinding = glBinding;
174 switch (glBinding) {
175 case kDesktop_GrGLBinding:
176 GrAssert(gl->supportsDesktop());
177 break;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000178 case kES2_GrGLBinding:
179 GrAssert(gl->supportsES2());
180 break;
181 default:
182 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000183 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185 GrGLClearErr(fGL);
186
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000187 const GrGLubyte* ext;
188 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000189 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000190 const GrGLubyte* vendor;
191 const GrGLubyte* renderer;
192 const GrGLubyte* version;
193 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
194 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
195 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000196 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
197 this);
198 GrPrintf("------ VENDOR %s\n", vendor);
199 GrPrintf("------ RENDERER %s\n", renderer);
200 GrPrintf("------ VERSION %s\n", version);
201 GrPrintf("------ EXTENSIONS\n %s \n", ext);
202 }
203
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000204 fGLVersion = GrGLGetVersion(gl);
205 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000206 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000208 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000209
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000211
bsalomon@google.comfe676522011-06-17 18:12:21 +0000212 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213}
214
215GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000216 // This must be called by before the GrDrawTarget destructor
217 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000218 // This subclass must do this before the base class destructor runs
219 // since we will unref the GrGLInterface.
220 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000221 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000222}
223
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000224///////////////////////////////////////////////////////////////////////////////
225
226static const GrGLuint kUnknownBitCount = ~0;
227
228void GrGpuGL::initCaps() {
229 GrGLint maxTextureUnits;
230 // check FS and fixed-function texture unit limits
231 // we only use textures in the fragment stage currently.
232 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000233 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
234 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 if (kES2_GrGLBinding != this->glBinding()) {
236 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000237 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 }
239 if (kES2_GrGLBinding == this->glBinding()) {
240 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
241 &fGLCaps.fMaxFragmentUniformVectors);
242 } else if (kDesktop_GrGLBinding != this->glBinding()) {
243 GrGLint max;
244 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
245 fGLCaps.fMaxFragmentUniformVectors = max / 4;
246 } else {
247 fGLCaps.fMaxFragmentUniformVectors = 16;
248 }
249
250 GrGLint numFormats;
251 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
252 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
253 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
254 for (int i = 0; i < numFormats; ++i) {
255 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
256 fCaps.f8BitPaletteSupport = true;
257 break;
258 }
259 }
260
261 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 // we could also look for GL_ATI_separate_stencil extension or
263 // GL_EXT_stencil_two_side but they use different function signatures
264 // than GL2.0+ (and than each other).
265 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
266 // supported on GL 1.4 and higher or by extension
267 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
268 this->hasExtension("GL_EXT_stencil_wrap");
269 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000270 // ES 2 has two sided stencil and stencil wrap
271 fCaps.fTwoSidedStencilSupport = true;
272 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000273 }
274
275 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000276 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000277 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000278 fGLCaps.fRGBA8RenderbufferSupport =
279 this->hasExtension("GL_OES_rgb8_rgba8") ||
280 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000281 }
282
283
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000285 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
286 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 } else {
288 bool hasBGRAExt = false;
289 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000290 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000292 fGLCaps.fBGRAFormatSupport = true;
293 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000294 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000295 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000297 }
298
299 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000300 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000301 this->hasExtension("GL_ARB_texture_swizzle");
302 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000303 fGLCaps.fTextureSwizzleSupport = false;
304 }
305
306 if (kDesktop_GrGLBinding == this->glBinding()) {
307 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000308 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000309 fGLCaps.fPackRowLengthSupport = true;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000310 fGLCaps.fPackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000311 } else {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000312 fGLCaps.fUnpackRowLengthSupport =this->hasExtension("GL_EXT_unpack_subimage");
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000313 fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy");
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000314 // no extension for pack row length
315 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000316 fGLCaps.fPackFlipYSupport =
317 this->hasExtension("GL_ANGLE_pack_reverse_row_order");
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000318 }
319
320 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000321 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
322 // extension includes glMapBuffer.
323 } else {
324 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
325 }
326
327 if (kDesktop_GrGLBinding == this->glBinding()) {
328 if (fGLVersion >= GR_GL_VER(2,0) ||
329 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
330 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000331 } else {
332 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000333 }
334 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000335 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000336 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000337 }
338
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000339 fGLCaps.fTextureUsageSupport = (kES2_GrGLBinding == this->glBinding()) &&
340 this->hasExtension("GL_ANGLE_texture_usage");
341
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000342 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
343
344 ////////////////////////////////////////////////////////////////////////////
345 // Experiments to determine limitations that can't be queried.
346 // TODO: Make these a preprocess that generate some compile time constants.
347 // TODO: probe once at startup, rather than once per context creation.
348
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000349 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
350 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
351 // Our render targets are always created with textures as the color
352 // attachment, hence this min:
353 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
354
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000355 this->initFSAASupport();
356 this->initStencilFormats();
357}
358
359void GrGpuGL::initFSAASupport() {
360 // TODO: Get rid of GrAALevel and use # samples directly.
361 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
362 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
363 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
364 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
365 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
366
367 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
368 if (kDesktop_GrGLBinding != this->glBinding()) {
369 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
370 // chrome's extension is equivalent to the EXT msaa
371 // and fbo_blit extensions.
372 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
373 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
374 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
375 }
376 } else {
377 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
378 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
379 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
380 this->hasExtension("GL_EXT_framebuffer_blit")) {
381 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
382 }
383 }
384
385 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
386 GrGLint maxSamples;
387 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
388 if (maxSamples > 1 ) {
389 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
390 fGLCaps.fAASamples[kLow_GrAALevel] =
391 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
392 fGLCaps.fAASamples[kMed_GrAALevel] =
393 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
394 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
395 }
396 }
397 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
398}
399
400void GrGpuGL::initStencilFormats() {
401
402 // Build up list of legal stencil formats (though perhaps not supported on
403 // the particular gpu/driver) from most preferred to least.
404
405 // these consts are in order of most preferred to least preferred
406 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
407 static const GrGLStencilBuffer::Format
408 // internal Format stencil bits total bits packed?
409 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
410 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
411 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
412 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
413 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
414 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
415
416 if (kDesktop_GrGLBinding == this->glBinding()) {
417 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
418 this->hasExtension("GL_EXT_packed_depth_stencil") ||
419 this->hasExtension("GL_ARB_framebuffer_object");
420
421 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
422 // require FBO support we can expect these are legal formats and don't
423 // check. These also all support the unsized GL_STENCIL_INDEX.
424 fGLCaps.fStencilFormats.push_back() = gS8;
425 fGLCaps.fStencilFormats.push_back() = gS16;
426 if (supportsPackedDS) {
427 fGLCaps.fStencilFormats.push_back() = gD24S8;
428 }
429 fGLCaps.fStencilFormats.push_back() = gS4;
430 if (supportsPackedDS) {
431 fGLCaps.fStencilFormats.push_back() = gDS;
432 }
433 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000434 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
435 // for other formats.
436 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000437
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000438 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000439 //fStencilFormats.push_back() = gS16;
440 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
441 fGLCaps.fStencilFormats.push_back() = gD24S8;
442 }
443 if (this->hasExtension("GL_OES_stencil4")) {
444 fGLCaps.fStencilFormats.push_back() = gS4;
445 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000446 }
447}
448
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000449GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000450 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
451 return GrPixelConfigSwapRAndB(config);
452 } else {
453 return config;
454 }
455}
456
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000457GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000458 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000459 return GrPixelConfigSwapRAndB(config);
460 } else {
461 return config;
462 }
463}
464
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000465bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
466 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
467}
468
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000469void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000470 if (gPrintStartupSpew && !fPrintedCaps) {
471 fPrintedCaps = true;
472 this->getCaps().print();
473 fGLCaps.print();
474 }
475
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000476 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000477 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000478 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000479
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000480 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000481 GL_CALL(Disable(GR_GL_DEPTH_TEST));
482 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000483
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000484 GL_CALL(Disable(GR_GL_CULL_FACE));
485 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000486 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000487
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000488 GL_CALL(Disable(GR_GL_DITHER));
489 if (kDesktop_GrGLBinding == this->glBinding()) {
490 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
491 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
492 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000493 fHWAAState.fMSAAEnabled = false;
494 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000495 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000496
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000497 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000498 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000499
reed@google.comac10a2d2010-12-22 21:39:39 +0000500 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000501 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000503 // invalid
504 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000505
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000507 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000508
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000509 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000510 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000511
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000512 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000513
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000514 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000515
tomhudson@google.com93813632011-10-27 20:21:16 +0000516 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000517 fHWDrawState.setTexture(s, NULL);
518 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
519 -GR_ScalarMax,
520 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000521 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000522 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000523 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000524
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000525 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000526 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000527 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000528 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000529
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000530 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000532 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000533
534 fHWGeometryState.fIndexBuffer = NULL;
535 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000536
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000537 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000538
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000539 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000540 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000541
542 // we assume these values
543 if (this->glCaps().fUnpackRowLengthSupport) {
544 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
545 }
546 if (this->glCaps().fPackRowLengthSupport) {
547 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
548 }
549 if (this->glCaps().fUnpackFlipYSupport) {
550 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
551 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000552 if (this->glCaps().fPackFlipYSupport) {
553 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
554 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000555}
556
bsalomon@google.come269f212011-11-07 13:29:52 +0000557GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000558 GrGLenum dontCare;
bsalomon@google.come269f212011-11-07 13:29:52 +0000559 GrGLTexture::Desc glTexDesc;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000560 if (!this->canBeTexture(desc.fConfig, &glTexDesc.fInternalFormat,
561 &dontCare, &dontCare)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000562 return NULL;
563 }
564
bsalomon@google.com99621082011-11-15 16:47:16 +0000565 glTexDesc.fWidth = desc.fWidth;
566 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000567 glTexDesc.fConfig = desc.fConfig;
568 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
569 glTexDesc.fOwnsID = false;
570 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
571
572 GrGLTexture* texture = NULL;
573 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
574 GrGLRenderTarget::Desc glRTDesc;
575 glRTDesc.fRTFBOID = 0;
576 glRTDesc.fTexFBOID = 0;
577 glRTDesc.fMSColorRenderbufferID = 0;
578 glRTDesc.fOwnIDs = true;
579 glRTDesc.fConfig = desc.fConfig;
580 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000581 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
582 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000583 glTexDesc.fTextureID,
584 &glRTDesc)) {
585 return NULL;
586 }
587 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
588 } else {
589 texture = new GrGLTexture(this, glTexDesc);
590 }
591 if (NULL == texture) {
592 return NULL;
593 }
594
595 this->setSpareTextureUnit();
596 return texture;
597}
598
599GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
600 GrGLRenderTarget::Desc glDesc;
601 glDesc.fConfig = desc.fConfig;
602 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
603 glDesc.fMSColorRenderbufferID = 0;
604 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
605 glDesc.fSampleCnt = desc.fSampleCnt;
606 glDesc.fOwnIDs = false;
607 GrGLIRect viewport;
608 viewport.fLeft = 0;
609 viewport.fBottom = 0;
610 viewport.fWidth = desc.fWidth;
611 viewport.fHeight = desc.fHeight;
612
613 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
614 if (desc.fStencilBits) {
615 GrGLStencilBuffer::Format format;
616 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
617 format.fPacked = false;
618 format.fStencilBits = desc.fStencilBits;
619 format.fTotalBits = desc.fStencilBits;
620 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
621 0,
622 desc.fWidth,
623 desc.fHeight,
624 desc.fSampleCnt,
625 format);
626 tgt->setStencilBuffer(sb);
627 sb->unref();
628 }
629 return tgt;
630}
631
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000632GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
633
634 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
635 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
636 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
637 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
638
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000639 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000640 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000641
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000642 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000643 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000644 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000645 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000646 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000647 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000648 } else {
649 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000650 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000651 }
652 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000653 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000654 }
655 // we don't know what the RB ids are without glGets and we don't care
656 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000657 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000658 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000659 if (desc.fStencilBits) {
660 GrGLStencilBuffer::Format format;
661 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
662 format.fPacked = false;
663 format.fStencilBits = desc.fStencilBits;
664 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000665 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
666 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000667 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000668 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000669 }
670
671 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000672 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000673 GrGLenum dontCare;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000674 if (!canBeTexture(desc.fConfig, &texDesc.fInternalFormat,
675 &dontCare, &dontCare)) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000676 return NULL;
677 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000678 texDesc.fWidth = desc.fWidth;
679 texDesc.fHeight = desc.fHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000680
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000681 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000682 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000683 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000684 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000685
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000686 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000687 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000688 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000689 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000690 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000691 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000692 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000693 } else {
694 GrGLIRect viewport;
695 viewport.fLeft = 0;
696 viewport.fBottom = 0;
697 viewport.fWidth = desc.fWidth;
698 viewport.fHeight = desc.fHeight;
699
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000700 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000701 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000702 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000703 }
704}
705
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000706
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000707////////////////////////////////////////////////////////////////////////////////
708
bsalomon@google.com6f379512011-11-16 20:36:03 +0000709void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
710 int left, int top, int width, int height,
711 GrPixelConfig config, const void* buffer,
712 size_t rowBytes) {
713 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000714
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000715 if (NULL == buffer) {
716 return;
717 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000718 this->setSpareTextureUnit();
719 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
720 GrGLTexture::Desc desc;
721 desc.fConfig = glTex->config();
722 desc.fWidth = glTex->width();
723 desc.fHeight = glTex->height();
724 desc.fOrientation = glTex->orientation();
725 desc.fTextureID = glTex->textureID();
726 desc.fInternalFormat = glTex->internalFormat();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000727
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000728 this->uploadTexData(desc, false,
729 left, top, width, height,
730 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000731}
732
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000733namespace {
734bool adjust_pixel_ops_params(int surfaceWidth,
735 int surfaceHeight,
736 size_t bpp,
737 int* left, int* top, int* width, int* height,
738 const void** data,
739 size_t* rowBytes) {
740 if (!*rowBytes) {
741 *rowBytes = *width * bpp;
742 }
743
744 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
745 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
746
747 if (!subRect.intersect(bounds)) {
748 return false;
749 }
750 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
751 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
752
753 *left = subRect.fLeft;
754 *top = subRect.fTop;
755 *width = subRect.width();
756 *height = subRect.height();
757 return true;
758}
759}
760
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000761bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
762 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000763 int left, int top, int width, int height,
764 GrPixelConfig dataConfig,
765 const void* data,
766 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000767 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000768
769 size_t bpp = GrBytesPerPixel(dataConfig);
770 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
771 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000772 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000773 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000774 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000775
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000776 // in case we need a temporary, trimmed copy of the src pixels
777 SkAutoSMalloc<128 * 128> tempStorage;
778
bsalomon@google.com6f379512011-11-16 20:36:03 +0000779 GrGLenum dontCare;
780 GrGLenum externalFormat;
781 GrGLenum externalType;
782 if (!this->canBeTexture(dataConfig, &dontCare,
783 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000784 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000785 }
786
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000787 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == desc.fInternalFormat) {
788 // paletted textures cannot be updated
789 return false;
790 }
791
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000792 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000793 * check whether to allocate a temporary buffer for flipping y or
794 * because our srcData has extra bytes past each row. If so, we need
795 * to trim those off here, since GL ES may not let us specify
796 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000797 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000798 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000799 bool swFlipY = false;
800 bool glFlipY = false;
801
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000802 if (NULL != data) {
803 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
804 if (this->glCaps().fUnpackFlipYSupport) {
805 glFlipY = true;
806 } else {
807 swFlipY = true;
808 }
809 }
810 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
811 // can't use this for flipping, only non-neg values allowed. :(
812 if (rowBytes != trimRowBytes) {
813 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
814 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
815 restoreGLRowLength = true;
816 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000817 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000818 if (trimRowBytes != rowBytes || swFlipY) {
819 // copy data into our new storage, skipping the trailing bytes
820 size_t trimSize = height * trimRowBytes;
821 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000822 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000823 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000824 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000825 char* dst = (char*)tempStorage.reset(trimSize);
826 for (int y = 0; y < height; y++) {
827 memcpy(dst, src, trimRowBytes);
828 if (swFlipY) {
829 src -= rowBytes;
830 } else {
831 src += rowBytes;
832 }
833 dst += trimRowBytes;
834 }
835 // now point data to our copied version
836 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000837 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000838 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000839 if (glFlipY) {
840 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
841 }
842 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000843 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000844 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000845 if (isNewTexture &&
846 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000847 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000848 GrGLClearErr(this->glInterface());
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000849 if (GR_GL_PALETTE8_RGBA8 == desc.fInternalFormat) {
850 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
851 kGrColorTableSize;
852 GR_GL_CALL_NOERRCHECK(this->glInterface(),
853 CompressedTexImage2D(GR_GL_TEXTURE_2D,
854 0, // level
855 desc.fInternalFormat,
856 desc.fWidth,
857 desc.fHeight,
858 0, // border
859 imageSize,
860 data));
861 } else {
862 GR_GL_CALL_NOERRCHECK(this->glInterface(),
863 TexImage2D(GR_GL_TEXTURE_2D,
864 0, // level
865 desc.fInternalFormat,
866 desc.fWidth,
867 desc.fHeight,
868 0, // border
869 externalFormat,
870 externalType,
871 data));
872 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000873 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000874 succeeded = false;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000875 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000876 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000877 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000878 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000879 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000880 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
881 0, // level
882 left, top,
883 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000884 externalFormat, externalType, data));
885 }
886
887 if (restoreGLRowLength) {
888 GrAssert(this->glCaps().fUnpackRowLengthSupport);
889 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000890 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000891 if (glFlipY) {
892 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
893 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000894 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000895}
896
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897bool GrGpuGL::createRenderTargetObjects(int width, int height,
898 GrGLuint texID,
899 GrGLRenderTarget::Desc* desc) {
900 desc->fMSColorRenderbufferID = 0;
901 desc->fRTFBOID = 0;
902 desc->fTexFBOID = 0;
903 desc->fOwnIDs = true;
904
905 GrGLenum status;
906 GrGLint err;
907
bsalomon@google.comab15d612011-08-09 12:57:56 +0000908 GrGLenum msColorFormat = 0; // suppress warning
909
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000910 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000911 if (!desc->fTexFBOID) {
912 goto FAILED;
913 }
914
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915
916 // If we are using multisampling we will create two FBOS. We render
917 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000918 if (desc->fSampleCnt > 0) {
919 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
920 goto FAILED;
921 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
923 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000924 if (!desc->fRTFBOID ||
925 !desc->fMSColorRenderbufferID ||
926 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
927 goto FAILED;
928 }
929 } else {
930 desc->fRTFBOID = desc->fTexFBOID;
931 }
932
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000933 // below here we may bind the FBO
934 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935 if (desc->fRTFBOID != desc->fTexFBOID) {
936 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000937 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000938 desc->fMSColorRenderbufferID));
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000939 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000940 GR_GL_CALL_NOERRCHECK(this->glInterface(),
941 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
942 desc->fSampleCnt,
943 msColorFormat,
944 width, height));
945 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000946 if (err != GR_GL_NO_ERROR) {
947 goto FAILED;
948 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000949 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
950 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000951 GR_GL_COLOR_ATTACHMENT0,
952 GR_GL_RENDERBUFFER,
953 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000954 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000955 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
956 goto FAILED;
957 }
958 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000959 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000960
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000961 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
962 GR_GL_COLOR_ATTACHMENT0,
963 GR_GL_TEXTURE_2D,
964 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000965 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000966 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
967 goto FAILED;
968 }
969
970 return true;
971
972FAILED:
973 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000974 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000975 }
976 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000977 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000978 }
979 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000980 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000981 }
982 return false;
983}
984
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000985// good to set a break-point here to know when createTexture fails
986static GrTexture* return_null_texture() {
987// GrAssert(!"null texture");
988 return NULL;
989}
990
991#if GR_DEBUG
992static size_t as_size_t(int x) {
993 return x;
994}
995#endif
996
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000997GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000998 const void* srcData,
999 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001000
1001#if GR_COLLECT_STATS
1002 ++fStats.fTextureCreateCnt;
1003#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001004
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001005 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001006 GrGLRenderTarget::Desc glRTDesc;
bsalomon@google.com6f379512011-11-16 20:36:03 +00001007 GrGLenum externalFormat;
1008 GrGLenum externalType;
reed@google.comac10a2d2010-12-22 21:39:39 +00001009
bsalomon@google.com99621082011-11-15 16:47:16 +00001010 glTexDesc.fWidth = desc.fWidth;
1011 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001012 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001013 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001014
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001015 glRTDesc.fMSColorRenderbufferID = 0;
1016 glRTDesc.fRTFBOID = 0;
1017 glRTDesc.fTexFBOID = 0;
1018 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001019 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001020
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001021 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001022 if (!canBeTexture(desc.fConfig,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001023 &glTexDesc.fInternalFormat,
1024 &externalFormat,
1025 &externalType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 return return_null_texture();
1027 }
1028
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001029 const Caps& caps = this->getCaps();
1030
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001031 // We keep GrRenderTargets in GL's normal orientation so that they
1032 // can be drawn to by the outside world without the client having
1033 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001034 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001035 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001036
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001037 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1038 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1039 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1040 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 GrPrintf("AA RT requested but not supported on this platform.");
1042 }
1043
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001045 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1046 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001047 return return_null_texture();
1048 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 }
1050
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001051 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001052 if (renderTarget && this->glCaps().fTextureUsageSupport) {
1053 // provides a hint about how this texture will be used
1054 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1055 GR_GL_TEXTURE_USAGE,
1056 GR_GL_FRAMEBUFFER_ATTACHMENT));
1057 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001058 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001059 return return_null_texture();
1060 }
1061
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001062 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001063 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001064
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001065 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1066 // drivers have a bug where an FBO won't be complete if it includes a
1067 // texture that is not mipmap complete (considering the filter in use).
1068 GrGLTexture::TexParams initialTexParams;
1069 // we only set a subset here so invalidate first
1070 initialTexParams.invalidate();
1071 initialTexParams.fFilter = GR_GL_NEAREST;
1072 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1073 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001074 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1075 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001076 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001077 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1078 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001079 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001080 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1081 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001082 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001083 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1084 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001085 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001086 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1087 glTexDesc.fWidth, glTexDesc.fHeight,
1088 desc.fConfig, srcData, rowBytes)) {
1089 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1090 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001091 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001092
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001093 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 if (renderTarget) {
1095#if GR_COLLECT_STATS
1096 ++fStats.fRenderTargetCreateCnt;
1097#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001098 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1099 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001100 glTexDesc.fTextureID,
1101 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001102 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001103 return return_null_texture();
1104 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001105 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001107 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001109 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001110#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001111 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1112 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001113#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 return tex;
1115}
1116
1117namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1119 GrGLuint rb,
1120 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 // we shouldn't ever know one size and not the other
1122 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1123 (kUnknownBitCount == format->fTotalBits));
1124 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001125 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001126 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1127 (GrGLint*)&format->fStencilBits);
1128 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001129 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1131 (GrGLint*)&format->fTotalBits);
1132 format->fTotalBits += format->fStencilBits;
1133 } else {
1134 format->fTotalBits = format->fStencilBits;
1135 }
1136 }
1137}
1138}
1139
1140bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1141 int width, int height) {
1142
1143 // All internally created RTs are also textures. We don't create
1144 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1145 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001146 GrAssert(width >= rt->width());
1147 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148
1149 int samples = rt->numSamples();
1150 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001151 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 if (!sbID) {
1153 return false;
1154 }
1155
1156 GrGLStencilBuffer* sb = NULL;
1157
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001158 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 // we start with the last stencil format that succeeded in hopes
1162 // that we won't go through this loop more than once after the
1163 // first (painful) stencil creation.
1164 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001165 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001166 GrGLClearErr(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001167 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 // version on a GL that doesn't have an MSAA extension.
1169 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001170 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1171 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172 GR_GL_RENDERBUFFER,
1173 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001174 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 width,
1176 height));
1177 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001178 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1179 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001180 sFmt.fInternalFormat,
1181 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 }
1183
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001184 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 if (err == GR_GL_NO_ERROR) {
1186 // After sized formats we attempt an unsized format and take whatever
1187 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001188 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001189 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001190 sb = new GrGLStencilBuffer(this, sbID, width, height,
1191 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1193 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001194 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 return true;
1197 }
1198 sb->abandon(); // otherwise we lose sbID
1199 sb->unref();
1200 }
1201 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001203 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204}
1205
1206bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1207 GrRenderTarget* rt) {
1208 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1209
1210 GrGLuint fbo = glrt->renderFBOID();
1211
1212 if (NULL == sb) {
1213 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001214 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 GR_GL_STENCIL_ATTACHMENT,
1216 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 GR_GL_DEPTH_ATTACHMENT,
1219 GR_GL_RENDERBUFFER, 0));
1220#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001221 GrGLenum status;
1222 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1224#endif
1225 }
1226 return true;
1227 } else {
1228 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1229 GrGLuint rb = glsb->renderbufferID();
1230
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001231 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1233 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 GR_GL_STENCIL_ATTACHMENT,
1235 GR_GL_RENDERBUFFER, rb));
1236 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 GR_GL_DEPTH_ATTACHMENT,
1239 GR_GL_RENDERBUFFER, rb));
1240 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001241 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242 GR_GL_DEPTH_ATTACHMENT,
1243 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001244 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001246 GrGLenum status;
1247 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001249 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250 GR_GL_STENCIL_ATTACHMENT,
1251 GR_GL_RENDERBUFFER, 0));
1252 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001253 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001254 GR_GL_DEPTH_ATTACHMENT,
1255 GR_GL_RENDERBUFFER, 0));
1256 }
1257 return false;
1258 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001259 return true;
1260 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001262}
1263
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001264////////////////////////////////////////////////////////////////////////////////
1265
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001266GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001267 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001268 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001270 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001271 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001274 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1275 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1276 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001277 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001278 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 // deleting bound buffer does implicit bind to 0
1280 fHWGeometryState.fVertexBuffer = NULL;
1281 return NULL;
1282 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001283 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 size, dynamic);
1285 fHWGeometryState.fVertexBuffer = vertexBuffer;
1286 return vertexBuffer;
1287 }
1288 return NULL;
1289}
1290
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001291GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001292 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001295 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1296 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001298 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1299 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1300 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001301 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001302 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 // deleting bound buffer does implicit bind to 0
1304 fHWGeometryState.fIndexBuffer = NULL;
1305 return NULL;
1306 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001307 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001308 size, dynamic);
1309 fHWGeometryState.fIndexBuffer = indexBuffer;
1310 return indexBuffer;
1311 }
1312 return NULL;
1313}
1314
reed@google.comac10a2d2010-12-22 21:39:39 +00001315void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001316 const GrDrawState& drawState = this->getDrawState();
1317 const GrGLRenderTarget* rt =
1318 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1319
1320 GrAssert(NULL != rt);
1321 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001322
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001323 GrGLIRect scissor;
1324 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001325 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001326 rect->width(), rect->height());
1327 if (scissor.contains(vp)) {
1328 rect = NULL;
1329 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 }
1331
1332 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001334 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 fHWBounds.fScissorRect = scissor;
1336 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001338 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 fHWBounds.fScissorEnabled = true;
1340 }
1341 } else {
1342 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001343 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 fHWBounds.fScissorEnabled = false;
1345 }
1346 }
1347}
1348
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001349void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001350 const GrDrawState& drawState = this->getDrawState();
1351 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001352 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001353 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001354
bsalomon@google.com74b98712011-11-11 19:46:16 +00001355 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001356 if (NULL != rect) {
1357 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001358 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001359 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001360 if (clippedRect.intersect(rtRect)) {
1361 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001362 } else {
1363 return;
1364 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001366 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001367 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001368
1369 GrGLfloat r, g, b, a;
1370 static const GrGLfloat scale255 = 1.f / 255.f;
1371 a = GrColorUnpackA(color) * scale255;
1372 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001373 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001374 scaleRGB *= a;
1375 }
1376 r = GrColorUnpackR(color) * scaleRGB;
1377 g = GrColorUnpackG(color) * scaleRGB;
1378 b = GrColorUnpackB(color) * scaleRGB;
1379
1380 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001381 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001382 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001383 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001384}
1385
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001386void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001387 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001388 return;
1389 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001390
1391 this->flushRenderTarget(&GrIRect::EmptyIRect());
1392
reed@google.comac10a2d2010-12-22 21:39:39 +00001393 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001394 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001395 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001396 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001397 GL_CALL(StencilMask(0xffffffff));
1398 GL_CALL(ClearStencil(0));
1399 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001400 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001401}
1402
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001403void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001404 const GrDrawState& drawState = this->getDrawState();
1405 const GrRenderTarget* rt = drawState.getRenderTarget();
1406 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001407
1408 // this should only be called internally when we know we have a
1409 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001410 GrAssert(NULL != rt->getStencilBuffer());
1411 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001412#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001414 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001415#else
1416 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001417 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001418 // turned into draws. Our contract on GrDrawTarget says that
1419 // changing the clip between stencil passes may or may not
1420 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001421 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001422#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001423 GrGLint value;
1424 if (insideClip) {
1425 value = (1 << (stencilBitCount - 1));
1426 } else {
1427 value = 0;
1428 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001429 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001430 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001431 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001432 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001433 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001434 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001435}
1436
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001437void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001438 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001439}
1440
bsalomon@google.comc4364992011-11-07 15:54:49 +00001441bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1442 int left, int top,
1443 int width, int height,
1444 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001445 size_t rowBytes) const {
1446 // if GL can do the flip then we'll never pay for it.
1447 if (this->glCaps().fPackFlipYSupport) {
1448 return false;
1449 }
1450
1451 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001452 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001453 if (this->glCaps().fPackRowLengthSupport) {
1454 return true;
1455 }
1456 // If we have to do memcpys to handle rowBytes then y-flip is free
1457 // Note the rowBytes might be tight to the passed in data, but if data
1458 // gets clipped in x to the target the rowBytes will no longer be tight.
1459 if (left >= 0 && (left + width) < renderTarget->width()) {
1460 return 0 == rowBytes ||
1461 GrBytesPerPixel(config) * width == rowBytes;
1462 } else {
1463 return false;
1464 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001465}
1466
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001467bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001468 int left, int top,
1469 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001470 GrPixelConfig config,
1471 void* buffer,
1472 size_t rowBytes,
1473 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001474 GrGLenum internalFormat; // we don't use this for glReadPixels
1475 GrGLenum format;
1476 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001477 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1478 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001479 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001480 size_t bpp = GrBytesPerPixel(config);
1481 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1482 &left, &top, &width, &height,
1483 const_cast<const void**>(&buffer),
1484 &rowBytes)) {
1485 return false;
1486 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001487
bsalomon@google.comc6980972011-11-02 19:57:21 +00001488 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001489 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001490 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001491 switch (tgt->getResolveType()) {
1492 case GrGLRenderTarget::kCantResolve_ResolveType:
1493 return false;
1494 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001495 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001496 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001497 break;
1498 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001499 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001500 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001501 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1502 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001503 break;
1504 default:
1505 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001506 }
1507
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001508 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001509
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001510 // the read rect is viewport-relative
1511 GrGLIRect readRect;
1512 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001513
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001514 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001515 if (0 == rowBytes) {
1516 rowBytes = tightRowBytes;
1517 }
1518 size_t readDstRowBytes = tightRowBytes;
1519 void* readDst = buffer;
1520
1521 // determine if GL can read using the passed rowBytes or if we need
1522 // a scratch buffer.
1523 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1524 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001525 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001526 GrAssert(!(rowBytes % sizeof(GrColor)));
1527 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1528 readDstRowBytes = rowBytes;
1529 } else {
1530 scratch.reset(tightRowBytes * height);
1531 readDst = scratch.get();
1532 }
1533 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001534 if (!invertY && this->glCaps().fPackFlipYSupport) {
1535 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1536 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001537 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1538 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 format, type, readDst));
1540 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001541 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001542 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1543 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001544 if (!invertY && this->glCaps().fPackFlipYSupport) {
1545 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1546 invertY = true;
1547 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001548
1549 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001550 // API presents top-to-bottom. We must preserve the padding contents. Note
1551 // that the above readPixels did not overwrite the padding.
1552 if (readDst == buffer) {
1553 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001554 if (!invertY) {
1555 scratch.reset(tightRowBytes);
1556 void* tmpRow = scratch.get();
1557 // flip y in-place by rows
1558 const int halfY = height >> 1;
1559 char* top = reinterpret_cast<char*>(buffer);
1560 char* bottom = top + (height - 1) * rowBytes;
1561 for (int y = 0; y < halfY; y++) {
1562 memcpy(tmpRow, top, tightRowBytes);
1563 memcpy(top, bottom, tightRowBytes);
1564 memcpy(bottom, tmpRow, tightRowBytes);
1565 top += rowBytes;
1566 bottom -= rowBytes;
1567 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001568 }
1569 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001570 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001571 // copy from readDst to buffer while flipping y
1572 const int halfY = height >> 1;
1573 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001574 char* dst = reinterpret_cast<char*>(buffer);
1575 if (!invertY) {
1576 dst += (height-1) * rowBytes;
1577 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001578 for (int y = 0; y < height; y++) {
1579 memcpy(dst, src, tightRowBytes);
1580 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001581 if (invertY) {
1582 dst += rowBytes;
1583 } else {
1584 dst -= rowBytes;
1585 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001586 }
1587 }
1588 return true;
1589}
1590
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001591void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001592
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001593 GrGLRenderTarget* rt =
1594 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1595 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001596
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001597 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001598 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001599 #if GR_COLLECT_STATS
1600 ++fStats.fRenderTargetChngCnt;
1601 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001602 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001603 GrGLenum status;
1604 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001605 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001606 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001607 }
1608 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001609 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001610 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001611 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001612 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001613 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 fHWBounds.fViewportRect = vp;
1615 }
1616 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001617 if (NULL == bound || !bound->isEmpty()) {
1618 rt->flagAsNeedingResolve(bound);
1619 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001620}
1621
twiz@google.com0f31ca72011-03-18 17:38:11 +00001622GrGLenum gPrimitiveType2GLMode[] = {
1623 GR_GL_TRIANGLES,
1624 GR_GL_TRIANGLE_STRIP,
1625 GR_GL_TRIANGLE_FAN,
1626 GR_GL_POINTS,
1627 GR_GL_LINES,
1628 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001629};
1630
bsalomon@google.comd302f142011-03-03 13:54:13 +00001631#define SWAP_PER_DRAW 0
1632
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001633#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001634 #if GR_MAC_BUILD
1635 #include <AGL/agl.h>
1636 #elif GR_WIN32_BUILD
1637 void SwapBuf() {
1638 DWORD procID = GetCurrentProcessId();
1639 HWND hwnd = GetTopWindow(GetDesktopWindow());
1640 while(hwnd) {
1641 DWORD wndProcID = 0;
1642 GetWindowThreadProcessId(hwnd, &wndProcID);
1643 if(wndProcID == procID) {
1644 SwapBuffers(GetDC(hwnd));
1645 }
1646 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1647 }
1648 }
1649 #endif
1650#endif
1651
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001652void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1653 uint32_t startVertex,
1654 uint32_t startIndex,
1655 uint32_t vertexCount,
1656 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1658
twiz@google.com0f31ca72011-03-18 17:38:11 +00001659 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001660
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001661 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1662 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1663
1664 // our setupGeometry better have adjusted this to zero since
1665 // DrawElements always draws from the begining of the arrays for idx 0.
1666 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001667
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001668 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1669 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001670#if SWAP_PER_DRAW
1671 glFlush();
1672 #if GR_MAC_BUILD
1673 aglSwapBuffers(aglGetCurrentContext());
1674 int set_a_break_pt_here = 9;
1675 aglSwapBuffers(aglGetCurrentContext());
1676 #elif GR_WIN32_BUILD
1677 SwapBuf();
1678 int set_a_break_pt_here = 9;
1679 SwapBuf();
1680 #endif
1681#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001682}
1683
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001684void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1685 uint32_t startVertex,
1686 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001687 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1688
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001689 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1690
1691 // our setupGeometry better have adjusted this to zero.
1692 // DrawElements doesn't take an offset so we always adjus the startVertex.
1693 GrAssert(0 == startVertex);
1694
1695 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1696 // account for startVertex in the DrawElements case. So we always
1697 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001698 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001699#if SWAP_PER_DRAW
1700 glFlush();
1701 #if GR_MAC_BUILD
1702 aglSwapBuffers(aglGetCurrentContext());
1703 int set_a_break_pt_here = 9;
1704 aglSwapBuffers(aglGetCurrentContext());
1705 #elif GR_WIN32_BUILD
1706 SwapBuf();
1707 int set_a_break_pt_here = 9;
1708 SwapBuf();
1709 #endif
1710#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001711}
1712
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001713void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001714
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001715 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001716 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001717 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001718 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1719 rt->renderFBOID()));
1720 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1721 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 #if GR_COLLECT_STATS
1723 ++fStats.fRenderTargetChngCnt;
1724 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001725 // make sure we go through flushRenderTarget() since we've modified
1726 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001727 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001728 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001729 const GrIRect dirtyRect = rt->getResolveRect();
1730 GrGLIRect r;
1731 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1732 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001733
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001734 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001735 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001736 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1737 GL_CALL(Scissor(r.fLeft, r.fBottom,
1738 r.fWidth, r.fHeight));
1739 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001740 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001741 fHWBounds.fScissorEnabled = true;
1742 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001743 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001744 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001745 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1746 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001747 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001748 int right = r.fLeft + r.fWidth;
1749 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001750 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1751 r.fLeft, r.fBottom, right, top,
1752 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001753 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001754 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001755 }
1756}
1757
twiz@google.com0f31ca72011-03-18 17:38:11 +00001758static const GrGLenum grToGLStencilFunc[] = {
1759 GR_GL_ALWAYS, // kAlways_StencilFunc
1760 GR_GL_NEVER, // kNever_StencilFunc
1761 GR_GL_GREATER, // kGreater_StencilFunc
1762 GR_GL_GEQUAL, // kGEqual_StencilFunc
1763 GR_GL_LESS, // kLess_StencilFunc
1764 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1765 GR_GL_EQUAL, // kEqual_StencilFunc,
1766 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001767};
1768GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1769GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1770GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1771GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1772GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1773GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1774GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1775GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1776GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1777
twiz@google.com0f31ca72011-03-18 17:38:11 +00001778static const GrGLenum grToGLStencilOp[] = {
1779 GR_GL_KEEP, // kKeep_StencilOp
1780 GR_GL_REPLACE, // kReplace_StencilOp
1781 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1782 GR_GL_INCR, // kIncClamp_StencilOp
1783 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1784 GR_GL_DECR, // kDecClamp_StencilOp
1785 GR_GL_ZERO, // kZero_StencilOp
1786 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001787};
1788GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1789GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1790GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1791GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1792GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1793GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1794GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1795GR_STATIC_ASSERT(6 == kZero_StencilOp);
1796GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1797
reed@google.comac10a2d2010-12-22 21:39:39 +00001798void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001799 const GrDrawState& drawState = this->getDrawState();
1800
1801 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001802
1803 // use stencil for clipping if clipping is enabled and the clip
1804 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001805 bool stencilClip = fClipInStencil && drawState.isClipState();
1806 bool drawClipToStencil =
1807 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001808 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1809 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001810 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1811 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001812
1813 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001814
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001815 // we can't simultaneously perform stencil-clipping and
1816 // modify the stencil clip
1817 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001818
bsalomon@google.comd302f142011-03-03 13:54:13 +00001819 if (settings->isDisabled()) {
1820 if (stencilClip) {
1821 settings = &gClipStencilSettings;
1822 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001823 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001824
1825 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001826 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001827 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001828 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001829 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001830 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001831 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1832 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1833 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1834 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1835 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1836 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1837 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1838 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001839 }
1840 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001841 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001842 GrStencilBuffer* stencilBuffer =
1843 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001844 if (NULL != stencilBuffer) {
1845 stencilBits = stencilBuffer->bits();
1846 }
1847 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001848 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001849
1850 GrGLuint clipStencilMask = 0;
1851 GrGLuint userStencilMask = ~0;
1852 if (stencilBits > 0) {
1853 clipStencilMask = 1 << (stencilBits - 1);
1854 userStencilMask = clipStencilMask - 1;
1855 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001856
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001857 unsigned int frontRef = settings->frontFuncRef();
1858 unsigned int frontMask = settings->frontFuncMask();
1859 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001860 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001862 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001863 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1864 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001865 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001866 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001867 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001868
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001869 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001870 stencilClip,
1871 clipStencilMask,
1872 userStencilMask,
1873 &frontRef,
1874 &frontMask);
1875 frontWriteMask &= userStencilMask;
1876 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001877 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001878 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001879 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001880 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001881 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001882 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001883 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001884 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001885 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001886 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001888 unsigned int backRef = settings->backFuncRef();
1889 unsigned int backMask = settings->backFuncMask();
1890 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001891
1892
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001893 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001894 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1895 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001896 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001897 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001898 stencilClip, settings->backFunc())];
1899 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900 stencilClip,
1901 clipStencilMask,
1902 userStencilMask,
1903 &backRef,
1904 &backMask);
1905 backWriteMask &= userStencilMask;
1906 }
1907
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001908 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1909 frontRef, frontMask));
1910 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1911 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1912 backRef, backMask));
1913 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1914 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001915 grToGLStencilOp[settings->frontFailOp()],
1916 grToGLStencilOp[settings->frontPassOp()],
1917 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001918
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001919 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001920 grToGLStencilOp[settings->backFailOp()],
1921 grToGLStencilOp[settings->backPassOp()],
1922 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001924 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1925 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001926 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1927 grToGLStencilOp[settings->frontPassOp()],
1928 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001929 }
1930 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001931 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001932 fHWStencilClip = stencilClip;
1933 }
1934}
1935
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001936void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001937 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001938 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001939 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1940 // smooth lines.
1941
1942 // we prefer smooth lines over multisampled lines
1943 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001944 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001945 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001946 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001947 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001948 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001949 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001950 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001951 fHWAAState.fSmoothLineEnabled = false;
1952 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001953 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001954 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001955 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956 fHWAAState.fMSAAEnabled = false;
1957 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001958 } else if (rt->isMultisampled() &&
1959 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001960 fHWAAState.fMSAAEnabled) {
1961 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001962 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001963 fHWAAState.fMSAAEnabled = false;
1964 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001965 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001966 fHWAAState.fMSAAEnabled = true;
1967 }
1968 }
1969 }
1970}
1971
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001972void GrGpuGL::flushBlend(GrPrimitiveType type,
1973 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001974 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001975 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001976 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001977 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001978 fHWBlendDisabled = false;
1979 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001980 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1981 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001982 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1983 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001984 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001985 }
1986 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001987 // any optimization to disable blending should
1988 // have already been applied and tweaked the coeffs
1989 // to (1, 0).
1990 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1991 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001992 if (fHWBlendDisabled != blendOff) {
1993 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001994 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001995 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001996 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001997 }
1998 fHWBlendDisabled = blendOff;
1999 }
2000 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002001 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2002 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002003 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2004 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002005 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002006 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002007 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002008 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2009 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002010 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002011
2012 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002013 GrColorUnpackR(blendConst) / 255.f,
2014 GrColorUnpackG(blendConst) / 255.f,
2015 GrColorUnpackB(blendConst) / 255.f,
2016 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002017 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002018 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002019 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002020 }
2021 }
2022 }
2023}
2024
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002025namespace {
2026
2027unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002028 switch (filter) {
2029 case GrSamplerState::kBilinear_Filter:
2030 case GrSamplerState::k4x4Downsample_Filter:
2031 return GR_GL_LINEAR;
2032 case GrSamplerState::kNearest_Filter:
2033 case GrSamplerState::kConvolution_Filter:
2034 return GR_GL_NEAREST;
2035 default:
2036 GrAssert(!"Unknown filter type");
2037 return GR_GL_LINEAR;
2038 }
2039}
2040
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002041const GrGLenum* get_swizzle(GrPixelConfig config,
2042 const GrSamplerState& sampler) {
2043 if (GrPixelConfigIsAlphaOnly(config)) {
2044 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2045 GR_GL_ALPHA, GR_GL_ALPHA };
2046 return gAlphaSmear;
2047 } else if (sampler.swapsRAndB()) {
2048 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2049 GR_GL_RED, GR_GL_ALPHA };
2050 return gRedBlueSwap;
2051 } else {
2052 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2053 GR_GL_BLUE, GR_GL_ALPHA };
2054 return gStraight;
2055 }
2056}
2057
2058void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2059 // should add texparameteri to interface to make 1 instead of 4 calls here
2060 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2061 GR_GL_TEXTURE_SWIZZLE_R,
2062 swizzle[0]));
2063 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2064 GR_GL_TEXTURE_SWIZZLE_G,
2065 swizzle[1]));
2066 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2067 GR_GL_TEXTURE_SWIZZLE_B,
2068 swizzle[2]));
2069 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2070 GR_GL_TEXTURE_SWIZZLE_A,
2071 swizzle[3]));
2072}
2073}
2074
bsalomon@google.comffca4002011-02-22 20:34:01 +00002075bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002076
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002077 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002078 // GrGpu::setupClipAndFlushState should have already checked this
2079 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002080 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002081
tomhudson@google.com93813632011-10-27 20:21:16 +00002082 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002083 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002084 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002085 GrGLTexture* nextTexture =
2086 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002087
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002088 // true for now, but maybe not with GrEffect.
2089 GrAssert(NULL != nextTexture);
2090 // if we created a rt/tex and rendered to it without using a
2091 // texture and now we're texuring from the rt it will still be
2092 // the last bound texture, but it needs resolving. So keep this
2093 // out of the "last != next" check.
2094 GrGLRenderTarget* texRT =
2095 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2096 if (NULL != texRT) {
2097 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002098 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002099
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002100 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002101 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002102 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002103 #if GR_COLLECT_STATS
2104 ++fStats.fTextureChngCnt;
2105 #endif
2106 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002107 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002108 // The texture matrix has to compensate for texture width/height
2109 // and NPOT-embedded-in-POT
2110 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002111 }
2112
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002113 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002114 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002115 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002116 nextTexture->getCachedTexParams(&timestamp);
2117 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002118 GrGLTexture::TexParams newTexParams;
2119
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002120 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002121
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002122 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002123 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2124 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002125 memcpy(newTexParams.fSwizzleRGBA,
2126 get_swizzle(nextTexture->config(), sampler),
2127 sizeof(newTexParams.fSwizzleRGBA));
2128 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002129 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002130 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002131 GR_GL_TEXTURE_MAG_FILTER,
2132 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002133 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002134 GR_GL_TEXTURE_MIN_FILTER,
2135 newTexParams.fFilter));
2136 }
2137 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2138 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002139 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002140 GR_GL_TEXTURE_WRAP_S,
2141 newTexParams.fWrapS));
2142 }
2143 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2144 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002145 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002146 GR_GL_TEXTURE_WRAP_T,
2147 newTexParams.fWrapT));
2148 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002149 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002150 (setAll ||
2151 memcmp(newTexParams.fSwizzleRGBA,
2152 oldTexParams.fSwizzleRGBA,
2153 sizeof(newTexParams.fSwizzleRGBA)))) {
2154 setTextureUnit(s);
2155 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2156 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002157 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002158 nextTexture->setCachedTexParams(newTexParams,
2159 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002160 }
2161 }
2162
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002163 GrIRect* rect = NULL;
2164 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002165 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002166 fClip.hasConservativeBounds()) {
2167 fClip.getConservativeBounds().roundOut(&clipBounds);
2168 rect = &clipBounds;
2169 }
2170 this->flushRenderTarget(rect);
2171 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002172
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002173 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2174 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002175 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002176 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002177 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002178 }
2179 }
2180
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002181 if (drawState->isColorWriteDisabled() !=
2182 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002183 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002184 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002185 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002186 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002187 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002188 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002189 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002190 }
2191
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002192 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2193 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002194 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002195 GL_CALL(Enable(GR_GL_CULL_FACE));
2196 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002197 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002198 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002199 GL_CALL(Enable(GR_GL_CULL_FACE));
2200 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002201 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002202 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002203 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002204 break;
2205 default:
2206 GrCrash("Unknown draw face.");
2207 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002208 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002209 }
2210
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002211#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002212 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002213 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002214 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002215 NULL == drawState->getRenderTarget() ||
2216 NULL == drawState->getTexture(s) ||
2217 drawState->getTexture(s)->asRenderTarget() !=
2218 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002219 }
2220#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002221
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002222 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002223
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002224 // This copy must happen after flushStencil() is called. flushStencil()
2225 // relies on detecting when the kModifyStencilClip_StateBit state has
2226 // changed since the last draw.
2227 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002228 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002229}
2230
2231void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002232 if (fHWGeometryState.fVertexBuffer != buffer) {
2233 fHWGeometryState.fArrayPtrsDirty = true;
2234 fHWGeometryState.fVertexBuffer = buffer;
2235 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002236}
2237
2238void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002239 if (fHWGeometryState.fVertexBuffer == buffer) {
2240 // deleting bound buffer does implied bind to 0
2241 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002242 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002243 }
2244}
2245
2246void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002247 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002248}
2249
2250void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 if (fHWGeometryState.fIndexBuffer == buffer) {
2252 // deleting bound buffer does implied bind to 0
2253 fHWGeometryState.fIndexBuffer = NULL;
2254 }
2255}
2256
reed@google.comac10a2d2010-12-22 21:39:39 +00002257void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2258 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002259 GrDrawState* drawState = this->drawState();
2260 if (drawState->getRenderTarget() == renderTarget) {
2261 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002262 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002263 if (fHWDrawState.getRenderTarget() == renderTarget) {
2264 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002265 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002266}
2267
2268void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002269 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002270 GrDrawState* drawState = this->drawState();
2271 if (drawState->getTexture(s) == texture) {
2272 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002273 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002274 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002275 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002276 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002277 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002278 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002279}
2280
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002281bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002282 GrGLenum* internalFormat,
bsalomon@google.com6f379512011-11-16 20:36:03 +00002283 GrGLenum* externalFormat,
2284 GrGLenum* externalType) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002285 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002286 case kRGBA_8888_PM_GrPixelConfig:
2287 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002288 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002289 *externalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002290 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002291 break;
2292 case kBGRA_8888_PM_GrPixelConfig:
2293 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002294 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002295 return false;
2296 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002297 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002298 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002299 } else {
2300 *internalFormat = GR_GL_RGBA;
2301 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002302 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002303 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002304 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002305 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002306 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002307 *externalFormat = GR_GL_RGB;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002308 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002309 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002310 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002311 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002312 *externalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002313 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002314 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002315 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002316 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002317 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002318 // glCompressedTexImage doesn't take external params
2319 *externalFormat = GR_GL_PALETTE8_RGBA8;
2320 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002321 } else {
2322 return false;
2323 }
2324 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002325 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002326 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002327 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002328 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002329 break;
2330 default:
2331 return false;
2332 }
2333 return true;
2334}
2335
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002336void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002337 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002338 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002339 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002340 fActiveTextureUnitIdx = unit;
2341 }
2342}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002343
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002344void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002345 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002346 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002347 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2348 }
2349}
2350
reed@google.comac10a2d2010-12-22 21:39:39 +00002351/* On ES the internalFormat and format must match for TexImage and we use
2352 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2353 decide the internalFormat. However, on ES internalFormat for
2354 RenderBufferStorage* has to be a specific format (not a base format like
2355 GL_RGBA).
2356 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002357bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002358 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002359 // The ES story for BGRA and RenderbufferStorage appears murky. It
2360 // takes an internal format as a parameter. The OES FBO extension and
2361 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2362 // BGRA extensions adds BGRA as both an internal and external format
2363 // and the other only as an external format (like desktop GL). OES
2364 // restricts RenderbufferStorage's format to a *sized* internal format.
2365 // There is no sized BGRA internal format.
2366 // So if the texture has internal format BGRA we just hope that the
2367 // resolve blit can do RGBA->BGRA internal format conversion.
2368 case kRGBA_8888_PM_GrPixelConfig:
2369 case kRGBA_8888_UPM_GrPixelConfig:
2370 case kBGRA_8888_PM_GrPixelConfig:
2371 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002372 if (fGLCaps.fRGBA8RenderbufferSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002373 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2374 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002375 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002376 return true;
2377 } else {
2378 return false;
2379 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002380 case kRGB_565_GrPixelConfig:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002381 // ES2 supports 565, but desktop GL does not.
bsalomon@google.comc4364992011-11-07 15:54:49 +00002382 if (kDesktop_GrGLBinding != this->glBinding()) {
2383 *format = GR_GL_RGB565;
2384 return true;
2385 } else {
2386 return false;
2387 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002388 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002389 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002390 return true;
2391 default:
2392 return false;
2393 }
2394}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002395
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002396void GrGpuGL::resetDirtyFlags() {
2397 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2398}
2399
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002400void GrGpuGL::setBuffers(bool indexed,
2401 int* extraVertexOffset,
2402 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002403
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002404 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002405
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002406 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2407
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002408 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002409 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002410 case kBuffer_GeometrySrcType:
2411 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002412 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002413 break;
2414 case kArray_GeometrySrcType:
2415 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002416 this->finalizeReservedVertices();
2417 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2418 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002419 break;
2420 default:
2421 vbuf = NULL; // suppress warning
2422 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002423 }
2424
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002425 GrAssert(NULL != vbuf);
2426 GrAssert(!vbuf->isLocked());
2427 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002428 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002429 fHWGeometryState.fArrayPtrsDirty = true;
2430 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002431 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002432
2433 if (indexed) {
2434 GrAssert(NULL != extraIndexOffset);
2435
2436 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002437 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002438 case kBuffer_GeometrySrcType:
2439 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002440 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002441 break;
2442 case kArray_GeometrySrcType:
2443 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002444 this->finalizeReservedIndices();
2445 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2446 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002447 break;
2448 default:
2449 ibuf = NULL; // suppress warning
2450 GrCrash("Unknown geometry src type!");
2451 }
2452
2453 GrAssert(NULL != ibuf);
2454 GrAssert(!ibuf->isLocked());
2455 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002456 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002457 fHWGeometryState.fIndexBuffer = ibuf;
2458 }
2459 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002460}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002461
2462int GrGpuGL::getMaxEdges() const {
2463 // FIXME: This is a pessimistic estimate based on how many other things
2464 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002465 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2466 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002467}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002468
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002469void GrGpuGL::GLCaps::print() const {
2470 for (int i = 0; i < fStencilFormats.count(); ++i) {
2471 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2472 i,
2473 fStencilFormats[i].fStencilBits,
2474 fStencilFormats[i].fTotalBits);
2475 }
2476
2477 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2478 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2479 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2480 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2481 static const char* gMSFBOExtStr[] = {
2482 "None",
2483 "ARB",
2484 "EXT",
2485 "Apple",
2486 };
2487 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002488 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002489 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2490 }
2491 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2492 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002493 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002494 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002495 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002496 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002497 (fTextureSwizzleSupport ? "YES": "NO"));
2498 GrPrintf("Unpack Row length support: %s\n",
2499 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002500 GrPrintf("Unpack Flip Y support: %s\n",
2501 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002502 GrPrintf("Pack Row length support: %s\n",
2503 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002504 GrPrintf("Pack Flip Y support: %s\n",
2505 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002506}