blob: bf7c895fdf57da1aba6048e7adb555714b566f5c [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"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000011#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000012#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
twiz@google.com0f31ca72011-03-18 17:38:11 +000014static const GrGLuint GR_MAX_GLUINT = ~0;
15static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com316f99232011-01-13 21:28:12 +000017// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000018// mucking with the state of any of the stages.
bsalomon@google.com316f99232011-01-13 21:28:12 +000019static const int SPARE_TEX_UNIT = GrGpuGL::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021#define SKIP_CACHE_CHECK true
22
twiz@google.com0f31ca72011-03-18 17:38:11 +000023static const GrGLenum gXfermodeCoeff2Blend[] = {
24 GR_GL_ZERO,
25 GR_GL_ONE,
26 GR_GL_SRC_COLOR,
27 GR_GL_ONE_MINUS_SRC_COLOR,
28 GR_GL_DST_COLOR,
29 GR_GL_ONE_MINUS_DST_COLOR,
30 GR_GL_SRC_ALPHA,
31 GR_GL_ONE_MINUS_SRC_ALPHA,
32 GR_GL_DST_ALPHA,
33 GR_GL_ONE_MINUS_DST_ALPHA,
34 GR_GL_CONSTANT_COLOR,
35 GR_GL_ONE_MINUS_CONSTANT_COLOR,
36 GR_GL_CONSTANT_ALPHA,
37 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000038
39 // extended blend coeffs
40 GR_GL_SRC1_COLOR,
41 GR_GL_ONE_MINUS_SRC1_COLOR,
42 GR_GL_SRC1_ALPHA,
43 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000044};
45
bsalomon@google.com271cffc2011-05-20 14:13:56 +000046bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000047 static const bool gCoeffReferencesBlendConst[] = {
48 false,
49 false,
50 false,
51 false,
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 true,
59 true,
60 true,
61 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062
63 // extended blend coeffs
64 false,
65 false,
66 false,
67 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000068 };
69 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000070 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
71
72 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
73 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
74 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
75 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
76 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
77 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
78 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
79 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
80 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
81 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
82 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
83 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
84 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
85 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
86
87 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
88 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
89 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
90 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
91
92 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
93 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000094}
95
reed@google.comac10a2d2010-12-22 21:39:39 +000096///////////////////////////////////////////////////////////////////////////////
97
bsalomon@google.comd302f142011-03-03 13:54:13 +000098void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
99 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000100 GrMatrix* matrix) {
101 GrAssert(NULL != texture);
102 GrAssert(NULL != matrix);
103 if (GR_Scalar1 != texture->contentScaleX() ||
104 GR_Scalar1 != texture->contentScaleY()) {
105 if (GrSamplerState::kRadial_SampleMode == mode) {
106 GrMatrix scale;
107 scale.setScale(texture->contentScaleX(), texture->contentScaleX());
108 matrix->postConcat(scale);
109 } else if (GrSamplerState::kNormal_SampleMode == mode) {
110 GrMatrix scale;
111 scale.setScale(texture->contentScaleX(), texture->contentScaleY());
112 matrix->postConcat(scale);
113 } else {
114 GrPrintf("We haven't handled NPOT adjustment for other sample modes!");
115 }
116 }
117 GrGLTexture::Orientation orientation = texture->orientation();
118 if (GrGLTexture::kBottomUp_Orientation == orientation) {
119 GrMatrix invY;
120 invY.setAll(GR_Scalar1, 0, 0,
121 0, -GR_Scalar1, GR_Scalar1,
122 0, 0, GrMatrix::I()[8]);
123 matrix->postConcat(invY);
124 } else {
125 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
126 }
127}
128
bsalomon@google.comd302f142011-03-03 13:54:13 +0000129bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000130 const GrSamplerState& sampler) {
131 GrAssert(NULL != texture);
132 if (!sampler.getMatrix().isIdentity()) {
133 return false;
134 }
135 if (GR_Scalar1 != texture->contentScaleX() ||
136 GR_Scalar1 != texture->contentScaleY()) {
137 return false;
138 }
139 GrGLTexture::Orientation orientation = texture->orientation();
140 if (GrGLTexture::kBottomUp_Orientation == orientation) {
141 return false;
142 } else {
143 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
144 }
145 return true;
146}
147
148///////////////////////////////////////////////////////////////////////////////
149
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000150static bool gPrintStartupSpew;
151
twiz@google.com59a190b2011-03-14 21:23:01 +0000152static bool fbo_test(int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000153
twiz@google.com0f31ca72011-03-18 17:38:11 +0000154 GrGLint savedFBO;
155 GrGLint savedTexUnit;
156 GR_GL_GetIntegerv(GR_GL_ACTIVE_TEXTURE, &savedTexUnit);
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000157 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, &savedFBO);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000158
twiz@google.com0f31ca72011-03-18 17:38:11 +0000159 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000160
twiz@google.com0f31ca72011-03-18 17:38:11 +0000161 GrGLuint testFBO;
twiz@google.com59a190b2011-03-14 21:23:01 +0000162 GR_GL(GenFramebuffers(1, &testFBO));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000163 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000164 GrGLuint testRTTex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 GR_GL(GenTextures(1, &testRTTex));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000166 GR_GL(BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000167 // some implementations require texture to be mip-map complete before
168 // FBO with level 0 bound as color attachment will be framebuffer complete.
twiz@google.com0f31ca72011-03-18 17:38:11 +0000169 GR_GL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
170 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
171 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
172 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000173 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000174 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000175 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000176 GR_GL(DeleteFramebuffers(1, &testFBO));
reed@google.comac10a2d2010-12-22 21:39:39 +0000177 GR_GL(DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000178
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000179 GR_GL(ActiveTexture(savedTexUnit));
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000180 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, savedFBO));
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
tomhudson@google.com747bf292011-06-14 18:16:52 +0000185static bool probe_for_npot_render_target_support(bool hasNPOTTextureSupport) {
186
187 /* Experimentation has found that some GLs that support NPOT textures
188 do not support FBOs with a NPOT texture. They report "unsupported" FBO
189 status. I don't know how to explicitly query for this. Do an
190 experiment. Note they may support NPOT with a renderbuffer but not a
191 texture. Presumably, the implementation bloats the renderbuffer
192 internally to the next POT.
193 */
194 if (hasNPOTTextureSupport) {
195 return fbo_test(200, 200);
196 }
197 return false;
198}
199
200static int probe_for_min_render_target_height(bool hasNPOTRenderTargetSupport,
201 int maxRenderTargetSize) {
202 /* The iPhone 4 has a restriction that for an FBO with texture color
203 attachment with height <= 8 then the width must be <= height. Here
204 we look for such a limitation.
205 */
206 if (gPrintStartupSpew) {
207 GrPrintf("Small height FBO texture experiments\n");
208 }
209 int minRenderTargetHeight = GR_INVAL_GLINT;
210 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? ++i : i *= 2) {
211 GrGLuint w = maxRenderTargetSize;
212 GrGLuint h = i;
213 if (fbo_test(w, h)) {
214 if (gPrintStartupSpew) {
215 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
216 }
217 minRenderTargetHeight = i;
218 break;
219 } else {
220 if (gPrintStartupSpew) {
221 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
222 }
223 }
224 }
225 GrAssert(GR_INVAL_GLINT != minRenderTargetHeight);
226
227 return minRenderTargetHeight;
228}
229
230static int probe_for_min_render_target_width(bool hasNPOTRenderTargetSupport,
231 int maxRenderTargetSize) {
232
233 if (gPrintStartupSpew) {
234 GrPrintf("Small width FBO texture experiments\n");
235 }
236 int minRenderTargetWidth = GR_INVAL_GLINT;
237 for (GrGLuint i = 1; i <= 256; hasNPOTRenderTargetSupport ? i *= 2 : ++i) {
238 GrGLuint w = i;
239 GrGLuint h = maxRenderTargetSize;
240 if (fbo_test(w, h)) {
241 if (gPrintStartupSpew) {
242 GrPrintf("\t[%d, %d]: PASSED\n", w, h);
243 }
244 minRenderTargetWidth = i;
245 break;
246 } else {
247 if (gPrintStartupSpew) {
248 GrPrintf("\t[%d, %d]: FAILED\n", w, h);
249 }
250 }
251 }
252 GrAssert(GR_INVAL_GLINT != minRenderTargetWidth);
253
254 return minRenderTargetWidth;
255}
256
257
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000258GrGpuGL::GrGpuGL()
259 : fStencilFormats(8) {
260
261 GrGLClearErr();
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000262
reed@google.comeeeb5a02010-12-23 15:12:59 +0000263 if (gPrintStartupSpew) {
264 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
265 this);
twiz@google.com59a190b2011-03-14 21:23:01 +0000266 GrPrintf("------ VENDOR %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000267 GrGLGetGLInterface()->fGetString(GR_GL_VENDOR));
twiz@google.com59a190b2011-03-14 21:23:01 +0000268 GrPrintf("------ RENDERER %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000269 GrGLGetGLInterface()->fGetString(GR_GL_RENDERER));
twiz@google.com59a190b2011-03-14 21:23:01 +0000270 GrPrintf("------ VERSION %s\n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000271 GrGLGetGLInterface()->fGetString(GR_GL_VERSION));
twiz@google.com59a190b2011-03-14 21:23:01 +0000272 GrPrintf("------ EXTENSIONS\n %s \n",
twiz@google.com0f31ca72011-03-18 17:38:11 +0000273 GrGLGetGLInterface()->fGetString(GR_GL_EXTENSIONS));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000274 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000275
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000276 fGLVersion = gl_version_as_float();
277 fExtensionString = (const char*) GR_GL(GetString(GR_GL_EXTENSIONS));
reed@google.comac10a2d2010-12-22 21:39:39 +0000278
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000279 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000280
twiz@google.com0f31ca72011-03-18 17:38:11 +0000281 GrGLint maxTextureUnits;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000282 // check FS and fixed-function texture unit limits
283 // we only use textures in the fragment stage currently.
284 // checks are > to make sure we have a spare unit.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000285 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES2) {
286 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
287 GrAssert(maxTextureUnits > kNumStages);
288 }
289 if (GR_GL_SUPPORT_DESKTOP || GR_GL_SUPPORT_ES1) {
290 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
291 GrAssert(maxTextureUnits > kNumStages);
292 }
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000293 if (GR_GL_SUPPORT_ES2) {
294 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
295 &fMaxFragmentUniformVectors);
296 } else if (GR_GL_SUPPORT_DESKTOP) {
297 GrGLint max;
298 GR_GL_GetIntegerv(GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
299 fMaxFragmentUniformVectors = max / 4;
300 } else {
301 fMaxFragmentUniformVectors = 16;
302 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000303
reed@google.comac10a2d2010-12-22 21:39:39 +0000304 ////////////////////////////////////////////////////////////////////////////
305 // Check for supported features.
306
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000307 this->setupStencilFormats();
reed@google.comac10a2d2010-12-22 21:39:39 +0000308
twiz@google.com0f31ca72011-03-18 17:38:11 +0000309 GrGLint numFormats;
310 GR_GL_GetIntegerv(GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000311 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
twiz@google.com0f31ca72011-03-18 17:38:11 +0000312 GR_GL_GetIntegerv(GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
reed@google.comac10a2d2010-12-22 21:39:39 +0000313 for (int i = 0; i < numFormats; ++i) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000314 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000315 f8bitPaletteSupport = true;
316 break;
317 }
318 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000319
320 if (gPrintStartupSpew) {
321 GrPrintf("Palette8 support: %s\n", (f8bitPaletteSupport ? "YES" : "NO"));
322 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000323
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000324 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
325 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
326 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
327 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
329 memset(fAASamples, 0, sizeof(fAASamples));
330 fMSFBOType = kNone_MSFBO;
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000331 if (GR_GL_SUPPORT_ES) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000332 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000333 // chrome's extension is equivalent to the EXT msaa
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000334 // and fbo_blit extensions.
335 fMSFBOType = kDesktopEXT_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000336 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000337 fMSFBOType = kAppleES_MSFBO;
338 }
339 } else {
340 GrAssert(GR_GL_SUPPORT_DESKTOP);
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000341 if ((fGLVersion >= 3.f) || this->hasExtension("GL_ARB_framebuffer_object")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000342 fMSFBOType = kDesktopARB_MSFBO;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000343 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
344 this->hasExtension("GL_EXT_framebuffer_blit")) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000345 fMSFBOType = kDesktopEXT_MSFBO;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000346 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000347 }
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000348 if (gPrintStartupSpew) {
349 switch (fMSFBOType) {
350 case kNone_MSFBO:
351 GrPrintf("MSAA Support: NONE\n");
352 break;
353 case kDesktopARB_MSFBO:
354 GrPrintf("MSAA Support: DESKTOP ARB.\n");
355 break;
356 case kDesktopEXT_MSFBO:
357 GrPrintf("MSAA Support: DESKTOP EXT.\n");
358 break;
359 case kAppleES_MSFBO:
360 GrPrintf("MSAA Support: APPLE ES.\n");
361 break;
reed@google.comeeeb5a02010-12-23 15:12:59 +0000362 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000363 }
364
365 if (kNone_MSFBO != fMSFBOType) {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000366 GrGLint maxSamples;
bsalomon@google.comd1e433532011-03-21 21:38:40 +0000367 GR_GL_GetIntegerv(GR_GL_MAX_SAMPLES, &maxSamples);
reed@google.comac10a2d2010-12-22 21:39:39 +0000368 if (maxSamples > 1 ) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000369 fAASamples[kNone_GrAALevel] = 0;
370 fAASamples[kLow_GrAALevel] = GrMax(2,
371 GrFixedFloorToInt((GR_FixedHalf) *
372 maxSamples));
373 fAASamples[kMed_GrAALevel] = GrMax(2,
374 GrFixedFloorToInt(((GR_Fixed1*3)/4) *
375 maxSamples));
376 fAASamples[kHigh_GrAALevel] = maxSamples;
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000378 if (gPrintStartupSpew) {
379 GrPrintf("\tMax Samples: %d\n", maxSamples);
380 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000381 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000382 fFSAASupport = fAASamples[kHigh_GrAALevel] > 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000383
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000384 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000385 fHasStencilWrap = (fGLVersion >= 1.4f) ||
386 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000387 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000388 fHasStencilWrap = (fGLVersion >= 2.0f) || this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000389 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000390 if (gPrintStartupSpew) {
391 GrPrintf("Stencil Wrap: %s\n", (fHasStencilWrap ? "YES" : "NO"));
392 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000394 if (GR_GL_SUPPORT_DESKTOP) {
395 // we could also look for GL_ATI_separate_stencil extension or
396 // GL_EXT_stencil_two_side but they use different function signatures
397 // than GL2.0+ (and than each other).
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000398 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000399 // supported on GL 1.4 and higher or by extension
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000400 fStencilWrapOpsSupport = (fGLVersion >= 1.4f) ||
401 this->hasExtension("GL_EXT_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000402 } else {
403 // ES 2 has two sided stencil but 1.1 doesn't. There doesn't seem to be
404 // an ES1 extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000405 fTwoSidedStencilSupport = (fGLVersion >= 2.f);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000406 // stencil wrap support is in ES2, ES1 requires extension.
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000407 fStencilWrapOpsSupport = (fGLVersion >= 2.f) ||
408 this->hasExtension("GL_OES_stencil_wrap");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000409 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000410 if (gPrintStartupSpew) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000411 GrPrintf("Stencil Caps: TwoSide: %s, Wrap: %s\n",
412 (fTwoSidedStencilSupport ? "YES" : "NO"),
413 (fStencilWrapOpsSupport ? "YES" : "NO"));
reed@google.comeeeb5a02010-12-23 15:12:59 +0000414 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000416 if (GR_GL_SUPPORT_DESKTOP) {
417 fRGBA8Renderbuffer = true;
418 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000419 fRGBA8Renderbuffer = this->hasExtension("GL_OES_rgb8_rgba8");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000420 }
reed@google.comeeeb5a02010-12-23 15:12:59 +0000421 if (gPrintStartupSpew) {
422 GrPrintf("RGBA Renderbuffer: %s\n", (fRGBA8Renderbuffer ? "YES" : "NO"));
423 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000424
425
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000426 if (GR_GL_SUPPORT_ES) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000427 if (GR_GL_32BPP_COLOR_FORMAT == GR_GL_BGRA) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000428 GrAssert(this->hasExtension("GL_EXT_texture_format_BGRA8888"));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000429 }
430 }
431
432 if (GR_GL_SUPPORT_DESKTOP) {
433 fBufferLockSupport = true; // we require VBO support and the desktop VBO
434 // extension includes glMapBuffer.
435 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000436 fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000437 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000438
reed@google.comeeeb5a02010-12-23 15:12:59 +0000439 if (gPrintStartupSpew) {
440 GrPrintf("Map Buffer: %s\n", (fBufferLockSupport ? "YES" : "NO"));
441 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000442
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000443 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000444 if (fGLVersion >= 2.f ||
445 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000446 fNPOTTextureTileSupport = true;
447 fNPOTTextureSupport = true;
448 } else {
449 fNPOTTextureTileSupport = false;
450 fNPOTTextureSupport = false;
451 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000453 if (fGLVersion >= 2.f) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000454 fNPOTTextureSupport = true;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000455 fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000456 } else {
457 fNPOTTextureSupport =
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000458 this->hasExtension("GL_APPLE_texture_2D_limited_npot");
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000459 fNPOTTextureTileSupport = false;
460 }
bsalomon@google.com0748f212011-02-01 22:56:16 +0000461 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000462
bsalomon@google.com205d4602011-04-25 12:43:45 +0000463 fAALineSupport = GR_GL_SUPPORT_DESKTOP;
464
reed@google.comac10a2d2010-12-22 21:39:39 +0000465 ////////////////////////////////////////////////////////////////////////////
tomhudson@google.com747bf292011-06-14 18:16:52 +0000466 // Experiments to determine limitations that can't be queried.
467 // TODO: Make these a preprocess that generate some compile time constants.
468 // TODO: probe once at startup, rather than once per context creation.
reed@google.comac10a2d2010-12-22 21:39:39 +0000469
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000470 int expectNPOTTargets = GrGLGetGLInterface()->fNPOTRenderTargetSupport;
471 if (expectNPOTTargets == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000472 fNPOTRenderTargetSupport =
473 probe_for_npot_render_target_support(fNPOTTextureSupport);
tomhudson@google.com30e4bb62011-06-15 19:41:46 +0000474 } else {
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000475 GrAssert(expectNPOTTargets == 0 || expectNPOTTargets == 1);
476 fNPOTRenderTargetSupport = static_cast<bool>(expectNPOTTargets);
bsalomon@google.com0748f212011-02-01 22:56:16 +0000477 }
bsalomon@google.com18908aa2011-02-07 14:51:55 +0000478
bsalomon@google.com0748f212011-02-01 22:56:16 +0000479 if (gPrintStartupSpew) {
480 if (fNPOTTextureSupport) {
481 GrPrintf("NPOT textures supported\n");
482 if (fNPOTTextureTileSupport) {
483 GrPrintf("NPOT texture tiling supported\n");
484 } else {
485 GrPrintf("NPOT texture tiling NOT supported\n");
486 }
487 if (fNPOTRenderTargetSupport) {
488 GrPrintf("NPOT render targets supported\n");
489 } else {
490 GrPrintf("NPOT render targets NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000491 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000492 } else {
bsalomon@google.com0748f212011-02-01 22:56:16 +0000493 GrPrintf("NPOT textures NOT supported\n");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000494 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000495 }
496
bsalomon@google.com91958362011-06-13 17:58:13 +0000497 GR_GL_GetIntegerv(GR_GL_MAX_TEXTURE_SIZE, &fMaxTextureSize);
498 GR_GL_GetIntegerv(GR_GL_MAX_RENDERBUFFER_SIZE, &fMaxRenderTargetSize);
tomhudson@google.com747bf292011-06-14 18:16:52 +0000499 // Our render targets are always created with textures as the color
bsalomon@google.com91958362011-06-13 17:58:13 +0000500 // attachment, hence this min:
501 fMaxRenderTargetSize = GrMin(fMaxTextureSize, fMaxRenderTargetSize);
bsalomon@google.com7aaee002011-04-11 19:54:04 +0000502
tomhudson@google.com747bf292011-06-14 18:16:52 +0000503 fMinRenderTargetHeight = GrGLGetGLInterface()->fMinRenderTargetHeight;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000504 if (fMinRenderTargetHeight == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000505 fMinRenderTargetHeight =
506 probe_for_min_render_target_height(fNPOTRenderTargetSupport,
507 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000508 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000509
tomhudson@google.com747bf292011-06-14 18:16:52 +0000510 fMinRenderTargetWidth = GrGLGetGLInterface()->fMinRenderTargetWidth;
tomhudson@google.come67bd3f2011-06-16 14:08:04 +0000511 if (fMinRenderTargetWidth == kProbe_GrGLCapability) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000512 fMinRenderTargetWidth =
513 probe_for_min_render_target_width(fNPOTRenderTargetSupport,
514 fMaxRenderTargetSize);
reed@google.comeeeb5a02010-12-23 15:12:59 +0000515 }
tomhudson@google.com747bf292011-06-14 18:16:52 +0000516
bsalomon@google.comfe676522011-06-17 18:12:21 +0000517 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000518}
519
520GrGpuGL::~GrGpuGL() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000521}
522
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000523void GrGpuGL::resetContext() {
524 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000525 fHWBlendDisabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000526 GR_GL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000527
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000528 // we don't use the zb at all
twiz@google.com0f31ca72011-03-18 17:38:11 +0000529 GR_GL(Disable(GR_GL_DEPTH_TEST));
530 GR_GL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000531
twiz@google.com0f31ca72011-03-18 17:38:11 +0000532 GR_GL(Disable(GR_GL_CULL_FACE));
533 GR_GL(FrontFace(GR_GL_CCW));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000534 fHWDrawState.fDrawFace = kBoth_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
twiz@google.com0f31ca72011-03-18 17:38:11 +0000536 GR_GL(Disable(GR_GL_DITHER));
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000537 if (GR_GL_SUPPORT_DESKTOP) {
538 GR_GL(Disable(GR_GL_LINE_SMOOTH));
539 GR_GL(Disable(GR_GL_POINT_SMOOTH));
540 GR_GL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000541 fHWAAState.fMSAAEnabled = false;
542 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000543 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000544
twiz@google.com0f31ca72011-03-18 17:38:11 +0000545 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +0000546 fHWDrawState.fFlagBits = 0;
547
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 // we only ever use lines in hairline mode
549 GR_GL(LineWidth(1));
550
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000551 // invalid
552 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000553
reed@google.comac10a2d2010-12-22 21:39:39 +0000554 // illegal values
bsalomon@google.comffca4002011-02-22 20:34:01 +0000555 fHWDrawState.fSrcBlend = (GrBlendCoeff)-1;
556 fHWDrawState.fDstBlend = (GrBlendCoeff)-1;
bsalomon@google.com080773c2011-03-15 19:09:25 +0000557
558 fHWDrawState.fBlendConstant = 0x00000000;
559 GR_GL(BlendColor(0,0,0,0));
560
reed@google.comac10a2d2010-12-22 21:39:39 +0000561 fHWDrawState.fColor = GrColor_ILLEGAL;
bsalomon@google.com316f99232011-01-13 21:28:12 +0000562
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000563 fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000564
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000565 for (int s = 0; s < kNumStages; ++s) {
566 fHWDrawState.fTextures[s] = NULL;
567 fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax,
568 -GR_ScalarMax,
569 true);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000570 fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000571 fHWDrawState.fSamplerStates[s].setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000572 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000573
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000574 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 fHWBounds.fScissorEnabled = false;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000576 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000577 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000578
bsalomon@google.comd302f142011-03-03 13:54:13 +0000579 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000580 fHWStencilClip = false;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000581 fClipState.fClipIsDirty = true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000582 fClipState.fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000583
584 fHWGeometryState.fIndexBuffer = NULL;
585 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000586
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000587 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
twiz@google.com0f31ca72011-03-18 17:38:11 +0000589 GR_GL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 fHWDrawState.fRenderTarget = NULL;
591}
592
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000593GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
594
595 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
596 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
597 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
598 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
599
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000600 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000601 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000602 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
603#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
604 if (desc.fSampleCnt) {
605#else
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000606 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000607#endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000608 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000609 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000610 } else {
611 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000612 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000613 }
614 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000615 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000616 }
617 // we don't know what the RB ids are without glGets and we don't care
618 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000619 rtDesc.fStencilRenderbufferID = 0;
620 rtDesc.fMSColorRenderbufferID = 0;
621#if GR_USE_PLATFORM_CREATE_SAMPLE_COUNT
622 rtDesc.fSampleCnt = desc.fSampleCnt;
623#else
624 if (kIsMultisampled_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
625 // just guess, this code path is only compiled in WK and we aren't
626 // using MSAA anyway. This will be stripped out soon when WK sets
627 // the fSampleCnt in GrPlatformSurfaceDesc.
628 rtDesc.fSampleCnt = 4;
629 } else {
630 rtDesc.fSampleCnt = 0;
631 }
632#endif
633 rtDesc.fStencilBits = desc.fStencilBits;
634 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000635 }
636
637 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000638 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000639 GrGLenum dontCare;
640 if (!canBeTexture(desc.fConfig, &dontCare,
641 &texDesc.fUploadFormat,
642 &texDesc.fUploadType)) {
643 return NULL;
644 }
645
646 GrGLTexture::TexParams params;
647
648 texDesc.fAllocWidth = texDesc.fContentWidth = desc.fWidth;
649 texDesc.fAllocHeight = texDesc.fContentHeight = desc.fHeight;
650
bsalomon@google.com90405932011-06-17 15:56:55 +0000651 texDesc.fFormat = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000652 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000653 texDesc.fTextureID = desc.fPlatformTexture;
654 texDesc.fUploadByteCount = GrBytesPerPixel(desc.fConfig);
655 texDesc.fOwnsID = false;
656
657 params.invalidate(); // rather than do glGets.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000658 if (isRenderTarget) {
659 return new GrGLTexture(this, texDesc, rtDesc, params);
660 } else {
661 return new GrGLTexture(this, texDesc, params);
662 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000663 } else {
664 GrGLIRect viewport;
665 viewport.fLeft = 0;
666 viewport.fBottom = 0;
667 viewport.fWidth = desc.fWidth;
668 viewport.fHeight = desc.fHeight;
669
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000670 return new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000671 }
672}
673
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000674namespace {
675
676static const GrGLenum kUnknownGLFormat = ~0;
677
678GrGLenum get_fbo_color_format() {
679 GrGLint cbType;
680 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
681 GR_GL_COLOR_ATTACHMENT0,
682 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
683 &cbType);
684 GrGLint cbID;
685 GrGLint cbFormat;
686 switch (cbType) {
687 case GR_GL_RENDERBUFFER:
688 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
689 GR_GL_COLOR_ATTACHMENT0,
690 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
691 &cbID);
692 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER, cbID));
693 GR_GL_GetRenderbufferParameteriv(GR_GL_RENDERBUFFER,
694 GR_GL_RENDERBUFFER_INTERNAL_FORMAT,
695 &cbFormat);
696 return cbFormat;
697 break;
698 case GR_GL_TEXTURE:
699 // ES doesn't have glGetTexLevelParameter
700 if (GR_GL_SUPPORT_DESKTOP) {
701 GrGLint cbLevel;
702 GrGLint cbFace;
703 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
704 GR_GL_COLOR_ATTACHMENT0,
705 GR_GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
706 &cbID);
707 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
708 GR_GL_COLOR_ATTACHMENT0,
709 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
710 &cbLevel);
711 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
712 GR_GL_COLOR_ATTACHMENT0,
713 GR_GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
714 &cbFace);
715 GrGLenum bind;
716 GrGLenum target;
717 if (cbFace) {
718 bind = GR_GL_TEXTURE_CUBE_MAP;
719 target = cbFace;
720 } else {
721 bind = GR_GL_TEXTURE_2D;
722 target = GR_GL_TEXTURE_2D;
723 }
724 GR_GL(BindTexture(bind, cbID));
725 GR_GL_GetTexLevelParameteriv(target, cbLevel,
726 GR_GL_TEXTURE_INTERNAL_FORMAT, &cbFormat);
727 return cbFormat;
728 } else {
729 return kUnknownGLFormat;
730 }
731 break;
732 default:
733 // we can get here with FBO 0, not a render buffer or a texture
734 return kUnknownGLFormat;
735 }
736}
737
738GrPixelConfig internal_color_format_to_config(GrGLenum iFormat) {
739 switch (iFormat) {
740 case GR_GL_RGB565:
741 return kRGB_565_GrPixelConfig;
742 case GR_GL_RGBA4:
743 return kRGBA_4444_GrPixelConfig;
744 case GR_GL_RGBA8:
745 case GR_GL_SRGB8_ALPHA8:
746 case GR_GL_SRGB_ALPHA:
747 case GR_GL_RGBA:
748 case GR_GL_BGRA:
749 return kRGBA_8888_GrPixelConfig;
750 case GR_GL_RGB8:
751 case GR_GL_SRGB8:
752 case GR_GL_SRGB:
753 return kRGBX_8888_GrPixelConfig;
754 default:
755 // there are many GL formats we don't have enums
756 // for. We should still render to them if the client
757 // asks us.
758 return kUnknown_GrPixelConfig;
759 }
760}
761
762GrPixelConfig get_implied_color_config(bool arbFBOExtension) {
763 GrGLint rSize, bSize, gSize, aSize;
764 if (arbFBOExtension) {
765 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
766 GR_GL_COLOR_ATTACHMENT0,
767 GR_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &rSize);
768 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
769 GR_GL_COLOR_ATTACHMENT0,
770 GR_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &gSize);
771 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
772 GR_GL_COLOR_ATTACHMENT0,
773 GR_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE, &bSize);
774 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
775 GR_GL_COLOR_ATTACHMENT0,
776 GR_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE, &aSize);
777 } else {
778 GR_GL_GetIntegerv(GR_GL_RED_BITS, &rSize);
779 GR_GL_GetIntegerv(GR_GL_GREEN_BITS, &gSize);
780 GR_GL_GetIntegerv(GR_GL_BLUE_BITS, &bSize);
781 GR_GL_GetIntegerv(GR_GL_ALPHA_BITS, &aSize);
782 }
783
784 if(8 == rSize && 8 == gSize && 8 == bSize) {
785 if (0 == aSize) {
786 return kRGBX_8888_GrPixelConfig;
787 } else if (8 == aSize) {
788 return kRGBA_8888_GrPixelConfig;
789 }
790 } else if (4 == rSize && 4 == gSize && 4 == bSize && 4 == aSize) {
791 return kRGBA_4444_GrPixelConfig;
792 } else if (5 == rSize && 6 == gSize && 5 == bSize && 0 == aSize) {
793 return kRGB_565_GrPixelConfig;
794 }
795 return kUnknown_GrPixelConfig;
796}
797
798int get_fbo_stencil_bits(bool arbFBOExtension) {
799 GrGLint stencilBits;
800 if (arbFBOExtension) {
801 GR_GL_GetFramebufferAttachmentParameteriv(GR_GL_FRAMEBUFFER,
802 GR_GL_STENCIL_ATTACHMENT,
803 GR_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE,
804 &stencilBits);
805 } else {
806 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, &stencilBits);
807 }
808 return stencilBits;
809}
810}
811
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000812GrRenderTarget* GrGpuGL::onCreateRenderTargetFrom3DApiState() {
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000813
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000814 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000815
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000816 GR_GL_GetIntegerv(GR_GL_FRAMEBUFFER_BINDING, (GrGLint*)&rtDesc.fRTFBOID);
817 rtDesc.fTexFBOID = rtDesc.fRTFBOID;
818 rtDesc.fMSColorRenderbufferID = 0;
819 rtDesc.fStencilRenderbufferID = 0;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000820
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000821 bool arbFBO = (GR_GL_SUPPORT_DESKTOP && (fGLVersion > 3.0 ||
822 this->hasExtension("GL_ARB_framebuffer_object")));
823
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000824 GrGLIRect viewport;
825 viewport.setFromGLViewport();
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000826 rtDesc.fStencilBits = get_fbo_stencil_bits(arbFBO);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000827
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000828 GR_GL_GetIntegerv(GR_GL_SAMPLES, &rtDesc.fSampleCnt);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000829 GrGLenum fmat = get_fbo_color_format();
830 if (kUnknownGLFormat == fmat) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000831 rtDesc.fConfig = get_implied_color_config(arbFBO);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000832 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000833 rtDesc.fConfig = internal_color_format_to_config(fmat);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000834 }
835
836 // may have to bind a texture to gets its format
837 this->setSpareTextureUnit();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000838
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000839 rtDesc.fOwnIDs = false;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000840
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000841 return new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000842}
843
bsalomon@google.com5782d712011-01-21 21:03:59 +0000844///////////////////////////////////////////////////////////////////////////////
845
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000846void GrGpuGL::setupStencilFormats() {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000847
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000848 // Build up list of legal stencil formats (though perhaps not supported on
849 // the particular gpu/driver) from most preferred to least.
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000850
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000851 // these consts are in order of most preferred to least preferred
852 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
853 static const StencilFormat gS8 = {GR_GL_STENCIL_INDEX8, 8, false};
854 static const StencilFormat gS16 = {GR_GL_STENCIL_INDEX16, 16, false};
855 static const StencilFormat gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, true };
856 static const StencilFormat gS4 = {GR_GL_STENCIL_INDEX4, 4, false};
857 static const StencilFormat gS = {GR_GL_STENCIL_INDEX, gUNKNOWN_BITCOUNT, false};
858 static const StencilFormat gDS = {GR_GL_DEPTH_STENCIL, gUNKNOWN_BITCOUNT, true };
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000859
860 if (GR_GL_SUPPORT_DESKTOP) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000861 bool supportsPackedDS = fGLVersion >= 3.0f ||
862 this->hasExtension("GL_EXT_packed_depth_stencil") ||
863 this->hasExtension("GL_ARB_framebuffer_object");
864
865 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
866 // require FBO support we can expect these are legal formats and don't
867 // check. These also all support the unsized GL_STENCIL_INDEX.
868 fStencilFormats.push_back() = gS8;
869 fStencilFormats.push_back() = gS16;
870 if (supportsPackedDS) {
871 fStencilFormats.push_back() = gD24S8;
872 }
873 fStencilFormats.push_back() = gS4;
874 if (supportsPackedDS) {
875 fStencilFormats.push_back() = gDS;
876 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000877 } else {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000878 // ES2 has STENCIL_INDEX8 without extensions.
879 // ES1 with GL_OES_framebuffer_object (which we require for ES1)
880 // introduces tokens for S1 thu S8 but there are separate extensions
881 // that make them legal (GL_OES_stencil1, ...).
882 // GL_OES_packed_depth_stencil adds DEPTH24_STENCIL8
883 // ES doesn't support using the unsized formats.
884
885 if (fGLVersion >= 2.f || this->hasExtension("GL_OES_stencil8")) {
886 fStencilFormats.push_back() = gS8;
887 }
888 //fStencilFormats.push_back() = gS16;
889 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
890 fStencilFormats.push_back() = gD24S8;
891 }
892 if (this->hasExtension("GL_OES_stencil4")) {
893 fStencilFormats.push_back() = gS4;
894 }
895 // we require some stencil format.
896 GrAssert(fStencilFormats.count() > 0);
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000897 }
898}
899
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000900// good to set a break-point here to know when createTexture fails
901static GrTexture* return_null_texture() {
902// GrAssert(!"null texture");
903 return NULL;
904}
905
906#if GR_DEBUG
907static size_t as_size_t(int x) {
908 return x;
909}
910#endif
911
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000912GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000913 const void* srcData,
914 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000915
916#if GR_COLLECT_STATS
917 ++fStats.fTextureCreateCnt;
918#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000919
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000920 this->setSpareTextureUnit();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000921
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000922 static const GrGLTexture::TexParams DEFAULT_PARAMS = {
twiz@google.com0f31ca72011-03-18 17:38:11 +0000923 GR_GL_NEAREST,
924 GR_GL_CLAMP_TO_EDGE,
925 GR_GL_CLAMP_TO_EDGE
bsalomon@google.comda96ea02010-12-23 16:53:57 +0000926 };
reed@google.com1fcd51e2011-01-05 15:50:27 +0000927
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000928 GrGLTexture::Desc glTexDesc;
twiz@google.com0f31ca72011-03-18 17:38:11 +0000929 GrGLenum internalFormat;
reed@google.comac10a2d2010-12-22 21:39:39 +0000930
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000931 glTexDesc.fContentWidth = desc.fWidth;
932 glTexDesc.fContentHeight = desc.fHeight;
933 glTexDesc.fAllocWidth = desc.fWidth;
934 glTexDesc.fAllocHeight = desc.fHeight;
935 glTexDesc.fFormat = desc.fFormat;
936 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000937
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000938 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000939 if (!canBeTexture(desc.fFormat,
940 &internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000941 &glTexDesc.fUploadFormat,
942 &glTexDesc.fUploadType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000943 return return_null_texture();
944 }
945
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000946 // We keep GrRenderTargets in GL's normal orientation so that they
947 // can be drawn to by the outside world without the client having
948 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000949 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000950 GrGLTexture::kTopDown_Orientation;
951
reed@google.comac10a2d2010-12-22 21:39:39 +0000952 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fAASamples));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000953 GrGLint samples = fAASamples[desc.fAALevel];
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000954 if (kNone_MSFBO == fMSFBOType && desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000955 GrPrintf("AA RT requested but not supported on this platform.");
956 }
957
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000958 glTexDesc.fUploadByteCount = GrBytesPerPixel(desc.fFormat);
reed@google.comac10a2d2010-12-22 21:39:39 +0000959
reed@google.com5e762232011-04-04 18:15:49 +0000960 // in case we need a temporary, trimmed copy of the src pixels
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000961 SkAutoSMalloc<128 * 128> tempStorage;
reed@google.com5e762232011-04-04 18:15:49 +0000962
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000963 if (!rowBytes) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000964 rowBytes = glTexDesc.fUploadByteCount * desc.fWidth;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000965 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000966 /*
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000967 * check whether to allocate a temporary buffer for flipping y or
968 * because our srcData has extra bytes past each row. If so, we need
969 * to trim those off here, since GL ES doesn't let us specify
970 * GL_UNPACK_ROW_LENGTH.
reed@google.comac10a2d2010-12-22 21:39:39 +0000971 */
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000972 bool flipY = GrGLTexture::kBottomUp_Orientation == glTexDesc.fOrientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000973 if (GR_GL_SUPPORT_DESKTOP && !flipY) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000974 if (srcData && rowBytes != desc.fWidth * glTexDesc.fUploadByteCount) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000975 GR_GL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000976 rowBytes / glTexDesc.fUploadByteCount));
reed@google.comac10a2d2010-12-22 21:39:39 +0000977 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000978 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000979 size_t trimRowBytes = desc.fWidth * glTexDesc.fUploadByteCount;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000980 if (srcData && (trimRowBytes < rowBytes || flipY)) {
reed@google.com5e762232011-04-04 18:15:49 +0000981 // copy the data into our new storage, skipping the trailing bytes
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000982 size_t trimSize = desc.fHeight * trimRowBytes;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000983 const char* src = (const char*)srcData;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000984 if (flipY) {
985 src += (desc.fHeight - 1) * rowBytes;
986 }
987 char* dst = (char*)tempStorage.realloc(trimSize);
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +0000988 for (int y = 0; y < desc.fHeight; y++) {
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000989 memcpy(dst, src, trimRowBytes);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000990 if (flipY) {
991 src -= rowBytes;
992 } else {
993 src += rowBytes;
994 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000995 dst += trimRowBytes;
996 }
997 // now point srcData to our trimmed version
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000998 srcData = tempStorage.get();
999 rowBytes = trimRowBytes;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00001000 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001001 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001002
reed@google.comac10a2d2010-12-22 21:39:39 +00001003 if (renderTarget) {
bsalomon@google.com0748f212011-02-01 22:56:16 +00001004 if (!this->npotRenderTargetSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001005 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1006 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com0748f212011-02-01 22:56:16 +00001007 }
1008
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001009 glTexDesc.fAllocWidth = GrMax(fMinRenderTargetWidth,
1010 glTexDesc.fAllocWidth);
1011 glTexDesc.fAllocHeight = GrMax(fMinRenderTargetHeight,
1012 glTexDesc.fAllocHeight);
1013 if (glTexDesc.fAllocWidth > fMaxRenderTargetSize ||
1014 glTexDesc.fAllocHeight > fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001015 return return_null_texture();
1016 }
bsalomon@google.com0748f212011-02-01 22:56:16 +00001017 } else if (!this->npotTextureSupport()) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001018 glTexDesc.fAllocWidth = GrNextPow2(desc.fWidth);
1019 glTexDesc.fAllocHeight = GrNextPow2(desc.fHeight);
1020 if (glTexDesc.fAllocWidth > fMaxTextureSize ||
1021 glTexDesc.fAllocHeight > fMaxTextureSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001022 return return_null_texture();
1023 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 }
1025
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001026 GR_GL(GenTextures(1, &glTexDesc.fTextureID));
1027 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001028 return return_null_texture();
1029 }
1030
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001031 GR_GL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001032 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1033 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001034 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001035 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1036 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001037 DEFAULT_PARAMS.fFilter));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001038 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1039 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001040 DEFAULT_PARAMS.fWrapS));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001041 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
1042 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.comda96ea02010-12-23 16:53:57 +00001043 DEFAULT_PARAMS.fWrapT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001044
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001045 GR_GL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, glTexDesc.fUploadByteCount));
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001046 if (kIndex_8_GrPixelConfig == desc.fFormat &&
reed@google.comac10a2d2010-12-22 21:39:39 +00001047 supports8BitPalette()) {
1048 // ES only supports CompressedTexImage2D, not CompressedTexSubimage2D
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001049 GrAssert(desc.fWidth == glTexDesc.fAllocWidth);
1050 GrAssert(desc.fHeight == glTexDesc.fAllocHeight);
1051 GrGLsizei imageSize = glTexDesc.fAllocWidth * glTexDesc.fAllocHeight +
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001052 kGrColorTableSize;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001053 GR_GL(CompressedTexImage2D(GR_GL_TEXTURE_2D, 0, glTexDesc.fUploadFormat,
1054 glTexDesc.fAllocWidth, glTexDesc.fAllocHeight,
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 0, imageSize, srcData));
bsalomon@google.comf987d1b2011-04-04 17:13:52 +00001056 GrGLRestoreResetRowLength();
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001058 if (NULL != srcData && (glTexDesc.fAllocWidth != desc.fWidth ||
1059 glTexDesc.fAllocHeight != desc.fHeight)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001060 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001061 glTexDesc.fAllocWidth, glTexDesc.fAllocHeight,
1062 0, glTexDesc.fUploadFormat, glTexDesc.fUploadType, NULL));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001063 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, 0, desc.fWidth,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001064 desc.fHeight, glTexDesc.fUploadFormat,
1065 glTexDesc.fUploadType, srcData));
bsalomon@google.comf987d1b2011-04-04 17:13:52 +00001066 GrGLRestoreResetRowLength();
reed@google.comac10a2d2010-12-22 21:39:39 +00001067
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001068 int extraW = glTexDesc.fAllocWidth - desc.fWidth;
1069 int extraH = glTexDesc.fAllocHeight - desc.fHeight;
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001070 int maxTexels = extraW * extraH;
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 maxTexels = GrMax(extraW * desc.fHeight, maxTexels);
1072 maxTexels = GrMax(desc.fWidth * extraH, maxTexels);
1073
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001074 SkAutoSMalloc<128*128> texels(glTexDesc.fUploadByteCount * maxTexels);
reed@google.comac10a2d2010-12-22 21:39:39 +00001075
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001076 // rowBytes is actual stride between rows in srcData
1077 // rowDataBytes is the actual amount of non-pad data in a row
1078 // and the stride used for uploading extraH rows.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001079 uint32_t rowDataBytes = desc.fWidth * glTexDesc.fUploadByteCount;
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 if (extraH) {
1081 uint8_t* lastRowStart = (uint8_t*) srcData +
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001082 (desc.fHeight - 1) * rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001083 uint8_t* extraRowStart = (uint8_t*)texels.get();
1084
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001085 for (int i = 0; i < extraH; ++i) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001086 memcpy(extraRowStart, lastRowStart, rowDataBytes);
1087 extraRowStart += rowDataBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001089 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, 0, desc.fHeight, desc.fWidth,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001090 extraH, glTexDesc.fUploadFormat, glTexDesc.fUploadType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 texels.get()));
1092 }
1093 if (extraW) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001094 uint8_t* edgeTexel = (uint8_t*)srcData + rowDataBytes - glTexDesc.fUploadByteCount;
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 uint8_t* extraTexel = (uint8_t*)texels.get();
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001096 for (int j = 0; j < desc.fHeight; ++j) {
1097 for (int i = 0; i < extraW; ++i) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001098 memcpy(extraTexel, edgeTexel, glTexDesc.fUploadByteCount);
1099 extraTexel += glTexDesc.fUploadByteCount;
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001101 edgeTexel += rowBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +00001102 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001103 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fWidth, 0, extraW,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001104 desc.fHeight, glTexDesc.fUploadFormat,
1105 glTexDesc.fUploadType, texels.get()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001106 }
1107 if (extraW && extraH) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001108 uint8_t* cornerTexel = (uint8_t*)srcData + desc.fHeight * rowBytes
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001109 - glTexDesc.fUploadByteCount;
reed@google.comac10a2d2010-12-22 21:39:39 +00001110 uint8_t* extraTexel = (uint8_t*)texels.get();
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +00001111 for (int i = 0; i < extraW*extraH; ++i) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001112 memcpy(extraTexel, cornerTexel, glTexDesc.fUploadByteCount);
1113 extraTexel += glTexDesc.fUploadByteCount;
reed@google.comac10a2d2010-12-22 21:39:39 +00001114 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001115 GR_GL(TexSubImage2D(GR_GL_TEXTURE_2D, 0, desc.fWidth, desc.fHeight,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001116 extraW, extraH, glTexDesc.fUploadFormat,
1117 glTexDesc.fUploadType, texels.get()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001118 }
1119
1120 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001121 GR_GL(TexImage2D(GR_GL_TEXTURE_2D, 0, internalFormat, glTexDesc.fAllocWidth,
1122 glTexDesc.fAllocHeight, 0, glTexDesc.fUploadFormat,
1123 glTexDesc.fUploadType, srcData));
bsalomon@google.comf987d1b2011-04-04 17:13:52 +00001124 GrGLRestoreResetRowLength();
reed@google.comac10a2d2010-12-22 21:39:39 +00001125 }
1126 }
1127
reed@google.comac10a2d2010-12-22 21:39:39 +00001128
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001129 GrGLRenderTarget::Desc glRTDesc;
1130 glRTDesc.fStencilRenderbufferID = 0;
1131 glRTDesc.fMSColorRenderbufferID = 0;
1132 glRTDesc.fRTFBOID = 0;
1133 glRTDesc.fTexFBOID = 0;
1134 glRTDesc.fOwnIDs = true;
1135 glRTDesc.fConfig = glTexDesc.fFormat;
1136 glRTDesc.fSampleCnt = samples;
1137
twiz@google.com0f31ca72011-03-18 17:38:11 +00001138 GrGLenum msColorRenderbufferFormat = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +00001139
1140 if (renderTarget) {
1141#if GR_COLLECT_STATS
1142 ++fStats.fRenderTargetCreateCnt;
1143#endif
1144 bool failed = true;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001145 GrGLenum status;
1146 GrGLint err;
reed@google.comac10a2d2010-12-22 21:39:39 +00001147
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001148 GR_GL(GenFramebuffers(1, &glRTDesc.fTexFBOID));
1149 GrAssert(glRTDesc.fTexFBOID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001150
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001151 // If we are using multisampling and we will create two FBOS We render
bsalomon@google.comd1e433532011-03-21 21:38:40 +00001152 // to one and then resolve to the texture bound to the other.
1153 if (samples > 1 && kNone_MSFBO != fMSFBOType) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001154 GR_GL(GenFramebuffers(1, &glRTDesc.fRTFBOID));
1155 GrAssert(0 != glRTDesc.fRTFBOID);
1156 GR_GL(GenRenderbuffers(1, &glRTDesc.fMSColorRenderbufferID));
1157 GrAssert(0 != glRTDesc.fMSColorRenderbufferID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001158 if (!fboInternalFormat(desc.fFormat, &msColorRenderbufferFormat)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001159 GR_GL(DeleteRenderbuffers(1, &glRTDesc.fMSColorRenderbufferID));
1160 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
1161 GR_GL(DeleteFramebuffers(1, &glRTDesc.fTexFBOID));
1162 GR_GL(DeleteFramebuffers(1, &glRTDesc.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001163 return return_null_texture();
1164 }
1165 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001166 glRTDesc.fRTFBOID = glRTDesc.fTexFBOID;
reed@google.comac10a2d2010-12-22 21:39:39 +00001167 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001168 if (!(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001169 GR_GL(GenRenderbuffers(1, &glRTDesc.fStencilRenderbufferID));
1170 GrAssert(0 != glRTDesc.fStencilRenderbufferID);
reed@google.comac10a2d2010-12-22 21:39:39 +00001171 }
1172
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001173 // someone suggested that some systems might require
bsalomon@google.com316f99232011-01-13 21:28:12 +00001174 // unbinding the texture before we call FramebufferTexture2D
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001175 // (seems unlikely)
twiz@google.com0f31ca72011-03-18 17:38:11 +00001176 GR_GL(BindTexture(GR_GL_TEXTURE_2D, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001177
twiz@google.com0f31ca72011-03-18 17:38:11 +00001178 err = ~GR_GL_NO_ERROR;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00001179
bsalomon@google.comfe676522011-06-17 18:12:21 +00001180 int stencilFmtCnt;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001181 if (glRTDesc.fStencilRenderbufferID) {
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001182 stencilFmtCnt = fStencilFormats.count();
bsalomon@google.comfe676522011-06-17 18:12:21 +00001183 } else {
1184 stencilFmtCnt = 1; // only 1 attempt when we don't need a stencil
1185 }
1186
1187 for (int i = 0; i < stencilFmtCnt; ++i) {
1188 // we start with the last stencil format that succeeded in hopes
1189 // that we won't go through this loop more than once after the
1190 // first (painful) stencil creation.
1191 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
1192
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001193 if (glRTDesc.fStencilRenderbufferID) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001194 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001195 glRTDesc.fStencilRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 if (samples > 1) {
twiz@google.com59a190b2011-03-14 21:23:01 +00001197 GR_GL_NO_ERR(RenderbufferStorageMultisample(
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001198 GR_GL_RENDERBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001199 samples,
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001200 fStencilFormats[sIdx].fEnum,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001201 glTexDesc.fAllocWidth,
1202 glTexDesc.fAllocHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +00001203 } else {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001204 GR_GL_NO_ERR(RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001205 fStencilFormats[sIdx].fEnum,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001206 glTexDesc.fAllocWidth,
1207 glTexDesc.fAllocHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +00001208 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00001209 err = GrGLGetGLInterface()->fGetError();
1210 if (err != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001211 continue;
1212 }
1213 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001214 if (glRTDesc.fRTFBOID != glRTDesc.fTexFBOID) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 GrAssert(samples > 1);
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001216 GR_GL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001217 glRTDesc.fMSColorRenderbufferID));
twiz@google.com59a190b2011-03-14 21:23:01 +00001218 GR_GL_NO_ERR(RenderbufferStorageMultisample(
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001219 GR_GL_RENDERBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001220 samples,
1221 msColorRenderbufferFormat,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001222 glTexDesc.fAllocWidth,
1223 glTexDesc.fAllocHeight));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001224 err = GrGLGetGLInterface()->fGetError();
1225 if (err != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 continue;
1227 }
1228 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001229 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRTDesc.fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001230
1231#if GR_COLLECT_STATS
1232 ++fStats.fRenderTargetChngCnt;
1233#endif
bsalomon@google.comd1e433532011-03-21 21:38:40 +00001234 GR_GL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1235 GR_GL_COLOR_ATTACHMENT0,
1236 GR_GL_TEXTURE_2D,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001237 glTexDesc.fTextureID, 0));
1238 if (glRTDesc.fRTFBOID != glRTDesc.fTexFBOID) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001239 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1240 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 continue;
1242 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001243 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRTDesc.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001244 #if GR_COLLECT_STATS
1245 ++fStats.fRenderTargetChngCnt;
1246 #endif
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001247 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1248 GR_GL_COLOR_ATTACHMENT0,
1249 GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001250 glRTDesc.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001251
1252 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001253 if (glRTDesc.fStencilRenderbufferID) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 // bind the stencil to rt fbo if present, othewise the tex fbo
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001255 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1256 GR_GL_STENCIL_ATTACHMENT,
1257 GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001258 glRTDesc.fStencilRenderbufferID));
bsalomon@google.com9283b582011-04-08 19:00:04 +00001259 // if it is a packed format bind to depth also, otherwise
1260 // we may get an unsupported fbo completeness result
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001261 if (fStencilFormats[sIdx].fPacked) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001262 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1263 GR_GL_DEPTH_ATTACHMENT,
1264 GR_GL_RENDERBUFFER,
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001265 glRTDesc.fStencilRenderbufferID));
bsalomon@google.com9283b582011-04-08 19:00:04 +00001266 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 }
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001268 status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
reed@google.comac10a2d2010-12-22 21:39:39 +00001269
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001270 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001271 // undo the depth bind
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001272 if (glRTDesc.fStencilRenderbufferID &&
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001273 fStencilFormats[sIdx].fPacked) {
bsalomon@google.com9283b582011-04-08 19:00:04 +00001274 GR_GL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1275 GR_GL_DEPTH_ATTACHMENT,
1276 GR_GL_RENDERBUFFER,
1277 0));
twiz@google.comb65e0cb2011-03-18 20:41:44 +00001278 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 continue;
1280 }
1281 // we're successful!
1282 failed = false;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001283 if (glRTDesc.fStencilRenderbufferID) {
bsalomon@google.comfe676522011-06-17 18:12:21 +00001284 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +00001285 if (gUNKNOWN_BITCOUNT == fStencilFormats[sIdx].fBits) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001286 GR_GL_GetIntegerv(GR_GL_STENCIL_BITS, (GrGLint*)&glRTDesc.fStencilBits);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001287 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001288 glRTDesc.fStencilBits = fStencilFormats[sIdx].fBits;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001289 }
1290 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 break;
1292 }
1293 if (failed) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001294 if (glRTDesc.fStencilRenderbufferID) {
1295 GR_GL(DeleteRenderbuffers(1, &glRTDesc.fStencilRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001297 if (glRTDesc.fMSColorRenderbufferID) {
1298 GR_GL(DeleteRenderbuffers(1, &glRTDesc.fMSColorRenderbufferID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001300 if (glRTDesc.fRTFBOID != glRTDesc.fTexFBOID) {
1301 GR_GL(DeleteFramebuffers(1, &glRTDesc.fRTFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001303 if (glRTDesc.fTexFBOID) {
1304 GR_GL(DeleteFramebuffers(1, &glRTDesc.fTexFBOID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001305 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001306 GR_GL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 return return_null_texture();
1308 }
1309 }
1310#ifdef TRACE_TEXTURE_CREATION
1311 GrPrintf("--- new texture [%d] size=(%d %d) bpp=%d\n",
1312 tex->fTextureID, width, height, tex->fUploadByteCount);
1313#endif
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001314 if (renderTarget) {
1315 GrGLTexture* tex = new GrGLTexture(this, glTexDesc,
1316 glRTDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 GrRenderTarget* rt = tex->asRenderTarget();
1318 // We've messed with FBO state but may not have set the correct viewport
1319 // so just dirty the rendertarget state to force a resend.
1320 fHWDrawState.fRenderTarget = NULL;
1321
1322 // clear the new stencil buffer if we have one
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001323 if (!(desc.fFlags & kNoStencil_GrTextureFlagBit)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 GrRenderTarget* rtSave = fCurrDrawState.fRenderTarget;
1325 fCurrDrawState.fRenderTarget = rt;
bsalomon@google.com398109c2011-04-14 18:40:27 +00001326 this->clearStencil(0, ~0);
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 fCurrDrawState.fRenderTarget = rtSave;
1328 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001329 return tex;
1330 } else {
1331 return new GrGLTexture(this, glTexDesc, DEFAULT_PARAMS);
reed@google.comac10a2d2010-12-22 21:39:39 +00001332 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001333}
1334
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001335GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001336 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 GR_GL(GenBuffers(1, &id));
1338 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001339 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001340 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 GrGLClearErr();
1342 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001343 GR_GL_NO_ERR(BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1344 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1345 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 GR_GL(DeleteBuffers(1, &id));
1347 // deleting bound buffer does implicit bind to 0
1348 fHWGeometryState.fVertexBuffer = NULL;
1349 return NULL;
1350 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001351 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 size, dynamic);
1353 fHWGeometryState.fVertexBuffer = vertexBuffer;
1354 return vertexBuffer;
1355 }
1356 return NULL;
1357}
1358
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001359GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001360 GrGLuint id;
reed@google.comac10a2d2010-12-22 21:39:39 +00001361 GR_GL(GenBuffers(1, &id));
1362 if (id) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001363 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001364 GrGLClearErr();
1365 // make sure driver can allocate memory for this buffer
twiz@google.com0f31ca72011-03-18 17:38:11 +00001366 GR_GL_NO_ERR(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1367 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1368 if (GrGLGetGLInterface()->fGetError() != GR_GL_NO_ERROR) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001369 GR_GL(DeleteBuffers(1, &id));
1370 // deleting bound buffer does implicit bind to 0
1371 fHWGeometryState.fIndexBuffer = NULL;
1372 return NULL;
1373 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001374 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001375 size, dynamic);
1376 fHWGeometryState.fIndexBuffer = indexBuffer;
1377 return indexBuffer;
1378 }
1379 return NULL;
1380}
1381
reed@google.comac10a2d2010-12-22 21:39:39 +00001382void GrGpuGL::flushScissor(const GrIRect* rect) {
1383 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001384 const GrGLIRect& vp =
bsalomon@google.comd302f142011-03-03 13:54:13 +00001385 ((GrGLRenderTarget*)fCurrDrawState.fRenderTarget)->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001386
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001387 GrGLIRect scissor;
1388 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001389 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001390 rect->width(), rect->height());
1391 if (scissor.contains(vp)) {
1392 rect = NULL;
1393 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001394 }
1395
1396 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001397 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001398 scissor.pushToGLScissor();
reed@google.comac10a2d2010-12-22 21:39:39 +00001399 fHWBounds.fScissorRect = scissor;
1400 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001401 if (!fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001402 GR_GL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001403 fHWBounds.fScissorEnabled = true;
1404 }
1405 } else {
1406 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001407 GR_GL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 fHWBounds.fScissorEnabled = false;
1409 }
1410 }
1411}
1412
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001413void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001414 if (NULL == fCurrDrawState.fRenderTarget) {
1415 return;
1416 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001417 GrIRect r;
1418 if (NULL != rect) {
1419 // flushScissor expects rect to be clipped to the target.
1420 r = *rect;
reed@google.com20efde72011-05-09 17:00:02 +00001421 GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
1422 fCurrDrawState.fRenderTarget->height());
1423 if (r.intersect(rtRect)) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001424 rect = &r;
1425 } else {
1426 return;
1427 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001428 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001429 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001430 this->flushScissor(rect);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001431 GR_GL(ColorMask(GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE,GR_GL_TRUE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001432 fHWDrawState.fFlagBits &= ~kNoColorWrites_StateBit;
reed@google.comac10a2d2010-12-22 21:39:39 +00001433 GR_GL(ClearColor(GrColorUnpackR(color)/255.f,
1434 GrColorUnpackG(color)/255.f,
1435 GrColorUnpackB(color)/255.f,
1436 GrColorUnpackA(color)/255.f));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001437 GR_GL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001438}
1439
bsalomon@google.com398109c2011-04-14 18:40:27 +00001440void GrGpuGL::clearStencil(uint32_t value, uint32_t mask) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001441 if (NULL == fCurrDrawState.fRenderTarget) {
1442 return;
1443 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001444
1445 this->flushRenderTarget(&GrIRect::EmptyIRect());
1446
reed@google.comac10a2d2010-12-22 21:39:39 +00001447 if (fHWBounds.fScissorEnabled) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001448 GR_GL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001449 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001450 }
1451 GR_GL(StencilMask(mask));
1452 GR_GL(ClearStencil(value));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001453 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001454 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001455}
1456
bsalomon@google.com398109c2011-04-14 18:40:27 +00001457void GrGpuGL::clearStencilClip(const GrIRect& rect) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001458 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001459#if 0
twiz@google.com0f31ca72011-03-18 17:38:11 +00001460 GrGLint stencilBitCount = fCurrDrawState.fRenderTarget->stencilBits();
reed@google.comac10a2d2010-12-22 21:39:39 +00001461 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001462 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001463#else
1464 // we could just clear the clip bit but when we go through
1465 // angle a partial stencil mask will cause clears to be
1466 // turned into draws. Our contract on GrDrawTarget says that
1467 // changing the clip between stencil passes may or may not
1468 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001469 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001470#endif
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001471 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.comd302f142011-03-03 13:54:13 +00001472 flushScissor(&rect);
1473 GR_GL(StencilMask(clipStencilMask));
1474 GR_GL(ClearStencil(0));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001475 GR_GL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001476 fHWDrawState.fStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001477}
1478
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001479void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001480 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001481}
1482
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001483bool GrGpuGL::onReadPixels(GrRenderTarget* target,
1484 int left, int top, int width, int height,
1485 GrPixelConfig config, void* buffer) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001486 GrGLenum internalFormat; // we don't use this for glReadPixels
1487 GrGLenum format;
1488 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001489 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1490 return false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001491 }
1492 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
1493 GrAutoTPtrValueRestore<GrRenderTarget*> autoTargetRestore;
1494 switch (tgt->getResolveType()) {
1495 case GrGLRenderTarget::kCantResolve_ResolveType:
1496 return false;
1497 case GrGLRenderTarget::kAutoResolves_ResolveType:
1498 autoTargetRestore.save(&fCurrDrawState.fRenderTarget);
1499 fCurrDrawState.fRenderTarget = target;
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001500 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001501 break;
1502 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001503 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001504 // we don't track the state of the READ FBO ID.
1505 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
1506 break;
1507 default:
1508 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001509 }
1510
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001511 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001512
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001513 // the read rect is viewport-relative
1514 GrGLIRect readRect;
1515 readRect.setRelativeTo(glvp, left, top, width, height);
1516 GR_GL(ReadPixels(readRect.fLeft, readRect.fBottom,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001517 readRect.fWidth, readRect.fHeight,
bsalomon@google.com316f99232011-01-13 21:28:12 +00001518 format, type, buffer));
reed@google.comac10a2d2010-12-22 21:39:39 +00001519
1520 // now reverse the order of the rows, since GL's are bottom-to-top, but our
1521 // API presents top-to-bottom
1522 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001523 size_t stride = width * GrBytesPerPixel(config);
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001524 SkAutoMalloc rowStorage(stride);
reed@google.comac10a2d2010-12-22 21:39:39 +00001525 void* tmp = rowStorage.get();
1526
1527 const int halfY = height >> 1;
1528 char* top = reinterpret_cast<char*>(buffer);
1529 char* bottom = top + (height - 1) * stride;
1530 for (int y = 0; y < halfY; y++) {
1531 memcpy(tmp, top, stride);
1532 memcpy(top, bottom, stride);
1533 memcpy(bottom, tmp, stride);
1534 top += stride;
1535 bottom -= stride;
1536 }
1537 }
1538 return true;
1539}
1540
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001541void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001542
1543 GrAssert(NULL != fCurrDrawState.fRenderTarget);
1544
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001545 GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +00001546 if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001547 GR_GL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001548 #if GR_COLLECT_STATS
1549 ++fStats.fRenderTargetChngCnt;
1550 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001551 #if GR_DEBUG
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001552 GrGLenum status = GR_GL(CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1553 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001554 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001555 }
1556 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001557 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001558 fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001559 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001560 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001561 vp.pushToGLViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001562 fHWBounds.fViewportRect = vp;
1563 }
1564 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001565 if (NULL == bound || !bound->isEmpty()) {
1566 rt->flagAsNeedingResolve(bound);
1567 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001568}
1569
twiz@google.com0f31ca72011-03-18 17:38:11 +00001570GrGLenum gPrimitiveType2GLMode[] = {
1571 GR_GL_TRIANGLES,
1572 GR_GL_TRIANGLE_STRIP,
1573 GR_GL_TRIANGLE_FAN,
1574 GR_GL_POINTS,
1575 GR_GL_LINES,
1576 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001577};
1578
bsalomon@google.comd302f142011-03-03 13:54:13 +00001579#define SWAP_PER_DRAW 0
1580
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001581#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001582 #if GR_MAC_BUILD
1583 #include <AGL/agl.h>
1584 #elif GR_WIN32_BUILD
1585 void SwapBuf() {
1586 DWORD procID = GetCurrentProcessId();
1587 HWND hwnd = GetTopWindow(GetDesktopWindow());
1588 while(hwnd) {
1589 DWORD wndProcID = 0;
1590 GetWindowThreadProcessId(hwnd, &wndProcID);
1591 if(wndProcID == procID) {
1592 SwapBuffers(GetDC(hwnd));
1593 }
1594 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1595 }
1596 }
1597 #endif
1598#endif
1599
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001600void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1601 uint32_t startVertex,
1602 uint32_t startIndex,
1603 uint32_t vertexCount,
1604 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001605 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1606
twiz@google.com0f31ca72011-03-18 17:38:11 +00001607 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001608
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001609 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1610 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1611
1612 // our setupGeometry better have adjusted this to zero since
1613 // DrawElements always draws from the begining of the arrays for idx 0.
1614 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001615
1616 GR_GL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
twiz@google.com0f31ca72011-03-18 17:38:11 +00001617 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001618#if SWAP_PER_DRAW
1619 glFlush();
1620 #if GR_MAC_BUILD
1621 aglSwapBuffers(aglGetCurrentContext());
1622 int set_a_break_pt_here = 9;
1623 aglSwapBuffers(aglGetCurrentContext());
1624 #elif GR_WIN32_BUILD
1625 SwapBuf();
1626 int set_a_break_pt_here = 9;
1627 SwapBuf();
1628 #endif
1629#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001630}
1631
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001632void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1633 uint32_t startVertex,
1634 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001635 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1636
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001637 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1638
1639 // our setupGeometry better have adjusted this to zero.
1640 // DrawElements doesn't take an offset so we always adjus the startVertex.
1641 GrAssert(0 == startVertex);
1642
1643 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1644 // account for startVertex in the DrawElements case. So we always
1645 // rely on setupGeometry to have accounted for startVertex.
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 GR_GL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001647#if SWAP_PER_DRAW
1648 glFlush();
1649 #if GR_MAC_BUILD
1650 aglSwapBuffers(aglGetCurrentContext());
1651 int set_a_break_pt_here = 9;
1652 aglSwapBuffers(aglGetCurrentContext());
1653 #elif GR_WIN32_BUILD
1654 SwapBuf();
1655 int set_a_break_pt_here = 9;
1656 SwapBuf();
1657 #endif
1658#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001659}
1660
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001661void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001662
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001663 if (rt->needsResolve()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001664 GrAssert(kNone_MSFBO != fMSFBOType);
1665 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001666 GR_GL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001667 rt->renderFBOID()));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001668 GR_GL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
reed@google.comac10a2d2010-12-22 21:39:39 +00001669 rt->textureFBOID()));
1670 #if GR_COLLECT_STATS
1671 ++fStats.fRenderTargetChngCnt;
1672 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001673 // make sure we go through flushRenderTarget() since we've modified
1674 // the bound DRAW FBO ID.
reed@google.comac10a2d2010-12-22 21:39:39 +00001675 fHWDrawState.fRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001676 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001677 const GrIRect dirtyRect = rt->getResolveRect();
1678 GrGLIRect r;
1679 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1680 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001681
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001682 if (kAppleES_MSFBO == fMSFBOType) {
1683 // Apple's extension uses the scissor as the blit bounds.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001684 GR_GL(Enable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001685 GR_GL(Scissor(r.fLeft, r.fBottom,
1686 r.fWidth, r.fHeight));
twiz@google.com59a190b2011-03-14 21:23:01 +00001687 GR_GL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001688 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001689 fHWBounds.fScissorEnabled = true;
1690 } else {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001691 if (kDesktopARB_MSFBO != fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001692 // this respects the scissor during the blit, so disable it.
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001693 GrAssert(kDesktopEXT_MSFBO == fMSFBOType);
1694 flushScissor(NULL);
1695 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001696 int right = r.fLeft + r.fWidth;
1697 int top = r.fBottom + r.fHeight;
1698 GR_GL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1699 r.fLeft, r.fBottom, right, top,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001700 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001701 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001702 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 }
1704}
1705
twiz@google.com0f31ca72011-03-18 17:38:11 +00001706static const GrGLenum grToGLStencilFunc[] = {
1707 GR_GL_ALWAYS, // kAlways_StencilFunc
1708 GR_GL_NEVER, // kNever_StencilFunc
1709 GR_GL_GREATER, // kGreater_StencilFunc
1710 GR_GL_GEQUAL, // kGEqual_StencilFunc
1711 GR_GL_LESS, // kLess_StencilFunc
1712 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1713 GR_GL_EQUAL, // kEqual_StencilFunc,
1714 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001715};
1716GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1717GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1718GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1719GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1720GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1721GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1722GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1723GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1724GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1725
twiz@google.com0f31ca72011-03-18 17:38:11 +00001726static const GrGLenum grToGLStencilOp[] = {
1727 GR_GL_KEEP, // kKeep_StencilOp
1728 GR_GL_REPLACE, // kReplace_StencilOp
1729 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1730 GR_GL_INCR, // kIncClamp_StencilOp
1731 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1732 GR_GL_DECR, // kDecClamp_StencilOp
1733 GR_GL_ZERO, // kZero_StencilOp
1734 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001735};
1736GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1737GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1738GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1739GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1740GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1741GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1742GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1743GR_STATIC_ASSERT(6 == kZero_StencilOp);
1744GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1745
reed@google.comac10a2d2010-12-22 21:39:39 +00001746void GrGpuGL::flushStencil() {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001747 const GrStencilSettings* settings = &fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001748
1749 // use stencil for clipping if clipping is enabled and the clip
1750 // has been written into the stencil.
1751 bool stencilClip = fClipState.fClipInStencil &&
1752 (kClip_StateBit & fCurrDrawState.fFlagBits);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001753 bool stencilChange = fHWStencilClip != stencilClip ||
1754 fHWDrawState.fStencilSettings != *settings ||
1755 ((fHWDrawState.fFlagBits & kModifyStencilClip_StateBit) !=
1756 (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001757
1758 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001759
bsalomon@google.comd302f142011-03-03 13:54:13 +00001760 // we can't simultaneously perform stencil-clipping and modify the stencil clip
1761 GrAssert(!stencilClip || !(fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit));
reed@google.comac10a2d2010-12-22 21:39:39 +00001762
bsalomon@google.comd302f142011-03-03 13:54:13 +00001763 if (settings->isDisabled()) {
1764 if (stencilClip) {
1765 settings = &gClipStencilSettings;
1766 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001767 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001768
1769 if (settings->isDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001770 GR_GL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001771 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001772 GR_GL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001773 #if GR_DEBUG
1774 if (!fStencilWrapOpsSupport) {
1775 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1776 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1777 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1778 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1779 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1780 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1781 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1782 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1783 }
1784 #endif
1785 int stencilBits = fCurrDrawState.fRenderTarget->stencilBits();
1786 GrAssert(stencilBits ||
1787 (GrStencilSettings::gDisabled ==
1788 fCurrDrawState.fStencilSettings));
twiz@google.com0f31ca72011-03-18 17:38:11 +00001789 GrGLuint clipStencilMask = 1 << (stencilBits - 1);
1790 GrGLuint userStencilMask = clipStencilMask - 1;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001791
1792 unsigned int frontRef = settings->fFrontFuncRef;
1793 unsigned int frontMask = settings->fFrontFuncMask;
1794 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001795 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001796
1797 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1798
1799 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1800 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1801 } else {
1802 frontFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fFrontFunc)];
1803
1804 ConvertStencilFuncAndMask(settings->fFrontFunc,
1805 stencilClip,
1806 clipStencilMask,
1807 userStencilMask,
1808 &frontRef,
1809 &frontMask);
1810 frontWriteMask &= userStencilMask;
1811 }
1812 GrAssert(settings->fFrontFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001813 (unsigned) settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001814 GrAssert(settings->fFrontPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001815 (unsigned) settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001816 GrAssert(settings->fBackFailOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001817 (unsigned) settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001818 GrAssert(settings->fBackPassOp >= 0 &&
bsalomon@google.com2022c942011-03-30 21:43:04 +00001819 (unsigned) settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001820 if (fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001821 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822
1823 unsigned int backRef = settings->fBackFuncRef;
1824 unsigned int backMask = settings->fBackFuncMask;
1825 unsigned int backWriteMask = settings->fBackWriteMask;
1826
1827
1828 if (fCurrDrawState.fFlagBits & kModifyStencilClip_StateBit) {
1829 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1830 backFunc = grToGLStencilFunc[settings->fBackFunc];
1831 } else {
1832 backFunc = grToGLStencilFunc[ConvertStencilFunc(stencilClip, settings->fBackFunc)];
1833 ConvertStencilFuncAndMask(settings->fBackFunc,
1834 stencilClip,
1835 clipStencilMask,
1836 userStencilMask,
1837 &backRef,
1838 &backMask);
1839 backWriteMask &= userStencilMask;
1840 }
1841
twiz@google.com0f31ca72011-03-18 17:38:11 +00001842 GR_GL(StencilFuncSeparate(GR_GL_FRONT, frontFunc, frontRef, frontMask));
1843 GR_GL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1844 GR_GL(StencilFuncSeparate(GR_GL_BACK, backFunc, backRef, backMask));
1845 GR_GL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1846 GR_GL(StencilOpSeparate(GR_GL_FRONT, grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001847 grToGLStencilOp[settings->fFrontPassOp],
1848 grToGLStencilOp[settings->fFrontPassOp]));
1849
twiz@google.com0f31ca72011-03-18 17:38:11 +00001850 GR_GL(StencilOpSeparate(GR_GL_BACK, grToGLStencilOp[settings->fBackFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001851 grToGLStencilOp[settings->fBackPassOp],
1852 grToGLStencilOp[settings->fBackPassOp]));
1853 } else {
1854 GR_GL(StencilFunc(frontFunc, frontRef, frontMask));
1855 GR_GL(StencilMask(frontWriteMask));
1856 GR_GL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
1857 grToGLStencilOp[settings->fFrontPassOp],
1858 grToGLStencilOp[settings->fFrontPassOp]));
1859 }
1860 }
1861 fHWDrawState.fStencilSettings = fCurrDrawState.fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001862 fHWStencilClip = stencilClip;
1863 }
1864}
1865
bsalomon@google.com0650e812011-04-08 18:07:53 +00001866bool GrGpuGL::useSmoothLines() {
1867 // there is a conflict between using smooth lines and our use of
1868 // premultiplied alpha. Smooth lines tweak the incoming alpha value
1869 // but not in a premul-alpha way. So we only use them when our alpha
1870 // is 0xff.
1871
1872 // TODO: write a smarter line frag shader.
1873
1874 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
1875 canDisableBlend();
1876}
1877
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001878void GrGpuGL::flushAAState(GrPrimitiveType type) {
1879 if (GR_GL_SUPPORT_DESKTOP) {
1880 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1881 // smooth lines.
1882
1883 // we prefer smooth lines over multisampled lines
1884 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001885 if (GrIsPrimTypeLines(type)) {
1886 bool smooth = useSmoothLines();
1887 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001888 GR_GL(Enable(GR_GL_LINE_SMOOTH));
1889 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001890 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001891 GR_GL(Disable(GR_GL_LINE_SMOOTH));
1892 fHWAAState.fSmoothLineEnabled = false;
1893 }
1894 if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1895 fHWAAState.fMSAAEnabled) {
1896 GR_GL(Disable(GR_GL_MULTISAMPLE));
1897 fHWAAState.fMSAAEnabled = false;
1898 }
1899 } else if (fCurrDrawState.fRenderTarget->isMultisampled() &&
1900 !!(kAntialias_StateBit & fCurrDrawState.fFlagBits) !=
1901 fHWAAState.fMSAAEnabled) {
1902 if (fHWAAState.fMSAAEnabled) {
1903 GR_GL(Disable(GR_GL_MULTISAMPLE));
1904 fHWAAState.fMSAAEnabled = false;
1905 } else {
1906 GR_GL(Enable(GR_GL_MULTISAMPLE));
1907 fHWAAState.fMSAAEnabled = true;
1908 }
1909 }
1910 }
1911}
1912
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001913void GrGpuGL::flushBlend(GrPrimitiveType type,
1914 GrBlendCoeff srcCoeff,
1915 GrBlendCoeff dstCoeff) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001916 if (GrIsPrimTypeLines(type) && useSmoothLines()) {
1917 if (fHWBlendDisabled) {
1918 GR_GL(Enable(GR_GL_BLEND));
1919 fHWBlendDisabled = false;
1920 }
1921 if (kSA_BlendCoeff != fHWDrawState.fSrcBlend ||
1922 kISA_BlendCoeff != fHWDrawState.fDstBlend) {
1923 GR_GL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1924 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
1925 fHWDrawState.fSrcBlend = kSA_BlendCoeff;
1926 fHWDrawState.fDstBlend = kISA_BlendCoeff;
1927 }
1928 } else {
1929 bool blendOff = canDisableBlend();
1930 if (fHWBlendDisabled != blendOff) {
1931 if (blendOff) {
1932 GR_GL(Disable(GR_GL_BLEND));
1933 } else {
1934 GR_GL(Enable(GR_GL_BLEND));
1935 }
1936 fHWBlendDisabled = blendOff;
1937 }
1938 if (!blendOff) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001939 if (fHWDrawState.fSrcBlend != srcCoeff ||
1940 fHWDrawState.fDstBlend != dstCoeff) {
1941 GR_GL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1942 gXfermodeCoeff2Blend[dstCoeff]));
1943 fHWDrawState.fSrcBlend = srcCoeff;
1944 fHWDrawState.fDstBlend = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001945 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001946 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1947 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com0650e812011-04-08 18:07:53 +00001948 fHWDrawState.fBlendConstant != fCurrDrawState.fBlendConstant) {
1949
1950 float c[] = {
1951 GrColorUnpackR(fCurrDrawState.fBlendConstant) / 255.f,
1952 GrColorUnpackG(fCurrDrawState.fBlendConstant) / 255.f,
1953 GrColorUnpackB(fCurrDrawState.fBlendConstant) / 255.f,
1954 GrColorUnpackA(fCurrDrawState.fBlendConstant) / 255.f
1955 };
1956 GR_GL(BlendColor(c[0], c[1], c[2], c[3]));
1957 fHWDrawState.fBlendConstant = fCurrDrawState.fBlendConstant;
1958 }
1959 }
1960 }
1961}
1962
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001963static unsigned grToGLFilter(GrSamplerState::Filter filter) {
1964 switch (filter) {
1965 case GrSamplerState::kBilinear_Filter:
1966 case GrSamplerState::k4x4Downsample_Filter:
1967 return GR_GL_LINEAR;
1968 case GrSamplerState::kNearest_Filter:
1969 case GrSamplerState::kConvolution_Filter:
1970 return GR_GL_NEAREST;
1971 default:
1972 GrAssert(!"Unknown filter type");
1973 return GR_GL_LINEAR;
1974 }
1975}
1976
bsalomon@google.comffca4002011-02-22 20:34:01 +00001977bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001978
1979 // GrGpu::setupClipAndFlushState should have already checked this
1980 // and bailed if not true.
1981 GrAssert(NULL != fCurrDrawState.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00001982
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001983 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001984 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001985 if (this->isStageEnabled(s)) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001986 GrGLTexture* nextTexture = (GrGLTexture*)fCurrDrawState.fTextures[s];
reed@google.comac10a2d2010-12-22 21:39:39 +00001987
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001988 // true for now, but maybe not with GrEffect.
1989 GrAssert(NULL != nextTexture);
1990 // if we created a rt/tex and rendered to it without using a
1991 // texture and now we're texuring from the rt it will still be
1992 // the last bound texture, but it needs resolving. So keep this
1993 // out of the "last != next" check.
1994 GrGLRenderTarget* texRT =
1995 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1996 if (NULL != texRT) {
1997 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001998 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001999
2000 if (fHWDrawState.fTextures[s] != nextTexture) {
2001 setTextureUnit(s);
2002 GR_GL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
2003 #if GR_COLLECT_STATS
2004 ++fStats.fTextureChngCnt;
2005 #endif
2006 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2007 fHWDrawState.fTextures[s] = nextTexture;
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002008 // The texture matrix has to compensate for texture width/height
2009 // and NPOT-embedded-in-POT
2010 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002011 }
2012
2013 const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[s];
2014 const GrGLTexture::TexParams& oldTexParams =
2015 nextTexture->getTexParams();
2016 GrGLTexture::TexParams newTexParams;
2017
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002018 newTexParams.fFilter = grToGLFilter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002019
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002020 newTexParams.fWrapS =
2021 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapX()];
2022 newTexParams.fWrapT =
2023 GrGLTexture::WrapMode2GLWrap()[sampler.getWrapY()];
2024
2025 if (newTexParams.fFilter != oldTexParams.fFilter) {
2026 setTextureUnit(s);
2027 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2028 GR_GL_TEXTURE_MAG_FILTER,
2029 newTexParams.fFilter));
2030 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2031 GR_GL_TEXTURE_MIN_FILTER,
2032 newTexParams.fFilter));
2033 }
2034 if (newTexParams.fWrapS != oldTexParams.fWrapS) {
2035 setTextureUnit(s);
2036 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2037 GR_GL_TEXTURE_WRAP_S,
2038 newTexParams.fWrapS));
2039 }
2040 if (newTexParams.fWrapT != oldTexParams.fWrapT) {
2041 setTextureUnit(s);
2042 GR_GL(TexParameteri(GR_GL_TEXTURE_2D,
2043 GR_GL_TEXTURE_WRAP_T,
2044 newTexParams.fWrapT));
2045 }
2046 nextTexture->setTexParams(newTexParams);
reed@google.comac10a2d2010-12-22 21:39:39 +00002047 }
2048 }
2049
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002050 GrIRect* rect = NULL;
2051 GrIRect clipBounds;
2052 if ((fCurrDrawState.fFlagBits & kClip_StateBit) &&
2053 fClip.hasConservativeBounds()) {
2054 fClip.getConservativeBounds().roundOut(&clipBounds);
2055 rect = &clipBounds;
2056 }
2057 this->flushRenderTarget(rect);
2058 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002059
reed@google.comac10a2d2010-12-22 21:39:39 +00002060 if ((fCurrDrawState.fFlagBits & kDither_StateBit) !=
2061 (fHWDrawState.fFlagBits & kDither_StateBit)) {
2062 if (fCurrDrawState.fFlagBits & kDither_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002063 GR_GL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002064 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002065 GR_GL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002066 }
2067 }
2068
bsalomon@google.comd302f142011-03-03 13:54:13 +00002069 if ((fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) !=
2070 (fHWDrawState.fFlagBits & kNoColorWrites_StateBit)) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002071 GrGLenum mask;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002072 if (fCurrDrawState.fFlagBits & kNoColorWrites_StateBit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002073 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002074 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002075 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002076 }
2077 GR_GL(ColorMask(mask, mask, mask, mask));
2078 }
2079
bsalomon@google.comd302f142011-03-03 13:54:13 +00002080 if (fHWDrawState.fDrawFace != fCurrDrawState.fDrawFace) {
2081 switch (fCurrDrawState.fDrawFace) {
2082 case kCCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002083 GR_GL(Enable(GR_GL_CULL_FACE));
2084 GR_GL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002085 break;
2086 case kCW_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002087 GR_GL(Enable(GR_GL_CULL_FACE));
2088 GR_GL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002089 break;
2090 case kBoth_DrawFace:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002091 GR_GL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002092 break;
2093 default:
2094 GrCrash("Unknown draw face.");
2095 }
2096 fHWDrawState.fDrawFace = fCurrDrawState.fDrawFace;
2097 }
2098
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002099#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002100 // check for circular rendering
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002101 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002102 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002103 NULL == fCurrDrawState.fRenderTarget ||
2104 NULL == fCurrDrawState.fTextures[s] ||
bsalomon@google.com316f99232011-01-13 21:28:12 +00002105 fCurrDrawState.fTextures[s]->asRenderTarget() !=
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002106 fCurrDrawState.fRenderTarget);
2107 }
2108#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002109
reed@google.comac10a2d2010-12-22 21:39:39 +00002110 flushStencil();
2111
bsalomon@google.comd302f142011-03-03 13:54:13 +00002112 // flushStencil may look at the private state bits, so keep it before this.
reed@google.comac10a2d2010-12-22 21:39:39 +00002113 fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002114 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002115}
2116
2117void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002118 if (fHWGeometryState.fVertexBuffer != buffer) {
2119 fHWGeometryState.fArrayPtrsDirty = true;
2120 fHWGeometryState.fVertexBuffer = buffer;
2121 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002122}
2123
2124void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002125 if (fHWGeometryState.fVertexBuffer == buffer) {
2126 // deleting bound buffer does implied bind to 0
2127 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002128 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002129 }
2130}
2131
2132void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002133 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002134}
2135
2136void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002137 if (fHWGeometryState.fIndexBuffer == buffer) {
2138 // deleting bound buffer does implied bind to 0
2139 fHWGeometryState.fIndexBuffer = NULL;
2140 }
2141}
2142
reed@google.comac10a2d2010-12-22 21:39:39 +00002143void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2144 GrAssert(NULL != renderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +00002145 if (fCurrDrawState.fRenderTarget == renderTarget) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002146 fCurrDrawState.fRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002147 }
2148 if (fHWDrawState.fRenderTarget == renderTarget) {
2149 fHWDrawState.fRenderTarget = NULL;
2150 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002151}
2152
2153void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002154 for (int s = 0; s < kNumStages; ++s) {
2155 if (fCurrDrawState.fTextures[s] == texture) {
2156 fCurrDrawState.fTextures[s] = NULL;
2157 }
2158 if (fHWDrawState.fTextures[s] == texture) {
2159 // deleting bound texture does implied bind to 0
2160 fHWDrawState.fTextures[s] = NULL;
2161 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002162 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002163}
2164
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002165bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002166 GrGLenum* internalFormat,
2167 GrGLenum* format,
2168 GrGLenum* type) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002169 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002170 case kRGBA_8888_GrPixelConfig:
2171 case kRGBX_8888_GrPixelConfig: // todo: can we tell it our X?
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +00002172 *format = GR_GL_32BPP_COLOR_FORMAT;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002173 if (GR_GL_SUPPORT_ES) {
2174 // according to GL_EXT_texture_format_BGRA8888 the *internal*
2175 // format for a BGRA is BGRA not RGBA (as on desktop)
2176 *internalFormat = GR_GL_32BPP_COLOR_FORMAT;
2177 } else {
2178 *internalFormat = GR_GL_RGBA;
2179 }
twiz@google.com0f31ca72011-03-18 17:38:11 +00002180 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002181 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002182 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002183 *format = GR_GL_RGB;
2184 *internalFormat = GR_GL_RGB;
2185 *type = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002186 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002187 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002188 *format = GR_GL_RGBA;
2189 *internalFormat = GR_GL_RGBA;
2190 *type = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002191 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002192 case kIndex_8_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002193 if (this->supports8BitPalette()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002194 *format = GR_GL_PALETTE8_RGBA8;
2195 *internalFormat = GR_GL_PALETTE8_RGBA8;
twiz@google.com0f31ca72011-03-18 17:38:11 +00002196 *type = GR_GL_UNSIGNED_BYTE; // unused I think
reed@google.comac10a2d2010-12-22 21:39:39 +00002197 } else {
2198 return false;
2199 }
2200 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002201 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002202 *format = GR_GL_ALPHA;
2203 *internalFormat = GR_GL_ALPHA;
2204 *type = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002205 break;
2206 default:
2207 return false;
2208 }
2209 return true;
2210}
2211
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002212void GrGpuGL::setTextureUnit(int unit) {
2213 GrAssert(unit >= 0 && unit < kNumStages);
2214 if (fActiveTextureUnitIdx != unit) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002215 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002216 fActiveTextureUnitIdx = unit;
2217 }
2218}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002219
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002220void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002221 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
2222 GR_GL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002223 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2224 }
2225}
2226
reed@google.comac10a2d2010-12-22 21:39:39 +00002227/* On ES the internalFormat and format must match for TexImage and we use
2228 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2229 decide the internalFormat. However, on ES internalFormat for
2230 RenderBufferStorage* has to be a specific format (not a base format like
2231 GL_RGBA).
2232 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002233bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002234 switch (config) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002235 case kRGBA_8888_GrPixelConfig:
2236 case kRGBX_8888_GrPixelConfig:
reed@google.comac10a2d2010-12-22 21:39:39 +00002237 if (fRGBA8Renderbuffer) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002238 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002239 return true;
2240 } else {
2241 return false;
2242 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002243 case kRGB_565_GrPixelConfig:
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002244 GrAssert(GR_GL_SUPPORT_ES); // ES2 supports 565. ES1 supports it
2245 // with FBO extension desktop GL has
2246 // no such internal format
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002247 *format = GR_GL_RGB565;
reed@google.comac10a2d2010-12-22 21:39:39 +00002248 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002249 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002250 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002251 return true;
2252 default:
2253 return false;
2254 }
2255}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002256
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002257void GrGpuGL::resetDirtyFlags() {
2258 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2259}
2260
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002261void GrGpuGL::setBuffers(bool indexed,
2262 int* extraVertexOffset,
2263 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002264
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002265 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002266
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002267 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2268
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002269 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002270 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002271 case kBuffer_GeometrySrcType:
2272 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002273 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002274 break;
2275 case kArray_GeometrySrcType:
2276 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002277 this->finalizeReservedVertices();
2278 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2279 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002280 break;
2281 default:
2282 vbuf = NULL; // suppress warning
2283 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002284 }
2285
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002286 GrAssert(NULL != vbuf);
2287 GrAssert(!vbuf->isLocked());
2288 if (fHWGeometryState.fVertexBuffer != vbuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002289 GR_GL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002290 fHWGeometryState.fArrayPtrsDirty = true;
2291 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002292 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002293
2294 if (indexed) {
2295 GrAssert(NULL != extraIndexOffset);
2296
2297 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002298 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002299 case kBuffer_GeometrySrcType:
2300 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002301 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002302 break;
2303 case kArray_GeometrySrcType:
2304 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002305 this->finalizeReservedIndices();
2306 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2307 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002308 break;
2309 default:
2310 ibuf = NULL; // suppress warning
2311 GrCrash("Unknown geometry src type!");
2312 }
2313
2314 GrAssert(NULL != ibuf);
2315 GrAssert(!ibuf->isLocked());
2316 if (fHWGeometryState.fIndexBuffer != ibuf) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002317 GR_GL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002318 fHWGeometryState.fIndexBuffer = ibuf;
2319 }
2320 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002321}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002322
2323int GrGpuGL::getMaxEdges() const {
2324 // FIXME: This is a pessimistic estimate based on how many other things
2325 // want to add uniforms. This should be centralized somewhere.
2326 return GR_CT_MIN(fMaxFragmentUniformVectors - 8, kMaxEdges);
2327}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002328