blob: 190a41230b6529a6287cb912e73362ba451aebe0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000011#include "GrGLStencilBuffer.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000012#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000013#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
twiz@google.com0f31ca72011-03-18 17:38:11 +000015static const GrGLuint GR_MAX_GLUINT = ~0;
16static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
bsalomon@google.com316f99232011-01-13 21:28:12 +000021// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000022// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000023static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
twiz@google.com0f31ca72011-03-18 17:38:11 +000027static const GrGLenum gXfermodeCoeff2Blend[] = {
28 GR_GL_ZERO,
29 GR_GL_ONE,
30 GR_GL_SRC_COLOR,
31 GR_GL_ONE_MINUS_SRC_COLOR,
32 GR_GL_DST_COLOR,
33 GR_GL_ONE_MINUS_DST_COLOR,
34 GR_GL_SRC_ALPHA,
35 GR_GL_ONE_MINUS_SRC_ALPHA,
36 GR_GL_DST_ALPHA,
37 GR_GL_ONE_MINUS_DST_ALPHA,
38 GR_GL_CONSTANT_COLOR,
39 GR_GL_ONE_MINUS_CONSTANT_COLOR,
40 GR_GL_CONSTANT_ALPHA,
41 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
43 // extended blend coeffs
44 GR_GL_SRC1_COLOR,
45 GR_GL_ONE_MINUS_SRC1_COLOR,
46 GR_GL_SRC1_ALPHA,
47 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000048};
49
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000051 static const bool gCoeffReferencesBlendConst[] = {
52 false,
53 false,
54 false,
55 false,
56 false,
57 false,
58 false,
59 false,
60 false,
61 false,
62 true,
63 true,
64 true,
65 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000066
67 // extended blend coeffs
68 false,
69 false,
70 false,
71 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000072 };
73 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
75
76 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
77 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
78 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
79 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
80 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
81 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
82 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
83 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
84 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
85 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
86 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
87 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
88 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
89 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
90
91 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
92 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
93 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
94 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
95
96 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
97 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +000098}
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comd302f142011-03-03 13:54:13 +0000102void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
103 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000104 GrMatrix* matrix) {
105 GrAssert(NULL != texture);
106 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000107 GrGLTexture::Orientation orientation = texture->orientation();
108 if (GrGLTexture::kBottomUp_Orientation == orientation) {
109 GrMatrix invY;
110 invY.setAll(GR_Scalar1, 0, 0,
111 0, -GR_Scalar1, GR_Scalar1,
112 0, 0, GrMatrix::I()[8]);
113 matrix->postConcat(invY);
114 } else {
115 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
116 }
117}
118
bsalomon@google.comd302f142011-03-03 13:54:13 +0000119bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120 const GrSamplerState& sampler) {
121 GrAssert(NULL != texture);
122 if (!sampler.getMatrix().isIdentity()) {
123 return false;
124 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000125 GrGLTexture::Orientation orientation = texture->orientation();
126 if (GrGLTexture::kBottomUp_Orientation == orientation) {
127 return false;
128 } else {
129 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
130 }
131 return true;
132}
133
134///////////////////////////////////////////////////////////////////////////////
135
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000136static bool gPrintStartupSpew;
137
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000139
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000141
twiz@google.com0f31ca72011-03-18 17:38:11 +0000142 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
144 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000145 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000146 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
147 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000148 // some implementations require texture to be mip-map complete before
149 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000150 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000151 GR_GL_TEXTURE_MIN_FILTER,
152 GR_GL_NEAREST));
153 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
154 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
155 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
156 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
157 GR_GL_COLOR_ATTACHMENT0,
158 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000159 GrGLenum status;
160 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000161 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
162 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000163
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000164 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000165}
166
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000167GrGpuGL::GrGpuGL(const GrGLInterface* gl, GrGLBinding glBinding) {
tomhudson@google.com747bf292011-06-14 18:16:52 +0000168
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000169 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000170
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 gl->ref();
172 fGL = gl;
173 fGLBinding = glBinding;
174 switch (glBinding) {
175 case kDesktop_GrGLBinding:
176 GrAssert(gl->supportsDesktop());
177 break;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000178 case kES2_GrGLBinding:
179 GrAssert(gl->supportsES2());
180 break;
181 default:
182 GrCrash("Expect exactly one valid GL binding bit to be in use.");
reed@google.comeeeb5a02010-12-23 15:12:59 +0000183 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185 GrGLClearErr(fGL);
186
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000187 const GrGLubyte* ext;
188 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000189 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000190 const GrGLubyte* vendor;
191 const GrGLubyte* renderer;
192 const GrGLubyte* version;
193 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
194 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
195 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000196 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
197 this);
198 GrPrintf("------ VENDOR %s\n", vendor);
199 GrPrintf("------ RENDERER %s\n", renderer);
200 GrPrintf("------ VERSION %s\n", version);
201 GrPrintf("------ EXTENSIONS\n %s \n", ext);
202 }
203
bsalomon@google.comc82b8892011-09-22 14:10:33 +0000204 fGLVersion = GrGLGetVersion(gl);
205 GrAssert(0 != fGLVersion);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000206 fExtensionString = (const char*) ext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000207
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000208 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000209
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000210 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000211
bsalomon@google.comfe676522011-06-17 18:12:21 +0000212 fLastSuccessfulStencilFmtIdx = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213}
214
215GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000216 // This must be called by before the GrDrawTarget destructor
217 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000218 // This subclass must do this before the base class destructor runs
219 // since we will unref the GrGLInterface.
220 this->releaseResources();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000221 fGL->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000222}
223
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000224///////////////////////////////////////////////////////////////////////////////
225
226static const GrGLuint kUnknownBitCount = ~0;
227
228void GrGpuGL::initCaps() {
229 GrGLint maxTextureUnits;
230 // check FS and fixed-function texture unit limits
231 // we only use textures in the fragment stage currently.
232 // checks are > to make sure we have a spare unit.
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000233 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
234 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235 if (kES2_GrGLBinding != this->glBinding()) {
236 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000237 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 }
239 if (kES2_GrGLBinding == this->glBinding()) {
240 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS,
241 &fGLCaps.fMaxFragmentUniformVectors);
242 } else if (kDesktop_GrGLBinding != this->glBinding()) {
243 GrGLint max;
244 GR_GL_GetIntegerv(fGL, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max);
245 fGLCaps.fMaxFragmentUniformVectors = max / 4;
246 } else {
247 fGLCaps.fMaxFragmentUniformVectors = 16;
248 }
249
250 GrGLint numFormats;
251 GR_GL_GetIntegerv(fGL, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
252 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
253 GR_GL_GetIntegerv(fGL, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
254 for (int i = 0; i < numFormats; ++i) {
255 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
256 fCaps.f8BitPaletteSupport = true;
257 break;
258 }
259 }
260
261 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 // we could also look for GL_ATI_separate_stencil extension or
263 // GL_EXT_stencil_two_side but they use different function signatures
264 // than GL2.0+ (and than each other).
265 fCaps.fTwoSidedStencilSupport = (fGLVersion >= GR_GL_VER(2,0));
266 // supported on GL 1.4 and higher or by extension
267 fCaps.fStencilWrapOpsSupport = (fGLVersion >= GR_GL_VER(1,4)) ||
268 this->hasExtension("GL_EXT_stencil_wrap");
269 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000270 // ES 2 has two sided stencil and stencil wrap
271 fCaps.fTwoSidedStencilSupport = true;
272 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000273 }
274
275 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000276 fGLCaps.fRGBA8RenderbufferSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000277 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000278 fGLCaps.fRGBA8RenderbufferSupport =
279 this->hasExtension("GL_OES_rgb8_rgba8") ||
280 this->hasExtension("GL_ARM_rgba8");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000281 }
282
283
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000285 fGLCaps.fBGRAFormatSupport = this->glVersion() >= GR_GL_VER(1,2) ||
286 this->hasExtension("GL_EXT_bgra");
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 } else {
288 bool hasBGRAExt = false;
289 if (this->hasExtension("GL_APPLE_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000290 fGLCaps.fBGRAFormatSupport = true;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 } else if (this->hasExtension("GL_EXT_texture_format_BGRA8888")) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000292 fGLCaps.fBGRAFormatSupport = true;
293 fGLCaps.fBGRAIsInternalFormat = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000294 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000295 GrAssert(fGLCaps.fBGRAFormatSupport ||
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000297 }
298
299 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000300 fGLCaps.fTextureSwizzleSupport = this->glVersion() >= GR_GL_VER(3,3) ||
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000301 this->hasExtension("GL_ARB_texture_swizzle");
302 } else {
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000303 fGLCaps.fTextureSwizzleSupport = false;
304 }
305
306 if (kDesktop_GrGLBinding == this->glBinding()) {
307 fGLCaps.fUnpackRowLengthSupport = true;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000308 fGLCaps.fUnpackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000309 fGLCaps.fPackRowLengthSupport = true;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000310 fGLCaps.fPackFlipYSupport = false;
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000311 } else {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000312 fGLCaps.fUnpackRowLengthSupport =this->hasExtension("GL_EXT_unpack_subimage");
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000313 fGLCaps.fUnpackFlipYSupport = this->hasExtension("GL_CHROMIUM_flipy");
bsalomon@google.com7107fa72011-11-10 14:54:14 +0000314 // no extension for pack row length
315 fGLCaps.fPackRowLengthSupport = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000316 fGLCaps.fPackFlipYSupport =
317 this->hasExtension("GL_ANGLE_pack_reverse_row_order");
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000318 }
319
320 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000321 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
322 // extension includes glMapBuffer.
323 } else {
324 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
325 }
326
327 if (kDesktop_GrGLBinding == this->glBinding()) {
328 if (fGLVersion >= GR_GL_VER(2,0) ||
329 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
330 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000331 } else {
332 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000333 }
334 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000335 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000336 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000337 }
338
339 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
340
341 ////////////////////////////////////////////////////////////////////////////
342 // Experiments to determine limitations that can't be queried.
343 // TODO: Make these a preprocess that generate some compile time constants.
344 // TODO: probe once at startup, rather than once per context creation.
345
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000346 GR_GL_GetIntegerv(fGL, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
347 GR_GL_GetIntegerv(fGL, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
348 // Our render targets are always created with textures as the color
349 // attachment, hence this min:
350 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
351
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000352 this->initFSAASupport();
353 this->initStencilFormats();
354}
355
356void GrGpuGL::initFSAASupport() {
357 // TODO: Get rid of GrAALevel and use # samples directly.
358 GR_STATIC_ASSERT(0 == kNone_GrAALevel);
359 GR_STATIC_ASSERT(1 == kLow_GrAALevel);
360 GR_STATIC_ASSERT(2 == kMed_GrAALevel);
361 GR_STATIC_ASSERT(3 == kHigh_GrAALevel);
362 memset(fGLCaps.fAASamples, 0, sizeof(fGLCaps.fAASamples));
363
364 fGLCaps.fMSFBOType = GLCaps::kNone_MSFBO;
365 if (kDesktop_GrGLBinding != this->glBinding()) {
366 if (this->hasExtension("GL_CHROMIUM_framebuffer_multisample")) {
367 // chrome's extension is equivalent to the EXT msaa
368 // and fbo_blit extensions.
369 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
370 } else if (this->hasExtension("GL_APPLE_framebuffer_multisample")) {
371 fGLCaps.fMSFBOType = GLCaps::kAppleES_MSFBO;
372 }
373 } else {
374 if ((fGLVersion >= GR_GL_VER(3,0)) || this->hasExtension("GL_ARB_framebuffer_object")) {
375 fGLCaps.fMSFBOType = GLCaps::kDesktopARB_MSFBO;
376 } else if (this->hasExtension("GL_EXT_framebuffer_multisample") &&
377 this->hasExtension("GL_EXT_framebuffer_blit")) {
378 fGLCaps.fMSFBOType = GLCaps::kDesktopEXT_MSFBO;
379 }
380 }
381
382 if (GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType) {
383 GrGLint maxSamples;
384 GR_GL_GetIntegerv(fGL, GR_GL_MAX_SAMPLES, &maxSamples);
385 if (maxSamples > 1 ) {
386 fGLCaps.fAASamples[kNone_GrAALevel] = 0;
387 fGLCaps.fAASamples[kLow_GrAALevel] =
388 GrMax(2, GrFixedFloorToInt((GR_FixedHalf) * maxSamples));
389 fGLCaps.fAASamples[kMed_GrAALevel] =
390 GrMax(2, GrFixedFloorToInt(((GR_Fixed1*3)/4) * maxSamples));
391 fGLCaps.fAASamples[kHigh_GrAALevel] = maxSamples;
392 }
393 }
394 fCaps.fFSAASupport = fGLCaps.fAASamples[kHigh_GrAALevel] > 0;
395}
396
397void GrGpuGL::initStencilFormats() {
398
399 // Build up list of legal stencil formats (though perhaps not supported on
400 // the particular gpu/driver) from most preferred to least.
401
402 // these consts are in order of most preferred to least preferred
403 // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8
404 static const GrGLStencilBuffer::Format
405 // internal Format stencil bits total bits packed?
406 gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false},
407 gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false},
408 gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true },
409 gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false},
410 gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false},
411 gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true };
412
413 if (kDesktop_GrGLBinding == this->glBinding()) {
414 bool supportsPackedDS = fGLVersion >= GR_GL_VER(3,0) ||
415 this->hasExtension("GL_EXT_packed_depth_stencil") ||
416 this->hasExtension("GL_ARB_framebuffer_object");
417
418 // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we
419 // require FBO support we can expect these are legal formats and don't
420 // check. These also all support the unsized GL_STENCIL_INDEX.
421 fGLCaps.fStencilFormats.push_back() = gS8;
422 fGLCaps.fStencilFormats.push_back() = gS16;
423 if (supportsPackedDS) {
424 fGLCaps.fStencilFormats.push_back() = gD24S8;
425 }
426 fGLCaps.fStencilFormats.push_back() = gS4;
427 if (supportsPackedDS) {
428 fGLCaps.fStencilFormats.push_back() = gDS;
429 }
430 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000431 // ES2 has STENCIL_INDEX8 without extensions but requires extensions
432 // for other formats.
433 // ES doesn't support using the unsized format.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000434
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000435 fGLCaps.fStencilFormats.push_back() = gS8;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000436 //fStencilFormats.push_back() = gS16;
437 if (this->hasExtension("GL_OES_packed_depth_stencil")) {
438 fGLCaps.fStencilFormats.push_back() = gD24S8;
439 }
440 if (this->hasExtension("GL_OES_stencil4")) {
441 fGLCaps.fStencilFormats.push_back() = gS4;
442 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000443 }
444}
445
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000446GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000447 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
448 return GrPixelConfigSwapRAndB(config);
449 } else {
450 return config;
451 }
452}
453
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000454GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000455 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000456 return GrPixelConfigSwapRAndB(config);
457 } else {
458 return config;
459 }
460}
461
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000462bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
463 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
464}
465
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000466void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000467 if (gPrintStartupSpew && !fPrintedCaps) {
468 fPrintedCaps = true;
469 this->getCaps().print();
470 fGLCaps.print();
471 }
472
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000473 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000474 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000475 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000476
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000477 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000478 GL_CALL(Disable(GR_GL_DEPTH_TEST));
479 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000480
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000481 GL_CALL(Disable(GR_GL_CULL_FACE));
482 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000483 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000484
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000485 GL_CALL(Disable(GR_GL_DITHER));
486 if (kDesktop_GrGLBinding == this->glBinding()) {
487 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
488 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
489 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000490 fHWAAState.fMSAAEnabled = false;
491 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000492 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000494 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000495 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000496
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000498 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000500 // invalid
501 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000504 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000505
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000506 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000507 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000508
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000509 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000510
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000511 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000512
tomhudson@google.com93813632011-10-27 20:21:16 +0000513 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000514 fHWDrawState.setTexture(s, NULL);
515 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
516 -GR_ScalarMax,
517 true);
518 fHWDrawState.sampler(s)->setMatrix(GrMatrix::InvalidMatrix());
519 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000520 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000521
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000522 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000523 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000524 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000525 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000526
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000527 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000528 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000529 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530
531 fHWGeometryState.fIndexBuffer = NULL;
532 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000533
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000534 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000536 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000537 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000538
539 // we assume these values
540 if (this->glCaps().fUnpackRowLengthSupport) {
541 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
542 }
543 if (this->glCaps().fPackRowLengthSupport) {
544 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
545 }
546 if (this->glCaps().fUnpackFlipYSupport) {
547 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
548 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000549 if (this->glCaps().fPackFlipYSupport) {
550 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
551 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000552}
553
bsalomon@google.come269f212011-11-07 13:29:52 +0000554GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000555 GrGLenum dontCare;
bsalomon@google.come269f212011-11-07 13:29:52 +0000556 GrGLTexture::Desc glTexDesc;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000557 if (!this->canBeTexture(desc.fConfig, &glTexDesc.fInternalFormat,
558 &dontCare, &dontCare)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000559 return NULL;
560 }
561
bsalomon@google.com99621082011-11-15 16:47:16 +0000562 glTexDesc.fWidth = desc.fWidth;
563 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000564 glTexDesc.fConfig = desc.fConfig;
565 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
566 glTexDesc.fOwnsID = false;
567 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
568
569 GrGLTexture* texture = NULL;
570 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
571 GrGLRenderTarget::Desc glRTDesc;
572 glRTDesc.fRTFBOID = 0;
573 glRTDesc.fTexFBOID = 0;
574 glRTDesc.fMSColorRenderbufferID = 0;
575 glRTDesc.fOwnIDs = true;
576 glRTDesc.fConfig = desc.fConfig;
577 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000578 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
579 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000580 glTexDesc.fTextureID,
581 &glRTDesc)) {
582 return NULL;
583 }
584 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
585 } else {
586 texture = new GrGLTexture(this, glTexDesc);
587 }
588 if (NULL == texture) {
589 return NULL;
590 }
591
592 this->setSpareTextureUnit();
593 return texture;
594}
595
596GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
597 GrGLRenderTarget::Desc glDesc;
598 glDesc.fConfig = desc.fConfig;
599 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
600 glDesc.fMSColorRenderbufferID = 0;
601 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
602 glDesc.fSampleCnt = desc.fSampleCnt;
603 glDesc.fOwnIDs = false;
604 GrGLIRect viewport;
605 viewport.fLeft = 0;
606 viewport.fBottom = 0;
607 viewport.fWidth = desc.fWidth;
608 viewport.fHeight = desc.fHeight;
609
610 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
611 if (desc.fStencilBits) {
612 GrGLStencilBuffer::Format format;
613 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
614 format.fPacked = false;
615 format.fStencilBits = desc.fStencilBits;
616 format.fTotalBits = desc.fStencilBits;
617 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
618 0,
619 desc.fWidth,
620 desc.fHeight,
621 desc.fSampleCnt,
622 format);
623 tgt->setStencilBuffer(sb);
624 sb->unref();
625 }
626 return tgt;
627}
628
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000629GrResource* GrGpuGL::onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) {
630
631 bool isTexture = kTexture_GrPlatformSurfaceType == desc.fSurfaceType ||
632 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
633 bool isRenderTarget = kRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType ||
634 kTextureRenderTarget_GrPlatformSurfaceType == desc.fSurfaceType;
635
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000636 GrGLRenderTarget::Desc rtDesc;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000637 SkAutoTUnref<GrGLStencilBuffer> sb;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000638
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000639 if (isRenderTarget) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000640 rtDesc.fRTFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000641 rtDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000642 if (desc.fSampleCnt) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000643 if (kGrCanResolve_GrPlatformRenderTargetFlagBit & desc.fRenderTargetFlags) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000644 rtDesc.fTexFBOID = desc.fPlatformResolveDestination;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000645 } else {
646 GrAssert(!isTexture); // this should have been filtered by GrContext
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000647 rtDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000648 }
649 } else {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000650 rtDesc.fTexFBOID = desc.fPlatformRenderTarget;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000651 }
652 // we don't know what the RB ids are without glGets and we don't care
653 // since we aren't responsible for deleting them.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000654 rtDesc.fMSColorRenderbufferID = 0;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000655 rtDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000656 if (desc.fStencilBits) {
657 GrGLStencilBuffer::Format format;
658 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
659 format.fPacked = false;
660 format.fStencilBits = desc.fStencilBits;
661 format.fTotalBits = desc.fStencilBits;
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000662 sb.reset(new GrGLStencilBuffer(this, 0, desc.fWidth, desc.fHeight,
663 rtDesc.fSampleCnt, format));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000664 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000665 rtDesc.fOwnIDs = false;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000666 }
667
668 if (isTexture) {
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000669 GrGLTexture::Desc texDesc;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000670 GrGLenum dontCare;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000671 if (!canBeTexture(desc.fConfig, &texDesc.fInternalFormat,
672 &dontCare, &dontCare)) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000673 return NULL;
674 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000675 texDesc.fWidth = desc.fWidth;
676 texDesc.fHeight = desc.fHeight;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000677
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000678 texDesc.fConfig = desc.fConfig;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000679 texDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000680 texDesc.fTextureID = desc.fPlatformTexture;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000681 texDesc.fOwnsID = false;
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000682
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000683 if (isRenderTarget) {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000684 GrTexture* tex = new GrGLTexture(this, texDesc, rtDesc);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000685 tex->asRenderTarget()->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000686 return tex;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000687 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000688 return new GrGLTexture(this, texDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000689 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000690 } else {
691 GrGLIRect viewport;
692 viewport.fLeft = 0;
693 viewport.fBottom = 0;
694 viewport.fWidth = desc.fWidth;
695 viewport.fHeight = desc.fHeight;
696
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000697 GrGLRenderTarget* rt = new GrGLRenderTarget(this, rtDesc, viewport);
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000698 rt->setStencilBuffer(sb.get());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000699 return rt;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000700 }
701}
702
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000703
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000704////////////////////////////////////////////////////////////////////////////////
705
bsalomon@google.com6f379512011-11-16 20:36:03 +0000706void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
707 int left, int top, int width, int height,
708 GrPixelConfig config, const void* buffer,
709 size_t rowBytes) {
710 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000711
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000712 if (NULL == buffer) {
713 return;
714 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000715 this->setSpareTextureUnit();
716 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
717 GrGLTexture::Desc desc;
718 desc.fConfig = glTex->config();
719 desc.fWidth = glTex->width();
720 desc.fHeight = glTex->height();
721 desc.fOrientation = glTex->orientation();
722 desc.fTextureID = glTex->textureID();
723 desc.fInternalFormat = glTex->internalFormat();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000724
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000725 this->uploadTexData(desc, false,
726 left, top, width, height,
727 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000728}
729
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000730namespace {
731bool adjust_pixel_ops_params(int surfaceWidth,
732 int surfaceHeight,
733 size_t bpp,
734 int* left, int* top, int* width, int* height,
735 const void** data,
736 size_t* rowBytes) {
737 if (!*rowBytes) {
738 *rowBytes = *width * bpp;
739 }
740
741 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
742 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
743
744 if (!subRect.intersect(bounds)) {
745 return false;
746 }
747 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
748 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
749
750 *left = subRect.fLeft;
751 *top = subRect.fTop;
752 *width = subRect.width();
753 *height = subRect.height();
754 return true;
755}
756}
757
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000758bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
759 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000760 int left, int top, int width, int height,
761 GrPixelConfig dataConfig,
762 const void* data,
763 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000764 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000765
766 size_t bpp = GrBytesPerPixel(dataConfig);
767 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
768 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000769 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000771 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000772
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000773 // in case we need a temporary, trimmed copy of the src pixels
774 SkAutoSMalloc<128 * 128> tempStorage;
775
bsalomon@google.com6f379512011-11-16 20:36:03 +0000776 GrGLenum dontCare;
777 GrGLenum externalFormat;
778 GrGLenum externalType;
779 if (!this->canBeTexture(dataConfig, &dontCare,
780 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000781 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000782 }
783
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000784 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == desc.fInternalFormat) {
785 // paletted textures cannot be updated
786 return false;
787 }
788
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000789 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000790 * check whether to allocate a temporary buffer for flipping y or
791 * because our srcData has extra bytes past each row. If so, we need
792 * to trim those off here, since GL ES may not let us specify
793 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000794 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000795 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000796 bool swFlipY = false;
797 bool glFlipY = false;
798
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000799 if (NULL != data) {
800 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
801 if (this->glCaps().fUnpackFlipYSupport) {
802 glFlipY = true;
803 } else {
804 swFlipY = true;
805 }
806 }
807 if (this->glCaps().fUnpackRowLengthSupport && !swFlipY) {
808 // can't use this for flipping, only non-neg values allowed. :(
809 if (rowBytes != trimRowBytes) {
810 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
811 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
812 restoreGLRowLength = true;
813 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000814 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000815 if (trimRowBytes != rowBytes || swFlipY) {
816 // copy data into our new storage, skipping the trailing bytes
817 size_t trimSize = height * trimRowBytes;
818 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000819 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000820 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000821 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000822 char* dst = (char*)tempStorage.reset(trimSize);
823 for (int y = 0; y < height; y++) {
824 memcpy(dst, src, trimRowBytes);
825 if (swFlipY) {
826 src -= rowBytes;
827 } else {
828 src += rowBytes;
829 }
830 dst += trimRowBytes;
831 }
832 // now point data to our copied version
833 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000834 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000835 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000836 if (glFlipY) {
837 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
838 }
839 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000840 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000841 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000842 if (isNewTexture &&
843 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000844 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000845 GrGLClearErr(this->glInterface());
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000846 if (GR_GL_PALETTE8_RGBA8 == desc.fInternalFormat) {
847 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
848 kGrColorTableSize;
849 GR_GL_CALL_NOERRCHECK(this->glInterface(),
850 CompressedTexImage2D(GR_GL_TEXTURE_2D,
851 0, // level
852 desc.fInternalFormat,
853 desc.fWidth,
854 desc.fHeight,
855 0, // border
856 imageSize,
857 data));
858 } else {
859 GR_GL_CALL_NOERRCHECK(this->glInterface(),
860 TexImage2D(GR_GL_TEXTURE_2D,
861 0, // level
862 desc.fInternalFormat,
863 desc.fWidth,
864 desc.fHeight,
865 0, // border
866 externalFormat,
867 externalType,
868 data));
869 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000870 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000871 succeeded = false;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000872 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000873 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000874 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000875 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000876 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000877 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
878 0, // level
879 left, top,
880 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000881 externalFormat, externalType, data));
882 }
883
884 if (restoreGLRowLength) {
885 GrAssert(this->glCaps().fUnpackRowLengthSupport);
886 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000887 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000888 if (glFlipY) {
889 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
890 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000891 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000892}
893
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000894bool GrGpuGL::createRenderTargetObjects(int width, int height,
895 GrGLuint texID,
896 GrGLRenderTarget::Desc* desc) {
897 desc->fMSColorRenderbufferID = 0;
898 desc->fRTFBOID = 0;
899 desc->fTexFBOID = 0;
900 desc->fOwnIDs = true;
901
902 GrGLenum status;
903 GrGLint err;
904
bsalomon@google.comab15d612011-08-09 12:57:56 +0000905 GrGLenum msColorFormat = 0; // suppress warning
906
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000907 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 if (!desc->fTexFBOID) {
909 goto FAILED;
910 }
911
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912
913 // If we are using multisampling we will create two FBOS. We render
914 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000915 if (desc->fSampleCnt > 0) {
916 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType) {
917 goto FAILED;
918 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
920 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921 if (!desc->fRTFBOID ||
922 !desc->fMSColorRenderbufferID ||
923 !this->fboInternalFormat(desc->fConfig, &msColorFormat)) {
924 goto FAILED;
925 }
926 } else {
927 desc->fRTFBOID = desc->fTexFBOID;
928 }
929
930 if (desc->fRTFBOID != desc->fTexFBOID) {
931 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000932 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000933 desc->fMSColorRenderbufferID));
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000934 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 GR_GL_CALL_NOERRCHECK(this->glInterface(),
936 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
937 desc->fSampleCnt,
938 msColorFormat,
939 width, height));
940 err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000941 if (err != GR_GL_NO_ERROR) {
942 goto FAILED;
943 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000944 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
945 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000946 GR_GL_COLOR_ATTACHMENT0,
947 GR_GL_RENDERBUFFER,
948 desc->fMSColorRenderbufferID));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000949 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
951 goto FAILED;
952 }
953 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000954 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000955
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000956 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
957 GR_GL_COLOR_ATTACHMENT0,
958 GR_GL_TEXTURE_2D,
959 texID, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000960 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000961 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
962 goto FAILED;
963 }
964
965 return true;
966
967FAILED:
968 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000969 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000970 }
971 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000972 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000973 }
974 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000975 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000976 }
977 return false;
978}
979
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000980// good to set a break-point here to know when createTexture fails
981static GrTexture* return_null_texture() {
982// GrAssert(!"null texture");
983 return NULL;
984}
985
986#if GR_DEBUG
987static size_t as_size_t(int x) {
988 return x;
989}
990#endif
991
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000992GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000993 const void* srcData,
994 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000995
996#if GR_COLLECT_STATS
997 ++fStats.fTextureCreateCnt;
998#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000999
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001000 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001001 GrGLRenderTarget::Desc glRTDesc;
bsalomon@google.com6f379512011-11-16 20:36:03 +00001002 GrGLenum externalFormat;
1003 GrGLenum externalType;
reed@google.comac10a2d2010-12-22 21:39:39 +00001004
bsalomon@google.com99621082011-11-15 16:47:16 +00001005 glTexDesc.fWidth = desc.fWidth;
1006 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001007 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001008 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001009
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001010 glRTDesc.fMSColorRenderbufferID = 0;
1011 glRTDesc.fRTFBOID = 0;
1012 glRTDesc.fTexFBOID = 0;
1013 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001014 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001015
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001016 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001017 if (!canBeTexture(desc.fConfig,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001018 &glTexDesc.fInternalFormat,
1019 &externalFormat,
1020 &externalType)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001021 return return_null_texture();
1022 }
1023
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001024 const Caps& caps = this->getCaps();
1025
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001026 // We keep GrRenderTargets in GL's normal orientation so that they
1027 // can be drawn to by the outside world without the client having
1028 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001029 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001030 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001031
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001032 GrAssert(as_size_t(desc.fAALevel) < GR_ARRAY_COUNT(fGLCaps.fAASamples));
1033 glRTDesc.fSampleCnt = fGLCaps.fAASamples[desc.fAALevel];
1034 if (GLCaps::kNone_MSFBO == fGLCaps.fMSFBOType &&
1035 desc.fAALevel != kNone_GrAALevel) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001036 GrPrintf("AA RT requested but not supported on this platform.");
1037 }
1038
reed@google.comac10a2d2010-12-22 21:39:39 +00001039 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001040 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1041 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001042 return return_null_texture();
1043 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001044 }
1045
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001047 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001048 return return_null_texture();
1049 }
1050
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001051 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001052 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001053
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001054 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1055 // drivers have a bug where an FBO won't be complete if it includes a
1056 // texture that is not mipmap complete (considering the filter in use).
1057 GrGLTexture::TexParams initialTexParams;
1058 // we only set a subset here so invalidate first
1059 initialTexParams.invalidate();
1060 initialTexParams.fFilter = GR_GL_NEAREST;
1061 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1062 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001063 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1064 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001065 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001066 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1067 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001068 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001069 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1070 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001071 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001072 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1073 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001074 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001075 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1076 glTexDesc.fWidth, glTexDesc.fHeight,
1077 desc.fConfig, srcData, rowBytes)) {
1078 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1079 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001080 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001081
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001083 if (renderTarget) {
1084#if GR_COLLECT_STATS
1085 ++fStats.fRenderTargetCreateCnt;
1086#endif
bsalomon@google.com99621082011-11-15 16:47:16 +00001087 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1088 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089 glTexDesc.fTextureID,
1090 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001091 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001092 return return_null_texture();
1093 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001094 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001095 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001096 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001097 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001098 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001099#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001100 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1101 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001102#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103 return tex;
1104}
1105
1106namespace {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1108 GrGLuint rb,
1109 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 // we shouldn't ever know one size and not the other
1111 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1112 (kUnknownBitCount == format->fTotalBits));
1113 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001114 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1116 (GrGLint*)&format->fStencilBits);
1117 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1120 (GrGLint*)&format->fTotalBits);
1121 format->fTotalBits += format->fStencilBits;
1122 } else {
1123 format->fTotalBits = format->fStencilBits;
1124 }
1125 }
1126}
1127}
1128
1129bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1130 int width, int height) {
1131
1132 // All internally created RTs are also textures. We don't create
1133 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1134 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001135 GrAssert(width >= rt->width());
1136 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137
1138 int samples = rt->numSamples();
1139 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001140 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141 if (!sbID) {
1142 return false;
1143 }
1144
1145 GrGLStencilBuffer* sb = NULL;
1146
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001147 int stencilFmtCnt = fGLCaps.fStencilFormats.count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001149 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 // we start with the last stencil format that succeeded in hopes
1151 // that we won't go through this loop more than once after the
1152 // first (painful) stencil creation.
1153 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001154 const GrGLStencilBuffer::Format& sFmt = fGLCaps.fStencilFormats[sIdx];
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001155 GrGLClearErr(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001156 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 // version on a GL that doesn't have an MSAA extension.
1158 if (samples > 1) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001159 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1160 RenderbufferStorageMultisample(
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 GR_GL_RENDERBUFFER,
1162 samples,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001163 sFmt.fInternalFormat,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001164 width,
1165 height));
1166 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001167 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1168 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001169 sFmt.fInternalFormat,
1170 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 }
1172
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001173 GrGLenum err = GR_GL_GET_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 if (err == GR_GL_NO_ERROR) {
1175 // After sized formats we attempt an unsized format and take whatever
1176 // sizes GL gives us. In that case we query for the size.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001177 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001178 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001179 sb = new GrGLStencilBuffer(this, sbID, width, height,
1180 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1182 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001183 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 return true;
1186 }
1187 sb->abandon(); // otherwise we lose sbID
1188 sb->unref();
1189 }
1190 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001192 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193}
1194
1195bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1196 GrRenderTarget* rt) {
1197 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1198
1199 GrGLuint fbo = glrt->renderFBOID();
1200
1201 if (NULL == sb) {
1202 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001203 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 GR_GL_STENCIL_ATTACHMENT,
1205 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 GR_GL_DEPTH_ATTACHMENT,
1208 GR_GL_RENDERBUFFER, 0));
1209#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001210 GrGLenum status;
1211 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1213#endif
1214 }
1215 return true;
1216 } else {
1217 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1218 GrGLuint rb = glsb->renderbufferID();
1219
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001220 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1222 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 GR_GL_STENCIL_ATTACHMENT,
1224 GR_GL_RENDERBUFFER, rb));
1225 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001226 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 GR_GL_DEPTH_ATTACHMENT,
1228 GR_GL_RENDERBUFFER, rb));
1229 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001230 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231 GR_GL_DEPTH_ATTACHMENT,
1232 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001235 GrGLenum status;
1236 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001238 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 GR_GL_STENCIL_ATTACHMENT,
1240 GR_GL_RENDERBUFFER, 0));
1241 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001242 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 GR_GL_DEPTH_ATTACHMENT,
1244 GR_GL_RENDERBUFFER, 0));
1245 }
1246 return false;
1247 } else {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248 return true;
1249 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001250 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001251}
1252
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001253////////////////////////////////////////////////////////////////////////////////
1254
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001255GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001256 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001257 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001259 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001260 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1264 BufferData(GR_GL_ARRAY_BUFFER, size, NULL,
1265 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001266 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001267 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001268 // deleting bound buffer does implicit bind to 0
1269 fHWGeometryState.fVertexBuffer = NULL;
1270 return NULL;
1271 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001272 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 size, dynamic);
1274 fHWGeometryState.fVertexBuffer = vertexBuffer;
1275 return vertexBuffer;
1276 }
1277 return NULL;
1278}
1279
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001280GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001281 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001282 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001284 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
1285 GrGLClearErr(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 // make sure driver can allocate memory for this buffer
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001287 GR_GL_CALL_NOERRCHECK(this->glInterface(),
1288 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, NULL,
1289 dynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001290 if (GR_GL_GET_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001291 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001292 // deleting bound buffer does implicit bind to 0
1293 fHWGeometryState.fIndexBuffer = NULL;
1294 return NULL;
1295 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001296 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 size, dynamic);
1298 fHWGeometryState.fIndexBuffer = indexBuffer;
1299 return indexBuffer;
1300 }
1301 return NULL;
1302}
1303
reed@google.comac10a2d2010-12-22 21:39:39 +00001304void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001305 const GrDrawState& drawState = this->getDrawState();
1306 const GrGLRenderTarget* rt =
1307 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1308
1309 GrAssert(NULL != rt);
1310 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001311
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001312 GrGLIRect scissor;
1313 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001314 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001315 rect->width(), rect->height());
1316 if (scissor.contains(vp)) {
1317 rect = NULL;
1318 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001319 }
1320
1321 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001323 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 fHWBounds.fScissorRect = scissor;
1325 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001327 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 fHWBounds.fScissorEnabled = true;
1329 }
1330 } else {
1331 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001332 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 fHWBounds.fScissorEnabled = false;
1334 }
1335 }
1336}
1337
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001338void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001339 const GrDrawState& drawState = this->getDrawState();
1340 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001341 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001342 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001343
bsalomon@google.com74b98712011-11-11 19:46:16 +00001344 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001345 if (NULL != rect) {
1346 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001347 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001348 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001349 if (clippedRect.intersect(rtRect)) {
1350 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001351 } else {
1352 return;
1353 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001355 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001356 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001357
1358 GrGLfloat r, g, b, a;
1359 static const GrGLfloat scale255 = 1.f / 255.f;
1360 a = GrColorUnpackA(color) * scale255;
1361 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001362 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001363 scaleRGB *= a;
1364 }
1365 r = GrColorUnpackR(color) * scaleRGB;
1366 g = GrColorUnpackG(color) * scaleRGB;
1367 b = GrColorUnpackB(color) * scaleRGB;
1368
1369 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001370 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001371 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001372 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001373}
1374
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001375void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001376 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001377 return;
1378 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001379
1380 this->flushRenderTarget(&GrIRect::EmptyIRect());
1381
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001383 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001384 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001385 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001386 GL_CALL(StencilMask(0xffffffff));
1387 GL_CALL(ClearStencil(0));
1388 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001389 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001390}
1391
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001392void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001393 const GrDrawState& drawState = this->getDrawState();
1394 const GrRenderTarget* rt = drawState.getRenderTarget();
1395 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001396
1397 // this should only be called internally when we know we have a
1398 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001399 GrAssert(NULL != rt->getStencilBuffer());
1400 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001401#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001402 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001403 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001404#else
1405 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001406 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001407 // turned into draws. Our contract on GrDrawTarget says that
1408 // changing the clip between stencil passes may or may not
1409 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001410 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001411#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001412 GrGLint value;
1413 if (insideClip) {
1414 value = (1 << (stencilBitCount - 1));
1415 } else {
1416 value = 0;
1417 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001418 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001419 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001420 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001421 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001422 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001423 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001424}
1425
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001426void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001427 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001428}
1429
bsalomon@google.comc4364992011-11-07 15:54:49 +00001430bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1431 int left, int top,
1432 int width, int height,
1433 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001434 size_t rowBytes) const {
1435 // if GL can do the flip then we'll never pay for it.
1436 if (this->glCaps().fPackFlipYSupport) {
1437 return false;
1438 }
1439
1440 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001441 // get the flip for free. Otherwise it costs.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001442 if (this->glCaps().fPackRowLengthSupport) {
1443 return true;
1444 }
1445 // If we have to do memcpys to handle rowBytes then y-flip is free
1446 // Note the rowBytes might be tight to the passed in data, but if data
1447 // gets clipped in x to the target the rowBytes will no longer be tight.
1448 if (left >= 0 && (left + width) < renderTarget->width()) {
1449 return 0 == rowBytes ||
1450 GrBytesPerPixel(config) * width == rowBytes;
1451 } else {
1452 return false;
1453 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001454}
1455
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001456bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001457 int left, int top,
1458 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001459 GrPixelConfig config,
1460 void* buffer,
1461 size_t rowBytes,
1462 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001463 GrGLenum internalFormat; // we don't use this for glReadPixels
1464 GrGLenum format;
1465 GrGLenum type;
reed@google.comac10a2d2010-12-22 21:39:39 +00001466 if (!this->canBeTexture(config, &internalFormat, &format, &type)) {
1467 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001468 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001469 size_t bpp = GrBytesPerPixel(config);
1470 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1471 &left, &top, &width, &height,
1472 const_cast<const void**>(&buffer),
1473 &rowBytes)) {
1474 return false;
1475 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001476
bsalomon@google.comc6980972011-11-02 19:57:21 +00001477 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001478 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001479 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001480 switch (tgt->getResolveType()) {
1481 case GrGLRenderTarget::kCantResolve_ResolveType:
1482 return false;
1483 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001484 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001485 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001486 break;
1487 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001488 this->resolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001489 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001490 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1491 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001492 break;
1493 default:
1494 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001495 }
1496
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001497 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001498
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001499 // the read rect is viewport-relative
1500 GrGLIRect readRect;
1501 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001502
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001503 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001504 if (0 == rowBytes) {
1505 rowBytes = tightRowBytes;
1506 }
1507 size_t readDstRowBytes = tightRowBytes;
1508 void* readDst = buffer;
1509
1510 // determine if GL can read using the passed rowBytes or if we need
1511 // a scratch buffer.
1512 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1513 if (rowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001514 if (this->glCaps().fPackRowLengthSupport) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001515 GrAssert(!(rowBytes % sizeof(GrColor)));
1516 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1517 readDstRowBytes = rowBytes;
1518 } else {
1519 scratch.reset(tightRowBytes * height);
1520 readDst = scratch.get();
1521 }
1522 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001523 if (!invertY && this->glCaps().fPackFlipYSupport) {
1524 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1525 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001526 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1527 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001528 format, type, readDst));
1529 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001530 GrAssert(this->glCaps().fPackRowLengthSupport);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001531 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1532 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001533 if (!invertY && this->glCaps().fPackFlipYSupport) {
1534 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1535 invertY = true;
1536 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001537
1538 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001539 // API presents top-to-bottom. We must preserve the padding contents. Note
1540 // that the above readPixels did not overwrite the padding.
1541 if (readDst == buffer) {
1542 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001543 if (!invertY) {
1544 scratch.reset(tightRowBytes);
1545 void* tmpRow = scratch.get();
1546 // flip y in-place by rows
1547 const int halfY = height >> 1;
1548 char* top = reinterpret_cast<char*>(buffer);
1549 char* bottom = top + (height - 1) * rowBytes;
1550 for (int y = 0; y < halfY; y++) {
1551 memcpy(tmpRow, top, tightRowBytes);
1552 memcpy(top, bottom, tightRowBytes);
1553 memcpy(bottom, tmpRow, tightRowBytes);
1554 top += rowBytes;
1555 bottom -= rowBytes;
1556 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001557 }
1558 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001559 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001560 // copy from readDst to buffer while flipping y
1561 const int halfY = height >> 1;
1562 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001563 char* dst = reinterpret_cast<char*>(buffer);
1564 if (!invertY) {
1565 dst += (height-1) * rowBytes;
1566 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001567 for (int y = 0; y < height; y++) {
1568 memcpy(dst, src, tightRowBytes);
1569 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001570 if (invertY) {
1571 dst += rowBytes;
1572 } else {
1573 dst -= rowBytes;
1574 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001575 }
1576 }
1577 return true;
1578}
1579
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001580void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001581
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001582 GrGLRenderTarget* rt =
1583 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1584 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001585
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001586 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001587 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 #if GR_COLLECT_STATS
1589 ++fStats.fRenderTargetChngCnt;
1590 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001592 GrGLenum status;
1593 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001594 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001595 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001596 }
1597 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001598 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001599 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001600 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001601 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001602 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 fHWBounds.fViewportRect = vp;
1604 }
1605 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001606 if (NULL == bound || !bound->isEmpty()) {
1607 rt->flagAsNeedingResolve(bound);
1608 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001609}
1610
twiz@google.com0f31ca72011-03-18 17:38:11 +00001611GrGLenum gPrimitiveType2GLMode[] = {
1612 GR_GL_TRIANGLES,
1613 GR_GL_TRIANGLE_STRIP,
1614 GR_GL_TRIANGLE_FAN,
1615 GR_GL_POINTS,
1616 GR_GL_LINES,
1617 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001618};
1619
bsalomon@google.comd302f142011-03-03 13:54:13 +00001620#define SWAP_PER_DRAW 0
1621
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001622#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001623 #if GR_MAC_BUILD
1624 #include <AGL/agl.h>
1625 #elif GR_WIN32_BUILD
1626 void SwapBuf() {
1627 DWORD procID = GetCurrentProcessId();
1628 HWND hwnd = GetTopWindow(GetDesktopWindow());
1629 while(hwnd) {
1630 DWORD wndProcID = 0;
1631 GetWindowThreadProcessId(hwnd, &wndProcID);
1632 if(wndProcID == procID) {
1633 SwapBuffers(GetDC(hwnd));
1634 }
1635 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1636 }
1637 }
1638 #endif
1639#endif
1640
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001641void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1642 uint32_t startVertex,
1643 uint32_t startIndex,
1644 uint32_t vertexCount,
1645 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1647
twiz@google.com0f31ca72011-03-18 17:38:11 +00001648 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001649
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001650 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1651 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1652
1653 // our setupGeometry better have adjusted this to zero since
1654 // DrawElements always draws from the begining of the arrays for idx 0.
1655 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001656
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001657 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1658 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001659#if SWAP_PER_DRAW
1660 glFlush();
1661 #if GR_MAC_BUILD
1662 aglSwapBuffers(aglGetCurrentContext());
1663 int set_a_break_pt_here = 9;
1664 aglSwapBuffers(aglGetCurrentContext());
1665 #elif GR_WIN32_BUILD
1666 SwapBuf();
1667 int set_a_break_pt_here = 9;
1668 SwapBuf();
1669 #endif
1670#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001671}
1672
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001673void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1674 uint32_t startVertex,
1675 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001676 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1677
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001678 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1679
1680 // our setupGeometry better have adjusted this to zero.
1681 // DrawElements doesn't take an offset so we always adjus the startVertex.
1682 GrAssert(0 == startVertex);
1683
1684 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1685 // account for startVertex in the DrawElements case. So we always
1686 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001687 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001688#if SWAP_PER_DRAW
1689 glFlush();
1690 #if GR_MAC_BUILD
1691 aglSwapBuffers(aglGetCurrentContext());
1692 int set_a_break_pt_here = 9;
1693 aglSwapBuffers(aglGetCurrentContext());
1694 #elif GR_WIN32_BUILD
1695 SwapBuf();
1696 int set_a_break_pt_here = 9;
1697 SwapBuf();
1698 #endif
1699#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001700}
1701
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001702void GrGpuGL::resolveRenderTarget(GrGLRenderTarget* rt) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001703
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001704 if (rt->needsResolve()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001705 GrAssert(GLCaps::kNone_MSFBO != fGLCaps.fMSFBOType);
reed@google.comac10a2d2010-12-22 21:39:39 +00001706 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001707 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1708 rt->renderFBOID()));
1709 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1710 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001711 #if GR_COLLECT_STATS
1712 ++fStats.fRenderTargetChngCnt;
1713 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001714 // make sure we go through flushRenderTarget() since we've modified
1715 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001716 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001717 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001718 const GrIRect dirtyRect = rt->getResolveRect();
1719 GrGLIRect r;
1720 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1721 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001722
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001723 if (GLCaps::kAppleES_MSFBO == fGLCaps.fMSFBOType) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001724 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001725 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1726 GL_CALL(Scissor(r.fLeft, r.fBottom,
1727 r.fWidth, r.fHeight));
1728 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001729 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 fHWBounds.fScissorEnabled = true;
1731 } else {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001732 if (GLCaps::kDesktopARB_MSFBO != fGLCaps.fMSFBOType) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001733 // this respects the scissor during the blit, so disable it.
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001734 GrAssert(GLCaps::kDesktopEXT_MSFBO == fGLCaps.fMSFBOType);
1735 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001736 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001737 int right = r.fLeft + r.fWidth;
1738 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001739 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1740 r.fLeft, r.fBottom, right, top,
1741 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001743 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001744 }
1745}
1746
twiz@google.com0f31ca72011-03-18 17:38:11 +00001747static const GrGLenum grToGLStencilFunc[] = {
1748 GR_GL_ALWAYS, // kAlways_StencilFunc
1749 GR_GL_NEVER, // kNever_StencilFunc
1750 GR_GL_GREATER, // kGreater_StencilFunc
1751 GR_GL_GEQUAL, // kGEqual_StencilFunc
1752 GR_GL_LESS, // kLess_StencilFunc
1753 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1754 GR_GL_EQUAL, // kEqual_StencilFunc,
1755 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001756};
1757GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1758GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1759GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1760GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1761GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1762GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1763GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1764GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1765GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1766
twiz@google.com0f31ca72011-03-18 17:38:11 +00001767static const GrGLenum grToGLStencilOp[] = {
1768 GR_GL_KEEP, // kKeep_StencilOp
1769 GR_GL_REPLACE, // kReplace_StencilOp
1770 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1771 GR_GL_INCR, // kIncClamp_StencilOp
1772 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1773 GR_GL_DECR, // kDecClamp_StencilOp
1774 GR_GL_ZERO, // kZero_StencilOp
1775 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001776};
1777GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1778GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1779GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1780GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1781GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1782GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1783GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1784GR_STATIC_ASSERT(6 == kZero_StencilOp);
1785GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1786
reed@google.comac10a2d2010-12-22 21:39:39 +00001787void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001788 const GrDrawState& drawState = this->getDrawState();
1789
1790 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001791
1792 // use stencil for clipping if clipping is enabled and the clip
1793 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001794 bool stencilClip = fClipInStencil && drawState.isClipState();
1795 bool drawClipToStencil =
1796 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001797 bool stencilChange = fHWStencilClip != stencilClip ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001798 fHWDrawState.getStencil() != *settings ||
1799 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1800 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001801
1802 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001803
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001804 // we can't simultaneously perform stencil-clipping and
1805 // modify the stencil clip
1806 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001807
bsalomon@google.comd302f142011-03-03 13:54:13 +00001808 if (settings->isDisabled()) {
1809 if (stencilClip) {
1810 settings = &gClipStencilSettings;
1811 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001812 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001813
1814 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001815 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001816 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001817 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001818 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001819 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001820 GrAssert(settings->fFrontPassOp != kIncWrap_StencilOp);
1821 GrAssert(settings->fFrontPassOp != kDecWrap_StencilOp);
1822 GrAssert(settings->fFrontFailOp != kIncWrap_StencilOp);
1823 GrAssert(settings->fBackFailOp != kDecWrap_StencilOp);
1824 GrAssert(settings->fBackPassOp != kIncWrap_StencilOp);
1825 GrAssert(settings->fBackPassOp != kDecWrap_StencilOp);
1826 GrAssert(settings->fBackFailOp != kIncWrap_StencilOp);
1827 GrAssert(settings->fFrontFailOp != kDecWrap_StencilOp);
1828 }
1829 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001830 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001831 GrStencilBuffer* stencilBuffer =
1832 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001833 if (NULL != stencilBuffer) {
1834 stencilBits = stencilBuffer->bits();
1835 }
1836 // TODO: dynamically attach a stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +00001837 GrAssert(stencilBits ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001838 (GrStencilSettings::gDisabled == *settings));
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001839
1840 GrGLuint clipStencilMask = 0;
1841 GrGLuint userStencilMask = ~0;
1842 if (stencilBits > 0) {
1843 clipStencilMask = 1 << (stencilBits - 1);
1844 userStencilMask = clipStencilMask - 1;
1845 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001846
1847 unsigned int frontRef = settings->fFrontFuncRef;
1848 unsigned int frontMask = settings->fFrontFuncMask;
1849 unsigned int frontWriteMask = settings->fFrontWriteMask;
twiz@google.com0f31ca72011-03-18 17:38:11 +00001850 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001851
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001852 if (drawClipToStencil) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001853 GrAssert(settings->fFrontFunc < kBasicStencilFuncCount);
1854 frontFunc = grToGLStencilFunc[settings->fFrontFunc];
1855 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001856 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
1857 stencilClip, settings->fFrontFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001858
1859 ConvertStencilFuncAndMask(settings->fFrontFunc,
1860 stencilClip,
1861 clipStencilMask,
1862 userStencilMask,
1863 &frontRef,
1864 &frontMask);
1865 frontWriteMask &= userStencilMask;
1866 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001867 GrAssert((size_t)
1868 settings->fFrontFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1869 GrAssert((size_t)
1870 settings->fFrontPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
1871 GrAssert((size_t)
1872 settings->fBackFailOp < GR_ARRAY_COUNT(grToGLStencilOp));
1873 GrAssert((size_t)
1874 settings->fBackPassOp < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001875 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001876 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001877
1878 unsigned int backRef = settings->fBackFuncRef;
1879 unsigned int backMask = settings->fBackFuncMask;
1880 unsigned int backWriteMask = settings->fBackWriteMask;
1881
1882
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001883 if (drawClipToStencil) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001884 GrAssert(settings->fBackFunc < kBasicStencilFuncCount);
1885 backFunc = grToGLStencilFunc[settings->fBackFunc];
1886 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001887 backFunc = grToGLStencilFunc[ConvertStencilFunc(
1888 stencilClip, settings->fBackFunc)];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001889 ConvertStencilFuncAndMask(settings->fBackFunc,
1890 stencilClip,
1891 clipStencilMask,
1892 userStencilMask,
1893 &backRef,
1894 &backMask);
1895 backWriteMask &= userStencilMask;
1896 }
1897
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001898 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1899 frontRef, frontMask));
1900 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1901 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1902 backRef, backMask));
1903 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1904 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
1905 grToGLStencilOp[settings->fFrontFailOp],
1906 grToGLStencilOp[settings->fFrontPassOp],
1907 grToGLStencilOp[settings->fFrontPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001908
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001909 GL_CALL(StencilOpSeparate(GR_GL_BACK,
1910 grToGLStencilOp[settings->fBackFailOp],
1911 grToGLStencilOp[settings->fBackPassOp],
1912 grToGLStencilOp[settings->fBackPassOp]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001913 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001914 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1915 GL_CALL(StencilMask(frontWriteMask));
1916 GL_CALL(StencilOp(grToGLStencilOp[settings->fFrontFailOp],
bsalomon@google.comd302f142011-03-03 13:54:13 +00001917 grToGLStencilOp[settings->fFrontPassOp],
1918 grToGLStencilOp[settings->fFrontPassOp]));
1919 }
1920 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001921 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001922 fHWStencilClip = stencilClip;
1923 }
1924}
1925
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001926void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001927 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001928 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001929 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1930 // smooth lines.
1931
1932 // we prefer smooth lines over multisampled lines
1933 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001934 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001935 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001936 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001938 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001939 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001940 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001941 fHWAAState.fSmoothLineEnabled = false;
1942 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001943 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001944 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001945 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001946 fHWAAState.fMSAAEnabled = false;
1947 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001948 } else if (rt->isMultisampled() &&
1949 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001950 fHWAAState.fMSAAEnabled) {
1951 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001952 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001953 fHWAAState.fMSAAEnabled = false;
1954 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001955 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001956 fHWAAState.fMSAAEnabled = true;
1957 }
1958 }
1959 }
1960}
1961
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001962void GrGpuGL::flushBlend(GrPrimitiveType type,
1963 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001964 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001965 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001966 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001967 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001968 fHWBlendDisabled = false;
1969 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001970 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1971 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001972 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1973 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001974 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001975 }
1976 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001977 // any optimization to disable blending should
1978 // have already been applied and tweaked the coeffs
1979 // to (1, 0).
1980 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1981 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001982 if (fHWBlendDisabled != blendOff) {
1983 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001984 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001985 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001986 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001987 }
1988 fHWBlendDisabled = blendOff;
1989 }
1990 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001991 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
1992 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001993 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1994 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001995 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001996 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001997 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001998 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1999 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002000 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002001
2002 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002003 GrColorUnpackR(blendConst) / 255.f,
2004 GrColorUnpackG(blendConst) / 255.f,
2005 GrColorUnpackB(blendConst) / 255.f,
2006 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002007 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002008 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002009 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002010 }
2011 }
2012 }
2013}
2014
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002015namespace {
2016
2017unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002018 switch (filter) {
2019 case GrSamplerState::kBilinear_Filter:
2020 case GrSamplerState::k4x4Downsample_Filter:
2021 return GR_GL_LINEAR;
2022 case GrSamplerState::kNearest_Filter:
2023 case GrSamplerState::kConvolution_Filter:
2024 return GR_GL_NEAREST;
2025 default:
2026 GrAssert(!"Unknown filter type");
2027 return GR_GL_LINEAR;
2028 }
2029}
2030
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002031const GrGLenum* get_swizzle(GrPixelConfig config,
2032 const GrSamplerState& sampler) {
2033 if (GrPixelConfigIsAlphaOnly(config)) {
2034 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2035 GR_GL_ALPHA, GR_GL_ALPHA };
2036 return gAlphaSmear;
2037 } else if (sampler.swapsRAndB()) {
2038 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2039 GR_GL_RED, GR_GL_ALPHA };
2040 return gRedBlueSwap;
2041 } else {
2042 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2043 GR_GL_BLUE, GR_GL_ALPHA };
2044 return gStraight;
2045 }
2046}
2047
2048void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2049 // should add texparameteri to interface to make 1 instead of 4 calls here
2050 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2051 GR_GL_TEXTURE_SWIZZLE_R,
2052 swizzle[0]));
2053 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2054 GR_GL_TEXTURE_SWIZZLE_G,
2055 swizzle[1]));
2056 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2057 GR_GL_TEXTURE_SWIZZLE_B,
2058 swizzle[2]));
2059 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2060 GR_GL_TEXTURE_SWIZZLE_A,
2061 swizzle[3]));
2062}
2063}
2064
bsalomon@google.comffca4002011-02-22 20:34:01 +00002065bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002066
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002067 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002068 // GrGpu::setupClipAndFlushState should have already checked this
2069 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002070 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002071
tomhudson@google.com93813632011-10-27 20:21:16 +00002072 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002073 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002074 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002075 GrGLTexture* nextTexture =
2076 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002077
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002078 // true for now, but maybe not with GrEffect.
2079 GrAssert(NULL != nextTexture);
2080 // if we created a rt/tex and rendered to it without using a
2081 // texture and now we're texuring from the rt it will still be
2082 // the last bound texture, but it needs resolving. So keep this
2083 // out of the "last != next" check.
2084 GrGLRenderTarget* texRT =
2085 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2086 if (NULL != texRT) {
2087 resolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002088 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002089
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002090 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002091 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002092 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002093 #if GR_COLLECT_STATS
2094 ++fStats.fTextureChngCnt;
2095 #endif
2096 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002097 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002098 // The texture matrix has to compensate for texture width/height
2099 // and NPOT-embedded-in-POT
2100 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002101 }
2102
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002103 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002104 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002105 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002106 nextTexture->getCachedTexParams(&timestamp);
2107 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002108 GrGLTexture::TexParams newTexParams;
2109
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002110 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002111
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002112 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002113 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2114 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002115 memcpy(newTexParams.fSwizzleRGBA,
2116 get_swizzle(nextTexture->config(), sampler),
2117 sizeof(newTexParams.fSwizzleRGBA));
2118 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002119 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002120 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002121 GR_GL_TEXTURE_MAG_FILTER,
2122 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002123 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002124 GR_GL_TEXTURE_MIN_FILTER,
2125 newTexParams.fFilter));
2126 }
2127 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2128 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002129 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002130 GR_GL_TEXTURE_WRAP_S,
2131 newTexParams.fWrapS));
2132 }
2133 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2134 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002135 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002136 GR_GL_TEXTURE_WRAP_T,
2137 newTexParams.fWrapT));
2138 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002139 if (this->glCaps().fTextureSwizzleSupport &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002140 (setAll ||
2141 memcmp(newTexParams.fSwizzleRGBA,
2142 oldTexParams.fSwizzleRGBA,
2143 sizeof(newTexParams.fSwizzleRGBA)))) {
2144 setTextureUnit(s);
2145 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2146 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002147 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002148 nextTexture->setCachedTexParams(newTexParams,
2149 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002150 }
2151 }
2152
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002153 GrIRect* rect = NULL;
2154 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002155 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002156 fClip.hasConservativeBounds()) {
2157 fClip.getConservativeBounds().roundOut(&clipBounds);
2158 rect = &clipBounds;
2159 }
2160 this->flushRenderTarget(rect);
2161 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002162
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002163 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2164 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002166 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002167 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002168 }
2169 }
2170
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002171 if (drawState->isColorWriteDisabled() !=
2172 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002173 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002174 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002175 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002176 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002177 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002178 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002179 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002180 }
2181
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002182 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2183 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002184 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002185 GL_CALL(Enable(GR_GL_CULL_FACE));
2186 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002187 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002188 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002189 GL_CALL(Enable(GR_GL_CULL_FACE));
2190 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002191 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002192 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002193 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002194 break;
2195 default:
2196 GrCrash("Unknown draw face.");
2197 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002198 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002199 }
2200
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002201#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002202 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002203 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002204 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002205 NULL == drawState->getRenderTarget() ||
2206 NULL == drawState->getTexture(s) ||
2207 drawState->getTexture(s)->asRenderTarget() !=
2208 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002209 }
2210#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002211
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002212 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002213
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002214 // This copy must happen after flushStencil() is called. flushStencil()
2215 // relies on detecting when the kModifyStencilClip_StateBit state has
2216 // changed since the last draw.
2217 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002218 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002219}
2220
2221void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002222 if (fHWGeometryState.fVertexBuffer != buffer) {
2223 fHWGeometryState.fArrayPtrsDirty = true;
2224 fHWGeometryState.fVertexBuffer = buffer;
2225 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002226}
2227
2228void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002229 if (fHWGeometryState.fVertexBuffer == buffer) {
2230 // deleting bound buffer does implied bind to 0
2231 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002232 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002233 }
2234}
2235
2236void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002237 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002238}
2239
2240void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002241 if (fHWGeometryState.fIndexBuffer == buffer) {
2242 // deleting bound buffer does implied bind to 0
2243 fHWGeometryState.fIndexBuffer = NULL;
2244 }
2245}
2246
reed@google.comac10a2d2010-12-22 21:39:39 +00002247void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2248 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002249 GrDrawState* drawState = this->drawState();
2250 if (drawState->getRenderTarget() == renderTarget) {
2251 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002252 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002253 if (fHWDrawState.getRenderTarget() == renderTarget) {
2254 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002256}
2257
2258void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002259 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002260 GrDrawState* drawState = this->drawState();
2261 if (drawState->getTexture(s) == texture) {
2262 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002263 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002264 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002265 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002266 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002267 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002268 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002269}
2270
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002271bool GrGpuGL::canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +00002272 GrGLenum* internalFormat,
bsalomon@google.com6f379512011-11-16 20:36:03 +00002273 GrGLenum* externalFormat,
2274 GrGLenum* externalType) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002275 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002276 case kRGBA_8888_PM_GrPixelConfig:
2277 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002278 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002279 *externalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002280 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002281 break;
2282 case kBGRA_8888_PM_GrPixelConfig:
2283 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002284 if (!fGLCaps.fBGRAFormatSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002285 return false;
2286 }
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002287 if (fGLCaps.fBGRAIsInternalFormat) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002288 *internalFormat = GR_GL_BGRA;
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002289 } else {
2290 *internalFormat = GR_GL_RGBA;
2291 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002292 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002293 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002294 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002295 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002296 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002297 *externalFormat = GR_GL_RGB;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002298 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002299 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002300 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002301 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002302 *externalFormat = GR_GL_RGBA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002303 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002304 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002305 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002306 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002307 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002308 // glCompressedTexImage doesn't take external params
2309 *externalFormat = GR_GL_PALETTE8_RGBA8;
2310 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002311 } else {
2312 return false;
2313 }
2314 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002315 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002316 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002317 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002318 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 break;
2320 default:
2321 return false;
2322 }
2323 return true;
2324}
2325
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002326void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002327 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002328 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002329 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002330 fActiveTextureUnitIdx = unit;
2331 }
2332}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002333
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002334void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002335 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002336 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002337 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2338 }
2339}
2340
reed@google.comac10a2d2010-12-22 21:39:39 +00002341/* On ES the internalFormat and format must match for TexImage and we use
2342 GL_RGB, GL_RGBA for color formats. We also generally like having the driver
2343 decide the internalFormat. However, on ES internalFormat for
2344 RenderBufferStorage* has to be a specific format (not a base format like
2345 GL_RGBA).
2346 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002347bool GrGpuGL::fboInternalFormat(GrPixelConfig config, GrGLenum* format) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002348 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002349 // The ES story for BGRA and RenderbufferStorage appears murky. It
2350 // takes an internal format as a parameter. The OES FBO extension and
2351 // 2.0 spec don't refer to BGRA as it's not part of the core. One ES
2352 // BGRA extensions adds BGRA as both an internal and external format
2353 // and the other only as an external format (like desktop GL). OES
2354 // restricts RenderbufferStorage's format to a *sized* internal format.
2355 // There is no sized BGRA internal format.
2356 // So if the texture has internal format BGRA we just hope that the
2357 // resolve blit can do RGBA->BGRA internal format conversion.
2358 case kRGBA_8888_PM_GrPixelConfig:
2359 case kRGBA_8888_UPM_GrPixelConfig:
2360 case kBGRA_8888_PM_GrPixelConfig:
2361 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002362 if (fGLCaps.fRGBA8RenderbufferSupport) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002363 // The GL_OES_rgba8_rgb8 extension defines GL_RGBA8 as a sized
2364 // internal format.
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002365 *format = GR_GL_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002366 return true;
2367 } else {
2368 return false;
2369 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002370 case kRGB_565_GrPixelConfig:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002371 // ES2 supports 565, but desktop GL does not.
bsalomon@google.comc4364992011-11-07 15:54:49 +00002372 if (kDesktop_GrGLBinding != this->glBinding()) {
2373 *format = GR_GL_RGB565;
2374 return true;
2375 } else {
2376 return false;
2377 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002378 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002379 *format = GR_GL_RGBA4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002380 return true;
2381 default:
2382 return false;
2383 }
2384}
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002385
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002386void GrGpuGL::resetDirtyFlags() {
2387 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2388}
2389
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002390void GrGpuGL::setBuffers(bool indexed,
2391 int* extraVertexOffset,
2392 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002393
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002394 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002395
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002396 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2397
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002398 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002399 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002400 case kBuffer_GeometrySrcType:
2401 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002402 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002403 break;
2404 case kArray_GeometrySrcType:
2405 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002406 this->finalizeReservedVertices();
2407 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2408 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002409 break;
2410 default:
2411 vbuf = NULL; // suppress warning
2412 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002413 }
2414
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002415 GrAssert(NULL != vbuf);
2416 GrAssert(!vbuf->isLocked());
2417 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002418 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002419 fHWGeometryState.fArrayPtrsDirty = true;
2420 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002421 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002422
2423 if (indexed) {
2424 GrAssert(NULL != extraIndexOffset);
2425
2426 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002427 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002428 case kBuffer_GeometrySrcType:
2429 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002430 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002431 break;
2432 case kArray_GeometrySrcType:
2433 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002434 this->finalizeReservedIndices();
2435 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2436 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002437 break;
2438 default:
2439 ibuf = NULL; // suppress warning
2440 GrCrash("Unknown geometry src type!");
2441 }
2442
2443 GrAssert(NULL != ibuf);
2444 GrAssert(!ibuf->isLocked());
2445 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002446 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002447 fHWGeometryState.fIndexBuffer = ibuf;
2448 }
2449 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002450}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002451
2452int GrGpuGL::getMaxEdges() const {
2453 // FIXME: This is a pessimistic estimate based on how many other things
2454 // want to add uniforms. This should be centralized somewhere.
tomhudson@google.com93813632011-10-27 20:21:16 +00002455 return GR_CT_MIN(fGLCaps.fMaxFragmentUniformVectors - 8,
2456 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002457}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002458
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002459void GrGpuGL::GLCaps::print() const {
2460 for (int i = 0; i < fStencilFormats.count(); ++i) {
2461 GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n",
2462 i,
2463 fStencilFormats[i].fStencilBits,
2464 fStencilFormats[i].fTotalBits);
2465 }
2466
2467 GR_STATIC_ASSERT(0 == kNone_MSFBO);
2468 GR_STATIC_ASSERT(1 == kDesktopARB_MSFBO);
2469 GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBO);
2470 GR_STATIC_ASSERT(3 == kAppleES_MSFBO);
2471 static const char* gMSFBOExtStr[] = {
2472 "None",
2473 "ARB",
2474 "EXT",
2475 "Apple",
2476 };
2477 GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]);
bsalomon@google.com2caf69d2011-09-23 14:27:29 +00002478 for (int i = 0; i < (int)GR_ARRAY_COUNT(fAASamples); ++i) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002479 GrPrintf("AA Level %d has %d samples\n", i, fAASamples[i]);
2480 }
2481 GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors);
2482 GrPrintf("Support RGBA8 Render Buffer: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002483 (fRGBA8RenderbufferSupport ? "YES": "NO"));
bsalomon@google.comc4364992011-11-07 15:54:49 +00002484 GrPrintf("BGRA is an internal format: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002485 (fBGRAIsInternalFormat ? "YES": "NO"));
bsalomon@google.com85b505b2011-11-07 14:56:51 +00002486 GrPrintf("Support texture swizzle: %s\n",
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002487 (fTextureSwizzleSupport ? "YES": "NO"));
2488 GrPrintf("Unpack Row length support: %s\n",
2489 (fUnpackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00002490 GrPrintf("Unpack Flip Y support: %s\n",
2491 (fUnpackFlipYSupport ? "YES": "NO"));
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002492 GrPrintf("Pack Row length support: %s\n",
2493 (fPackRowLengthSupport ? "YES": "NO"));
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002494 GrPrintf("Pack Flip Y support: %s\n",
2495 (fPackFlipYSupport ? "YES": "NO"));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002496}