blob: cd9de9181838453d36aed37dac64747948acfd87 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
twiz@google.com0f31ca72011-03-18 17:38:11 +000027static const GrGLenum gXfermodeCoeff2Blend[] = {
28 GR_GL_ZERO,
29 GR_GL_ONE,
30 GR_GL_SRC_COLOR,
31 GR_GL_ONE_MINUS_SRC_COLOR,
32 GR_GL_DST_COLOR,
33 GR_GL_ONE_MINUS_DST_COLOR,
34 GR_GL_SRC_ALPHA,
35 GR_GL_ONE_MINUS_SRC_ALPHA,
36 GR_GL_DST_ALPHA,
37 GR_GL_ONE_MINUS_DST_ALPHA,
38 GR_GL_CONSTANT_COLOR,
39 GR_GL_ONE_MINUS_CONSTANT_COLOR,
40 GR_GL_CONSTANT_ALPHA,
41 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
43 // extended blend coeffs
44 GR_GL_SRC1_COLOR,
45 GR_GL_ONE_MINUS_SRC1_COLOR,
46 GR_GL_SRC1_ALPHA,
47 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000048};
49
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 static const bool gCoeffReferencesBlendConst[] = {
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 false,
62 true,
63 true,
64 true,
65 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000066
67 // extended blend coeffs
68 false,
69 false,
70 false,
71 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000072 };
73 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
75
76 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
77 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
78 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
79 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
80 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
81 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
82 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
83 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
84 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
85 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
86 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
87 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
88 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
89 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
90
91 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
92 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
93 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
94 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
95
96 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
97 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000098}
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
103 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104 GrMatrix* matrix) {
105 GrAssert(NULL != texture);
106 GrAssert(NULL != matrix);
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
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000260GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000261
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000263
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;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000271 case kES2_GrGLBinding:
272 GrAssert(gl->supportsES2());
273 break;
274 default:
275 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000276 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000277
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000278 GrGLClearErr(fGL);
279
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000280 const GrGLubyte* ext;
281 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000282 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000283 const GrGLubyte* vendor;
284 const GrGLubyte* renderer;
285 const GrGLubyte* version;
286 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
287 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
288 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000289 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
290 this);
291 GrPrintf("------ VENDOR %s\n", vendor);
292 GrPrintf("------ RENDERER %s\n", renderer);
293 GrPrintf("------ VERSION %s\n", version);
294 GrPrintf("------ EXTENSIONS\n %s \n", ext);
295 }
296
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000297 fGLVersion = GrGLGetVersion(gl);
298 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000299 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000300
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000301 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000302
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000303 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000304
bsalomon@google.comfe676522011-06-17 18:12:21 +0000305 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000306}
307
308GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000309 // This must be called by before the GrDrawTarget destructor
310 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000311 // This subclass must do this before the base class destructor runs
312 // since we will unref the GrGLInterface.
313 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000314 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000315}
316
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000317///////////////////////////////////////////////////////////////////////////////
318
319static const GrGLuint kUnknownBitCount = ~0;
320
321void GrGpuGL::initCaps() {
322 GrGLint maxTextureUnits;
323 // check FS and fixed-function texture unit limits
324 // we only use textures in the fragment stage currently.
325 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000326 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
327 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000328 if (kES2_GrGLBinding != this->glBinding()) {
329 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000330 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000331 }
332 if (kES2_GrGLBinding == this->glBinding()) {
333 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
334 &fGLCaps.fMaxFragmentUniformVectors);
335 } else if (kDesktop_GrGLBinding != this->glBinding()) {
336 GrGLint max;
337 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
338 fGLCaps.fMaxFragmentUniformVectors = max / 4;
339 } else {
340 fGLCaps.fMaxFragmentUniformVectors = 16;
341 }
342
343 GrGLint numFormats;
344 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
345 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
346 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
347 for (int i = 0; i < numFormats; ++i) {
348 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
349 fCaps.f8BitPaletteSupport = true;
350 break;
351 }
352 }
353
354 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000355 // we could also look for GL_ATI_separate_stencil extension or
356 // GL_EXT_stencil_two_side but they use different function signatures
357 // than GL2.0+ (and than each other).
358 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
359 // supported on GL 1.4 and higher or by extension
360 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
361 this->hasExtension("GL_EXT_stencil_wrap");
362 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000363 // ES 2 has two sided stencil and stencil wrap
364 fCaps.fTwoSidedStencilSupport = true;
365 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000366 }
367
368 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000369 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000370 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000371 fGLCaps.fRGBA8RenderbufferSupport =
372 this->hasExtension("GL_OES_rgb8_rgba8") ||
373 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000374 }
375
376
bsalomon@google.comc4364992011-11-07 15:54:49 +0000377 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000378 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
379 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000380 } else {
381 bool hasBGRAExt = false;
382 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000383 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000384 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000385 fGLCaps.fBGRAFormatSupport = true;
386 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000387 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000388 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000389 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000390 }
391
392 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000393 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000394 this->hasExtension("GL_ARB_texture_swizzle");
395 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000396 fGLCaps.fTextureSwizzleSupport = false;
397 }
398
399 if (kDesktop_GrGLBinding == this->glBinding()) {
400 fGLCaps.fUnpackRowLengthSupport = true;
401 fGLCaps.fPackRowLengthSupport = true;
402 } else {
403 fGLCaps.fUnpackRowLengthSupport = this->hasExtension("GL_EXT_unpack_subimage");
404 // no extension for pack row length
405 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000406 }
407
408 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000409 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
410 // extension includes glMapBuffer.
411 } else {
412 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
413 }
414
415 if (kDesktop_GrGLBinding == this->glBinding()) {
416 if (fGLVersion >= GR_GL_VER(2,0) ||
417 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
418 fCaps.fNPOTTextureTileSupport = true;
419 fCaps.fNPOTTextureSupport = true;
420 } else {
421 fCaps.fNPOTTextureTileSupport = false;
422 fCaps.fNPOTTextureSupport = false;
423 }
424 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000425 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
426 fCaps.fNPOTTextureSupport = true;
427 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000428 }
429
430 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
431
432 ////////////////////////////////////////////////////////////////////////////
433 // Experiments to determine limitations that can't be queried.
434 // TODO: Make these a preprocess that generate some compile time constants.
435 // TODO: probe once at startup, rather than once per context creation.
436
437 int expectNPOTTargets = fGL->fNPOTRenderTargetSupport;
438 if (expectNPOTTargets == kProbe_GrGLCapability) {
439 fCaps.fNPOTRenderTargetSupport =
440 probe_for_npot_render_target_support(fGL, fCaps.fNPOTTextureSupport);
441 } else {
442 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000443 fCaps.fNPOTRenderTargetSupport = (0 != expectNPOTTargets);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000444 }
445
446 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
447 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
448 // Our render targets are always created with textures as the color
449 // attachment, hence this min:
450 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
451
452 fCaps.fMinRenderTargetHeight = fGL->fMinRenderTargetHeight;
453 if (fCaps.fMinRenderTargetHeight == kProbe_GrGLCapability) {
454 fCaps.fMinRenderTargetHeight =
455 probe_for_min_render_target_height(fGL, fCaps.fNPOTRenderTargetSupport,
456 fCaps.fMaxRenderTargetSize);
457 }
458
459 fCaps.fMinRenderTargetWidth = fGL->fMinRenderTargetWidth;
460 if (fCaps.fMinRenderTargetWidth == kProbe_GrGLCapability) {
461 fCaps.fMinRenderTargetWidth =
462 probe_for_min_render_target_width(fGL, fCaps.fNPOTRenderTargetSupport,
463 fCaps.fMaxRenderTargetSize);
464 }
465
466 this->initFSAASupport();
467 this->initStencilFormats();
468}
469
470void GrGpuGL::initFSAASupport() {
471 // TODO: Get rid of GrAALevel and use # samples directly.
472 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
473 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
474 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
475 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
476 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
477
478 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
479 if (kDesktop_GrGLBinding != this->glBinding()) {
480 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
481 // chrome's extension is equivalent to the EXT msaa
482 // and fbo_blit extensions.
483 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
484 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
485 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
486 }
487 } else {
488 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
489 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
490 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
491 this->hasExtension("GL_EXT_framebuffer_blit")) {
492 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
493 }
494 }
495
496 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
497 GrGLint maxSamples;
498 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
499 if (maxSamples > 1 ) {
500 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
501 fGLCaps.fAASamples[kLow_GrAALevel] =
502 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
503 fGLCaps.fAASamples[kMed_GrAALevel] =
504 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
505 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
506 }
507 }
508 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
509}
510
511void GrGpuGL::initStencilFormats() {
512
513 // Build up list of legal stencil formats (though perhaps not supported on
514 // the particular gpu/driver) from most preferred to least.
515
516 // these consts are in order of most preferred to least preferred
517 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
518 static const GrGLStencilBuffer::Format
519 // internal Format stencil bits total bits packed?
520 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
521 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
522 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
523 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
524 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
525 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
526
527 if (kDesktop_GrGLBinding == this->glBinding()) {
528 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
529 this->hasExtension("GL_EXT_packed_depth_stencil") ||
530 this->hasExtension("GL_ARB_framebuffer_object");
531
532 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
533 // require FBO support we can expect these are legal formats and don't
534 // check. These also all support the unsized GL_STENCIL_INDEX.
535 fGLCaps.fStencilFormats.push_back() = gS8;
536 fGLCaps.fStencilFormats.push_back() = gS16;
537 if (supportsPackedDS) {
538 fGLCaps.fStencilFormats.push_back() = gD24S8;
539 }
540 fGLCaps.fStencilFormats.push_back() = gS4;
541 if (supportsPackedDS) {
542 fGLCaps.fStencilFormats.push_back() = gDS;
543 }
544 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000545 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
546 // for other formats.
547 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000548
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000549 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000550 //fStencilFormats.push_back() = gS16;
551 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
552 fGLCaps.fStencilFormats.push_back() = gD24S8;
553 }
554 if (this->hasExtension("GL_OES_stencil4")) {
555 fGLCaps.fStencilFormats.push_back() = gS4;
556 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000557 }
558}
559
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000560GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) {
561 if (GR_GL_RGBA_8888_READBACK_SLOW && GrPixelConfigIsRGBA8888(config)) {
562 return GrPixelConfigSwapRAndB(config);
563 } else {
564 return config;
565 }
566}
567
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000568void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000569 if (gPrintStartupSpew && !fPrintedCaps) {
570 fPrintedCaps = true;
571 this->getCaps().print();
572 fGLCaps.print();
573 }
574
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000575 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000576 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000578
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000579 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000580 GL_CALL(Disable(GR_GL_DEPTH_TEST));
581 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000582
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000583 GL_CALL(Disable(GR_GL_CULL_FACE));
584 GL_CALL(FrontFace(GR_GL_CCW));
tomhudson@google.com93813632011-10-27 20:21:16 +0000585 fHWDrawState.fDrawFace = GrDrawState::kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000586
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000587 GL_CALL(Disable(GR_GL_DITHER));
588 if (kDesktop_GrGLBinding == this->glBinding()) {
589 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
590 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
591 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000592 fHWAAState.fMSAAEnabled = false;
593 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000594 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000595
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000596 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000597 fHWDrawState.fFlagBits = 0;
598
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000600 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000601
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000602 // invalid
603 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000604
reed@google.comac10a2d2010-12-22 21:39:39 +0000605 // illegal values
tomhudson@google.com62b09682011-11-09 16:39:17 +0000606 //fHWDrawState.fSrcBlend = (GrBlendCoeff)(uint8_t)-1;
607 fHWDrawState.fSrcBlend = (GrBlendCoeff)0xFF;
608 fHWDrawState.fDstBlend = (GrBlendCoeff)(uint8_t)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000609
610 fHWDrawState.fBlendConstant = 0x00000000;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000611 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000612
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000614
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000615 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000616
tomhudson@google.com93813632011-10-27 20:21:16 +0000617 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000618 fHWDrawState.fTextures[s] = NULL;
619 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
620 -GR_ScalarMax,
621 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000622 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000623 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000624 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000625
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000626 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000627 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000628 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000629 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000630
bsalomon@google.comd302f142011-03-03 13:54:13 +0000631 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000632 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000633 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000634
635 fHWGeometryState.fIndexBuffer = NULL;
636 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000637
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000638 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000639
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000640 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000641 fHWDrawState.fRenderTarget = NULL;
642}
643
bsalomon@google.come269f212011-11-07 13:29:52 +0000644GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
645 GrGLenum internalFormat; // we don't need this value
646 GrGLTexture::Desc glTexDesc;
647 if (!this->canBeTexture(desc.fConfig, &internalFormat,
648 &glTexDesc.fUploadFormat, &glTexDesc.fUploadType)) {
649 return NULL;
650 }
651
652 glTexDesc.fContentWidth = glTexDesc.fAllocWidth = desc.fWidth;
653 glTexDesc.fContentHeight = glTexDesc.fAllocHeight = desc.fHeight;
654 glTexDesc.fConfig = desc.fConfig;
655 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
656 glTexDesc.fOwnsID = false;
657 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
658
659 GrGLTexture* texture = NULL;
660 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
661 GrGLRenderTarget::Desc glRTDesc;
662 glRTDesc.fRTFBOID = 0;
663 glRTDesc.fTexFBOID = 0;
664 glRTDesc.fMSColorRenderbufferID = 0;
665 glRTDesc.fOwnIDs = true;
666 glRTDesc.fConfig = desc.fConfig;
667 glRTDesc.fSampleCnt = desc.fSampleCnt;
668 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
669 glTexDesc.fAllocHeight,
670 glTexDesc.fTextureID,
671 &glRTDesc)) {
672 return NULL;
673 }
674 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
675 } else {
676 texture = new GrGLTexture(this, glTexDesc);
677 }
678 if (NULL == texture) {
679 return NULL;
680 }
681
682 this->setSpareTextureUnit();
683 return texture;
684}
685
686GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
687 GrGLRenderTarget::Desc glDesc;
688 glDesc.fConfig = desc.fConfig;
689 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
690 glDesc.fMSColorRenderbufferID = 0;
691 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
692 glDesc.fSampleCnt = desc.fSampleCnt;
693 glDesc.fOwnIDs = false;
694 GrGLIRect viewport;
695 viewport.fLeft = 0;
696 viewport.fBottom = 0;
697 viewport.fWidth = desc.fWidth;
698 viewport.fHeight = desc.fHeight;
699
700 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
701 if (desc.fStencilBits) {
702 GrGLStencilBuffer::Format format;
703 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
704 format.fPacked = false;
705 format.fStencilBits = desc.fStencilBits;
706 format.fTotalBits = desc.fStencilBits;
707 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
708 0,
709 desc.fWidth,
710 desc.fHeight,
711 desc.fSampleCnt,
712 format);
713 tgt->setStencilBuffer(sb);
714 sb->unref();
715 }
716 return tgt;
717}
718
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000719GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
720
721 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
722 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
723 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
724 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
725
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000726 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000727 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000728
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000729 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000730 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000731 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000732 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000733 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000734 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000735 } else {
736 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000737 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000738 }
739 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000740 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000741 }
742 // we don't know what the RB ids are without glGets and we don't care
743 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000744 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000745 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000746 if (desc.fStencilBits) {
747 GrGLStencilBuffer::Format format;
748 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
749 format.fPacked = false;
750 format.fStencilBits = desc.fStencilBits;
751 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000752 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
753 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000754 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000755 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000756 }
757
758 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000759 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000760 GrGLenum dontCare;
761 if (!canBeTexture(desc.fConfig, &dontCare,
762 &texDesc.fUploadFormat,
763 &texDesc.fUploadType)) {
764 return NULL;
765 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000766 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
767 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
768
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000769 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000770 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000771 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000772 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000773
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000774 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000775 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000776 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000777 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000778 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000779 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000780 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000781 } else {
782 GrGLIRect viewport;
783 viewport.fLeft = 0;
784 viewport.fBottom = 0;
785 viewport.fWidth = desc.fWidth;
786 viewport.fHeight = desc.fHeight;
787
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000788 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000789 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000790 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000791 }
792}
793
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000794
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000795////////////////////////////////////////////////////////////////////////////////
796
797void GrGpuGL::allocateAndUploadTexData(const GrGLTexture::Desc& desc,
798 GrGLenum internalFormat,
799 const void* data,
800 size_t rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000801 // we assume the texture is bound
802
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000803 size_t bpp = GrBytesPerPixel(desc.fConfig);
804 size_t trimRowBytes = desc.fContentWidth * bpp;
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000805
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000806 if (!rowBytes) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000807 rowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000808 }
809
810 // in case we need a temporary, trimmed copy of the src pixels
811 SkAutoSMalloc<128 * 128> tempStorage;
812
813 /*
814 * check whether to allocate a temporary buffer for flipping y or
815 * because our data has extra bytes past each row. If so, we need
816 * to trim those off here, since GL ES doesn't let us specify
817 * GL_UNPACK_ROW_LENGTH.
818 */
819 bool flipY = GrGLTexture::kBottomUp_Orientation == desc.fOrientation;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000820 if (this->glCaps().fUnpackRowLengthSupport && !flipY) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000821 if (data && rowBytes != trimRowBytes) {
822 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
823 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000824 }
825 } else {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000826 if (data && (trimRowBytes != rowBytes || flipY)) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000827 // copy the data into our new storage, skipping the trailing bytes
828 size_t trimSize = desc.fContentHeight * trimRowBytes;
829 const char* src = (const char*)data;
830 if (flipY) {
831 src += (desc.fContentHeight - 1) * rowBytes;
832 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000833 char* dst = (char*)tempStorage.reset(trimSize);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000834 for (int y = 0; y < desc.fContentHeight; y++) {
835 memcpy(dst, src, trimRowBytes);
836 if (flipY) {
837 src -= rowBytes;
838 } else {
839 src += rowBytes;
840 }
841 dst += trimRowBytes;
842 }
843 // now point data to our trimmed version
844 data = tempStorage.get();
845 rowBytes = trimRowBytes;
846 }
847 }
848
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000849 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000850 if (kIndex_8_GrPixelConfig == desc.fConfig &&
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000851 this->getCaps().f8BitPaletteSupport) {
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000852 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
853 GrAssert(desc.fContentWidth == desc.fAllocWidth);
854 GrAssert(desc.fContentHeight == desc.fAllocHeight);
855 GrGLsizei imageSize = desc.fAllocWidth * desc.fAllocHeight +
856 kGrColorTableSize;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000857 GL_CALL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, desc.fUploadFormat,
858 desc.fAllocWidth, desc.fAllocHeight,
859 0, imageSize, data));
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000860 if (this->glCaps().fUnpackRowLengthSupport) {
861 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
862 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000863 } else {
864 if (NULL != data && (desc.fAllocWidth != desc.fContentWidth ||
865 desc.fAllocHeight != desc.fContentHeight)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000866 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
867 desc.fAllocWidth, desc.fAllocHeight,
868 0, desc.fUploadFormat, desc.fUploadType, NULL));
869 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fContentWidth,
870 desc.fContentHeight, desc.fUploadFormat,
871 desc.fUploadType, data));
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000872 if (this->glCaps().fUnpackRowLengthSupport) {
873 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
874 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000875
876 int extraW = desc.fAllocWidth - desc.fContentWidth;
877 int extraH = desc.fAllocHeight - desc.fContentHeight;
878 int maxTexels = extraW * extraH;
879 maxTexels = GrMax(extraW * desc.fContentHeight, maxTexels);
880 maxTexels = GrMax(desc.fContentWidth * extraH, maxTexels);
881
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000882 SkAutoSMalloc<128*128> texels(bpp * maxTexels);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000883
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000884 if (extraH) {
885 uint8_t* lastRowStart = (uint8_t*) data +
886 (desc.fContentHeight - 1) * rowBytes;
887 uint8_t* extraRowStart = (uint8_t*)texels.get();
888
889 for (int i = 0; i < extraH; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000890 memcpy(extraRowStart, lastRowStart, trimRowBytes);
891 extraRowStart += trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000892 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000893 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0,
894 desc.fContentHeight, desc.fContentWidth,
895 extraH, desc.fUploadFormat,
896 desc.fUploadType, texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000897 }
898 if (extraW) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000899 uint8_t* edgeTexel = (uint8_t*)data + trimRowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000900 uint8_t* extraTexel = (uint8_t*)texels.get();
901 for (int j = 0; j < desc.fContentHeight; ++j) {
902 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000903 memcpy(extraTexel, edgeTexel, bpp);
904 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000905 }
906 edgeTexel += rowBytes;
907 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000908 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
909 0, extraW, desc.fContentHeight,
910 desc.fUploadFormat, desc.fUploadType,
911 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000912 }
913 if (extraW && extraH) {
914 uint8_t* cornerTexel = (uint8_t*)data +
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000915 desc.fContentHeight * rowBytes - bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000916 uint8_t* extraTexel = (uint8_t*)texels.get();
917 for (int i = 0; i < extraW*extraH; ++i) {
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000918 memcpy(extraTexel, cornerTexel, bpp);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000919 extraTexel += bpp;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000920 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000921 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fContentWidth,
922 desc.fContentHeight, extraW, extraH,
923 desc.fUploadFormat, desc.fUploadType,
924 texels.get()));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000925 }
926
927 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000928 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
929 desc.fAllocWidth, desc.fAllocHeight, 0,
930 desc.fUploadFormat, desc.fUploadType, data));
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000931 if (this->glCaps().fUnpackRowLengthSupport) {
932 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
933 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000934 }
935 }
936}
937
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000938bool GrGpuGL::createRenderTargetObjects(int width, int height,
939 GrGLuint texID,
940 GrGLRenderTarget::Desc* desc) {
941 desc->fMSColorRenderbufferID = 0;
942 desc->fRTFBOID = 0;
943 desc->fTexFBOID = 0;
944 desc->fOwnIDs = true;
945
946 GrGLenum status;
947 GrGLint err;
948
bsalomon@google.comab15d612011-08-09 12:57:56 +0000949 GrGLenum msColorFormat = 0; // suppress warning
950
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000951 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000952 if (!desc->fTexFBOID) {
953 goto FAILED;
954 }
955
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000956
957 // If we are using multisampling we will create two FBOS. We render
958 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000959 if (desc->fSampleCnt > 0) {
960 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
961 goto FAILED;
962 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000963 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
964 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000965 if (!desc->fRTFBOID ||
966 !desc->fMSColorRenderbufferID ||
967 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
968 goto FAILED;
969 }
970 } else {
971 desc->fRTFBOID = desc->fTexFBOID;
972 }
973
974 if (desc->fRTFBOID != desc->fTexFBOID) {
975 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000976 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000977 desc->fMSColorRenderbufferID));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000978 GR_GL_CALL_NOERRCHECK(this->glInterface(),
979 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
980 desc->fSampleCnt,
981 msColorFormat,
982 width, height));
983 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000984 if (err != GR_GL_NO_ERROR) {
985 goto FAILED;
986 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000987 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
988 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000989 GR_GL_COLOR_ATTACHMENT0,
990 GR_GL_RENDERBUFFER,
991 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000992 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000993 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
994 goto FAILED;
995 }
996 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000997 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000998
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000999 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1000 GR_GL_COLOR_ATTACHMENT0,
1001 GR_GL_TEXTURE_2D,
1002 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001003 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001004 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1005 goto FAILED;
1006 }
1007
1008 return true;
1009
1010FAILED:
1011 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001012 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001013 }
1014 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001015 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001016 }
1017 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001018 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001019 }
1020 return false;
1021}
1022
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001023// good to set a break-point here to know when createTexture fails
1024static GrTexture* return_null_texture() {
1025// GrAssert(!"null texture");
1026 return NULL;
1027}
1028
1029#if GR_DEBUG
1030static size_t as_size_t(int x) {
1031 return x;
1032}
1033#endif
1034
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001035GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001036 const void* srcData,
1037 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001038
1039#if GR_COLLECT_STATS
1040 ++fStats.fTextureCreateCnt;
1041#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +00001042
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001043 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001044 GrGLRenderTarget::Desc glRTDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001045 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +00001046
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001047 glTexDesc.fContentWidth = desc.fWidth;
1048 glTexDesc.fContentHeight = desc.fHeight;
1049 glTexDesc.fAllocWidth = desc.fWidth;
1050 glTexDesc.fAllocHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001051 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001052 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001053
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001054 glRTDesc.fMSColorRenderbufferID = 0;
1055 glRTDesc.fRTFBOID = 0;
1056 glRTDesc.fTexFBOID = 0;
1057 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001058 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001059
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001060 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001061 if (!canBeTexture(desc.fConfig,
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001063 &glTexDesc.fUploadFormat,
1064 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 return return_null_texture();
1066 }
1067
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001068 const Caps& caps = this->getCaps();
1069
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001070 // We keep GrRenderTargets in GL's normal orientation so that they
1071 // can be drawn to by the outside world without the client having
1072 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001073 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001075
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001076 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1077 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1078 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1079 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 GrPrintf("AA RT requested but not supported on this platform.");
1081 }
1082
reed@google.comac10a2d2010-12-22 21:39:39 +00001083 if (renderTarget) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001084 if (!caps.fNPOTRenderTargetSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001085 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1086 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001087 }
1088
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001089 glTexDesc.fAllocWidth = GrMax(caps.fMinRenderTargetWidth,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001090 glTexDesc.fAllocWidth);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001091 glTexDesc.fAllocHeight = GrMax(caps.fMinRenderTargetHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001092 glTexDesc.fAllocHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001093 if (glTexDesc.fAllocWidth > caps.fMaxRenderTargetSize ||
1094 glTexDesc.fAllocHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001095 return return_null_texture();
1096 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001097 } else if (!caps.fNPOTTextureSupport) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001098 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1099 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001100 if (glTexDesc.fAllocWidth > caps.fMaxTextureSize ||
1101 glTexDesc.fAllocHeight > caps.fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001102 return return_null_texture();
1103 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001104 }
1105
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001106 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001107 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001108 return return_null_texture();
1109 }
1110
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001111 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001112 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001113
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001114 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1115 // drivers have a bug where an FBO won't be complete if it includes a
1116 // texture that is not mipmap complete (considering the filter in use).
1117 GrGLTexture::TexParams initialTexParams;
1118 // we only set a subset here so invalidate first
1119 initialTexParams.invalidate();
1120 initialTexParams.fFilter = GR_GL_NEAREST;
1121 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1122 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001123 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1124 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001125 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001126 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1127 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001128 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001129 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1130 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001131 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001132 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1133 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001134 initialTexParams.fWrapT));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001135 this->allocateAndUploadTexData(glTexDesc, internalFormat,srcData, rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001136
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 if (renderTarget) {
1139#if GR_COLLECT_STATS
1140 ++fStats.fRenderTargetCreateCnt;
1141#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 if (!this->createRenderTargetObjects(glTexDesc.fAllocWidth,
1143 glTexDesc.fAllocHeight,
1144 glTexDesc.fTextureID,
1145 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 return return_null_texture();
1148 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001149 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001151 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001153 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001154#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001155 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1156 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001157#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 return tex;
1159}
1160
1161namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001162void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1163 GrGLuint rb,
1164 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 // we shouldn't ever know one size and not the other
1166 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1167 (kUnknownBitCount == format->fTotalBits));
1168 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1171 (GrGLint*)&format->fStencilBits);
1172 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001173 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1175 (GrGLint*)&format->fTotalBits);
1176 format->fTotalBits += format->fStencilBits;
1177 } else {
1178 format->fTotalBits = format->fStencilBits;
1179 }
1180 }
1181}
1182}
1183
1184bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1185 int width, int height) {
1186
1187 // All internally created RTs are also textures. We don't create
1188 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1189 GrAssert(rt->asTexture());
bsalomon@google.com0168afc2011-08-08 13:21:05 +00001190 GrAssert(width >= rt->allocatedWidth());
1191 GrAssert(height >= rt->allocatedHeight());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192
1193 int samples = rt->numSamples();
1194 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001195 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 if (!sbID) {
1197 return false;
1198 }
1199
1200 GrGLStencilBuffer* sb = NULL;
1201
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001202 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 // we start with the last stencil format that succeeded in hopes
1206 // that we won't go through this loop more than once after the
1207 // first (painful) stencil creation.
1208 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001209 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001210 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 // version on a GL that doesn't have an MSAA extension.
1212 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001213 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1214 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 GR_GL_RENDERBUFFER,
1216 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001217 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 width,
1219 height));
1220 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1222 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001223 sFmt.fInternalFormat,
1224 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 }
1226
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228 if (err == GR_GL_NO_ERROR) {
1229 // After sized formats we attempt an unsized format and take whatever
1230 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001231 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001233 sb = new GrGLStencilBuffer(this, sbID, width, height,
1234 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1236 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001237 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 return true;
1240 }
1241 sb->abandon(); // otherwise we lose sbID
1242 sb->unref();
1243 }
1244 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001245 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001246 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247}
1248
1249bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1250 GrRenderTarget* rt) {
1251 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1252
1253 GrGLuint fbo = glrt->renderFBOID();
1254
1255 if (NULL == sb) {
1256 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001257 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001258 GR_GL_STENCIL_ATTACHMENT,
1259 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001260 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001261 GR_GL_DEPTH_ATTACHMENT,
1262 GR_GL_RENDERBUFFER, 0));
1263#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001264 GrGLenum status;
1265 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001266 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1267#endif
1268 }
1269 return true;
1270 } else {
1271 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1272 GrGLuint rb = glsb->renderbufferID();
1273
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001275 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1276 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001277 GR_GL_STENCIL_ATTACHMENT,
1278 GR_GL_RENDERBUFFER, rb));
1279 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001280 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001281 GR_GL_DEPTH_ATTACHMENT,
1282 GR_GL_RENDERBUFFER, rb));
1283 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001284 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001285 GR_GL_DEPTH_ATTACHMENT,
1286 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001288
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001289 GrGLenum status;
1290 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001291 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001292 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001293 GR_GL_STENCIL_ATTACHMENT,
1294 GR_GL_RENDERBUFFER, 0));
1295 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001296 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001297 GR_GL_DEPTH_ATTACHMENT,
1298 GR_GL_RENDERBUFFER, 0));
1299 }
1300 return false;
1301 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001302 return true;
1303 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001305}
1306
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001307////////////////////////////////////////////////////////////////////////////////
1308
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001309GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001310 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001311 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001313 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001314 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001315 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001317 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1318 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1319 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1320 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1321 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 // deleting bound buffer does implicit bind to 0
1323 fHWGeometryState.fVertexBuffer = NULL;
1324 return NULL;
1325 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001326 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 size, dynamic);
1328 fHWGeometryState.fVertexBuffer = vertexBuffer;
1329 return vertexBuffer;
1330 }
1331 return NULL;
1332}
1333
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001334GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001335 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001336 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001338 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1339 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001341 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1342 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1343 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1344 if (this->glInterface()->fGetError() != GR_GL_NO_ERROR) {
1345 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 // deleting bound buffer does implicit bind to 0
1347 fHWGeometryState.fIndexBuffer = NULL;
1348 return NULL;
1349 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001350 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 size, dynamic);
1352 fHWGeometryState.fIndexBuffer = indexBuffer;
1353 return indexBuffer;
1354 }
1355 return NULL;
1356}
1357
reed@google.comac10a2d2010-12-22 21:39:39 +00001358void GrGpuGL::flushScissor(const GrIRect* rect) {
1359 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001360 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001361 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001362
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001363 GrGLIRect scissor;
1364 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001365 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001366 rect->width(), rect->height());
1367 if (scissor.contains(vp)) {
1368 rect = NULL;
1369 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001370 }
1371
1372 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001373 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001374 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001375 fHWBounds.fScissorRect = scissor;
1376 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001378 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 fHWBounds.fScissorEnabled = true;
1380 }
1381 } else {
1382 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001383 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001384 fHWBounds.fScissorEnabled = false;
1385 }
1386 }
1387}
1388
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001389void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001390 // parent class should never let us get here with no RT
1391 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1392
bsalomon@google.com74b98712011-11-11 19:46:16 +00001393 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001394 if (NULL != rect) {
1395 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001396 clippedRect = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001397 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1398 fCurrDrawState.fRenderTarget->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001399 if (clippedRect.intersect(rtRect)) {
1400 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001401 } else {
1402 return;
1403 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001404 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001405 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001406 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001407
1408 GrGLfloat r, g, b, a;
1409 static const GrGLfloat scale255 = 1.f / 255.f;
1410 a = GrColorUnpackA(color) * scale255;
1411 GrGLfloat scaleRGB = scale255;
1412 if (GrPixelConfigIsUnpremultiplied(fCurrDrawState.fRenderTarget->config())) {
1413 scaleRGB *= a;
1414 }
1415 r = GrColorUnpackR(color) * scaleRGB;
1416 g = GrColorUnpackG(color) * scaleRGB;
1417 b = GrColorUnpackB(color) * scaleRGB;
1418
1419 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001420 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001421 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001422 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001423}
1424
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001425void GrGpuGL::clearStencil() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001426 if (NULL == fCurrDrawState.fRenderTarget) {
1427 return;
1428 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001429
1430 this->flushRenderTarget(&GrIRect::EmptyIRect());
1431
reed@google.comac10a2d2010-12-22 21:39:39 +00001432 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001433 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001434 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001435 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001436 GL_CALL(StencilMask(0xffffffff));
1437 GL_CALL(ClearStencil(0));
1438 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001439 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001440}
1441
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001442void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001443 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001444
1445 // this should only be called internally when we know we have a
1446 // stencil buffer.
1447 GrAssert(NULL != fCurrDrawState.fRenderTarget->getStencilBuffer());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001448 GrGLint stencilBitCount =
1449 fCurrDrawState.fRenderTarget->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001450#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001451 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001452 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001453#else
1454 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001455 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001456 // turned into draws. Our contract on GrDrawTarget says that
1457 // changing the clip between stencil passes may or may not
1458 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001459 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001460#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001461 GrGLint value;
1462 if (insideClip) {
1463 value = (1 << (stencilBitCount - 1));
1464 } else {
1465 value = 0;
1466 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001467 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001468 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001469 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001470 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001471 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001472 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001473}
1474
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001475void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001476 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001477}
1478
bsalomon@google.comc4364992011-11-07 15:54:49 +00001479bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1480 int left, int top,
1481 int width, int height,
1482 GrPixelConfig config,
1483 size_t rowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001484 // if we have to do memcpy to handle non-trim rowBytes then we
1485 // get the flip for free. Otherwise it costs.
1486 return this->glCaps().fPackRowLengthSupport ||
1487 0 == rowBytes ||
1488 GrBytesPerPixel(config) * width == rowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001489}
1490
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001491bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001492 int left, int top,
1493 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001494 GrPixelConfig config,
1495 void* buffer,
1496 size_t rowBytes,
1497 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001498 GrGLenum internalFormat; // we don't use this for glReadPixels
1499 GrGLenum format;
1500 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001501 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1502 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001503 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001504
bsalomon@google.comc6980972011-11-02 19:57:21 +00001505 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001506 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1507 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1508 switch (tgt->getResolveType()) {
1509 case GrGLRenderTarget::kCantResolve_ResolveType:
1510 return false;
1511 case GrGLRenderTarget::kAutoResolves_ResolveType:
1512 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1513 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001514 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001515 break;
1516 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001517 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001518 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001519 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1520 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001521 break;
1522 default:
1523 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001524 }
1525
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001526 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001527
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001528 // the read rect is viewport-relative
1529 GrGLIRect readRect;
1530 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001531
1532 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1533 if (0 == rowBytes) {
1534 rowBytes = tightRowBytes;
1535 }
1536 size_t readDstRowBytes = tightRowBytes;
1537 void* readDst = buffer;
1538
1539 // determine if GL can read using the passed rowBytes or if we need
1540 // a scratch buffer.
1541 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1542 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001543 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001544 GrAssert(!(rowBytes % sizeof(GrColor)));
1545 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1546 readDstRowBytes = rowBytes;
1547 } else {
1548 scratch.reset(tightRowBytes * height);
1549 readDst = scratch.get();
1550 }
1551 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001552 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1553 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001554 format, type, readDst));
1555 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001556 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001557 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1558 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001559
1560 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001561 // API presents top-to-bottom. We must preserve the padding contents. Note
1562 // that the above readPixels did not overwrite the padding.
1563 if (readDst == buffer) {
1564 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001565 if (!invertY) {
1566 scratch.reset(tightRowBytes);
1567 void* tmpRow = scratch.get();
1568 // flip y in-place by rows
1569 const int halfY = height >> 1;
1570 char* top = reinterpret_cast<char*>(buffer);
1571 char* bottom = top + (height - 1) * rowBytes;
1572 for (int y = 0; y < halfY; y++) {
1573 memcpy(tmpRow, top, tightRowBytes);
1574 memcpy(top, bottom, tightRowBytes);
1575 memcpy(bottom, tmpRow, tightRowBytes);
1576 top += rowBytes;
1577 bottom -= rowBytes;
1578 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001579 }
1580 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001581 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001582 // copy from readDst to buffer while flipping y
1583 const int halfY = height >> 1;
1584 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001585 char* dst = reinterpret_cast<char*>(buffer);
1586 if (!invertY) {
1587 dst += (height-1) * rowBytes;
1588 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001589 for (int y = 0; y < height; y++) {
1590 memcpy(dst, src, tightRowBytes);
1591 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001592 if (invertY) {
1593 dst += rowBytes;
1594 } else {
1595 dst -= rowBytes;
1596 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001597 }
1598 }
1599 return true;
1600}
1601
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001602void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001603
1604 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1605
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001606 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001607 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001608 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 #if GR_COLLECT_STATS
1610 ++fStats.fRenderTargetChngCnt;
1611 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001613 GrGLenum status;
1614 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001615 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001616 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 }
1618 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001619 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001620 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001621 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001622 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001623 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001624 fHWBounds.fViewportRect = vp;
1625 }
1626 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001627 if (NULL == bound || !bound->isEmpty()) {
1628 rt->flagAsNeedingResolve(bound);
1629 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001630}
1631
twiz@google.com0f31ca72011-03-18 17:38:11 +00001632GrGLenum gPrimitiveType2GLMode[] = {
1633 GR_GL_TRIANGLES,
1634 GR_GL_TRIANGLE_STRIP,
1635 GR_GL_TRIANGLE_FAN,
1636 GR_GL_POINTS,
1637 GR_GL_LINES,
1638 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001639};
1640
bsalomon@google.comd302f142011-03-03 13:54:13 +00001641#define SWAP_PER_DRAW 0
1642
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001643#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001644 #if GR_MAC_BUILD
1645 #include <AGL/agl.h>
1646 #elif GR_WIN32_BUILD
1647 void SwapBuf() {
1648 DWORD procID = GetCurrentProcessId();
1649 HWND hwnd = GetTopWindow(GetDesktopWindow());
1650 while(hwnd) {
1651 DWORD wndProcID = 0;
1652 GetWindowThreadProcessId(hwnd, &wndProcID);
1653 if(wndProcID == procID) {
1654 SwapBuffers(GetDC(hwnd));
1655 }
1656 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1657 }
1658 }
1659 #endif
1660#endif
1661
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001662void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1663 uint32_t startVertex,
1664 uint32_t startIndex,
1665 uint32_t vertexCount,
1666 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001667 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1668
twiz@google.com0f31ca72011-03-18 17:38:11 +00001669 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001670
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001671 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1672 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1673
1674 // our setupGeometry better have adjusted this to zero since
1675 // DrawElements always draws from the begining of the arrays for idx 0.
1676 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001677
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001678 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1679 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001680#if SWAP_PER_DRAW
1681 glFlush();
1682 #if GR_MAC_BUILD
1683 aglSwapBuffers(aglGetCurrentContext());
1684 int set_a_break_pt_here = 9;
1685 aglSwapBuffers(aglGetCurrentContext());
1686 #elif GR_WIN32_BUILD
1687 SwapBuf();
1688 int set_a_break_pt_here = 9;
1689 SwapBuf();
1690 #endif
1691#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001692}
1693
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001694void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1695 uint32_t startVertex,
1696 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1698
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001699 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1700
1701 // our setupGeometry better have adjusted this to zero.
1702 // DrawElements doesn't take an offset so we always adjus the startVertex.
1703 GrAssert(0 == startVertex);
1704
1705 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1706 // account for startVertex in the DrawElements case. So we always
1707 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001708 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001709#if SWAP_PER_DRAW
1710 glFlush();
1711 #if GR_MAC_BUILD
1712 aglSwapBuffers(aglGetCurrentContext());
1713 int set_a_break_pt_here = 9;
1714 aglSwapBuffers(aglGetCurrentContext());
1715 #elif GR_WIN32_BUILD
1716 SwapBuf();
1717 int set_a_break_pt_here = 9;
1718 SwapBuf();
1719 #endif
1720#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001721}
1722
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001723void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001724
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001725 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001726 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001727 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001728 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1729 rt->renderFBOID()));
1730 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1731 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001732 #if GR_COLLECT_STATS
1733 ++fStats.fRenderTargetChngCnt;
1734 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001735 // make sure we go through flushRenderTarget() since we've modified
1736 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001737 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001738 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001739 const GrIRect dirtyRect = rt->getResolveRect();
1740 GrGLIRect r;
1741 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1742 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001743
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001744 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001745 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001746 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1747 GL_CALL(Scissor(r.fLeft, r.fBottom,
1748 r.fWidth, r.fHeight));
1749 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001750 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001751 fHWBounds.fScissorEnabled = true;
1752 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001753 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001754 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001755 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1756 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001757 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001758 int right = r.fLeft + r.fWidth;
1759 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001760 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1761 r.fLeft, r.fBottom, right, top,
1762 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001764 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 }
1766}
1767
twiz@google.com0f31ca72011-03-18 17:38:11 +00001768static const GrGLenum grToGLStencilFunc[] = {
1769 GR_GL_ALWAYS, // kAlways_StencilFunc
1770 GR_GL_NEVER, // kNever_StencilFunc
1771 GR_GL_GREATER, // kGreater_StencilFunc
1772 GR_GL_GEQUAL, // kGEqual_StencilFunc
1773 GR_GL_LESS, // kLess_StencilFunc
1774 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1775 GR_GL_EQUAL, // kEqual_StencilFunc,
1776 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001777};
1778GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1779GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1780GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1781GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1782GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1783GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1784GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1785GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1786GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1787
twiz@google.com0f31ca72011-03-18 17:38:11 +00001788static const GrGLenum grToGLStencilOp[] = {
1789 GR_GL_KEEP, // kKeep_StencilOp
1790 GR_GL_REPLACE, // kReplace_StencilOp
1791 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1792 GR_GL_INCR, // kIncClamp_StencilOp
1793 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1794 GR_GL_DECR, // kDecClamp_StencilOp
1795 GR_GL_ZERO, // kZero_StencilOp
1796 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001797};
1798GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1799GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1800GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1801GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1802GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1803GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1804GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1805GR_STATIC_ASSERT(6 == kZero_StencilOp);
1806GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1807
reed@google.comac10a2d2010-12-22 21:39:39 +00001808void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001809 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001810
1811 // use stencil for clipping if clipping is enabled and the clip
1812 // has been written into the stencil.
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +00001813 bool stencilClip = fClipInStencil &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001814 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001815 bool stencilChange = fHWStencilClip != stencilClip ||
1816 fHWDrawState.fStencilSettings != *settings ||
1817 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1818 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001819
1820 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001821
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1823 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001824
bsalomon@google.comd302f142011-03-03 13:54:13 +00001825 if (settings->isDisabled()) {
1826 if (stencilClip) {
1827 settings = &gClipStencilSettings;
1828 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001829 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830
1831 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001832 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001833 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001834 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001836 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001837 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1838 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1839 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1840 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1841 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1842 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1843 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1844 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1845 }
1846 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001847 int stencilBits = 0;
1848 GrStencilBuffer* stencilBuffer =
1849 fCurrDrawState.fRenderTarget->getStencilBuffer();
1850 if (NULL != stencilBuffer) {
1851 stencilBits = stencilBuffer->bits();
1852 }
1853 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001854 GrAssert(stencilBits ||
1855 (GrStencilSettings::gDisabled ==
1856 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001857 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1858 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859
1860 unsigned int frontRef = settings->fFrontFuncRef;
1861 unsigned int frontMask = settings->fFrontFuncMask;
1862 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001863 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864
1865 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1866
1867 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1868 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1869 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001870 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
1871 stencilClip, settings->fFrontFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001872
1873 ConvertStencilFuncAndMask(settings->fFrontFunc,
1874 stencilClip,
1875 clipStencilMask,
1876 userStencilMask,
1877 &frontRef,
1878 &frontMask);
1879 frontWriteMask &= userStencilMask;
1880 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001881 GrAssert((size_t)
1882 settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1883 GrAssert((size_t)
1884 settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
1885 GrAssert((size_t)
1886 settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1887 GrAssert((size_t)
1888 settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001889 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001890 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001891
1892 unsigned int backRef = settings->fBackFuncRef;
1893 unsigned int backMask = settings->fBackFuncMask;
1894 unsigned int backWriteMask = settings->fBackWriteMask;
1895
1896
1897 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1898 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1899 backFunc = grToGLStencilFunc[settings->fBackFunc];
1900 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001901 backFunc = grToGLStencilFunc[ConvertStencilFunc(
1902 stencilClip, settings->fBackFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001903 ConvertStencilFuncAndMask(settings->fBackFunc,
1904 stencilClip,
1905 clipStencilMask,
1906 userStencilMask,
1907 &backRef,
1908 &backMask);
1909 backWriteMask &= userStencilMask;
1910 }
1911
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001912 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1913 frontRef, frontMask));
1914 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1915 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1916 backRef, backMask));
1917 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1918 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1919 grToGLStencilOp[settings->fFrontFailOp],
1920 grToGLStencilOp[settings->fFrontPassOp],
1921 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001923 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1924 grToGLStencilOp[settings->fBackFailOp],
1925 grToGLStencilOp[settings->fBackPassOp],
1926 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001927 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001928 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1929 GL_CALL(StencilMask(frontWriteMask));
1930 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001931 grToGLStencilOp[settings->fFrontPassOp],
1932 grToGLStencilOp[settings->fFrontPassOp]));
1933 }
1934 }
1935 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001936 fHWStencilClip = stencilClip;
1937 }
1938}
1939
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001940void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001941 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001942 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1943 // smooth lines.
1944
1945 // we prefer smooth lines over multisampled lines
1946 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001947 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001948 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001949 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001950 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001951 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001952 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001953 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001954 fHWAAState.fSmoothLineEnabled = false;
1955 }
1956 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1957 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 fHWAAState.fMSAAEnabled = false;
1960 }
1961 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +00001962 SkToBool(kHWAntialias_StateBit & fCurrDrawState.fFlagBits) !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001963 fHWAAState.fMSAAEnabled) {
1964 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001965 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001966 fHWAAState.fMSAAEnabled = false;
1967 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001968 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001969 fHWAAState.fMSAAEnabled = true;
1970 }
1971 }
1972 }
1973}
1974
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001975void GrGpuGL::flushBlend(GrPrimitiveType type,
1976 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001977 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001978 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001979 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001980 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001981 fHWBlendDisabled = false;
1982 }
1983 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1984 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001985 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1986 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001987 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1988 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1989 }
1990 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001991 // any optimization to disable blending should
1992 // have already been applied and tweaked the coeffs
1993 // to (1, 0).
1994 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1995 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001996 if (fHWBlendDisabled != blendOff) {
1997 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001998 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001999 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002000 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002001 }
2002 fHWBlendDisabled = blendOff;
2003 }
2004 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002005 if (fHWDrawState.fSrcBlend != srcCoeff ||
2006 fHWDrawState.fDstBlend != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002007 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2008 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002009 fHWDrawState.fSrcBlend = srcCoeff;
2010 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002011 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002012 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2013 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00002014 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
2015
2016 float c[] = {
2017 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
2018 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
2019 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
2020 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
2021 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002022 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002023 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
2024 }
2025 }
2026 }
2027}
2028
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002029namespace {
2030
2031unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002032 switch (filter) {
2033 case GrSamplerState::kBilinear_Filter:
2034 case GrSamplerState::k4x4Downsample_Filter:
2035 return GR_GL_LINEAR;
2036 case GrSamplerState::kNearest_Filter:
2037 case GrSamplerState::kConvolution_Filter:
2038 return GR_GL_NEAREST;
2039 default:
2040 GrAssert(!"Unknown filter type");
2041 return GR_GL_LINEAR;
2042 }
2043}
2044
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002045const GrGLenum* get_swizzle(GrPixelConfig config,
2046 const GrSamplerState& sampler) {
2047 if (GrPixelConfigIsAlphaOnly(config)) {
2048 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2049 GR_GL_ALPHA, GR_GL_ALPHA };
2050 return gAlphaSmear;
2051 } else if (sampler.swapsRAndB()) {
2052 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2053 GR_GL_RED, GR_GL_ALPHA };
2054 return gRedBlueSwap;
2055 } else {
2056 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2057 GR_GL_BLUE, GR_GL_ALPHA };
2058 return gStraight;
2059 }
2060}
2061
2062void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2063 // should add texparameteri to interface to make 1 instead of 4 calls here
2064 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2065 GR_GL_TEXTURE_SWIZZLE_R,
2066 swizzle[0]));
2067 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2068 GR_GL_TEXTURE_SWIZZLE_G,
2069 swizzle[1]));
2070 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2071 GR_GL_TEXTURE_SWIZZLE_B,
2072 swizzle[2]));
2073 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2074 GR_GL_TEXTURE_SWIZZLE_A,
2075 swizzle[3]));
2076}
2077}
2078
bsalomon@google.comffca4002011-02-22 20:34:01 +00002079bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002080
2081 // GrGpu::setupClipAndFlushState should have already checked this
2082 // and bailed if not true.
2083 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002084
tomhudson@google.com93813632011-10-27 20:21:16 +00002085 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002086 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002087 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002088 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00002089
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002090 // true for now, but maybe not with GrEffect.
2091 GrAssert(NULL != nextTexture);
2092 // if we created a rt/tex and rendered to it without using a
2093 // texture and now we're texuring from the rt it will still be
2094 // the last bound texture, but it needs resolving. So keep this
2095 // out of the "last != next" check.
2096 GrGLRenderTarget* texRT =
2097 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2098 if (NULL != texRT) {
2099 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002100 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002101
2102 if (fHWDrawState.fTextures[s] != nextTexture) {
2103 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002104 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002105 #if GR_COLLECT_STATS
2106 ++fStats.fTextureChngCnt;
2107 #endif
2108 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2109 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002110 // The texture matrix has to compensate for texture width/height
2111 // and NPOT-embedded-in-POT
2112 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002113 }
2114
2115 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002116 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002117 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002118 nextTexture->getCachedTexParams(&timestamp);
2119 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002120 GrGLTexture::TexParams newTexParams;
2121
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002122 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002123
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002124 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002125 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2126 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002127 memcpy(newTexParams.fSwizzleRGBA,
2128 get_swizzle(nextTexture->config(), sampler),
2129 sizeof(newTexParams.fSwizzleRGBA));
2130 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002131 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002132 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002133 GR_GL_TEXTURE_MAG_FILTER,
2134 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002135 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002136 GR_GL_TEXTURE_MIN_FILTER,
2137 newTexParams.fFilter));
2138 }
2139 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2140 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002141 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002142 GR_GL_TEXTURE_WRAP_S,
2143 newTexParams.fWrapS));
2144 }
2145 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2146 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002147 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002148 GR_GL_TEXTURE_WRAP_T,
2149 newTexParams.fWrapT));
2150 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002151 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002152 (setAll ||
2153 memcmp(newTexParams.fSwizzleRGBA,
2154 oldTexParams.fSwizzleRGBA,
2155 sizeof(newTexParams.fSwizzleRGBA)))) {
2156 setTextureUnit(s);
2157 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2158 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002159 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002160 nextTexture->setCachedTexParams(newTexParams,
2161 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002162 }
2163 }
2164
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002165 GrIRect* rect = NULL;
2166 GrIRect clipBounds;
2167 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2168 fClip.hasConservativeBounds()) {
2169 fClip.getConservativeBounds().roundOut(&clipBounds);
2170 rect = &clipBounds;
2171 }
2172 this->flushRenderTarget(rect);
2173 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002174
reed@google.comac10a2d2010-12-22 21:39:39 +00002175 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2176 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2177 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002178 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002179 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002180 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002181 }
2182 }
2183
bsalomon@google.comd302f142011-03-03 13:54:13 +00002184 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2185 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002186 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002187 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002188 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002189 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002190 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002191 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002192 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002193 }
2194
bsalomon@google.comd302f142011-03-03 13:54:13 +00002195 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2196 switch (fCurrDrawState.fDrawFace) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002197 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002198 GL_CALL(Enable(GR_GL_CULL_FACE));
2199 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002200 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002201 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002202 GL_CALL(Enable(GR_GL_CULL_FACE));
2203 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002204 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002205 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002206 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002207 break;
2208 default:
2209 GrCrash("Unknown draw face.");
2210 }
2211 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2212 }
2213
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002214#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002216 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002217 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002218 NULL == fCurrDrawState.fRenderTarget ||
2219 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002220 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002221 fCurrDrawState.fRenderTarget);
2222 }
2223#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002224
reed@google.comac10a2d2010-12-22 21:39:39 +00002225 flushStencil();
2226
bsalomon@google.comd302f142011-03-03 13:54:13 +00002227 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002228 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002229 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002230}
2231
2232void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002233 if (fHWGeometryState.fVertexBuffer != buffer) {
2234 fHWGeometryState.fArrayPtrsDirty = true;
2235 fHWGeometryState.fVertexBuffer = buffer;
2236 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002237}
2238
2239void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002240 if (fHWGeometryState.fVertexBuffer == buffer) {
2241 // deleting bound buffer does implied bind to 0
2242 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002243 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002244 }
2245}
2246
2247void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002248 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002249}
2250
2251void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002252 if (fHWGeometryState.fIndexBuffer == buffer) {
2253 // deleting bound buffer does implied bind to 0
2254 fHWGeometryState.fIndexBuffer = NULL;
2255 }
2256}
2257
reed@google.comac10a2d2010-12-22 21:39:39 +00002258void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2259 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002260 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002261 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002262 }
2263 if (fHWDrawState.fRenderTarget == renderTarget) {
2264 fHWDrawState.fRenderTarget = NULL;
2265 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002266}
2267
2268void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002269 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002270 if (fCurrDrawState.fTextures[s] == texture) {
2271 fCurrDrawState.fTextures[s] = NULL;
2272 }
2273 if (fHWDrawState.fTextures[s] == texture) {
2274 // deleting bound texture does implied bind to 0
2275 fHWDrawState.fTextures[s] = NULL;
2276 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002277 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002278}
2279
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002280bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002281 GrGLenum* internalFormat,
2282 GrGLenum* format,
2283 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002284 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002285 case kRGBA_8888_PM_GrPixelConfig:
2286 case kRGBA_8888_UPM_GrPixelConfig:
2287 *format = GR_GL_RGBA;
2288 *internalFormat = GR_GL_RGBA;
2289 *type = GR_GL_UNSIGNED_BYTE;
2290 break;
2291 case kBGRA_8888_PM_GrPixelConfig:
2292 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002293 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002294 return false;
2295 }
2296 *format = GR_GL_BGRA;
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002297 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002298 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002299 } else {
2300 *internalFormat = GR_GL_RGBA;
2301 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002302 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002303 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002304 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002305 *format = GR_GL_RGB;
2306 *internalFormat = GR_GL_RGB;
2307 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002308 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002309 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002310 *format = GR_GL_RGBA;
2311 *internalFormat = GR_GL_RGBA;
2312 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002313 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002314 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002315 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002316 *format = GR_GL_PALETTE8_RGBA8;
2317 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002318 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 } else {
2320 return false;
2321 }
2322 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002323 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002324 *format = GR_GL_ALPHA;
2325 *internalFormat = GR_GL_ALPHA;
2326 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002327 break;
2328 default:
2329 return false;
2330 }
2331 return true;
2332}
2333
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002334void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002335 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002336 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002337 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002338 fActiveTextureUnitIdx = unit;
2339 }
2340}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002341
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002342void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002343 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002344 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002345 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2346 }
2347}
2348
reed@google.comac10a2d2010-12-22 21:39:39 +00002349/* On ES the internalFormat and format must match for TexImage and we use
2350 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2351 decide the internalFormat. However, on ES internalFormat for
2352 RenderBufferStorage* has to be a specific format (not a base format like
2353 GL_RGBA).
2354 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002355bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002356 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002357 // The ES story for BGRA and RenderbufferStorage appears murky. It
2358 // takes an internal format as a parameter. The OES FBO extension and
2359 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2360 // BGRA extensions adds BGRA as both an internal and external format
2361 // and the other only as an external format (like desktop GL). OES
2362 // restricts RenderbufferStorage's format to a *sized* internal format.
2363 // There is no sized BGRA internal format.
2364 // So if the texture has internal format BGRA we just hope that the
2365 // resolve blit can do RGBA->BGRA internal format conversion.
2366 case kRGBA_8888_PM_GrPixelConfig:
2367 case kRGBA_8888_UPM_GrPixelConfig:
2368 case kBGRA_8888_PM_GrPixelConfig:
2369 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002370 if (fGLCaps.fRGBA8RenderbufferSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002371 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2372 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002373 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002374 return true;
2375 } else {
2376 return false;
2377 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002378 case kRGB_565_GrPixelConfig:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002379 // ES2 supports 565, but desktop GL does not.
bsalomon@google.comc4364992011-11-07 15:54:49 +00002380 if (kDesktop_GrGLBinding != this->glBinding()) {
2381 *format = GR_GL_RGB565;
2382 return true;
2383 } else {
2384 return false;
2385 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002386 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002387 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002388 return true;
2389 default:
2390 return false;
2391 }
2392}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002393
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002394void GrGpuGL::resetDirtyFlags() {
2395 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2396}
2397
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002398void GrGpuGL::setBuffers(bool indexed,
2399 int* extraVertexOffset,
2400 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002401
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002402 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002403
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002404 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2405
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002406 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002407 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002408 case kBuffer_GeometrySrcType:
2409 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002410 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002411 break;
2412 case kArray_GeometrySrcType:
2413 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002414 this->finalizeReservedVertices();
2415 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2416 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002417 break;
2418 default:
2419 vbuf = NULL; // suppress warning
2420 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002421 }
2422
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002423 GrAssert(NULL != vbuf);
2424 GrAssert(!vbuf->isLocked());
2425 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002426 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002427 fHWGeometryState.fArrayPtrsDirty = true;
2428 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002429 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002430
2431 if (indexed) {
2432 GrAssert(NULL != extraIndexOffset);
2433
2434 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002435 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002436 case kBuffer_GeometrySrcType:
2437 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002438 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439 break;
2440 case kArray_GeometrySrcType:
2441 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002442 this->finalizeReservedIndices();
2443 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2444 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002445 break;
2446 default:
2447 ibuf = NULL; // suppress warning
2448 GrCrash("Unknown geometry src type!");
2449 }
2450
2451 GrAssert(NULL != ibuf);
2452 GrAssert(!ibuf->isLocked());
2453 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002454 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002455 fHWGeometryState.fIndexBuffer = ibuf;
2456 }
2457 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002458}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002459
2460int GrGpuGL::getMaxEdges() const {
2461 // FIXME: This is a pessimistic estimate based on how many other things
2462 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002463 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2464 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002465}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002466
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002467void GrGpuGL::GLCaps::print() const {
2468 for (int i = 0; i < fStencilFormats.count(); ++i) {
2469 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2470 i,
2471 fStencilFormats[i].fStencilBits,
2472 fStencilFormats[i].fTotalBits);
2473 }
2474
2475 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2476 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2477 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2478 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2479 static const char* gMSFBOExtStr[] = {
2480 "None",
2481 "ARB",
2482 "EXT",
2483 "Apple",
2484 };
2485 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002486 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002487 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2488 }
2489 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2490 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002491 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002492 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002493 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002494 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002495 (fTextureSwizzleSupport ? "YES": "NO"));
2496 GrPrintf("Unpack Row length support: %s\n",
2497 (fUnpackRowLengthSupport ? "YES": "NO"));
2498 GrPrintf("Pack Row length support: %s\n",
2499 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002500}