blob: 9114a0e9cf478105092505d380f5fd43f0278e37 [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
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
31#else
32 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000037
38///////////////////////////////////////////////////////////////////////////////
39
twiz@google.com0f31ca72011-03-18 17:38:11 +000040static const GrGLenum gXfermodeCoeff2Blend[] = {
41 GR_GL_ZERO,
42 GR_GL_ONE,
43 GR_GL_SRC_COLOR,
44 GR_GL_ONE_MINUS_SRC_COLOR,
45 GR_GL_DST_COLOR,
46 GR_GL_ONE_MINUS_DST_COLOR,
47 GR_GL_SRC_ALPHA,
48 GR_GL_ONE_MINUS_SRC_ALPHA,
49 GR_GL_DST_ALPHA,
50 GR_GL_ONE_MINUS_DST_ALPHA,
51 GR_GL_CONSTANT_COLOR,
52 GR_GL_ONE_MINUS_CONSTANT_COLOR,
53 GR_GL_CONSTANT_ALPHA,
54 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000055
56 // extended blend coeffs
57 GR_GL_SRC1_COLOR,
58 GR_GL_ONE_MINUS_SRC1_COLOR,
59 GR_GL_SRC1_ALPHA,
60 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000061};
62
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000064 static const bool gCoeffReferencesBlendConst[] = {
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 true,
76 true,
77 true,
78 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000079
80 // extended blend coeffs
81 false,
82 false,
83 false,
84 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000085 };
86 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com271cffc2011-05-20 14:13:56 +000087 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
88
89 GR_STATIC_ASSERT(0 == kZero_BlendCoeff);
90 GR_STATIC_ASSERT(1 == kOne_BlendCoeff);
91 GR_STATIC_ASSERT(2 == kSC_BlendCoeff);
92 GR_STATIC_ASSERT(3 == kISC_BlendCoeff);
93 GR_STATIC_ASSERT(4 == kDC_BlendCoeff);
94 GR_STATIC_ASSERT(5 == kIDC_BlendCoeff);
95 GR_STATIC_ASSERT(6 == kSA_BlendCoeff);
96 GR_STATIC_ASSERT(7 == kISA_BlendCoeff);
97 GR_STATIC_ASSERT(8 == kDA_BlendCoeff);
98 GR_STATIC_ASSERT(9 == kIDA_BlendCoeff);
99 GR_STATIC_ASSERT(10 == kConstC_BlendCoeff);
100 GR_STATIC_ASSERT(11 == kIConstC_BlendCoeff);
101 GR_STATIC_ASSERT(12 == kConstA_BlendCoeff);
102 GR_STATIC_ASSERT(13 == kIConstA_BlendCoeff);
103
104 GR_STATIC_ASSERT(14 == kS2C_BlendCoeff);
105 GR_STATIC_ASSERT(15 == kIS2C_BlendCoeff);
106 GR_STATIC_ASSERT(16 == kS2A_BlendCoeff);
107 GR_STATIC_ASSERT(17 == kIS2A_BlendCoeff);
108
109 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
110 GR_STATIC_ASSERT(kTotalBlendCoeffCount == GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000111}
112
reed@google.comac10a2d2010-12-22 21:39:39 +0000113///////////////////////////////////////////////////////////////////////////////
114
bsalomon@google.comd302f142011-03-03 13:54:13 +0000115void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture,
116 GrSamplerState::SampleMode mode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000117 GrMatrix* matrix) {
118 GrAssert(NULL != texture);
119 GrAssert(NULL != matrix);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120 GrGLTexture::Orientation orientation = texture->orientation();
121 if (GrGLTexture::kBottomUp_Orientation == orientation) {
122 GrMatrix invY;
123 invY.setAll(GR_Scalar1, 0, 0,
124 0, -GR_Scalar1, GR_Scalar1,
125 0, 0, GrMatrix::I()[8]);
126 matrix->postConcat(invY);
127 } else {
128 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
129 }
130}
131
bsalomon@google.comd302f142011-03-03 13:54:13 +0000132bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000133 const GrSamplerState& sampler) {
134 GrAssert(NULL != texture);
135 if (!sampler.getMatrix().isIdentity()) {
136 return false;
137 }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000138 GrGLTexture::Orientation orientation = texture->orientation();
139 if (GrGLTexture::kBottomUp_Orientation == orientation) {
140 return false;
141 } else {
142 GrAssert(GrGLTexture::kTopDown_Orientation == orientation);
143 }
144 return true;
145}
146
147///////////////////////////////////////////////////////////////////////////////
148
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000149static bool gPrintStartupSpew;
150
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000151static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000152
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000153 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000154
twiz@google.com0f31ca72011-03-18 17:38:11 +0000155 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000156 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
157 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000158 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000159 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
160 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000161 // some implementations require texture to be mip-map complete before
162 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000163 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000164 GR_GL_TEXTURE_MIN_FILTER,
165 GR_GL_NEAREST));
166 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
167 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
168 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
169 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
170 GR_GL_COLOR_ATTACHMENT0,
171 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000172 GrGLenum status;
173 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000174 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
175 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000176
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000177 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000178}
179
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000180GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
181
182 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000183
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000184 fillInConfigRenderableTable();
185
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000186 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000187
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000188 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000189
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000190 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000191 const GrGLubyte* ext;
192 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000193 const GrGLubyte* vendor;
194 const GrGLubyte* renderer;
195 const GrGLubyte* version;
196 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
197 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
198 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000199 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
200 this);
201 GrPrintf("------ VENDOR %s\n", vendor);
202 GrPrintf("------ RENDERER %s\n", renderer);
203 GrPrintf("------ VERSION %s\n", version);
204 GrPrintf("------ EXTENSIONS\n %s \n", ext);
205 }
206
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000207 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000208
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000209 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000210
bsalomon@google.comfe676522011-06-17 18:12:21 +0000211 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000212 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
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();
reed@google.comac10a2d2010-12-22 21:39:39 +0000221}
222
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000223///////////////////////////////////////////////////////////////////////////////
224
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000225void GrGpuGL::initCaps() {
226 GrGLint maxTextureUnits;
227 // check FS and fixed-function texture unit limits
228 // we only use textures in the fragment stage currently.
229 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000230 const GrGLInterface* gl = this->glInterface();
231 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000232 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000233 if (kES2_GrGLBinding != this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000234 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000235 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000236 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237
238 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000239 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000240 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000241 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000242 for (int i = 0; i < numFormats; ++i) {
243 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
244 fCaps.f8BitPaletteSupport = true;
245 break;
246 }
247 }
248
249 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 // we could also look for GL_ATI_separate_stencil extension or
251 // GL_EXT_stencil_two_side but they use different function signatures
252 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000253 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000254 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000255 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000256 this->hasExtension("GL_EXT_stencil_wrap");
257 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000258 // ES 2 has two sided stencil and stencil wrap
259 fCaps.fTwoSidedStencilSupport = true;
260 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000261 }
262
263 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000264 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
265 // extension includes glMapBuffer.
266 } else {
267 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
268 }
269
270 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000271 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000272 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
273 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000274 } else {
275 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000276 }
277 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000278 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000279 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000280 }
281
282 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
283
284 ////////////////////////////////////////////////////////////////////////////
285 // Experiments to determine limitations that can't be queried.
286 // TODO: Make these a preprocess that generate some compile time constants.
287 // TODO: probe once at startup, rather than once per context creation.
288
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000289 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
290 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000291 // Our render targets are always created with textures as the color
292 // attachment, hence this min:
293 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
294
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000295 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000296}
297
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000298void GrGpuGL::fillInConfigRenderableTable() {
299
300 // OpenGL < 3.0
301 // no support for render targets unless the GL_ARB_framebuffer_object
302 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
303 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
304 // probably don't get R8 in this case.
305
306 // OpenGL 3.0
307 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
308 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
309
310 // >= OpenGL 3.1
311 // base color renderable: RED, RG, RGB, and RGBA
312 // sized derivatives: R8, RGBA4, RGBA8
313 // if the GL_ARB_compatibility extension is supported then we get back
314 // support for GL_ALPHA and ALPHA8
315
316 // GL_EXT_bgra adds BGRA render targets to any version
317
318 // ES 2.0
319 // color renderable: RGBA4, RGB5_A1, RGB565
320 // GL_EXT_texture_rg adds support for R8 as a color render target
321 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
322 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
323 // added BGRA support
324
325 if (kDesktop_GrGLBinding == this->glBinding()) {
326 // Post 3.0 we will get R8
327 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
328 if (this->glVersion() >= GR_GL_VER(3,0) ||
329 this->hasExtension("GL_ARB_framebuffer_object")) {
330 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
331 }
332 } else {
333 // On ES we can only hope for R8
334 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
335 this->glCaps().textureRedSupport();
336 }
337
338 if (kDesktop_GrGLBinding != this->glBinding()) {
339 // only available in ES
340 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
341 }
342
343 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
344 // GL_EXT_framebuffer_object for FBO support. Both of these
345 // allow RGBA4 render targets so this is always supported.
346 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
347
348 if (this->glCaps().rgba8RenderbufferSupport()) {
349 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
350 }
351
352 if (this->glCaps().bgraFormatSupport()) {
353 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
354 }
355
356 // the un-premultiplied formats just inherit the premultiplied setting
357 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
358 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
359 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
360 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
361}
362
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000363bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
364 if (kUnknown_CanPreserveUnpremulRoundtrip ==
365 fCanPreserveUnpremulRoundtrip) {
366
367 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
368 uint32_t* srcData = data.get();
369 uint32_t* firstRead = data.get() + 256 * 256;
370 uint32_t* secondRead = data.get() + 2 * 256 * 256;
371
372 for (int y = 0; y < 256; ++y) {
373 for (int x = 0; x < 256; ++x) {
374 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
375 color[3] = y;
376 color[2] = x;
377 color[1] = x;
378 color[0] = x;
379 }
380 }
381
382 // We have broader support for read/write pixels on render targets
383 // than on textures.
384 GrTextureDesc dstDesc;
385 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
386 kNoStencil_GrTextureFlagBit;
387 dstDesc.fWidth = 256;
388 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000389 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000390 dstDesc.fSampleCnt = 0;
391
392 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
393 if (!dstTex.get()) {
394 return false;
395 }
396 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
397 GrAssert(NULL != rt);
398
399 bool failed = true;
400 static const UnpremulConversion gMethods[] = {
401 kUpOnWrite_DownOnRead_UnpremulConversion,
402 kDownOnWrite_UpOnRead_UnpremulConversion,
403 };
404
405 // pretend that we can do the roundtrip to avoid recursive calls to
406 // this function
407 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
408 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
409 fUnpremulConversion = gMethods[i];
410 rt->writePixels(0, 0,
411 256, 256,
412 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
413 rt->readPixels(0, 0,
414 256, 256,
415 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
416 rt->writePixels(0, 0,
417 256, 256,
418 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
419 rt->readPixels(0, 0,
420 256, 256,
421 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
422 failed = false;
423 for (int j = 0; j < 256 * 256; ++j) {
424 if (firstRead[j] != secondRead[j]) {
425 failed = true;
426 break;
427 }
428 }
429 }
430 fCanPreserveUnpremulRoundtrip = failed ?
431 kNo_CanPreserveUnpremulRoundtrip :
432 kYes_CanPreserveUnpremulRoundtrip;
433 }
434
435 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
436 return true;
437 } else {
438 return false;
439 }
440}
441
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000442GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000443 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
444 return GrPixelConfigSwapRAndB(config);
445 } else {
446 return config;
447 }
448}
449
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000450GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000451 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000452 return GrPixelConfigSwapRAndB(config);
453 } else {
454 return config;
455 }
456}
457
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000458bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
459 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
460}
461
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000462void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000463 if (gPrintStartupSpew && !fPrintedCaps) {
464 fPrintedCaps = true;
465 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000466 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000467 }
468
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000469 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000471 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000472
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000473 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000474 GL_CALL(Disable(GR_GL_DEPTH_TEST));
475 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000476
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000477 GL_CALL(Disable(GR_GL_CULL_FACE));
478 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000479 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000480
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000481 GL_CALL(Disable(GR_GL_DITHER));
482 if (kDesktop_GrGLBinding == this->glBinding()) {
483 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
484 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
485 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000486 fHWAAState.fMSAAEnabled = false;
487 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000488 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000489
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000490 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000491 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000492
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000494 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000496 // invalid
497 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000498
reed@google.comac10a2d2010-12-22 21:39:39 +0000499 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000500 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000501
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000502 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000503 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000504
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000505 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000506
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000507 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000508
tomhudson@google.com93813632011-10-27 20:21:16 +0000509 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000510 fHWDrawState.setTexture(s, NULL);
511 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
512 -GR_ScalarMax,
513 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000514 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000515 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000516 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000517
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000518 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000519 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000520 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000521 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000522
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000523 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000524 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000525 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000526
527 fHWGeometryState.fIndexBuffer = NULL;
528 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000529
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000530 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000531
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000532 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000533 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000534
535 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000536 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000537 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
538 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000539 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000540 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
541 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000542 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000543 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
544 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000545 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000546 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
547 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000548}
549
bsalomon@google.come269f212011-11-07 13:29:52 +0000550GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000551 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000552 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000553 return NULL;
554 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000555
bsalomon@google.com99621082011-11-15 16:47:16 +0000556 glTexDesc.fWidth = desc.fWidth;
557 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000558 glTexDesc.fConfig = desc.fConfig;
559 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
560 glTexDesc.fOwnsID = false;
561 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
562
563 GrGLTexture* texture = NULL;
564 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
565 GrGLRenderTarget::Desc glRTDesc;
566 glRTDesc.fRTFBOID = 0;
567 glRTDesc.fTexFBOID = 0;
568 glRTDesc.fMSColorRenderbufferID = 0;
569 glRTDesc.fOwnIDs = true;
570 glRTDesc.fConfig = desc.fConfig;
571 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000572 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
573 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000574 glTexDesc.fTextureID,
575 &glRTDesc)) {
576 return NULL;
577 }
578 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
579 } else {
580 texture = new GrGLTexture(this, glTexDesc);
581 }
582 if (NULL == texture) {
583 return NULL;
584 }
585
586 this->setSpareTextureUnit();
587 return texture;
588}
589
590GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
591 GrGLRenderTarget::Desc glDesc;
592 glDesc.fConfig = desc.fConfig;
593 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
594 glDesc.fMSColorRenderbufferID = 0;
595 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
596 glDesc.fSampleCnt = desc.fSampleCnt;
597 glDesc.fOwnIDs = false;
598 GrGLIRect viewport;
599 viewport.fLeft = 0;
600 viewport.fBottom = 0;
601 viewport.fWidth = desc.fWidth;
602 viewport.fHeight = desc.fHeight;
603
604 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
605 if (desc.fStencilBits) {
606 GrGLStencilBuffer::Format format;
607 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
608 format.fPacked = false;
609 format.fStencilBits = desc.fStencilBits;
610 format.fTotalBits = desc.fStencilBits;
611 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
612 0,
613 desc.fWidth,
614 desc.fHeight,
615 desc.fSampleCnt,
616 format);
617 tgt->setStencilBuffer(sb);
618 sb->unref();
619 }
620 return tgt;
621}
622
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000623////////////////////////////////////////////////////////////////////////////////
624
bsalomon@google.com6f379512011-11-16 20:36:03 +0000625void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
626 int left, int top, int width, int height,
627 GrPixelConfig config, const void* buffer,
628 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000629 if (NULL == buffer) {
630 return;
631 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000632 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
633
bsalomon@google.com6f379512011-11-16 20:36:03 +0000634 this->setSpareTextureUnit();
635 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
636 GrGLTexture::Desc desc;
637 desc.fConfig = glTex->config();
638 desc.fWidth = glTex->width();
639 desc.fHeight = glTex->height();
640 desc.fOrientation = glTex->orientation();
641 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000642
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000643 this->uploadTexData(desc, false,
644 left, top, width, height,
645 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000646}
647
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000648namespace {
649bool adjust_pixel_ops_params(int surfaceWidth,
650 int surfaceHeight,
651 size_t bpp,
652 int* left, int* top, int* width, int* height,
653 const void** data,
654 size_t* rowBytes) {
655 if (!*rowBytes) {
656 *rowBytes = *width * bpp;
657 }
658
659 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
660 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
661
662 if (!subRect.intersect(bounds)) {
663 return false;
664 }
665 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
666 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
667
668 *left = subRect.fLeft;
669 *top = subRect.fTop;
670 *width = subRect.width();
671 *height = subRect.height();
672 return true;
673}
674}
675
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000676bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
677 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000678 int left, int top, int width, int height,
679 GrPixelConfig dataConfig,
680 const void* data,
681 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000682 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000683
684 size_t bpp = GrBytesPerPixel(dataConfig);
685 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
686 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000687 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000688 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000689 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000690
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000691 // in case we need a temporary, trimmed copy of the src pixels
692 SkAutoSMalloc<128 * 128> tempStorage;
693
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000694 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000695 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000696 if (useTexStorage) {
697 if (kDesktop_GrGLBinding == this->glBinding()) {
698 // 565 is not a sized internal format on desktop GL. So on desktop
699 // with 565 we always use an unsized internal format to let the
700 // system pick the best sized format to convert the 565 data to.
701 // Since glTexStorage only allows sized internal formats we will
702 // instead fallback to glTexImage2D.
703 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
704 } else {
705 // ES doesn't allow paletted textures to be used with tex storage
706 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
707 }
708 }
709
710 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000711 GrGLenum externalFormat;
712 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000713 // glTexStorage requires sized internal formats on both desktop and ES. ES
714 // glTexImage requires an unsized format.
715 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
716 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000717 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000718 }
719
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000720 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000721 // paletted textures cannot be updated
722 return false;
723 }
724
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000725 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000726 * check whether to allocate a temporary buffer for flipping y or
727 * because our srcData has extra bytes past each row. If so, we need
728 * to trim those off here, since GL ES may not let us specify
729 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000730 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000731 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000732 bool swFlipY = false;
733 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000734 if (NULL != data) {
735 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000736 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000737 glFlipY = true;
738 } else {
739 swFlipY = true;
740 }
741 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000742 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000743 // can't use this for flipping, only non-neg values allowed. :(
744 if (rowBytes != trimRowBytes) {
745 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
746 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
747 restoreGLRowLength = true;
748 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000749 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000750 if (trimRowBytes != rowBytes || swFlipY) {
751 // copy data into our new storage, skipping the trailing bytes
752 size_t trimSize = height * trimRowBytes;
753 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000754 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000755 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000756 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000757 char* dst = (char*)tempStorage.reset(trimSize);
758 for (int y = 0; y < height; y++) {
759 memcpy(dst, src, trimRowBytes);
760 if (swFlipY) {
761 src -= rowBytes;
762 } else {
763 src += rowBytes;
764 }
765 dst += trimRowBytes;
766 }
767 // now point data to our copied version
768 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000769 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000771 if (glFlipY) {
772 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
773 }
774 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000775 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000776 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000777 if (isNewTexture &&
778 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000779 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000780 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000781 if (useTexStorage) {
782 // We never resize or change formats of textures. We don't use
783 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000784 GL_ALLOC_CALL(this->glInterface(),
785 TexStorage2D(GR_GL_TEXTURE_2D,
786 1, // levels
787 internalFormat,
788 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000789 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000790 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
791 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
792 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000793 GL_ALLOC_CALL(this->glInterface(),
794 CompressedTexImage2D(GR_GL_TEXTURE_2D,
795 0, // level
796 internalFormat,
797 desc.fWidth, desc.fHeight,
798 0, // border
799 imageSize,
800 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000801 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000802 GL_ALLOC_CALL(this->glInterface(),
803 TexImage2D(GR_GL_TEXTURE_2D,
804 0, // level
805 internalFormat,
806 desc.fWidth, desc.fHeight,
807 0, // border
808 externalFormat, externalType,
809 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000810 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000811 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000812 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000813 if (error != GR_GL_NO_ERROR) {
814 succeeded = false;
815 } else {
816 // if we have data and we used TexStorage to create the texture, we
817 // now upload with TexSubImage.
818 if (NULL != data && useTexStorage) {
819 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
820 0, // level
821 left, top,
822 width, height,
823 externalFormat, externalType,
824 data));
825 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000826 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000827 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000828 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000829 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000830 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000831 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
832 0, // level
833 left, top,
834 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000835 externalFormat, externalType, data));
836 }
837
838 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000839 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000840 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000841 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000842 if (glFlipY) {
843 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
844 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000845 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846}
847
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000848namespace {
849bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
850 int sampleCount,
851 GrGLenum format,
852 int width, int height) {
853 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
854 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
855 bool created = false;
856 if (GrGLCaps::kNVDesktop_CoverageAAType ==
857 ctxInfo.caps().coverageAAType()) {
858 const GrGLCaps::MSAACoverageMode& mode =
859 ctxInfo.caps().getMSAACoverageMode(sampleCount);
860 GL_ALLOC_CALL(ctxInfo.interface(),
861 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
862 mode.fCoverageSampleCnt,
863 mode.fColorSampleCnt,
864 format,
865 width, height));
866 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
867 }
868 if (!created) {
869 GL_ALLOC_CALL(ctxInfo.interface(),
870 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
871 sampleCount,
872 format,
873 width, height));
874 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
875 }
876 return created;
877}
878}
879
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880bool GrGpuGL::createRenderTargetObjects(int width, int height,
881 GrGLuint texID,
882 GrGLRenderTarget::Desc* desc) {
883 desc->fMSColorRenderbufferID = 0;
884 desc->fRTFBOID = 0;
885 desc->fTexFBOID = 0;
886 desc->fOwnIDs = true;
887
888 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000889
bsalomon@google.comab15d612011-08-09 12:57:56 +0000890 GrGLenum msColorFormat = 0; // suppress warning
891
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000892 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000893 if (!desc->fTexFBOID) {
894 goto FAILED;
895 }
896
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897
898 // If we are using multisampling we will create two FBOS. We render
899 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000900 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000901 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000902 goto FAILED;
903 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000904 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
905 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000906 if (!desc->fRTFBOID ||
907 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000908 !this->configToGLFormats(desc->fConfig,
909 // GLES requires sized internal formats
910 kES2_GrGLBinding == this->glBinding(),
911 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 goto FAILED;
913 }
914 } else {
915 desc->fRTFBOID = desc->fTexFBOID;
916 }
917
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000918 // below here we may bind the FBO
919 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000920 if (desc->fRTFBOID != desc->fTexFBOID) {
921 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000922 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000923 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000924 if (!renderbuffer_storage_msaa(fGLContextInfo,
925 desc->fSampleCnt,
926 msColorFormat,
927 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 goto FAILED;
929 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000930 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
931 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932 GR_GL_COLOR_ATTACHMENT0,
933 GR_GL_RENDERBUFFER,
934 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000935 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000936 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
937 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
938 goto FAILED;
939 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000940 fGLContextInfo.caps().markConfigAsValidColorAttachment(
941 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000942 }
943 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000944 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000946 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
947 GR_GL_COLOR_ATTACHMENT0,
948 GR_GL_TEXTURE_2D,
949 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000950 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000951 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
952 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
953 goto FAILED;
954 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000955 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000956 }
957
958 return true;
959
960FAILED:
961 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000962 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000963 }
964 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000966 }
967 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000968 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000969 }
970 return false;
971}
972
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000973// good to set a break-point here to know when createTexture fails
974static GrTexture* return_null_texture() {
975// GrAssert(!"null texture");
976 return NULL;
977}
978
979#if GR_DEBUG
980static size_t as_size_t(int x) {
981 return x;
982}
983#endif
984
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000985GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000986 const void* srcData,
987 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000988
989#if GR_COLLECT_STATS
990 ++fStats.fTextureCreateCnt;
991#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000992
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000993 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000994 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000995
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000996 // Attempt to catch un- or wrongly initialized sample counts;
997 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
998
bsalomon@google.com99621082011-11-15 16:47:16 +0000999 glTexDesc.fWidth = desc.fWidth;
1000 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001001 glTexDesc.fConfig = desc.fConfig;
1002 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001004 glRTDesc.fMSColorRenderbufferID = 0;
1005 glRTDesc.fRTFBOID = 0;
1006 glRTDesc.fTexFBOID = 0;
1007 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001008 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001009
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001010 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001011
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001012 const Caps& caps = this->getCaps();
1013
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001014 // We keep GrRenderTargets in GL's normal orientation so that they
1015 // can be drawn to by the outside world without the client having
1016 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001017 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001018 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001019
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001020 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001021 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001022 desc.fSampleCnt) {
1023 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 }
1025
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001027 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1028 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001029 return return_null_texture();
1030 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001031 }
1032
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001033 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001034 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001035 // provides a hint about how this texture will be used
1036 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1037 GR_GL_TEXTURE_USAGE,
1038 GR_GL_FRAMEBUFFER_ATTACHMENT));
1039 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001040 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001041 return return_null_texture();
1042 }
1043
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001044 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001045 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001046
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001047 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1048 // drivers have a bug where an FBO won't be complete if it includes a
1049 // texture that is not mipmap complete (considering the filter in use).
1050 GrGLTexture::TexParams initialTexParams;
1051 // we only set a subset here so invalidate first
1052 initialTexParams.invalidate();
1053 initialTexParams.fFilter = GR_GL_NEAREST;
1054 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1055 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001056 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1057 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001058 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1060 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001061 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001062 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1063 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001064 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001065 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1066 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001067 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001068 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1069 glTexDesc.fWidth, glTexDesc.fHeight,
1070 desc.fConfig, srcData, rowBytes)) {
1071 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1072 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001073 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001074
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001076 if (renderTarget) {
1077#if GR_COLLECT_STATS
1078 ++fStats.fRenderTargetCreateCnt;
1079#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001080 // unbind the texture from the texture unit before binding it to the frame buffer
1081 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1082
bsalomon@google.com99621082011-11-15 16:47:16 +00001083 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1084 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001085 glTexDesc.fTextureID,
1086 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001087 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 return return_null_texture();
1089 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001090 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001092 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001094 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001095#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001096 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1097 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001098#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001099 return tex;
1100}
1101
1102namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001103
1104const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1105
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001106void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1107 GrGLuint rb,
1108 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 // we shouldn't ever know one size and not the other
1110 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1111 (kUnknownBitCount == format->fTotalBits));
1112 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1115 (GrGLint*)&format->fStencilBits);
1116 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001117 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1119 (GrGLint*)&format->fTotalBits);
1120 format->fTotalBits += format->fStencilBits;
1121 } else {
1122 format->fTotalBits = format->fStencilBits;
1123 }
1124 }
1125}
1126}
1127
1128bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1129 int width, int height) {
1130
1131 // All internally created RTs are also textures. We don't create
1132 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1133 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001134 GrAssert(width >= rt->width());
1135 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136
1137 int samples = rt->numSamples();
1138 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001140 if (!sbID) {
1141 return false;
1142 }
1143
1144 GrGLStencilBuffer* sb = NULL;
1145
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001146 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001148 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 // we start with the last stencil format that succeeded in hopes
1150 // that we won't go through this loop more than once after the
1151 // first (painful) stencil creation.
1152 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001153 const GrGLCaps::StencilFormat& sFmt =
1154 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001155 CLEAR_ERROR_BEFORE_ALLOC(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.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001158 bool created;
1159 if (samples > 0) {
1160 created = renderbuffer_storage_msaa(fGLContextInfo,
1161 samples,
1162 sFmt.fInternalFormat,
1163 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001164 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001165 GL_ALLOC_CALL(this->glInterface(),
1166 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001167 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001168 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001169 created =
1170 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001172 if (created) {
1173 // After sized formats we attempt an unsized format and take
1174 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001175 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001176 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001177 sb = new GrGLStencilBuffer(this, sbID, width, height,
1178 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1180 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001181 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 return true;
1184 }
1185 sb->abandon(); // otherwise we lose sbID
1186 sb->unref();
1187 }
1188 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001189 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001190 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191}
1192
1193bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1194 GrRenderTarget* rt) {
1195 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1196
1197 GrGLuint fbo = glrt->renderFBOID();
1198
1199 if (NULL == sb) {
1200 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 GR_GL_STENCIL_ATTACHMENT,
1203 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 GR_GL_DEPTH_ATTACHMENT,
1206 GR_GL_RENDERBUFFER, 0));
1207#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001208 GrGLenum status;
1209 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1211#endif
1212 }
1213 return true;
1214 } else {
1215 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1216 GrGLuint rb = glsb->renderbufferID();
1217
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001218 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 GR_GL_STENCIL_ATTACHMENT,
1222 GR_GL_RENDERBUFFER, rb));
1223 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001224 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 GR_GL_DEPTH_ATTACHMENT,
1226 GR_GL_RENDERBUFFER, rb));
1227 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001228 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229 GR_GL_DEPTH_ATTACHMENT,
1230 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001233 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001234 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001235 glsb->format())) {
1236 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1237 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001238 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001239 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001241 if (glsb->format().fPacked) {
1242 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1243 GR_GL_DEPTH_ATTACHMENT,
1244 GR_GL_RENDERBUFFER, 0));
1245 }
1246 return false;
1247 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001248 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001249 rt->config(),
1250 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001253 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001255}
1256
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001257////////////////////////////////////////////////////////////////////////////////
1258
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001259GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001260 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001264 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001265 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001267 GL_ALLOC_CALL(this->glInterface(),
1268 BufferData(GR_GL_ARRAY_BUFFER,
1269 size,
1270 NULL, // data ptr
1271 dynamic ? GR_GL_DYNAMIC_DRAW :
1272 GR_GL_STATIC_DRAW));
1273 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001274 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001275 // deleting bound buffer does implicit bind to 0
1276 fHWGeometryState.fVertexBuffer = NULL;
1277 return NULL;
1278 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001279 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 size, dynamic);
1281 fHWGeometryState.fVertexBuffer = vertexBuffer;
1282 return vertexBuffer;
1283 }
1284 return NULL;
1285}
1286
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001287GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001288 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001291 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001292 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001294 GL_ALLOC_CALL(this->glInterface(),
1295 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1296 size,
1297 NULL, // data ptr
1298 dynamic ? GR_GL_DYNAMIC_DRAW :
1299 GR_GL_STATIC_DRAW));
1300 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001301 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 // deleting bound buffer does implicit bind to 0
1303 fHWGeometryState.fIndexBuffer = NULL;
1304 return NULL;
1305 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001306 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 size, dynamic);
1308 fHWGeometryState.fIndexBuffer = indexBuffer;
1309 return indexBuffer;
1310 }
1311 return NULL;
1312}
1313
reed@google.comac10a2d2010-12-22 21:39:39 +00001314void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001315 const GrDrawState& drawState = this->getDrawState();
1316 const GrGLRenderTarget* rt =
1317 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1318
1319 GrAssert(NULL != rt);
1320 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001321
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001322 GrGLIRect scissor;
1323 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001324 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001325 rect->width(), rect->height());
1326 if (scissor.contains(vp)) {
1327 rect = NULL;
1328 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001329 }
1330
1331 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001332 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001333 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 fHWBounds.fScissorRect = scissor;
1335 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001337 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 fHWBounds.fScissorEnabled = true;
1339 }
1340 } else {
1341 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001342 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001343 fHWBounds.fScissorEnabled = false;
1344 }
1345 }
1346}
1347
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001348void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001349 const GrDrawState& drawState = this->getDrawState();
1350 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001351 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001352 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001353
bsalomon@google.com74b98712011-11-11 19:46:16 +00001354 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001355 if (NULL != rect) {
1356 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001357 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001358 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001359 if (clippedRect.intersect(rtRect)) {
1360 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001361 } else {
1362 return;
1363 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001364 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001365 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001366 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001367
1368 GrGLfloat r, g, b, a;
1369 static const GrGLfloat scale255 = 1.f / 255.f;
1370 a = GrColorUnpackA(color) * scale255;
1371 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001372 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001373 scaleRGB *= a;
1374 }
1375 r = GrColorUnpackR(color) * scaleRGB;
1376 g = GrColorUnpackG(color) * scaleRGB;
1377 b = GrColorUnpackB(color) * scaleRGB;
1378
1379 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001380 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001381 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001382 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001383}
1384
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001385void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001386 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001387 return;
1388 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001389
1390 this->flushRenderTarget(&GrIRect::EmptyIRect());
1391
reed@google.comac10a2d2010-12-22 21:39:39 +00001392 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001393 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001394 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001395 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001396 GL_CALL(StencilMask(0xffffffff));
1397 GL_CALL(ClearStencil(0));
1398 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001399 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001400}
1401
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001402void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001403 const GrDrawState& drawState = this->getDrawState();
1404 const GrRenderTarget* rt = drawState.getRenderTarget();
1405 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001406
1407 // this should only be called internally when we know we have a
1408 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001409 GrAssert(NULL != rt->getStencilBuffer());
1410 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001411#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001412 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001413 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001414#else
1415 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001416 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001417 // turned into draws. Our contract on GrDrawTarget says that
1418 // changing the clip between stencil passes may or may not
1419 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001420 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001421#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001422 GrGLint value;
1423 if (insideClip) {
1424 value = (1 << (stencilBitCount - 1));
1425 } else {
1426 value = 0;
1427 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001428 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001429 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001430 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001431 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001432 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001433 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001434}
1435
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001436void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001437 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001438}
1439
bsalomon@google.comc4364992011-11-07 15:54:49 +00001440bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1441 int left, int top,
1442 int width, int height,
1443 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001444 size_t rowBytes) const {
1445 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001446 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001447 return false;
1448 }
1449
1450 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001451 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001452 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001453 return true;
1454 }
1455 // If we have to do memcpys to handle rowBytes then y-flip is free
1456 // Note the rowBytes might be tight to the passed in data, but if data
1457 // gets clipped in x to the target the rowBytes will no longer be tight.
1458 if (left >= 0 && (left + width) < renderTarget->width()) {
1459 return 0 == rowBytes ||
1460 GrBytesPerPixel(config) * width == rowBytes;
1461 } else {
1462 return false;
1463 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001464}
1465
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001466bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001467 int left, int top,
1468 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001469 GrPixelConfig config,
1470 void* buffer,
1471 size_t rowBytes,
1472 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001473 GrGLenum format;
1474 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001475 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001476 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001477 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001478 size_t bpp = GrBytesPerPixel(config);
1479 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1480 &left, &top, &width, &height,
1481 const_cast<const void**>(&buffer),
1482 &rowBytes)) {
1483 return false;
1484 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001485
bsalomon@google.comc6980972011-11-02 19:57:21 +00001486 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001487 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001488 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001489 switch (tgt->getResolveType()) {
1490 case GrGLRenderTarget::kCantResolve_ResolveType:
1491 return false;
1492 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001493 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001494 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001495 break;
1496 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001497 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001498 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001499 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1500 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001501 break;
1502 default:
1503 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001504 }
1505
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001506 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001507
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001508 // the read rect is viewport-relative
1509 GrGLIRect readRect;
1510 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001512 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001513 if (0 == rowBytes) {
1514 rowBytes = tightRowBytes;
1515 }
1516 size_t readDstRowBytes = tightRowBytes;
1517 void* readDst = buffer;
1518
1519 // determine if GL can read using the passed rowBytes or if we need
1520 // a scratch buffer.
1521 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1522 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001523 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001524 GrAssert(!(rowBytes % sizeof(GrColor)));
1525 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1526 readDstRowBytes = rowBytes;
1527 } else {
1528 scratch.reset(tightRowBytes * height);
1529 readDst = scratch.get();
1530 }
1531 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001532 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001533 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1534 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001535 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1536 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001537 format, type, readDst));
1538 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001539 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001540 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1541 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001542 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001543 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1544 invertY = true;
1545 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001546
1547 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001548 // API presents top-to-bottom. We must preserve the padding contents. Note
1549 // that the above readPixels did not overwrite the padding.
1550 if (readDst == buffer) {
1551 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001552 if (!invertY) {
1553 scratch.reset(tightRowBytes);
1554 void* tmpRow = scratch.get();
1555 // flip y in-place by rows
1556 const int halfY = height >> 1;
1557 char* top = reinterpret_cast<char*>(buffer);
1558 char* bottom = top + (height - 1) * rowBytes;
1559 for (int y = 0; y < halfY; y++) {
1560 memcpy(tmpRow, top, tightRowBytes);
1561 memcpy(top, bottom, tightRowBytes);
1562 memcpy(bottom, tmpRow, tightRowBytes);
1563 top += rowBytes;
1564 bottom -= rowBytes;
1565 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001566 }
1567 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001568 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001569 // copy from readDst to buffer while flipping y
1570 const int halfY = height >> 1;
1571 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001572 char* dst = reinterpret_cast<char*>(buffer);
1573 if (!invertY) {
1574 dst += (height-1) * rowBytes;
1575 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001576 for (int y = 0; y < height; y++) {
1577 memcpy(dst, src, tightRowBytes);
1578 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001579 if (invertY) {
1580 dst += rowBytes;
1581 } else {
1582 dst -= rowBytes;
1583 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001584 }
1585 }
1586 return true;
1587}
1588
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001589void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001590
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001591 GrGLRenderTarget* rt =
1592 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1593 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001594
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001595 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001596 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001597 #if GR_COLLECT_STATS
1598 ++fStats.fRenderTargetChngCnt;
1599 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001600 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001601 GrGLenum status;
1602 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001603 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001604 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001605 }
1606 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001607 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001608 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001609 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001610 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001611 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 fHWBounds.fViewportRect = vp;
1613 }
1614 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001615 if (NULL == bound || !bound->isEmpty()) {
1616 rt->flagAsNeedingResolve(bound);
1617 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001618}
1619
twiz@google.com0f31ca72011-03-18 17:38:11 +00001620GrGLenum gPrimitiveType2GLMode[] = {
1621 GR_GL_TRIANGLES,
1622 GR_GL_TRIANGLE_STRIP,
1623 GR_GL_TRIANGLE_FAN,
1624 GR_GL_POINTS,
1625 GR_GL_LINES,
1626 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001627};
1628
bsalomon@google.comd302f142011-03-03 13:54:13 +00001629#define SWAP_PER_DRAW 0
1630
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001631#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001632 #if GR_MAC_BUILD
1633 #include <AGL/agl.h>
1634 #elif GR_WIN32_BUILD
1635 void SwapBuf() {
1636 DWORD procID = GetCurrentProcessId();
1637 HWND hwnd = GetTopWindow(GetDesktopWindow());
1638 while(hwnd) {
1639 DWORD wndProcID = 0;
1640 GetWindowThreadProcessId(hwnd, &wndProcID);
1641 if(wndProcID == procID) {
1642 SwapBuffers(GetDC(hwnd));
1643 }
1644 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1645 }
1646 }
1647 #endif
1648#endif
1649
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001650void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1651 uint32_t startVertex,
1652 uint32_t startIndex,
1653 uint32_t vertexCount,
1654 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1656
twiz@google.com0f31ca72011-03-18 17:38:11 +00001657 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001658
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001659 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1660 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1661
1662 // our setupGeometry better have adjusted this to zero since
1663 // DrawElements always draws from the begining of the arrays for idx 0.
1664 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001665
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001666 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1667 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001668#if SWAP_PER_DRAW
1669 glFlush();
1670 #if GR_MAC_BUILD
1671 aglSwapBuffers(aglGetCurrentContext());
1672 int set_a_break_pt_here = 9;
1673 aglSwapBuffers(aglGetCurrentContext());
1674 #elif GR_WIN32_BUILD
1675 SwapBuf();
1676 int set_a_break_pt_here = 9;
1677 SwapBuf();
1678 #endif
1679#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001680}
1681
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001682void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1683 uint32_t startVertex,
1684 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001685 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1686
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001687 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1688
1689 // our setupGeometry better have adjusted this to zero.
1690 // DrawElements doesn't take an offset so we always adjus the startVertex.
1691 GrAssert(0 == startVertex);
1692
1693 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1694 // account for startVertex in the DrawElements case. So we always
1695 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001696 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001697#if SWAP_PER_DRAW
1698 glFlush();
1699 #if GR_MAC_BUILD
1700 aglSwapBuffers(aglGetCurrentContext());
1701 int set_a_break_pt_here = 9;
1702 aglSwapBuffers(aglGetCurrentContext());
1703 #elif GR_WIN32_BUILD
1704 SwapBuf();
1705 int set_a_break_pt_here = 9;
1706 SwapBuf();
1707 #endif
1708#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001709}
1710
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001711void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1712
1713 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001714
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001715 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001716 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001717 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001718 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1719 rt->renderFBOID()));
1720 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1721 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 #if GR_COLLECT_STATS
1723 ++fStats.fRenderTargetChngCnt;
1724 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001725 // make sure we go through flushRenderTarget() since we've modified
1726 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001727 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001728 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001729 const GrIRect dirtyRect = rt->getResolveRect();
1730 GrGLIRect r;
1731 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1732 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001733
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001734 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001735 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001736 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1737 GL_CALL(Scissor(r.fLeft, r.fBottom,
1738 r.fWidth, r.fHeight));
1739 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001740 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001741 fHWBounds.fScissorEnabled = true;
1742 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001743 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001744 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001745 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1746 this->glCaps().msFBOType());
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001747 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001748 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001749 int right = r.fLeft + r.fWidth;
1750 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001751 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1752 r.fLeft, r.fBottom, right, top,
1753 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001755 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 }
1757}
1758
twiz@google.com0f31ca72011-03-18 17:38:11 +00001759static const GrGLenum grToGLStencilFunc[] = {
1760 GR_GL_ALWAYS, // kAlways_StencilFunc
1761 GR_GL_NEVER, // kNever_StencilFunc
1762 GR_GL_GREATER, // kGreater_StencilFunc
1763 GR_GL_GEQUAL, // kGEqual_StencilFunc
1764 GR_GL_LESS, // kLess_StencilFunc
1765 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1766 GR_GL_EQUAL, // kEqual_StencilFunc,
1767 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001768};
1769GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1770GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1771GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1772GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1773GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1774GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1775GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1776GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1777GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1778
twiz@google.com0f31ca72011-03-18 17:38:11 +00001779static const GrGLenum grToGLStencilOp[] = {
1780 GR_GL_KEEP, // kKeep_StencilOp
1781 GR_GL_REPLACE, // kReplace_StencilOp
1782 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1783 GR_GL_INCR, // kIncClamp_StencilOp
1784 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1785 GR_GL_DECR, // kDecClamp_StencilOp
1786 GR_GL_ZERO, // kZero_StencilOp
1787 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001788};
1789GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1790GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1791GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1792GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1793GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1794GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1795GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1796GR_STATIC_ASSERT(6 == kZero_StencilOp);
1797GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1798
reed@google.comac10a2d2010-12-22 21:39:39 +00001799void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001800 const GrDrawState& drawState = this->getDrawState();
1801
1802 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001803
1804 // use stencil for clipping if clipping is enabled and the clip
1805 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001806 bool stencilClip = fClipInStencil && drawState.isClipState();
1807 bool drawClipToStencil =
1808 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001809 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1810 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001811 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1812 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001813
1814 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001815
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001816 // we can't simultaneously perform stencil-clipping and
1817 // modify the stencil clip
1818 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001819
bsalomon@google.comd302f142011-03-03 13:54:13 +00001820 if (settings->isDisabled()) {
1821 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001822 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001823 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001824 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001825
1826 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001827 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001829 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001831 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001832 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1833 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1834 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1835 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1836 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1837 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1838 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1839 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001840 }
1841 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001842 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001843 GrStencilBuffer* stencilBuffer =
1844 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001845 if (NULL != stencilBuffer) {
1846 stencilBits = stencilBuffer->bits();
1847 }
1848 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001849 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001850
1851 GrGLuint clipStencilMask = 0;
1852 GrGLuint userStencilMask = ~0;
1853 if (stencilBits > 0) {
1854 clipStencilMask = 1 << (stencilBits - 1);
1855 userStencilMask = clipStencilMask - 1;
1856 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001857
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001858 unsigned int frontRef = settings->frontFuncRef();
1859 unsigned int frontMask = settings->frontFuncMask();
1860 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001861 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001862
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001863 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001864 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1865 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001867 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001868 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001870 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001871 stencilClip,
1872 clipStencilMask,
1873 userStencilMask,
1874 &frontRef,
1875 &frontMask);
1876 frontWriteMask &= userStencilMask;
1877 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001878 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001879 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001880 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001881 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001882 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001883 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001884 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001885 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001886 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001887 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001888
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001889 unsigned int backRef = settings->backFuncRef();
1890 unsigned int backMask = settings->backFuncMask();
1891 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001892
1893
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001894 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001895 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1896 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001898 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001899 stencilClip, settings->backFunc())];
1900 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001901 stencilClip,
1902 clipStencilMask,
1903 userStencilMask,
1904 &backRef,
1905 &backMask);
1906 backWriteMask &= userStencilMask;
1907 }
1908
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001909 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1910 frontRef, frontMask));
1911 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1912 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1913 backRef, backMask));
1914 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1915 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001916 grToGLStencilOp[settings->frontFailOp()],
1917 grToGLStencilOp[settings->frontPassOp()],
1918 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001919
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001920 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001921 grToGLStencilOp[settings->backFailOp()],
1922 grToGLStencilOp[settings->backPassOp()],
1923 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001924 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001925 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1926 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001927 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1928 grToGLStencilOp[settings->frontPassOp()],
1929 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930 }
1931 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001932 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001933 fHWStencilClip = stencilClip;
1934 }
1935}
1936
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001937void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001938 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001939 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001940 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1941 // smooth lines.
1942
1943 // we prefer smooth lines over multisampled lines
1944 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001945 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001946 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001947 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001948 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001949 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001950 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001951 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001952 fHWAAState.fSmoothLineEnabled = false;
1953 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001954 if (rt->isMultisampled() &&
1955 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001956 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001957 fHWAAState.fMSAAEnabled = false;
1958 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001959 } else if (rt->isMultisampled() &&
1960 this->getDrawState().isHWAntialiasState() !=
1961 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001962 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001963 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001964 fHWAAState.fMSAAEnabled = false;
1965 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001966 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001967 fHWAAState.fMSAAEnabled = true;
1968 }
1969 }
1970 }
1971}
1972
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001973void GrGpuGL::flushBlend(GrPrimitiveType type,
1974 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001975 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001976 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001977 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001978 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001979 fHWBlendDisabled = false;
1980 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001981 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1982 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001983 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1984 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001985 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001986 }
1987 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001988 // any optimization to disable blending should
1989 // have already been applied and tweaked the coeffs
1990 // to (1, 0).
1991 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1992 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001993 if (fHWBlendDisabled != blendOff) {
1994 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001995 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001996 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001997 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001998 }
1999 fHWBlendDisabled = blendOff;
2000 }
2001 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002002 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2003 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002004 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2005 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002006 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002007 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002008 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002009 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2010 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002011 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002012
2013 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002014 GrColorUnpackR(blendConst) / 255.f,
2015 GrColorUnpackG(blendConst) / 255.f,
2016 GrColorUnpackB(blendConst) / 255.f,
2017 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002018 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002019 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002020 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002021 }
2022 }
2023 }
2024}
2025
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002026namespace {
2027
2028unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002029 switch (filter) {
2030 case GrSamplerState::kBilinear_Filter:
2031 case GrSamplerState::k4x4Downsample_Filter:
2032 return GR_GL_LINEAR;
2033 case GrSamplerState::kNearest_Filter:
2034 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002035 case GrSamplerState::kErode_Filter:
2036 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002037 return GR_GL_NEAREST;
2038 default:
2039 GrAssert(!"Unknown filter type");
2040 return GR_GL_LINEAR;
2041 }
2042}
2043
robertphillips@google.com69950682012-04-06 18:06:10 +00002044// get_swizzle is only called from this .cpp so it is OK to inline it here
2045inline const GrGLenum* get_swizzle(GrPixelConfig config,
2046 const GrSamplerState& sampler,
2047 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002048 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com69950682012-04-06 18:06:10 +00002049 if (glCaps.textureRedSupport()) {
2050 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2051 GR_GL_RED, GR_GL_RED };
2052 return gRedSmear;
2053 } else {
2054 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2055 GR_GL_ALPHA, GR_GL_ALPHA };
2056 return gAlphaSmear;
2057 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002058 } else if (sampler.swapsRAndB()) {
2059 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2060 GR_GL_RED, GR_GL_ALPHA };
2061 return gRedBlueSwap;
2062 } else {
2063 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2064 GR_GL_BLUE, GR_GL_ALPHA };
2065 return gStraight;
2066 }
2067}
2068
2069void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2070 // should add texparameteri to interface to make 1 instead of 4 calls here
2071 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2072 GR_GL_TEXTURE_SWIZZLE_R,
2073 swizzle[0]));
2074 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2075 GR_GL_TEXTURE_SWIZZLE_G,
2076 swizzle[1]));
2077 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2078 GR_GL_TEXTURE_SWIZZLE_B,
2079 swizzle[2]));
2080 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2081 GR_GL_TEXTURE_SWIZZLE_A,
2082 swizzle[3]));
2083}
2084}
2085
bsalomon@google.comffca4002011-02-22 20:34:01 +00002086bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002087
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002088 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002089 // GrGpu::setupClipAndFlushState should have already checked this
2090 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002091 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002092
tomhudson@google.com93813632011-10-27 20:21:16 +00002093 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002094 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002095 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002096 GrGLTexture* nextTexture =
2097 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002098
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002099 // true for now, but maybe not with GrEffect.
2100 GrAssert(NULL != nextTexture);
2101 // if we created a rt/tex and rendered to it without using a
2102 // texture and now we're texuring from the rt it will still be
2103 // the last bound texture, but it needs resolving. So keep this
2104 // out of the "last != next" check.
2105 GrGLRenderTarget* texRT =
2106 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2107 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002108 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002109 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002110
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002111 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002112 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002113 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002114 #if GR_COLLECT_STATS
2115 ++fStats.fTextureChngCnt;
2116 #endif
2117 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002118 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002119 // The texture matrix has to compensate for texture width/height
2120 // and NPOT-embedded-in-POT
2121 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002122 }
2123
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002124 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002125 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002126 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002127 nextTexture->getCachedTexParams(&timestamp);
2128 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002129 GrGLTexture::TexParams newTexParams;
2130
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002131 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002132
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002133 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002134 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2135 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002136 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com69950682012-04-06 18:06:10 +00002137 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002138 sizeof(newTexParams.fSwizzleRGBA));
2139 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002140 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002141 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002142 GR_GL_TEXTURE_MAG_FILTER,
2143 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002144 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002145 GR_GL_TEXTURE_MIN_FILTER,
2146 newTexParams.fFilter));
2147 }
2148 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2149 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002150 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002151 GR_GL_TEXTURE_WRAP_S,
2152 newTexParams.fWrapS));
2153 }
2154 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2155 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002156 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002157 GR_GL_TEXTURE_WRAP_T,
2158 newTexParams.fWrapT));
2159 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002160 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002161 (setAll ||
2162 memcmp(newTexParams.fSwizzleRGBA,
2163 oldTexParams.fSwizzleRGBA,
2164 sizeof(newTexParams.fSwizzleRGBA)))) {
2165 setTextureUnit(s);
2166 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2167 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002168 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002169 nextTexture->setCachedTexParams(newTexParams,
2170 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002171 }
2172 }
2173
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002174 GrIRect* rect = NULL;
2175 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002176 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002177 fClip.hasConservativeBounds()) {
2178 fClip.getConservativeBounds().roundOut(&clipBounds);
2179 rect = &clipBounds;
2180 }
2181 this->flushRenderTarget(rect);
2182 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002183
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002184 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2185 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002186 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002187 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002188 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002189 }
2190 }
2191
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002192 if (drawState->isColorWriteDisabled() !=
2193 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002194 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002195 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002196 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002197 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002198 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002199 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002200 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002201 }
2202
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002203 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002204 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002205 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002206 GL_CALL(Enable(GR_GL_CULL_FACE));
2207 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002208 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002209 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002210 GL_CALL(Enable(GR_GL_CULL_FACE));
2211 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002212 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002213 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002214 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002215 break;
2216 default:
2217 GrCrash("Unknown draw face.");
2218 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002219 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002220 }
2221
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002222#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002223 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002224 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002225 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002226 NULL == drawState->getRenderTarget() ||
2227 NULL == drawState->getTexture(s) ||
2228 drawState->getTexture(s)->asRenderTarget() !=
2229 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002230 }
2231#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002232
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002233 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002234
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002235 // This copy must happen after flushStencil() is called. flushStencil()
2236 // relies on detecting when the kModifyStencilClip_StateBit state has
2237 // changed since the last draw.
2238 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002239 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002240}
2241
2242void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002243 if (fHWGeometryState.fVertexBuffer != buffer) {
2244 fHWGeometryState.fArrayPtrsDirty = true;
2245 fHWGeometryState.fVertexBuffer = buffer;
2246 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002247}
2248
2249void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002250 if (fHWGeometryState.fVertexBuffer == buffer) {
2251 // deleting bound buffer does implied bind to 0
2252 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002253 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002254 }
2255}
2256
2257void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002258 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002259}
2260
2261void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002262 if (fHWGeometryState.fIndexBuffer == buffer) {
2263 // deleting bound buffer does implied bind to 0
2264 fHWGeometryState.fIndexBuffer = NULL;
2265 }
2266}
2267
reed@google.comac10a2d2010-12-22 21:39:39 +00002268void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2269 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002270 GrDrawState* drawState = this->drawState();
2271 if (drawState->getRenderTarget() == renderTarget) {
2272 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002273 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002274 if (fHWDrawState.getRenderTarget() == renderTarget) {
2275 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002276 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002277}
2278
2279void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002280 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002281 GrDrawState* drawState = this->drawState();
2282 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002283 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002284 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002285 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002286 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002287 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002288 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002289 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002290}
2291
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002292bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2293 bool getSizedInternalFormat,
2294 GrGLenum* internalFormat,
2295 GrGLenum* externalFormat,
2296 GrGLenum* externalType) {
2297 GrGLenum dontCare;
2298 if (NULL == internalFormat) {
2299 internalFormat = &dontCare;
2300 }
2301 if (NULL == externalFormat) {
2302 externalFormat = &dontCare;
2303 }
2304 if (NULL == externalType) {
2305 externalType = &dontCare;
2306 }
2307
reed@google.comac10a2d2010-12-22 21:39:39 +00002308 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002309 case kRGBA_8888_PM_GrPixelConfig:
2310 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002311 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002312 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002313 if (getSizedInternalFormat) {
2314 *internalFormat = GR_GL_RGBA8;
2315 } else {
2316 *internalFormat = GR_GL_RGBA;
2317 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002318 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002319 break;
2320 case kBGRA_8888_PM_GrPixelConfig:
2321 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002322 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002323 return false;
2324 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002325 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002326 if (getSizedInternalFormat) {
2327 *internalFormat = GR_GL_BGRA8;
2328 } else {
2329 *internalFormat = GR_GL_BGRA;
2330 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002331 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002332 if (getSizedInternalFormat) {
2333 *internalFormat = GR_GL_RGBA8;
2334 } else {
2335 *internalFormat = GR_GL_RGBA;
2336 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002337 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002338 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002339 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002340 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002341 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002342 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002343 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002344 if (getSizedInternalFormat) {
2345 if (this->glBinding() == kDesktop_GrGLBinding) {
2346 return false;
2347 } else {
2348 *internalFormat = GR_GL_RGB565;
2349 }
2350 } else {
2351 *internalFormat = GR_GL_RGB;
2352 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002353 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002354 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002355 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002356 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002357 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002358 if (getSizedInternalFormat) {
2359 *internalFormat = GR_GL_RGBA4;
2360 } else {
2361 *internalFormat = GR_GL_RGBA;
2362 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002363 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002364 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002365 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002366 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002367 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002368 // glCompressedTexImage doesn't take external params
2369 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002370 // no sized/unsized internal format distinction here
2371 *internalFormat = GR_GL_PALETTE8_RGBA8;
2372 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002373 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002374 } else {
2375 return false;
2376 }
2377 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002378 case kAlpha_8_GrPixelConfig:
robertphillips@google.com69950682012-04-06 18:06:10 +00002379 if (this->glCaps().textureRedSupport()) {
2380 *internalFormat = GR_GL_RED;
2381 *externalFormat = GR_GL_RED;
2382 if (getSizedInternalFormat) {
2383 *internalFormat = GR_GL_R8;
2384 } else {
2385 *internalFormat = GR_GL_RED;
2386 }
2387 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002388 } else {
2389 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com69950682012-04-06 18:06:10 +00002390 *externalFormat = GR_GL_ALPHA;
2391 if (getSizedInternalFormat) {
2392 *internalFormat = GR_GL_ALPHA8;
2393 } else {
2394 *internalFormat = GR_GL_ALPHA;
2395 }
2396 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002397 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002398 break;
2399 default:
2400 return false;
2401 }
2402 return true;
2403}
2404
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002405void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002406 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002407 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002408 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002409 fActiveTextureUnitIdx = unit;
2410 }
2411}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002412
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002413void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002414 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002415 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002416 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2417 }
2418}
2419
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002420void GrGpuGL::resetDirtyFlags() {
2421 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2422}
2423
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002424void GrGpuGL::setBuffers(bool indexed,
2425 int* extraVertexOffset,
2426 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002427
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002428 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002429
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002430 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2431
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002432 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002433 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434 case kBuffer_GeometrySrcType:
2435 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002436 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002437 break;
2438 case kArray_GeometrySrcType:
2439 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002440 this->finalizeReservedVertices();
2441 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2442 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443 break;
2444 default:
2445 vbuf = NULL; // suppress warning
2446 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002447 }
2448
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002449 GrAssert(NULL != vbuf);
2450 GrAssert(!vbuf->isLocked());
2451 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002452 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002453 fHWGeometryState.fArrayPtrsDirty = true;
2454 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002455 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002456
2457 if (indexed) {
2458 GrAssert(NULL != extraIndexOffset);
2459
2460 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002461 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462 case kBuffer_GeometrySrcType:
2463 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002464 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002465 break;
2466 case kArray_GeometrySrcType:
2467 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002468 this->finalizeReservedIndices();
2469 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2470 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002471 break;
2472 default:
2473 ibuf = NULL; // suppress warning
2474 GrCrash("Unknown geometry src type!");
2475 }
2476
2477 GrAssert(NULL != ibuf);
2478 GrAssert(!ibuf->isLocked());
2479 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002480 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002481 fHWGeometryState.fIndexBuffer = ibuf;
2482 }
2483 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002484}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002485
2486int GrGpuGL::getMaxEdges() const {
2487 // FIXME: This is a pessimistic estimate based on how many other things
2488 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002489 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002490 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002491}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002492