blob: 2a8634f00c238bc9cbdc99b1fe317ff7129dbc5f [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.com8f9cbd62011-12-09 15:55:34 +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.com8f9cbd62011-12-09 15:55:34 +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
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000504 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000505
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000506 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.com8f9cbd62011-12-09 15:55:34 +0000509 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000510
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +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.com8f9cbd62011-12-09 15:55:34 +0000514 fHWDrawState.setTexture(s, NULL);
515 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
516 -GR_ScalarMax,
517 true);
518 fHWDrawState.sampler(s)->setMatrix(GrMatrix::InvalidMatrix());
519 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000520 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000521
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000522 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000523 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000524 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000525 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000526
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000527 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000528 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000529 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530
531 fHWGeometryState.fIndexBuffer = NULL;
532 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000533
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000534 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000536 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000537 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000538
539 // we assume these values
540 if (this->glCaps().fUnpackRowLengthSupport) {
541 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
542 }
543 if (this->glCaps().fPackRowLengthSupport) {
544 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
545 }
546 if (this->glCaps().fUnpackFlipYSupport) {
547 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
548 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000549 if (this->glCaps().fPackFlipYSupport) {
550 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
551 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000552}
553
bsalomon@google.come269f212011-11-07 13:29:52 +0000554GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000555 GrGLenum dontCare;
bsalomon@google.come269f212011-11-07 13:29:52 +0000556 GrGLTexture::Desc glTexDesc;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000557 if (!this->canBeTexture(desc.fConfig, &glTexDesc.fInternalFormat,
558 &dontCare, &dontCare)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000559 return NULL;
560 }
561
bsalomon@google.com99621082011-11-15 16:47:16 +0000562 glTexDesc.fWidth = desc.fWidth;
563 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000564 glTexDesc.fConfig = desc.fConfig;
565 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
566 glTexDesc.fOwnsID = false;
567 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
568
569 GrGLTexture* texture = NULL;
570 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
571 GrGLRenderTarget::Desc glRTDesc;
572 glRTDesc.fRTFBOID = 0;
573 glRTDesc.fTexFBOID = 0;
574 glRTDesc.fMSColorRenderbufferID = 0;
575 glRTDesc.fOwnIDs = true;
576 glRTDesc.fConfig = desc.fConfig;
577 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000578 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
579 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000580 glTexDesc.fTextureID,
581 &glRTDesc)) {
582 return NULL;
583 }
584 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
585 } else {
586 texture = new GrGLTexture(this, glTexDesc);
587 }
588 if (NULL == texture) {
589 return NULL;
590 }
591
592 this->setSpareTextureUnit();
593 return texture;
594}
595
596GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
597 GrGLRenderTarget::Desc glDesc;
598 glDesc.fConfig = desc.fConfig;
599 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
600 glDesc.fMSColorRenderbufferID = 0;
601 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
602 glDesc.fSampleCnt = desc.fSampleCnt;
603 glDesc.fOwnIDs = false;
604 GrGLIRect viewport;
605 viewport.fLeft = 0;
606 viewport.fBottom = 0;
607 viewport.fWidth = desc.fWidth;
608 viewport.fHeight = desc.fHeight;
609
610 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
611 if (desc.fStencilBits) {
612 GrGLStencilBuffer::Format format;
613 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
614 format.fPacked = false;
615 format.fStencilBits = desc.fStencilBits;
616 format.fTotalBits = desc.fStencilBits;
617 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
618 0,
619 desc.fWidth,
620 desc.fHeight,
621 desc.fSampleCnt,
622 format);
623 tgt->setStencilBuffer(sb);
624 sb->unref();
625 }
626 return tgt;
627}
628
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000629GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
630
631 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
632 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
633 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
634 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
635
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000636 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000637 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000638
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000639 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000640 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000641 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000642 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000643 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000644 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000645 } else {
646 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000647 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000648 }
649 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000650 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000651 }
652 // we don't know what the RB ids are without glGets and we don't care
653 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000654 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000655 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000656 if (desc.fStencilBits) {
657 GrGLStencilBuffer::Format format;
658 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
659 format.fPacked = false;
660 format.fStencilBits = desc.fStencilBits;
661 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000662 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
663 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000664 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000665 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000666 }
667
668 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000669 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000670 GrGLenum dontCare;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000671 if (!canBeTexture(desc.fConfig, &texDesc.fInternalFormat,
672 &dontCare, &dontCare)) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000673 return NULL;
674 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000675 texDesc.fWidth = desc.fWidth;
676 texDesc.fHeight = desc.fHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000677
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000678 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000679 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000680 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000681 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000682
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000683 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000684 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000685 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000686 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000687 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000688 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000689 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000690 } else {
691 GrGLIRect viewport;
692 viewport.fLeft = 0;
693 viewport.fBottom = 0;
694 viewport.fWidth = desc.fWidth;
695 viewport.fHeight = desc.fHeight;
696
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000697 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000698 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000699 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000700 }
701}
702
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000703
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000704////////////////////////////////////////////////////////////////////////////////
705
bsalomon@google.com6f379512011-11-16 20:36:03 +0000706void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
707 int left, int top, int width, int height,
708 GrPixelConfig config, const void* buffer,
709 size_t rowBytes) {
710 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000711
bsalomon@google.com6f379512011-11-16 20:36:03 +0000712 this->setSpareTextureUnit();
713 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
714 GrGLTexture::Desc desc;
715 desc.fConfig = glTex->config();
716 desc.fWidth = glTex->width();
717 desc.fHeight = glTex->height();
718 desc.fOrientation = glTex->orientation();
719 desc.fTextureID = glTex->textureID();
720 desc.fInternalFormat = glTex->internalFormat();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000721
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000722 this->uploadTexData(desc, false,
723 left, top, width, height,
724 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000725}
726
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000727namespace {
728bool adjust_pixel_ops_params(int surfaceWidth,
729 int surfaceHeight,
730 size_t bpp,
731 int* left, int* top, int* width, int* height,
732 const void** data,
733 size_t* rowBytes) {
734 if (!*rowBytes) {
735 *rowBytes = *width * bpp;
736 }
737
738 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
739 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
740
741 if (!subRect.intersect(bounds)) {
742 return false;
743 }
744 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
745 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
746
747 *left = subRect.fLeft;
748 *top = subRect.fTop;
749 *width = subRect.width();
750 *height = subRect.height();
751 return true;
752}
753}
754
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000755bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
756 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000757 int left, int top, int width, int height,
758 GrPixelConfig dataConfig,
759 const void* data,
760 size_t rowBytes) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000761
762 size_t bpp = GrBytesPerPixel(dataConfig);
763 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
764 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000765 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000766 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000767 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000768
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000769 // in case we need a temporary, trimmed copy of the src pixels
770 SkAutoSMalloc<128 * 128> tempStorage;
771
bsalomon@google.com6f379512011-11-16 20:36:03 +0000772 GrGLenum dontCare;
773 GrGLenum externalFormat;
774 GrGLenum externalType;
775 if (!this->canBeTexture(dataConfig, &dontCare,
776 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000777 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000778 }
779
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000780 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000781 * check whether to allocate a temporary buffer for flipping y or
782 * because our srcData has extra bytes past each row. If so, we need
783 * to trim those off here, since GL ES may not let us specify
784 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000785 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000786 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000787 bool swFlipY = false;
788 bool glFlipY = false;
789
790 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
791 if (this->glCaps().fUnpackFlipYSupport) {
792 glFlipY = true;
793 } else {
794 swFlipY = true;
795 }
796 }
797 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000798 // can't use this for flipping, only non-neg values allowed. :(
799 if (rowBytes != trimRowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000800 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
801 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com6f379512011-11-16 20:36:03 +0000802 restoreGLRowLength = true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000803 }
804 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000805 if (trimRowBytes != rowBytes || swFlipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000806 // copy the data into our new storage, skipping the trailing bytes
bsalomon@google.com6f379512011-11-16 20:36:03 +0000807 size_t trimSize = height * trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000808 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000809 if (swFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000810 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000811 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000812 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000813 for (int y = 0; y < height; y++) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000814 memcpy(dst, src, trimRowBytes);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000815 if (swFlipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000816 src -= rowBytes;
817 } else {
818 src += rowBytes;
819 }
820 dst += trimRowBytes;
821 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000822 // now point data to our copied version
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000823 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000824 }
825 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000826 if (glFlipY) {
827 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
828 }
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000829 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000830 if (isNewTexture &&
831 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000832 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000833 GrGLClearErr(this->glInterface());
834 GR_GL_CALL_NOERRCHECK(this->glInterface(),
835 TexImage2D(GR_GL_TEXTURE_2D, 0,
836 desc.fInternalFormat,
837 desc.fWidth, desc.fHeight, 0,
838 externalFormat, externalType, data));
839 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
840 return false;
841 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000842 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000843 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000844 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000845 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000846 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, left, top, width, height,
847 externalFormat, externalType, data));
848 }
849
850 if (restoreGLRowLength) {
851 GrAssert(this->glCaps().fUnpackRowLengthSupport);
852 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000853 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000854 if (glFlipY) {
855 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
856 }
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000857 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000858}
859
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000860bool GrGpuGL::createRenderTargetObjects(int width, int height,
861 GrGLuint texID,
862 GrGLRenderTarget::Desc* desc) {
863 desc->fMSColorRenderbufferID = 0;
864 desc->fRTFBOID = 0;
865 desc->fTexFBOID = 0;
866 desc->fOwnIDs = true;
867
868 GrGLenum status;
869 GrGLint err;
870
bsalomon@google.comab15d612011-08-09 12:57:56 +0000871 GrGLenum msColorFormat = 0; // suppress warning
872
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000873 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874 if (!desc->fTexFBOID) {
875 goto FAILED;
876 }
877
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000878
879 // If we are using multisampling we will create two FBOS. We render
880 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000881 if (desc->fSampleCnt > 0) {
882 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
883 goto FAILED;
884 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000885 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
886 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000887 if (!desc->fRTFBOID ||
888 !desc->fMSColorRenderbufferID ||
889 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
890 goto FAILED;
891 }
892 } else {
893 desc->fRTFBOID = desc->fTexFBOID;
894 }
895
896 if (desc->fRTFBOID != desc->fTexFBOID) {
897 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000898 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 desc->fMSColorRenderbufferID));
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000900 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000901 GR_GL_CALL_NOERRCHECK(this->glInterface(),
902 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
903 desc->fSampleCnt,
904 msColorFormat,
905 width, height));
906 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000907 if (err != GR_GL_NO_ERROR) {
908 goto FAILED;
909 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000910 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
911 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 GR_GL_COLOR_ATTACHMENT0,
913 GR_GL_RENDERBUFFER,
914 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000915 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000916 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
917 goto FAILED;
918 }
919 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000920 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
923 GR_GL_COLOR_ATTACHMENT0,
924 GR_GL_TEXTURE_2D,
925 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000926 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000927 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
928 goto FAILED;
929 }
930
931 return true;
932
933FAILED:
934 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000936 }
937 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000938 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 }
940 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000941 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942 }
943 return false;
944}
945
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000946// good to set a break-point here to know when createTexture fails
947static GrTexture* return_null_texture() {
948// GrAssert(!"null texture");
949 return NULL;
950}
951
952#if GR_DEBUG
953static size_t as_size_t(int x) {
954 return x;
955}
956#endif
957
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000958GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000959 const void* srcData,
960 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000961
962#if GR_COLLECT_STATS
963 ++fStats.fTextureCreateCnt;
964#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000965
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000966 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000967 GrGLRenderTarget::Desc glRTDesc;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000968 GrGLenum externalFormat;
969 GrGLenum externalType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000970
bsalomon@google.com99621082011-11-15 16:47:16 +0000971 glTexDesc.fWidth = desc.fWidth;
972 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000973 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000974 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000975
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000976 glRTDesc.fMSColorRenderbufferID = 0;
977 glRTDesc.fRTFBOID = 0;
978 glRTDesc.fTexFBOID = 0;
979 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000980 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000981
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000982 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000983 if (!canBeTexture(desc.fConfig,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000984 &glTexDesc.fInternalFormat,
985 &externalFormat,
986 &externalType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000987 return return_null_texture();
988 }
989
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000990 const Caps& caps = this->getCaps();
991
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000992 // We keep GrRenderTargets in GL's normal orientation so that they
993 // can be drawn to by the outside world without the client having
994 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000995 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000996 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000997
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000998 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
999 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1000 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1001 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001002 GrPrintf("AA RT requested but not supported on this platform.");
1003 }
1004
reed@google.comac10a2d2010-12-22 21:39:39 +00001005 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001006 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1007 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001008 return return_null_texture();
1009 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 }
1011
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001012 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001013 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001014 return return_null_texture();
1015 }
1016
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001017 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001018 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001019
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001020 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1021 // drivers have a bug where an FBO won't be complete if it includes a
1022 // texture that is not mipmap complete (considering the filter in use).
1023 GrGLTexture::TexParams initialTexParams;
1024 // we only set a subset here so invalidate first
1025 initialTexParams.invalidate();
1026 initialTexParams.fFilter = GR_GL_NEAREST;
1027 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1028 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001029 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1030 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001031 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001032 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1033 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001034 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001035 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1036 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001037 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001038 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1039 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001040 initialTexParams.fWrapT));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001041 if (NULL == srcData) {
1042 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, glTexDesc.fInternalFormat,
1043 glTexDesc.fWidth, glTexDesc.fHeight, 0,
1044 externalFormat, externalType, NULL));
1045 } else {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001046 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1047 glTexDesc.fWidth, glTexDesc.fHeight,
1048 desc.fConfig, srcData, rowBytes)) {
1049 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1050 return return_null_texture();
1051 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001052 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001053
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001054 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 if (renderTarget) {
1056#if GR_COLLECT_STATS
1057 ++fStats.fRenderTargetCreateCnt;
1058#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001059 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1060 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001061 glTexDesc.fTextureID,
1062 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001063 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 return return_null_texture();
1065 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001066 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001067 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001068 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001070 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001071#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001072 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1073 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001074#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 return tex;
1076}
1077
1078namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1080 GrGLuint rb,
1081 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 // we shouldn't ever know one size and not the other
1083 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1084 (kUnknownBitCount == format->fTotalBits));
1085 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001086 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001087 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1088 (GrGLint*)&format->fStencilBits);
1089 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001090 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1092 (GrGLint*)&format->fTotalBits);
1093 format->fTotalBits += format->fStencilBits;
1094 } else {
1095 format->fTotalBits = format->fStencilBits;
1096 }
1097 }
1098}
1099}
1100
1101bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1102 int width, int height) {
1103
1104 // All internally created RTs are also textures. We don't create
1105 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1106 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001107 GrAssert(width >= rt->width());
1108 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109
1110 int samples = rt->numSamples();
1111 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001112 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113 if (!sbID) {
1114 return false;
1115 }
1116
1117 GrGLStencilBuffer* sb = NULL;
1118
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001119 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001120 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001121 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001122 // we start with the last stencil format that succeeded in hopes
1123 // that we won't go through this loop more than once after the
1124 // first (painful) stencil creation.
1125 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001126 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001127 GrGLClearErr(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001128 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001129 // version on a GL that doesn't have an MSAA extension.
1130 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1132 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 GR_GL_RENDERBUFFER,
1134 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001135 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 width,
1137 height));
1138 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1140 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001141 sFmt.fInternalFormat,
1142 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143 }
1144
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001145 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 if (err == GR_GL_NO_ERROR) {
1147 // After sized formats we attempt an unsized format and take whatever
1148 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001149 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001150 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001151 sb = new GrGLStencilBuffer(this, sbID, width, height,
1152 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001153 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1154 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001155 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 return true;
1158 }
1159 sb->abandon(); // otherwise we lose sbID
1160 sb->unref();
1161 }
1162 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001163 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001164 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165}
1166
1167bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1168 GrRenderTarget* rt) {
1169 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1170
1171 GrGLuint fbo = glrt->renderFBOID();
1172
1173 if (NULL == sb) {
1174 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001175 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 GR_GL_STENCIL_ATTACHMENT,
1177 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001178 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 GR_GL_DEPTH_ATTACHMENT,
1180 GR_GL_RENDERBUFFER, 0));
1181#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001182 GrGLenum status;
1183 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1185#endif
1186 }
1187 return true;
1188 } else {
1189 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1190 GrGLuint rb = glsb->renderbufferID();
1191
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001192 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001193 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1194 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 GR_GL_STENCIL_ATTACHMENT,
1196 GR_GL_RENDERBUFFER, rb));
1197 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001198 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001199 GR_GL_DEPTH_ATTACHMENT,
1200 GR_GL_RENDERBUFFER, rb));
1201 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 GR_GL_DEPTH_ATTACHMENT,
1204 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001205 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001207 GrGLenum status;
1208 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001210 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 GR_GL_STENCIL_ATTACHMENT,
1212 GR_GL_RENDERBUFFER, 0));
1213 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001214 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 GR_GL_DEPTH_ATTACHMENT,
1216 GR_GL_RENDERBUFFER, 0));
1217 }
1218 return false;
1219 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 return true;
1221 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001223}
1224
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001225////////////////////////////////////////////////////////////////////////////////
1226
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001227GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001228 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001229 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001232 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001233 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001235 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1236 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1237 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001238 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001239 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 // deleting bound buffer does implicit bind to 0
1241 fHWGeometryState.fVertexBuffer = NULL;
1242 return NULL;
1243 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001244 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001245 size, dynamic);
1246 fHWGeometryState.fVertexBuffer = vertexBuffer;
1247 return vertexBuffer;
1248 }
1249 return NULL;
1250}
1251
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001252GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001253 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001254 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001255 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001256 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1257 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001259 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1260 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1261 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001262 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 // deleting bound buffer does implicit bind to 0
1265 fHWGeometryState.fIndexBuffer = NULL;
1266 return NULL;
1267 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001268 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 size, dynamic);
1270 fHWGeometryState.fIndexBuffer = indexBuffer;
1271 return indexBuffer;
1272 }
1273 return NULL;
1274}
1275
reed@google.comac10a2d2010-12-22 21:39:39 +00001276void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001277 const GrDrawState& drawState = this->getDrawState();
1278 const GrGLRenderTarget* rt =
1279 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1280
1281 GrAssert(NULL != rt);
1282 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001283
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001284 GrGLIRect scissor;
1285 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001286 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001287 rect->width(), rect->height());
1288 if (scissor.contains(vp)) {
1289 rect = NULL;
1290 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 }
1292
1293 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001295 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 fHWBounds.fScissorRect = scissor;
1297 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001299 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001300 fHWBounds.fScissorEnabled = true;
1301 }
1302 } else {
1303 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001304 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001305 fHWBounds.fScissorEnabled = false;
1306 }
1307 }
1308}
1309
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001310void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001311 const GrDrawState& drawState = this->getDrawState();
1312 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001313 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001314 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001315
bsalomon@google.com74b98712011-11-11 19:46:16 +00001316 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001317 if (NULL != rect) {
1318 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001319 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001320 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001321 if (clippedRect.intersect(rtRect)) {
1322 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001323 } else {
1324 return;
1325 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001327 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001328 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001329
1330 GrGLfloat r, g, b, a;
1331 static const GrGLfloat scale255 = 1.f / 255.f;
1332 a = GrColorUnpackA(color) * scale255;
1333 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001334 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001335 scaleRGB *= a;
1336 }
1337 r = GrColorUnpackR(color) * scaleRGB;
1338 g = GrColorUnpackG(color) * scaleRGB;
1339 b = GrColorUnpackB(color) * scaleRGB;
1340
1341 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001342 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001343 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001344 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001345}
1346
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001347void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001348 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001349 return;
1350 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001351
1352 this->flushRenderTarget(&GrIRect::EmptyIRect());
1353
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001355 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001356 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001358 GL_CALL(StencilMask(0xffffffff));
1359 GL_CALL(ClearStencil(0));
1360 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001361 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001362}
1363
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001364void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001365 const GrDrawState& drawState = this->getDrawState();
1366 const GrRenderTarget* rt = drawState.getRenderTarget();
1367 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001368
1369 // this should only be called internally when we know we have a
1370 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001371 GrAssert(NULL != rt->getStencilBuffer());
1372 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001373#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001374 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001375 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001376#else
1377 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001378 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001379 // turned into draws. Our contract on GrDrawTarget says that
1380 // changing the clip between stencil passes may or may not
1381 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001382 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001383#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001384 GrGLint value;
1385 if (insideClip) {
1386 value = (1 << (stencilBitCount - 1));
1387 } else {
1388 value = 0;
1389 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001390 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001391 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001392 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001393 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001394 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001395 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001396}
1397
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001398void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001399 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001400}
1401
bsalomon@google.comc4364992011-11-07 15:54:49 +00001402bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1403 int left, int top,
1404 int width, int height,
1405 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001406 size_t rowBytes) const {
1407 // if GL can do the flip then we'll never pay for it.
1408 if (this->glCaps().fPackFlipYSupport) {
1409 return false;
1410 }
1411
1412 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001413 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001414 if (this->glCaps().fPackRowLengthSupport) {
1415 return true;
1416 }
1417 // If we have to do memcpys to handle rowBytes then y-flip is free
1418 // Note the rowBytes might be tight to the passed in data, but if data
1419 // gets clipped in x to the target the rowBytes will no longer be tight.
1420 if (left >= 0 && (left + width) < renderTarget->width()) {
1421 return 0 == rowBytes ||
1422 GrBytesPerPixel(config) * width == rowBytes;
1423 } else {
1424 return false;
1425 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001426}
1427
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001428bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001429 int left, int top,
1430 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001431 GrPixelConfig config,
1432 void* buffer,
1433 size_t rowBytes,
1434 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001435 GrGLenum internalFormat; // we don't use this for glReadPixels
1436 GrGLenum format;
1437 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001438 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1439 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001440 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001441 size_t bpp = GrBytesPerPixel(config);
1442 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1443 &left, &top, &width, &height,
1444 const_cast<const void**>(&buffer),
1445 &rowBytes)) {
1446 return false;
1447 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001448
bsalomon@google.comc6980972011-11-02 19:57:21 +00001449 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001450 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001451 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001452 switch (tgt->getResolveType()) {
1453 case GrGLRenderTarget::kCantResolve_ResolveType:
1454 return false;
1455 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001456 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001457 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001458 break;
1459 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001460 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001461 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001462 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1463 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001464 break;
1465 default:
1466 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001467 }
1468
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001469 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001470
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001471 // the read rect is viewport-relative
1472 GrGLIRect readRect;
1473 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001474
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001475 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001476 if (0 == rowBytes) {
1477 rowBytes = tightRowBytes;
1478 }
1479 size_t readDstRowBytes = tightRowBytes;
1480 void* readDst = buffer;
1481
1482 // determine if GL can read using the passed rowBytes or if we need
1483 // a scratch buffer.
1484 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1485 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001486 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001487 GrAssert(!(rowBytes % sizeof(GrColor)));
1488 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1489 readDstRowBytes = rowBytes;
1490 } else {
1491 scratch.reset(tightRowBytes * height);
1492 readDst = scratch.get();
1493 }
1494 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001495 if (!invertY && this->glCaps().fPackFlipYSupport) {
1496 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1497 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001498 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1499 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001500 format, type, readDst));
1501 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001502 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001503 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1504 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001505 if (!invertY && this->glCaps().fPackFlipYSupport) {
1506 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1507 invertY = true;
1508 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001509
1510 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511 // API presents top-to-bottom. We must preserve the padding contents. Note
1512 // that the above readPixels did not overwrite the padding.
1513 if (readDst == buffer) {
1514 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001515 if (!invertY) {
1516 scratch.reset(tightRowBytes);
1517 void* tmpRow = scratch.get();
1518 // flip y in-place by rows
1519 const int halfY = height >> 1;
1520 char* top = reinterpret_cast<char*>(buffer);
1521 char* bottom = top + (height - 1) * rowBytes;
1522 for (int y = 0; y < halfY; y++) {
1523 memcpy(tmpRow, top, tightRowBytes);
1524 memcpy(top, bottom, tightRowBytes);
1525 memcpy(bottom, tmpRow, tightRowBytes);
1526 top += rowBytes;
1527 bottom -= rowBytes;
1528 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001529 }
1530 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001531 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001532 // copy from readDst to buffer while flipping y
1533 const int halfY = height >> 1;
1534 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001535 char* dst = reinterpret_cast<char*>(buffer);
1536 if (!invertY) {
1537 dst += (height-1) * rowBytes;
1538 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 for (int y = 0; y < height; y++) {
1540 memcpy(dst, src, tightRowBytes);
1541 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001542 if (invertY) {
1543 dst += rowBytes;
1544 } else {
1545 dst -= rowBytes;
1546 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001547 }
1548 }
1549 return true;
1550}
1551
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001552void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001553
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001554 GrGLRenderTarget* rt =
1555 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1556 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001557
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001558 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001559 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001560 #if GR_COLLECT_STATS
1561 ++fStats.fRenderTargetChngCnt;
1562 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001563 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001564 GrGLenum status;
1565 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001566 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001567 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001568 }
1569 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001570 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001571 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001572 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001573 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001574 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001575 fHWBounds.fViewportRect = vp;
1576 }
1577 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001578 if (NULL == bound || !bound->isEmpty()) {
1579 rt->flagAsNeedingResolve(bound);
1580 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001581}
1582
twiz@google.com0f31ca72011-03-18 17:38:11 +00001583GrGLenum gPrimitiveType2GLMode[] = {
1584 GR_GL_TRIANGLES,
1585 GR_GL_TRIANGLE_STRIP,
1586 GR_GL_TRIANGLE_FAN,
1587 GR_GL_POINTS,
1588 GR_GL_LINES,
1589 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001590};
1591
bsalomon@google.comd302f142011-03-03 13:54:13 +00001592#define SWAP_PER_DRAW 0
1593
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001594#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001595 #if GR_MAC_BUILD
1596 #include <AGL/agl.h>
1597 #elif GR_WIN32_BUILD
1598 void SwapBuf() {
1599 DWORD procID = GetCurrentProcessId();
1600 HWND hwnd = GetTopWindow(GetDesktopWindow());
1601 while(hwnd) {
1602 DWORD wndProcID = 0;
1603 GetWindowThreadProcessId(hwnd, &wndProcID);
1604 if(wndProcID == procID) {
1605 SwapBuffers(GetDC(hwnd));
1606 }
1607 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1608 }
1609 }
1610 #endif
1611#endif
1612
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001613void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1614 uint32_t startVertex,
1615 uint32_t startIndex,
1616 uint32_t vertexCount,
1617 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001618 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1619
twiz@google.com0f31ca72011-03-18 17:38:11 +00001620 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001621
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001622 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1623 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1624
1625 // our setupGeometry better have adjusted this to zero since
1626 // DrawElements always draws from the begining of the arrays for idx 0.
1627 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001628
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001629 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1630 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001631#if SWAP_PER_DRAW
1632 glFlush();
1633 #if GR_MAC_BUILD
1634 aglSwapBuffers(aglGetCurrentContext());
1635 int set_a_break_pt_here = 9;
1636 aglSwapBuffers(aglGetCurrentContext());
1637 #elif GR_WIN32_BUILD
1638 SwapBuf();
1639 int set_a_break_pt_here = 9;
1640 SwapBuf();
1641 #endif
1642#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001643}
1644
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001645void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1646 uint32_t startVertex,
1647 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001648 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1649
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001650 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1651
1652 // our setupGeometry better have adjusted this to zero.
1653 // DrawElements doesn't take an offset so we always adjus the startVertex.
1654 GrAssert(0 == startVertex);
1655
1656 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1657 // account for startVertex in the DrawElements case. So we always
1658 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001659 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001660#if SWAP_PER_DRAW
1661 glFlush();
1662 #if GR_MAC_BUILD
1663 aglSwapBuffers(aglGetCurrentContext());
1664 int set_a_break_pt_here = 9;
1665 aglSwapBuffers(aglGetCurrentContext());
1666 #elif GR_WIN32_BUILD
1667 SwapBuf();
1668 int set_a_break_pt_here = 9;
1669 SwapBuf();
1670 #endif
1671#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001672}
1673
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001674void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001675
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001676 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001677 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001678 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001679 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1680 rt->renderFBOID()));
1681 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1682 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 #if GR_COLLECT_STATS
1684 ++fStats.fRenderTargetChngCnt;
1685 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001686 // make sure we go through flushRenderTarget() since we've modified
1687 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001688 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001689 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001690 const GrIRect dirtyRect = rt->getResolveRect();
1691 GrGLIRect r;
1692 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1693 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001694
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001695 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001696 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001697 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1698 GL_CALL(Scissor(r.fLeft, r.fBottom,
1699 r.fWidth, r.fHeight));
1700 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001701 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 fHWBounds.fScissorEnabled = true;
1703 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001704 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001705 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001706 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1707 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001708 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001709 int right = r.fLeft + r.fWidth;
1710 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001711 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1712 r.fLeft, r.fBottom, right, top,
1713 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001715 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001716 }
1717}
1718
twiz@google.com0f31ca72011-03-18 17:38:11 +00001719static const GrGLenum grToGLStencilFunc[] = {
1720 GR_GL_ALWAYS, // kAlways_StencilFunc
1721 GR_GL_NEVER, // kNever_StencilFunc
1722 GR_GL_GREATER, // kGreater_StencilFunc
1723 GR_GL_GEQUAL, // kGEqual_StencilFunc
1724 GR_GL_LESS, // kLess_StencilFunc
1725 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1726 GR_GL_EQUAL, // kEqual_StencilFunc,
1727 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001728};
1729GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1730GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1731GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1732GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1733GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1734GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1735GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1736GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1737GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1738
twiz@google.com0f31ca72011-03-18 17:38:11 +00001739static const GrGLenum grToGLStencilOp[] = {
1740 GR_GL_KEEP, // kKeep_StencilOp
1741 GR_GL_REPLACE, // kReplace_StencilOp
1742 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1743 GR_GL_INCR, // kIncClamp_StencilOp
1744 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1745 GR_GL_DECR, // kDecClamp_StencilOp
1746 GR_GL_ZERO, // kZero_StencilOp
1747 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001748};
1749GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1750GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1751GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1752GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1753GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1754GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1755GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1756GR_STATIC_ASSERT(6 == kZero_StencilOp);
1757GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1758
reed@google.comac10a2d2010-12-22 21:39:39 +00001759void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001760 const GrDrawState& drawState = this->getDrawState();
1761
1762 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001763
1764 // use stencil for clipping if clipping is enabled and the clip
1765 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001766 bool stencilClip = fClipInStencil && drawState.isClipState();
1767 bool drawClipToStencil =
1768 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001769 bool stencilChange = fHWStencilClip != stencilClip ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001770 fHWDrawState.getStencil() != *settings ||
1771 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1772 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001773
1774 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001775
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001776 // we can't simultaneously perform stencil-clipping and
1777 // modify the stencil clip
1778 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001779
bsalomon@google.comd302f142011-03-03 13:54:13 +00001780 if (settings->isDisabled()) {
1781 if (stencilClip) {
1782 settings = &gClipStencilSettings;
1783 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001784 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001785
1786 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001787 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001788 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001789 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001790 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001791 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001792 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1793 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1794 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1795 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1796 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1797 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1798 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1799 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1800 }
1801 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001802 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001803 GrStencilBuffer* stencilBuffer =
1804 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001805 if (NULL != stencilBuffer) {
1806 stencilBits = stencilBuffer->bits();
1807 }
1808 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001809 GrAssert(stencilBits ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001810 (GrStencilSettings::gDisabled == *settings));
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001811
1812 GrGLuint clipStencilMask = 0;
1813 GrGLuint userStencilMask = ~0;
1814 if (stencilBits > 0) {
1815 clipStencilMask = 1 << (stencilBits - 1);
1816 userStencilMask = clipStencilMask - 1;
1817 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001818
1819 unsigned int frontRef = settings->fFrontFuncRef;
1820 unsigned int frontMask = settings->fFrontFuncMask;
1821 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001822 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001823
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001824 if (drawClipToStencil) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001825 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1826 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1827 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001828 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
1829 stencilClip, settings->fFrontFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830
1831 ConvertStencilFuncAndMask(settings->fFrontFunc,
1832 stencilClip,
1833 clipStencilMask,
1834 userStencilMask,
1835 &frontRef,
1836 &frontMask);
1837 frontWriteMask &= userStencilMask;
1838 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001839 GrAssert((size_t)
1840 settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1841 GrAssert((size_t)
1842 settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
1843 GrAssert((size_t)
1844 settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1845 GrAssert((size_t)
1846 settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001847 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001848 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001849
1850 unsigned int backRef = settings->fBackFuncRef;
1851 unsigned int backMask = settings->fBackFuncMask;
1852 unsigned int backWriteMask = settings->fBackWriteMask;
1853
1854
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001855 if (drawClipToStencil) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001856 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1857 backFunc = grToGLStencilFunc[settings->fBackFunc];
1858 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001859 backFunc = grToGLStencilFunc[ConvertStencilFunc(
1860 stencilClip, settings->fBackFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861 ConvertStencilFuncAndMask(settings->fBackFunc,
1862 stencilClip,
1863 clipStencilMask,
1864 userStencilMask,
1865 &backRef,
1866 &backMask);
1867 backWriteMask &= userStencilMask;
1868 }
1869
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001870 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1871 frontRef, frontMask));
1872 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1873 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1874 backRef, backMask));
1875 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1876 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1877 grToGLStencilOp[settings->fFrontFailOp],
1878 grToGLStencilOp[settings->fFrontPassOp],
1879 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001880
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001881 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1882 grToGLStencilOp[settings->fBackFailOp],
1883 grToGLStencilOp[settings->fBackPassOp],
1884 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001885 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001886 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1887 GL_CALL(StencilMask(frontWriteMask));
1888 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001889 grToGLStencilOp[settings->fFrontPassOp],
1890 grToGLStencilOp[settings->fFrontPassOp]));
1891 }
1892 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001893 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001894 fHWStencilClip = stencilClip;
1895 }
1896}
1897
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001898void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001899 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001900 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001901 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1902 // smooth lines.
1903
1904 // we prefer smooth lines over multisampled lines
1905 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001906 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001907 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001908 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001909 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001910 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001911 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001912 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001913 fHWAAState.fSmoothLineEnabled = false;
1914 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001915 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001916 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001917 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001918 fHWAAState.fMSAAEnabled = false;
1919 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001920 } else if (rt->isMultisampled() &&
1921 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001922 fHWAAState.fMSAAEnabled) {
1923 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001924 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001925 fHWAAState.fMSAAEnabled = false;
1926 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001927 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001928 fHWAAState.fMSAAEnabled = true;
1929 }
1930 }
1931 }
1932}
1933
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001934void GrGpuGL::flushBlend(GrPrimitiveType type,
1935 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001936 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001937 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001938 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001939 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001940 fHWBlendDisabled = false;
1941 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001942 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1943 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001944 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1945 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001946 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001947 }
1948 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001949 // any optimization to disable blending should
1950 // have already been applied and tweaked the coeffs
1951 // to (1, 0).
1952 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1953 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001954 if (fHWBlendDisabled != blendOff) {
1955 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001956 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001957 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001959 }
1960 fHWBlendDisabled = blendOff;
1961 }
1962 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001963 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
1964 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001965 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1966 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001967 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001968 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001969 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001970 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1971 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001972 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001973
1974 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001975 GrColorUnpackR(blendConst) / 255.f,
1976 GrColorUnpackG(blendConst) / 255.f,
1977 GrColorUnpackB(blendConst) / 255.f,
1978 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001979 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001980 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001981 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001982 }
1983 }
1984 }
1985}
1986
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001987namespace {
1988
1989unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001990 switch (filter) {
1991 case GrSamplerState::kBilinear_Filter:
1992 case GrSamplerState::k4x4Downsample_Filter:
1993 return GR_GL_LINEAR;
1994 case GrSamplerState::kNearest_Filter:
1995 case GrSamplerState::kConvolution_Filter:
1996 return GR_GL_NEAREST;
1997 default:
1998 GrAssert(!"Unknown filter type");
1999 return GR_GL_LINEAR;
2000 }
2001}
2002
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002003const GrGLenum* get_swizzle(GrPixelConfig config,
2004 const GrSamplerState& sampler) {
2005 if (GrPixelConfigIsAlphaOnly(config)) {
2006 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2007 GR_GL_ALPHA, GR_GL_ALPHA };
2008 return gAlphaSmear;
2009 } else if (sampler.swapsRAndB()) {
2010 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2011 GR_GL_RED, GR_GL_ALPHA };
2012 return gRedBlueSwap;
2013 } else {
2014 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2015 GR_GL_BLUE, GR_GL_ALPHA };
2016 return gStraight;
2017 }
2018}
2019
2020void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2021 // should add texparameteri to interface to make 1 instead of 4 calls here
2022 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2023 GR_GL_TEXTURE_SWIZZLE_R,
2024 swizzle[0]));
2025 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2026 GR_GL_TEXTURE_SWIZZLE_G,
2027 swizzle[1]));
2028 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2029 GR_GL_TEXTURE_SWIZZLE_B,
2030 swizzle[2]));
2031 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2032 GR_GL_TEXTURE_SWIZZLE_A,
2033 swizzle[3]));
2034}
2035}
2036
bsalomon@google.comffca4002011-02-22 20:34:01 +00002037bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002038
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002039 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002040 // GrGpu::setupClipAndFlushState should have already checked this
2041 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002042 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002043
tomhudson@google.com93813632011-10-27 20:21:16 +00002044 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002045 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002046 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002047 GrGLTexture* nextTexture =
2048 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002049
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002050 // true for now, but maybe not with GrEffect.
2051 GrAssert(NULL != nextTexture);
2052 // if we created a rt/tex and rendered to it without using a
2053 // texture and now we're texuring from the rt it will still be
2054 // the last bound texture, but it needs resolving. So keep this
2055 // out of the "last != next" check.
2056 GrGLRenderTarget* texRT =
2057 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2058 if (NULL != texRT) {
2059 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002060 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002061
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002062 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002063 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002064 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002065 #if GR_COLLECT_STATS
2066 ++fStats.fTextureChngCnt;
2067 #endif
2068 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002069 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002070 // The texture matrix has to compensate for texture width/height
2071 // and NPOT-embedded-in-POT
2072 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002073 }
2074
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002075 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002076 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002077 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002078 nextTexture->getCachedTexParams(&timestamp);
2079 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002080 GrGLTexture::TexParams newTexParams;
2081
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002082 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002083
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002084 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002085 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2086 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002087 memcpy(newTexParams.fSwizzleRGBA,
2088 get_swizzle(nextTexture->config(), sampler),
2089 sizeof(newTexParams.fSwizzleRGBA));
2090 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002091 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002092 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002093 GR_GL_TEXTURE_MAG_FILTER,
2094 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002095 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002096 GR_GL_TEXTURE_MIN_FILTER,
2097 newTexParams.fFilter));
2098 }
2099 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2100 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002101 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002102 GR_GL_TEXTURE_WRAP_S,
2103 newTexParams.fWrapS));
2104 }
2105 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2106 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002107 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002108 GR_GL_TEXTURE_WRAP_T,
2109 newTexParams.fWrapT));
2110 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002111 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002112 (setAll ||
2113 memcmp(newTexParams.fSwizzleRGBA,
2114 oldTexParams.fSwizzleRGBA,
2115 sizeof(newTexParams.fSwizzleRGBA)))) {
2116 setTextureUnit(s);
2117 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2118 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002119 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002120 nextTexture->setCachedTexParams(newTexParams,
2121 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002122 }
2123 }
2124
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002125 GrIRect* rect = NULL;
2126 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002127 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002128 fClip.hasConservativeBounds()) {
2129 fClip.getConservativeBounds().roundOut(&clipBounds);
2130 rect = &clipBounds;
2131 }
2132 this->flushRenderTarget(rect);
2133 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002134
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002135 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2136 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002137 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002138 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002139 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002140 }
2141 }
2142
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002143 if (drawState->isColorWriteDisabled() !=
2144 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002145 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002146 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002147 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002148 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002149 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002150 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002151 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002152 }
2153
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002154 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2155 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002156 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002157 GL_CALL(Enable(GR_GL_CULL_FACE));
2158 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002159 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002160 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002161 GL_CALL(Enable(GR_GL_CULL_FACE));
2162 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002163 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002164 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002166 break;
2167 default:
2168 GrCrash("Unknown draw face.");
2169 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002170 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002171 }
2172
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002173#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002174 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002175 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002176 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002177 NULL == drawState->getRenderTarget() ||
2178 NULL == drawState->getTexture(s) ||
2179 drawState->getTexture(s)->asRenderTarget() !=
2180 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002181 }
2182#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002183
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002184 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002185
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002186 // This copy must happen after flushStencil() is called. flushStencil()
2187 // relies on detecting when the kModifyStencilClip_StateBit state has
2188 // changed since the last draw.
2189 fHWDrawState.copyStateFlags(*drawState);
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.com8f9cbd62011-12-09 15:55:34 +00002221 GrDrawState* drawState = this->drawState();
2222 if (drawState->getRenderTarget() == renderTarget) {
2223 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002224 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002225 if (fHWDrawState.getRenderTarget() == renderTarget) {
2226 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002227 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002228}
2229
2230void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002231 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002232 GrDrawState* drawState = this->drawState();
2233 if (drawState->getTexture(s) == texture) {
2234 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002235 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002236 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002237 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002238 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002239 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002240 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002241}
2242
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002243bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002244 GrGLenum* internalFormat,
bsalomon@google.com6f379512011-11-16 20:36:03 +00002245 GrGLenum* externalFormat,
2246 GrGLenum* externalType) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002247 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002248 case kRGBA_8888_PM_GrPixelConfig:
2249 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002250 *externalFormat = GR_GL_RGBA;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002251 *internalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002252 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002253 break;
2254 case kBGRA_8888_PM_GrPixelConfig:
2255 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002256 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002257 return false;
2258 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002259 *externalFormat = GR_GL_BGRA;
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002260 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002261 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002262 } else {
2263 *internalFormat = GR_GL_RGBA;
2264 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002265 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002266 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002267 case kRGB_565_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002268 *externalFormat = GR_GL_RGB;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002269 *internalFormat = GR_GL_RGB;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002270 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002271 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002272 case kRGBA_4444_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002273 *externalFormat = GR_GL_RGBA;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002274 *internalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002275 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002276 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002277 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002278 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00002279 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002280 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002281 *externalType = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002282 } else {
2283 return false;
2284 }
2285 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002286 case kAlpha_8_GrPixelConfig:
bsalomon@google.com6f379512011-11-16 20:36:03 +00002287 *externalFormat = GR_GL_ALPHA;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002288 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002289 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002290 break;
2291 default:
2292 return false;
2293 }
2294 return true;
2295}
2296
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002297void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002298 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002299 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002300 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002301 fActiveTextureUnitIdx = unit;
2302 }
2303}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002304
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002305void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002306 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002307 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002308 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2309 }
2310}
2311
reed@google.comac10a2d2010-12-22 21:39:39 +00002312/* On ES the internalFormat and format must match for TexImage and we use
2313 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2314 decide the internalFormat. However, on ES internalFormat for
2315 RenderBufferStorage* has to be a specific format (not a base format like
2316 GL_RGBA).
2317 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002318bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002320 // The ES story for BGRA and RenderbufferStorage appears murky. It
2321 // takes an internal format as a parameter. The OES FBO extension and
2322 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2323 // BGRA extensions adds BGRA as both an internal and external format
2324 // and the other only as an external format (like desktop GL). OES
2325 // restricts RenderbufferStorage's format to a *sized* internal format.
2326 // There is no sized BGRA internal format.
2327 // So if the texture has internal format BGRA we just hope that the
2328 // resolve blit can do RGBA->BGRA internal format conversion.
2329 case kRGBA_8888_PM_GrPixelConfig:
2330 case kRGBA_8888_UPM_GrPixelConfig:
2331 case kBGRA_8888_PM_GrPixelConfig:
2332 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002333 if (fGLCaps.fRGBA8RenderbufferSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002334 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2335 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002336 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002337 return true;
2338 } else {
2339 return false;
2340 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002341 case kRGB_565_GrPixelConfig:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002342 // ES2 supports 565, but desktop GL does not.
bsalomon@google.comc4364992011-11-07 15:54:49 +00002343 if (kDesktop_GrGLBinding != this->glBinding()) {
2344 *format = GR_GL_RGB565;
2345 return true;
2346 } else {
2347 return false;
2348 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002349 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002350 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002351 return true;
2352 default:
2353 return false;
2354 }
2355}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002356
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002357void GrGpuGL::resetDirtyFlags() {
2358 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2359}
2360
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002361void GrGpuGL::setBuffers(bool indexed,
2362 int* extraVertexOffset,
2363 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002364
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002365 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002366
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002367 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2368
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002369 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002370 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002371 case kBuffer_GeometrySrcType:
2372 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002373 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002374 break;
2375 case kArray_GeometrySrcType:
2376 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002377 this->finalizeReservedVertices();
2378 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2379 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002380 break;
2381 default:
2382 vbuf = NULL; // suppress warning
2383 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002384 }
2385
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002386 GrAssert(NULL != vbuf);
2387 GrAssert(!vbuf->isLocked());
2388 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002389 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002390 fHWGeometryState.fArrayPtrsDirty = true;
2391 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002392 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002393
2394 if (indexed) {
2395 GrAssert(NULL != extraIndexOffset);
2396
2397 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002398 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002399 case kBuffer_GeometrySrcType:
2400 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002401 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002402 break;
2403 case kArray_GeometrySrcType:
2404 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002405 this->finalizeReservedIndices();
2406 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2407 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002408 break;
2409 default:
2410 ibuf = NULL; // suppress warning
2411 GrCrash("Unknown geometry src type!");
2412 }
2413
2414 GrAssert(NULL != ibuf);
2415 GrAssert(!ibuf->isLocked());
2416 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002417 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002418 fHWGeometryState.fIndexBuffer = ibuf;
2419 }
2420 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002421}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002422
2423int GrGpuGL::getMaxEdges() const {
2424 // FIXME: This is a pessimistic estimate based on how many other things
2425 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002426 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2427 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002428}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002429
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002430void GrGpuGL::GLCaps::print() const {
2431 for (int i = 0; i < fStencilFormats.count(); ++i) {
2432 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2433 i,
2434 fStencilFormats[i].fStencilBits,
2435 fStencilFormats[i].fTotalBits);
2436 }
2437
2438 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2439 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2440 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2441 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2442 static const char* gMSFBOExtStr[] = {
2443 "None",
2444 "ARB",
2445 "EXT",
2446 "Apple",
2447 };
2448 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002449 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002450 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2451 }
2452 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2453 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002454 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002455 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002456 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002457 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002458 (fTextureSwizzleSupport ? "YES": "NO"));
2459 GrPrintf("Unpack Row length support: %s\n",
2460 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002461 GrPrintf("Unpack Flip Y support: %s\n",
2462 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002463 GrPrintf("Pack Row length support: %s\n",
2464 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002465 GrPrintf("Pack Flip Y support: %s\n",
2466 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002467}