blob: 9906037f5d263cfe7676f1469fe18cfb1f113dc3 [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.
bsalomon@google.com316f99232011-01-13 21:28:12 +000023static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
twiz@google.com0f31ca72011-03-18 17:38:11 +000027static const GrGLenum gXfermodeCoeff2Blend[] = {
28 GR_GL_ZERO,
29 GR_GL_ONE,
30 GR_GL_SRC_COLOR,
31 GR_GL_ONE_MINUS_SRC_COLOR,
32 GR_GL_DST_COLOR,
33 GR_GL_ONE_MINUS_DST_COLOR,
34 GR_GL_SRC_ALPHA,
35 GR_GL_ONE_MINUS_SRC_ALPHA,
36 GR_GL_DST_ALPHA,
37 GR_GL_ONE_MINUS_DST_ALPHA,
38 GR_GL_CONSTANT_COLOR,
39 GR_GL_ONE_MINUS_CONSTANT_COLOR,
40 GR_GL_CONSTANT_ALPHA,
41 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
43 // extended blend coeffs
44 GR_GL_SRC1_COLOR,
45 GR_GL_ONE_MINUS_SRC1_COLOR,
46 GR_GL_SRC1_ALPHA,
47 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000048};
49
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 static const bool gCoeffReferencesBlendConst[] = {
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 false,
62 true,
63 true,
64 true,
65 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000066
67 // extended blend coeffs
68 false,
69 false,
70 false,
71 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000072 };
73 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
75
76 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
77 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
78 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
79 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
80 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
81 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
82 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
83 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
84 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
85 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
86 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
87 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
88 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
89 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
90
91 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
92 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
93 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
94 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
95
96 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
97 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000098}
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
103 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104 GrMatrix* matrix) {
105 GrAssert(NULL != texture);
106 GrAssert(NULL != matrix);
107 if (GR_Scalar1 != texture->contentScaleX() ||
108 GR_Scalar1 != texture->contentScaleY()) {
109 if (GrSamplerState::kRadial_SampleMode == mode) {
110 GrMatrix scale;
111 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
112 matrix->postConcat(scale);
113 } else if (GrSamplerState::kNormal_SampleMode == mode) {
114 GrMatrix scale;
115 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
116 matrix->postConcat(scale);
117 } else {
118 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
119 }
120 }
121 GrGLTexture::Orientation orientation = texture->orientation();
122 if (GrGLTexture::kBottomUp_Orientation == orientation) {
123 GrMatrix invY;
124 invY.setAll(GR_Scalar1, 0, 0,
125 0, -GR_Scalar1, GR_Scalar1,
126 0, 0, GrMatrix::I()[8]);
127 matrix->postConcat(invY);
128 } else {
129 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
130 }
131}
132
bsalomon@google.comd302f142011-03-03 13:54:13 +0000133bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134 const GrSamplerState& sampler) {
135 GrAssert(NULL != texture);
136 if (!sampler.getMatrix().isIdentity()) {
137 return false;
138 }
139 if (GR_Scalar1 != texture->contentScaleX() ||
140 GR_Scalar1 != texture->contentScaleY()) {
141 return false;
142 }
143 GrGLTexture::Orientation orientation = texture->orientation();
144 if (GrGLTexture::kBottomUp_Orientation == orientation) {
145 return false;
146 } else {
147 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
148 }
149 return true;
150}
151
152///////////////////////////////////////////////////////////////////////////////
153
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000154static bool gPrintStartupSpew;
155
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000156static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000157
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000158 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000159
twiz@google.com0f31ca72011-03-18 17:38:11 +0000160 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
162 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000163 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000164 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
165 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000166 // some implementations require texture to be mip-map complete before
167 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000168 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
169 GR_GL_TEXTURE_MIN_FILTER,
170 GR_GL_NEAREST));
171 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
172 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
173 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
174 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
175 GR_GL_COLOR_ATTACHMENT0,
176 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000177 GrGLenum status;
178 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000179 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
180 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000181
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000182 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000183}
184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185static bool probe_for_npot_render_target_support(const GrGLInterface* gl,
186 bool hasNPOTTextureSupport) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000187
188 /* Experimentation has found that some GLs that support NPOT textures
189 do not support FBOs with a NPOT texture. They report "unsupported" FBO
190 status. I don't know how to explicitly query for this. Do an
191 experiment. Note they may support NPOT with a renderbuffer but not a
192 texture. Presumably, the implementation bloats the renderbuffer
193 internally to the next POT.
194 */
195 if (hasNPOTTextureSupport) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000196 return fbo_test(gl, 200, 200);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000197 }
198 return false;
199}
200
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000201static int probe_for_min_render_target_height(const GrGLInterface* gl,
202 bool hasNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000203 int maxRenderTargetSize) {
204 /* The iPhone 4 has a restriction that for an FBO with texture color
205 attachment with height <= 8 then the width must be <= height. Here
206 we look for such a limitation.
207 */
208 if (gPrintStartupSpew) {
209 GrPrintf("Small height FBO texture experiments\n");
210 }
211 int minRenderTargetHeight = GR_INVAL_GLINT;
212 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
213 GrGLuint w = maxRenderTargetSize;
214 GrGLuint h = i;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000215 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000216 if (gPrintStartupSpew) {
217 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
218 }
219 minRenderTargetHeight = i;
220 break;
221 } else {
222 if (gPrintStartupSpew) {
223 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
224 }
225 }
226 }
227 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
228
229 return minRenderTargetHeight;
230}
231
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000232static int probe_for_min_render_target_width(const GrGLInterface* gl,
233 bool hasNPOTRenderTargetSupport,
234 int maxRenderTargetSize) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000235
236 if (gPrintStartupSpew) {
237 GrPrintf("Small width FBO texture experiments\n");
238 }
239 int minRenderTargetWidth = GR_INVAL_GLINT;
240 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
241 GrGLuint w = i;
242 GrGLuint h = maxRenderTargetSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000243 if (fbo_test(gl, w, h)) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000244 if (gPrintStartupSpew) {
245 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
246 }
247 minRenderTargetWidth = i;
248 break;
249 } else {
250 if (gPrintStartupSpew) {
251 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
252 }
253 }
254 }
255 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
256
257 return minRenderTargetWidth;
258}
259
260
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000261GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding)
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000262 : fStencilFormats(8) {
263
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000264 gl->ref();
265 fGL = gl;
266 fGLBinding = glBinding;
267 switch (glBinding) {
268 case kDesktop_GrGLBinding:
269 GrAssert(gl->supportsDesktop());
270 break;
271 case kES1_GrGLBinding:
272 GrAssert(gl->supportsES1());
273 break;
274 case kES2_GrGLBinding:
275 GrAssert(gl->supportsES2());
276 break;
277 default:
278 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000279 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000280
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000281 GrGLClearErr(fGL);
282
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000283 const GrGLubyte* ext;
284 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000285 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000286 const GrGLubyte* vendor;
287 const GrGLubyte* renderer;
288 const GrGLubyte* version;
289 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
290 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
291 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000292 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
293 this);
294 GrPrintf("------ VENDOR %s\n", vendor);
295 GrPrintf("------ RENDERER %s\n", renderer);
296 GrPrintf("------ VERSION %s\n", version);
297 GrPrintf("------ EXTENSIONS\n %s \n", ext);
298 }
299
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000300 fGLVersion = GrGLGetVersion(gl);
301 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000302 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000304 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000305
twiz@google.com0f31ca72011-03-18 17:38:11 +0000306 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000307 // check FS and fixed-function texture unit limits
308 // we only use textures in the fragment stage currently.
309 // checks are > to make sure we have a spare unit.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000310 if (kES1_GrGLBinding != this->glBinding()) {
311 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000312 GrAssert(maxTextureUnits > kNumStages);
313 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000314 if (kES2_GrGLBinding != this->glBinding()) {
315 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000316 GrAssert(maxTextureUnits > kNumStages);
317 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000318 if (kES2_GrGLBinding == this->glBinding()) {
319 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000320 &fMaxFragmentUniformVectors);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000321 } else if (kDesktop_GrGLBinding != this->glBinding()) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000322 GrGLint max;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000323 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000324 fMaxFragmentUniformVectors = max / 4;
325 } else {
326 fMaxFragmentUniformVectors = 16;
327 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000328
reed@google.comac10a2d2010-12-22 21:39:39 +0000329 ////////////////////////////////////////////////////////////////////////////
330 // Check for supported features.
331
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000332 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000333
twiz@google.com0f31ca72011-03-18 17:38:11 +0000334 GrGLint numFormats;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000335 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000336 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000337 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000338 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000339 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000340 f8bitPaletteSupport = true;
341 break;
342 }
343 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000344
345 if (gPrintStartupSpew) {
346 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
347 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000348
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000349 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
350 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
351 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
352 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000353
354 memset(fAASamples, 0, sizeof(fAASamples));
355 fMSFBOType = kNone_MSFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000356 if (kDesktop_GrGLBinding != this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000357 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000358 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000359 // and fbo_blit extensions.
360 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000361 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000362 fMSFBOType = kAppleES_MSFBO;
363 }
364 } else {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000365 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000366 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000367 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
368 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000369 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000370 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000372 if (gPrintStartupSpew) {
373 switch (fMSFBOType) {
374 case kNone_MSFBO:
375 GrPrintf("MSAA Support: NONE\n");
376 break;
377 case kDesktopARB_MSFBO:
378 GrPrintf("MSAA Support: DESKTOP ARB.\n");
379 break;
380 case kDesktopEXT_MSFBO:
381 GrPrintf("MSAA Support: DESKTOP EXT.\n");
382 break;
383 case kAppleES_MSFBO:
384 GrPrintf("MSAA Support: APPLE ES.\n");
385 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000386 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000387 }
388
389 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000390 GrGLint maxSamples;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000391 GR_GL_GetIntegerv(gl, GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000392 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000393 fAASamples[kNone_GrAALevel] = 0;
394 fAASamples[kLow_GrAALevel] = GrMax(2,
395 GrFixedFloorToInt((GR_FixedHalf) *
396 maxSamples));
397 fAASamples[kMed_GrAALevel] = GrMax(2,
398 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
399 maxSamples));
400 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000401 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000402 if (gPrintStartupSpew) {
403 GrPrintf("\tMax Samples: %d\n", maxSamples);
404 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000406 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000407
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000408 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000409 fHasStencilWrap = (fGLVersion >= GR_GL_VER(1,4)) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000410 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000411 } else {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000412 fHasStencilWrap = (fGLVersion >= GR_GL_VER(2,0)) ||
413 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000414 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000415 if (gPrintStartupSpew) {
416 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
417 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000418
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000419 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000420 // we could also look for GL_ATI_separate_stencil extension or
421 // GL_EXT_stencil_two_side but they use different function signatures
422 // than GL2.0+ (and than each other).
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000423 fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000424 // supported on GL 1.4 and higher or by extension
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000425 fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000426 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000427 } else {
428 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
429 // an ES1 extension.
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000430 fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000431 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000432 fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(2,0)) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000433 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000434 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000435 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000436 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
437 (fTwoSidedStencilSupport ? "YES" : "NO"),
438 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000439 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000440
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000441 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000442 fRGBA8Renderbuffer = true;
443 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000444 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000445 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000446 if (gPrintStartupSpew) {
447 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
448 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
450
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000451 if (kDesktop_GrGLBinding != this->glBinding()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000452 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000453 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000454 }
455 }
456
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000457 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000458 fBufferLockSupport = true; // we require VBO support and the desktop VBO
459 // extension includes glMapBuffer.
460 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000461 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000462 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000463
reed@google.comeeeb5a02010-12-23 15:12:59 +0000464 if (gPrintStartupSpew) {
465 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
466 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000468 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000469 if (fGLVersion >= GR_GL_VER(2,0) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000470 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000471 fNPOTTextureTileSupport = true;
472 fNPOTTextureSupport = true;
473 } else {
474 fNPOTTextureTileSupport = false;
475 fNPOTTextureSupport = false;
476 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000477 } else {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000478 if (fGLVersion >= GR_GL_VER(2,0)) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000479 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000480 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000481 } else {
482 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000483 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000484 fNPOTTextureTileSupport = false;
485 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000486 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000487
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000488 fAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com205d4602011-04-25 12:43:45 +0000489
reed@google.comac10a2d2010-12-22 21:39:39 +0000490 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000491 // Experiments to determine limitations that can't be queried.
492 // TODO: Make these a preprocess that generate some compile time constants.
493 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000494
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000495 int expectNPOTTargets = gl->fNPOTRenderTargetSupport;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000496 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000497 fNPOTRenderTargetSupport =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000498 probe_for_npot_render_target_support(gl, fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000499 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000500 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
501 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000502 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000503
bsalomon@google.com0748f212011-02-01 22:56:16 +0000504 if (gPrintStartupSpew) {
505 if (fNPOTTextureSupport) {
506 GrPrintf("NPOT textures supported\n");
507 if (fNPOTTextureTileSupport) {
508 GrPrintf("NPOT texture tiling supported\n");
509 } else {
510 GrPrintf("NPOT texture tiling NOT supported\n");
511 }
512 if (fNPOTRenderTargetSupport) {
513 GrPrintf("NPOT render targets supported\n");
514 } else {
515 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000516 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000518 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000519 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000520 }
521
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000522 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
523 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000524 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000525 // attachment, hence this min:
526 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000527
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000528 fMinRenderTargetHeight = gl->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000529 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000530 fMinRenderTargetHeight =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000531 probe_for_min_render_target_height(gl,fNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000532 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000533 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000534
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000535 fMinRenderTargetWidth = gl->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000536 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000537 fMinRenderTargetWidth =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000538 probe_for_min_render_target_width(gl, fNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000539 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000540 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000541
bsalomon@google.comfe676522011-06-17 18:12:21 +0000542 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000543}
544
545GrGpuGL::~GrGpuGL() {
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000546 // This subclass must do this before the base class destructor runs
547 // since we will unref the GrGLInterface.
548 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000549 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000550}
551
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000552void GrGpuGL::resetContext() {
553 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000554 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000555 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000556
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000557 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000558 GL_CALL(Disable(GR_GL_DEPTH_TEST));
559 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000560
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000561 GL_CALL(Disable(GR_GL_CULL_FACE));
562 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000563 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000564
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000565 GL_CALL(Disable(GR_GL_DITHER));
566 if (kDesktop_GrGLBinding == this->glBinding()) {
567 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
568 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
569 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000570 fHWAAState.fMSAAEnabled = false;
571 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000572 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000573
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000574 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000575 fHWDrawState.fFlagBits = 0;
576
reed@google.comac10a2d2010-12-22 21:39:39 +0000577 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000578 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000579
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000580 // invalid
581 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000582
reed@google.comac10a2d2010-12-22 21:39:39 +0000583 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000584 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
585 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000586
587 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000588 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000589
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000591
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000592 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000593
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000594 for (int s = 0; s < kNumStages; ++s) {
595 fHWDrawState.fTextures[s] = NULL;
596 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
597 -GR_ScalarMax,
598 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000599 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000600 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000601 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000602
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000603 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000604 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000605 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000606 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000607
bsalomon@google.comd302f142011-03-03 13:54:13 +0000608 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000610 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000611
612 fHWGeometryState.fIndexBuffer = NULL;
613 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000614
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000615 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000616
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000617 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 fHWDrawState.fRenderTarget = NULL;
619}
620
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000621GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
622
623 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
624 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
625 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
626 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
627
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000628 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000629 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000630
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000631 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000632 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000633 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000634 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000635 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000636 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000637 } else {
638 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000639 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000640 }
641 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000642 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000643 }
644 // we don't know what the RB ids are without glGets and we don't care
645 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000646 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000647 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000648 if (desc.fStencilBits) {
649 GrGLStencilBuffer::Format format;
650 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
651 format.fPacked = false;
652 format.fStencilBits = desc.fStencilBits;
653 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000654 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
655 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000656 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000657 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000658 }
659
660 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000661 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000662 GrGLenum dontCare;
663 if (!canBeTexture(desc.fConfig, &dontCare,
664 &texDesc.fUploadFormat,
665 &texDesc.fUploadType)) {
666 return NULL;
667 }
668
669 GrGLTexture::TexParams params;
670
671 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
672 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
673
bsalomon@google.com90405932011-06-17 15:56:55 +0000674 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000675 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000676 texDesc.fTextureID = desc.fPlatformTexture;
677 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
678 texDesc.fOwnsID = false;
679
680 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000681 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000682 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000683 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000684 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000685 } else {
686 return new GrGLTexture(this, texDesc, params);
687 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000688 } else {
689 GrGLIRect viewport;
690 viewport.fLeft = 0;
691 viewport.fBottom = 0;
692 viewport.fWidth = desc.fWidth;
693 viewport.fHeight = desc.fHeight;
694
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000695 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000696 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000697 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000698 }
699}
700
bsalomon@google.com5782d712011-01-21 21:03:59 +0000701///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000702
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000703static const GrGLuint kUnknownBitCount = ~0;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000704
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000705void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000706
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000707 // Build up list of legal stencil formats (though perhaps not supported on
708 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000709
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000710 // these consts are in order of most preferred to least preferred
711 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000712 static const GrGLStencilBuffer::Format
713 // internal Format stencil bits total bits packed?
714 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
715 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
716 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
717 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
718 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
719 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000720
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000721 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000722 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000723 this->hasExtension("GL_EXT_packed_depth_stencil") ||
724 this->hasExtension("GL_ARB_framebuffer_object");
725
726 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
727 // require FBO support we can expect these are legal formats and don't
728 // check. These also all support the unsized GL_STENCIL_INDEX.
729 fStencilFormats.push_back() = gS8;
730 fStencilFormats.push_back() = gS16;
731 if (supportsPackedDS) {
732 fStencilFormats.push_back() = gD24S8;
733 }
734 fStencilFormats.push_back() = gS4;
735 if (supportsPackedDS) {
736 fStencilFormats.push_back() = gDS;
737 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000738 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000739 // ES2 has STENCIL_INDEX8 without extensions.
740 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
741 // introduces tokens for S1 thu S8 but there are separate extensions
742 // that make them legal (GL_OES_stencil1, ...).
743 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
744 // ES doesn't support using the unsized formats.
745
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000746 if (fGLVersion >= GR_GL_VER(2,0) ||
747 this->hasExtension("GL_OES_stencil8")) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000748 fStencilFormats.push_back() = gS8;
749 }
750 //fStencilFormats.push_back() = gS16;
751 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
752 fStencilFormats.push_back() = gD24S8;
753 }
754 if (this->hasExtension("GL_OES_stencil4")) {
755 fStencilFormats.push_back() = gS4;
756 }
757 // we require some stencil format.
758 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000759 }
760}
761
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000762////////////////////////////////////////////////////////////////////////////////
763
764void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
765 GrGLenum internalFormat,
766 const void* data,
767 size_t rowBytes) {
768 // we assume the texture is bound;
769 if (!rowBytes) {
770 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
771 }
772
773 // in case we need a temporary, trimmed copy of the src pixels
774 SkAutoSMalloc<128 * 128> tempStorage;
775
776 /*
777 * check whether to allocate a temporary buffer for flipping y or
778 * because our data has extra bytes past each row. If so, we need
779 * to trim those off here, since GL ES doesn't let us specify
780 * GL_UNPACK_ROW_LENGTH.
781 */
782 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000783 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000784 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000785 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
786 rowBytes / desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000787 }
788 } else {
789 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
790 if (data && (trimRowBytes < rowBytes || flipY)) {
791 // copy the data into our new storage, skipping the trailing bytes
792 size_t trimSize = desc.fContentHeight * trimRowBytes;
793 const char* src = (const char*)data;
794 if (flipY) {
795 src += (desc.fContentHeight - 1) * rowBytes;
796 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000797 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000798 for (int y = 0; y < desc.fContentHeight; y++) {
799 memcpy(dst, src, trimRowBytes);
800 if (flipY) {
801 src -= rowBytes;
802 } else {
803 src += rowBytes;
804 }
805 dst += trimRowBytes;
806 }
807 // now point data to our trimmed version
808 data = tempStorage.get();
809 rowBytes = trimRowBytes;
810 }
811 }
812
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000813 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000814 if (kIndex_8_GrPixelConfig == desc.fFormat &&
815 supports8BitPalette()) {
816 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
817 GrAssert(desc.fContentWidth == desc.fAllocWidth);
818 GrAssert(desc.fContentHeight == desc.fAllocHeight);
819 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
820 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000821 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
822 desc.fAllocWidth, desc.fAllocHeight,
823 0, imageSize, data));
824 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000825 } else {
826 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
827 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000828 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
829 desc.fAllocWidth, desc.fAllocHeight,
830 0, desc.fUploadFormat, desc.fUploadType, NULL));
831 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
832 desc.fContentHeight, desc.fUploadFormat,
833 desc.fUploadType, data));
834 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000835
836 int extraW = desc.fAllocWidth - desc.fContentWidth;
837 int extraH = desc.fAllocHeight - desc.fContentHeight;
838 int maxTexels = extraW * extraH;
839 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
840 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
841
842 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
843
844 // rowBytes is actual stride between rows in data
845 // rowDataBytes is the actual amount of non-pad data in a row
846 // and the stride used for uploading extraH rows.
847 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
848 if (extraH) {
849 uint8_t* lastRowStart = (uint8_t*) data +
850 (desc.fContentHeight - 1) * rowBytes;
851 uint8_t* extraRowStart = (uint8_t*)texels.get();
852
853 for (int i = 0; i < extraH; ++i) {
854 memcpy(extraRowStart, lastRowStart, rowDataBytes);
855 extraRowStart += rowDataBytes;
856 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000857 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
858 desc.fContentHeight, desc.fContentWidth,
859 extraH, desc.fUploadFormat,
860 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000861 }
862 if (extraW) {
863 uint8_t* edgeTexel = (uint8_t*)data +
864 rowDataBytes - desc.fUploadByteCount;
865 uint8_t* extraTexel = (uint8_t*)texels.get();
866 for (int j = 0; j < desc.fContentHeight; ++j) {
867 for (int i = 0; i < extraW; ++i) {
868 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
869 extraTexel += desc.fUploadByteCount;
870 }
871 edgeTexel += rowBytes;
872 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000873 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
874 0, extraW, desc.fContentHeight,
875 desc.fUploadFormat, desc.fUploadType,
876 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000877 }
878 if (extraW && extraH) {
879 uint8_t* cornerTexel = (uint8_t*)data +
880 desc.fContentHeight * rowBytes -
881 desc.fUploadByteCount;
882 uint8_t* extraTexel = (uint8_t*)texels.get();
883 for (int i = 0; i < extraW*extraH; ++i) {
884 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
885 extraTexel += desc.fUploadByteCount;
886 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000887 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
888 desc.fContentHeight, extraW, extraH,
889 desc.fUploadFormat, desc.fUploadType,
890 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000891 }
892
893 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000894 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
895 desc.fAllocWidth, desc.fAllocHeight, 0,
896 desc.fUploadFormat, desc.fUploadType, data));
897 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000898 }
899 }
900}
901
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000902bool GrGpuGL::createRenderTargetObjects(int width, int height,
903 GrGLuint texID,
904 GrGLRenderTarget::Desc* desc) {
905 desc->fMSColorRenderbufferID = 0;
906 desc->fRTFBOID = 0;
907 desc->fTexFBOID = 0;
908 desc->fOwnIDs = true;
909
910 GrGLenum status;
911 GrGLint err;
912
bsalomon@google.comab15d612011-08-09 12:57:56 +0000913 GrGLenum msColorFormat = 0; // suppress warning
914
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000915 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000916 if (!desc->fTexFBOID) {
917 goto FAILED;
918 }
919
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000920
921 // If we are using multisampling we will create two FBOS. We render
922 // to one and then resolve to the texture bound to the other.
923 if (desc->fSampleCnt > 1 && kNone_MSFBO != fMSFBOType) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000924 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
925 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000926 if (!desc->fRTFBOID ||
927 !desc->fMSColorRenderbufferID ||
928 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
929 goto FAILED;
930 }
931 } else {
932 desc->fRTFBOID = desc->fTexFBOID;
933 }
934
935 if (desc->fRTFBOID != desc->fTexFBOID) {
936 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000937 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000938 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000939 GR_GL_CALL_NOERRCHECK(this->glInterface(),
940 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
941 desc->fSampleCnt,
942 msColorFormat,
943 width, height));
944 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945 if (err != GR_GL_NO_ERROR) {
946 goto FAILED;
947 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000948 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
949 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950 GR_GL_COLOR_ATTACHMENT0,
951 GR_GL_RENDERBUFFER,
952 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000953 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000954 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
955 goto FAILED;
956 }
957 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000958 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000959
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000960 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
961 GR_GL_COLOR_ATTACHMENT0,
962 GR_GL_TEXTURE_2D,
963 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000964 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000965 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
966 goto FAILED;
967 }
968
969 return true;
970
971FAILED:
972 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000973 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000974 }
975 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000976 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000977 }
978 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000979 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000980 }
981 return false;
982}
983
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000984// good to set a break-point here to know when createTexture fails
985static GrTexture* return_null_texture() {
986// GrAssert(!"null texture");
987 return NULL;
988}
989
990#if GR_DEBUG
991static size_t as_size_t(int x) {
992 return x;
993}
994#endif
995
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000996GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000997 const void* srcData,
998 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000999
1000#if GR_COLLECT_STATS
1001 ++fStats.fTextureCreateCnt;
1002#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001003
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001004 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001005 GR_GL_NEAREST,
1006 GR_GL_CLAMP_TO_EDGE,
1007 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001008 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001009
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001010 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001011 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001012 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001013
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001014 glTexDesc.fContentWidth = desc.fWidth;
1015 glTexDesc.fContentHeight = desc.fHeight;
1016 glTexDesc.fAllocWidth = desc.fWidth;
1017 glTexDesc.fAllocHeight = desc.fHeight;
1018 glTexDesc.fFormat = desc.fFormat;
1019 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001020
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001021 glRTDesc.fMSColorRenderbufferID = 0;
1022 glRTDesc.fRTFBOID = 0;
1023 glRTDesc.fTexFBOID = 0;
1024 glRTDesc.fOwnIDs = true;
1025 glRTDesc.fConfig = glTexDesc.fFormat;
1026
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001027 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001028 if (!canBeTexture(desc.fFormat,
1029 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001030 &glTexDesc.fUploadFormat,
1031 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001032 return return_null_texture();
1033 }
1034
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001035 // We keep GrRenderTargets in GL's normal orientation so that they
1036 // can be drawn to by the outside world without the client having
1037 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001038 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001039 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001040
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001042 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001043 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 GrPrintf("AA RT requested but not supported on this platform.");
1045 }
1046
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001047 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001048
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001050 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001051 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1052 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001053 }
1054
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001055 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001056 glTexDesc.fAllocWidth);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001057 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001058 glTexDesc.fAllocHeight);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001059 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1060 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001061 return return_null_texture();
1062 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001063 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001064 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1065 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1066 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1067 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001068 return return_null_texture();
1069 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 }
1071
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001072 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001073 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001074 return return_null_texture();
1075 }
1076
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001077 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001078 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1079 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1080 GR_GL_TEXTURE_MAG_FILTER,
1081 DEFAULT_PARAMS.fFilter));
1082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1083 GR_GL_TEXTURE_MIN_FILTER,
1084 DEFAULT_PARAMS.fFilter));
1085 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1086 GR_GL_TEXTURE_WRAP_S,
1087 DEFAULT_PARAMS.fWrapS));
1088 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1089 GR_GL_TEXTURE_WRAP_T,
1090 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001091
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001092 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001093
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001094 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001096 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001097#if GR_COLLECT_STATS
1098 ++fStats.fRenderTargetCreateCnt;
1099#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001100 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1101 glTexDesc.fAllocHeight,
1102 glTexDesc.fTextureID,
1103 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001104 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001105 return return_null_texture();
1106 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001107 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1108 } else {
1109 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001110 }
1111#ifdef TRACE_TEXTURE_CREATION
1112 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1113 tex->fTextureID, width, height, tex->fUploadByteCount);
1114#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 return tex;
1116}
1117
1118namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001119void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1120 GrGLuint rb,
1121 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001122 // we shouldn't ever know one size and not the other
1123 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1124 (kUnknownBitCount == format->fTotalBits));
1125 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001126 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1128 (GrGLint*)&format->fStencilBits);
1129 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001130 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001131 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1132 (GrGLint*)&format->fTotalBits);
1133 format->fTotalBits += format->fStencilBits;
1134 } else {
1135 format->fTotalBits = format->fStencilBits;
1136 }
1137 }
1138}
1139}
1140
1141bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1142 int width, int height) {
1143
1144 // All internally created RTs are also textures. We don't create
1145 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1146 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001147 GrAssert(width >= rt->allocatedWidth());
1148 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149
1150 int samples = rt->numSamples();
1151 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001152 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001153 if (!sbID) {
1154 return false;
1155 }
1156
1157 GrGLStencilBuffer* sb = NULL;
1158
1159 int stencilFmtCnt = fStencilFormats.count();
1160 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001161 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 // we start with the last stencil format that succeeded in hopes
1163 // that we won't go through this loop more than once after the
1164 // first (painful) stencil creation.
1165 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001166 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001167 // version on a GL that doesn't have an MSAA extension.
1168 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1170 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 GR_GL_RENDERBUFFER,
1172 samples,
1173 fStencilFormats[sIdx].fInternalFormat,
1174 width,
1175 height));
1176 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001177 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1178 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 fStencilFormats[sIdx].fInternalFormat,
1180 width, height));
1181 }
1182
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001183 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 if (err == GR_GL_NO_ERROR) {
1185 // After sized formats we attempt an unsized format and take whatever
1186 // sizes GL gives us. In that case we query for the size.
1187 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001188 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001189 sb = new GrGLStencilBuffer(this, sbID, width, height,
1190 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1192 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001193 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 return true;
1196 }
1197 sb->abandon(); // otherwise we lose sbID
1198 sb->unref();
1199 }
1200 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001202 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203}
1204
1205bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1206 GrRenderTarget* rt) {
1207 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1208
1209 GrGLuint fbo = glrt->renderFBOID();
1210
1211 if (NULL == sb) {
1212 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001214 GR_GL_STENCIL_ATTACHMENT,
1215 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 GR_GL_DEPTH_ATTACHMENT,
1218 GR_GL_RENDERBUFFER, 0));
1219#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001220 GrGLenum status;
1221 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1223#endif
1224 }
1225 return true;
1226 } else {
1227 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1228 GrGLuint rb = glsb->renderbufferID();
1229
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1232 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 GR_GL_STENCIL_ATTACHMENT,
1234 GR_GL_RENDERBUFFER, rb));
1235 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001236 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237 GR_GL_DEPTH_ATTACHMENT,
1238 GR_GL_RENDERBUFFER, rb));
1239 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 GR_GL_DEPTH_ATTACHMENT,
1242 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001243 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001245 GrGLenum status;
1246 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249 GR_GL_STENCIL_ATTACHMENT,
1250 GR_GL_RENDERBUFFER, 0));
1251 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001252 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001253 GR_GL_DEPTH_ATTACHMENT,
1254 GR_GL_RENDERBUFFER, 0));
1255 }
1256 return false;
1257 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001258 return true;
1259 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001261}
1262
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001263////////////////////////////////////////////////////////////////////////////////
1264
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001265GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001266 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001267 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001269 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001270 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001271 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001273 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1274 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1275 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1276 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1277 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 // deleting bound buffer does implicit bind to 0
1279 fHWGeometryState.fVertexBuffer = NULL;
1280 return NULL;
1281 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001282 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 size, dynamic);
1284 fHWGeometryState.fVertexBuffer = vertexBuffer;
1285 return vertexBuffer;
1286 }
1287 return NULL;
1288}
1289
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001290GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001291 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001292 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001294 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1295 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001297 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1298 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1299 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1300 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1301 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 // deleting bound buffer does implicit bind to 0
1303 fHWGeometryState.fIndexBuffer = NULL;
1304 return NULL;
1305 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001306 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 size, dynamic);
1308 fHWGeometryState.fIndexBuffer = indexBuffer;
1309 return indexBuffer;
1310 }
1311 return NULL;
1312}
1313
reed@google.comac10a2d2010-12-22 21:39:39 +00001314void GrGpuGL::flushScissor(const GrIRect* rect) {
1315 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001316 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001317 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001318
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001319 GrGLIRect scissor;
1320 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001321 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001322 rect->width(), rect->height());
1323 if (scissor.contains(vp)) {
1324 rect = NULL;
1325 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 }
1327
1328 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001329 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001330 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 fHWBounds.fScissorRect = scissor;
1332 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001334 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 fHWBounds.fScissorEnabled = true;
1336 }
1337 } else {
1338 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001339 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 fHWBounds.fScissorEnabled = false;
1341 }
1342 }
1343}
1344
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001345void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001346 if (NULL == fCurrDrawState.fRenderTarget) {
1347 return;
1348 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001349 GrIRect r;
1350 if (NULL != rect) {
1351 // flushScissor expects rect to be clipped to the target.
1352 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001353 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1354 fCurrDrawState.fRenderTarget->height());
1355 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001356 rect = &r;
1357 } else {
1358 return;
1359 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001360 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001361 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001362 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001363 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001364 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001365 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1366 GrColorUnpackG(color)/255.f,
1367 GrColorUnpackB(color)/255.f,
1368 GrColorUnpackA(color)/255.f));
1369 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001370}
1371
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001372void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001373 if (NULL == fCurrDrawState.fRenderTarget) {
1374 return;
1375 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001376
1377 this->flushRenderTarget(&GrIRect::EmptyIRect());
1378
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001380 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001381 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001383 GL_CALL(StencilMask(0xffffffff));
1384 GL_CALL(ClearStencil(0));
1385 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001386 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001387}
1388
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001389void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001390 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001391
1392 // this should only be called internally when we know we have a
1393 // stencil buffer.
1394 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001395 GrGLint stencilBitCount =
1396 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001397#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001398 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001399 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001400#else
1401 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001402 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001403 // turned into draws. Our contract on GrDrawTarget says that
1404 // changing the clip between stencil passes may or may not
1405 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001406 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001407#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001408 GrGLint value;
1409 if (insideClip) {
1410 value = (1 << (stencilBitCount - 1));
1411 } else {
1412 value = 0;
1413 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001414 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001415 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001416 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001417 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001418 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001419 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001420}
1421
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001422void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001423 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001424}
1425
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001426bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1427 int left, int top, int width, int height,
1428 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001429 GrGLenum internalFormat; // we don't use this for glReadPixels
1430 GrGLenum format;
1431 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001432 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1433 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001434 }
1435 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1436 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1437 switch (tgt->getResolveType()) {
1438 case GrGLRenderTarget::kCantResolve_ResolveType:
1439 return false;
1440 case GrGLRenderTarget::kAutoResolves_ResolveType:
1441 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1442 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001443 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001444 break;
1445 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001446 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001447 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001448 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1449 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001450 break;
1451 default:
1452 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001453 }
1454
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001455 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001456
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001457 // the read rect is viewport-relative
1458 GrGLIRect readRect;
1459 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001460 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1461 readRect.fWidth, readRect.fHeight,
1462 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001463
1464 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1465 // API presents top-to-bottom
1466 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001467 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001468 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001469 void* tmp = rowStorage.get();
1470
1471 const int halfY = height >> 1;
1472 char* top = reinterpret_cast<char*>(buffer);
1473 char* bottom = top + (height - 1) * stride;
1474 for (int y = 0; y < halfY; y++) {
1475 memcpy(tmp, top, stride);
1476 memcpy(top, bottom, stride);
1477 memcpy(bottom, tmp, stride);
1478 top += stride;
1479 bottom -= stride;
1480 }
1481 }
1482 return true;
1483}
1484
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001485void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001486
1487 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1488
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001489 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001490 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001491 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001492 #if GR_COLLECT_STATS
1493 ++fStats.fRenderTargetChngCnt;
1494 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001495 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001496 GrGLenum status;
1497 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001498 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001499 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 }
1501 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001502 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001503 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001504 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001505 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001506 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001507 fHWBounds.fViewportRect = vp;
1508 }
1509 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001510 if (NULL == bound || !bound->isEmpty()) {
1511 rt->flagAsNeedingResolve(bound);
1512 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001513}
1514
twiz@google.com0f31ca72011-03-18 17:38:11 +00001515GrGLenum gPrimitiveType2GLMode[] = {
1516 GR_GL_TRIANGLES,
1517 GR_GL_TRIANGLE_STRIP,
1518 GR_GL_TRIANGLE_FAN,
1519 GR_GL_POINTS,
1520 GR_GL_LINES,
1521 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001522};
1523
bsalomon@google.comd302f142011-03-03 13:54:13 +00001524#define SWAP_PER_DRAW 0
1525
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001526#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001527 #if GR_MAC_BUILD
1528 #include <AGL/agl.h>
1529 #elif GR_WIN32_BUILD
1530 void SwapBuf() {
1531 DWORD procID = GetCurrentProcessId();
1532 HWND hwnd = GetTopWindow(GetDesktopWindow());
1533 while(hwnd) {
1534 DWORD wndProcID = 0;
1535 GetWindowThreadProcessId(hwnd, &wndProcID);
1536 if(wndProcID == procID) {
1537 SwapBuffers(GetDC(hwnd));
1538 }
1539 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1540 }
1541 }
1542 #endif
1543#endif
1544
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001545void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1546 uint32_t startVertex,
1547 uint32_t startIndex,
1548 uint32_t vertexCount,
1549 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001550 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1551
twiz@google.com0f31ca72011-03-18 17:38:11 +00001552 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001553
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001554 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1555 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1556
1557 // our setupGeometry better have adjusted this to zero since
1558 // DrawElements always draws from the begining of the arrays for idx 0.
1559 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001560
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001561 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1562 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001563#if SWAP_PER_DRAW
1564 glFlush();
1565 #if GR_MAC_BUILD
1566 aglSwapBuffers(aglGetCurrentContext());
1567 int set_a_break_pt_here = 9;
1568 aglSwapBuffers(aglGetCurrentContext());
1569 #elif GR_WIN32_BUILD
1570 SwapBuf();
1571 int set_a_break_pt_here = 9;
1572 SwapBuf();
1573 #endif
1574#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001575}
1576
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001577void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1578 uint32_t startVertex,
1579 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001580 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1581
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001582 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1583
1584 // our setupGeometry better have adjusted this to zero.
1585 // DrawElements doesn't take an offset so we always adjus the startVertex.
1586 GrAssert(0 == startVertex);
1587
1588 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1589 // account for startVertex in the DrawElements case. So we always
1590 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001591 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001592#if SWAP_PER_DRAW
1593 glFlush();
1594 #if GR_MAC_BUILD
1595 aglSwapBuffers(aglGetCurrentContext());
1596 int set_a_break_pt_here = 9;
1597 aglSwapBuffers(aglGetCurrentContext());
1598 #elif GR_WIN32_BUILD
1599 SwapBuf();
1600 int set_a_break_pt_here = 9;
1601 SwapBuf();
1602 #endif
1603#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001604}
1605
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001606void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001607
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001608 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 GrAssert(kNone_MSFBO != fMSFBOType);
1610 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001611 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1612 rt->renderFBOID()));
1613 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1614 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001615 #if GR_COLLECT_STATS
1616 ++fStats.fRenderTargetChngCnt;
1617 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001618 // make sure we go through flushRenderTarget() since we've modified
1619 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001621 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001622 const GrIRect dirtyRect = rt->getResolveRect();
1623 GrGLIRect r;
1624 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1625 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001626
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001627 if (kAppleES_MSFBO == fMSFBOType) {
1628 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001629 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1630 GL_CALL(Scissor(r.fLeft, r.fBottom,
1631 r.fWidth, r.fHeight));
1632 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001633 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001634 fHWBounds.fScissorEnabled = true;
1635 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001636 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001637 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001638 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1639 flushScissor(NULL);
1640 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001641 int right = r.fLeft + r.fWidth;
1642 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001643 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1644 r.fLeft, r.fBottom, right, top,
1645 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001647 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001648 }
1649}
1650
twiz@google.com0f31ca72011-03-18 17:38:11 +00001651static const GrGLenum grToGLStencilFunc[] = {
1652 GR_GL_ALWAYS, // kAlways_StencilFunc
1653 GR_GL_NEVER, // kNever_StencilFunc
1654 GR_GL_GREATER, // kGreater_StencilFunc
1655 GR_GL_GEQUAL, // kGEqual_StencilFunc
1656 GR_GL_LESS, // kLess_StencilFunc
1657 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1658 GR_GL_EQUAL, // kEqual_StencilFunc,
1659 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001660};
1661GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1662GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1663GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1664GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1665GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1666GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1667GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1668GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1669GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1670
twiz@google.com0f31ca72011-03-18 17:38:11 +00001671static const GrGLenum grToGLStencilOp[] = {
1672 GR_GL_KEEP, // kKeep_StencilOp
1673 GR_GL_REPLACE, // kReplace_StencilOp
1674 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1675 GR_GL_INCR, // kIncClamp_StencilOp
1676 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1677 GR_GL_DECR, // kDecClamp_StencilOp
1678 GR_GL_ZERO, // kZero_StencilOp
1679 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001680};
1681GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1682GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1683GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1684GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1685GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1686GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1687GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1688GR_STATIC_ASSERT(6 == kZero_StencilOp);
1689GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1690
reed@google.comac10a2d2010-12-22 21:39:39 +00001691void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001692 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001693
1694 // use stencil for clipping if clipping is enabled and the clip
1695 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001696 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001698 bool stencilChange = fHWStencilClip != stencilClip ||
1699 fHWDrawState.fStencilSettings != *settings ||
1700 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1701 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001702
1703 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001704
bsalomon@google.comd302f142011-03-03 13:54:13 +00001705 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1706 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001707
bsalomon@google.comd302f142011-03-03 13:54:13 +00001708 if (settings->isDisabled()) {
1709 if (stencilClip) {
1710 settings = &gClipStencilSettings;
1711 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001712 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001713
1714 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001715 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001716 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001717 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001718 #if GR_DEBUG
1719 if (!fStencilWrapOpsSupport) {
1720 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1721 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1722 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1723 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1724 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1725 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1726 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1727 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1728 }
1729 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001730 int stencilBits = 0;
1731 GrStencilBuffer* stencilBuffer =
1732 fCurrDrawState.fRenderTarget->getStencilBuffer();
1733 if (NULL != stencilBuffer) {
1734 stencilBits = stencilBuffer->bits();
1735 }
1736 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001737 GrAssert(stencilBits ||
1738 (GrStencilSettings::gDisabled ==
1739 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001740 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1741 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001742
1743 unsigned int frontRef = settings->fFrontFuncRef;
1744 unsigned int frontMask = settings->fFrontFuncMask;
1745 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001746 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001747
1748 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1749
1750 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1751 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1752 } else {
1753 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1754
1755 ConvertStencilFuncAndMask(settings->fFrontFunc,
1756 stencilClip,
1757 clipStencilMask,
1758 userStencilMask,
1759 &frontRef,
1760 &frontMask);
1761 frontWriteMask &= userStencilMask;
1762 }
1763 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001764 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001765 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001766 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001767 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001768 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001769 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001770 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001771 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001772 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001773
1774 unsigned int backRef = settings->fBackFuncRef;
1775 unsigned int backMask = settings->fBackFuncMask;
1776 unsigned int backWriteMask = settings->fBackWriteMask;
1777
1778
1779 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1780 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1781 backFunc = grToGLStencilFunc[settings->fBackFunc];
1782 } else {
1783 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1784 ConvertStencilFuncAndMask(settings->fBackFunc,
1785 stencilClip,
1786 clipStencilMask,
1787 userStencilMask,
1788 &backRef,
1789 &backMask);
1790 backWriteMask &= userStencilMask;
1791 }
1792
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001793 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1794 frontRef, frontMask));
1795 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1796 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1797 backRef, backMask));
1798 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1799 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1800 grToGLStencilOp[settings->fFrontFailOp],
1801 grToGLStencilOp[settings->fFrontPassOp],
1802 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001804 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1805 grToGLStencilOp[settings->fBackFailOp],
1806 grToGLStencilOp[settings->fBackPassOp],
1807 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001808 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001809 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1810 GL_CALL(StencilMask(frontWriteMask));
1811 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001812 grToGLStencilOp[settings->fFrontPassOp],
1813 grToGLStencilOp[settings->fFrontPassOp]));
1814 }
1815 }
1816 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001817 fHWStencilClip = stencilClip;
1818 }
1819}
1820
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001821void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001822 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001823 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1824 // smooth lines.
1825
1826 // we prefer smooth lines over multisampled lines
1827 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001828 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001829 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001830 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001831 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001832 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001833 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001834 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001835 fHWAAState.fSmoothLineEnabled = false;
1836 }
1837 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1838 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001839 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001840 fHWAAState.fMSAAEnabled = false;
1841 }
1842 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1843 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
1844 fHWAAState.fMSAAEnabled) {
1845 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001846 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001847 fHWAAState.fMSAAEnabled = false;
1848 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001849 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001850 fHWAAState.fMSAAEnabled = true;
1851 }
1852 }
1853 }
1854}
1855
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001856void GrGpuGL::flushBlend(GrPrimitiveType type,
1857 GrBlendCoeff srcCoeff,
1858 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001859 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001860 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001861 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001862 fHWBlendDisabled = false;
1863 }
1864 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1865 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001866 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1867 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001868 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1869 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1870 }
1871 } else {
1872 bool blendOff = canDisableBlend();
1873 if (fHWBlendDisabled != blendOff) {
1874 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001875 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001876 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001877 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001878 }
1879 fHWBlendDisabled = blendOff;
1880 }
1881 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001882 if (fHWDrawState.fSrcBlend != srcCoeff ||
1883 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001884 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1885 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001886 fHWDrawState.fSrcBlend = srcCoeff;
1887 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001888 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001889 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1890 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001891 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1892
1893 float c[] = {
1894 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1895 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1896 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1897 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1898 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001899 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001900 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1901 }
1902 }
1903 }
1904}
1905
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001906static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1907 switch (filter) {
1908 case GrSamplerState::kBilinear_Filter:
1909 case GrSamplerState::k4x4Downsample_Filter:
1910 return GR_GL_LINEAR;
1911 case GrSamplerState::kNearest_Filter:
1912 case GrSamplerState::kConvolution_Filter:
1913 return GR_GL_NEAREST;
1914 default:
1915 GrAssert(!"Unknown filter type");
1916 return GR_GL_LINEAR;
1917 }
1918}
1919
bsalomon@google.comffca4002011-02-22 20:34:01 +00001920bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001921
1922 // GrGpu::setupClipAndFlushState should have already checked this
1923 // and bailed if not true.
1924 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001925
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001926 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001927 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001928 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001929 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001930
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001931 // true for now, but maybe not with GrEffect.
1932 GrAssert(NULL != nextTexture);
1933 // if we created a rt/tex and rendered to it without using a
1934 // texture and now we're texuring from the rt it will still be
1935 // the last bound texture, but it needs resolving. So keep this
1936 // out of the "last != next" check.
1937 GrGLRenderTarget* texRT =
1938 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1939 if (NULL != texRT) {
1940 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001941 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001942
1943 if (fHWDrawState.fTextures[s] != nextTexture) {
1944 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001945 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001946 #if GR_COLLECT_STATS
1947 ++fStats.fTextureChngCnt;
1948 #endif
1949 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1950 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001951 // The texture matrix has to compensate for texture width/height
1952 // and NPOT-embedded-in-POT
1953 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001954 }
1955
1956 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
1957 const GrGLTexture::TexParams& oldTexParams =
1958 nextTexture->getTexParams();
1959 GrGLTexture::TexParams newTexParams;
1960
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001961 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001962
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001963 const GrGLenum* wraps =
1964 GrGLTexture::WrapMode2GLWrap(this->glBinding());
1965 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1966 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001967
1968 if (newTexParams.fFilter != oldTexParams.fFilter) {
1969 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001970 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1971 GR_GL_TEXTURE_MAG_FILTER,
1972 newTexParams.fFilter));
1973 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1974 GR_GL_TEXTURE_MIN_FILTER,
1975 newTexParams.fFilter));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001976 }
1977 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
1978 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1980 GR_GL_TEXTURE_WRAP_S,
1981 newTexParams.fWrapS));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001982 }
1983 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
1984 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001985 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1986 GR_GL_TEXTURE_WRAP_T,
1987 newTexParams.fWrapT));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001988 }
1989 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00001990 }
1991 }
1992
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001993 GrIRect* rect = NULL;
1994 GrIRect clipBounds;
1995 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
1996 fClip.hasConservativeBounds()) {
1997 fClip.getConservativeBounds().roundOut(&clipBounds);
1998 rect = &clipBounds;
1999 }
2000 this->flushRenderTarget(rect);
2001 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002002
reed@google.comac10a2d2010-12-22 21:39:39 +00002003 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2004 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2005 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002006 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002007 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002008 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002009 }
2010 }
2011
bsalomon@google.comd302f142011-03-03 13:54:13 +00002012 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2013 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002014 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002015 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002016 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002017 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002018 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002019 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002020 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002021 }
2022
bsalomon@google.comd302f142011-03-03 13:54:13 +00002023 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2024 switch (fCurrDrawState.fDrawFace) {
2025 case kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002026 GL_CALL(Enable(GR_GL_CULL_FACE));
2027 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002028 break;
2029 case kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002030 GL_CALL(Enable(GR_GL_CULL_FACE));
2031 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002032 break;
2033 case kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002034 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002035 break;
2036 default:
2037 GrCrash("Unknown draw face.");
2038 }
2039 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2040 }
2041
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002042#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002043 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002044 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002045 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002046 NULL == fCurrDrawState.fRenderTarget ||
2047 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002048 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002049 fCurrDrawState.fRenderTarget);
2050 }
2051#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002052
reed@google.comac10a2d2010-12-22 21:39:39 +00002053 flushStencil();
2054
bsalomon@google.comd302f142011-03-03 13:54:13 +00002055 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002056 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002057 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002058}
2059
2060void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002061 if (fHWGeometryState.fVertexBuffer != buffer) {
2062 fHWGeometryState.fArrayPtrsDirty = true;
2063 fHWGeometryState.fVertexBuffer = buffer;
2064 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002065}
2066
2067void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002068 if (fHWGeometryState.fVertexBuffer == buffer) {
2069 // deleting bound buffer does implied bind to 0
2070 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002071 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002072 }
2073}
2074
2075void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002076 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002077}
2078
2079void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002080 if (fHWGeometryState.fIndexBuffer == buffer) {
2081 // deleting bound buffer does implied bind to 0
2082 fHWGeometryState.fIndexBuffer = NULL;
2083 }
2084}
2085
reed@google.comac10a2d2010-12-22 21:39:39 +00002086void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2087 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002088 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002089 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002090 }
2091 if (fHWDrawState.fRenderTarget == renderTarget) {
2092 fHWDrawState.fRenderTarget = NULL;
2093 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002094}
2095
2096void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002097 for (int s = 0; s < kNumStages; ++s) {
2098 if (fCurrDrawState.fTextures[s] == texture) {
2099 fCurrDrawState.fTextures[s] = NULL;
2100 }
2101 if (fHWDrawState.fTextures[s] == texture) {
2102 // deleting bound texture does implied bind to 0
2103 fHWDrawState.fTextures[s] = NULL;
2104 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002105 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002106}
2107
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002108bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002109 GrGLenum* internalFormat,
2110 GrGLenum* format,
2111 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002112 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002113 case kRGBA_8888_GrPixelConfig:
2114 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002115 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002116 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002117 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2118 // format for a BGRA is BGRA not RGBA (as on desktop)
2119 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2120 } else {
2121 *internalFormat = GR_GL_RGBA;
2122 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002123 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002124 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002125 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002126 *format = GR_GL_RGB;
2127 *internalFormat = GR_GL_RGB;
2128 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002129 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002130 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002131 *format = GR_GL_RGBA;
2132 *internalFormat = GR_GL_RGBA;
2133 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002134 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002135 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002136 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002137 *format = GR_GL_PALETTE8_RGBA8;
2138 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002139 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002140 } else {
2141 return false;
2142 }
2143 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002144 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002145 *format = GR_GL_ALPHA;
2146 *internalFormat = GR_GL_ALPHA;
2147 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002148 break;
2149 default:
2150 return false;
2151 }
2152 return true;
2153}
2154
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002155void GrGpuGL::setTextureUnit(int unit) {
2156 GrAssert(unit >= 0 && unit < kNumStages);
2157 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002158 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002159 fActiveTextureUnitIdx = unit;
2160 }
2161}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002162
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002163void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002164 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002166 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2167 }
2168}
2169
reed@google.comac10a2d2010-12-22 21:39:39 +00002170/* On ES the internalFormat and format must match for TexImage and we use
2171 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2172 decide the internalFormat. However, on ES internalFormat for
2173 RenderBufferStorage* has to be a specific format (not a base format like
2174 GL_RGBA).
2175 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002176bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002177 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002178 case kRGBA_8888_GrPixelConfig:
2179 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002180 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002181 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002182 return true;
2183 } else {
2184 return false;
2185 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002186 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002187 // ES2 supports 565. ES1 supports it
2188 // with FBO extension desktop GL has
2189 // no such internal format
2190 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002191 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002193 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002194 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002195 return true;
2196 default:
2197 return false;
2198 }
2199}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002200
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002201void GrGpuGL::resetDirtyFlags() {
2202 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2203}
2204
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002205void GrGpuGL::setBuffers(bool indexed,
2206 int* extraVertexOffset,
2207 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002208
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002209 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002210
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002211 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2212
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002213 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002214 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002215 case kBuffer_GeometrySrcType:
2216 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002217 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002218 break;
2219 case kArray_GeometrySrcType:
2220 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002221 this->finalizeReservedVertices();
2222 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2223 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002224 break;
2225 default:
2226 vbuf = NULL; // suppress warning
2227 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002228 }
2229
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002230 GrAssert(NULL != vbuf);
2231 GrAssert(!vbuf->isLocked());
2232 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002233 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002234 fHWGeometryState.fArrayPtrsDirty = true;
2235 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002236 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002237
2238 if (indexed) {
2239 GrAssert(NULL != extraIndexOffset);
2240
2241 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002242 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002243 case kBuffer_GeometrySrcType:
2244 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002245 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002246 break;
2247 case kArray_GeometrySrcType:
2248 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002249 this->finalizeReservedIndices();
2250 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2251 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002252 break;
2253 default:
2254 ibuf = NULL; // suppress warning
2255 GrCrash("Unknown geometry src type!");
2256 }
2257
2258 GrAssert(NULL != ibuf);
2259 GrAssert(!ibuf->isLocked());
2260 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002261 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002262 fHWGeometryState.fIndexBuffer = ibuf;
2263 }
2264 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002265}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002266
2267int GrGpuGL::getMaxEdges() const {
2268 // FIXME: This is a pessimistic estimate based on how many other things
2269 // want to add uniforms. This should be centralized somewhere.
2270 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2271}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002272