blob: d0fb12fe1566e1faf46ce2f714da20817a78adb3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
twiz@google.com0f31ca72011-03-18 17:38:11 +000027static const GrGLenum gXfermodeCoeff2Blend[] = {
28 GR_GL_ZERO,
29 GR_GL_ONE,
30 GR_GL_SRC_COLOR,
31 GR_GL_ONE_MINUS_SRC_COLOR,
32 GR_GL_DST_COLOR,
33 GR_GL_ONE_MINUS_DST_COLOR,
34 GR_GL_SRC_ALPHA,
35 GR_GL_ONE_MINUS_SRC_ALPHA,
36 GR_GL_DST_ALPHA,
37 GR_GL_ONE_MINUS_DST_ALPHA,
38 GR_GL_CONSTANT_COLOR,
39 GR_GL_ONE_MINUS_CONSTANT_COLOR,
40 GR_GL_CONSTANT_ALPHA,
41 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
43 // extended blend coeffs
44 GR_GL_SRC1_COLOR,
45 GR_GL_ONE_MINUS_SRC1_COLOR,
46 GR_GL_SRC1_ALPHA,
47 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000048};
49
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 static const bool gCoeffReferencesBlendConst[] = {
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 false,
62 true,
63 true,
64 true,
65 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000066
67 // extended blend coeffs
68 false,
69 false,
70 false,
71 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000072 };
73 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
75
76 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
77 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
78 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
79 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
80 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
81 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
82 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
83 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
84 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
85 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
86 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
87 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
88 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
89 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
90
91 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
92 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
93 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
94 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
95
96 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
97 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000098}
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
103 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104 GrMatrix* matrix) {
105 GrAssert(NULL != texture);
106 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000107 GrGLTexture::Orientation orientation = texture->orientation();
108 if (GrGLTexture::kBottomUp_Orientation == orientation) {
109 GrMatrix invY;
110 invY.setAll(GR_Scalar1, 0, 0,
111 0, -GR_Scalar1, GR_Scalar1,
112 0, 0, GrMatrix::I()[8]);
113 matrix->postConcat(invY);
114 } else {
115 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
116 }
117}
118
bsalomon@google.comd302f142011-03-03 13:54:13 +0000119bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120 const GrSamplerState& sampler) {
121 GrAssert(NULL != texture);
122 if (!sampler.getMatrix().isIdentity()) {
123 return false;
124 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000125 GrGLTexture::Orientation orientation = texture->orientation();
126 if (GrGLTexture::kBottomUp_Orientation == orientation) {
127 return false;
128 } else {
129 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
130 }
131 return true;
132}
133
134///////////////////////////////////////////////////////////////////////////////
135
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000136static bool gPrintStartupSpew;
137
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000139
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000141
twiz@google.com0f31ca72011-03-18 17:38:11 +0000142 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
144 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000145 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000146 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
147 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000148 // some implementations require texture to be mip-map complete before
149 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000150 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000151 GR_GL_TEXTURE_MIN_FILTER,
152 GR_GL_NEAREST));
153 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
154 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
155 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
156 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
157 GR_GL_COLOR_ATTACHMENT0,
158 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000159 GrGLenum status;
160 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
162 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000163
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000165}
166
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000167GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000168
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000169 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000170
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 gl->ref();
172 fGL = gl;
173 fGLBinding = glBinding;
174 switch (glBinding) {
175 case kDesktop_GrGLBinding:
176 GrAssert(gl->supportsDesktop());
177 break;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000178 case kES2_GrGLBinding:
179 GrAssert(gl->supportsES2());
180 break;
181 default:
182 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000183 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185 GrGLClearErr(fGL);
186
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000187 const GrGLubyte* ext;
188 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000189 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000190 const GrGLubyte* vendor;
191 const GrGLubyte* renderer;
192 const GrGLubyte* version;
193 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
194 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
195 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000196 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
197 this);
198 GrPrintf("------ VENDOR %s\n", vendor);
199 GrPrintf("------ RENDERER %s\n", renderer);
200 GrPrintf("------ VERSION %s\n", version);
201 GrPrintf("------ EXTENSIONS\n %s \n", ext);
202 }
203
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000204 fGLVersion = GrGLGetVersion(gl);
205 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000206 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000208 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000209
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000211
bsalomon@google.comfe676522011-06-17 18:12:21 +0000212 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213}
214
215GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000216 // This must be called by before the GrDrawTarget destructor
217 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000218 // This subclass must do this before the base class destructor runs
219 // since we will unref the GrGLInterface.
220 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000221 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000222}
223
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000224///////////////////////////////////////////////////////////////////////////////
225
226static const GrGLuint kUnknownBitCount = ~0;
227
228void GrGpuGL::initCaps() {
229 GrGLint maxTextureUnits;
230 // check FS and fixed-function texture unit limits
231 // we only use textures in the fragment stage currently.
232 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000233 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
234 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 if (kES2_GrGLBinding != this->glBinding()) {
236 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000237 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 }
239 if (kES2_GrGLBinding == this->glBinding()) {
240 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
241 &fGLCaps.fMaxFragmentUniformVectors);
242 } else if (kDesktop_GrGLBinding != this->glBinding()) {
243 GrGLint max;
244 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
245 fGLCaps.fMaxFragmentUniformVectors = max / 4;
246 } else {
247 fGLCaps.fMaxFragmentUniformVectors = 16;
248 }
249
250 GrGLint numFormats;
251 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
252 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
253 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
254 for (int i = 0; i < numFormats; ++i) {
255 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
256 fCaps.f8BitPaletteSupport = true;
257 break;
258 }
259 }
260
261 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 // we could also look for GL_ATI_separate_stencil extension or
263 // GL_EXT_stencil_two_side but they use different function signatures
264 // than GL2.0+ (and than each other).
265 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
266 // supported on GL 1.4 and higher or by extension
267 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
268 this->hasExtension("GL_EXT_stencil_wrap");
269 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000270 // ES 2 has two sided stencil and stencil wrap
271 fCaps.fTwoSidedStencilSupport = true;
272 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000273 }
274
275 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000276 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000277 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000278 fGLCaps.fRGBA8RenderbufferSupport =
279 this->hasExtension("GL_OES_rgb8_rgba8") ||
280 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000281 }
282
283
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000285 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
286 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 } else {
288 bool hasBGRAExt = false;
289 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000290 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000292 fGLCaps.fBGRAFormatSupport = true;
293 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000294 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000295 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000297 }
298
299 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000300 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000301 this->hasExtension("GL_ARB_texture_swizzle");
302 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000303 fGLCaps.fTextureSwizzleSupport = false;
304 }
305
306 if (kDesktop_GrGLBinding == this->glBinding()) {
307 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000308 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000309 fGLCaps.fPackRowLengthSupport = true;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000310 fGLCaps.fPackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000311 } else {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000312 fGLCaps.fUnpackRowLengthSupport =this->hasExtension("GL_EXT_unpack_subimage");
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000313 fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy");
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000314 // no extension for pack row length
315 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000316 fGLCaps.fPackFlipYSupport =
317 this->hasExtension("GL_ANGLE_pack_reverse_row_order");
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000318 }
319
320 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000321 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
322 // extension includes glMapBuffer.
323 } else {
324 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
325 }
326
327 if (kDesktop_GrGLBinding == this->glBinding()) {
328 if (fGLVersion >= GR_GL_VER(2,0) ||
329 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
330 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000331 } else {
332 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000333 }
334 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000335 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000336 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000337 }
338
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000339 fGLCaps.fTextureUsageSupport = (kES2_GrGLBinding == this->glBinding()) &&
340 this->hasExtension("GL_ANGLE_texture_usage");
341
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000342 // Tex storage is in desktop 4.2 and can be an extension to desktop or ES.
343 fGLCaps.fTexStorageSupport = (kDesktop_GrGLBinding == this->glBinding() &&
344 fGLVersion >= GR_GL_VER(4,2)) ||
345 this->hasExtension("GL_ARB_texture_storage") ||
346 this->hasExtension("GL_EXT_texture_storage");
347
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000348 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
349
350 ////////////////////////////////////////////////////////////////////////////
351 // Experiments to determine limitations that can't be queried.
352 // TODO: Make these a preprocess that generate some compile time constants.
353 // TODO: probe once at startup, rather than once per context creation.
354
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000355 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
356 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
357 // Our render targets are always created with textures as the color
358 // attachment, hence this min:
359 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
360
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000361 this->initFSAASupport();
362 this->initStencilFormats();
363}
364
365void GrGpuGL::initFSAASupport() {
366 // TODO: Get rid of GrAALevel and use # samples directly.
367 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
368 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
369 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
370 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
371 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
372
373 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
374 if (kDesktop_GrGLBinding != this->glBinding()) {
375 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
376 // chrome's extension is equivalent to the EXT msaa
377 // and fbo_blit extensions.
378 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
379 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
380 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
381 }
382 } else {
383 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
384 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
385 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
386 this->hasExtension("GL_EXT_framebuffer_blit")) {
387 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
388 }
389 }
390
391 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
392 GrGLint maxSamples;
393 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
394 if (maxSamples > 1 ) {
395 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
396 fGLCaps.fAASamples[kLow_GrAALevel] =
397 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
398 fGLCaps.fAASamples[kMed_GrAALevel] =
399 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
400 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
401 }
402 }
403 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
404}
405
406void GrGpuGL::initStencilFormats() {
407
408 // Build up list of legal stencil formats (though perhaps not supported on
409 // the particular gpu/driver) from most preferred to least.
410
411 // these consts are in order of most preferred to least preferred
412 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
413 static const GrGLStencilBuffer::Format
414 // internal Format stencil bits total bits packed?
415 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
416 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
417 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
418 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
419 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
420 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
421
422 if (kDesktop_GrGLBinding == this->glBinding()) {
423 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
424 this->hasExtension("GL_EXT_packed_depth_stencil") ||
425 this->hasExtension("GL_ARB_framebuffer_object");
426
427 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
428 // require FBO support we can expect these are legal formats and don't
429 // check. These also all support the unsized GL_STENCIL_INDEX.
430 fGLCaps.fStencilFormats.push_back() = gS8;
431 fGLCaps.fStencilFormats.push_back() = gS16;
432 if (supportsPackedDS) {
433 fGLCaps.fStencilFormats.push_back() = gD24S8;
434 }
435 fGLCaps.fStencilFormats.push_back() = gS4;
436 if (supportsPackedDS) {
437 fGLCaps.fStencilFormats.push_back() = gDS;
438 }
439 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000440 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
441 // for other formats.
442 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000443
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000444 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000445 //fStencilFormats.push_back() = gS16;
446 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
447 fGLCaps.fStencilFormats.push_back() = gD24S8;
448 }
449 if (this->hasExtension("GL_OES_stencil4")) {
450 fGLCaps.fStencilFormats.push_back() = gS4;
451 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000452 }
453}
454
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000455GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000456 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
457 return GrPixelConfigSwapRAndB(config);
458 } else {
459 return config;
460 }
461}
462
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000463GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000464 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000465 return GrPixelConfigSwapRAndB(config);
466 } else {
467 return config;
468 }
469}
470
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000471bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
472 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
473}
474
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000475void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000476 if (gPrintStartupSpew && !fPrintedCaps) {
477 fPrintedCaps = true;
478 this->getCaps().print();
479 fGLCaps.print();
480 }
481
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000482 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000484 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000486 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000487 GL_CALL(Disable(GR_GL_DEPTH_TEST));
488 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000489
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000490 GL_CALL(Disable(GR_GL_CULL_FACE));
491 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000492 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000494 GL_CALL(Disable(GR_GL_DITHER));
495 if (kDesktop_GrGLBinding == this->glBinding()) {
496 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
497 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
498 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000499 fHWAAState.fMSAAEnabled = false;
500 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000501 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000503 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000504 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000505
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000507 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000508
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000509 // invalid
510 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000513 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000514
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000515 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000516 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000517
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000518 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000519
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000520 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000521
tomhudson@google.com93813632011-10-27 20:21:16 +0000522 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000523 fHWDrawState.setTexture(s, NULL);
524 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
525 -GR_ScalarMax,
526 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000527 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000528 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000529 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000530
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000531 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000533 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000534 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000536 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000537 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000538 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000539
540 fHWGeometryState.fIndexBuffer = NULL;
541 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000542
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000543 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000544
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000545 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000546 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000547
548 // we assume these values
549 if (this->glCaps().fUnpackRowLengthSupport) {
550 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
551 }
552 if (this->glCaps().fPackRowLengthSupport) {
553 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
554 }
555 if (this->glCaps().fUnpackFlipYSupport) {
556 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
557 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000558 if (this->glCaps().fPackFlipYSupport) {
559 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
560 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000561}
562
bsalomon@google.come269f212011-11-07 13:29:52 +0000563GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000564 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000565 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000566 return NULL;
567 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000568
bsalomon@google.com99621082011-11-15 16:47:16 +0000569 glTexDesc.fWidth = desc.fWidth;
570 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000571 glTexDesc.fConfig = desc.fConfig;
572 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
573 glTexDesc.fOwnsID = false;
574 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
575
576 GrGLTexture* texture = NULL;
577 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
578 GrGLRenderTarget::Desc glRTDesc;
579 glRTDesc.fRTFBOID = 0;
580 glRTDesc.fTexFBOID = 0;
581 glRTDesc.fMSColorRenderbufferID = 0;
582 glRTDesc.fOwnIDs = true;
583 glRTDesc.fConfig = desc.fConfig;
584 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000585 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
586 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000587 glTexDesc.fTextureID,
588 &glRTDesc)) {
589 return NULL;
590 }
591 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
592 } else {
593 texture = new GrGLTexture(this, glTexDesc);
594 }
595 if (NULL == texture) {
596 return NULL;
597 }
598
599 this->setSpareTextureUnit();
600 return texture;
601}
602
603GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
604 GrGLRenderTarget::Desc glDesc;
605 glDesc.fConfig = desc.fConfig;
606 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
607 glDesc.fMSColorRenderbufferID = 0;
608 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
609 glDesc.fSampleCnt = desc.fSampleCnt;
610 glDesc.fOwnIDs = false;
611 GrGLIRect viewport;
612 viewport.fLeft = 0;
613 viewport.fBottom = 0;
614 viewport.fWidth = desc.fWidth;
615 viewport.fHeight = desc.fHeight;
616
617 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
618 if (desc.fStencilBits) {
619 GrGLStencilBuffer::Format format;
620 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
621 format.fPacked = false;
622 format.fStencilBits = desc.fStencilBits;
623 format.fTotalBits = desc.fStencilBits;
624 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
625 0,
626 desc.fWidth,
627 desc.fHeight,
628 desc.fSampleCnt,
629 format);
630 tgt->setStencilBuffer(sb);
631 sb->unref();
632 }
633 return tgt;
634}
635
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000636GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
637
638 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
639 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
640 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
641 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
642
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000643 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000644 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000645
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000646 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000647 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000648 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000649 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000650 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000651 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000652 } else {
653 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000654 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000655 }
656 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000657 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000658 }
659 // we don't know what the RB ids are without glGets and we don't care
660 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000661 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000662 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000663 if (desc.fStencilBits) {
664 GrGLStencilBuffer::Format format;
665 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
666 format.fPacked = false;
667 format.fStencilBits = desc.fStencilBits;
668 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000669 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
670 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000671 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000672 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000673 }
674
675 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000676 GrGLTexture::Desc texDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000677 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000678 return NULL;
679 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000680 texDesc.fWidth = desc.fWidth;
681 texDesc.fHeight = desc.fHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000682
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000683 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000684 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000685 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000686 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000687
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000688 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000689 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000690 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000691 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000692 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000693 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000694 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000695 } else {
696 GrGLIRect viewport;
697 viewport.fLeft = 0;
698 viewport.fBottom = 0;
699 viewport.fWidth = desc.fWidth;
700 viewport.fHeight = desc.fHeight;
701
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000702 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000703 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000704 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000705 }
706}
707
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000708
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000709////////////////////////////////////////////////////////////////////////////////
710
bsalomon@google.com6f379512011-11-16 20:36:03 +0000711void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
712 int left, int top, int width, int height,
713 GrPixelConfig config, const void* buffer,
714 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000715 if (NULL == buffer) {
716 return;
717 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000718 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
719
bsalomon@google.com6f379512011-11-16 20:36:03 +0000720 this->setSpareTextureUnit();
721 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
722 GrGLTexture::Desc desc;
723 desc.fConfig = glTex->config();
724 desc.fWidth = glTex->width();
725 desc.fHeight = glTex->height();
726 desc.fOrientation = glTex->orientation();
727 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000728
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000729 this->uploadTexData(desc, false,
730 left, top, width, height,
731 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000732}
733
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000734namespace {
735bool adjust_pixel_ops_params(int surfaceWidth,
736 int surfaceHeight,
737 size_t bpp,
738 int* left, int* top, int* width, int* height,
739 const void** data,
740 size_t* rowBytes) {
741 if (!*rowBytes) {
742 *rowBytes = *width * bpp;
743 }
744
745 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
746 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
747
748 if (!subRect.intersect(bounds)) {
749 return false;
750 }
751 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
752 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
753
754 *left = subRect.fLeft;
755 *top = subRect.fTop;
756 *width = subRect.width();
757 *height = subRect.height();
758 return true;
759}
760}
761
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000762bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
763 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000764 int left, int top, int width, int height,
765 GrPixelConfig dataConfig,
766 const void* data,
767 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000768 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000769
770 size_t bpp = GrBytesPerPixel(dataConfig);
771 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
772 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000773 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000774 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000775 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000776
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000777 // in case we need a temporary, trimmed copy of the src pixels
778 SkAutoSMalloc<128 * 128> tempStorage;
779
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000780 bool useTexStorage = isNewTexture &&
781 this->glCaps().fTexStorageSupport;
782 if (useTexStorage) {
783 if (kDesktop_GrGLBinding == this->glBinding()) {
784 // 565 is not a sized internal format on desktop GL. So on desktop
785 // with 565 we always use an unsized internal format to let the
786 // system pick the best sized format to convert the 565 data to.
787 // Since glTexStorage only allows sized internal formats we will
788 // instead fallback to glTexImage2D.
789 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
790 } else {
791 // ES doesn't allow paletted textures to be used with tex storage
792 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
793 }
794 }
795
796 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000797 GrGLenum externalFormat;
798 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000799 // glTexStorage requires sized internal formats on both desktop and ES. ES
800 // glTexImage requires an unsized format.
801 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
802 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000803 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000804 }
805
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000806 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000807 // paletted textures cannot be updated
808 return false;
809 }
810
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000811 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000812 * check whether to allocate a temporary buffer for flipping y or
813 * because our srcData has extra bytes past each row. If so, we need
814 * to trim those off here, since GL ES may not let us specify
815 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000816 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000817 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000818 bool swFlipY = false;
819 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000820 if (NULL != data) {
821 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
822 if (this->glCaps().fUnpackFlipYSupport) {
823 glFlipY = true;
824 } else {
825 swFlipY = true;
826 }
827 }
828 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
829 // can't use this for flipping, only non-neg values allowed. :(
830 if (rowBytes != trimRowBytes) {
831 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
832 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
833 restoreGLRowLength = true;
834 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000835 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000836 if (trimRowBytes != rowBytes || swFlipY) {
837 // copy data into our new storage, skipping the trailing bytes
838 size_t trimSize = height * trimRowBytes;
839 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000840 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000841 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000842 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000843 char* dst = (char*)tempStorage.reset(trimSize);
844 for (int y = 0; y < height; y++) {
845 memcpy(dst, src, trimRowBytes);
846 if (swFlipY) {
847 src -= rowBytes;
848 } else {
849 src += rowBytes;
850 }
851 dst += trimRowBytes;
852 }
853 // now point data to our copied version
854 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000855 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000856 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000857 if (glFlipY) {
858 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
859 }
860 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000861 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000862 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000863 if (isNewTexture &&
864 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000865 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000866 GrGLClearErr(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000867 if (useTexStorage) {
868 // We never resize or change formats of textures. We don't use
869 // mipmaps currently.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000870 GR_GL_CALL_NOERRCHECK(this->glInterface(),
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000871 TexStorage2D(GR_GL_TEXTURE_2D,
872 1, // levels
873 internalFormat,
874 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000875 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000876 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
877 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
878 kGrColorTableSize;
879 GR_GL_CALL_NOERRCHECK(this->glInterface(),
880 CompressedTexImage2D(GR_GL_TEXTURE_2D,
881 0, // level
882 internalFormat,
883 desc.fWidth,
884 desc.fHeight,
885 0, // border
886 imageSize,
887 data));
888 } else {
889 GR_GL_CALL_NOERRCHECK(this->glInterface(),
890 TexImage2D(GR_GL_TEXTURE_2D,
891 0, // level
892 internalFormat,
893 desc.fWidth, desc.fHeight,
894 0, // border
895 externalFormat, externalType,
896 data));
897 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000898 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000899 GrGLenum error = GR_GL_GET_ERROR(this->glInterface());
900 if (error != GR_GL_NO_ERROR) {
901 succeeded = false;
902 } else {
903 // if we have data and we used TexStorage to create the texture, we
904 // now upload with TexSubImage.
905 if (NULL != data && useTexStorage) {
906 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
907 0, // level
908 left, top,
909 width, height,
910 externalFormat, externalType,
911 data));
912 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000913 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000914 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000915 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000916 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000917 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000918 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
919 0, // level
920 left, top,
921 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000922 externalFormat, externalType, data));
923 }
924
925 if (restoreGLRowLength) {
926 GrAssert(this->glCaps().fUnpackRowLengthSupport);
927 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000928 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000929 if (glFlipY) {
930 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
931 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000932 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000933}
934
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935bool GrGpuGL::createRenderTargetObjects(int width, int height,
936 GrGLuint texID,
937 GrGLRenderTarget::Desc* desc) {
938 desc->fMSColorRenderbufferID = 0;
939 desc->fRTFBOID = 0;
940 desc->fTexFBOID = 0;
941 desc->fOwnIDs = true;
942
943 GrGLenum status;
944 GrGLint err;
945
bsalomon@google.comab15d612011-08-09 12:57:56 +0000946 GrGLenum msColorFormat = 0; // suppress warning
947
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000948 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000949 if (!desc->fTexFBOID) {
950 goto FAILED;
951 }
952
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000953
954 // If we are using multisampling we will create two FBOS. We render
955 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000956 if (desc->fSampleCnt > 0) {
957 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
958 goto FAILED;
959 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000960 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
961 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000962 if (!desc->fRTFBOID ||
963 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000964 !this->configToGLFormats(desc->fConfig,
965 // GLES requires sized internal formats
966 kES2_GrGLBinding == this->glBinding(),
967 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000968 goto FAILED;
969 }
970 } else {
971 desc->fRTFBOID = desc->fTexFBOID;
972 }
973
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000974 // below here we may bind the FBO
975 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000976 if (desc->fRTFBOID != desc->fTexFBOID) {
977 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000978 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000979 desc->fMSColorRenderbufferID));
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000980 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000981 GR_GL_CALL_NOERRCHECK(this->glInterface(),
982 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
983 desc->fSampleCnt,
984 msColorFormat,
985 width, height));
986 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000987 if (err != GR_GL_NO_ERROR) {
988 goto FAILED;
989 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000990 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
991 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000992 GR_GL_COLOR_ATTACHMENT0,
993 GR_GL_RENDERBUFFER,
994 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000995 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000996 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
997 goto FAILED;
998 }
999 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001000 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001001
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001002 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1003 GR_GL_COLOR_ATTACHMENT0,
1004 GR_GL_TEXTURE_2D,
1005 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001006 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1008 goto FAILED;
1009 }
1010
1011 return true;
1012
1013FAILED:
1014 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001015 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001016 }
1017 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001018 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001019 }
1020 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001021 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001022 }
1023 return false;
1024}
1025
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001026// good to set a break-point here to know when createTexture fails
1027static GrTexture* return_null_texture() {
1028// GrAssert(!"null texture");
1029 return NULL;
1030}
1031
1032#if GR_DEBUG
1033static size_t as_size_t(int x) {
1034 return x;
1035}
1036#endif
1037
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001038GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001039 const void* srcData,
1040 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001041
1042#if GR_COLLECT_STATS
1043 ++fStats.fTextureCreateCnt;
1044#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001045
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001046 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001047 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001048
bsalomon@google.com99621082011-11-15 16:47:16 +00001049 glTexDesc.fWidth = desc.fWidth;
1050 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001051 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001052 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001053
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001054 glRTDesc.fMSColorRenderbufferID = 0;
1055 glRTDesc.fRTFBOID = 0;
1056 glRTDesc.fTexFBOID = 0;
1057 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001058 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001059
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001060 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001061
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001062 const Caps& caps = this->getCaps();
1063
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001064 // We keep GrRenderTargets in GL's normal orientation so that they
1065 // can be drawn to by the outside world without the client having
1066 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001067 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001068 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001069
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001070 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1071 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1072 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1073 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001074 GrPrintf("AA RT requested but not supported on this platform.");
1075 }
1076
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001078 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1079 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001080 return return_null_texture();
1081 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001082 }
1083
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001084 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001085 if (renderTarget && this->glCaps().fTextureUsageSupport) {
1086 // provides a hint about how this texture will be used
1087 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1088 GR_GL_TEXTURE_USAGE,
1089 GR_GL_FRAMEBUFFER_ATTACHMENT));
1090 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001091 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001092 return return_null_texture();
1093 }
1094
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001095 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001096 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001097
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001098 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1099 // drivers have a bug where an FBO won't be complete if it includes a
1100 // texture that is not mipmap complete (considering the filter in use).
1101 GrGLTexture::TexParams initialTexParams;
1102 // we only set a subset here so invalidate first
1103 initialTexParams.invalidate();
1104 initialTexParams.fFilter = GR_GL_NEAREST;
1105 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1106 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1108 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001109 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001110 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1111 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001112 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1114 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001115 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001116 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1117 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001118 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001119 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1120 glTexDesc.fWidth, glTexDesc.fHeight,
1121 desc.fConfig, srcData, rowBytes)) {
1122 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1123 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001124 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001125
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001126 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001127 if (renderTarget) {
1128#if GR_COLLECT_STATS
1129 ++fStats.fRenderTargetCreateCnt;
1130#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001131 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1132 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 glTexDesc.fTextureID,
1134 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001136 return return_null_texture();
1137 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001138 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001140 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001141 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001142 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001143#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001144 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1145 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001146#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 return tex;
1148}
1149
1150namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001151void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1152 GrGLuint rb,
1153 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 // we shouldn't ever know one size and not the other
1155 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1156 (kUnknownBitCount == format->fTotalBits));
1157 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001158 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1160 (GrGLint*)&format->fStencilBits);
1161 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001162 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1164 (GrGLint*)&format->fTotalBits);
1165 format->fTotalBits += format->fStencilBits;
1166 } else {
1167 format->fTotalBits = format->fStencilBits;
1168 }
1169 }
1170}
1171}
1172
1173bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1174 int width, int height) {
1175
1176 // All internally created RTs are also textures. We don't create
1177 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1178 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001179 GrAssert(width >= rt->width());
1180 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181
1182 int samples = rt->numSamples();
1183 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001184 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 if (!sbID) {
1186 return false;
1187 }
1188
1189 GrGLStencilBuffer* sb = NULL;
1190
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001191 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001193 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 // we start with the last stencil format that succeeded in hopes
1195 // that we won't go through this loop more than once after the
1196 // first (painful) stencil creation.
1197 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001198 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001199 GrGLClearErr(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001200 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 // version on a GL that doesn't have an MSAA extension.
1202 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001203 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1204 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 GR_GL_RENDERBUFFER,
1206 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001207 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 width,
1209 height));
1210 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001211 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1212 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001213 sFmt.fInternalFormat,
1214 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 }
1216
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 if (err == GR_GL_NO_ERROR) {
1219 // After sized formats we attempt an unsized format and take whatever
1220 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001221 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001222 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001223 sb = new GrGLStencilBuffer(this, sbID, width, height,
1224 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1226 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001227 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229 return true;
1230 }
1231 sb->abandon(); // otherwise we lose sbID
1232 sb->unref();
1233 }
1234 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001235 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001236 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237}
1238
1239bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1240 GrRenderTarget* rt) {
1241 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1242
1243 GrGLuint fbo = glrt->renderFBOID();
1244
1245 if (NULL == sb) {
1246 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001247 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248 GR_GL_STENCIL_ATTACHMENT,
1249 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001250 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 GR_GL_DEPTH_ATTACHMENT,
1252 GR_GL_RENDERBUFFER, 0));
1253#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001254 GrGLenum status;
1255 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001256 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1257#endif
1258 }
1259 return true;
1260 } else {
1261 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1262 GrGLuint rb = glsb->renderbufferID();
1263
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001264 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001265 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1266 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001267 GR_GL_STENCIL_ATTACHMENT,
1268 GR_GL_RENDERBUFFER, rb));
1269 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001270 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001271 GR_GL_DEPTH_ATTACHMENT,
1272 GR_GL_RENDERBUFFER, rb));
1273 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001274 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001275 GR_GL_DEPTH_ATTACHMENT,
1276 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001278
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001279 GrGLenum status;
1280 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001281 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001282 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001283 GR_GL_STENCIL_ATTACHMENT,
1284 GR_GL_RENDERBUFFER, 0));
1285 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001286 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001287 GR_GL_DEPTH_ATTACHMENT,
1288 GR_GL_RENDERBUFFER, 0));
1289 }
1290 return false;
1291 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001292 return true;
1293 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001295}
1296
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001297////////////////////////////////////////////////////////////////////////////////
1298
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001299GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001300 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001301 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001303 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001304 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001305 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001306 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001307 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1308 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1309 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001310 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001311 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 // deleting bound buffer does implicit bind to 0
1313 fHWGeometryState.fVertexBuffer = NULL;
1314 return NULL;
1315 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001316 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 size, dynamic);
1318 fHWGeometryState.fVertexBuffer = vertexBuffer;
1319 return vertexBuffer;
1320 }
1321 return NULL;
1322}
1323
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001324GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001325 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001326 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001328 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1329 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001331 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1332 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1333 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001334 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001335 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 // deleting bound buffer does implicit bind to 0
1337 fHWGeometryState.fIndexBuffer = NULL;
1338 return NULL;
1339 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001340 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 size, dynamic);
1342 fHWGeometryState.fIndexBuffer = indexBuffer;
1343 return indexBuffer;
1344 }
1345 return NULL;
1346}
1347
reed@google.comac10a2d2010-12-22 21:39:39 +00001348void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001349 const GrDrawState& drawState = this->getDrawState();
1350 const GrGLRenderTarget* rt =
1351 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1352
1353 GrAssert(NULL != rt);
1354 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001355
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001356 GrGLIRect scissor;
1357 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001358 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001359 rect->width(), rect->height());
1360 if (scissor.contains(vp)) {
1361 rect = NULL;
1362 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 }
1364
1365 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001366 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001367 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001368 fHWBounds.fScissorRect = scissor;
1369 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001371 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 fHWBounds.fScissorEnabled = true;
1373 }
1374 } else {
1375 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001376 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 fHWBounds.fScissorEnabled = false;
1378 }
1379 }
1380}
1381
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001382void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001383 const GrDrawState& drawState = this->getDrawState();
1384 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001385 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001386 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001387
bsalomon@google.com74b98712011-11-11 19:46:16 +00001388 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001389 if (NULL != rect) {
1390 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001391 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001392 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001393 if (clippedRect.intersect(rtRect)) {
1394 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001395 } else {
1396 return;
1397 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001398 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001399 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001400 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001401
1402 GrGLfloat r, g, b, a;
1403 static const GrGLfloat scale255 = 1.f / 255.f;
1404 a = GrColorUnpackA(color) * scale255;
1405 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001406 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001407 scaleRGB *= a;
1408 }
1409 r = GrColorUnpackR(color) * scaleRGB;
1410 g = GrColorUnpackG(color) * scaleRGB;
1411 b = GrColorUnpackB(color) * scaleRGB;
1412
1413 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001414 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001415 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001416 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001417}
1418
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001419void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001420 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001421 return;
1422 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001423
1424 this->flushRenderTarget(&GrIRect::EmptyIRect());
1425
reed@google.comac10a2d2010-12-22 21:39:39 +00001426 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001427 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001428 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001429 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001430 GL_CALL(StencilMask(0xffffffff));
1431 GL_CALL(ClearStencil(0));
1432 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001433 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001434}
1435
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001436void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001437 const GrDrawState& drawState = this->getDrawState();
1438 const GrRenderTarget* rt = drawState.getRenderTarget();
1439 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001440
1441 // this should only be called internally when we know we have a
1442 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001443 GrAssert(NULL != rt->getStencilBuffer());
1444 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001445#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001446 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001447 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001448#else
1449 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001450 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001451 // turned into draws. Our contract on GrDrawTarget says that
1452 // changing the clip between stencil passes may or may not
1453 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001454 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001455#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001456 GrGLint value;
1457 if (insideClip) {
1458 value = (1 << (stencilBitCount - 1));
1459 } else {
1460 value = 0;
1461 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001462 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001463 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001464 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001465 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001466 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001467 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001468}
1469
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001470void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001471 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001472}
1473
bsalomon@google.comc4364992011-11-07 15:54:49 +00001474bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1475 int left, int top,
1476 int width, int height,
1477 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001478 size_t rowBytes) const {
1479 // if GL can do the flip then we'll never pay for it.
1480 if (this->glCaps().fPackFlipYSupport) {
1481 return false;
1482 }
1483
1484 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001485 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001486 if (this->glCaps().fPackRowLengthSupport) {
1487 return true;
1488 }
1489 // If we have to do memcpys to handle rowBytes then y-flip is free
1490 // Note the rowBytes might be tight to the passed in data, but if data
1491 // gets clipped in x to the target the rowBytes will no longer be tight.
1492 if (left >= 0 && (left + width) < renderTarget->width()) {
1493 return 0 == rowBytes ||
1494 GrBytesPerPixel(config) * width == rowBytes;
1495 } else {
1496 return false;
1497 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001498}
1499
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001500bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001501 int left, int top,
1502 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001503 GrPixelConfig config,
1504 void* buffer,
1505 size_t rowBytes,
1506 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001507 GrGLenum format;
1508 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001509 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001510 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001512 size_t bpp = GrBytesPerPixel(config);
1513 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1514 &left, &top, &width, &height,
1515 const_cast<const void**>(&buffer),
1516 &rowBytes)) {
1517 return false;
1518 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001519
bsalomon@google.comc6980972011-11-02 19:57:21 +00001520 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001521 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001522 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001523 switch (tgt->getResolveType()) {
1524 case GrGLRenderTarget::kCantResolve_ResolveType:
1525 return false;
1526 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001527 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001528 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001529 break;
1530 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001531 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001532 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001533 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1534 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001535 break;
1536 default:
1537 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001538 }
1539
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001540 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001541
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001542 // the read rect is viewport-relative
1543 GrGLIRect readRect;
1544 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001545
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001546 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001547 if (0 == rowBytes) {
1548 rowBytes = tightRowBytes;
1549 }
1550 size_t readDstRowBytes = tightRowBytes;
1551 void* readDst = buffer;
1552
1553 // determine if GL can read using the passed rowBytes or if we need
1554 // a scratch buffer.
1555 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1556 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001557 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001558 GrAssert(!(rowBytes % sizeof(GrColor)));
1559 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1560 readDstRowBytes = rowBytes;
1561 } else {
1562 scratch.reset(tightRowBytes * height);
1563 readDst = scratch.get();
1564 }
1565 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001566 if (!invertY && this->glCaps().fPackFlipYSupport) {
1567 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1568 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001569 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1570 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001571 format, type, readDst));
1572 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001573 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001574 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1575 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001576 if (!invertY && this->glCaps().fPackFlipYSupport) {
1577 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1578 invertY = true;
1579 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001580
1581 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001582 // API presents top-to-bottom. We must preserve the padding contents. Note
1583 // that the above readPixels did not overwrite the padding.
1584 if (readDst == buffer) {
1585 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001586 if (!invertY) {
1587 scratch.reset(tightRowBytes);
1588 void* tmpRow = scratch.get();
1589 // flip y in-place by rows
1590 const int halfY = height >> 1;
1591 char* top = reinterpret_cast<char*>(buffer);
1592 char* bottom = top + (height - 1) * rowBytes;
1593 for (int y = 0; y < halfY; y++) {
1594 memcpy(tmpRow, top, tightRowBytes);
1595 memcpy(top, bottom, tightRowBytes);
1596 memcpy(bottom, tmpRow, tightRowBytes);
1597 top += rowBytes;
1598 bottom -= rowBytes;
1599 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001600 }
1601 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001602 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001603 // copy from readDst to buffer while flipping y
1604 const int halfY = height >> 1;
1605 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001606 char* dst = reinterpret_cast<char*>(buffer);
1607 if (!invertY) {
1608 dst += (height-1) * rowBytes;
1609 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001610 for (int y = 0; y < height; y++) {
1611 memcpy(dst, src, tightRowBytes);
1612 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001613 if (invertY) {
1614 dst += rowBytes;
1615 } else {
1616 dst -= rowBytes;
1617 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001618 }
1619 }
1620 return true;
1621}
1622
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001623void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001624
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001625 GrGLRenderTarget* rt =
1626 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1627 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001628
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001629 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001630 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001631 #if GR_COLLECT_STATS
1632 ++fStats.fRenderTargetChngCnt;
1633 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001634 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001635 GrGLenum status;
1636 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001637 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001638 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001639 }
1640 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001641 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001642 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001643 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001644 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001645 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 fHWBounds.fViewportRect = vp;
1647 }
1648 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001649 if (NULL == bound || !bound->isEmpty()) {
1650 rt->flagAsNeedingResolve(bound);
1651 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001652}
1653
twiz@google.com0f31ca72011-03-18 17:38:11 +00001654GrGLenum gPrimitiveType2GLMode[] = {
1655 GR_GL_TRIANGLES,
1656 GR_GL_TRIANGLE_STRIP,
1657 GR_GL_TRIANGLE_FAN,
1658 GR_GL_POINTS,
1659 GR_GL_LINES,
1660 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001661};
1662
bsalomon@google.comd302f142011-03-03 13:54:13 +00001663#define SWAP_PER_DRAW 0
1664
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001665#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001666 #if GR_MAC_BUILD
1667 #include <AGL/agl.h>
1668 #elif GR_WIN32_BUILD
1669 void SwapBuf() {
1670 DWORD procID = GetCurrentProcessId();
1671 HWND hwnd = GetTopWindow(GetDesktopWindow());
1672 while(hwnd) {
1673 DWORD wndProcID = 0;
1674 GetWindowThreadProcessId(hwnd, &wndProcID);
1675 if(wndProcID == procID) {
1676 SwapBuffers(GetDC(hwnd));
1677 }
1678 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1679 }
1680 }
1681 #endif
1682#endif
1683
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001684void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1685 uint32_t startVertex,
1686 uint32_t startIndex,
1687 uint32_t vertexCount,
1688 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001689 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1690
twiz@google.com0f31ca72011-03-18 17:38:11 +00001691 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001692
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001693 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1694 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1695
1696 // our setupGeometry better have adjusted this to zero since
1697 // DrawElements always draws from the begining of the arrays for idx 0.
1698 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001699
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001700 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1701 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001702#if SWAP_PER_DRAW
1703 glFlush();
1704 #if GR_MAC_BUILD
1705 aglSwapBuffers(aglGetCurrentContext());
1706 int set_a_break_pt_here = 9;
1707 aglSwapBuffers(aglGetCurrentContext());
1708 #elif GR_WIN32_BUILD
1709 SwapBuf();
1710 int set_a_break_pt_here = 9;
1711 SwapBuf();
1712 #endif
1713#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001714}
1715
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001716void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1717 uint32_t startVertex,
1718 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001719 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1720
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001721 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1722
1723 // our setupGeometry better have adjusted this to zero.
1724 // DrawElements doesn't take an offset so we always adjus the startVertex.
1725 GrAssert(0 == startVertex);
1726
1727 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1728 // account for startVertex in the DrawElements case. So we always
1729 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001730 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001731#if SWAP_PER_DRAW
1732 glFlush();
1733 #if GR_MAC_BUILD
1734 aglSwapBuffers(aglGetCurrentContext());
1735 int set_a_break_pt_here = 9;
1736 aglSwapBuffers(aglGetCurrentContext());
1737 #elif GR_WIN32_BUILD
1738 SwapBuf();
1739 int set_a_break_pt_here = 9;
1740 SwapBuf();
1741 #endif
1742#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001743}
1744
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001745void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001746
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001747 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001748 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001750 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1751 rt->renderFBOID()));
1752 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1753 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 #if GR_COLLECT_STATS
1755 ++fStats.fRenderTargetChngCnt;
1756 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001757 // make sure we go through flushRenderTarget() since we've modified
1758 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001759 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001760 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001761 const GrIRect dirtyRect = rt->getResolveRect();
1762 GrGLIRect r;
1763 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1764 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001765
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001766 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001767 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001768 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1769 GL_CALL(Scissor(r.fLeft, r.fBottom,
1770 r.fWidth, r.fHeight));
1771 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001772 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001773 fHWBounds.fScissorEnabled = true;
1774 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001775 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001776 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001777 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1778 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001779 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001780 int right = r.fLeft + r.fWidth;
1781 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001782 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1783 r.fLeft, r.fBottom, right, top,
1784 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001785 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001786 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001787 }
1788}
1789
twiz@google.com0f31ca72011-03-18 17:38:11 +00001790static const GrGLenum grToGLStencilFunc[] = {
1791 GR_GL_ALWAYS, // kAlways_StencilFunc
1792 GR_GL_NEVER, // kNever_StencilFunc
1793 GR_GL_GREATER, // kGreater_StencilFunc
1794 GR_GL_GEQUAL, // kGEqual_StencilFunc
1795 GR_GL_LESS, // kLess_StencilFunc
1796 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1797 GR_GL_EQUAL, // kEqual_StencilFunc,
1798 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001799};
1800GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1801GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1802GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1803GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1804GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1805GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1806GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1807GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1808GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1809
twiz@google.com0f31ca72011-03-18 17:38:11 +00001810static const GrGLenum grToGLStencilOp[] = {
1811 GR_GL_KEEP, // kKeep_StencilOp
1812 GR_GL_REPLACE, // kReplace_StencilOp
1813 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1814 GR_GL_INCR, // kIncClamp_StencilOp
1815 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1816 GR_GL_DECR, // kDecClamp_StencilOp
1817 GR_GL_ZERO, // kZero_StencilOp
1818 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001819};
1820GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1821GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1822GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1823GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1824GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1825GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1826GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1827GR_STATIC_ASSERT(6 == kZero_StencilOp);
1828GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1829
reed@google.comac10a2d2010-12-22 21:39:39 +00001830void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001831 const GrDrawState& drawState = this->getDrawState();
1832
1833 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001834
1835 // use stencil for clipping if clipping is enabled and the clip
1836 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001837 bool stencilClip = fClipInStencil && drawState.isClipState();
1838 bool drawClipToStencil =
1839 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001840 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1841 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001842 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1843 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001844
1845 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001846
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001847 // we can't simultaneously perform stencil-clipping and
1848 // modify the stencil clip
1849 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001850
bsalomon@google.comd302f142011-03-03 13:54:13 +00001851 if (settings->isDisabled()) {
1852 if (stencilClip) {
1853 settings = &gClipStencilSettings;
1854 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001855 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001856
1857 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001858 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001860 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001862 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001863 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1864 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1865 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1866 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1867 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1868 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1869 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1870 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001871 }
1872 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001873 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001874 GrStencilBuffer* stencilBuffer =
1875 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001876 if (NULL != stencilBuffer) {
1877 stencilBits = stencilBuffer->bits();
1878 }
1879 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001880 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001881
1882 GrGLuint clipStencilMask = 0;
1883 GrGLuint userStencilMask = ~0;
1884 if (stencilBits > 0) {
1885 clipStencilMask = 1 << (stencilBits - 1);
1886 userStencilMask = clipStencilMask - 1;
1887 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001888
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001889 unsigned int frontRef = settings->frontFuncRef();
1890 unsigned int frontMask = settings->frontFuncMask();
1891 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001892 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001893
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001894 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001895 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1896 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001898 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001899 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001901 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001902 stencilClip,
1903 clipStencilMask,
1904 userStencilMask,
1905 &frontRef,
1906 &frontMask);
1907 frontWriteMask &= userStencilMask;
1908 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001909 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001910 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001911 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001912 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001913 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001914 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001915 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001916 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001917 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001918 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001919
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001920 unsigned int backRef = settings->backFuncRef();
1921 unsigned int backMask = settings->backFuncMask();
1922 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923
1924
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001925 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001926 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1927 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001928 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001929 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001930 stencilClip, settings->backFunc())];
1931 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001932 stencilClip,
1933 clipStencilMask,
1934 userStencilMask,
1935 &backRef,
1936 &backMask);
1937 backWriteMask &= userStencilMask;
1938 }
1939
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001940 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1941 frontRef, frontMask));
1942 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1943 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1944 backRef, backMask));
1945 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1946 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001947 grToGLStencilOp[settings->frontFailOp()],
1948 grToGLStencilOp[settings->frontPassOp()],
1949 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001950
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001951 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001952 grToGLStencilOp[settings->backFailOp()],
1953 grToGLStencilOp[settings->backPassOp()],
1954 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001955 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001956 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1957 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001958 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1959 grToGLStencilOp[settings->frontPassOp()],
1960 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001961 }
1962 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001963 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001964 fHWStencilClip = stencilClip;
1965 }
1966}
1967
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001968void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001969 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001970 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001971 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1972 // smooth lines.
1973
1974 // we prefer smooth lines over multisampled lines
1975 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001976 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001977 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001978 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001980 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001981 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001982 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001983 fHWAAState.fSmoothLineEnabled = false;
1984 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001985 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001986 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001987 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001988 fHWAAState.fMSAAEnabled = false;
1989 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001990 } else if (rt->isMultisampled() &&
1991 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001992 fHWAAState.fMSAAEnabled) {
1993 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001994 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001995 fHWAAState.fMSAAEnabled = false;
1996 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001997 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001998 fHWAAState.fMSAAEnabled = true;
1999 }
2000 }
2001 }
2002}
2003
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002004void GrGpuGL::flushBlend(GrPrimitiveType type,
2005 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002006 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002007 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002008 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002009 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002010 fHWBlendDisabled = false;
2011 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002012 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
2013 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002014 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2015 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002016 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002017 }
2018 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002019 // any optimization to disable blending should
2020 // have already been applied and tweaked the coeffs
2021 // to (1, 0).
2022 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2023 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002024 if (fHWBlendDisabled != blendOff) {
2025 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002026 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002027 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002028 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002029 }
2030 fHWBlendDisabled = blendOff;
2031 }
2032 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002033 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2034 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002035 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2036 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002037 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002038 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002039 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002040 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2041 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002042 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002043
2044 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002045 GrColorUnpackR(blendConst) / 255.f,
2046 GrColorUnpackG(blendConst) / 255.f,
2047 GrColorUnpackB(blendConst) / 255.f,
2048 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002049 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002050 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002051 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002052 }
2053 }
2054 }
2055}
2056
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002057namespace {
2058
2059unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002060 switch (filter) {
2061 case GrSamplerState::kBilinear_Filter:
2062 case GrSamplerState::k4x4Downsample_Filter:
2063 return GR_GL_LINEAR;
2064 case GrSamplerState::kNearest_Filter:
2065 case GrSamplerState::kConvolution_Filter:
2066 return GR_GL_NEAREST;
2067 default:
2068 GrAssert(!"Unknown filter type");
2069 return GR_GL_LINEAR;
2070 }
2071}
2072
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002073const GrGLenum* get_swizzle(GrPixelConfig config,
2074 const GrSamplerState& sampler) {
2075 if (GrPixelConfigIsAlphaOnly(config)) {
2076 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2077 GR_GL_ALPHA, GR_GL_ALPHA };
2078 return gAlphaSmear;
2079 } else if (sampler.swapsRAndB()) {
2080 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2081 GR_GL_RED, GR_GL_ALPHA };
2082 return gRedBlueSwap;
2083 } else {
2084 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2085 GR_GL_BLUE, GR_GL_ALPHA };
2086 return gStraight;
2087 }
2088}
2089
2090void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2091 // should add texparameteri to interface to make 1 instead of 4 calls here
2092 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2093 GR_GL_TEXTURE_SWIZZLE_R,
2094 swizzle[0]));
2095 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2096 GR_GL_TEXTURE_SWIZZLE_G,
2097 swizzle[1]));
2098 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2099 GR_GL_TEXTURE_SWIZZLE_B,
2100 swizzle[2]));
2101 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2102 GR_GL_TEXTURE_SWIZZLE_A,
2103 swizzle[3]));
2104}
2105}
2106
bsalomon@google.comffca4002011-02-22 20:34:01 +00002107bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002108
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002109 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002110 // GrGpu::setupClipAndFlushState should have already checked this
2111 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002112 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002113
tomhudson@google.com93813632011-10-27 20:21:16 +00002114 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002115 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002116 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002117 GrGLTexture* nextTexture =
2118 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002119
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002120 // true for now, but maybe not with GrEffect.
2121 GrAssert(NULL != nextTexture);
2122 // if we created a rt/tex and rendered to it without using a
2123 // texture and now we're texuring from the rt it will still be
2124 // the last bound texture, but it needs resolving. So keep this
2125 // out of the "last != next" check.
2126 GrGLRenderTarget* texRT =
2127 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2128 if (NULL != texRT) {
2129 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002130 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002131
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002132 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002133 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002134 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002135 #if GR_COLLECT_STATS
2136 ++fStats.fTextureChngCnt;
2137 #endif
2138 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002139 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002140 // The texture matrix has to compensate for texture width/height
2141 // and NPOT-embedded-in-POT
2142 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002143 }
2144
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002145 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002146 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002147 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002148 nextTexture->getCachedTexParams(&timestamp);
2149 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002150 GrGLTexture::TexParams newTexParams;
2151
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002152 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002153
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002154 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002155 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2156 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002157 memcpy(newTexParams.fSwizzleRGBA,
2158 get_swizzle(nextTexture->config(), sampler),
2159 sizeof(newTexParams.fSwizzleRGBA));
2160 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002161 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002162 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002163 GR_GL_TEXTURE_MAG_FILTER,
2164 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002166 GR_GL_TEXTURE_MIN_FILTER,
2167 newTexParams.fFilter));
2168 }
2169 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2170 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002171 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002172 GR_GL_TEXTURE_WRAP_S,
2173 newTexParams.fWrapS));
2174 }
2175 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2176 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002177 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002178 GR_GL_TEXTURE_WRAP_T,
2179 newTexParams.fWrapT));
2180 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002181 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002182 (setAll ||
2183 memcmp(newTexParams.fSwizzleRGBA,
2184 oldTexParams.fSwizzleRGBA,
2185 sizeof(newTexParams.fSwizzleRGBA)))) {
2186 setTextureUnit(s);
2187 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2188 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002189 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002190 nextTexture->setCachedTexParams(newTexParams,
2191 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 }
2193 }
2194
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002195 GrIRect* rect = NULL;
2196 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002197 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002198 fClip.hasConservativeBounds()) {
2199 fClip.getConservativeBounds().roundOut(&clipBounds);
2200 rect = &clipBounds;
2201 }
2202 this->flushRenderTarget(rect);
2203 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002204
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002205 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2206 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002207 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002208 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002209 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002210 }
2211 }
2212
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002213 if (drawState->isColorWriteDisabled() !=
2214 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002215 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002216 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002217 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002218 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002219 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002220 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002221 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002222 }
2223
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002224 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2225 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002226 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002227 GL_CALL(Enable(GR_GL_CULL_FACE));
2228 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002229 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002230 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002231 GL_CALL(Enable(GR_GL_CULL_FACE));
2232 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002233 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002234 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002235 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002236 break;
2237 default:
2238 GrCrash("Unknown draw face.");
2239 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002240 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002241 }
2242
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002243#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002244 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002245 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002246 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002247 NULL == drawState->getRenderTarget() ||
2248 NULL == drawState->getTexture(s) ||
2249 drawState->getTexture(s)->asRenderTarget() !=
2250 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002251 }
2252#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002253
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002254 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002255
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002256 // This copy must happen after flushStencil() is called. flushStencil()
2257 // relies on detecting when the kModifyStencilClip_StateBit state has
2258 // changed since the last draw.
2259 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002260 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002261}
2262
2263void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002264 if (fHWGeometryState.fVertexBuffer != buffer) {
2265 fHWGeometryState.fArrayPtrsDirty = true;
2266 fHWGeometryState.fVertexBuffer = buffer;
2267 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002268}
2269
2270void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002271 if (fHWGeometryState.fVertexBuffer == buffer) {
2272 // deleting bound buffer does implied bind to 0
2273 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002274 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002275 }
2276}
2277
2278void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002279 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002280}
2281
2282void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002283 if (fHWGeometryState.fIndexBuffer == buffer) {
2284 // deleting bound buffer does implied bind to 0
2285 fHWGeometryState.fIndexBuffer = NULL;
2286 }
2287}
2288
reed@google.comac10a2d2010-12-22 21:39:39 +00002289void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2290 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002291 GrDrawState* drawState = this->drawState();
2292 if (drawState->getRenderTarget() == renderTarget) {
2293 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002294 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002295 if (fHWDrawState.getRenderTarget() == renderTarget) {
2296 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002297 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002298}
2299
2300void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002301 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002302 GrDrawState* drawState = this->drawState();
2303 if (drawState->getTexture(s) == texture) {
2304 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002305 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002306 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002307 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002308 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002309 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002310 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002311}
2312
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002313bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2314 bool getSizedInternalFormat,
2315 GrGLenum* internalFormat,
2316 GrGLenum* externalFormat,
2317 GrGLenum* externalType) {
2318 GrGLenum dontCare;
2319 if (NULL == internalFormat) {
2320 internalFormat = &dontCare;
2321 }
2322 if (NULL == externalFormat) {
2323 externalFormat = &dontCare;
2324 }
2325 if (NULL == externalType) {
2326 externalType = &dontCare;
2327 }
2328
reed@google.comac10a2d2010-12-22 21:39:39 +00002329 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002330 case kRGBA_8888_PM_GrPixelConfig:
2331 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002332 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002333 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002334 if (getSizedInternalFormat) {
2335 *internalFormat = GR_GL_RGBA8;
2336 } else {
2337 *internalFormat = GR_GL_RGBA;
2338 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002339 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002340 break;
2341 case kBGRA_8888_PM_GrPixelConfig:
2342 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002343 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002344 return false;
2345 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002346 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002347 if (getSizedInternalFormat) {
2348 *internalFormat = GR_GL_BGRA8;
2349 } else {
2350 *internalFormat = GR_GL_BGRA;
2351 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002352 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002353 if (getSizedInternalFormat) {
2354 *internalFormat = GR_GL_RGBA8;
2355 } else {
2356 *internalFormat = GR_GL_RGBA;
2357 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002358 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002359 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002360 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002361 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002362 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002363 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002364 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002365 if (getSizedInternalFormat) {
2366 if (this->glBinding() == kDesktop_GrGLBinding) {
2367 return false;
2368 } else {
2369 *internalFormat = GR_GL_RGB565;
2370 }
2371 } else {
2372 *internalFormat = GR_GL_RGB;
2373 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002374 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002375 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002376 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002377 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002378 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002379 if (getSizedInternalFormat) {
2380 *internalFormat = GR_GL_RGBA4;
2381 } else {
2382 *internalFormat = GR_GL_RGBA;
2383 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002384 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002385 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002386 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002387 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002388 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002389 // glCompressedTexImage doesn't take external params
2390 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002391 // no sized/unsized internal format distinction here
2392 *internalFormat = GR_GL_PALETTE8_RGBA8;
2393 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002394 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002395 } else {
2396 return false;
2397 }
2398 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002399 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002400 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002401 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002402 if (getSizedInternalFormat) {
2403 *internalFormat = GR_GL_ALPHA8;
2404 } else {
2405 *internalFormat = GR_GL_ALPHA;
2406 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002407 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002408 break;
2409 default:
2410 return false;
2411 }
2412 return true;
2413}
2414
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002415void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002416 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002417 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002418 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002419 fActiveTextureUnitIdx = unit;
2420 }
2421}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002422
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002423void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002424 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002425 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002426 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2427 }
2428}
2429
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002430void GrGpuGL::resetDirtyFlags() {
2431 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2432}
2433
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434void GrGpuGL::setBuffers(bool indexed,
2435 int* extraVertexOffset,
2436 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002437
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002438 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002439
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002440 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2441
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002442 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002443 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002444 case kBuffer_GeometrySrcType:
2445 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002446 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002447 break;
2448 case kArray_GeometrySrcType:
2449 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002450 this->finalizeReservedVertices();
2451 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2452 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002453 break;
2454 default:
2455 vbuf = NULL; // suppress warning
2456 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002457 }
2458
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459 GrAssert(NULL != vbuf);
2460 GrAssert(!vbuf->isLocked());
2461 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002462 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002463 fHWGeometryState.fArrayPtrsDirty = true;
2464 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002465 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002466
2467 if (indexed) {
2468 GrAssert(NULL != extraIndexOffset);
2469
2470 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002471 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002472 case kBuffer_GeometrySrcType:
2473 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002474 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002475 break;
2476 case kArray_GeometrySrcType:
2477 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002478 this->finalizeReservedIndices();
2479 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2480 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002481 break;
2482 default:
2483 ibuf = NULL; // suppress warning
2484 GrCrash("Unknown geometry src type!");
2485 }
2486
2487 GrAssert(NULL != ibuf);
2488 GrAssert(!ibuf->isLocked());
2489 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002490 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002491 fHWGeometryState.fIndexBuffer = ibuf;
2492 }
2493 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002494}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002495
2496int GrGpuGL::getMaxEdges() const {
2497 // FIXME: This is a pessimistic estimate based on how many other things
2498 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002499 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2500 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002501}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002502
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002503void GrGpuGL::GLCaps::print() const {
2504 for (int i = 0; i < fStencilFormats.count(); ++i) {
2505 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2506 i,
2507 fStencilFormats[i].fStencilBits,
2508 fStencilFormats[i].fTotalBits);
2509 }
2510
2511 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2512 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2513 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2514 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2515 static const char* gMSFBOExtStr[] = {
2516 "None",
2517 "ARB",
2518 "EXT",
2519 "Apple",
2520 };
2521 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002522 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002523 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2524 }
2525 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2526 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002527 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002528 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002529 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002530 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002531 (fTextureSwizzleSupport ? "YES": "NO"));
2532 GrPrintf("Unpack Row length support: %s\n",
2533 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002534 GrPrintf("Unpack Flip Y support: %s\n",
2535 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002536 GrPrintf("Pack Row length support: %s\n",
2537 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002538 GrPrintf("Pack Flip Y support: %s\n",
2539 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002540}