blob: fefc914b5005762bf643788bc3526ac84c89a668 [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.com15b11df2011-09-16 21:18:29 +0000544 // This subclass must do this before the base class destructor runs
545 // since we will unref the GrGLInterface.
546 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000547 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000548}
549
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000550void GrGpuGL::resetContext() {
551 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000552 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000553 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000555 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000556 GL_CALL(Disable(GR_GL_DEPTH_TEST));
557 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000558
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000559 GL_CALL(Disable(GR_GL_CULL_FACE));
560 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000561 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000562
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000563 GL_CALL(Disable(GR_GL_DITHER));
564 if (kDesktop_GrGLBinding == this->glBinding()) {
565 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
566 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
567 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000568 fHWAAState.fMSAAEnabled = false;
569 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000570 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000572 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000573 fHWDrawState.fFlagBits = 0;
574
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000576 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000577
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000578 // invalid
579 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000582 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
583 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000584
585 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000586 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000587
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000589
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000590 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000591
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000592 for (int s = 0; s < kNumStages; ++s) {
593 fHWDrawState.fTextures[s] = NULL;
594 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
595 -GR_ScalarMax,
596 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000597 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000598 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000599 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000600
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000601 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000602 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000603 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000604 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
bsalomon@google.comd302f142011-03-03 13:54:13 +0000606 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000607 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000608 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000609
610 fHWGeometryState.fIndexBuffer = NULL;
611 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000612
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000613 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000614
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000615 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 fHWDrawState.fRenderTarget = NULL;
617}
618
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000619GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
620
621 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
622 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
623 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
624 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
625
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000626 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000627 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000628
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000629 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000630 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000631 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000632 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000633 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000634 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000635 } else {
636 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000637 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000638 }
639 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000640 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000641 }
642 // we don't know what the RB ids are without glGets and we don't care
643 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000644 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000645 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000646 if (desc.fStencilBits) {
647 GrGLStencilBuffer::Format format;
648 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
649 format.fPacked = false;
650 format.fStencilBits = desc.fStencilBits;
651 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000652 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
653 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000654 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000655 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000656 }
657
658 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000659 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000660 GrGLenum dontCare;
661 if (!canBeTexture(desc.fConfig, &dontCare,
662 &texDesc.fUploadFormat,
663 &texDesc.fUploadType)) {
664 return NULL;
665 }
666
667 GrGLTexture::TexParams params;
668
669 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
670 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
671
bsalomon@google.com90405932011-06-17 15:56:55 +0000672 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000673 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000674 texDesc.fTextureID = desc.fPlatformTexture;
675 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
676 texDesc.fOwnsID = false;
677
678 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000679 if (isRenderTarget) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000680 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc, params);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000681 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000682 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000683 } else {
684 return new GrGLTexture(this, texDesc, params);
685 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000686 } else {
687 GrGLIRect viewport;
688 viewport.fLeft = 0;
689 viewport.fBottom = 0;
690 viewport.fWidth = desc.fWidth;
691 viewport.fHeight = desc.fHeight;
692
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000693 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000694 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000695 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000696 }
697}
698
bsalomon@google.com5782d712011-01-21 21:03:59 +0000699///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000700
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000701static const GrGLuint kUnknownBitCount = ~0;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000702
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000703void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000704
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000705 // Build up list of legal stencil formats (though perhaps not supported on
706 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000707
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000708 // these consts are in order of most preferred to least preferred
709 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000710 static const GrGLStencilBuffer::Format
711 // internal Format stencil bits total bits packed?
712 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
713 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
714 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
715 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
716 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
717 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000718
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000719 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000720 bool supportsPackedDS = fGLVersion >= 3.0f ||
721 this->hasExtension("GL_EXT_packed_depth_stencil") ||
722 this->hasExtension("GL_ARB_framebuffer_object");
723
724 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
725 // require FBO support we can expect these are legal formats and don't
726 // check. These also all support the unsized GL_STENCIL_INDEX.
727 fStencilFormats.push_back() = gS8;
728 fStencilFormats.push_back() = gS16;
729 if (supportsPackedDS) {
730 fStencilFormats.push_back() = gD24S8;
731 }
732 fStencilFormats.push_back() = gS4;
733 if (supportsPackedDS) {
734 fStencilFormats.push_back() = gDS;
735 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000736 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000737 // ES2 has STENCIL_INDEX8 without extensions.
738 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
739 // introduces tokens for S1 thu S8 but there are separate extensions
740 // that make them legal (GL_OES_stencil1, ...).
741 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
742 // ES doesn't support using the unsized formats.
743
744 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
745 fStencilFormats.push_back() = gS8;
746 }
747 //fStencilFormats.push_back() = gS16;
748 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
749 fStencilFormats.push_back() = gD24S8;
750 }
751 if (this->hasExtension("GL_OES_stencil4")) {
752 fStencilFormats.push_back() = gS4;
753 }
754 // we require some stencil format.
755 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000756 }
757}
758
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000759////////////////////////////////////////////////////////////////////////////////
760
761void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
762 GrGLenum internalFormat,
763 const void* data,
764 size_t rowBytes) {
765 // we assume the texture is bound;
766 if (!rowBytes) {
767 rowBytes = desc.fUploadByteCount * desc.fContentWidth;
768 }
769
770 // in case we need a temporary, trimmed copy of the src pixels
771 SkAutoSMalloc<128 * 128> tempStorage;
772
773 /*
774 * check whether to allocate a temporary buffer for flipping y or
775 * because our data has extra bytes past each row. If so, we need
776 * to trim those off here, since GL ES doesn't let us specify
777 * GL_UNPACK_ROW_LENGTH.
778 */
779 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000780 if (kDesktop_GrGLBinding == this->glBinding() && !flipY) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000781 if (data && rowBytes != desc.fContentWidth * desc.fUploadByteCount) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000782 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
783 rowBytes / desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000784 }
785 } else {
786 size_t trimRowBytes = desc.fContentWidth * desc.fUploadByteCount;
787 if (data && (trimRowBytes < rowBytes || flipY)) {
788 // copy the data into our new storage, skipping the trailing bytes
789 size_t trimSize = desc.fContentHeight * trimRowBytes;
790 const char* src = (const char*)data;
791 if (flipY) {
792 src += (desc.fContentHeight - 1) * rowBytes;
793 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000794 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000795 for (int y = 0; y < desc.fContentHeight; y++) {
796 memcpy(dst, src, trimRowBytes);
797 if (flipY) {
798 src -= rowBytes;
799 } else {
800 src += rowBytes;
801 }
802 dst += trimRowBytes;
803 }
804 // now point data to our trimmed version
805 data = tempStorage.get();
806 rowBytes = trimRowBytes;
807 }
808 }
809
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000810 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, desc.fUploadByteCount));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000811 if (kIndex_8_GrPixelConfig == desc.fFormat &&
812 supports8BitPalette()) {
813 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
814 GrAssert(desc.fContentWidth == desc.fAllocWidth);
815 GrAssert(desc.fContentHeight == desc.fAllocHeight);
816 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
817 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000818 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
819 desc.fAllocWidth, desc.fAllocHeight,
820 0, imageSize, data));
821 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000822 } else {
823 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
824 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000825 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
826 desc.fAllocWidth, desc.fAllocHeight,
827 0, desc.fUploadFormat, desc.fUploadType, NULL));
828 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
829 desc.fContentHeight, desc.fUploadFormat,
830 desc.fUploadType, data));
831 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000832
833 int extraW = desc.fAllocWidth - desc.fContentWidth;
834 int extraH = desc.fAllocHeight - desc.fContentHeight;
835 int maxTexels = extraW * extraH;
836 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
837 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
838
839 SkAutoSMalloc<128*128> texels(desc.fUploadByteCount * maxTexels);
840
841 // rowBytes is actual stride between rows in data
842 // rowDataBytes is the actual amount of non-pad data in a row
843 // and the stride used for uploading extraH rows.
844 uint32_t rowDataBytes = desc.fContentWidth * desc.fUploadByteCount;
845 if (extraH) {
846 uint8_t* lastRowStart = (uint8_t*) data +
847 (desc.fContentHeight - 1) * rowBytes;
848 uint8_t* extraRowStart = (uint8_t*)texels.get();
849
850 for (int i = 0; i < extraH; ++i) {
851 memcpy(extraRowStart, lastRowStart, rowDataBytes);
852 extraRowStart += rowDataBytes;
853 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000854 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
855 desc.fContentHeight, desc.fContentWidth,
856 extraH, desc.fUploadFormat,
857 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000858 }
859 if (extraW) {
860 uint8_t* edgeTexel = (uint8_t*)data +
861 rowDataBytes - desc.fUploadByteCount;
862 uint8_t* extraTexel = (uint8_t*)texels.get();
863 for (int j = 0; j < desc.fContentHeight; ++j) {
864 for (int i = 0; i < extraW; ++i) {
865 memcpy(extraTexel, edgeTexel, desc.fUploadByteCount);
866 extraTexel += desc.fUploadByteCount;
867 }
868 edgeTexel += rowBytes;
869 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000870 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
871 0, extraW, desc.fContentHeight,
872 desc.fUploadFormat, desc.fUploadType,
873 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000874 }
875 if (extraW && extraH) {
876 uint8_t* cornerTexel = (uint8_t*)data +
877 desc.fContentHeight * rowBytes -
878 desc.fUploadByteCount;
879 uint8_t* extraTexel = (uint8_t*)texels.get();
880 for (int i = 0; i < extraW*extraH; ++i) {
881 memcpy(extraTexel, cornerTexel, desc.fUploadByteCount);
882 extraTexel += desc.fUploadByteCount;
883 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000884 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
885 desc.fContentHeight, extraW, extraH,
886 desc.fUploadFormat, desc.fUploadType,
887 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000888 }
889
890 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
892 desc.fAllocWidth, desc.fAllocHeight, 0,
893 desc.fUploadFormat, desc.fUploadType, data));
894 GrGLResetRowLength(this->glInterface());
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000895 }
896 }
897}
898
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899bool GrGpuGL::createRenderTargetObjects(int width, int height,
900 GrGLuint texID,
901 GrGLRenderTarget::Desc* desc) {
902 desc->fMSColorRenderbufferID = 0;
903 desc->fRTFBOID = 0;
904 desc->fTexFBOID = 0;
905 desc->fOwnIDs = true;
906
907 GrGLenum status;
908 GrGLint err;
909
bsalomon@google.comab15d612011-08-09 12:57:56 +0000910 GrGLenum msColorFormat = 0; // suppress warning
911
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000912 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000913 if (!desc->fTexFBOID) {
914 goto FAILED;
915 }
916
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917
918 // If we are using multisampling we will create two FBOS. We render
919 // to one and then resolve to the texture bound to the other.
920 if (desc->fSampleCnt > 1 && kNone_MSFBO != fMSFBOType) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000921 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
922 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000923 if (!desc->fRTFBOID ||
924 !desc->fMSColorRenderbufferID ||
925 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
926 goto FAILED;
927 }
928 } else {
929 desc->fRTFBOID = desc->fTexFBOID;
930 }
931
932 if (desc->fRTFBOID != desc->fTexFBOID) {
933 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000934 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GR_GL_CALL_NOERRCHECK(this->glInterface(),
937 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
938 desc->fSampleCnt,
939 msColorFormat,
940 width, height));
941 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942 if (err != GR_GL_NO_ERROR) {
943 goto FAILED;
944 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000945 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
946 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000947 GR_GL_COLOR_ATTACHMENT0,
948 GR_GL_RENDERBUFFER,
949 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000950 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000951 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
952 goto FAILED;
953 }
954 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000955 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000956
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000957 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
958 GR_GL_COLOR_ATTACHMENT0,
959 GR_GL_TEXTURE_2D,
960 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000961 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000962 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
963 goto FAILED;
964 }
965
966 return true;
967
968FAILED:
969 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000970 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 }
972 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000973 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000974 }
975 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000976 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000977 }
978 return false;
979}
980
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000981// good to set a break-point here to know when createTexture fails
982static GrTexture* return_null_texture() {
983// GrAssert(!"null texture");
984 return NULL;
985}
986
987#if GR_DEBUG
988static size_t as_size_t(int x) {
989 return x;
990}
991#endif
992
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000993GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000994 const void* srcData,
995 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000996
997#if GR_COLLECT_STATS
998 ++fStats.fTextureCreateCnt;
999#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001000
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001001 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001002 GR_GL_NEAREST,
1003 GR_GL_CLAMP_TO_EDGE,
1004 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001005 };
reed@google.com1fcd51e2011-01-05 15:50:27 +00001006
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001007 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001008 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001009 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001010
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001011 glTexDesc.fContentWidth = desc.fWidth;
1012 glTexDesc.fContentHeight = desc.fHeight;
1013 glTexDesc.fAllocWidth = desc.fWidth;
1014 glTexDesc.fAllocHeight = desc.fHeight;
1015 glTexDesc.fFormat = desc.fFormat;
1016 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001017
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001018 glRTDesc.fMSColorRenderbufferID = 0;
1019 glRTDesc.fRTFBOID = 0;
1020 glRTDesc.fTexFBOID = 0;
1021 glRTDesc.fOwnIDs = true;
1022 glRTDesc.fConfig = glTexDesc.fFormat;
1023
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001024 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001025 if (!canBeTexture(desc.fFormat,
1026 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001027 &glTexDesc.fUploadFormat,
1028 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 return return_null_texture();
1030 }
1031
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001032 // We keep GrRenderTargets in GL's normal orientation so that they
1033 // can be drawn to by the outside world without the client having
1034 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001035 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001036 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001037
reed@google.comac10a2d2010-12-22 21:39:39 +00001038 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001039 glRTDesc.fSampleCnt = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001040 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001041 GrPrintf("AA RT requested but not supported on this platform.");
1042 }
1043
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001044 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +00001045
reed@google.comac10a2d2010-12-22 21:39:39 +00001046 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001047 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001048 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1049 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001050 }
1051
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001052 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001053 glTexDesc.fAllocWidth);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001054 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001055 glTexDesc.fAllocHeight);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001056 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1057 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001058 return return_null_texture();
1059 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001060 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001061 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1062 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1063 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1064 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001065 return return_null_texture();
1066 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 }
1068
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001069 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001070 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001071 return return_null_texture();
1072 }
1073
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001074 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001075 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1077 GR_GL_TEXTURE_MAG_FILTER,
1078 DEFAULT_PARAMS.fFilter));
1079 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1080 GR_GL_TEXTURE_MIN_FILTER,
1081 DEFAULT_PARAMS.fFilter));
1082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1083 GR_GL_TEXTURE_WRAP_S,
1084 DEFAULT_PARAMS.fWrapS));
1085 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1086 GR_GL_TEXTURE_WRAP_T,
1087 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001088
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001089 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001090
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001092 if (renderTarget) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001093 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094#if GR_COLLECT_STATS
1095 ++fStats.fRenderTargetCreateCnt;
1096#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1098 glTexDesc.fAllocHeight,
1099 glTexDesc.fTextureID,
1100 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001101 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001102 return return_null_texture();
1103 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001104 tex = new GrGLTexture(this, glTexDesc, glRTDesc, DEFAULT_PARAMS);
1105 } else {
1106 tex = new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001107 }
1108#ifdef TRACE_TEXTURE_CREATION
1109 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1110 tex->fTextureID, width, height, tex->fUploadByteCount);
1111#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 return tex;
1113}
1114
1115namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001116void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1117 GrGLuint rb,
1118 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 // we shouldn't ever know one size and not the other
1120 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1121 (kUnknownBitCount == format->fTotalBits));
1122 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001123 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001124 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1125 (GrGLint*)&format->fStencilBits);
1126 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1129 (GrGLint*)&format->fTotalBits);
1130 format->fTotalBits += format->fStencilBits;
1131 } else {
1132 format->fTotalBits = format->fStencilBits;
1133 }
1134 }
1135}
1136}
1137
1138bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1139 int width, int height) {
1140
1141 // All internally created RTs are also textures. We don't create
1142 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1143 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001144 GrAssert(width >= rt->allocatedWidth());
1145 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146
1147 int samples = rt->numSamples();
1148 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001149 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 if (!sbID) {
1151 return false;
1152 }
1153
1154 GrGLStencilBuffer* sb = NULL;
1155
1156 int stencilFmtCnt = fStencilFormats.count();
1157 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001158 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 // we start with the last stencil format that succeeded in hopes
1160 // that we won't go through this loop more than once after the
1161 // first (painful) stencil creation.
1162 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001163 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001164 // version on a GL that doesn't have an MSAA extension.
1165 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001166 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1167 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 GR_GL_RENDERBUFFER,
1169 samples,
1170 fStencilFormats[sIdx].fInternalFormat,
1171 width,
1172 height));
1173 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001174 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1175 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 fStencilFormats[sIdx].fInternalFormat,
1177 width, height));
1178 }
1179
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001180 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 if (err == GR_GL_NO_ERROR) {
1182 // After sized formats we attempt an unsized format and take whatever
1183 // sizes GL gives us. In that case we query for the size.
1184 GrGLStencilBuffer::Format format = fStencilFormats[sIdx];
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001185 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001186 sb = new GrGLStencilBuffer(this, sbID, width, height,
1187 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1189 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001190 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 return true;
1193 }
1194 sb->abandon(); // otherwise we lose sbID
1195 sb->unref();
1196 }
1197 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001198 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001199 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200}
1201
1202bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1203 GrRenderTarget* rt) {
1204 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1205
1206 GrGLuint fbo = glrt->renderFBOID();
1207
1208 if (NULL == sb) {
1209 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001210 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 GR_GL_STENCIL_ATTACHMENT,
1212 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001214 GR_GL_DEPTH_ATTACHMENT,
1215 GR_GL_RENDERBUFFER, 0));
1216#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001217 GrGLenum status;
1218 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1220#endif
1221 }
1222 return true;
1223 } else {
1224 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1225 GrGLuint rb = glsb->renderbufferID();
1226
reed@google.comac10a2d2010-12-22 21:39:39 +00001227 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001228 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1229 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001230 GR_GL_STENCIL_ATTACHMENT,
1231 GR_GL_RENDERBUFFER, rb));
1232 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001233 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 GR_GL_DEPTH_ATTACHMENT,
1235 GR_GL_RENDERBUFFER, rb));
1236 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 GR_GL_DEPTH_ATTACHMENT,
1239 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001242 GrGLenum status;
1243 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001245 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001246 GR_GL_STENCIL_ATTACHMENT,
1247 GR_GL_RENDERBUFFER, 0));
1248 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001249 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250 GR_GL_DEPTH_ATTACHMENT,
1251 GR_GL_RENDERBUFFER, 0));
1252 }
1253 return false;
1254 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255 return true;
1256 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001257 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001258}
1259
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001260////////////////////////////////////////////////////////////////////////////////
1261
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001262GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001263 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001264 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001266 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001267 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001268 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001270 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1271 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1272 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1273 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1274 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001275 // deleting bound buffer does implicit bind to 0
1276 fHWGeometryState.fVertexBuffer = NULL;
1277 return NULL;
1278 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001279 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 size, dynamic);
1281 fHWGeometryState.fVertexBuffer = vertexBuffer;
1282 return vertexBuffer;
1283 }
1284 return NULL;
1285}
1286
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001287GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001288 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001291 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1292 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001294 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1295 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1296 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1297 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1298 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 // deleting bound buffer does implicit bind to 0
1300 fHWGeometryState.fIndexBuffer = NULL;
1301 return NULL;
1302 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001303 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 size, dynamic);
1305 fHWGeometryState.fIndexBuffer = indexBuffer;
1306 return indexBuffer;
1307 }
1308 return NULL;
1309}
1310
reed@google.comac10a2d2010-12-22 21:39:39 +00001311void GrGpuGL::flushScissor(const GrIRect* rect) {
1312 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001313 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001314 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001315
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001316 GrGLIRect scissor;
1317 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001318 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001319 rect->width(), rect->height());
1320 if (scissor.contains(vp)) {
1321 rect = NULL;
1322 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001323 }
1324
1325 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001327 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 fHWBounds.fScissorRect = scissor;
1329 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001331 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001332 fHWBounds.fScissorEnabled = true;
1333 }
1334 } else {
1335 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001336 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 fHWBounds.fScissorEnabled = false;
1338 }
1339 }
1340}
1341
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001342void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001343 if (NULL == fCurrDrawState.fRenderTarget) {
1344 return;
1345 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001346 GrIRect r;
1347 if (NULL != rect) {
1348 // flushScissor expects rect to be clipped to the target.
1349 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001350 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1351 fCurrDrawState.fRenderTarget->height());
1352 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001353 rect = &r;
1354 } else {
1355 return;
1356 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001358 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001359 this->flushScissor(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001360 GL_CALL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001361 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001362 GL_CALL(ClearColor(GrColorUnpackR(color)/255.f,
1363 GrColorUnpackG(color)/255.f,
1364 GrColorUnpackB(color)/255.f,
1365 GrColorUnpackA(color)/255.f));
1366 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001367}
1368
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001369void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001370 if (NULL == fCurrDrawState.fRenderTarget) {
1371 return;
1372 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001373
1374 this->flushRenderTarget(&GrIRect::EmptyIRect());
1375
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001377 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001378 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001380 GL_CALL(StencilMask(0xffffffff));
1381 GL_CALL(ClearStencil(0));
1382 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001383 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001384}
1385
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001386void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001387 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001388
1389 // this should only be called internally when we know we have a
1390 // stencil buffer.
1391 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001392 GrGLint stencilBitCount =
1393 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001394#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001395 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001396 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001397#else
1398 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001399 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001400 // turned into draws. Our contract on GrDrawTarget says that
1401 // changing the clip between stencil passes may or may not
1402 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001403 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001404#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001405 GrGLint value;
1406 if (insideClip) {
1407 value = (1 << (stencilBitCount - 1));
1408 } else {
1409 value = 0;
1410 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001411 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001412 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001413 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001414 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001415 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001416 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001417}
1418
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001419void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001420 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001421}
1422
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001423bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1424 int left, int top, int width, int height,
1425 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001426 GrGLenum internalFormat; // we don't use this for glReadPixels
1427 GrGLenum format;
1428 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001429 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1430 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001431 }
1432 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1433 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1434 switch (tgt->getResolveType()) {
1435 case GrGLRenderTarget::kCantResolve_ResolveType:
1436 return false;
1437 case GrGLRenderTarget::kAutoResolves_ResolveType:
1438 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1439 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001440 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001441 break;
1442 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001443 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001444 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001445 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1446 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001447 break;
1448 default:
1449 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001450 }
1451
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001452 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001453
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001454 // the read rect is viewport-relative
1455 GrGLIRect readRect;
1456 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001457 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1458 readRect.fWidth, readRect.fHeight,
1459 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001460
1461 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1462 // API presents top-to-bottom
1463 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001464 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001465 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001466 void* tmp = rowStorage.get();
1467
1468 const int halfY = height >> 1;
1469 char* top = reinterpret_cast<char*>(buffer);
1470 char* bottom = top + (height - 1) * stride;
1471 for (int y = 0; y < halfY; y++) {
1472 memcpy(tmp, top, stride);
1473 memcpy(top, bottom, stride);
1474 memcpy(bottom, tmp, stride);
1475 top += stride;
1476 bottom -= stride;
1477 }
1478 }
1479 return true;
1480}
1481
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001482void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001483
1484 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1485
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001486 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001488 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001489 #if GR_COLLECT_STATS
1490 ++fStats.fRenderTargetChngCnt;
1491 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001492 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001493 GrGLenum status;
1494 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001495 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001496 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001497 }
1498 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001499 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001500 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001501 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001502 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001503 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001504 fHWBounds.fViewportRect = vp;
1505 }
1506 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001507 if (NULL == bound || !bound->isEmpty()) {
1508 rt->flagAsNeedingResolve(bound);
1509 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001510}
1511
twiz@google.com0f31ca72011-03-18 17:38:11 +00001512GrGLenum gPrimitiveType2GLMode[] = {
1513 GR_GL_TRIANGLES,
1514 GR_GL_TRIANGLE_STRIP,
1515 GR_GL_TRIANGLE_FAN,
1516 GR_GL_POINTS,
1517 GR_GL_LINES,
1518 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001519};
1520
bsalomon@google.comd302f142011-03-03 13:54:13 +00001521#define SWAP_PER_DRAW 0
1522
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001523#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001524 #if GR_MAC_BUILD
1525 #include <AGL/agl.h>
1526 #elif GR_WIN32_BUILD
1527 void SwapBuf() {
1528 DWORD procID = GetCurrentProcessId();
1529 HWND hwnd = GetTopWindow(GetDesktopWindow());
1530 while(hwnd) {
1531 DWORD wndProcID = 0;
1532 GetWindowThreadProcessId(hwnd, &wndProcID);
1533 if(wndProcID == procID) {
1534 SwapBuffers(GetDC(hwnd));
1535 }
1536 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1537 }
1538 }
1539 #endif
1540#endif
1541
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001542void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1543 uint32_t startVertex,
1544 uint32_t startIndex,
1545 uint32_t vertexCount,
1546 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001547 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1548
twiz@google.com0f31ca72011-03-18 17:38:11 +00001549 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001550
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001551 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1552 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1553
1554 // our setupGeometry better have adjusted this to zero since
1555 // DrawElements always draws from the begining of the arrays for idx 0.
1556 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001557
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001558 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1559 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001560#if SWAP_PER_DRAW
1561 glFlush();
1562 #if GR_MAC_BUILD
1563 aglSwapBuffers(aglGetCurrentContext());
1564 int set_a_break_pt_here = 9;
1565 aglSwapBuffers(aglGetCurrentContext());
1566 #elif GR_WIN32_BUILD
1567 SwapBuf();
1568 int set_a_break_pt_here = 9;
1569 SwapBuf();
1570 #endif
1571#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001572}
1573
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001574void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1575 uint32_t startVertex,
1576 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001577 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1578
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001579 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1580
1581 // our setupGeometry better have adjusted this to zero.
1582 // DrawElements doesn't take an offset so we always adjus the startVertex.
1583 GrAssert(0 == startVertex);
1584
1585 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1586 // account for startVertex in the DrawElements case. So we always
1587 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001588 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001589#if SWAP_PER_DRAW
1590 glFlush();
1591 #if GR_MAC_BUILD
1592 aglSwapBuffers(aglGetCurrentContext());
1593 int set_a_break_pt_here = 9;
1594 aglSwapBuffers(aglGetCurrentContext());
1595 #elif GR_WIN32_BUILD
1596 SwapBuf();
1597 int set_a_break_pt_here = 9;
1598 SwapBuf();
1599 #endif
1600#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001601}
1602
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001603void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001604
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001605 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001606 GrAssert(kNone_MSFBO != fMSFBOType);
1607 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001608 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1609 rt->renderFBOID()));
1610 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1611 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 #if GR_COLLECT_STATS
1613 ++fStats.fRenderTargetChngCnt;
1614 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001615 // make sure we go through flushRenderTarget() since we've modified
1616 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001618 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001619 const GrIRect dirtyRect = rt->getResolveRect();
1620 GrGLIRect r;
1621 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1622 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001623
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001624 if (kAppleES_MSFBO == fMSFBOType) {
1625 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001626 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1627 GL_CALL(Scissor(r.fLeft, r.fBottom,
1628 r.fWidth, r.fHeight));
1629 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001630 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001631 fHWBounds.fScissorEnabled = true;
1632 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001633 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001634 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001635 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1636 flushScissor(NULL);
1637 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001638 int right = r.fLeft + r.fWidth;
1639 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001640 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1641 r.fLeft, r.fBottom, right, top,
1642 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001643 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001644 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001645 }
1646}
1647
twiz@google.com0f31ca72011-03-18 17:38:11 +00001648static const GrGLenum grToGLStencilFunc[] = {
1649 GR_GL_ALWAYS, // kAlways_StencilFunc
1650 GR_GL_NEVER, // kNever_StencilFunc
1651 GR_GL_GREATER, // kGreater_StencilFunc
1652 GR_GL_GEQUAL, // kGEqual_StencilFunc
1653 GR_GL_LESS, // kLess_StencilFunc
1654 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1655 GR_GL_EQUAL, // kEqual_StencilFunc,
1656 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001657};
1658GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1659GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1660GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1661GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1662GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1663GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1664GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1665GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1666GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1667
twiz@google.com0f31ca72011-03-18 17:38:11 +00001668static const GrGLenum grToGLStencilOp[] = {
1669 GR_GL_KEEP, // kKeep_StencilOp
1670 GR_GL_REPLACE, // kReplace_StencilOp
1671 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1672 GR_GL_INCR, // kIncClamp_StencilOp
1673 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1674 GR_GL_DECR, // kDecClamp_StencilOp
1675 GR_GL_ZERO, // kZero_StencilOp
1676 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001677};
1678GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1679GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1680GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1681GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1682GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1683GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1684GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1685GR_STATIC_ASSERT(6 == kZero_StencilOp);
1686GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1687
reed@google.comac10a2d2010-12-22 21:39:39 +00001688void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001689 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001690
1691 // use stencil for clipping if clipping is enabled and the clip
1692 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001693 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001694 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001695 bool stencilChange = fHWStencilClip != stencilClip ||
1696 fHWDrawState.fStencilSettings != *settings ||
1697 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1698 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001699
1700 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001701
bsalomon@google.comd302f142011-03-03 13:54:13 +00001702 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1703 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001704
bsalomon@google.comd302f142011-03-03 13:54:13 +00001705 if (settings->isDisabled()) {
1706 if (stencilClip) {
1707 settings = &gClipStencilSettings;
1708 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001709 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001710
1711 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001712 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001713 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001714 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001715 #if GR_DEBUG
1716 if (!fStencilWrapOpsSupport) {
1717 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1718 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1719 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1720 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1721 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1722 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1723 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1724 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1725 }
1726 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001727 int stencilBits = 0;
1728 GrStencilBuffer* stencilBuffer =
1729 fCurrDrawState.fRenderTarget->getStencilBuffer();
1730 if (NULL != stencilBuffer) {
1731 stencilBits = stencilBuffer->bits();
1732 }
1733 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001734 GrAssert(stencilBits ||
1735 (GrStencilSettings::gDisabled ==
1736 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001737 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1738 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001739
1740 unsigned int frontRef = settings->fFrontFuncRef;
1741 unsigned int frontMask = settings->fFrontFuncMask;
1742 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001743 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001744
1745 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1746
1747 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1748 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1749 } else {
1750 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1751
1752 ConvertStencilFuncAndMask(settings->fFrontFunc,
1753 stencilClip,
1754 clipStencilMask,
1755 userStencilMask,
1756 &frontRef,
1757 &frontMask);
1758 frontWriteMask &= userStencilMask;
1759 }
1760 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001761 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001762 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001763 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001764 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001765 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001766 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001767 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001768 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001769 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001770
1771 unsigned int backRef = settings->fBackFuncRef;
1772 unsigned int backMask = settings->fBackFuncMask;
1773 unsigned int backWriteMask = settings->fBackWriteMask;
1774
1775
1776 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1777 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1778 backFunc = grToGLStencilFunc[settings->fBackFunc];
1779 } else {
1780 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1781 ConvertStencilFuncAndMask(settings->fBackFunc,
1782 stencilClip,
1783 clipStencilMask,
1784 userStencilMask,
1785 &backRef,
1786 &backMask);
1787 backWriteMask &= userStencilMask;
1788 }
1789
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001790 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1791 frontRef, frontMask));
1792 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1793 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1794 backRef, backMask));
1795 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1796 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1797 grToGLStencilOp[settings->fFrontFailOp],
1798 grToGLStencilOp[settings->fFrontPassOp],
1799 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001800
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001801 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1802 grToGLStencilOp[settings->fBackFailOp],
1803 grToGLStencilOp[settings->fBackPassOp],
1804 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001805 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001806 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1807 GL_CALL(StencilMask(frontWriteMask));
1808 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001809 grToGLStencilOp[settings->fFrontPassOp],
1810 grToGLStencilOp[settings->fFrontPassOp]));
1811 }
1812 }
1813 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001814 fHWStencilClip = stencilClip;
1815 }
1816}
1817
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001818void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001819 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001820 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1821 // smooth lines.
1822
1823 // we prefer smooth lines over multisampled lines
1824 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001825 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001826 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001827 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001828 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001829 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001830 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001831 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001832 fHWAAState.fSmoothLineEnabled = false;
1833 }
1834 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1835 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001836 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001837 fHWAAState.fMSAAEnabled = false;
1838 }
1839 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1840 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
1841 fHWAAState.fMSAAEnabled) {
1842 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001843 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001844 fHWAAState.fMSAAEnabled = false;
1845 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001846 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001847 fHWAAState.fMSAAEnabled = true;
1848 }
1849 }
1850 }
1851}
1852
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001853void GrGpuGL::flushBlend(GrPrimitiveType type,
1854 GrBlendCoeff srcCoeff,
1855 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001856 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001857 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001858 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001859 fHWBlendDisabled = false;
1860 }
1861 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1862 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001863 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1864 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001865 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1866 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1867 }
1868 } else {
1869 bool blendOff = canDisableBlend();
1870 if (fHWBlendDisabled != blendOff) {
1871 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001872 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001873 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001874 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001875 }
1876 fHWBlendDisabled = blendOff;
1877 }
1878 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001879 if (fHWDrawState.fSrcBlend != srcCoeff ||
1880 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001881 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1882 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001883 fHWDrawState.fSrcBlend = srcCoeff;
1884 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001885 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001886 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1887 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001888 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1889
1890 float c[] = {
1891 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1892 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1893 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1894 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1895 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001896 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001897 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1898 }
1899 }
1900 }
1901}
1902
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001903static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1904 switch (filter) {
1905 case GrSamplerState::kBilinear_Filter:
1906 case GrSamplerState::k4x4Downsample_Filter:
1907 return GR_GL_LINEAR;
1908 case GrSamplerState::kNearest_Filter:
1909 case GrSamplerState::kConvolution_Filter:
1910 return GR_GL_NEAREST;
1911 default:
1912 GrAssert(!"Unknown filter type");
1913 return GR_GL_LINEAR;
1914 }
1915}
1916
bsalomon@google.comffca4002011-02-22 20:34:01 +00001917bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001918
1919 // GrGpu::setupClipAndFlushState should have already checked this
1920 // and bailed if not true.
1921 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001922
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001923 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001924 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001925 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001926 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001927
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001928 // true for now, but maybe not with GrEffect.
1929 GrAssert(NULL != nextTexture);
1930 // if we created a rt/tex and rendered to it without using a
1931 // texture and now we're texuring from the rt it will still be
1932 // the last bound texture, but it needs resolving. So keep this
1933 // out of the "last != next" check.
1934 GrGLRenderTarget* texRT =
1935 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1936 if (NULL != texRT) {
1937 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001938 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001939
1940 if (fHWDrawState.fTextures[s] != nextTexture) {
1941 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001942 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001943 #if GR_COLLECT_STATS
1944 ++fStats.fTextureChngCnt;
1945 #endif
1946 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
1947 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001948 // The texture matrix has to compensate for texture width/height
1949 // and NPOT-embedded-in-POT
1950 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001951 }
1952
1953 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
1954 const GrGLTexture::TexParams& oldTexParams =
1955 nextTexture->getTexParams();
1956 GrGLTexture::TexParams newTexParams;
1957
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001958 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001959
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001960 const GrGLenum* wraps =
1961 GrGLTexture::WrapMode2GLWrap(this->glBinding());
1962 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1963 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001964
1965 if (newTexParams.fFilter != oldTexParams.fFilter) {
1966 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001967 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1968 GR_GL_TEXTURE_MAG_FILTER,
1969 newTexParams.fFilter));
1970 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1971 GR_GL_TEXTURE_MIN_FILTER,
1972 newTexParams.fFilter));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001973 }
1974 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
1975 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001976 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1977 GR_GL_TEXTURE_WRAP_S,
1978 newTexParams.fWrapS));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001979 }
1980 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
1981 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001982 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1983 GR_GL_TEXTURE_WRAP_T,
1984 newTexParams.fWrapT));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001985 }
1986 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00001987 }
1988 }
1989
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001990 GrIRect* rect = NULL;
1991 GrIRect clipBounds;
1992 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
1993 fClip.hasConservativeBounds()) {
1994 fClip.getConservativeBounds().roundOut(&clipBounds);
1995 rect = &clipBounds;
1996 }
1997 this->flushRenderTarget(rect);
1998 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001999
reed@google.comac10a2d2010-12-22 21:39:39 +00002000 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2001 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2002 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002003 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002004 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002005 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002006 }
2007 }
2008
bsalomon@google.comd302f142011-03-03 13:54:13 +00002009 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2010 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002011 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002012 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002013 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002014 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002015 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002016 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002017 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002018 }
2019
bsalomon@google.comd302f142011-03-03 13:54:13 +00002020 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2021 switch (fCurrDrawState.fDrawFace) {
2022 case kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002023 GL_CALL(Enable(GR_GL_CULL_FACE));
2024 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002025 break;
2026 case kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002027 GL_CALL(Enable(GR_GL_CULL_FACE));
2028 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002029 break;
2030 case kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002031 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002032 break;
2033 default:
2034 GrCrash("Unknown draw face.");
2035 }
2036 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2037 }
2038
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002039#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002040 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002041 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002042 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002043 NULL == fCurrDrawState.fRenderTarget ||
2044 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002045 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002046 fCurrDrawState.fRenderTarget);
2047 }
2048#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002049
reed@google.comac10a2d2010-12-22 21:39:39 +00002050 flushStencil();
2051
bsalomon@google.comd302f142011-03-03 13:54:13 +00002052 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002053 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002054 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002055}
2056
2057void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002058 if (fHWGeometryState.fVertexBuffer != buffer) {
2059 fHWGeometryState.fArrayPtrsDirty = true;
2060 fHWGeometryState.fVertexBuffer = buffer;
2061 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002062}
2063
2064void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002065 if (fHWGeometryState.fVertexBuffer == buffer) {
2066 // deleting bound buffer does implied bind to 0
2067 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002068 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002069 }
2070}
2071
2072void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002073 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002074}
2075
2076void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002077 if (fHWGeometryState.fIndexBuffer == buffer) {
2078 // deleting bound buffer does implied bind to 0
2079 fHWGeometryState.fIndexBuffer = NULL;
2080 }
2081}
2082
reed@google.comac10a2d2010-12-22 21:39:39 +00002083void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2084 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002085 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002086 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002087 }
2088 if (fHWDrawState.fRenderTarget == renderTarget) {
2089 fHWDrawState.fRenderTarget = NULL;
2090 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002091}
2092
2093void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002094 for (int s = 0; s < kNumStages; ++s) {
2095 if (fCurrDrawState.fTextures[s] == texture) {
2096 fCurrDrawState.fTextures[s] = NULL;
2097 }
2098 if (fHWDrawState.fTextures[s] == texture) {
2099 // deleting bound texture does implied bind to 0
2100 fHWDrawState.fTextures[s] = NULL;
2101 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002102 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002103}
2104
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002105bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002106 GrGLenum* internalFormat,
2107 GrGLenum* format,
2108 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002109 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002110 case kRGBA_8888_GrPixelConfig:
2111 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002112 *format = GR_GL_32BPP_COLOR_FORMAT;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002113 if (kDesktop_GrGLBinding != this->glBinding()) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002114 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2115 // format for a BGRA is BGRA not RGBA (as on desktop)
2116 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2117 } else {
2118 *internalFormat = GR_GL_RGBA;
2119 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002120 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002121 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002122 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002123 *format = GR_GL_RGB;
2124 *internalFormat = GR_GL_RGB;
2125 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002126 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002127 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002128 *format = GR_GL_RGBA;
2129 *internalFormat = GR_GL_RGBA;
2130 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002131 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002132 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002133 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002134 *format = GR_GL_PALETTE8_RGBA8;
2135 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002136 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002137 } else {
2138 return false;
2139 }
2140 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002141 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002142 *format = GR_GL_ALPHA;
2143 *internalFormat = GR_GL_ALPHA;
2144 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002145 break;
2146 default:
2147 return false;
2148 }
2149 return true;
2150}
2151
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002152void GrGpuGL::setTextureUnit(int unit) {
2153 GrAssert(unit >= 0 && unit < kNumStages);
2154 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002155 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002156 fActiveTextureUnitIdx = unit;
2157 }
2158}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002159
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002160void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002161 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002162 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002163 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2164 }
2165}
2166
reed@google.comac10a2d2010-12-22 21:39:39 +00002167/* On ES the internalFormat and format must match for TexImage and we use
2168 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2169 decide the internalFormat. However, on ES internalFormat for
2170 RenderBufferStorage* has to be a specific format (not a base format like
2171 GL_RGBA).
2172 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002173bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002174 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002175 case kRGBA_8888_GrPixelConfig:
2176 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002177 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002178 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002179 return true;
2180 } else {
2181 return false;
2182 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002183 case kRGB_565_GrPixelConfig:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002184 // ES2 supports 565. ES1 supports it
2185 // with FBO extension desktop GL has
2186 // no such internal format
2187 GrAssert(kDesktop_GrGLBinding != this->glBinding());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002188 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002189 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002190 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002191 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 return true;
2193 default:
2194 return false;
2195 }
2196}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002197
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002198void GrGpuGL::resetDirtyFlags() {
2199 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2200}
2201
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002202void GrGpuGL::setBuffers(bool indexed,
2203 int* extraVertexOffset,
2204 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002205
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002206 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002207
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002208 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2209
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002210 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002211 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002212 case kBuffer_GeometrySrcType:
2213 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002214 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002215 break;
2216 case kArray_GeometrySrcType:
2217 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002218 this->finalizeReservedVertices();
2219 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2220 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002221 break;
2222 default:
2223 vbuf = NULL; // suppress warning
2224 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002225 }
2226
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002227 GrAssert(NULL != vbuf);
2228 GrAssert(!vbuf->isLocked());
2229 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002230 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002231 fHWGeometryState.fArrayPtrsDirty = true;
2232 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002233 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002234
2235 if (indexed) {
2236 GrAssert(NULL != extraIndexOffset);
2237
2238 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002239 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002240 case kBuffer_GeometrySrcType:
2241 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002242 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002243 break;
2244 case kArray_GeometrySrcType:
2245 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002246 this->finalizeReservedIndices();
2247 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2248 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002249 break;
2250 default:
2251 ibuf = NULL; // suppress warning
2252 GrCrash("Unknown geometry src type!");
2253 }
2254
2255 GrAssert(NULL != ibuf);
2256 GrAssert(!ibuf->isLocked());
2257 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002258 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002259 fHWGeometryState.fIndexBuffer = ibuf;
2260 }
2261 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002262}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002263
2264int GrGpuGL::getMaxEdges() const {
2265 // FIXME: This is a pessimistic estimate based on how many other things
2266 // want to add uniforms. This should be centralized somewhere.
2267 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2268}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002269