blob: 92d836da1e7571feeaaae21fe6c2f08e44ce9301 [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
300 fGLVersion = gl_version_as_float(gl);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000301 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000302
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000303 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000304
twiz@google.com0f31ca72011-03-18 17:38:11 +0000305 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000306 // check FS and fixed-function texture unit limits
307 // we only use textures in the fragment stage currently.
308 // checks are > to make sure we have a spare unit.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000309 if (kES1_GrGLBinding != this->glBinding()) {
310 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000311 GrAssert(maxTextureUnits > kNumStages);
312 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000313 if (kES2_GrGLBinding != this->glBinding()) {
314 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000315 GrAssert(maxTextureUnits > kNumStages);
316 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000317 if (kES2_GrGLBinding == this->glBinding()) {
318 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000319 &fMaxFragmentUniformVectors);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000320 } else if (kDesktop_GrGLBinding != this->glBinding()) {
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000321 GrGLint max;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000322 GR_GL_GetIntegerv(gl, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000323 fMaxFragmentUniformVectors = max / 4;
324 } else {
325 fMaxFragmentUniformVectors = 16;
326 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000327
reed@google.comac10a2d2010-12-22 21:39:39 +0000328 ////////////////////////////////////////////////////////////////////////////
329 // Check for supported features.
330
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000331 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000332
twiz@google.com0f31ca72011-03-18 17:38:11 +0000333 GrGLint numFormats;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000334 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000335 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000336 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000337 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000338 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000339 f8bitPaletteSupport = true;
340 break;
341 }
342 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000343
344 if (gPrintStartupSpew) {
345 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
346 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000347
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000348 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
349 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
350 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
351 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
353 memset(fAASamples, 0, sizeof(fAASamples));
354 fMSFBOType = kNone_MSFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000355 if (kDesktop_GrGLBinding != this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000356 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000357 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000358 // and fbo_blit extensions.
359 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000360 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000361 fMSFBOType = kAppleES_MSFBO;
362 }
363 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000364 if ((fGLVersion >= 3.f) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000365 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000366 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
367 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000368 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000369 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000371 if (gPrintStartupSpew) {
372 switch (fMSFBOType) {
373 case kNone_MSFBO:
374 GrPrintf("MSAA Support: NONE\n");
375 break;
376 case kDesktopARB_MSFBO:
377 GrPrintf("MSAA Support: DESKTOP ARB.\n");
378 break;
379 case kDesktopEXT_MSFBO:
380 GrPrintf("MSAA Support: DESKTOP EXT.\n");
381 break;
382 case kAppleES_MSFBO:
383 GrPrintf("MSAA Support: APPLE ES.\n");
384 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000385 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000386 }
387
388 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000389 GrGLint maxSamples;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000390 GR_GL_GetIntegerv(gl, GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000391 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000392 fAASamples[kNone_GrAALevel] = 0;
393 fAASamples[kLow_GrAALevel] = GrMax(2,
394 GrFixedFloorToInt((GR_FixedHalf) *
395 maxSamples));
396 fAASamples[kMed_GrAALevel] = GrMax(2,
397 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
398 maxSamples));
399 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000400 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000401 if (gPrintStartupSpew) {
402 GrPrintf("\tMax Samples: %d\n", maxSamples);
403 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000404 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000405 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000406
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000407 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000408 fHasStencilWrap = (fGLVersion >= 1.4f) ||
409 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000410 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000411 fHasStencilWrap = (fGLVersion >= 2.0f) || this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000412 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000413 if (gPrintStartupSpew) {
414 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
415 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000416
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000417 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000418 // we could also look for GL_ATI_separate_stencil extension or
419 // GL_EXT_stencil_two_side but they use different function signatures
420 // than GL2.0+ (and than each other).
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000421 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000422 // supported on GL 1.4 and higher or by extension
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000423 fStencilWrapOpsSupport = (fGLVersion >= 1.4f) ||
424 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000425 } else {
426 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
427 // an ES1 extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000428 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000429 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000430 fStencilWrapOpsSupport = (fGLVersion >= 2.f) ||
431 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000432 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000433 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000434 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
435 (fTwoSidedStencilSupport ? "YES" : "NO"),
436 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000437 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000438
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000439 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000440 fRGBA8Renderbuffer = true;
441 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000442 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000443 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000444 if (gPrintStartupSpew) {
445 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
446 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000447
448
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000449 if (kDesktop_GrGLBinding != this->glBinding()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000450 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000451 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000452 }
453 }
454
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000455 if (kDesktop_GrGLBinding == this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000456 fBufferLockSupport = true; // we require VBO support and the desktop VBO
457 // extension includes glMapBuffer.
458 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000459 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000460 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000461
reed@google.comeeeb5a02010-12-23 15:12:59 +0000462 if (gPrintStartupSpew) {
463 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
464 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000466 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000467 if (fGLVersion >= 2.f ||
468 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000469 fNPOTTextureTileSupport = true;
470 fNPOTTextureSupport = true;
471 } else {
472 fNPOTTextureTileSupport = false;
473 fNPOTTextureSupport = false;
474 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000476 if (fGLVersion >= 2.f) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000477 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000478 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000479 } else {
480 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000481 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000482 fNPOTTextureTileSupport = false;
483 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000484 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000485
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000486 fAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com205d4602011-04-25 12:43:45 +0000487
reed@google.comac10a2d2010-12-22 21:39:39 +0000488 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000489 // Experiments to determine limitations that can't be queried.
490 // TODO: Make these a preprocess that generate some compile time constants.
491 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000492
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000493 int expectNPOTTargets = gl->fNPOTRenderTargetSupport;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000494 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000495 fNPOTRenderTargetSupport =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000496 probe_for_npot_render_target_support(gl, fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000497 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000498 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
499 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000500 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000501
bsalomon@google.com0748f212011-02-01 22:56:16 +0000502 if (gPrintStartupSpew) {
503 if (fNPOTTextureSupport) {
504 GrPrintf("NPOT textures supported\n");
505 if (fNPOTTextureTileSupport) {
506 GrPrintf("NPOT texture tiling supported\n");
507 } else {
508 GrPrintf("NPOT texture tiling NOT supported\n");
509 }
510 if (fNPOTRenderTargetSupport) {
511 GrPrintf("NPOT render targets supported\n");
512 } else {
513 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000514 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000516 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000517 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000518 }
519
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000520 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
521 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000522 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000523 // attachment, hence this min:
524 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000525
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000526 fMinRenderTargetHeight = gl->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000527 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000528 fMinRenderTargetHeight =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000529 probe_for_min_render_target_height(gl,fNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000530 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000531 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000532
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000533 fMinRenderTargetWidth = gl->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000534 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000535 fMinRenderTargetWidth =
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000536 probe_for_min_render_target_width(gl, fNPOTRenderTargetSupport,
tomhudson@google.com747bf292011-06-14 18:16:52 +0000537 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000538 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000539
bsalomon@google.comfe676522011-06-17 18:12:21 +0000540 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000541}
542
543GrGpuGL::~GrGpuGL() {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000544 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000545}
546
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000547void GrGpuGL::resetContext() {
548 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000549 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000550 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000551
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000552 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000553 GL_CALL(Disable(GR_GL_DEPTH_TEST));
554 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000555
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000556 GL_CALL(Disable(GR_GL_CULL_FACE));
557 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000558 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000559
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000560 GL_CALL(Disable(GR_GL_DITHER));
561 if (kDesktop_GrGLBinding == this->glBinding()) {
562 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
563 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
564 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000565 fHWAAState.fMSAAEnabled = false;
566 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000567 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000568
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000569 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000570 fHWDrawState.fFlagBits = 0;
571
reed@google.comac10a2d2010-12-22 21:39:39 +0000572 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000573 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000574
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000575 // invalid
576 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000577
reed@google.comac10a2d2010-12-22 21:39:39 +0000578 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000579 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
580 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000581
582 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000583 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000584
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000586
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000587 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000588
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000589 for (int s = 0; s < kNumStages; ++s) {
590 fHWDrawState.fTextures[s] = NULL;
591 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
592 -GR_ScalarMax,
593 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000594 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000595 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000596 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000597
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000598 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000600 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000601 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000602
bsalomon@google.comd302f142011-03-03 13:54:13 +0000603 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000604 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000605 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000606
607 fHWGeometryState.fIndexBuffer = NULL;
608 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000609
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000610 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000611
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000612 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 fHWDrawState.fRenderTarget = NULL;
614}
615
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000616GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
617
618 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
619 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
620 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
621 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
622
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000623 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000624 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000625
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000626 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000627 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000628 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000629 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000630 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000631 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000632 } else {
633 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000634 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000635 }
636 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000637 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000638 }
639 // we don't know what the RB ids are without glGets and we don't care
640 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000641 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000642 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000643 if (desc.fStencilBits) {
644 GrGLStencilBuffer::Format format;
645 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
646 format.fPacked = false;
647 format.fStencilBits = desc.fStencilBits;
648 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000649 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
650 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000651 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000652 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000653 }
654
655 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000656 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000657 GrGLenum dontCare;
658 if (!canBeTexture(desc.fConfig, &dontCare,
659 &texDesc.fUploadFormat,
660 &texDesc.fUploadType)) {
661 return NULL;
662 }
663
664 GrGLTexture::TexParams params;
665
666 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
667 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
668
bsalomon@google.com90405932011-06-17 15:56:55 +0000669 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000670 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000671 texDesc.fTextureID = desc.fPlatformTexture;
672 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
673 texDesc.fOwnsID = false;
674
675 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000676 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000677 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000678 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000679 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000680 } else {
681 return new GrGLTexture(this, texDesc, params);
682 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000683 } else {
684 GrGLIRect viewport;
685 viewport.fLeft = 0;
686 viewport.fBottom = 0;
687 viewport.fWidth = desc.fWidth;
688 viewport.fHeight = desc.fHeight;
689
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000690 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000691 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000692 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000693 }
694}
695
bsalomon@google.com5782d712011-01-21 21:03:59 +0000696///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000697
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000698static const GrGLuint kUnknownBitCount = ~0;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000699
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000700void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000701
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000702 // Build up list of legal stencil formats (though perhaps not supported on
703 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000704
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000705 // these consts are in order of most preferred to least preferred
706 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000707 static const GrGLStencilBuffer::Format
708 // internal Format stencil bits total bits packed?
709 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
710 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
711 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
712 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
713 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
714 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000715
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000716 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000717 bool supportsPackedDS = fGLVersion >= 3.0f ||
718 this->hasExtension("GL_EXT_packed_depth_stencil") ||
719 this->hasExtension("GL_ARB_framebuffer_object");
720
721 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
722 // require FBO support we can expect these are legal formats and don't
723 // check. These also all support the unsized GL_STENCIL_INDEX.
724 fStencilFormats.push_back() = gS8;
725 fStencilFormats.push_back() = gS16;
726 if (supportsPackedDS) {
727 fStencilFormats.push_back() = gD24S8;
728 }
729 fStencilFormats.push_back() = gS4;
730 if (supportsPackedDS) {
731 fStencilFormats.push_back() = gDS;
732 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000733 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000734 // ES2 has STENCIL_INDEX8 without extensions.
735 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
736 // introduces tokens for S1 thu S8 but there are separate extensions
737 // that make them legal (GL_OES_stencil1, ...).
738 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
739 // ES doesn't support using the unsized formats.
740
741 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
742 fStencilFormats.push_back() = gS8;
743 }
744 //fStencilFormats.push_back() = gS16;
745 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
746 fStencilFormats.push_back() = gD24S8;
747 }
748 if (this->hasExtension("GL_OES_stencil4")) {
749 fStencilFormats.push_back() = gS4;
750 }
751 // we require some stencil format.
752 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000753 }
754}
755
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000756////////////////////////////////////////////////////////////////////////////////
757
758void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
759 GrGLenum internalFormat,
760 const void* data,
761 size_t rowBytes) {
762 // we assume the texture is bound;
763 if (!rowBytes) {
764 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
765 }
766
767 // in case we need a temporary, trimmed copy of the src pixels
768 SkAutoSMalloc<128 * 128> tempStorage;
769
770 /*
771 * check whether to allocate a temporary buffer for flipping y or
772 * because our data has extra bytes past each row. If so, we need
773 * to trim those off here, since GL ES doesn't let us specify
774 * GL_UNPACK_ROW_LENGTH.
775 */
776 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000777 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000778 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000779 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
780 rowBytes / desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000781 }
782 } else {
783 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
784 if (data && (trimRowBytes < rowBytes || flipY)) {
785 // copy the data into our new storage, skipping the trailing bytes
786 size_t trimSize = desc.fContentHeight * trimRowBytes;
787 const char* src = (const char*)data;
788 if (flipY) {
789 src += (desc.fContentHeight - 1) * rowBytes;
790 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000791 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000792 for (int y = 0; y < desc.fContentHeight; y++) {
793 memcpy(dst, src, trimRowBytes);
794 if (flipY) {
795 src -= rowBytes;
796 } else {
797 src += rowBytes;
798 }
799 dst += trimRowBytes;
800 }
801 // now point data to our trimmed version
802 data = tempStorage.get();
803 rowBytes = trimRowBytes;
804 }
805 }
806
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000807 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000808 if (kIndex_8_GrPixelConfig == desc.fFormat &&
809 supports8BitPalette()) {
810 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
811 GrAssert(desc.fContentWidth == desc.fAllocWidth);
812 GrAssert(desc.fContentHeight == desc.fAllocHeight);
813 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
814 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000815 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
816 desc.fAllocWidth, desc.fAllocHeight,
817 0, imageSize, data));
818 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000819 } else {
820 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
821 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000822 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
823 desc.fAllocWidth, desc.fAllocHeight,
824 0, desc.fUploadFormat, desc.fUploadType, NULL));
825 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
826 desc.fContentHeight, desc.fUploadFormat,
827 desc.fUploadType, data));
828 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000829
830 int extraW = desc.fAllocWidth - desc.fContentWidth;
831 int extraH = desc.fAllocHeight - desc.fContentHeight;
832 int maxTexels = extraW * extraH;
833 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
834 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
835
836 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
837
838 // rowBytes is actual stride between rows in data
839 // rowDataBytes is the actual amount of non-pad data in a row
840 // and the stride used for uploading extraH rows.
841 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
842 if (extraH) {
843 uint8_t* lastRowStart = (uint8_t*) data +
844 (desc.fContentHeight - 1) * rowBytes;
845 uint8_t* extraRowStart = (uint8_t*)texels.get();
846
847 for (int i = 0; i < extraH; ++i) {
848 memcpy(extraRowStart, lastRowStart, rowDataBytes);
849 extraRowStart += rowDataBytes;
850 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000851 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
852 desc.fContentHeight, desc.fContentWidth,
853 extraH, desc.fUploadFormat,
854 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000855 }
856 if (extraW) {
857 uint8_t* edgeTexel = (uint8_t*)data +
858 rowDataBytes - desc.fUploadByteCount;
859 uint8_t* extraTexel = (uint8_t*)texels.get();
860 for (int j = 0; j < desc.fContentHeight; ++j) {
861 for (int i = 0; i < extraW; ++i) {
862 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
863 extraTexel += desc.fUploadByteCount;
864 }
865 edgeTexel += rowBytes;
866 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000867 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
868 0, extraW, desc.fContentHeight,
869 desc.fUploadFormat, desc.fUploadType,
870 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000871 }
872 if (extraW && extraH) {
873 uint8_t* cornerTexel = (uint8_t*)data +
874 desc.fContentHeight * rowBytes -
875 desc.fUploadByteCount;
876 uint8_t* extraTexel = (uint8_t*)texels.get();
877 for (int i = 0; i < extraW*extraH; ++i) {
878 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
879 extraTexel += desc.fUploadByteCount;
880 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000881 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
882 desc.fContentHeight, extraW, extraH,
883 desc.fUploadFormat, desc.fUploadType,
884 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000885 }
886
887 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000888 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
889 desc.fAllocWidth, desc.fAllocHeight, 0,
890 desc.fUploadFormat, desc.fUploadType, data));
891 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000892 }
893 }
894}
895
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000896bool GrGpuGL::createRenderTargetObjects(int width, int height,
897 GrGLuint texID,
898 GrGLRenderTarget::Desc* desc) {
899 desc->fMSColorRenderbufferID = 0;
900 desc->fRTFBOID = 0;
901 desc->fTexFBOID = 0;
902 desc->fOwnIDs = true;
903
904 GrGLenum status;
905 GrGLint err;
906
bsalomon@google.comab15d612011-08-09 12:57:56 +0000907 GrGLenum msColorFormat = 0; // suppress warning
908
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910 if (!desc->fTexFBOID) {
911 goto FAILED;
912 }
913
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000914
915 // If we are using multisampling we will create two FBOS. We render
916 // to one and then resolve to the texture bound to the other.
917 if (desc->fSampleCnt > 1 && kNone_MSFBO != fMSFBOType) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000918 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
919 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000920 if (!desc->fRTFBOID ||
921 !desc->fMSColorRenderbufferID ||
922 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
923 goto FAILED;
924 }
925 } else {
926 desc->fRTFBOID = desc->fTexFBOID;
927 }
928
929 if (desc->fRTFBOID != desc->fTexFBOID) {
930 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000931 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000933 GR_GL_CALL_NOERRCHECK(this->glInterface(),
934 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
935 desc->fSampleCnt,
936 msColorFormat,
937 width, height));
938 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000939 if (err != GR_GL_NO_ERROR) {
940 goto FAILED;
941 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
943 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000944 GR_GL_COLOR_ATTACHMENT0,
945 GR_GL_RENDERBUFFER,
946 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000947 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
949 goto FAILED;
950 }
951 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000952 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000953
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000954 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
955 GR_GL_COLOR_ATTACHMENT0,
956 GR_GL_TEXTURE_2D,
957 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000958 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000959 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
960 goto FAILED;
961 }
962
963 return true;
964
965FAILED:
966 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000967 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000968 }
969 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000970 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 }
972 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000973 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000974 }
975 return false;
976}
977
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000978// good to set a break-point here to know when createTexture fails
979static GrTexture* return_null_texture() {
980// GrAssert(!"null texture");
981 return NULL;
982}
983
984#if GR_DEBUG
985static size_t as_size_t(int x) {
986 return x;
987}
988#endif
989
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000990GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000991 const void* srcData,
992 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000993
994#if GR_COLLECT_STATS
995 ++fStats.fTextureCreateCnt;
996#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000997
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000998 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000999 GR_GL_NEAREST,
1000 GR_GL_CLAMP_TO_EDGE,
1001 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001002 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001003
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001004 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001005 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001006 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001007
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001008 glTexDesc.fContentWidth = desc.fWidth;
1009 glTexDesc.fContentHeight = desc.fHeight;
1010 glTexDesc.fAllocWidth = desc.fWidth;
1011 glTexDesc.fAllocHeight = desc.fHeight;
1012 glTexDesc.fFormat = desc.fFormat;
1013 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001014
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001015 glRTDesc.fMSColorRenderbufferID = 0;
1016 glRTDesc.fRTFBOID = 0;
1017 glRTDesc.fTexFBOID = 0;
1018 glRTDesc.fOwnIDs = true;
1019 glRTDesc.fConfig = glTexDesc.fFormat;
1020
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001021 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001022 if (!canBeTexture(desc.fFormat,
1023 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001024 &glTexDesc.fUploadFormat,
1025 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 return return_null_texture();
1027 }
1028
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001029 // We keep GrRenderTargets in GL's normal orientation so that they
1030 // can be drawn to by the outside world without the client having
1031 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001032 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001033 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001034
reed@google.comac10a2d2010-12-22 21:39:39 +00001035 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001036 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001037 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001038 GrPrintf("AA RT requested but not supported on this platform.");
1039 }
1040
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001041 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001042
reed@google.comac10a2d2010-12-22 21:39:39 +00001043 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001044 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001045 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1046 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001047 }
1048
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001049 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001050 glTexDesc.fAllocWidth);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001051 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001052 glTexDesc.fAllocHeight);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001053 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1054 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001055 return return_null_texture();
1056 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001057 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001058 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1059 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1060 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1061 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001062 return return_null_texture();
1063 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 }
1065
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001066 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001067 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001068 return return_null_texture();
1069 }
1070
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001071 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001072 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1073 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1074 GR_GL_TEXTURE_MAG_FILTER,
1075 DEFAULT_PARAMS.fFilter));
1076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1077 GR_GL_TEXTURE_MIN_FILTER,
1078 DEFAULT_PARAMS.fFilter));
1079 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1080 GR_GL_TEXTURE_WRAP_S,
1081 DEFAULT_PARAMS.fWrapS));
1082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1083 GR_GL_TEXTURE_WRAP_T,
1084 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001085
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001086 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001087
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001090 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001091#if GR_COLLECT_STATS
1092 ++fStats.fRenderTargetCreateCnt;
1093#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001094 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1095 glTexDesc.fAllocHeight,
1096 glTexDesc.fTextureID,
1097 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001098 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001099 return return_null_texture();
1100 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1102 } else {
1103 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001104 }
1105#ifdef TRACE_TEXTURE_CREATION
1106 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1107 tex->fTextureID, width, height, tex->fUploadByteCount);
1108#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 return tex;
1110}
1111
1112namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1114 GrGLuint rb,
1115 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001116 // we shouldn't ever know one size and not the other
1117 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1118 (kUnknownBitCount == format->fTotalBits));
1119 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1122 (GrGLint*)&format->fStencilBits);
1123 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001124 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1126 (GrGLint*)&format->fTotalBits);
1127 format->fTotalBits += format->fStencilBits;
1128 } else {
1129 format->fTotalBits = format->fStencilBits;
1130 }
1131 }
1132}
1133}
1134
1135bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1136 int width, int height) {
1137
1138 // All internally created RTs are also textures. We don't create
1139 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1140 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001141 GrAssert(width >= rt->allocatedWidth());
1142 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143
1144 int samples = rt->numSamples();
1145 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 if (!sbID) {
1148 return false;
1149 }
1150
1151 GrGLStencilBuffer* sb = NULL;
1152
1153 int stencilFmtCnt = fStencilFormats.count();
1154 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001155 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 // we start with the last stencil format that succeeded in hopes
1157 // that we won't go through this loop more than once after the
1158 // first (painful) stencil creation.
1159 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001160 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 // version on a GL that doesn't have an MSAA extension.
1162 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001163 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1164 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 GR_GL_RENDERBUFFER,
1166 samples,
1167 fStencilFormats[sIdx].fInternalFormat,
1168 width,
1169 height));
1170 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001171 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1172 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 fStencilFormats[sIdx].fInternalFormat,
1174 width, height));
1175 }
1176
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001177 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 if (err == GR_GL_NO_ERROR) {
1179 // After sized formats we attempt an unsized format and take whatever
1180 // sizes GL gives us. In that case we query for the size.
1181 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001182 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001183 sb = new GrGLStencilBuffer(this, sbID, width, height,
1184 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1186 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001187 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 return true;
1190 }
1191 sb->abandon(); // otherwise we lose sbID
1192 sb->unref();
1193 }
1194 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001195 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001196 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197}
1198
1199bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1200 GrRenderTarget* rt) {
1201 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1202
1203 GrGLuint fbo = glrt->renderFBOID();
1204
1205 if (NULL == sb) {
1206 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001207 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 GR_GL_STENCIL_ATTACHMENT,
1209 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001210 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 GR_GL_DEPTH_ATTACHMENT,
1212 GR_GL_RENDERBUFFER, 0));
1213#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001214 GrGLenum status;
1215 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1217#endif
1218 }
1219 return true;
1220 } else {
1221 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1222 GrGLuint rb = glsb->renderbufferID();
1223
reed@google.comac10a2d2010-12-22 21:39:39 +00001224 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1226 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 GR_GL_STENCIL_ATTACHMENT,
1228 GR_GL_RENDERBUFFER, rb));
1229 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001230 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231 GR_GL_DEPTH_ATTACHMENT,
1232 GR_GL_RENDERBUFFER, rb));
1233 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 GR_GL_DEPTH_ATTACHMENT,
1236 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001237 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001239 GrGLenum status;
1240 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001242 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 GR_GL_STENCIL_ATTACHMENT,
1244 GR_GL_RENDERBUFFER, 0));
1245 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001246 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247 GR_GL_DEPTH_ATTACHMENT,
1248 GR_GL_RENDERBUFFER, 0));
1249 }
1250 return false;
1251 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 return true;
1253 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001255}
1256
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001257////////////////////////////////////////////////////////////////////////////////
1258
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001259GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001260 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001264 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001265 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001267 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1268 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1269 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1270 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1271 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 // deleting bound buffer does implicit bind to 0
1273 fHWGeometryState.fVertexBuffer = NULL;
1274 return NULL;
1275 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001276 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 size, dynamic);
1278 fHWGeometryState.fVertexBuffer = vertexBuffer;
1279 return vertexBuffer;
1280 }
1281 return NULL;
1282}
1283
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001284GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001285 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001286 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1289 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001291 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1292 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1293 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1294 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1295 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 // deleting bound buffer does implicit bind to 0
1297 fHWGeometryState.fIndexBuffer = NULL;
1298 return NULL;
1299 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001300 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 size, dynamic);
1302 fHWGeometryState.fIndexBuffer = indexBuffer;
1303 return indexBuffer;
1304 }
1305 return NULL;
1306}
1307
reed@google.comac10a2d2010-12-22 21:39:39 +00001308void GrGpuGL::flushScissor(const GrIRect* rect) {
1309 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001310 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001311 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001312
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001313 GrGLIRect scissor;
1314 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001315 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001316 rect->width(), rect->height());
1317 if (scissor.contains(vp)) {
1318 rect = NULL;
1319 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 }
1321
1322 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001323 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001324 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 fHWBounds.fScissorRect = scissor;
1326 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001328 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001329 fHWBounds.fScissorEnabled = true;
1330 }
1331 } else {
1332 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001333 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 fHWBounds.fScissorEnabled = false;
1335 }
1336 }
1337}
1338
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001339void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001340 if (NULL == fCurrDrawState.fRenderTarget) {
1341 return;
1342 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001343 GrIRect r;
1344 if (NULL != rect) {
1345 // flushScissor expects rect to be clipped to the target.
1346 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001347 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1348 fCurrDrawState.fRenderTarget->height());
1349 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001350 rect = &r;
1351 } else {
1352 return;
1353 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001355 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001356 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001357 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001358 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001359 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1360 GrColorUnpackG(color)/255.f,
1361 GrColorUnpackB(color)/255.f,
1362 GrColorUnpackA(color)/255.f));
1363 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001364}
1365
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001366void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001367 if (NULL == fCurrDrawState.fRenderTarget) {
1368 return;
1369 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001370
1371 this->flushRenderTarget(&GrIRect::EmptyIRect());
1372
reed@google.comac10a2d2010-12-22 21:39:39 +00001373 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001374 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001375 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001377 GL_CALL(StencilMask(0xffffffff));
1378 GL_CALL(ClearStencil(0));
1379 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001380 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001381}
1382
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001383void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001384 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001385
1386 // this should only be called internally when we know we have a
1387 // stencil buffer.
1388 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001389 GrGLint stencilBitCount =
1390 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001391#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001392 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001393 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001394#else
1395 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001396 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001397 // turned into draws. Our contract on GrDrawTarget says that
1398 // changing the clip between stencil passes may or may not
1399 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001400 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001401#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001402 GrGLint value;
1403 if (insideClip) {
1404 value = (1 << (stencilBitCount - 1));
1405 } else {
1406 value = 0;
1407 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001408 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001409 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001410 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001411 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001412 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001413 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001414}
1415
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001416void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001417 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001418}
1419
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001420bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1421 int left, int top, int width, int height,
1422 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001423 GrGLenum internalFormat; // we don't use this for glReadPixels
1424 GrGLenum format;
1425 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001426 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1427 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001428 }
1429 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1430 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1431 switch (tgt->getResolveType()) {
1432 case GrGLRenderTarget::kCantResolve_ResolveType:
1433 return false;
1434 case GrGLRenderTarget::kAutoResolves_ResolveType:
1435 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1436 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001437 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001438 break;
1439 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001440 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001441 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001442 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1443 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001444 break;
1445 default:
1446 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001447 }
1448
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001449 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001450
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001451 // the read rect is viewport-relative
1452 GrGLIRect readRect;
1453 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001454 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1455 readRect.fWidth, readRect.fHeight,
1456 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001457
1458 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1459 // API presents top-to-bottom
1460 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001461 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001462 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 void* tmp = rowStorage.get();
1464
1465 const int halfY = height >> 1;
1466 char* top = reinterpret_cast<char*>(buffer);
1467 char* bottom = top + (height - 1) * stride;
1468 for (int y = 0; y < halfY; y++) {
1469 memcpy(tmp, top, stride);
1470 memcpy(top, bottom, stride);
1471 memcpy(bottom, tmp, stride);
1472 top += stride;
1473 bottom -= stride;
1474 }
1475 }
1476 return true;
1477}
1478
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001479void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001480
1481 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1482
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001483 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001484 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001485 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001486 #if GR_COLLECT_STATS
1487 ++fStats.fRenderTargetChngCnt;
1488 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001489 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001490 GrGLenum status;
1491 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001492 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001493 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001494 }
1495 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001496 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001497 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001498 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001499 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001500 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001501 fHWBounds.fViewportRect = vp;
1502 }
1503 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001504 if (NULL == bound || !bound->isEmpty()) {
1505 rt->flagAsNeedingResolve(bound);
1506 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001507}
1508
twiz@google.com0f31ca72011-03-18 17:38:11 +00001509GrGLenum gPrimitiveType2GLMode[] = {
1510 GR_GL_TRIANGLES,
1511 GR_GL_TRIANGLE_STRIP,
1512 GR_GL_TRIANGLE_FAN,
1513 GR_GL_POINTS,
1514 GR_GL_LINES,
1515 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001516};
1517
bsalomon@google.comd302f142011-03-03 13:54:13 +00001518#define SWAP_PER_DRAW 0
1519
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001520#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001521 #if GR_MAC_BUILD
1522 #include <AGL/agl.h>
1523 #elif GR_WIN32_BUILD
1524 void SwapBuf() {
1525 DWORD procID = GetCurrentProcessId();
1526 HWND hwnd = GetTopWindow(GetDesktopWindow());
1527 while(hwnd) {
1528 DWORD wndProcID = 0;
1529 GetWindowThreadProcessId(hwnd, &wndProcID);
1530 if(wndProcID == procID) {
1531 SwapBuffers(GetDC(hwnd));
1532 }
1533 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1534 }
1535 }
1536 #endif
1537#endif
1538
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001539void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1540 uint32_t startVertex,
1541 uint32_t startIndex,
1542 uint32_t vertexCount,
1543 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001544 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1545
twiz@google.com0f31ca72011-03-18 17:38:11 +00001546 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001547
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001548 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1549 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1550
1551 // our setupGeometry better have adjusted this to zero since
1552 // DrawElements always draws from the begining of the arrays for idx 0.
1553 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001554
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001555 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1556 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001557#if SWAP_PER_DRAW
1558 glFlush();
1559 #if GR_MAC_BUILD
1560 aglSwapBuffers(aglGetCurrentContext());
1561 int set_a_break_pt_here = 9;
1562 aglSwapBuffers(aglGetCurrentContext());
1563 #elif GR_WIN32_BUILD
1564 SwapBuf();
1565 int set_a_break_pt_here = 9;
1566 SwapBuf();
1567 #endif
1568#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001569}
1570
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001571void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1572 uint32_t startVertex,
1573 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001574 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1575
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001576 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1577
1578 // our setupGeometry better have adjusted this to zero.
1579 // DrawElements doesn't take an offset so we always adjus the startVertex.
1580 GrAssert(0 == startVertex);
1581
1582 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1583 // account for startVertex in the DrawElements case. So we always
1584 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001585 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001586#if SWAP_PER_DRAW
1587 glFlush();
1588 #if GR_MAC_BUILD
1589 aglSwapBuffers(aglGetCurrentContext());
1590 int set_a_break_pt_here = 9;
1591 aglSwapBuffers(aglGetCurrentContext());
1592 #elif GR_WIN32_BUILD
1593 SwapBuf();
1594 int set_a_break_pt_here = 9;
1595 SwapBuf();
1596 #endif
1597#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001598}
1599
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001600void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001601
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001602 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 GrAssert(kNone_MSFBO != fMSFBOType);
1604 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001605 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1606 rt->renderFBOID()));
1607 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1608 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 #if GR_COLLECT_STATS
1610 ++fStats.fRenderTargetChngCnt;
1611 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001612 // make sure we go through flushRenderTarget() since we've modified
1613 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001615 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001616 const GrIRect dirtyRect = rt->getResolveRect();
1617 GrGLIRect r;
1618 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1619 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001620
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001621 if (kAppleES_MSFBO == fMSFBOType) {
1622 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001623 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1624 GL_CALL(Scissor(r.fLeft, r.fBottom,
1625 r.fWidth, r.fHeight));
1626 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001627 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 fHWBounds.fScissorEnabled = true;
1629 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001630 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001631 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001632 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1633 flushScissor(NULL);
1634 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001635 int right = r.fLeft + r.fWidth;
1636 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001637 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1638 r.fLeft, r.fBottom, right, top,
1639 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001641 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 }
1643}
1644
twiz@google.com0f31ca72011-03-18 17:38:11 +00001645static const GrGLenum grToGLStencilFunc[] = {
1646 GR_GL_ALWAYS, // kAlways_StencilFunc
1647 GR_GL_NEVER, // kNever_StencilFunc
1648 GR_GL_GREATER, // kGreater_StencilFunc
1649 GR_GL_GEQUAL, // kGEqual_StencilFunc
1650 GR_GL_LESS, // kLess_StencilFunc
1651 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1652 GR_GL_EQUAL, // kEqual_StencilFunc,
1653 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001654};
1655GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1656GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1657GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1658GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1659GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1660GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1661GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1662GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1663GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1664
twiz@google.com0f31ca72011-03-18 17:38:11 +00001665static const GrGLenum grToGLStencilOp[] = {
1666 GR_GL_KEEP, // kKeep_StencilOp
1667 GR_GL_REPLACE, // kReplace_StencilOp
1668 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1669 GR_GL_INCR, // kIncClamp_StencilOp
1670 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1671 GR_GL_DECR, // kDecClamp_StencilOp
1672 GR_GL_ZERO, // kZero_StencilOp
1673 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001674};
1675GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1676GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1677GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1678GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1679GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1680GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1681GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1682GR_STATIC_ASSERT(6 == kZero_StencilOp);
1683GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1684
reed@google.comac10a2d2010-12-22 21:39:39 +00001685void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001686 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001687
1688 // use stencil for clipping if clipping is enabled and the clip
1689 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001690 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001691 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001692 bool stencilChange = fHWStencilClip != stencilClip ||
1693 fHWDrawState.fStencilSettings != *settings ||
1694 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1695 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001696
1697 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001698
bsalomon@google.comd302f142011-03-03 13:54:13 +00001699 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1700 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001701
bsalomon@google.comd302f142011-03-03 13:54:13 +00001702 if (settings->isDisabled()) {
1703 if (stencilClip) {
1704 settings = &gClipStencilSettings;
1705 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001706 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001707
1708 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001709 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001710 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001711 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001712 #if GR_DEBUG
1713 if (!fStencilWrapOpsSupport) {
1714 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1715 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1716 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1717 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1718 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1719 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1720 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1721 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1722 }
1723 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001724 int stencilBits = 0;
1725 GrStencilBuffer* stencilBuffer =
1726 fCurrDrawState.fRenderTarget->getStencilBuffer();
1727 if (NULL != stencilBuffer) {
1728 stencilBits = stencilBuffer->bits();
1729 }
1730 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001731 GrAssert(stencilBits ||
1732 (GrStencilSettings::gDisabled ==
1733 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001734 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1735 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001736
1737 unsigned int frontRef = settings->fFrontFuncRef;
1738 unsigned int frontMask = settings->fFrontFuncMask;
1739 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001740 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001741
1742 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1743
1744 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1745 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1746 } else {
1747 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1748
1749 ConvertStencilFuncAndMask(settings->fFrontFunc,
1750 stencilClip,
1751 clipStencilMask,
1752 userStencilMask,
1753 &frontRef,
1754 &frontMask);
1755 frontWriteMask &= userStencilMask;
1756 }
1757 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001758 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001759 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001760 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001761 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001762 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001763 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001764 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001765 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001766 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001767
1768 unsigned int backRef = settings->fBackFuncRef;
1769 unsigned int backMask = settings->fBackFuncMask;
1770 unsigned int backWriteMask = settings->fBackWriteMask;
1771
1772
1773 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1774 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1775 backFunc = grToGLStencilFunc[settings->fBackFunc];
1776 } else {
1777 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1778 ConvertStencilFuncAndMask(settings->fBackFunc,
1779 stencilClip,
1780 clipStencilMask,
1781 userStencilMask,
1782 &backRef,
1783 &backMask);
1784 backWriteMask &= userStencilMask;
1785 }
1786
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001787 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1788 frontRef, frontMask));
1789 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1790 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1791 backRef, backMask));
1792 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1793 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1794 grToGLStencilOp[settings->fFrontFailOp],
1795 grToGLStencilOp[settings->fFrontPassOp],
1796 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001797
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001798 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1799 grToGLStencilOp[settings->fBackFailOp],
1800 grToGLStencilOp[settings->fBackPassOp],
1801 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001802 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001803 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1804 GL_CALL(StencilMask(frontWriteMask));
1805 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001806 grToGLStencilOp[settings->fFrontPassOp],
1807 grToGLStencilOp[settings->fFrontPassOp]));
1808 }
1809 }
1810 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001811 fHWStencilClip = stencilClip;
1812 }
1813}
1814
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001815void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001816 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001817 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1818 // smooth lines.
1819
1820 // we prefer smooth lines over multisampled lines
1821 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001822 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001823 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001824 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001825 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001826 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001827 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001828 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001829 fHWAAState.fSmoothLineEnabled = false;
1830 }
1831 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1832 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001833 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001834 fHWAAState.fMSAAEnabled = false;
1835 }
1836 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1837 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
1838 fHWAAState.fMSAAEnabled) {
1839 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001840 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001841 fHWAAState.fMSAAEnabled = false;
1842 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001843 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001844 fHWAAState.fMSAAEnabled = true;
1845 }
1846 }
1847 }
1848}
1849
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001850void GrGpuGL::flushBlend(GrPrimitiveType type,
1851 GrBlendCoeff srcCoeff,
1852 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001853 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001854 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001855 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001856 fHWBlendDisabled = false;
1857 }
1858 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1859 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001860 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1861 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001862 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1863 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1864 }
1865 } else {
1866 bool blendOff = canDisableBlend();
1867 if (fHWBlendDisabled != blendOff) {
1868 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001869 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001870 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001871 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001872 }
1873 fHWBlendDisabled = blendOff;
1874 }
1875 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001876 if (fHWDrawState.fSrcBlend != srcCoeff ||
1877 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001878 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1879 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001880 fHWDrawState.fSrcBlend = srcCoeff;
1881 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001882 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001883 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1884 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001885 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1886
1887 float c[] = {
1888 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1889 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1890 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1891 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1892 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001893 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001894 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1895 }
1896 }
1897 }
1898}
1899
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001900static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1901 switch (filter) {
1902 case GrSamplerState::kBilinear_Filter:
1903 case GrSamplerState::k4x4Downsample_Filter:
1904 return GR_GL_LINEAR;
1905 case GrSamplerState::kNearest_Filter:
1906 case GrSamplerState::kConvolution_Filter:
1907 return GR_GL_NEAREST;
1908 default:
1909 GrAssert(!"Unknown filter type");
1910 return GR_GL_LINEAR;
1911 }
1912}
1913
bsalomon@google.comffca4002011-02-22 20:34:01 +00001914bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001915
1916 // GrGpu::setupClipAndFlushState should have already checked this
1917 // and bailed if not true.
1918 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001919
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001920 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001921 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001922 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001923 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001924
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001925 // true for now, but maybe not with GrEffect.
1926 GrAssert(NULL != nextTexture);
1927 // if we created a rt/tex and rendered to it without using a
1928 // texture and now we're texuring from the rt it will still be
1929 // the last bound texture, but it needs resolving. So keep this
1930 // out of the "last != next" check.
1931 GrGLRenderTarget* texRT =
1932 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1933 if (NULL != texRT) {
1934 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001935 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001936
1937 if (fHWDrawState.fTextures[s] != nextTexture) {
1938 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001939 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001940 #if GR_COLLECT_STATS
1941 ++fStats.fTextureChngCnt;
1942 #endif
1943 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1944 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001945 // The texture matrix has to compensate for texture width/height
1946 // and NPOT-embedded-in-POT
1947 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001948 }
1949
1950 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
1951 const GrGLTexture::TexParams& oldTexParams =
1952 nextTexture->getTexParams();
1953 GrGLTexture::TexParams newTexParams;
1954
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001955 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001956
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001957 const GrGLenum* wraps =
1958 GrGLTexture::WrapMode2GLWrap(this->glBinding());
1959 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1960 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001961
1962 if (newTexParams.fFilter != oldTexParams.fFilter) {
1963 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001964 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1965 GR_GL_TEXTURE_MAG_FILTER,
1966 newTexParams.fFilter));
1967 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1968 GR_GL_TEXTURE_MIN_FILTER,
1969 newTexParams.fFilter));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001970 }
1971 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
1972 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001973 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1974 GR_GL_TEXTURE_WRAP_S,
1975 newTexParams.fWrapS));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001976 }
1977 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
1978 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001979 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1980 GR_GL_TEXTURE_WRAP_T,
1981 newTexParams.fWrapT));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001982 }
1983 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00001984 }
1985 }
1986
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001987 GrIRect* rect = NULL;
1988 GrIRect clipBounds;
1989 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
1990 fClip.hasConservativeBounds()) {
1991 fClip.getConservativeBounds().roundOut(&clipBounds);
1992 rect = &clipBounds;
1993 }
1994 this->flushRenderTarget(rect);
1995 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001996
reed@google.comac10a2d2010-12-22 21:39:39 +00001997 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
1998 (fHWDrawState.fFlagBits & kDither_StateBit)) {
1999 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002000 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002001 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002002 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002003 }
2004 }
2005
bsalomon@google.comd302f142011-03-03 13:54:13 +00002006 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2007 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002008 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002009 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002010 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002011 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002012 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002013 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002014 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002015 }
2016
bsalomon@google.comd302f142011-03-03 13:54:13 +00002017 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2018 switch (fCurrDrawState.fDrawFace) {
2019 case kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002020 GL_CALL(Enable(GR_GL_CULL_FACE));
2021 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002022 break;
2023 case kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002024 GL_CALL(Enable(GR_GL_CULL_FACE));
2025 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002026 break;
2027 case kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002028 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002029 break;
2030 default:
2031 GrCrash("Unknown draw face.");
2032 }
2033 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2034 }
2035
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002036#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002037 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002038 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002039 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002040 NULL == fCurrDrawState.fRenderTarget ||
2041 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002042 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002043 fCurrDrawState.fRenderTarget);
2044 }
2045#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002046
reed@google.comac10a2d2010-12-22 21:39:39 +00002047 flushStencil();
2048
bsalomon@google.comd302f142011-03-03 13:54:13 +00002049 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002050 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002051 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002052}
2053
2054void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002055 if (fHWGeometryState.fVertexBuffer != buffer) {
2056 fHWGeometryState.fArrayPtrsDirty = true;
2057 fHWGeometryState.fVertexBuffer = buffer;
2058 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002059}
2060
2061void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002062 if (fHWGeometryState.fVertexBuffer == buffer) {
2063 // deleting bound buffer does implied bind to 0
2064 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002065 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002066 }
2067}
2068
2069void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002070 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002071}
2072
2073void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002074 if (fHWGeometryState.fIndexBuffer == buffer) {
2075 // deleting bound buffer does implied bind to 0
2076 fHWGeometryState.fIndexBuffer = NULL;
2077 }
2078}
2079
reed@google.comac10a2d2010-12-22 21:39:39 +00002080void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2081 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002082 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002083 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002084 }
2085 if (fHWDrawState.fRenderTarget == renderTarget) {
2086 fHWDrawState.fRenderTarget = NULL;
2087 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002088}
2089
2090void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002091 for (int s = 0; s < kNumStages; ++s) {
2092 if (fCurrDrawState.fTextures[s] == texture) {
2093 fCurrDrawState.fTextures[s] = NULL;
2094 }
2095 if (fHWDrawState.fTextures[s] == texture) {
2096 // deleting bound texture does implied bind to 0
2097 fHWDrawState.fTextures[s] = NULL;
2098 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002099 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002100}
2101
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002102bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002103 GrGLenum* internalFormat,
2104 GrGLenum* format,
2105 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002106 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002107 case kRGBA_8888_GrPixelConfig:
2108 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002109 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002110 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002111 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2112 // format for a BGRA is BGRA not RGBA (as on desktop)
2113 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2114 } else {
2115 *internalFormat = GR_GL_RGBA;
2116 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002117 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002118 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002119 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002120 *format = GR_GL_RGB;
2121 *internalFormat = GR_GL_RGB;
2122 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002123 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002124 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002125 *format = GR_GL_RGBA;
2126 *internalFormat = GR_GL_RGBA;
2127 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002128 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002129 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002130 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002131 *format = GR_GL_PALETTE8_RGBA8;
2132 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002133 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002134 } else {
2135 return false;
2136 }
2137 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002138 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002139 *format = GR_GL_ALPHA;
2140 *internalFormat = GR_GL_ALPHA;
2141 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002142 break;
2143 default:
2144 return false;
2145 }
2146 return true;
2147}
2148
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002149void GrGpuGL::setTextureUnit(int unit) {
2150 GrAssert(unit >= 0 && unit < kNumStages);
2151 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002152 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002153 fActiveTextureUnitIdx = unit;
2154 }
2155}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002156
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002157void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002158 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002159 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002160 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2161 }
2162}
2163
reed@google.comac10a2d2010-12-22 21:39:39 +00002164/* On ES the internalFormat and format must match for TexImage and we use
2165 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2166 decide the internalFormat. However, on ES internalFormat for
2167 RenderBufferStorage* has to be a specific format (not a base format like
2168 GL_RGBA).
2169 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002170bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002171 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002172 case kRGBA_8888_GrPixelConfig:
2173 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002174 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002175 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002176 return true;
2177 } else {
2178 return false;
2179 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002180 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002181 // ES2 supports 565. ES1 supports it
2182 // with FBO extension desktop GL has
2183 // no such internal format
2184 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002185 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002186 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002187 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002188 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002189 return true;
2190 default:
2191 return false;
2192 }
2193}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002194
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002195void GrGpuGL::resetDirtyFlags() {
2196 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2197}
2198
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002199void GrGpuGL::setBuffers(bool indexed,
2200 int* extraVertexOffset,
2201 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002202
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002203 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002204
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002205 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2206
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002207 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002208 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002209 case kBuffer_GeometrySrcType:
2210 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002211 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002212 break;
2213 case kArray_GeometrySrcType:
2214 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002215 this->finalizeReservedVertices();
2216 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2217 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002218 break;
2219 default:
2220 vbuf = NULL; // suppress warning
2221 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002222 }
2223
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002224 GrAssert(NULL != vbuf);
2225 GrAssert(!vbuf->isLocked());
2226 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002227 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002228 fHWGeometryState.fArrayPtrsDirty = true;
2229 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002230 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002231
2232 if (indexed) {
2233 GrAssert(NULL != extraIndexOffset);
2234
2235 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002236 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002237 case kBuffer_GeometrySrcType:
2238 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002239 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002240 break;
2241 case kArray_GeometrySrcType:
2242 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002243 this->finalizeReservedIndices();
2244 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2245 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002246 break;
2247 default:
2248 ibuf = NULL; // suppress warning
2249 GrCrash("Unknown geometry src type!");
2250 }
2251
2252 GrAssert(NULL != ibuf);
2253 GrAssert(!ibuf->isLocked());
2254 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002255 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002256 fHWGeometryState.fIndexBuffer = ibuf;
2257 }
2258 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002259}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002260
2261int GrGpuGL::getMaxEdges() const {
2262 // FIXME: This is a pessimistic estimate based on how many other things
2263 // want to add uniforms. This should be centralized somewhere.
2264 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2265}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002266