blob: f1bc63dac4c4f2c49b48b4554f25d64cb908bd70 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
31#else
32 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
twiz@google.com0f31ca72011-03-18 17:38:11 +000037static const GrGLenum gXfermodeCoeff2Blend[] = {
38 GR_GL_ZERO,
39 GR_GL_ONE,
40 GR_GL_SRC_COLOR,
41 GR_GL_ONE_MINUS_SRC_COLOR,
42 GR_GL_DST_COLOR,
43 GR_GL_ONE_MINUS_DST_COLOR,
44 GR_GL_SRC_ALPHA,
45 GR_GL_ONE_MINUS_SRC_ALPHA,
46 GR_GL_DST_ALPHA,
47 GR_GL_ONE_MINUS_DST_ALPHA,
48 GR_GL_CONSTANT_COLOR,
49 GR_GL_ONE_MINUS_CONSTANT_COLOR,
50 GR_GL_CONSTANT_ALPHA,
51 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000052
53 // extended blend coeffs
54 GR_GL_SRC1_COLOR,
55 GR_GL_ONE_MINUS_SRC1_COLOR,
56 GR_GL_SRC1_ALPHA,
57 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000058};
59
bsalomon@google.com271cffc2011-05-20 14:13:56 +000060bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000061 static const bool gCoeffReferencesBlendConst[] = {
62 false,
63 false,
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 true,
73 true,
74 true,
75 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000076
77 // extended blend coeffs
78 false,
79 false,
80 false,
81 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000082 };
83 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000084 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
85
86 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
87 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
88 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
89 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
90 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
91 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
92 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
93 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
94 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
95 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
96 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
97 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
98 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
99 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
100
101 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
102 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
103 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
104 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
105
106 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
107 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000108}
109
reed@google.comac10a2d2010-12-22 21:39:39 +0000110///////////////////////////////////////////////////////////////////////////////
111
bsalomon@google.comd302f142011-03-03 13:54:13 +0000112void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
113 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000114 GrMatrix* matrix) {
115 GrAssert(NULL != texture);
116 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000117 GrGLTexture::Orientation orientation = texture->orientation();
118 if (GrGLTexture::kBottomUp_Orientation == orientation) {
119 GrMatrix invY;
120 invY.setAll(GR_Scalar1, 0, 0,
121 0, -GR_Scalar1, GR_Scalar1,
122 0, 0, GrMatrix::I()[8]);
123 matrix->postConcat(invY);
124 } else {
125 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
126 }
127}
128
bsalomon@google.comd302f142011-03-03 13:54:13 +0000129bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000130 const GrSamplerState& sampler) {
131 GrAssert(NULL != texture);
132 if (!sampler.getMatrix().isIdentity()) {
133 return false;
134 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000135 GrGLTexture::Orientation orientation = texture->orientation();
136 if (GrGLTexture::kBottomUp_Orientation == orientation) {
137 return false;
138 } else {
139 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
140 }
141 return true;
142}
143
144///////////////////////////////////////////////////////////////////////////////
145
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000146static bool gPrintStartupSpew;
147
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000148static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000149
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000150 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000151
twiz@google.com0f31ca72011-03-18 17:38:11 +0000152 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000153 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
154 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000155 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000156 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
157 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000158 // some implementations require texture to be mip-map complete before
159 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000160 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_TEXTURE_MIN_FILTER,
162 GR_GL_NEAREST));
163 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
164 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
165 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
166 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
167 GR_GL_COLOR_ATTACHMENT0,
168 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000169 GrGLenum status;
170 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
172 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000173
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000174 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000175}
176
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000177GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000178
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000179 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000180
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000181 gl->ref();
182 fGL = gl;
183 fGLBinding = glBinding;
184 switch (glBinding) {
185 case kDesktop_GrGLBinding:
186 GrAssert(gl->supportsDesktop());
187 break;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000188 case kES2_GrGLBinding:
189 GrAssert(gl->supportsES2());
190 break;
191 default:
192 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000193 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000194
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000195 GrGLClearErr(fGL);
196
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000197 const GrGLubyte* ext;
198 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000199 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000200 const GrGLubyte* vendor;
201 const GrGLubyte* renderer;
202 const GrGLubyte* version;
203 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
204 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
205 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000206 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
207 this);
208 GrPrintf("------ VENDOR %s\n", vendor);
209 GrPrintf("------ RENDERER %s\n", renderer);
210 GrPrintf("------ VERSION %s\n", version);
211 GrPrintf("------ EXTENSIONS\n %s \n", ext);
212 }
213
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000214 fGLVersion = GrGLGetVersion(gl);
215 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000216 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000217
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000218 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000219
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000220 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000221
bsalomon@google.comfe676522011-06-17 18:12:21 +0000222 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000223}
224
225GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000226 // This must be called by before the GrDrawTarget destructor
227 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000228 // This subclass must do this before the base class destructor runs
229 // since we will unref the GrGLInterface.
230 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000231 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000232}
233
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234///////////////////////////////////////////////////////////////////////////////
235
236static const GrGLuint kUnknownBitCount = ~0;
237
238void GrGpuGL::initCaps() {
239 GrGLint maxTextureUnits;
240 // check FS and fixed-function texture unit limits
241 // we only use textures in the fragment stage currently.
242 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000243 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
244 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000245 if (kES2_GrGLBinding != this->glBinding()) {
246 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000247 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000248 }
249 if (kES2_GrGLBinding == this->glBinding()) {
250 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
251 &fGLCaps.fMaxFragmentUniformVectors);
252 } else if (kDesktop_GrGLBinding != this->glBinding()) {
253 GrGLint max;
254 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
255 fGLCaps.fMaxFragmentUniformVectors = max / 4;
256 } else {
257 fGLCaps.fMaxFragmentUniformVectors = 16;
258 }
259
260 GrGLint numFormats;
261 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
262 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
263 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
264 for (int i = 0; i < numFormats; ++i) {
265 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
266 fCaps.f8BitPaletteSupport = true;
267 break;
268 }
269 }
270
271 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000272 // we could also look for GL_ATI_separate_stencil extension or
273 // GL_EXT_stencil_two_side but they use different function signatures
274 // than GL2.0+ (and than each other).
275 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
276 // supported on GL 1.4 and higher or by extension
277 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
278 this->hasExtension("GL_EXT_stencil_wrap");
279 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000280 // ES 2 has two sided stencil and stencil wrap
281 fCaps.fTwoSidedStencilSupport = true;
282 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000283 }
284
285 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000286 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000287 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000288 fGLCaps.fRGBA8RenderbufferSupport =
289 this->hasExtension("GL_OES_rgb8_rgba8") ||
290 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000291 }
292
293
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000295 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
296 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 } else {
298 bool hasBGRAExt = false;
299 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000300 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000302 fGLCaps.fBGRAFormatSupport = true;
303 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000304 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000305 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000306 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000307 }
308
309 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000310 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000311 this->hasExtension("GL_ARB_texture_swizzle");
312 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000313 fGLCaps.fTextureSwizzleSupport = false;
314 }
315
316 if (kDesktop_GrGLBinding == this->glBinding()) {
317 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000318 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000319 fGLCaps.fPackRowLengthSupport = true;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000320 fGLCaps.fPackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000321 } else {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000322 fGLCaps.fUnpackRowLengthSupport =this->hasExtension("GL_EXT_unpack_subimage");
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000323 fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy");
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000324 // no extension for pack row length
325 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000326 fGLCaps.fPackFlipYSupport =
327 this->hasExtension("GL_ANGLE_pack_reverse_row_order");
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000328 }
329
330 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000331 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
332 // extension includes glMapBuffer.
333 } else {
334 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
335 }
336
337 if (kDesktop_GrGLBinding == this->glBinding()) {
338 if (fGLVersion >= GR_GL_VER(2,0) ||
339 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
340 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000341 } else {
342 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000343 }
344 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000345 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000346 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000347 }
348
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000349 fGLCaps.fTextureUsageSupport = (kES2_GrGLBinding == this->glBinding()) &&
350 this->hasExtension("GL_ANGLE_texture_usage");
351
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000352 // Tex storage is in desktop 4.2 and can be an extension to desktop or ES.
353 fGLCaps.fTexStorageSupport = (kDesktop_GrGLBinding == this->glBinding() &&
354 fGLVersion >= GR_GL_VER(4,2)) ||
355 this->hasExtension("GL_ARB_texture_storage") ||
356 this->hasExtension("GL_EXT_texture_storage");
357
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000358 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
359
360 ////////////////////////////////////////////////////////////////////////////
361 // Experiments to determine limitations that can't be queried.
362 // TODO: Make these a preprocess that generate some compile time constants.
363 // TODO: probe once at startup, rather than once per context creation.
364
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000365 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
366 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
367 // Our render targets are always created with textures as the color
368 // attachment, hence this min:
369 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
370
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000371 this->initFSAASupport();
372 this->initStencilFormats();
373}
374
375void GrGpuGL::initFSAASupport() {
376 // TODO: Get rid of GrAALevel and use # samples directly.
377 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
378 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
379 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
380 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
381 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
382
383 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
384 if (kDesktop_GrGLBinding != this->glBinding()) {
385 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
386 // chrome's extension is equivalent to the EXT msaa
387 // and fbo_blit extensions.
388 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
389 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
390 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
391 }
392 } else {
393 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
394 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
395 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
396 this->hasExtension("GL_EXT_framebuffer_blit")) {
397 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
398 }
399 }
400
401 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
402 GrGLint maxSamples;
403 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
404 if (maxSamples > 1 ) {
405 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
406 fGLCaps.fAASamples[kLow_GrAALevel] =
407 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
408 fGLCaps.fAASamples[kMed_GrAALevel] =
409 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
410 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
411 }
412 }
413 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
414}
415
416void GrGpuGL::initStencilFormats() {
417
418 // Build up list of legal stencil formats (though perhaps not supported on
419 // the particular gpu/driver) from most preferred to least.
420
421 // these consts are in order of most preferred to least preferred
422 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
423 static const GrGLStencilBuffer::Format
424 // internal Format stencil bits total bits packed?
425 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
426 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
427 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
428 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
429 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
430 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
431
432 if (kDesktop_GrGLBinding == this->glBinding()) {
433 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
434 this->hasExtension("GL_EXT_packed_depth_stencil") ||
435 this->hasExtension("GL_ARB_framebuffer_object");
436
437 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
438 // require FBO support we can expect these are legal formats and don't
439 // check. These also all support the unsized GL_STENCIL_INDEX.
440 fGLCaps.fStencilFormats.push_back() = gS8;
441 fGLCaps.fStencilFormats.push_back() = gS16;
442 if (supportsPackedDS) {
443 fGLCaps.fStencilFormats.push_back() = gD24S8;
444 }
445 fGLCaps.fStencilFormats.push_back() = gS4;
446 if (supportsPackedDS) {
447 fGLCaps.fStencilFormats.push_back() = gDS;
448 }
449 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000450 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
451 // for other formats.
452 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000453
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000454 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000455 //fStencilFormats.push_back() = gS16;
456 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
457 fGLCaps.fStencilFormats.push_back() = gD24S8;
458 }
459 if (this->hasExtension("GL_OES_stencil4")) {
460 fGLCaps.fStencilFormats.push_back() = gS4;
461 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000462 }
463}
464
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000465GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000466 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
467 return GrPixelConfigSwapRAndB(config);
468 } else {
469 return config;
470 }
471}
472
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000473GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000474 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000475 return GrPixelConfigSwapRAndB(config);
476 } else {
477 return config;
478 }
479}
480
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000481bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
482 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
483}
484
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000485void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000486 if (gPrintStartupSpew && !fPrintedCaps) {
487 fPrintedCaps = true;
488 this->getCaps().print();
489 fGLCaps.print();
490 }
491
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000492 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000494 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000496 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000497 GL_CALL(Disable(GR_GL_DEPTH_TEST));
498 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000499
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000500 GL_CALL(Disable(GR_GL_CULL_FACE));
501 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000504 GL_CALL(Disable(GR_GL_DITHER));
505 if (kDesktop_GrGLBinding == this->glBinding()) {
506 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
507 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
508 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000509 fHWAAState.fMSAAEnabled = false;
510 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000511 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000512
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000513 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000514 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000515
reed@google.comac10a2d2010-12-22 21:39:39 +0000516 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000517 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000518
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000519 // invalid
520 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000521
reed@google.comac10a2d2010-12-22 21:39:39 +0000522 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000523 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000524
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000525 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000526 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000527
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000528 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000529
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000530 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000531
tomhudson@google.com93813632011-10-27 20:21:16 +0000532 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000533 fHWDrawState.setTexture(s, NULL);
534 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
535 -GR_ScalarMax,
536 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000537 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000538 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000539 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000540
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000541 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000542 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000543 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000544 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000545
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000546 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000547 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000548 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000549
550 fHWGeometryState.fIndexBuffer = NULL;
551 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000552
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000553 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000555 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000556 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000557
558 // we assume these values
559 if (this->glCaps().fUnpackRowLengthSupport) {
560 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
561 }
562 if (this->glCaps().fPackRowLengthSupport) {
563 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
564 }
565 if (this->glCaps().fUnpackFlipYSupport) {
566 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
567 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000568 if (this->glCaps().fPackFlipYSupport) {
569 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
570 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000571}
572
bsalomon@google.come269f212011-11-07 13:29:52 +0000573GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000574 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000575 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000576 return NULL;
577 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000578
bsalomon@google.com99621082011-11-15 16:47:16 +0000579 glTexDesc.fWidth = desc.fWidth;
580 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000581 glTexDesc.fConfig = desc.fConfig;
582 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
583 glTexDesc.fOwnsID = false;
584 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
585
586 GrGLTexture* texture = NULL;
587 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
588 GrGLRenderTarget::Desc glRTDesc;
589 glRTDesc.fRTFBOID = 0;
590 glRTDesc.fTexFBOID = 0;
591 glRTDesc.fMSColorRenderbufferID = 0;
592 glRTDesc.fOwnIDs = true;
593 glRTDesc.fConfig = desc.fConfig;
594 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000595 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
596 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000597 glTexDesc.fTextureID,
598 &glRTDesc)) {
599 return NULL;
600 }
601 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
602 } else {
603 texture = new GrGLTexture(this, glTexDesc);
604 }
605 if (NULL == texture) {
606 return NULL;
607 }
608
609 this->setSpareTextureUnit();
610 return texture;
611}
612
613GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
614 GrGLRenderTarget::Desc glDesc;
615 glDesc.fConfig = desc.fConfig;
616 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
617 glDesc.fMSColorRenderbufferID = 0;
618 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
619 glDesc.fSampleCnt = desc.fSampleCnt;
620 glDesc.fOwnIDs = false;
621 GrGLIRect viewport;
622 viewport.fLeft = 0;
623 viewport.fBottom = 0;
624 viewport.fWidth = desc.fWidth;
625 viewport.fHeight = desc.fHeight;
626
627 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
628 if (desc.fStencilBits) {
629 GrGLStencilBuffer::Format format;
630 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
631 format.fPacked = false;
632 format.fStencilBits = desc.fStencilBits;
633 format.fTotalBits = desc.fStencilBits;
634 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
635 0,
636 desc.fWidth,
637 desc.fHeight,
638 desc.fSampleCnt,
639 format);
640 tgt->setStencilBuffer(sb);
641 sb->unref();
642 }
643 return tgt;
644}
645
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000646GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
647
648 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
649 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
650 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
651 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
652
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000653 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000654 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000655
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000656 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000657 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000658 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000659 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000660 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000661 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000662 } else {
663 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000664 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000665 }
666 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000667 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000668 }
669 // we don't know what the RB ids are without glGets and we don't care
670 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000671 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000672 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000673 if (desc.fStencilBits) {
674 GrGLStencilBuffer::Format format;
675 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
676 format.fPacked = false;
677 format.fStencilBits = desc.fStencilBits;
678 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000679 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
680 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000681 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000682 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000683 }
684
685 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000686 GrGLTexture::Desc texDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000687 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000688 return NULL;
689 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000690 texDesc.fWidth = desc.fWidth;
691 texDesc.fHeight = desc.fHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000692
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000693 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000694 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000695 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000696 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000697
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000698 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000699 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000700 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000701 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000702 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000703 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000704 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000705 } else {
706 GrGLIRect viewport;
707 viewport.fLeft = 0;
708 viewport.fBottom = 0;
709 viewport.fWidth = desc.fWidth;
710 viewport.fHeight = desc.fHeight;
711
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000712 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000713 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000714 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000715 }
716}
717
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000718
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000719////////////////////////////////////////////////////////////////////////////////
720
bsalomon@google.com6f379512011-11-16 20:36:03 +0000721void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
722 int left, int top, int width, int height,
723 GrPixelConfig config, const void* buffer,
724 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000725 if (NULL == buffer) {
726 return;
727 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000728 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
729
bsalomon@google.com6f379512011-11-16 20:36:03 +0000730 this->setSpareTextureUnit();
731 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
732 GrGLTexture::Desc desc;
733 desc.fConfig = glTex->config();
734 desc.fWidth = glTex->width();
735 desc.fHeight = glTex->height();
736 desc.fOrientation = glTex->orientation();
737 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000738
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000739 this->uploadTexData(desc, false,
740 left, top, width, height,
741 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000742}
743
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000744namespace {
745bool adjust_pixel_ops_params(int surfaceWidth,
746 int surfaceHeight,
747 size_t bpp,
748 int* left, int* top, int* width, int* height,
749 const void** data,
750 size_t* rowBytes) {
751 if (!*rowBytes) {
752 *rowBytes = *width * bpp;
753 }
754
755 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
756 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
757
758 if (!subRect.intersect(bounds)) {
759 return false;
760 }
761 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
762 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
763
764 *left = subRect.fLeft;
765 *top = subRect.fTop;
766 *width = subRect.width();
767 *height = subRect.height();
768 return true;
769}
770}
771
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000772bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
773 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000774 int left, int top, int width, int height,
775 GrPixelConfig dataConfig,
776 const void* data,
777 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000778 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000779
780 size_t bpp = GrBytesPerPixel(dataConfig);
781 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
782 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000783 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000784 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000785 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000786
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000787 // in case we need a temporary, trimmed copy of the src pixels
788 SkAutoSMalloc<128 * 128> tempStorage;
789
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000790 bool useTexStorage = isNewTexture &&
791 this->glCaps().fTexStorageSupport;
792 if (useTexStorage) {
793 if (kDesktop_GrGLBinding == this->glBinding()) {
794 // 565 is not a sized internal format on desktop GL. So on desktop
795 // with 565 we always use an unsized internal format to let the
796 // system pick the best sized format to convert the 565 data to.
797 // Since glTexStorage only allows sized internal formats we will
798 // instead fallback to glTexImage2D.
799 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
800 } else {
801 // ES doesn't allow paletted textures to be used with tex storage
802 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
803 }
804 }
805
806 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000807 GrGLenum externalFormat;
808 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000809 // glTexStorage requires sized internal formats on both desktop and ES. ES
810 // glTexImage requires an unsized format.
811 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
812 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000813 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000814 }
815
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000816 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000817 // paletted textures cannot be updated
818 return false;
819 }
820
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000821 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000822 * check whether to allocate a temporary buffer for flipping y or
823 * because our srcData has extra bytes past each row. If so, we need
824 * to trim those off here, since GL ES may not let us specify
825 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000826 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000827 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000828 bool swFlipY = false;
829 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000830 if (NULL != data) {
831 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
832 if (this->glCaps().fUnpackFlipYSupport) {
833 glFlipY = true;
834 } else {
835 swFlipY = true;
836 }
837 }
838 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
839 // can't use this for flipping, only non-neg values allowed. :(
840 if (rowBytes != trimRowBytes) {
841 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
842 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
843 restoreGLRowLength = true;
844 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000845 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000846 if (trimRowBytes != rowBytes || swFlipY) {
847 // copy data into our new storage, skipping the trailing bytes
848 size_t trimSize = height * trimRowBytes;
849 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000850 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000851 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000852 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000853 char* dst = (char*)tempStorage.reset(trimSize);
854 for (int y = 0; y < height; y++) {
855 memcpy(dst, src, trimRowBytes);
856 if (swFlipY) {
857 src -= rowBytes;
858 } else {
859 src += rowBytes;
860 }
861 dst += trimRowBytes;
862 }
863 // now point data to our copied version
864 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000865 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000866 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000867 if (glFlipY) {
868 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
869 }
870 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000871 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000872 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000873 if (isNewTexture &&
874 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000875 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000876 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000877 if (useTexStorage) {
878 // We never resize or change formats of textures. We don't use
879 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000880 GL_ALLOC_CALL(this->glInterface(),
881 TexStorage2D(GR_GL_TEXTURE_2D,
882 1, // levels
883 internalFormat,
884 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000885 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000886 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
887 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
888 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000889 GL_ALLOC_CALL(this->glInterface(),
890 CompressedTexImage2D(GR_GL_TEXTURE_2D,
891 0, // level
892 internalFormat,
893 desc.fWidth, desc.fHeight,
894 0, // border
895 imageSize,
896 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000897 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000898 GL_ALLOC_CALL(this->glInterface(),
899 TexImage2D(GR_GL_TEXTURE_2D,
900 0, // level
901 internalFormat,
902 desc.fWidth, desc.fHeight,
903 0, // border
904 externalFormat, externalType,
905 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000906 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000907 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000908 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000909 if (error != GR_GL_NO_ERROR) {
910 succeeded = false;
911 } else {
912 // if we have data and we used TexStorage to create the texture, we
913 // now upload with TexSubImage.
914 if (NULL != data && useTexStorage) {
915 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
916 0, // level
917 left, top,
918 width, height,
919 externalFormat, externalType,
920 data));
921 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000922 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000923 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000924 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000925 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000926 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000927 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
928 0, // level
929 left, top,
930 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000931 externalFormat, externalType, data));
932 }
933
934 if (restoreGLRowLength) {
935 GrAssert(this->glCaps().fUnpackRowLengthSupport);
936 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000937 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000938 if (glFlipY) {
939 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
940 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000941 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000942}
943
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000944bool GrGpuGL::createRenderTargetObjects(int width, int height,
945 GrGLuint texID,
946 GrGLRenderTarget::Desc* desc) {
947 desc->fMSColorRenderbufferID = 0;
948 desc->fRTFBOID = 0;
949 desc->fTexFBOID = 0;
950 desc->fOwnIDs = true;
951
952 GrGLenum status;
953 GrGLint err;
954
bsalomon@google.comab15d612011-08-09 12:57:56 +0000955 GrGLenum msColorFormat = 0; // suppress warning
956
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000957 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000958 if (!desc->fTexFBOID) {
959 goto FAILED;
960 }
961
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000962
963 // If we are using multisampling we will create two FBOS. We render
964 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000965 if (desc->fSampleCnt > 0) {
966 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
967 goto FAILED;
968 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000969 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
970 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 if (!desc->fRTFBOID ||
972 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000973 !this->configToGLFormats(desc->fConfig,
974 // GLES requires sized internal formats
975 kES2_GrGLBinding == this->glBinding(),
976 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000977 goto FAILED;
978 }
979 } else {
980 desc->fRTFBOID = desc->fTexFBOID;
981 }
982
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000983 // below here we may bind the FBO
984 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000985 if (desc->fRTFBOID != desc->fTexFBOID) {
986 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000987 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000988 desc->fMSColorRenderbufferID));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000989 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
990 GL_ALLOC_CALL(this->glInterface(),
991 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
992 desc->fSampleCnt,
993 msColorFormat,
994 width, height));
995 err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000996 if (err != GR_GL_NO_ERROR) {
997 goto FAILED;
998 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000999 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
1000 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001001 GR_GL_COLOR_ATTACHMENT0,
1002 GR_GL_RENDERBUFFER,
1003 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001004 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001005 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1006 goto FAILED;
1007 }
1008 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001009 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001010
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001011 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1012 GR_GL_COLOR_ATTACHMENT0,
1013 GR_GL_TEXTURE_2D,
1014 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001015 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001016 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1017 goto FAILED;
1018 }
1019
1020 return true;
1021
1022FAILED:
1023 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001024 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001025 }
1026 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001028 }
1029 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001030 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001031 }
1032 return false;
1033}
1034
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001035// good to set a break-point here to know when createTexture fails
1036static GrTexture* return_null_texture() {
1037// GrAssert(!"null texture");
1038 return NULL;
1039}
1040
1041#if GR_DEBUG
1042static size_t as_size_t(int x) {
1043 return x;
1044}
1045#endif
1046
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001047GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001048 const void* srcData,
1049 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001050
1051#if GR_COLLECT_STATS
1052 ++fStats.fTextureCreateCnt;
1053#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001054
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001055 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001056 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001057
bsalomon@google.com99621082011-11-15 16:47:16 +00001058 glTexDesc.fWidth = desc.fWidth;
1059 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001060 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001061 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001062
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001063 glRTDesc.fMSColorRenderbufferID = 0;
1064 glRTDesc.fRTFBOID = 0;
1065 glRTDesc.fTexFBOID = 0;
1066 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001067 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001068
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001069 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001070
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001071 const Caps& caps = this->getCaps();
1072
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001073 // We keep GrRenderTargets in GL's normal orientation so that they
1074 // can be drawn to by the outside world without the client having
1075 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001076 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001078
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001079 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1080 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1081 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1082 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001083 GrPrintf("AA RT requested but not supported on this platform.");
1084 }
1085
reed@google.comac10a2d2010-12-22 21:39:39 +00001086 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001087 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1088 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001089 return return_null_texture();
1090 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 }
1092
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001093 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001094 if (renderTarget && this->glCaps().fTextureUsageSupport) {
1095 // provides a hint about how this texture will be used
1096 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1097 GR_GL_TEXTURE_USAGE,
1098 GR_GL_FRAMEBUFFER_ATTACHMENT));
1099 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001100 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001101 return return_null_texture();
1102 }
1103
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001104 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001105 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001106
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001107 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1108 // drivers have a bug where an FBO won't be complete if it includes a
1109 // texture that is not mipmap complete (considering the filter in use).
1110 GrGLTexture::TexParams initialTexParams;
1111 // we only set a subset here so invalidate first
1112 initialTexParams.invalidate();
1113 initialTexParams.fFilter = GR_GL_NEAREST;
1114 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1115 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001116 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1117 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001118 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001119 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1120 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001121 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1123 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001124 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001125 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1126 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001127 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001128 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1129 glTexDesc.fWidth, glTexDesc.fHeight,
1130 desc.fConfig, srcData, rowBytes)) {
1131 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1132 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001133 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001134
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001136 if (renderTarget) {
1137#if GR_COLLECT_STATS
1138 ++fStats.fRenderTargetCreateCnt;
1139#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001140 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1141 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 glTexDesc.fTextureID,
1143 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001145 return return_null_texture();
1146 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001147 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001149 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001150 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001151 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001152#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001153 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1154 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001155#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 return tex;
1157}
1158
1159namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1161 GrGLuint rb,
1162 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 // we shouldn't ever know one size and not the other
1164 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1165 (kUnknownBitCount == format->fTotalBits));
1166 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001167 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1169 (GrGLint*)&format->fStencilBits);
1170 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001171 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1173 (GrGLint*)&format->fTotalBits);
1174 format->fTotalBits += format->fStencilBits;
1175 } else {
1176 format->fTotalBits = format->fStencilBits;
1177 }
1178 }
1179}
1180}
1181
1182bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1183 int width, int height) {
1184
1185 // All internally created RTs are also textures. We don't create
1186 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1187 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001188 GrAssert(width >= rt->width());
1189 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190
1191 int samples = rt->numSamples();
1192 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001193 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 if (!sbID) {
1195 return false;
1196 }
1197
1198 GrGLStencilBuffer* sb = NULL;
1199
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001200 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 // we start with the last stencil format that succeeded in hopes
1204 // that we won't go through this loop more than once after the
1205 // first (painful) stencil creation.
1206 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001207 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001208 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001209 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 // version on a GL that doesn't have an MSAA extension.
1211 if (samples > 1) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001212 GL_ALLOC_CALL(this->glInterface(),
1213 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1214 samples,
1215 sFmt.fInternalFormat,
1216 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001218 GL_ALLOC_CALL(this->glInterface(),
1219 RenderbufferStorage(GR_GL_RENDERBUFFER,
1220 sFmt.fInternalFormat,
1221 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 }
1223
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001224 GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 if (err == GR_GL_NO_ERROR) {
1226 // After sized formats we attempt an unsized format and take whatever
1227 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001228 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001229 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001230 sb = new GrGLStencilBuffer(this, sbID, width, height,
1231 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1233 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001234 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 return true;
1237 }
1238 sb->abandon(); // otherwise we lose sbID
1239 sb->unref();
1240 }
1241 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001242 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001243 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244}
1245
1246bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1247 GrRenderTarget* rt) {
1248 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1249
1250 GrGLuint fbo = glrt->renderFBOID();
1251
1252 if (NULL == sb) {
1253 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001254 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255 GR_GL_STENCIL_ATTACHMENT,
1256 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001257 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001258 GR_GL_DEPTH_ATTACHMENT,
1259 GR_GL_RENDERBUFFER, 0));
1260#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001261 GrGLenum status;
1262 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001263 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1264#endif
1265 }
1266 return true;
1267 } else {
1268 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1269 GrGLuint rb = glsb->renderbufferID();
1270
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001271 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1273 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001274 GR_GL_STENCIL_ATTACHMENT,
1275 GR_GL_RENDERBUFFER, rb));
1276 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001277 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001278 GR_GL_DEPTH_ATTACHMENT,
1279 GR_GL_RENDERBUFFER, rb));
1280 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001281 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001282 GR_GL_DEPTH_ATTACHMENT,
1283 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001285
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001286 GrGLenum status;
1287 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001288 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290 GR_GL_STENCIL_ATTACHMENT,
1291 GR_GL_RENDERBUFFER, 0));
1292 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001294 GR_GL_DEPTH_ATTACHMENT,
1295 GR_GL_RENDERBUFFER, 0));
1296 }
1297 return false;
1298 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001299 return true;
1300 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001302}
1303
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001304////////////////////////////////////////////////////////////////////////////////
1305
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001306GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001307 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001308 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001310 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001311 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001312 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001314 GL_ALLOC_CALL(this->glInterface(),
1315 BufferData(GR_GL_ARRAY_BUFFER,
1316 size,
1317 NULL, // data ptr
1318 dynamic ? GR_GL_DYNAMIC_DRAW :
1319 GR_GL_STATIC_DRAW));
1320 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001321 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 // deleting bound buffer does implicit bind to 0
1323 fHWGeometryState.fVertexBuffer = NULL;
1324 return NULL;
1325 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001326 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 size, dynamic);
1328 fHWGeometryState.fVertexBuffer = vertexBuffer;
1329 return vertexBuffer;
1330 }
1331 return NULL;
1332}
1333
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001334GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001335 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001336 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001338 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001339 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001341 GL_ALLOC_CALL(this->glInterface(),
1342 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1343 size,
1344 NULL, // data ptr
1345 dynamic ? GR_GL_DYNAMIC_DRAW :
1346 GR_GL_STATIC_DRAW));
1347 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001348 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 // deleting bound buffer does implicit bind to 0
1350 fHWGeometryState.fIndexBuffer = NULL;
1351 return NULL;
1352 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001353 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 size, dynamic);
1355 fHWGeometryState.fIndexBuffer = indexBuffer;
1356 return indexBuffer;
1357 }
1358 return NULL;
1359}
1360
reed@google.comac10a2d2010-12-22 21:39:39 +00001361void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001362 const GrDrawState& drawState = this->getDrawState();
1363 const GrGLRenderTarget* rt =
1364 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1365
1366 GrAssert(NULL != rt);
1367 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001368
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001369 GrGLIRect scissor;
1370 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001371 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001372 rect->width(), rect->height());
1373 if (scissor.contains(vp)) {
1374 rect = NULL;
1375 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 }
1377
1378 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001380 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 fHWBounds.fScissorRect = scissor;
1382 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001383 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001384 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001385 fHWBounds.fScissorEnabled = true;
1386 }
1387 } else {
1388 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001389 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001390 fHWBounds.fScissorEnabled = false;
1391 }
1392 }
1393}
1394
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001395void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001396 const GrDrawState& drawState = this->getDrawState();
1397 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001398 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001399 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001400
bsalomon@google.com74b98712011-11-11 19:46:16 +00001401 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001402 if (NULL != rect) {
1403 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001404 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001405 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001406 if (clippedRect.intersect(rtRect)) {
1407 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001408 } else {
1409 return;
1410 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001412 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001413 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001414
1415 GrGLfloat r, g, b, a;
1416 static const GrGLfloat scale255 = 1.f / 255.f;
1417 a = GrColorUnpackA(color) * scale255;
1418 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001419 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001420 scaleRGB *= a;
1421 }
1422 r = GrColorUnpackR(color) * scaleRGB;
1423 g = GrColorUnpackG(color) * scaleRGB;
1424 b = GrColorUnpackB(color) * scaleRGB;
1425
1426 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001427 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001428 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001429 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001430}
1431
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001432void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001433 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001434 return;
1435 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001436
1437 this->flushRenderTarget(&GrIRect::EmptyIRect());
1438
reed@google.comac10a2d2010-12-22 21:39:39 +00001439 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001440 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001441 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001442 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001443 GL_CALL(StencilMask(0xffffffff));
1444 GL_CALL(ClearStencil(0));
1445 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001446 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001447}
1448
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001449void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001450 const GrDrawState& drawState = this->getDrawState();
1451 const GrRenderTarget* rt = drawState.getRenderTarget();
1452 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001453
1454 // this should only be called internally when we know we have a
1455 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001456 GrAssert(NULL != rt->getStencilBuffer());
1457 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001458#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001460 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001461#else
1462 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001463 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001464 // turned into draws. Our contract on GrDrawTarget says that
1465 // changing the clip between stencil passes may or may not
1466 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001467 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001468#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001469 GrGLint value;
1470 if (insideClip) {
1471 value = (1 << (stencilBitCount - 1));
1472 } else {
1473 value = 0;
1474 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001475 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001476 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001477 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001478 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001479 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001480 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001481}
1482
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001483void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001484 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001485}
1486
bsalomon@google.comc4364992011-11-07 15:54:49 +00001487bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1488 int left, int top,
1489 int width, int height,
1490 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001491 size_t rowBytes) const {
1492 // if GL can do the flip then we'll never pay for it.
1493 if (this->glCaps().fPackFlipYSupport) {
1494 return false;
1495 }
1496
1497 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001498 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001499 if (this->glCaps().fPackRowLengthSupport) {
1500 return true;
1501 }
1502 // If we have to do memcpys to handle rowBytes then y-flip is free
1503 // Note the rowBytes might be tight to the passed in data, but if data
1504 // gets clipped in x to the target the rowBytes will no longer be tight.
1505 if (left >= 0 && (left + width) < renderTarget->width()) {
1506 return 0 == rowBytes ||
1507 GrBytesPerPixel(config) * width == rowBytes;
1508 } else {
1509 return false;
1510 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001511}
1512
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001513bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001514 int left, int top,
1515 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001516 GrPixelConfig config,
1517 void* buffer,
1518 size_t rowBytes,
1519 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001520 GrGLenum format;
1521 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001522 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001523 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001524 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001525 size_t bpp = GrBytesPerPixel(config);
1526 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1527 &left, &top, &width, &height,
1528 const_cast<const void**>(&buffer),
1529 &rowBytes)) {
1530 return false;
1531 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001532
bsalomon@google.comc6980972011-11-02 19:57:21 +00001533 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001534 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001535 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001536 switch (tgt->getResolveType()) {
1537 case GrGLRenderTarget::kCantResolve_ResolveType:
1538 return false;
1539 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001540 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001541 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001542 break;
1543 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001544 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001545 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001546 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1547 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001548 break;
1549 default:
1550 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001551 }
1552
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001553 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001554
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001555 // the read rect is viewport-relative
1556 GrGLIRect readRect;
1557 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001558
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001559 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001560 if (0 == rowBytes) {
1561 rowBytes = tightRowBytes;
1562 }
1563 size_t readDstRowBytes = tightRowBytes;
1564 void* readDst = buffer;
1565
1566 // determine if GL can read using the passed rowBytes or if we need
1567 // a scratch buffer.
1568 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1569 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001570 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001571 GrAssert(!(rowBytes % sizeof(GrColor)));
1572 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1573 readDstRowBytes = rowBytes;
1574 } else {
1575 scratch.reset(tightRowBytes * height);
1576 readDst = scratch.get();
1577 }
1578 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001579 if (!invertY && this->glCaps().fPackFlipYSupport) {
1580 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1581 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001582 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1583 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001584 format, type, readDst));
1585 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001586 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001587 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1588 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001589 if (!invertY && this->glCaps().fPackFlipYSupport) {
1590 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1591 invertY = true;
1592 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001593
1594 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001595 // API presents top-to-bottom. We must preserve the padding contents. Note
1596 // that the above readPixels did not overwrite the padding.
1597 if (readDst == buffer) {
1598 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001599 if (!invertY) {
1600 scratch.reset(tightRowBytes);
1601 void* tmpRow = scratch.get();
1602 // flip y in-place by rows
1603 const int halfY = height >> 1;
1604 char* top = reinterpret_cast<char*>(buffer);
1605 char* bottom = top + (height - 1) * rowBytes;
1606 for (int y = 0; y < halfY; y++) {
1607 memcpy(tmpRow, top, tightRowBytes);
1608 memcpy(top, bottom, tightRowBytes);
1609 memcpy(bottom, tmpRow, tightRowBytes);
1610 top += rowBytes;
1611 bottom -= rowBytes;
1612 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001613 }
1614 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001615 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001616 // copy from readDst to buffer while flipping y
1617 const int halfY = height >> 1;
1618 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001619 char* dst = reinterpret_cast<char*>(buffer);
1620 if (!invertY) {
1621 dst += (height-1) * rowBytes;
1622 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001623 for (int y = 0; y < height; y++) {
1624 memcpy(dst, src, tightRowBytes);
1625 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001626 if (invertY) {
1627 dst += rowBytes;
1628 } else {
1629 dst -= rowBytes;
1630 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001631 }
1632 }
1633 return true;
1634}
1635
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001636void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001637
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001638 GrGLRenderTarget* rt =
1639 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1640 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001641
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001642 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001643 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 #if GR_COLLECT_STATS
1645 ++fStats.fRenderTargetChngCnt;
1646 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001648 GrGLenum status;
1649 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001650 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001651 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001652 }
1653 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001654 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001655 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001656 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001657 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001658 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 fHWBounds.fViewportRect = vp;
1660 }
1661 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001662 if (NULL == bound || !bound->isEmpty()) {
1663 rt->flagAsNeedingResolve(bound);
1664 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001665}
1666
twiz@google.com0f31ca72011-03-18 17:38:11 +00001667GrGLenum gPrimitiveType2GLMode[] = {
1668 GR_GL_TRIANGLES,
1669 GR_GL_TRIANGLE_STRIP,
1670 GR_GL_TRIANGLE_FAN,
1671 GR_GL_POINTS,
1672 GR_GL_LINES,
1673 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001674};
1675
bsalomon@google.comd302f142011-03-03 13:54:13 +00001676#define SWAP_PER_DRAW 0
1677
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001678#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001679 #if GR_MAC_BUILD
1680 #include <AGL/agl.h>
1681 #elif GR_WIN32_BUILD
1682 void SwapBuf() {
1683 DWORD procID = GetCurrentProcessId();
1684 HWND hwnd = GetTopWindow(GetDesktopWindow());
1685 while(hwnd) {
1686 DWORD wndProcID = 0;
1687 GetWindowThreadProcessId(hwnd, &wndProcID);
1688 if(wndProcID == procID) {
1689 SwapBuffers(GetDC(hwnd));
1690 }
1691 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1692 }
1693 }
1694 #endif
1695#endif
1696
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001697void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1698 uint32_t startVertex,
1699 uint32_t startIndex,
1700 uint32_t vertexCount,
1701 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1703
twiz@google.com0f31ca72011-03-18 17:38:11 +00001704 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001705
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001706 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1707 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1708
1709 // our setupGeometry better have adjusted this to zero since
1710 // DrawElements always draws from the begining of the arrays for idx 0.
1711 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001712
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001713 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1714 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001715#if SWAP_PER_DRAW
1716 glFlush();
1717 #if GR_MAC_BUILD
1718 aglSwapBuffers(aglGetCurrentContext());
1719 int set_a_break_pt_here = 9;
1720 aglSwapBuffers(aglGetCurrentContext());
1721 #elif GR_WIN32_BUILD
1722 SwapBuf();
1723 int set_a_break_pt_here = 9;
1724 SwapBuf();
1725 #endif
1726#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001727}
1728
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001729void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1730 uint32_t startVertex,
1731 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001732 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1733
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001734 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1735
1736 // our setupGeometry better have adjusted this to zero.
1737 // DrawElements doesn't take an offset so we always adjus the startVertex.
1738 GrAssert(0 == startVertex);
1739
1740 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1741 // account for startVertex in the DrawElements case. So we always
1742 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001743 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001744#if SWAP_PER_DRAW
1745 glFlush();
1746 #if GR_MAC_BUILD
1747 aglSwapBuffers(aglGetCurrentContext());
1748 int set_a_break_pt_here = 9;
1749 aglSwapBuffers(aglGetCurrentContext());
1750 #elif GR_WIN32_BUILD
1751 SwapBuf();
1752 int set_a_break_pt_here = 9;
1753 SwapBuf();
1754 #endif
1755#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001756}
1757
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001758void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001759
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001760 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001761 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001763 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1764 rt->renderFBOID()));
1765 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1766 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001767 #if GR_COLLECT_STATS
1768 ++fStats.fRenderTargetChngCnt;
1769 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001770 // make sure we go through flushRenderTarget() since we've modified
1771 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001772 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001773 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001774 const GrIRect dirtyRect = rt->getResolveRect();
1775 GrGLIRect r;
1776 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1777 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001778
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001779 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001780 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001781 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1782 GL_CALL(Scissor(r.fLeft, r.fBottom,
1783 r.fWidth, r.fHeight));
1784 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001785 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001786 fHWBounds.fScissorEnabled = true;
1787 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001788 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001789 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001790 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1791 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001792 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001793 int right = r.fLeft + r.fWidth;
1794 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001795 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1796 r.fLeft, r.fBottom, right, top,
1797 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001798 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001799 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001800 }
1801}
1802
twiz@google.com0f31ca72011-03-18 17:38:11 +00001803static const GrGLenum grToGLStencilFunc[] = {
1804 GR_GL_ALWAYS, // kAlways_StencilFunc
1805 GR_GL_NEVER, // kNever_StencilFunc
1806 GR_GL_GREATER, // kGreater_StencilFunc
1807 GR_GL_GEQUAL, // kGEqual_StencilFunc
1808 GR_GL_LESS, // kLess_StencilFunc
1809 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1810 GR_GL_EQUAL, // kEqual_StencilFunc,
1811 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001812};
1813GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1814GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1815GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1816GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1817GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1818GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1819GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1820GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1821GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1822
twiz@google.com0f31ca72011-03-18 17:38:11 +00001823static const GrGLenum grToGLStencilOp[] = {
1824 GR_GL_KEEP, // kKeep_StencilOp
1825 GR_GL_REPLACE, // kReplace_StencilOp
1826 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1827 GR_GL_INCR, // kIncClamp_StencilOp
1828 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1829 GR_GL_DECR, // kDecClamp_StencilOp
1830 GR_GL_ZERO, // kZero_StencilOp
1831 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001832};
1833GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1834GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1835GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1836GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1837GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1838GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1839GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1840GR_STATIC_ASSERT(6 == kZero_StencilOp);
1841GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1842
reed@google.comac10a2d2010-12-22 21:39:39 +00001843void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001844 const GrDrawState& drawState = this->getDrawState();
1845
1846 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001847
1848 // use stencil for clipping if clipping is enabled and the clip
1849 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001850 bool stencilClip = fClipInStencil && drawState.isClipState();
1851 bool drawClipToStencil =
1852 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001853 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1854 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001855 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1856 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001857
1858 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001859
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001860 // we can't simultaneously perform stencil-clipping and
1861 // modify the stencil clip
1862 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001863
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864 if (settings->isDisabled()) {
1865 if (stencilClip) {
1866 settings = &gClipStencilSettings;
1867 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001868 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869
1870 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001871 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001872 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001873 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001874 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001875 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001876 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1877 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1878 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1879 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1880 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1881 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1882 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1883 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001884 }
1885 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001886 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001887 GrStencilBuffer* stencilBuffer =
1888 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001889 if (NULL != stencilBuffer) {
1890 stencilBits = stencilBuffer->bits();
1891 }
1892 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001893 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001894
1895 GrGLuint clipStencilMask = 0;
1896 GrGLuint userStencilMask = ~0;
1897 if (stencilBits > 0) {
1898 clipStencilMask = 1 << (stencilBits - 1);
1899 userStencilMask = clipStencilMask - 1;
1900 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001901
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001902 unsigned int frontRef = settings->frontFuncRef();
1903 unsigned int frontMask = settings->frontFuncMask();
1904 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001905 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001906
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001907 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001908 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1909 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001910 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001911 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001912 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001913
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001914 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001915 stencilClip,
1916 clipStencilMask,
1917 userStencilMask,
1918 &frontRef,
1919 &frontMask);
1920 frontWriteMask &= userStencilMask;
1921 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001922 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001923 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001924 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001925 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001926 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001927 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001928 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001929 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001930 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001931 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001932
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001933 unsigned int backRef = settings->backFuncRef();
1934 unsigned int backMask = settings->backFuncMask();
1935 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001936
1937
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001938 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001939 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1940 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001941 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001942 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001943 stencilClip, settings->backFunc())];
1944 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001945 stencilClip,
1946 clipStencilMask,
1947 userStencilMask,
1948 &backRef,
1949 &backMask);
1950 backWriteMask &= userStencilMask;
1951 }
1952
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001953 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1954 frontRef, frontMask));
1955 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1956 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1957 backRef, backMask));
1958 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1959 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001960 grToGLStencilOp[settings->frontFailOp()],
1961 grToGLStencilOp[settings->frontPassOp()],
1962 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001963
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001964 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001965 grToGLStencilOp[settings->backFailOp()],
1966 grToGLStencilOp[settings->backPassOp()],
1967 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001968 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001969 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1970 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001971 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1972 grToGLStencilOp[settings->frontPassOp()],
1973 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001974 }
1975 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001976 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001977 fHWStencilClip = stencilClip;
1978 }
1979}
1980
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001981void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001982 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001983 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001984 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1985 // smooth lines.
1986
1987 // we prefer smooth lines over multisampled lines
1988 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001989 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001990 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001991 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001992 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001993 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001994 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001995 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001996 fHWAAState.fSmoothLineEnabled = false;
1997 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001998 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001999 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002000 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002001 fHWAAState.fMSAAEnabled = false;
2002 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002003 } else if (rt->isMultisampled() &&
2004 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002005 fHWAAState.fMSAAEnabled) {
2006 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002007 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002008 fHWAAState.fMSAAEnabled = false;
2009 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002010 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002011 fHWAAState.fMSAAEnabled = true;
2012 }
2013 }
2014 }
2015}
2016
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002017void GrGpuGL::flushBlend(GrPrimitiveType type,
2018 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002019 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00002020 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002021 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002022 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002023 fHWBlendDisabled = false;
2024 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002025 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
2026 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002027 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
2028 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002029 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002030 }
2031 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002032 // any optimization to disable blending should
2033 // have already been applied and tweaked the coeffs
2034 // to (1, 0).
2035 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2036 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002037 if (fHWBlendDisabled != blendOff) {
2038 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002039 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002040 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002041 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002042 }
2043 fHWBlendDisabled = blendOff;
2044 }
2045 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002046 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2047 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002048 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2049 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002050 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002051 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002052 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002053 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2054 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002055 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002056
2057 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002058 GrColorUnpackR(blendConst) / 255.f,
2059 GrColorUnpackG(blendConst) / 255.f,
2060 GrColorUnpackB(blendConst) / 255.f,
2061 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002062 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002063 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002064 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002065 }
2066 }
2067 }
2068}
2069
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002070namespace {
2071
2072unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002073 switch (filter) {
2074 case GrSamplerState::kBilinear_Filter:
2075 case GrSamplerState::k4x4Downsample_Filter:
2076 return GR_GL_LINEAR;
2077 case GrSamplerState::kNearest_Filter:
2078 case GrSamplerState::kConvolution_Filter:
2079 return GR_GL_NEAREST;
2080 default:
2081 GrAssert(!"Unknown filter type");
2082 return GR_GL_LINEAR;
2083 }
2084}
2085
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002086const GrGLenum* get_swizzle(GrPixelConfig config,
2087 const GrSamplerState& sampler) {
2088 if (GrPixelConfigIsAlphaOnly(config)) {
2089 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2090 GR_GL_ALPHA, GR_GL_ALPHA };
2091 return gAlphaSmear;
2092 } else if (sampler.swapsRAndB()) {
2093 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2094 GR_GL_RED, GR_GL_ALPHA };
2095 return gRedBlueSwap;
2096 } else {
2097 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2098 GR_GL_BLUE, GR_GL_ALPHA };
2099 return gStraight;
2100 }
2101}
2102
2103void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2104 // should add texparameteri to interface to make 1 instead of 4 calls here
2105 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2106 GR_GL_TEXTURE_SWIZZLE_R,
2107 swizzle[0]));
2108 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2109 GR_GL_TEXTURE_SWIZZLE_G,
2110 swizzle[1]));
2111 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2112 GR_GL_TEXTURE_SWIZZLE_B,
2113 swizzle[2]));
2114 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2115 GR_GL_TEXTURE_SWIZZLE_A,
2116 swizzle[3]));
2117}
2118}
2119
bsalomon@google.comffca4002011-02-22 20:34:01 +00002120bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002121
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002122 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002123 // GrGpu::setupClipAndFlushState should have already checked this
2124 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002125 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002126
tomhudson@google.com93813632011-10-27 20:21:16 +00002127 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002128 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002129 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002130 GrGLTexture* nextTexture =
2131 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002132
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002133 // true for now, but maybe not with GrEffect.
2134 GrAssert(NULL != nextTexture);
2135 // if we created a rt/tex and rendered to it without using a
2136 // texture and now we're texuring from the rt it will still be
2137 // the last bound texture, but it needs resolving. So keep this
2138 // out of the "last != next" check.
2139 GrGLRenderTarget* texRT =
2140 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2141 if (NULL != texRT) {
2142 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002143 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002144
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002145 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002146 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002147 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002148 #if GR_COLLECT_STATS
2149 ++fStats.fTextureChngCnt;
2150 #endif
2151 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002152 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002153 // The texture matrix has to compensate for texture width/height
2154 // and NPOT-embedded-in-POT
2155 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002156 }
2157
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002158 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002159 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002160 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002161 nextTexture->getCachedTexParams(&timestamp);
2162 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002163 GrGLTexture::TexParams newTexParams;
2164
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002165 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002166
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002167 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002168 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2169 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002170 memcpy(newTexParams.fSwizzleRGBA,
2171 get_swizzle(nextTexture->config(), sampler),
2172 sizeof(newTexParams.fSwizzleRGBA));
2173 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002174 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002175 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002176 GR_GL_TEXTURE_MAG_FILTER,
2177 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002178 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002179 GR_GL_TEXTURE_MIN_FILTER,
2180 newTexParams.fFilter));
2181 }
2182 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2183 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002184 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002185 GR_GL_TEXTURE_WRAP_S,
2186 newTexParams.fWrapS));
2187 }
2188 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2189 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002190 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002191 GR_GL_TEXTURE_WRAP_T,
2192 newTexParams.fWrapT));
2193 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002194 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002195 (setAll ||
2196 memcmp(newTexParams.fSwizzleRGBA,
2197 oldTexParams.fSwizzleRGBA,
2198 sizeof(newTexParams.fSwizzleRGBA)))) {
2199 setTextureUnit(s);
2200 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2201 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002202 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002203 nextTexture->setCachedTexParams(newTexParams,
2204 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002205 }
2206 }
2207
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002208 GrIRect* rect = NULL;
2209 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002210 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002211 fClip.hasConservativeBounds()) {
2212 fClip.getConservativeBounds().roundOut(&clipBounds);
2213 rect = &clipBounds;
2214 }
2215 this->flushRenderTarget(rect);
2216 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002217
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002218 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2219 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002220 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002221 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002222 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002223 }
2224 }
2225
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002226 if (drawState->isColorWriteDisabled() !=
2227 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002228 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002229 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002230 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002231 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002232 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002233 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002234 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002235 }
2236
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002237 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2238 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002239 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002240 GL_CALL(Enable(GR_GL_CULL_FACE));
2241 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002242 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002243 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002244 GL_CALL(Enable(GR_GL_CULL_FACE));
2245 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002246 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002247 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002248 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002249 break;
2250 default:
2251 GrCrash("Unknown draw face.");
2252 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002253 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002254 }
2255
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002256#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002257 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002258 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002259 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002260 NULL == drawState->getRenderTarget() ||
2261 NULL == drawState->getTexture(s) ||
2262 drawState->getTexture(s)->asRenderTarget() !=
2263 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002264 }
2265#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002266
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002267 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002268
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002269 // This copy must happen after flushStencil() is called. flushStencil()
2270 // relies on detecting when the kModifyStencilClip_StateBit state has
2271 // changed since the last draw.
2272 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002273 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002274}
2275
2276void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002277 if (fHWGeometryState.fVertexBuffer != buffer) {
2278 fHWGeometryState.fArrayPtrsDirty = true;
2279 fHWGeometryState.fVertexBuffer = buffer;
2280 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002281}
2282
2283void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002284 if (fHWGeometryState.fVertexBuffer == buffer) {
2285 // deleting bound buffer does implied bind to 0
2286 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002287 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 }
2289}
2290
2291void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002292 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002293}
2294
2295void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002296 if (fHWGeometryState.fIndexBuffer == buffer) {
2297 // deleting bound buffer does implied bind to 0
2298 fHWGeometryState.fIndexBuffer = NULL;
2299 }
2300}
2301
reed@google.comac10a2d2010-12-22 21:39:39 +00002302void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2303 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002304 GrDrawState* drawState = this->drawState();
2305 if (drawState->getRenderTarget() == renderTarget) {
2306 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002307 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002308 if (fHWDrawState.getRenderTarget() == renderTarget) {
2309 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002310 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002311}
2312
2313void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002314 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002315 GrDrawState* drawState = this->drawState();
2316 if (drawState->getTexture(s) == texture) {
2317 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002318 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002319 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002320 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002321 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002322 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002323 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002324}
2325
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002326bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2327 bool getSizedInternalFormat,
2328 GrGLenum* internalFormat,
2329 GrGLenum* externalFormat,
2330 GrGLenum* externalType) {
2331 GrGLenum dontCare;
2332 if (NULL == internalFormat) {
2333 internalFormat = &dontCare;
2334 }
2335 if (NULL == externalFormat) {
2336 externalFormat = &dontCare;
2337 }
2338 if (NULL == externalType) {
2339 externalType = &dontCare;
2340 }
2341
reed@google.comac10a2d2010-12-22 21:39:39 +00002342 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002343 case kRGBA_8888_PM_GrPixelConfig:
2344 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002345 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002346 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002347 if (getSizedInternalFormat) {
2348 *internalFormat = GR_GL_RGBA8;
2349 } else {
2350 *internalFormat = GR_GL_RGBA;
2351 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002352 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002353 break;
2354 case kBGRA_8888_PM_GrPixelConfig:
2355 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002356 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002357 return false;
2358 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002359 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002360 if (getSizedInternalFormat) {
2361 *internalFormat = GR_GL_BGRA8;
2362 } else {
2363 *internalFormat = GR_GL_BGRA;
2364 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002365 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002366 if (getSizedInternalFormat) {
2367 *internalFormat = GR_GL_RGBA8;
2368 } else {
2369 *internalFormat = GR_GL_RGBA;
2370 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002371 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002372 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002373 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002374 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002375 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002376 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002377 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002378 if (getSizedInternalFormat) {
2379 if (this->glBinding() == kDesktop_GrGLBinding) {
2380 return false;
2381 } else {
2382 *internalFormat = GR_GL_RGB565;
2383 }
2384 } else {
2385 *internalFormat = GR_GL_RGB;
2386 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002387 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002388 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002389 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002390 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002391 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002392 if (getSizedInternalFormat) {
2393 *internalFormat = GR_GL_RGBA4;
2394 } else {
2395 *internalFormat = GR_GL_RGBA;
2396 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002397 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002398 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002399 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002400 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002401 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002402 // glCompressedTexImage doesn't take external params
2403 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002404 // no sized/unsized internal format distinction here
2405 *internalFormat = GR_GL_PALETTE8_RGBA8;
2406 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002407 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002408 } else {
2409 return false;
2410 }
2411 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002412 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002413 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002414 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002415 if (getSizedInternalFormat) {
2416 *internalFormat = GR_GL_ALPHA8;
2417 } else {
2418 *internalFormat = GR_GL_ALPHA;
2419 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002420 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002421 break;
2422 default:
2423 return false;
2424 }
2425 return true;
2426}
2427
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002428void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002429 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002430 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002431 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002432 fActiveTextureUnitIdx = unit;
2433 }
2434}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002435
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002436void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002437 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002438 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002439 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2440 }
2441}
2442
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002443void GrGpuGL::resetDirtyFlags() {
2444 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2445}
2446
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002447void GrGpuGL::setBuffers(bool indexed,
2448 int* extraVertexOffset,
2449 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002450
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002451 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002452
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002453 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2454
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002455 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002456 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002457 case kBuffer_GeometrySrcType:
2458 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002459 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002460 break;
2461 case kArray_GeometrySrcType:
2462 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002463 this->finalizeReservedVertices();
2464 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2465 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002466 break;
2467 default:
2468 vbuf = NULL; // suppress warning
2469 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002470 }
2471
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002472 GrAssert(NULL != vbuf);
2473 GrAssert(!vbuf->isLocked());
2474 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002475 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002476 fHWGeometryState.fArrayPtrsDirty = true;
2477 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002478 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002479
2480 if (indexed) {
2481 GrAssert(NULL != extraIndexOffset);
2482
2483 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002484 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002485 case kBuffer_GeometrySrcType:
2486 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002487 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002488 break;
2489 case kArray_GeometrySrcType:
2490 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002491 this->finalizeReservedIndices();
2492 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2493 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002494 break;
2495 default:
2496 ibuf = NULL; // suppress warning
2497 GrCrash("Unknown geometry src type!");
2498 }
2499
2500 GrAssert(NULL != ibuf);
2501 GrAssert(!ibuf->isLocked());
2502 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002503 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002504 fHWGeometryState.fIndexBuffer = ibuf;
2505 }
2506 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002507}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002508
2509int GrGpuGL::getMaxEdges() const {
2510 // FIXME: This is a pessimistic estimate based on how many other things
2511 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002512 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2513 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002514}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002515
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002516void GrGpuGL::GLCaps::print() const {
2517 for (int i = 0; i < fStencilFormats.count(); ++i) {
2518 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2519 i,
2520 fStencilFormats[i].fStencilBits,
2521 fStencilFormats[i].fTotalBits);
2522 }
2523
2524 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2525 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2526 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2527 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2528 static const char* gMSFBOExtStr[] = {
2529 "None",
2530 "ARB",
2531 "EXT",
2532 "Apple",
2533 };
2534 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002535 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002536 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2537 }
2538 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2539 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002540 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002541 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002542 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002543 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002544 (fTextureSwizzleSupport ? "YES": "NO"));
2545 GrPrintf("Unpack Row length support: %s\n",
2546 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002547 GrPrintf("Unpack Flip Y support: %s\n",
2548 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002549 GrPrintf("Pack Row length support: %s\n",
2550 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002551 GrPrintf("Pack Flip Y support: %s\n",
2552 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002553}