blob: 5df1ed1a5d12acf5e03894f2be028fdc8f5a4031 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
twiz@google.com0f31ca72011-03-18 17:38:11 +000027static const GrGLenum gXfermodeCoeff2Blend[] = {
28 GR_GL_ZERO,
29 GR_GL_ONE,
30 GR_GL_SRC_COLOR,
31 GR_GL_ONE_MINUS_SRC_COLOR,
32 GR_GL_DST_COLOR,
33 GR_GL_ONE_MINUS_DST_COLOR,
34 GR_GL_SRC_ALPHA,
35 GR_GL_ONE_MINUS_SRC_ALPHA,
36 GR_GL_DST_ALPHA,
37 GR_GL_ONE_MINUS_DST_ALPHA,
38 GR_GL_CONSTANT_COLOR,
39 GR_GL_ONE_MINUS_CONSTANT_COLOR,
40 GR_GL_CONSTANT_ALPHA,
41 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
43 // extended blend coeffs
44 GR_GL_SRC1_COLOR,
45 GR_GL_ONE_MINUS_SRC1_COLOR,
46 GR_GL_SRC1_ALPHA,
47 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000048};
49
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 static const bool gCoeffReferencesBlendConst[] = {
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 false,
62 true,
63 true,
64 true,
65 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000066
67 // extended blend coeffs
68 false,
69 false,
70 false,
71 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000072 };
73 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
75
76 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
77 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
78 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
79 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
80 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
81 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
82 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
83 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
84 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
85 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
86 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
87 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
88 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
89 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
90
91 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
92 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
93 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
94 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
95
96 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
97 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000098}
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
103 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104 GrMatrix* matrix) {
105 GrAssert(NULL != texture);
106 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000107 GrGLTexture::Orientation orientation = texture->orientation();
108 if (GrGLTexture::kBottomUp_Orientation == orientation) {
109 GrMatrix invY;
110 invY.setAll(GR_Scalar1, 0, 0,
111 0, -GR_Scalar1, GR_Scalar1,
112 0, 0, GrMatrix::I()[8]);
113 matrix->postConcat(invY);
114 } else {
115 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
116 }
117}
118
bsalomon@google.comd302f142011-03-03 13:54:13 +0000119bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120 const GrSamplerState& sampler) {
121 GrAssert(NULL != texture);
122 if (!sampler.getMatrix().isIdentity()) {
123 return false;
124 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000125 GrGLTexture::Orientation orientation = texture->orientation();
126 if (GrGLTexture::kBottomUp_Orientation == orientation) {
127 return false;
128 } else {
129 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
130 }
131 return true;
132}
133
134///////////////////////////////////////////////////////////////////////////////
135
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000136static bool gPrintStartupSpew;
137
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000139
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000141
twiz@google.com0f31ca72011-03-18 17:38:11 +0000142 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
144 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000145 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000146 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
147 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000148 // some implementations require texture to be mip-map complete before
149 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000150 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
151 GR_GL_TEXTURE_MIN_FILTER,
152 GR_GL_NEAREST));
153 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
154 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
155 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
156 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
157 GR_GL_COLOR_ATTACHMENT0,
158 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000159 GrGLenum status;
160 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
162 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000163
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000165}
166
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000167GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000168
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000169 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000170
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 gl->ref();
172 fGL = gl;
173 fGLBinding = glBinding;
174 switch (glBinding) {
175 case kDesktop_GrGLBinding:
176 GrAssert(gl->supportsDesktop());
177 break;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000178 case kES2_GrGLBinding:
179 GrAssert(gl->supportsES2());
180 break;
181 default:
182 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000183 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185 GrGLClearErr(fGL);
186
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000187 const GrGLubyte* ext;
188 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000189 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000190 const GrGLubyte* vendor;
191 const GrGLubyte* renderer;
192 const GrGLubyte* version;
193 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
194 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
195 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000196 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
197 this);
198 GrPrintf("------ VENDOR %s\n", vendor);
199 GrPrintf("------ RENDERER %s\n", renderer);
200 GrPrintf("------ VERSION %s\n", version);
201 GrPrintf("------ EXTENSIONS\n %s \n", ext);
202 }
203
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000204 fGLVersion = GrGLGetVersion(gl);
205 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000206 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000208 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000209
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000211
bsalomon@google.comfe676522011-06-17 18:12:21 +0000212 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213}
214
215GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000216 // This must be called by before the GrDrawTarget destructor
217 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000218 // This subclass must do this before the base class destructor runs
219 // since we will unref the GrGLInterface.
220 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000221 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000222}
223
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000224///////////////////////////////////////////////////////////////////////////////
225
226static const GrGLuint kUnknownBitCount = ~0;
227
228void GrGpuGL::initCaps() {
229 GrGLint maxTextureUnits;
230 // check FS and fixed-function texture unit limits
231 // we only use textures in the fragment stage currently.
232 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000233 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
234 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 if (kES2_GrGLBinding != this->glBinding()) {
236 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000237 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 }
239 if (kES2_GrGLBinding == this->glBinding()) {
240 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
241 &fGLCaps.fMaxFragmentUniformVectors);
242 } else if (kDesktop_GrGLBinding != this->glBinding()) {
243 GrGLint max;
244 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
245 fGLCaps.fMaxFragmentUniformVectors = max / 4;
246 } else {
247 fGLCaps.fMaxFragmentUniformVectors = 16;
248 }
249
250 GrGLint numFormats;
251 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
252 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
253 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
254 for (int i = 0; i < numFormats; ++i) {
255 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
256 fCaps.f8BitPaletteSupport = true;
257 break;
258 }
259 }
260
261 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 // we could also look for GL_ATI_separate_stencil extension or
263 // GL_EXT_stencil_two_side but they use different function signatures
264 // than GL2.0+ (and than each other).
265 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
266 // supported on GL 1.4 and higher or by extension
267 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
268 this->hasExtension("GL_EXT_stencil_wrap");
269 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000270 // ES 2 has two sided stencil and stencil wrap
271 fCaps.fTwoSidedStencilSupport = true;
272 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000273 }
274
275 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000276 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000277 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000278 fGLCaps.fRGBA8RenderbufferSupport =
279 this->hasExtension("GL_OES_rgb8_rgba8") ||
280 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000281 }
282
283
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000285 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
286 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 } else {
288 bool hasBGRAExt = false;
289 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000290 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000292 fGLCaps.fBGRAFormatSupport = true;
293 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000294 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000295 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000297 }
298
299 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000300 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000301 this->hasExtension("GL_ARB_texture_swizzle");
302 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000303 fGLCaps.fTextureSwizzleSupport = false;
304 }
305
306 if (kDesktop_GrGLBinding == this->glBinding()) {
307 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000308 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000309 fGLCaps.fPackRowLengthSupport = true;
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
339 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
340
341 ////////////////////////////////////////////////////////////////////////////
342 // Experiments to determine limitations that can't be queried.
343 // TODO: Make these a preprocess that generate some compile time constants.
344 // TODO: probe once at startup, rather than once per context creation.
345
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000346 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
347 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
348 // Our render targets are always created with textures as the color
349 // attachment, hence this min:
350 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
351
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000352 this->initFSAASupport();
353 this->initStencilFormats();
354}
355
356void GrGpuGL::initFSAASupport() {
357 // TODO: Get rid of GrAALevel and use # samples directly.
358 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
359 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
360 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
361 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
362 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
363
364 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
365 if (kDesktop_GrGLBinding != this->glBinding()) {
366 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
367 // chrome's extension is equivalent to the EXT msaa
368 // and fbo_blit extensions.
369 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
370 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
371 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
372 }
373 } else {
374 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
375 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
376 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
377 this->hasExtension("GL_EXT_framebuffer_blit")) {
378 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
379 }
380 }
381
382 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
383 GrGLint maxSamples;
384 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
385 if (maxSamples > 1 ) {
386 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
387 fGLCaps.fAASamples[kLow_GrAALevel] =
388 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
389 fGLCaps.fAASamples[kMed_GrAALevel] =
390 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
391 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
392 }
393 }
394 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
395}
396
397void GrGpuGL::initStencilFormats() {
398
399 // Build up list of legal stencil formats (though perhaps not supported on
400 // the particular gpu/driver) from most preferred to least.
401
402 // these consts are in order of most preferred to least preferred
403 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
404 static const GrGLStencilBuffer::Format
405 // internal Format stencil bits total bits packed?
406 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
407 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
408 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
409 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
410 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
411 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
412
413 if (kDesktop_GrGLBinding == this->glBinding()) {
414 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
415 this->hasExtension("GL_EXT_packed_depth_stencil") ||
416 this->hasExtension("GL_ARB_framebuffer_object");
417
418 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
419 // require FBO support we can expect these are legal formats and don't
420 // check. These also all support the unsized GL_STENCIL_INDEX.
421 fGLCaps.fStencilFormats.push_back() = gS8;
422 fGLCaps.fStencilFormats.push_back() = gS16;
423 if (supportsPackedDS) {
424 fGLCaps.fStencilFormats.push_back() = gD24S8;
425 }
426 fGLCaps.fStencilFormats.push_back() = gS4;
427 if (supportsPackedDS) {
428 fGLCaps.fStencilFormats.push_back() = gDS;
429 }
430 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000431 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
432 // for other formats.
433 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000434
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000435 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000436 //fStencilFormats.push_back() = gS16;
437 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
438 fGLCaps.fStencilFormats.push_back() = gD24S8;
439 }
440 if (this->hasExtension("GL_OES_stencil4")) {
441 fGLCaps.fStencilFormats.push_back() = gS4;
442 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000443 }
444}
445
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000446GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000447 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
448 return GrPixelConfigSwapRAndB(config);
449 } else {
450 return config;
451 }
452}
453
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000454GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000455 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000456 return GrPixelConfigSwapRAndB(config);
457 } else {
458 return config;
459 }
460}
461
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000462bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
463 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
464}
465
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000466void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000467 if (gPrintStartupSpew && !fPrintedCaps) {
468 fPrintedCaps = true;
469 this->getCaps().print();
470 fGLCaps.print();
471 }
472
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000473 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000474 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000475 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000476
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000477 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000478 GL_CALL(Disable(GR_GL_DEPTH_TEST));
479 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000480
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000481 GL_CALL(Disable(GR_GL_CULL_FACE));
482 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000483 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000484
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000485 GL_CALL(Disable(GR_GL_DITHER));
486 if (kDesktop_GrGLBinding == this->glBinding()) {
487 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
488 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
489 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000490 fHWAAState.fMSAAEnabled = false;
491 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000492 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000494 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000495 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000496
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000498 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000500 // invalid
501 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 // illegal values
tomhudson@google.com62b09682011-11-09 16:39:17 +0000504 //fHWDrawState.fSrcBlend = (GrBlendCoeff)(uint8_t)-1;
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000505 fHWDrawState.setBlendFunc((GrBlendCoeff)-1, (GrBlendCoeff)-1);
506 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000507 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000508
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000509 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000510
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000511 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000512
tomhudson@google.com93813632011-10-27 20:21:16 +0000513 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000514 fHWDrawState.setTexture(s, NULL);
515 GrSamplerState* sampler = fHWDrawState.sampler(s);
516 sampler->setRadial2Params(-GR_ScalarMax,
517 -GR_ScalarMax,
518 true);
519 sampler->setMatrix(GrMatrix::InvalidMatrix());
520 sampler->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000521 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000522
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000523 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000525 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000526 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000527
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000528 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000529 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000530 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000531
532 fHWGeometryState.fIndexBuffer = NULL;
533 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000534
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000535 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000536
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000537 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000538 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000539
540 // we assume these values
541 if (this->glCaps().fUnpackRowLengthSupport) {
542 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
543 }
544 if (this->glCaps().fPackRowLengthSupport) {
545 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
546 }
547 if (this->glCaps().fUnpackFlipYSupport) {
548 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
549 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000550 if (this->glCaps().fPackFlipYSupport) {
551 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
552 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000553}
554
bsalomon@google.come269f212011-11-07 13:29:52 +0000555GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000556 GrGLenum dontCare;
bsalomon@google.come269f212011-11-07 13:29:52 +0000557 GrGLTexture::Desc glTexDesc;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000558 if (!this->canBeTexture(desc.fConfig, &glTexDesc.fInternalFormat,
559 &dontCare, &dontCare)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000560 return NULL;
561 }
562
bsalomon@google.com99621082011-11-15 16:47:16 +0000563 glTexDesc.fWidth = desc.fWidth;
564 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000565 glTexDesc.fConfig = desc.fConfig;
566 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
567 glTexDesc.fOwnsID = false;
568 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
569
570 GrGLTexture* texture = NULL;
571 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
572 GrGLRenderTarget::Desc glRTDesc;
573 glRTDesc.fRTFBOID = 0;
574 glRTDesc.fTexFBOID = 0;
575 glRTDesc.fMSColorRenderbufferID = 0;
576 glRTDesc.fOwnIDs = true;
577 glRTDesc.fConfig = desc.fConfig;
578 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000579 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
580 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000581 glTexDesc.fTextureID,
582 &glRTDesc)) {
583 return NULL;
584 }
585 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
586 } else {
587 texture = new GrGLTexture(this, glTexDesc);
588 }
589 if (NULL == texture) {
590 return NULL;
591 }
592
593 this->setSpareTextureUnit();
594 return texture;
595}
596
597GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
598 GrGLRenderTarget::Desc glDesc;
599 glDesc.fConfig = desc.fConfig;
600 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
601 glDesc.fMSColorRenderbufferID = 0;
602 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
603 glDesc.fSampleCnt = desc.fSampleCnt;
604 glDesc.fOwnIDs = false;
605 GrGLIRect viewport;
606 viewport.fLeft = 0;
607 viewport.fBottom = 0;
608 viewport.fWidth = desc.fWidth;
609 viewport.fHeight = desc.fHeight;
610
611 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
612 if (desc.fStencilBits) {
613 GrGLStencilBuffer::Format format;
614 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
615 format.fPacked = false;
616 format.fStencilBits = desc.fStencilBits;
617 format.fTotalBits = desc.fStencilBits;
618 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
619 0,
620 desc.fWidth,
621 desc.fHeight,
622 desc.fSampleCnt,
623 format);
624 tgt->setStencilBuffer(sb);
625 sb->unref();
626 }
627 return tgt;
628}
629
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000630GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
631
632 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
633 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
634 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
635 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
636
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000637 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000638 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000639
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000640 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000641 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000642 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000643 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000644 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000645 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000646 } else {
647 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000648 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000649 }
650 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000651 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000652 }
653 // we don't know what the RB ids are without glGets and we don't care
654 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000655 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000656 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000657 if (desc.fStencilBits) {
658 GrGLStencilBuffer::Format format;
659 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
660 format.fPacked = false;
661 format.fStencilBits = desc.fStencilBits;
662 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000663 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
664 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000665 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000666 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000667 }
668
669 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000670 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000671 GrGLenum dontCare;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000672 if (!canBeTexture(desc.fConfig, &texDesc.fInternalFormat,
673 &dontCare, &dontCare)) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000674 return NULL;
675 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000676 texDesc.fWidth = desc.fWidth;
677 texDesc.fHeight = desc.fHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000678
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000679 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000680 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000681 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000682 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000683
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000684 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000685 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000686 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000687 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000688 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000689 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000690 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000691 } else {
692 GrGLIRect viewport;
693 viewport.fLeft = 0;
694 viewport.fBottom = 0;
695 viewport.fWidth = desc.fWidth;
696 viewport.fHeight = desc.fHeight;
697
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000698 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000699 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000700 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000701 }
702}
703
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000704
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000705////////////////////////////////////////////////////////////////////////////////
706
bsalomon@google.com6f379512011-11-16 20:36:03 +0000707void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
708 int left, int top, int width, int height,
709 GrPixelConfig config, const void* buffer,
710 size_t rowBytes) {
711 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000712
bsalomon@google.com6f379512011-11-16 20:36:03 +0000713 this->setSpareTextureUnit();
714 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
715 GrGLTexture::Desc desc;
716 desc.fConfig = glTex->config();
717 desc.fWidth = glTex->width();
718 desc.fHeight = glTex->height();
719 desc.fOrientation = glTex->orientation();
720 desc.fTextureID = glTex->textureID();
721 desc.fInternalFormat = glTex->internalFormat();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000722
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000723 this->uploadTexData(desc, false,
724 left, top, width, height,
725 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000726}
727
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000728namespace {
729bool adjust_pixel_ops_params(int surfaceWidth,
730 int surfaceHeight,
731 size_t bpp,
732 int* left, int* top, int* width, int* height,
733 const void** data,
734 size_t* rowBytes) {
735 if (!*rowBytes) {
736 *rowBytes = *width * bpp;
737 }
738
739 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
740 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
741
742 if (!subRect.intersect(bounds)) {
743 return false;
744 }
745 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
746 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
747
748 *left = subRect.fLeft;
749 *top = subRect.fTop;
750 *width = subRect.width();
751 *height = subRect.height();
752 return true;
753}
754}
755
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000756bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
757 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000758 int left, int top, int width, int height,
759 GrPixelConfig dataConfig,
760 const void* data,
761 size_t rowBytes) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000762
763 size_t bpp = GrBytesPerPixel(dataConfig);
764 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
765 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000766 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000767 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000768 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000769
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 // in case we need a temporary, trimmed copy of the src pixels
771 SkAutoSMalloc<128 * 128> tempStorage;
772
bsalomon@google.com6f379512011-11-16 20:36:03 +0000773 GrGLenum dontCare;
774 GrGLenum externalFormat;
775 GrGLenum externalType;
776 if (!this->canBeTexture(dataConfig, &dontCare,
777 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000778 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000779 }
780
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000781 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000782 * check whether to allocate a temporary buffer for flipping y or
783 * because our srcData has extra bytes past each row. If so, we need
784 * to trim those off here, since GL ES may not let us specify
785 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000786 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000787 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000788 bool swFlipY = false;
789 bool glFlipY = false;
790
791 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
792 if (this->glCaps().fUnpackFlipYSupport) {
793 glFlipY = true;
794 } else {
795 swFlipY = true;
796 }
797 }
798 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000799 // can't use this for flipping, only non-neg values allowed. :(
800 if (rowBytes != trimRowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000801 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
802 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com6f379512011-11-16 20:36:03 +0000803 restoreGLRowLength = true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000804 }
805 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000806 if (trimRowBytes != rowBytes || swFlipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000807 // copy the data into our new storage, skipping the trailing bytes
bsalomon@google.com6f379512011-11-16 20:36:03 +0000808 size_t trimSize = height * trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000809 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000810 if (swFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000811 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000812 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000813 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000814 for (int y = 0; y < height; y++) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000815 memcpy(dst, src, trimRowBytes);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000816 if (swFlipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000817 src -= rowBytes;
818 } else {
819 src += rowBytes;
820 }
821 dst += trimRowBytes;
822 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000823 // now point data to our copied version
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000824 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000825 }
826 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000827 if (glFlipY) {
828 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
829 }
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000830 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000831 if (isNewTexture &&
832 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000833 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000834 GrGLClearErr(this->glInterface());
835 GR_GL_CALL_NOERRCHECK(this->glInterface(),
836 TexImage2D(GR_GL_TEXTURE_2D, 0,
837 desc.fInternalFormat,
838 desc.fWidth, desc.fHeight, 0,
839 externalFormat, externalType, data));
840 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
841 return false;
842 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000843 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000844 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000845 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000847 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, left, top, width, height,
848 externalFormat, externalType, data));
849 }
850
851 if (restoreGLRowLength) {
852 GrAssert(this->glCaps().fUnpackRowLengthSupport);
853 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000854 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000855 if (glFlipY) {
856 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
857 }
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000858 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000859}
860
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000861bool GrGpuGL::createRenderTargetObjects(int width, int height,
862 GrGLuint texID,
863 GrGLRenderTarget::Desc* desc) {
864 desc->fMSColorRenderbufferID = 0;
865 desc->fRTFBOID = 0;
866 desc->fTexFBOID = 0;
867 desc->fOwnIDs = true;
868
869 GrGLenum status;
870 GrGLint err;
871
bsalomon@google.comab15d612011-08-09 12:57:56 +0000872 GrGLenum msColorFormat = 0; // suppress warning
873
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000874 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000875 if (!desc->fTexFBOID) {
876 goto FAILED;
877 }
878
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000879
880 // If we are using multisampling we will create two FBOS. We render
881 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000882 if (desc->fSampleCnt > 0) {
883 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
884 goto FAILED;
885 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000886 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
887 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000888 if (!desc->fRTFBOID ||
889 !desc->fMSColorRenderbufferID ||
890 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
891 goto FAILED;
892 }
893 } else {
894 desc->fRTFBOID = desc->fTexFBOID;
895 }
896
897 if (desc->fRTFBOID != desc->fTexFBOID) {
898 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000899 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000900 desc->fMSColorRenderbufferID));
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000901 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000902 GR_GL_CALL_NOERRCHECK(this->glInterface(),
903 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
904 desc->fSampleCnt,
905 msColorFormat,
906 width, height));
907 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 if (err != GR_GL_NO_ERROR) {
909 goto FAILED;
910 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000911 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
912 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913 GR_GL_COLOR_ATTACHMENT0,
914 GR_GL_RENDERBUFFER,
915 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000916 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
918 goto FAILED;
919 }
920 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000921 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000922
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000923 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
924 GR_GL_COLOR_ATTACHMENT0,
925 GR_GL_TEXTURE_2D,
926 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000927 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
929 goto FAILED;
930 }
931
932 return true;
933
934FAILED:
935 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000937 }
938 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000939 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940 }
941 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943 }
944 return false;
945}
946
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000947// good to set a break-point here to know when createTexture fails
948static GrTexture* return_null_texture() {
949// GrAssert(!"null texture");
950 return NULL;
951}
952
953#if GR_DEBUG
954static size_t as_size_t(int x) {
955 return x;
956}
957#endif
958
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000959GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000960 const void* srcData,
961 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000962
963#if GR_COLLECT_STATS
964 ++fStats.fTextureCreateCnt;
965#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000966
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000967 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000968 GrGLRenderTarget::Desc glRTDesc;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000969 GrGLenum externalFormat;
970 GrGLenum externalType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000971
bsalomon@google.com99621082011-11-15 16:47:16 +0000972 glTexDesc.fWidth = desc.fWidth;
973 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000974 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000975 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000976
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000977 glRTDesc.fMSColorRenderbufferID = 0;
978 glRTDesc.fRTFBOID = 0;
979 glRTDesc.fTexFBOID = 0;
980 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000981 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000982
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000983 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000984 if (!canBeTexture(desc.fConfig,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000985 &glTexDesc.fInternalFormat,
986 &externalFormat,
987 &externalType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000988 return return_null_texture();
989 }
990
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000991 const Caps& caps = this->getCaps();
992
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000993 // We keep GrRenderTargets in GL's normal orientation so that they
994 // can be drawn to by the outside world without the client having
995 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000996 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000997 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000998
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000999 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1000 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1001 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1002 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001003 GrPrintf("AA RT requested but not supported on this platform.");
1004 }
1005
reed@google.comac10a2d2010-12-22 21:39:39 +00001006 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001007 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1008 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001009 return return_null_texture();
1010 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001011 }
1012
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001013 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001014 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001015 return return_null_texture();
1016 }
1017
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001018 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001019 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001020
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001021 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1022 // drivers have a bug where an FBO won't be complete if it includes a
1023 // texture that is not mipmap complete (considering the filter in use).
1024 GrGLTexture::TexParams initialTexParams;
1025 // we only set a subset here so invalidate first
1026 initialTexParams.invalidate();
1027 initialTexParams.fFilter = GR_GL_NEAREST;
1028 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1029 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001030 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1031 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001032 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001033 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1034 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001035 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001036 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1037 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001038 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001039 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1040 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001041 initialTexParams.fWrapT));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001042 if (NULL == srcData) {
1043 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, glTexDesc.fInternalFormat,
1044 glTexDesc.fWidth, glTexDesc.fHeight, 0,
1045 externalFormat, externalType, NULL));
1046 } else {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001047 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1048 glTexDesc.fWidth, glTexDesc.fHeight,
1049 desc.fConfig, srcData, rowBytes)) {
1050 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1051 return return_null_texture();
1052 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001053 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001054
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001055 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 if (renderTarget) {
1057#if GR_COLLECT_STATS
1058 ++fStats.fRenderTargetCreateCnt;
1059#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001060 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1061 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 glTexDesc.fTextureID,
1063 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001064 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 return return_null_texture();
1066 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001067 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001068 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001069 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001071 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001072#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001073 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1074 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001075#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001076 return tex;
1077}
1078
1079namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001080void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1081 GrGLuint rb,
1082 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001083 // we shouldn't ever know one size and not the other
1084 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1085 (kUnknownBitCount == format->fTotalBits));
1086 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001087 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1089 (GrGLint*)&format->fStencilBits);
1090 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001091 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001092 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1093 (GrGLint*)&format->fTotalBits);
1094 format->fTotalBits += format->fStencilBits;
1095 } else {
1096 format->fTotalBits = format->fStencilBits;
1097 }
1098 }
1099}
1100}
1101
1102bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1103 int width, int height) {
1104
1105 // All internally created RTs are also textures. We don't create
1106 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1107 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001108 GrAssert(width >= rt->width());
1109 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110
1111 int samples = rt->numSamples();
1112 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 if (!sbID) {
1115 return false;
1116 }
1117
1118 GrGLStencilBuffer* sb = NULL;
1119
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001120 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123 // we start with the last stencil format that succeeded in hopes
1124 // that we won't go through this loop more than once after the
1125 // first (painful) stencil creation.
1126 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001127 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001128 GrGLClearErr(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001129 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 // version on a GL that doesn't have an MSAA extension.
1131 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001132 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1133 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 GR_GL_RENDERBUFFER,
1135 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001136 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 width,
1138 height));
1139 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001140 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1141 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001142 sFmt.fInternalFormat,
1143 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 }
1145
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 if (err == GR_GL_NO_ERROR) {
1148 // After sized formats we attempt an unsized format and take whatever
1149 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001150 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001151 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001152 sb = new GrGLStencilBuffer(this, sbID, width, height,
1153 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1155 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001156 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 return true;
1159 }
1160 sb->abandon(); // otherwise we lose sbID
1161 sb->unref();
1162 }
1163 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001165 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166}
1167
1168bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1169 GrRenderTarget* rt) {
1170 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1171
1172 GrGLuint fbo = glrt->renderFBOID();
1173
1174 if (NULL == sb) {
1175 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001176 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 GR_GL_STENCIL_ATTACHMENT,
1178 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001179 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 GR_GL_DEPTH_ATTACHMENT,
1181 GR_GL_RENDERBUFFER, 0));
1182#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001183 GrGLenum status;
1184 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1186#endif
1187 }
1188 return true;
1189 } else {
1190 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1191 GrGLuint rb = glsb->renderbufferID();
1192
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001193 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1195 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 GR_GL_STENCIL_ATTACHMENT,
1197 GR_GL_RENDERBUFFER, rb));
1198 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 GR_GL_DEPTH_ATTACHMENT,
1201 GR_GL_RENDERBUFFER, rb));
1202 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001203 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 GR_GL_DEPTH_ATTACHMENT,
1205 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001206 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001208 GrGLenum status;
1209 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001211 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 GR_GL_STENCIL_ATTACHMENT,
1213 GR_GL_RENDERBUFFER, 0));
1214 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001215 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 GR_GL_DEPTH_ATTACHMENT,
1217 GR_GL_RENDERBUFFER, 0));
1218 }
1219 return false;
1220 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 return true;
1222 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001223 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001224}
1225
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001226////////////////////////////////////////////////////////////////////////////////
1227
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001228GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001229 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001230 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001233 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001236 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1237 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1238 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001239 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 // deleting bound buffer does implicit bind to 0
1242 fHWGeometryState.fVertexBuffer = NULL;
1243 return NULL;
1244 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001245 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 size, dynamic);
1247 fHWGeometryState.fVertexBuffer = vertexBuffer;
1248 return vertexBuffer;
1249 }
1250 return NULL;
1251}
1252
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001253GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001254 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001255 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001257 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1258 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001259 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001260 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1261 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1262 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001263 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001264 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 // deleting bound buffer does implicit bind to 0
1266 fHWGeometryState.fIndexBuffer = NULL;
1267 return NULL;
1268 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001269 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001270 size, dynamic);
1271 fHWGeometryState.fIndexBuffer = indexBuffer;
1272 return indexBuffer;
1273 }
1274 return NULL;
1275}
1276
reed@google.comac10a2d2010-12-22 21:39:39 +00001277void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001278 GrAssert(this->getRenderTarget() != NULL);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001279 const GrGLIRect& vp =
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001280 ((GrGLRenderTarget*)this->getRenderTarget())->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001281
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001282 GrGLIRect scissor;
1283 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001284 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001285 rect->width(), rect->height());
1286 if (scissor.contains(vp)) {
1287 rect = NULL;
1288 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 }
1290
1291 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001292 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 fHWBounds.fScissorRect = scissor;
1295 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001297 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 fHWBounds.fScissorEnabled = true;
1299 }
1300 } else {
1301 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001302 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 fHWBounds.fScissorEnabled = false;
1304 }
1305 }
1306}
1307
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001308void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001309 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001310 // parent class should never let us get here with no RT
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001311 GrAssert(rt != NULL);
1312
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001313
bsalomon@google.com74b98712011-11-11 19:46:16 +00001314 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001315 if (NULL != rect) {
1316 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001317 clippedRect = *rect;
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001318 GrIRect rtRect = SkIRect::MakeWH(rt->width(),
1319 rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001320 if (clippedRect.intersect(rtRect)) {
1321 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001322 } else {
1323 return;
1324 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001326 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001327 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001328
1329 GrGLfloat r, g, b, a;
1330 static const GrGLfloat scale255 = 1.f / 255.f;
1331 a = GrColorUnpackA(color) * scale255;
1332 GrGLfloat scaleRGB = scale255;
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001333 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001334 scaleRGB *= a;
1335 }
1336 r = GrColorUnpackR(color) * scaleRGB;
1337 g = GrColorUnpackG(color) * scaleRGB;
1338 b = GrColorUnpackB(color) * scaleRGB;
1339
1340 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001341 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001342 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001343 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001344}
1345
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001346void GrGpuGL::clearStencil() {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001347 if (NULL == this->getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001348 return;
1349 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001350
1351 this->flushRenderTarget(&GrIRect::EmptyIRect());
1352
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001354 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001355 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001357 GL_CALL(StencilMask(0xffffffff));
1358 GL_CALL(ClearStencil(0));
1359 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001360 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001361}
1362
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001363void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001364 GrRenderTarget* rt = this->getRenderTarget();
1365 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001366
1367 // this should only be called internally when we know we have a
1368 // stencil buffer.
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001369 GrAssert(NULL != rt->getStencilBuffer());
1370 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001371#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001373 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001374#else
1375 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001376 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001377 // turned into draws. Our contract on GrDrawTarget says that
1378 // changing the clip between stencil passes may or may not
1379 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001380 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001381#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001382 GrGLint value;
1383 if (insideClip) {
1384 value = (1 << (stencilBitCount - 1));
1385 } else {
1386 value = 0;
1387 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001388 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001389 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001390 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001391 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001392 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001393 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001394}
1395
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001396void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001397 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001398}
1399
bsalomon@google.comc4364992011-11-07 15:54:49 +00001400bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1401 int left, int top,
1402 int width, int height,
1403 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001404 size_t rowBytes) const {
1405 // if GL can do the flip then we'll never pay for it.
1406 if (this->glCaps().fPackFlipYSupport) {
1407 return false;
1408 }
1409
1410 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001411 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001412 if (this->glCaps().fPackRowLengthSupport) {
1413 return true;
1414 }
1415 // If we have to do memcpys to handle rowBytes then y-flip is free
1416 // Note the rowBytes might be tight to the passed in data, but if data
1417 // gets clipped in x to the target the rowBytes will no longer be tight.
1418 if (left >= 0 && (left + width) < renderTarget->width()) {
1419 return 0 == rowBytes ||
1420 GrBytesPerPixel(config) * width == rowBytes;
1421 } else {
1422 return false;
1423 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001424}
1425
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001426bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001427 int left, int top,
1428 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001429 GrPixelConfig config,
1430 void* buffer,
1431 size_t rowBytes,
1432 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001433 GrGLenum internalFormat; // we don't use this for glReadPixels
1434 GrGLenum format;
1435 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001436 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1437 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001438 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001439 size_t bpp = GrBytesPerPixel(config);
1440 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1441 &left, &top, &width, &height,
1442 const_cast<const void**>(&buffer),
1443 &rowBytes)) {
1444 return false;
1445 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001446
bsalomon@google.comc6980972011-11-02 19:57:21 +00001447 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001448 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001449 GrDrawState::AutoRenderTargetRestore arr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001450 switch (tgt->getResolveType()) {
1451 case GrGLRenderTarget::kCantResolve_ResolveType:
1452 return false;
1453 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001454 arr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001455 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001456 break;
1457 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001458 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001459 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001460 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1461 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001462 break;
1463 default:
1464 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 }
1466
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001467 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001468
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001469 // the read rect is viewport-relative
1470 GrGLIRect readRect;
1471 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001472
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001473 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001474 if (0 == rowBytes) {
1475 rowBytes = tightRowBytes;
1476 }
1477 size_t readDstRowBytes = tightRowBytes;
1478 void* readDst = buffer;
1479
1480 // determine if GL can read using the passed rowBytes or if we need
1481 // a scratch buffer.
1482 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1483 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001484 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001485 GrAssert(!(rowBytes % sizeof(GrColor)));
1486 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1487 readDstRowBytes = rowBytes;
1488 } else {
1489 scratch.reset(tightRowBytes * height);
1490 readDst = scratch.get();
1491 }
1492 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001493 if (!invertY && this->glCaps().fPackFlipYSupport) {
1494 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1495 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001496 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1497 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498 format, type, readDst));
1499 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001500 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001501 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1502 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001503 if (!invertY && this->glCaps().fPackFlipYSupport) {
1504 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1505 invertY = true;
1506 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001507
1508 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001509 // API presents top-to-bottom. We must preserve the padding contents. Note
1510 // that the above readPixels did not overwrite the padding.
1511 if (readDst == buffer) {
1512 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001513 if (!invertY) {
1514 scratch.reset(tightRowBytes);
1515 void* tmpRow = scratch.get();
1516 // flip y in-place by rows
1517 const int halfY = height >> 1;
1518 char* top = reinterpret_cast<char*>(buffer);
1519 char* bottom = top + (height - 1) * rowBytes;
1520 for (int y = 0; y < halfY; y++) {
1521 memcpy(tmpRow, top, tightRowBytes);
1522 memcpy(top, bottom, tightRowBytes);
1523 memcpy(bottom, tmpRow, tightRowBytes);
1524 top += rowBytes;
1525 bottom -= rowBytes;
1526 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001527 }
1528 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001529 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001530 // copy from readDst to buffer while flipping y
1531 const int halfY = height >> 1;
1532 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001533 char* dst = reinterpret_cast<char*>(buffer);
1534 if (!invertY) {
1535 dst += (height-1) * rowBytes;
1536 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537 for (int y = 0; y < height; y++) {
1538 memcpy(dst, src, tightRowBytes);
1539 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001540 if (invertY) {
1541 dst += rowBytes;
1542 } else {
1543 dst -= rowBytes;
1544 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001545 }
1546 }
1547 return true;
1548}
1549
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001550void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001551
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001552 GrAssert(this->getRenderTarget() != NULL);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001553
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001554 GrGLRenderTarget* rt =
1555 static_cast<GrGLRenderTarget*>(this->getRenderTarget());
1556 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001557 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001558 #if GR_COLLECT_STATS
1559 ++fStats.fRenderTargetChngCnt;
1560 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001561 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001562 GrGLenum status;
1563 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001564 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001565 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001566 }
1567 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001568 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001569 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001570 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001571 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001572 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001573 fHWBounds.fViewportRect = vp;
1574 }
1575 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001576 if (NULL == bound || !bound->isEmpty()) {
1577 rt->flagAsNeedingResolve(bound);
1578 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001579}
1580
twiz@google.com0f31ca72011-03-18 17:38:11 +00001581GrGLenum gPrimitiveType2GLMode[] = {
1582 GR_GL_TRIANGLES,
1583 GR_GL_TRIANGLE_STRIP,
1584 GR_GL_TRIANGLE_FAN,
1585 GR_GL_POINTS,
1586 GR_GL_LINES,
1587 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001588};
1589
bsalomon@google.comd302f142011-03-03 13:54:13 +00001590#define SWAP_PER_DRAW 0
1591
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001592#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001593 #if GR_MAC_BUILD
1594 #include <AGL/agl.h>
1595 #elif GR_WIN32_BUILD
1596 void SwapBuf() {
1597 DWORD procID = GetCurrentProcessId();
1598 HWND hwnd = GetTopWindow(GetDesktopWindow());
1599 while(hwnd) {
1600 DWORD wndProcID = 0;
1601 GetWindowThreadProcessId(hwnd, &wndProcID);
1602 if(wndProcID == procID) {
1603 SwapBuffers(GetDC(hwnd));
1604 }
1605 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1606 }
1607 }
1608 #endif
1609#endif
1610
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001611void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1612 uint32_t startVertex,
1613 uint32_t startIndex,
1614 uint32_t vertexCount,
1615 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001616 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1617
twiz@google.com0f31ca72011-03-18 17:38:11 +00001618 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001619
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001620 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1621 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1622
1623 // our setupGeometry better have adjusted this to zero since
1624 // DrawElements always draws from the begining of the arrays for idx 0.
1625 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001626
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001627 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1628 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001629#if SWAP_PER_DRAW
1630 glFlush();
1631 #if GR_MAC_BUILD
1632 aglSwapBuffers(aglGetCurrentContext());
1633 int set_a_break_pt_here = 9;
1634 aglSwapBuffers(aglGetCurrentContext());
1635 #elif GR_WIN32_BUILD
1636 SwapBuf();
1637 int set_a_break_pt_here = 9;
1638 SwapBuf();
1639 #endif
1640#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001641}
1642
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001643void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1644 uint32_t startVertex,
1645 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1647
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001648 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1649
1650 // our setupGeometry better have adjusted this to zero.
1651 // DrawElements doesn't take an offset so we always adjus the startVertex.
1652 GrAssert(0 == startVertex);
1653
1654 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1655 // account for startVertex in the DrawElements case. So we always
1656 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001657 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001658#if SWAP_PER_DRAW
1659 glFlush();
1660 #if GR_MAC_BUILD
1661 aglSwapBuffers(aglGetCurrentContext());
1662 int set_a_break_pt_here = 9;
1663 aglSwapBuffers(aglGetCurrentContext());
1664 #elif GR_WIN32_BUILD
1665 SwapBuf();
1666 int set_a_break_pt_here = 9;
1667 SwapBuf();
1668 #endif
1669#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001670}
1671
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001672void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001673
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001674 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001675 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001676 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001677 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1678 rt->renderFBOID()));
1679 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1680 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001681 #if GR_COLLECT_STATS
1682 ++fStats.fRenderTargetChngCnt;
1683 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001684 // make sure we go through flushRenderTarget() since we've modified
1685 // the bound DRAW FBO ID.
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001686 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001687 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001688 const GrIRect dirtyRect = rt->getResolveRect();
1689 GrGLIRect r;
1690 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1691 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001692
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001693 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001694 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001695 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1696 GL_CALL(Scissor(r.fLeft, r.fBottom,
1697 r.fWidth, r.fHeight));
1698 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001699 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001700 fHWBounds.fScissorEnabled = true;
1701 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001702 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001703 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001704 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1705 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001706 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001707 int right = r.fLeft + r.fWidth;
1708 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001709 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1710 r.fLeft, r.fBottom, right, top,
1711 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001712 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001713 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 }
1715}
1716
twiz@google.com0f31ca72011-03-18 17:38:11 +00001717static const GrGLenum grToGLStencilFunc[] = {
1718 GR_GL_ALWAYS, // kAlways_StencilFunc
1719 GR_GL_NEVER, // kNever_StencilFunc
1720 GR_GL_GREATER, // kGreater_StencilFunc
1721 GR_GL_GEQUAL, // kGEqual_StencilFunc
1722 GR_GL_LESS, // kLess_StencilFunc
1723 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1724 GR_GL_EQUAL, // kEqual_StencilFunc,
1725 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001726};
1727GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1728GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1729GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1730GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1731GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1732GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1733GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1734GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1735GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1736
twiz@google.com0f31ca72011-03-18 17:38:11 +00001737static const GrGLenum grToGLStencilOp[] = {
1738 GR_GL_KEEP, // kKeep_StencilOp
1739 GR_GL_REPLACE, // kReplace_StencilOp
1740 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1741 GR_GL_INCR, // kIncClamp_StencilOp
1742 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1743 GR_GL_DECR, // kDecClamp_StencilOp
1744 GR_GL_ZERO, // kZero_StencilOp
1745 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001746};
1747GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1748GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1749GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1750GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1751GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1752GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1753GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1754GR_STATIC_ASSERT(6 == kZero_StencilOp);
1755GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1756
reed@google.comac10a2d2010-12-22 21:39:39 +00001757void GrGpuGL::flushStencil() {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001758 const GrDrawState& drawState = this->getDrawState();
1759 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001760
1761 // use stencil for clipping if clipping is enabled and the clip
1762 // has been written into the stencil.
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001763 bool stencilClip = fClipInStencil && drawState.isClipState();
1764
1765 bool modifyingStencilClip = drawState.isStateFlagEnabled(
1766 kModifyStencilClip_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001767 bool stencilChange = fHWStencilClip != stencilClip ||
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001768 fHWDrawState.getStencil() != *settings ||
1769 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1770 modifyingStencilClip);
reed@google.comac10a2d2010-12-22 21:39:39 +00001771
1772 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001773
bsalomon@google.comd302f142011-03-03 13:54:13 +00001774 // we can't simultaneously perform stencil-clipping and modify the stencil clip
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001775 GrAssert(!stencilClip || !drawState.isStateFlagEnabled(kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001776
bsalomon@google.comd302f142011-03-03 13:54:13 +00001777 if (settings->isDisabled()) {
1778 if (stencilClip) {
1779 settings = &gClipStencilSettings;
1780 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001781 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001782
1783 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001784 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001785 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001786 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001787 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001788 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001789 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1790 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1791 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1792 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1793 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1794 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1795 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1796 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1797 }
1798 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001799 int stencilBits = 0;
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001800 GrStencilBuffer* stencilBuffer =
1801 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001802 if (NULL != stencilBuffer) {
1803 stencilBits = stencilBuffer->bits();
1804 }
1805 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001806 GrAssert(stencilBits ||
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001807 (GrStencilSettings::gDisabled == *settings));
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001808
1809 GrGLuint clipStencilMask = 0;
1810 GrGLuint userStencilMask = ~0;
1811 if (stencilBits > 0) {
1812 clipStencilMask = 1 << (stencilBits - 1);
1813 userStencilMask = clipStencilMask - 1;
1814 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001815
1816 unsigned int frontRef = settings->fFrontFuncRef;
1817 unsigned int frontMask = settings->fFrontFuncMask;
1818 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001819 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001820
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001821 if (modifyingStencilClip) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822
1823 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1824 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1825 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001826 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
1827 stencilClip, settings->fFrontFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828
1829 ConvertStencilFuncAndMask(settings->fFrontFunc,
1830 stencilClip,
1831 clipStencilMask,
1832 userStencilMask,
1833 &frontRef,
1834 &frontMask);
1835 frontWriteMask &= userStencilMask;
1836 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001837 GrAssert((size_t)
1838 settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1839 GrAssert((size_t)
1840 settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
1841 GrAssert((size_t)
1842 settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1843 GrAssert((size_t)
1844 settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001845 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001846 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001847
1848 unsigned int backRef = settings->fBackFuncRef;
1849 unsigned int backMask = settings->fBackFuncMask;
1850 unsigned int backWriteMask = settings->fBackWriteMask;
1851
1852
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001853 if (modifyingStencilClip) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001854 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1855 backFunc = grToGLStencilFunc[settings->fBackFunc];
1856 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001857 backFunc = grToGLStencilFunc[ConvertStencilFunc(
1858 stencilClip, settings->fBackFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859 ConvertStencilFuncAndMask(settings->fBackFunc,
1860 stencilClip,
1861 clipStencilMask,
1862 userStencilMask,
1863 &backRef,
1864 &backMask);
1865 backWriteMask &= userStencilMask;
1866 }
1867
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001868 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1869 frontRef, frontMask));
1870 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1871 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1872 backRef, backMask));
1873 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1874 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1875 grToGLStencilOp[settings->fFrontFailOp],
1876 grToGLStencilOp[settings->fFrontPassOp],
1877 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001878
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001879 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1880 grToGLStencilOp[settings->fBackFailOp],
1881 grToGLStencilOp[settings->fBackPassOp],
1882 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001883 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001884 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1885 GL_CALL(StencilMask(frontWriteMask));
1886 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001887 grToGLStencilOp[settings->fFrontPassOp],
1888 grToGLStencilOp[settings->fFrontPassOp]));
1889 }
1890 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001891 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001892 fHWStencilClip = stencilClip;
1893 }
1894}
1895
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001896void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001897 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001898 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1899 // smooth lines.
1900
1901 // we prefer smooth lines over multisampled lines
1902 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001903
1904 GrRenderTarget* rt = this->getRenderTarget();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001905 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001906 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001907 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001908 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001909 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001910 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001911 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001912 fHWAAState.fSmoothLineEnabled = false;
1913 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001914 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001915 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001916 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001917 fHWAAState.fMSAAEnabled = false;
1918 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001919 } else if (rt->isMultisampled() &&
1920 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001921 fHWAAState.fMSAAEnabled) {
1922 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001923 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001924 fHWAAState.fMSAAEnabled = false;
1925 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001926 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001927 fHWAAState.fMSAAEnabled = true;
1928 }
1929 }
1930 }
1931}
1932
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001933void GrGpuGL::flushBlend(GrPrimitiveType type,
1934 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001935 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001936 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001937 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001938 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001939 fHWBlendDisabled = false;
1940 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001941 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1942 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001943 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1944 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001945 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001946 }
1947 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001948 // any optimization to disable blending should
1949 // have already been applied and tweaked the coeffs
1950 // to (1, 0).
1951 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1952 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001953 if (fHWBlendDisabled != blendOff) {
1954 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001955 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001956 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001957 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001958 }
1959 fHWBlendDisabled = blendOff;
1960 }
1961 if (!blendOff) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001962 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
1963 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001964 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1965 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001966 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001967 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001968 GrColor blendConstant = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001969 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1970 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001971 fHWDrawState.getBlendConstant() != blendConstant) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001972
1973 float c[] = {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001974 GrColorUnpackR(blendConstant) / 255.f,
1975 GrColorUnpackG(blendConstant) / 255.f,
1976 GrColorUnpackB(blendConstant) / 255.f,
1977 GrColorUnpackA(blendConstant) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001978 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00001980 fHWDrawState.setBlendConstant(blendConstant);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001981 }
1982 }
1983 }
1984}
1985
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001986namespace {
1987
1988unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001989 switch (filter) {
1990 case GrSamplerState::kBilinear_Filter:
1991 case GrSamplerState::k4x4Downsample_Filter:
1992 return GR_GL_LINEAR;
1993 case GrSamplerState::kNearest_Filter:
1994 case GrSamplerState::kConvolution_Filter:
1995 return GR_GL_NEAREST;
1996 default:
1997 GrAssert(!"Unknown filter type");
1998 return GR_GL_LINEAR;
1999 }
2000}
2001
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002002const GrGLenum* get_swizzle(GrPixelConfig config,
2003 const GrSamplerState& sampler) {
2004 if (GrPixelConfigIsAlphaOnly(config)) {
2005 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2006 GR_GL_ALPHA, GR_GL_ALPHA };
2007 return gAlphaSmear;
2008 } else if (sampler.swapsRAndB()) {
2009 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2010 GR_GL_RED, GR_GL_ALPHA };
2011 return gRedBlueSwap;
2012 } else {
2013 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2014 GR_GL_BLUE, GR_GL_ALPHA };
2015 return gStraight;
2016 }
2017}
2018
2019void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2020 // should add texparameteri to interface to make 1 instead of 4 calls here
2021 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2022 GR_GL_TEXTURE_SWIZZLE_R,
2023 swizzle[0]));
2024 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2025 GR_GL_TEXTURE_SWIZZLE_G,
2026 swizzle[1]));
2027 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2028 GR_GL_TEXTURE_SWIZZLE_B,
2029 swizzle[2]));
2030 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2031 GR_GL_TEXTURE_SWIZZLE_A,
2032 swizzle[3]));
2033}
2034}
2035
bsalomon@google.comffca4002011-02-22 20:34:01 +00002036bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002037
2038 // GrGpu::setupClipAndFlushState should have already checked this
2039 // and bailed if not true.
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002040 GrAssert(this->getRenderTarget() != NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002041
tomhudson@google.com93813632011-10-27 20:21:16 +00002042 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002043 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002044 if (this->isStageEnabled(s)) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002045 GrGLTexture* nextTexture =
2046 static_cast<GrGLTexture*>(this->drawState()->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002047
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002048 // true for now, but maybe not with GrEffect.
2049 GrAssert(NULL != nextTexture);
2050 // if we created a rt/tex and rendered to it without using a
2051 // texture and now we're texuring from the rt it will still be
2052 // the last bound texture, but it needs resolving. So keep this
2053 // out of the "last != next" check.
2054 GrGLRenderTarget* texRT =
2055 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2056 if (NULL != texRT) {
2057 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002058 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002059
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002060 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002061 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002062 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002063 #if GR_COLLECT_STATS
2064 ++fStats.fTextureChngCnt;
2065 #endif
2066 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002067 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002068 // The texture matrix has to compensate for texture width/height
2069 // and NPOT-embedded-in-POT
2070 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002071 }
2072
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002073 const GrSamplerState& sampler = this->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002074 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002075 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002076 nextTexture->getCachedTexParams(&timestamp);
2077 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002078 GrGLTexture::TexParams newTexParams;
2079
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002080 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002081
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002082 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002083 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2084 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002085 memcpy(newTexParams.fSwizzleRGBA,
2086 get_swizzle(nextTexture->config(), sampler),
2087 sizeof(newTexParams.fSwizzleRGBA));
2088 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002089 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002090 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002091 GR_GL_TEXTURE_MAG_FILTER,
2092 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002093 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002094 GR_GL_TEXTURE_MIN_FILTER,
2095 newTexParams.fFilter));
2096 }
2097 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2098 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002099 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002100 GR_GL_TEXTURE_WRAP_S,
2101 newTexParams.fWrapS));
2102 }
2103 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2104 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002105 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002106 GR_GL_TEXTURE_WRAP_T,
2107 newTexParams.fWrapT));
2108 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002109 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002110 (setAll ||
2111 memcmp(newTexParams.fSwizzleRGBA,
2112 oldTexParams.fSwizzleRGBA,
2113 sizeof(newTexParams.fSwizzleRGBA)))) {
2114 setTextureUnit(s);
2115 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2116 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002117 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002118 nextTexture->setCachedTexParams(newTexParams,
2119 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002120 }
2121 }
2122
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002123 GrIRect* rect = NULL;
2124 GrIRect clipBounds;
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002125 if (this->getDrawState().isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002126 fClip.hasConservativeBounds()) {
2127 fClip.getConservativeBounds().roundOut(&clipBounds);
2128 rect = &clipBounds;
2129 }
2130 this->flushRenderTarget(rect);
2131 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002132
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002133 if (this->getDrawState().isDitherState() !=
2134 fHWDrawState.isDitherState()) {
2135 if (this->getDrawState().isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002136 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002137 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002138 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002139 }
2140 }
2141
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002142 if (this->getDrawState().isColorWriteDisabled() !=
2143 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002144 GrGLenum mask;
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002145 if (this->getDrawState().isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002146 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002147 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002148 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002149 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002150 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002151 }
2152
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002153 if (fHWDrawState.getDrawFace() != this->getDrawFace()) {
2154 switch (this->getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002155 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002156 GL_CALL(Enable(GR_GL_CULL_FACE));
2157 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002158 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002159 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002160 GL_CALL(Enable(GR_GL_CULL_FACE));
2161 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002162 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002163 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002164 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002165 break;
2166 default:
2167 GrCrash("Unknown draw face.");
2168 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002169 fHWDrawState.setDrawFace(this->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002170 }
2171
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002172#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002173 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002174 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002175 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002176 NULL == this->getRenderTarget() ||
2177 NULL == this->getTexture(s) ||
2178 this->getTexture(s)->asRenderTarget() !=
2179 this->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002180 }
2181#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002182
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002183 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002184
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002185 // the flushStencil() function called above detecs a change in the
2186 // kModifyStencilClip_StateBit flag. Therefore this copy must happen after
2187 // flushStencil()
2188 fHWDrawState.copyStateFlags(this->getDrawState());
2189
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002190 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002191}
2192
2193void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002194 if (fHWGeometryState.fVertexBuffer != buffer) {
2195 fHWGeometryState.fArrayPtrsDirty = true;
2196 fHWGeometryState.fVertexBuffer = buffer;
2197 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002198}
2199
2200void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002201 if (fHWGeometryState.fVertexBuffer == buffer) {
2202 // deleting bound buffer does implied bind to 0
2203 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002204 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002205 }
2206}
2207
2208void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002209 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002210}
2211
2212void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002213 if (fHWGeometryState.fIndexBuffer == buffer) {
2214 // deleting bound buffer does implied bind to 0
2215 fHWGeometryState.fIndexBuffer = NULL;
2216 }
2217}
2218
reed@google.comac10a2d2010-12-22 21:39:39 +00002219void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2220 GrAssert(NULL != renderTarget);
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002221 if (this->getRenderTarget() == renderTarget) {
2222 this->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002223 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002224 if (this->getRenderTarget() == renderTarget) {
2225 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002226 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002227}
2228
2229void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002230 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002231 if (this->getTexture(s) == texture) {
2232 this->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002233 }
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002234 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002235 // deleting bound texture does implied bind to 0
bsalomon@google.com0fec61d2011-12-08 15:53:53 +00002236 this->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002237 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002238 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002239}
2240
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002241bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002242 GrGLenum* internalFormat,
bsalomon@google.com6f379512011-11-16 20:36:03 +00002243 GrGLenum* externalFormat,
2244 GrGLenum* externalType) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002245 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002246 case kRGBA_8888_PM_GrPixelConfig:
2247 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002248 *externalFormat = GR_GL_RGBA;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002249 *internalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002250 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002251 break;
2252 case kBGRA_8888_PM_GrPixelConfig:
2253 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002254 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002255 return false;
2256 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002257 *externalFormat = GR_GL_BGRA;
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002258 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002259 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002260 } else {
2261 *internalFormat = GR_GL_RGBA;
2262 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002263 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002264 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002265 case kRGB_565_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002266 *externalFormat = GR_GL_RGB;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002267 *internalFormat = GR_GL_RGB;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002268 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002269 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002270 case kRGBA_4444_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002271 *externalFormat = GR_GL_RGBA;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002272 *internalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002273 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002274 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002275 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002276 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00002277 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002278 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002279 *externalType = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 } else {
2281 return false;
2282 }
2283 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002284 case kAlpha_8_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002285 *externalFormat = GR_GL_ALPHA;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002286 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002287 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 break;
2289 default:
2290 return false;
2291 }
2292 return true;
2293}
2294
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002295void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002296 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002297 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002298 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002299 fActiveTextureUnitIdx = unit;
2300 }
2301}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002302
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002303void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002304 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002305 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002306 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2307 }
2308}
2309
reed@google.comac10a2d2010-12-22 21:39:39 +00002310/* On ES the internalFormat and format must match for TexImage and we use
2311 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2312 decide the internalFormat. However, on ES internalFormat for
2313 RenderBufferStorage* has to be a specific format (not a base format like
2314 GL_RGBA).
2315 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002316bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002317 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002318 // The ES story for BGRA and RenderbufferStorage appears murky. It
2319 // takes an internal format as a parameter. The OES FBO extension and
2320 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2321 // BGRA extensions adds BGRA as both an internal and external format
2322 // and the other only as an external format (like desktop GL). OES
2323 // restricts RenderbufferStorage's format to a *sized* internal format.
2324 // There is no sized BGRA internal format.
2325 // So if the texture has internal format BGRA we just hope that the
2326 // resolve blit can do RGBA->BGRA internal format conversion.
2327 case kRGBA_8888_PM_GrPixelConfig:
2328 case kRGBA_8888_UPM_GrPixelConfig:
2329 case kBGRA_8888_PM_GrPixelConfig:
2330 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002331 if (fGLCaps.fRGBA8RenderbufferSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002332 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2333 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002334 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002335 return true;
2336 } else {
2337 return false;
2338 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002339 case kRGB_565_GrPixelConfig:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002340 // ES2 supports 565, but desktop GL does not.
bsalomon@google.comc4364992011-11-07 15:54:49 +00002341 if (kDesktop_GrGLBinding != this->glBinding()) {
2342 *format = GR_GL_RGB565;
2343 return true;
2344 } else {
2345 return false;
2346 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002347 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002348 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002349 return true;
2350 default:
2351 return false;
2352 }
2353}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002354
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002355void GrGpuGL::resetDirtyFlags() {
2356 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2357}
2358
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002359void GrGpuGL::setBuffers(bool indexed,
2360 int* extraVertexOffset,
2361 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002362
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002363 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002364
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002365 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2366
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002367 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002368 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002369 case kBuffer_GeometrySrcType:
2370 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002371 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002372 break;
2373 case kArray_GeometrySrcType:
2374 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002375 this->finalizeReservedVertices();
2376 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2377 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002378 break;
2379 default:
2380 vbuf = NULL; // suppress warning
2381 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002382 }
2383
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002384 GrAssert(NULL != vbuf);
2385 GrAssert(!vbuf->isLocked());
2386 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002387 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002388 fHWGeometryState.fArrayPtrsDirty = true;
2389 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002390 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002391
2392 if (indexed) {
2393 GrAssert(NULL != extraIndexOffset);
2394
2395 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002396 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002397 case kBuffer_GeometrySrcType:
2398 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002399 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002400 break;
2401 case kArray_GeometrySrcType:
2402 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002403 this->finalizeReservedIndices();
2404 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2405 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002406 break;
2407 default:
2408 ibuf = NULL; // suppress warning
2409 GrCrash("Unknown geometry src type!");
2410 }
2411
2412 GrAssert(NULL != ibuf);
2413 GrAssert(!ibuf->isLocked());
2414 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002415 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002416 fHWGeometryState.fIndexBuffer = ibuf;
2417 }
2418 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002419}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002420
2421int GrGpuGL::getMaxEdges() const {
2422 // FIXME: This is a pessimistic estimate based on how many other things
2423 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002424 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2425 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002426}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002427
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002428void GrGpuGL::GLCaps::print() const {
2429 for (int i = 0; i < fStencilFormats.count(); ++i) {
2430 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2431 i,
2432 fStencilFormats[i].fStencilBits,
2433 fStencilFormats[i].fTotalBits);
2434 }
2435
2436 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2437 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2438 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2439 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2440 static const char* gMSFBOExtStr[] = {
2441 "None",
2442 "ARB",
2443 "EXT",
2444 "Apple",
2445 };
2446 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002447 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002448 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2449 }
2450 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2451 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002452 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002453 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002454 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002455 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002456 (fTextureSwizzleSupport ? "YES": "NO"));
2457 GrPrintf("Unpack Row length support: %s\n",
2458 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002459 GrPrintf("Unpack Flip Y support: %s\n",
2460 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002461 GrPrintf("Pack Row length support: %s\n",
2462 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002463 GrPrintf("Pack Flip Y support: %s\n",
2464 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002465}