blob: a17f7d74462bc8e2c085d5e19d0d6bd126fa04eb [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();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000519 this->disableScissor();
520
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;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000525
526 // TODO: I believe this should actually go in GrGpu::onResetContext
527 // rather than here
528 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000529
530 fHWGeometryState.fIndexBuffer = NULL;
531 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000532
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000533 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000534
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000535 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000536 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000537
538 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000539 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000540 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
541 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000542 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000543 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
544 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000545 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000546 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
547 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000548 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000549 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
550 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000551}
552
bsalomon@google.come269f212011-11-07 13:29:52 +0000553GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000554 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000555 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000556 return NULL;
557 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000558
bsalomon@google.com99621082011-11-15 16:47:16 +0000559 glTexDesc.fWidth = desc.fWidth;
560 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000561 glTexDesc.fConfig = desc.fConfig;
562 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
563 glTexDesc.fOwnsID = false;
564 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
565
566 GrGLTexture* texture = NULL;
567 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
568 GrGLRenderTarget::Desc glRTDesc;
569 glRTDesc.fRTFBOID = 0;
570 glRTDesc.fTexFBOID = 0;
571 glRTDesc.fMSColorRenderbufferID = 0;
572 glRTDesc.fOwnIDs = true;
573 glRTDesc.fConfig = desc.fConfig;
574 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000575 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
576 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000577 glTexDesc.fTextureID,
578 &glRTDesc)) {
579 return NULL;
580 }
581 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
582 } else {
583 texture = new GrGLTexture(this, glTexDesc);
584 }
585 if (NULL == texture) {
586 return NULL;
587 }
588
589 this->setSpareTextureUnit();
590 return texture;
591}
592
593GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
594 GrGLRenderTarget::Desc glDesc;
595 glDesc.fConfig = desc.fConfig;
596 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
597 glDesc.fMSColorRenderbufferID = 0;
598 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
599 glDesc.fSampleCnt = desc.fSampleCnt;
600 glDesc.fOwnIDs = false;
601 GrGLIRect viewport;
602 viewport.fLeft = 0;
603 viewport.fBottom = 0;
604 viewport.fWidth = desc.fWidth;
605 viewport.fHeight = desc.fHeight;
606
607 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
608 if (desc.fStencilBits) {
609 GrGLStencilBuffer::Format format;
610 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
611 format.fPacked = false;
612 format.fStencilBits = desc.fStencilBits;
613 format.fTotalBits = desc.fStencilBits;
614 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
615 0,
616 desc.fWidth,
617 desc.fHeight,
618 desc.fSampleCnt,
619 format);
620 tgt->setStencilBuffer(sb);
621 sb->unref();
622 }
623 return tgt;
624}
625
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000626////////////////////////////////////////////////////////////////////////////////
627
bsalomon@google.com6f379512011-11-16 20:36:03 +0000628void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
629 int left, int top, int width, int height,
630 GrPixelConfig config, const void* buffer,
631 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000632 if (NULL == buffer) {
633 return;
634 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000635 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
636
bsalomon@google.com6f379512011-11-16 20:36:03 +0000637 this->setSpareTextureUnit();
638 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
639 GrGLTexture::Desc desc;
640 desc.fConfig = glTex->config();
641 desc.fWidth = glTex->width();
642 desc.fHeight = glTex->height();
643 desc.fOrientation = glTex->orientation();
644 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000645
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000646 this->uploadTexData(desc, false,
647 left, top, width, height,
648 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000649}
650
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000651namespace {
652bool adjust_pixel_ops_params(int surfaceWidth,
653 int surfaceHeight,
654 size_t bpp,
655 int* left, int* top, int* width, int* height,
656 const void** data,
657 size_t* rowBytes) {
658 if (!*rowBytes) {
659 *rowBytes = *width * bpp;
660 }
661
662 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
663 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
664
665 if (!subRect.intersect(bounds)) {
666 return false;
667 }
668 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
669 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
670
671 *left = subRect.fLeft;
672 *top = subRect.fTop;
673 *width = subRect.width();
674 *height = subRect.height();
675 return true;
676}
677}
678
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000679bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
680 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000681 int left, int top, int width, int height,
682 GrPixelConfig dataConfig,
683 const void* data,
684 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000685 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000686
687 size_t bpp = GrBytesPerPixel(dataConfig);
688 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
689 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000690 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000691 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000692 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000693
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000694 // in case we need a temporary, trimmed copy of the src pixels
695 SkAutoSMalloc<128 * 128> tempStorage;
696
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000697 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000698 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000699 if (useTexStorage) {
700 if (kDesktop_GrGLBinding == this->glBinding()) {
701 // 565 is not a sized internal format on desktop GL. So on desktop
702 // with 565 we always use an unsized internal format to let the
703 // system pick the best sized format to convert the 565 data to.
704 // Since glTexStorage only allows sized internal formats we will
705 // instead fallback to glTexImage2D.
706 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
707 } else {
708 // ES doesn't allow paletted textures to be used with tex storage
709 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
710 }
711 }
712
713 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000714 GrGLenum externalFormat;
715 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000716 // glTexStorage requires sized internal formats on both desktop and ES. ES
717 // glTexImage requires an unsized format.
718 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
719 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000720 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000721 }
722
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000723 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000724 // paletted textures cannot be updated
725 return false;
726 }
727
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000728 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000729 * check whether to allocate a temporary buffer for flipping y or
730 * because our srcData has extra bytes past each row. If so, we need
731 * to trim those off here, since GL ES may not let us specify
732 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000733 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000734 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000735 bool swFlipY = false;
736 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000737 if (NULL != data) {
738 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000739 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000740 glFlipY = true;
741 } else {
742 swFlipY = true;
743 }
744 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000745 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000746 // can't use this for flipping, only non-neg values allowed. :(
747 if (rowBytes != trimRowBytes) {
748 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
749 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
750 restoreGLRowLength = true;
751 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000752 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000753 if (trimRowBytes != rowBytes || swFlipY) {
754 // copy data into our new storage, skipping the trailing bytes
755 size_t trimSize = height * trimRowBytes;
756 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000757 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000758 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000759 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000760 char* dst = (char*)tempStorage.reset(trimSize);
761 for (int y = 0; y < height; y++) {
762 memcpy(dst, src, trimRowBytes);
763 if (swFlipY) {
764 src -= rowBytes;
765 } else {
766 src += rowBytes;
767 }
768 dst += trimRowBytes;
769 }
770 // now point data to our copied version
771 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000772 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000773 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000774 if (glFlipY) {
775 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
776 }
777 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000778 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000779 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000780 if (isNewTexture &&
781 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000782 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000783 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000784 if (useTexStorage) {
785 // We never resize or change formats of textures. We don't use
786 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000787 GL_ALLOC_CALL(this->glInterface(),
788 TexStorage2D(GR_GL_TEXTURE_2D,
789 1, // levels
790 internalFormat,
791 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000792 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000793 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
794 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
795 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000796 GL_ALLOC_CALL(this->glInterface(),
797 CompressedTexImage2D(GR_GL_TEXTURE_2D,
798 0, // level
799 internalFormat,
800 desc.fWidth, desc.fHeight,
801 0, // border
802 imageSize,
803 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000804 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000805 GL_ALLOC_CALL(this->glInterface(),
806 TexImage2D(GR_GL_TEXTURE_2D,
807 0, // level
808 internalFormat,
809 desc.fWidth, desc.fHeight,
810 0, // border
811 externalFormat, externalType,
812 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000813 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000814 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000815 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000816 if (error != GR_GL_NO_ERROR) {
817 succeeded = false;
818 } else {
819 // if we have data and we used TexStorage to create the texture, we
820 // now upload with TexSubImage.
821 if (NULL != data && useTexStorage) {
822 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
823 0, // level
824 left, top,
825 width, height,
826 externalFormat, externalType,
827 data));
828 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000829 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000830 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000831 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000832 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000833 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000834 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
835 0, // level
836 left, top,
837 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000838 externalFormat, externalType, data));
839 }
840
841 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000842 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000843 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000844 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000845 if (glFlipY) {
846 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
847 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000848 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000849}
850
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000851namespace {
852bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
853 int sampleCount,
854 GrGLenum format,
855 int width, int height) {
856 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
857 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
858 bool created = false;
859 if (GrGLCaps::kNVDesktop_CoverageAAType ==
860 ctxInfo.caps().coverageAAType()) {
861 const GrGLCaps::MSAACoverageMode& mode =
862 ctxInfo.caps().getMSAACoverageMode(sampleCount);
863 GL_ALLOC_CALL(ctxInfo.interface(),
864 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
865 mode.fCoverageSampleCnt,
866 mode.fColorSampleCnt,
867 format,
868 width, height));
869 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
870 }
871 if (!created) {
872 GL_ALLOC_CALL(ctxInfo.interface(),
873 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
874 sampleCount,
875 format,
876 width, height));
877 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
878 }
879 return created;
880}
881}
882
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000883bool GrGpuGL::createRenderTargetObjects(int width, int height,
884 GrGLuint texID,
885 GrGLRenderTarget::Desc* desc) {
886 desc->fMSColorRenderbufferID = 0;
887 desc->fRTFBOID = 0;
888 desc->fTexFBOID = 0;
889 desc->fOwnIDs = true;
890
891 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000892
bsalomon@google.comab15d612011-08-09 12:57:56 +0000893 GrGLenum msColorFormat = 0; // suppress warning
894
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000895 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000896 if (!desc->fTexFBOID) {
897 goto FAILED;
898 }
899
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000900
901 // If we are using multisampling we will create two FBOS. We render
902 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000903 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000904 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000905 goto FAILED;
906 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000907 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
908 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 if (!desc->fRTFBOID ||
910 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000911 !this->configToGLFormats(desc->fConfig,
912 // GLES requires sized internal formats
913 kES2_GrGLBinding == this->glBinding(),
914 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000915 goto FAILED;
916 }
917 } else {
918 desc->fRTFBOID = desc->fTexFBOID;
919 }
920
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000921 // below here we may bind the FBO
922 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000923 if (desc->fRTFBOID != desc->fTexFBOID) {
924 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000925 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000926 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000927 if (!renderbuffer_storage_msaa(fGLContextInfo,
928 desc->fSampleCnt,
929 msColorFormat,
930 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000931 goto FAILED;
932 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000933 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
934 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000935 GR_GL_COLOR_ATTACHMENT0,
936 GR_GL_RENDERBUFFER,
937 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000938 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000939 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
940 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
941 goto FAILED;
942 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000943 fGLContextInfo.caps().markConfigAsValidColorAttachment(
944 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000945 }
946 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000947 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000948
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000949 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
950 GR_GL_COLOR_ATTACHMENT0,
951 GR_GL_TEXTURE_2D,
952 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000953 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000954 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
955 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
956 goto FAILED;
957 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000958 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000959 }
960
961 return true;
962
963FAILED:
964 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000966 }
967 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000968 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000969 }
970 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000971 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000972 }
973 return false;
974}
975
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000976// good to set a break-point here to know when createTexture fails
977static GrTexture* return_null_texture() {
978// GrAssert(!"null texture");
979 return NULL;
980}
981
982#if GR_DEBUG
983static size_t as_size_t(int x) {
984 return x;
985}
986#endif
987
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000988GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000989 const void* srcData,
990 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000991
992#if GR_COLLECT_STATS
993 ++fStats.fTextureCreateCnt;
994#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000995
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000996 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000997 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000998
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000999 // Attempt to catch un- or wrongly initialized sample counts;
1000 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1001
bsalomon@google.com99621082011-11-15 16:47:16 +00001002 glTexDesc.fWidth = desc.fWidth;
1003 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001004 glTexDesc.fConfig = desc.fConfig;
1005 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001006
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001007 glRTDesc.fMSColorRenderbufferID = 0;
1008 glRTDesc.fRTFBOID = 0;
1009 glRTDesc.fTexFBOID = 0;
1010 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001011 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001012
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001013 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001014
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001015 const Caps& caps = this->getCaps();
1016
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001017 // We keep GrRenderTargets in GL's normal orientation so that they
1018 // can be drawn to by the outside world without the client having
1019 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001020 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001021 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001022
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001023 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001024 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001025 desc.fSampleCnt) {
1026 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001027 }
1028
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001030 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1031 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001032 return return_null_texture();
1033 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001034 }
1035
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001036 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001037 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001038 // provides a hint about how this texture will be used
1039 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1040 GR_GL_TEXTURE_USAGE,
1041 GR_GL_FRAMEBUFFER_ATTACHMENT));
1042 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001043 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001044 return return_null_texture();
1045 }
1046
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001047 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001049
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001050 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1051 // drivers have a bug where an FBO won't be complete if it includes a
1052 // texture that is not mipmap complete (considering the filter in use).
1053 GrGLTexture::TexParams initialTexParams;
1054 // we only set a subset here so invalidate first
1055 initialTexParams.invalidate();
1056 initialTexParams.fFilter = GR_GL_NEAREST;
1057 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1058 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1060 GR_GL_TEXTURE_MAG_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_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001064 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001065 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1066 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001067 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001068 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1069 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001070 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001071 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1072 glTexDesc.fWidth, glTexDesc.fHeight,
1073 desc.fConfig, srcData, rowBytes)) {
1074 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1075 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001076 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001077
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001079 if (renderTarget) {
1080#if GR_COLLECT_STATS
1081 ++fStats.fRenderTargetCreateCnt;
1082#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001083 // unbind the texture from the texture unit before binding it to the frame buffer
1084 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1085
bsalomon@google.com99621082011-11-15 16:47:16 +00001086 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1087 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 glTexDesc.fTextureID,
1089 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001090 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 return return_null_texture();
1092 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001093 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001094 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001095 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001096 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001097 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001098#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001099 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1100 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001101#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001102 return tex;
1103}
1104
1105namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001106
1107const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1108
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001109void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1110 GrGLuint rb,
1111 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 // we shouldn't ever know one size and not the other
1113 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1114 (kUnknownBitCount == format->fTotalBits));
1115 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001116 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001117 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1118 (GrGLint*)&format->fStencilBits);
1119 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1122 (GrGLint*)&format->fTotalBits);
1123 format->fTotalBits += format->fStencilBits;
1124 } else {
1125 format->fTotalBits = format->fStencilBits;
1126 }
1127 }
1128}
1129}
1130
1131bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1132 int width, int height) {
1133
1134 // All internally created RTs are also textures. We don't create
1135 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1136 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001137 GrAssert(width >= rt->width());
1138 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139
1140 int samples = rt->numSamples();
1141 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001142 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143 if (!sbID) {
1144 return false;
1145 }
1146
1147 GrGLStencilBuffer* sb = NULL;
1148
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001149 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001151 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 // we start with the last stencil format that succeeded in hopes
1153 // that we won't go through this loop more than once after the
1154 // first (painful) stencil creation.
1155 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001156 const GrGLCaps::StencilFormat& sFmt =
1157 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001158 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001159 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001160 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001161 bool created;
1162 if (samples > 0) {
1163 created = renderbuffer_storage_msaa(fGLContextInfo,
1164 samples,
1165 sFmt.fInternalFormat,
1166 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001167 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001168 GL_ALLOC_CALL(this->glInterface(),
1169 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001170 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001171 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001172 created =
1173 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001175 if (created) {
1176 // After sized formats we attempt an unsized format and take
1177 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001178 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001179 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001180 sb = new GrGLStencilBuffer(this, sbID, width, height,
1181 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1183 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001184 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001186 return true;
1187 }
1188 sb->abandon(); // otherwise we lose sbID
1189 sb->unref();
1190 }
1191 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001193 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194}
1195
1196bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1197 GrRenderTarget* rt) {
1198 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1199
1200 GrGLuint fbo = glrt->renderFBOID();
1201
1202 if (NULL == sb) {
1203 if (NULL != rt->getStencilBuffer()) {
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_STENCIL_ATTACHMENT,
1206 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001207 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 GR_GL_DEPTH_ATTACHMENT,
1209 GR_GL_RENDERBUFFER, 0));
1210#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001211 GrGLenum status;
1212 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1214#endif
1215 }
1216 return true;
1217 } else {
1218 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1219 GrGLuint rb = glsb->renderbufferID();
1220
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001221 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001222 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1223 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 GR_GL_STENCIL_ATTACHMENT,
1225 GR_GL_RENDERBUFFER, rb));
1226 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228 GR_GL_DEPTH_ATTACHMENT,
1229 GR_GL_RENDERBUFFER, rb));
1230 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 GR_GL_DEPTH_ATTACHMENT,
1233 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001236 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001237 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001238 glsb->format())) {
1239 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1240 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001241 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001242 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001244 if (glsb->format().fPacked) {
1245 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1246 GR_GL_DEPTH_ATTACHMENT,
1247 GR_GL_RENDERBUFFER, 0));
1248 }
1249 return false;
1250 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001251 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001252 rt->config(),
1253 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001254 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001256 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001257 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001258}
1259
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001260////////////////////////////////////////////////////////////////////////////////
1261
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001262GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001263 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001264 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001266 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001267 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001268 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001270 GL_ALLOC_CALL(this->glInterface(),
1271 BufferData(GR_GL_ARRAY_BUFFER,
1272 size,
1273 NULL, // data ptr
1274 dynamic ? GR_GL_DYNAMIC_DRAW :
1275 GR_GL_STATIC_DRAW));
1276 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001277 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 // deleting bound buffer does implicit bind to 0
1279 fHWGeometryState.fVertexBuffer = NULL;
1280 return NULL;
1281 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001282 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 size, dynamic);
1284 fHWGeometryState.fVertexBuffer = vertexBuffer;
1285 return vertexBuffer;
1286 }
1287 return NULL;
1288}
1289
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001290GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001291 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001292 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001294 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001295 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001297 GL_ALLOC_CALL(this->glInterface(),
1298 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1299 size,
1300 NULL, // data ptr
1301 dynamic ? GR_GL_DYNAMIC_DRAW :
1302 GR_GL_STATIC_DRAW));
1303 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001304 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001305 // deleting bound buffer does implicit bind to 0
1306 fHWGeometryState.fIndexBuffer = NULL;
1307 return NULL;
1308 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001309 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 size, dynamic);
1311 fHWGeometryState.fIndexBuffer = indexBuffer;
1312 return indexBuffer;
1313 }
1314 return NULL;
1315}
1316
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001317void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001318 const GrDrawState& drawState = this->getDrawState();
1319 const GrGLRenderTarget* rt =
1320 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1321
1322 GrAssert(NULL != rt);
1323 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001324
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001325 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001326 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1327 rect.width(), rect.height());
1328 if (scissor.contains(vp)) {
1329 disableScissor();
1330 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 }
1332
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001333 if (fHWBounds.fScissorRect != scissor) {
1334 scissor.pushToGLScissor(this->glInterface());
1335 fHWBounds.fScissorRect = scissor;
1336 }
1337 if (!fHWBounds.fScissorEnabled) {
1338 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1339 fHWBounds.fScissorEnabled = true;
1340 }
1341}
1342
1343void GrGpuGL::disableScissor() {
1344 if (fHWBounds.fScissorEnabled) {
1345 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1346 fHWBounds.fScissorEnabled = false;
1347// fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001348 }
1349}
1350
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001351void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001352 const GrDrawState& drawState = this->getDrawState();
1353 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001354 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001355 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001356
bsalomon@google.com74b98712011-11-11 19:46:16 +00001357 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001358 if (NULL != rect) {
1359 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001360 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001361 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001362 if (clippedRect.intersect(rtRect)) {
1363 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001364 } else {
1365 return;
1366 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001367 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001368 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001369 if (NULL != rect)
1370 this->enableScissoring(*rect);
1371 else
1372 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001373
1374 GrGLfloat r, g, b, a;
1375 static const GrGLfloat scale255 = 1.f / 255.f;
1376 a = GrColorUnpackA(color) * scale255;
1377 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001378 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001379 scaleRGB *= a;
1380 }
1381 r = GrColorUnpackR(color) * scaleRGB;
1382 g = GrColorUnpackG(color) * scaleRGB;
1383 b = GrColorUnpackB(color) * scaleRGB;
1384
1385 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001386 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001387 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001388 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001389}
1390
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001391void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001392 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001393 return;
1394 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001395
1396 this->flushRenderTarget(&GrIRect::EmptyIRect());
1397
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001398 this->disableScissor();
1399
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001400 GL_CALL(StencilMask(0xffffffff));
1401 GL_CALL(ClearStencil(0));
1402 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001403 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001404}
1405
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001406void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001407 const GrDrawState& drawState = this->getDrawState();
1408 const GrRenderTarget* rt = drawState.getRenderTarget();
1409 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001410
1411 // this should only be called internally when we know we have a
1412 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001413 GrAssert(NULL != rt->getStencilBuffer());
1414 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001415#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001416 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001417 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001418#else
1419 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001420 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001421 // turned into draws. Our contract on GrDrawTarget says that
1422 // changing the clip between stencil passes may or may not
1423 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001424 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001425#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001426 GrGLint value;
1427 if (insideClip) {
1428 value = (1 << (stencilBitCount - 1));
1429 } else {
1430 value = 0;
1431 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001432 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001433 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001434 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001435 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001436 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001437 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001438}
1439
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001440void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001441 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001442}
1443
bsalomon@google.comc4364992011-11-07 15:54:49 +00001444bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1445 int left, int top,
1446 int width, int height,
1447 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001448 size_t rowBytes) const {
1449 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001450 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001451 return false;
1452 }
1453
1454 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001455 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001456 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001457 return true;
1458 }
1459 // If we have to do memcpys to handle rowBytes then y-flip is free
1460 // Note the rowBytes might be tight to the passed in data, but if data
1461 // gets clipped in x to the target the rowBytes will no longer be tight.
1462 if (left >= 0 && (left + width) < renderTarget->width()) {
1463 return 0 == rowBytes ||
1464 GrBytesPerPixel(config) * width == rowBytes;
1465 } else {
1466 return false;
1467 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001468}
1469
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001470bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001471 int left, int top,
1472 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001473 GrPixelConfig config,
1474 void* buffer,
1475 size_t rowBytes,
1476 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001477 GrGLenum format;
1478 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001479 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001480 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001481 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001482 size_t bpp = GrBytesPerPixel(config);
1483 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1484 &left, &top, &width, &height,
1485 const_cast<const void**>(&buffer),
1486 &rowBytes)) {
1487 return false;
1488 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001489
bsalomon@google.comc6980972011-11-02 19:57:21 +00001490 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001491 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001492 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001493 switch (tgt->getResolveType()) {
1494 case GrGLRenderTarget::kCantResolve_ResolveType:
1495 return false;
1496 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001497 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001498 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001499 break;
1500 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001501 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001502 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001503 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1504 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001505 break;
1506 default:
1507 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 }
1509
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001510 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001511
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001512 // the read rect is viewport-relative
1513 GrGLIRect readRect;
1514 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001515
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001516 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001517 if (0 == rowBytes) {
1518 rowBytes = tightRowBytes;
1519 }
1520 size_t readDstRowBytes = tightRowBytes;
1521 void* readDst = buffer;
1522
1523 // determine if GL can read using the passed rowBytes or if we need
1524 // a scratch buffer.
1525 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1526 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001527 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001528 GrAssert(!(rowBytes % sizeof(GrColor)));
1529 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1530 readDstRowBytes = rowBytes;
1531 } else {
1532 scratch.reset(tightRowBytes * height);
1533 readDst = scratch.get();
1534 }
1535 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001536 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001537 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1538 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001539 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1540 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001541 format, type, readDst));
1542 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001543 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001544 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1545 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001546 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001547 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1548 invertY = true;
1549 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001550
1551 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001552 // API presents top-to-bottom. We must preserve the padding contents. Note
1553 // that the above readPixels did not overwrite the padding.
1554 if (readDst == buffer) {
1555 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001556 if (!invertY) {
1557 scratch.reset(tightRowBytes);
1558 void* tmpRow = scratch.get();
1559 // flip y in-place by rows
1560 const int halfY = height >> 1;
1561 char* top = reinterpret_cast<char*>(buffer);
1562 char* bottom = top + (height - 1) * rowBytes;
1563 for (int y = 0; y < halfY; y++) {
1564 memcpy(tmpRow, top, tightRowBytes);
1565 memcpy(top, bottom, tightRowBytes);
1566 memcpy(bottom, tmpRow, tightRowBytes);
1567 top += rowBytes;
1568 bottom -= rowBytes;
1569 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001570 }
1571 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001572 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001573 // copy from readDst to buffer while flipping y
1574 const int halfY = height >> 1;
1575 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001576 char* dst = reinterpret_cast<char*>(buffer);
1577 if (!invertY) {
1578 dst += (height-1) * rowBytes;
1579 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001580 for (int y = 0; y < height; y++) {
1581 memcpy(dst, src, tightRowBytes);
1582 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001583 if (invertY) {
1584 dst += rowBytes;
1585 } else {
1586 dst -= rowBytes;
1587 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 }
1589 }
1590 return true;
1591}
1592
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001593void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001594
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001595 GrGLRenderTarget* rt =
1596 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1597 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001598
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001599 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001600 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001601 #if GR_COLLECT_STATS
1602 ++fStats.fRenderTargetChngCnt;
1603 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001605 GrGLenum status;
1606 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001607 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001608 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 }
1610 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001611 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001612 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001613 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001614 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001615 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001616 fHWBounds.fViewportRect = vp;
1617 }
1618 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001619 if (NULL == bound || !bound->isEmpty()) {
1620 rt->flagAsNeedingResolve(bound);
1621 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001622}
1623
twiz@google.com0f31ca72011-03-18 17:38:11 +00001624GrGLenum gPrimitiveType2GLMode[] = {
1625 GR_GL_TRIANGLES,
1626 GR_GL_TRIANGLE_STRIP,
1627 GR_GL_TRIANGLE_FAN,
1628 GR_GL_POINTS,
1629 GR_GL_LINES,
1630 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001631};
1632
bsalomon@google.comd302f142011-03-03 13:54:13 +00001633#define SWAP_PER_DRAW 0
1634
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001635#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001636 #if GR_MAC_BUILD
1637 #include <AGL/agl.h>
1638 #elif GR_WIN32_BUILD
1639 void SwapBuf() {
1640 DWORD procID = GetCurrentProcessId();
1641 HWND hwnd = GetTopWindow(GetDesktopWindow());
1642 while(hwnd) {
1643 DWORD wndProcID = 0;
1644 GetWindowThreadProcessId(hwnd, &wndProcID);
1645 if(wndProcID == procID) {
1646 SwapBuffers(GetDC(hwnd));
1647 }
1648 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1649 }
1650 }
1651 #endif
1652#endif
1653
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001654void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1655 uint32_t startVertex,
1656 uint32_t startIndex,
1657 uint32_t vertexCount,
1658 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1660
twiz@google.com0f31ca72011-03-18 17:38:11 +00001661 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001662
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001663 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1664 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1665
1666 // our setupGeometry better have adjusted this to zero since
1667 // DrawElements always draws from the begining of the arrays for idx 0.
1668 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001669
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001670 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1671 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001672#if SWAP_PER_DRAW
1673 glFlush();
1674 #if GR_MAC_BUILD
1675 aglSwapBuffers(aglGetCurrentContext());
1676 int set_a_break_pt_here = 9;
1677 aglSwapBuffers(aglGetCurrentContext());
1678 #elif GR_WIN32_BUILD
1679 SwapBuf();
1680 int set_a_break_pt_here = 9;
1681 SwapBuf();
1682 #endif
1683#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001684}
1685
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001686void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1687 uint32_t startVertex,
1688 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001689 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1690
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001691 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1692
1693 // our setupGeometry better have adjusted this to zero.
1694 // DrawElements doesn't take an offset so we always adjus the startVertex.
1695 GrAssert(0 == startVertex);
1696
1697 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1698 // account for startVertex in the DrawElements case. So we always
1699 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001700 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001701#if SWAP_PER_DRAW
1702 glFlush();
1703 #if GR_MAC_BUILD
1704 aglSwapBuffers(aglGetCurrentContext());
1705 int set_a_break_pt_here = 9;
1706 aglSwapBuffers(aglGetCurrentContext());
1707 #elif GR_WIN32_BUILD
1708 SwapBuf();
1709 int set_a_break_pt_here = 9;
1710 SwapBuf();
1711 #endif
1712#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001713}
1714
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001715void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1716
1717 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001718
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001719 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001720 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001721 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001722 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1723 rt->renderFBOID()));
1724 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1725 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001726 #if GR_COLLECT_STATS
1727 ++fStats.fRenderTargetChngCnt;
1728 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001729 // make sure we go through flushRenderTarget() since we've modified
1730 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001731 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001732 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001733 const GrIRect dirtyRect = rt->getResolveRect();
1734 GrGLIRect r;
1735 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1736 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001737
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001738 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001739 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001740#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001741 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1742 GL_CALL(Scissor(r.fLeft, r.fBottom,
1743 r.fWidth, r.fHeight));
1744 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001745 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001746 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001747#else
1748 this->enableScissoring(dirtyRect);
1749 GL_CALL(ResolveMultisampleFramebuffer());
1750#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001751 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001752 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001753 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001754 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1755 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001756 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001757 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001758 int right = r.fLeft + r.fWidth;
1759 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001760 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1761 r.fLeft, r.fBottom, right, top,
1762 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001764 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 }
1766}
1767
twiz@google.com0f31ca72011-03-18 17:38:11 +00001768static const GrGLenum grToGLStencilFunc[] = {
1769 GR_GL_ALWAYS, // kAlways_StencilFunc
1770 GR_GL_NEVER, // kNever_StencilFunc
1771 GR_GL_GREATER, // kGreater_StencilFunc
1772 GR_GL_GEQUAL, // kGEqual_StencilFunc
1773 GR_GL_LESS, // kLess_StencilFunc
1774 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1775 GR_GL_EQUAL, // kEqual_StencilFunc,
1776 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001777};
1778GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1779GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1780GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1781GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1782GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1783GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1784GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1785GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1786GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1787
twiz@google.com0f31ca72011-03-18 17:38:11 +00001788static const GrGLenum grToGLStencilOp[] = {
1789 GR_GL_KEEP, // kKeep_StencilOp
1790 GR_GL_REPLACE, // kReplace_StencilOp
1791 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1792 GR_GL_INCR, // kIncClamp_StencilOp
1793 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1794 GR_GL_DECR, // kDecClamp_StencilOp
1795 GR_GL_ZERO, // kZero_StencilOp
1796 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001797};
1798GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1799GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1800GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1801GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1802GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1803GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1804GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1805GR_STATIC_ASSERT(6 == kZero_StencilOp);
1806GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1807
reed@google.comac10a2d2010-12-22 21:39:39 +00001808void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001809 const GrDrawState& drawState = this->getDrawState();
1810
1811 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001812
1813 // use stencil for clipping if clipping is enabled and the clip
1814 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001815 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001816 bool drawClipToStencil =
1817 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001818 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1819 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001820 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1821 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001822
1823 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001824
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001825 // we can't simultaneously perform stencil-clipping and
1826 // modify the stencil clip
1827 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001828
bsalomon@google.comd302f142011-03-03 13:54:13 +00001829 if (settings->isDisabled()) {
1830 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001831 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001832 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001833 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001834
1835 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001836 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001837 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001838 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001839 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001840 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001841 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1842 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1843 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1844 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1845 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1846 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1847 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1848 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001849 }
1850 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001851 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001852 GrStencilBuffer* stencilBuffer =
1853 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001854 if (NULL != stencilBuffer) {
1855 stencilBits = stencilBuffer->bits();
1856 }
1857 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001858 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001859
1860 GrGLuint clipStencilMask = 0;
1861 GrGLuint userStencilMask = ~0;
1862 if (stencilBits > 0) {
1863 clipStencilMask = 1 << (stencilBits - 1);
1864 userStencilMask = clipStencilMask - 1;
1865 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001867 unsigned int frontRef = settings->frontFuncRef();
1868 unsigned int frontMask = settings->frontFuncMask();
1869 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001870 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001871
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001872 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001873 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1874 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001875 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001876 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001877 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001878
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001879 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001880 stencilClip,
1881 clipStencilMask,
1882 userStencilMask,
1883 &frontRef,
1884 &frontMask);
1885 frontWriteMask &= userStencilMask;
1886 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001887 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001888 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001889 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001890 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001891 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001892 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001893 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001894 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001895 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001896 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001897
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001898 unsigned int backRef = settings->backFuncRef();
1899 unsigned int backMask = settings->backFuncMask();
1900 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001901
1902
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001903 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001904 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1905 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001906 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001907 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001908 stencilClip, settings->backFunc())];
1909 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001910 stencilClip,
1911 clipStencilMask,
1912 userStencilMask,
1913 &backRef,
1914 &backMask);
1915 backWriteMask &= userStencilMask;
1916 }
1917
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001918 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1919 frontRef, frontMask));
1920 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1921 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1922 backRef, backMask));
1923 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1924 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001925 grToGLStencilOp[settings->frontFailOp()],
1926 grToGLStencilOp[settings->frontPassOp()],
1927 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001928
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001929 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001930 grToGLStencilOp[settings->backFailOp()],
1931 grToGLStencilOp[settings->backPassOp()],
1932 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001933 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001934 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1935 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001936 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1937 grToGLStencilOp[settings->frontPassOp()],
1938 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001939 }
1940 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001941 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001942 fHWStencilClip = stencilClip;
1943 }
1944}
1945
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001946void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001947 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001948 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001949 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1950 // smooth lines.
1951
1952 // we prefer smooth lines over multisampled lines
1953 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001954 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001955 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001956 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001957 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001958 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001959 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001960 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001961 fHWAAState.fSmoothLineEnabled = false;
1962 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001963 if (rt->isMultisampled() &&
1964 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001965 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001966 fHWAAState.fMSAAEnabled = false;
1967 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001968 } else if (rt->isMultisampled() &&
1969 this->getDrawState().isHWAntialiasState() !=
1970 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001971 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001972 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001973 fHWAAState.fMSAAEnabled = false;
1974 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001975 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001976 fHWAAState.fMSAAEnabled = true;
1977 }
1978 }
1979 }
1980}
1981
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001982void GrGpuGL::flushBlend(GrPrimitiveType type,
1983 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001984 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001985 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001986 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001987 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001988 fHWBlendDisabled = false;
1989 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001990 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1991 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001992 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1993 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001994 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001995 }
1996 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001997 // any optimization to disable blending should
1998 // have already been applied and tweaked the coeffs
1999 // to (1, 0).
2000 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2001 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002002 if (fHWBlendDisabled != blendOff) {
2003 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002004 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002005 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002006 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002007 }
2008 fHWBlendDisabled = blendOff;
2009 }
2010 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002011 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2012 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002013 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2014 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002015 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002016 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002017 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002018 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2019 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002020 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002021
2022 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002023 GrColorUnpackR(blendConst) / 255.f,
2024 GrColorUnpackG(blendConst) / 255.f,
2025 GrColorUnpackB(blendConst) / 255.f,
2026 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002027 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002028 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002029 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002030 }
2031 }
2032 }
2033}
2034
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002035namespace {
2036
2037unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002038 switch (filter) {
2039 case GrSamplerState::kBilinear_Filter:
2040 case GrSamplerState::k4x4Downsample_Filter:
2041 return GR_GL_LINEAR;
2042 case GrSamplerState::kNearest_Filter:
2043 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002044 case GrSamplerState::kErode_Filter:
2045 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002046 return GR_GL_NEAREST;
2047 default:
2048 GrAssert(!"Unknown filter type");
2049 return GR_GL_LINEAR;
2050 }
2051}
2052
robertphillips@google.com69950682012-04-06 18:06:10 +00002053// get_swizzle is only called from this .cpp so it is OK to inline it here
2054inline const GrGLenum* get_swizzle(GrPixelConfig config,
2055 const GrSamplerState& sampler,
2056 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002057 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com69950682012-04-06 18:06:10 +00002058 if (glCaps.textureRedSupport()) {
2059 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2060 GR_GL_RED, GR_GL_RED };
2061 return gRedSmear;
2062 } else {
2063 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2064 GR_GL_ALPHA, GR_GL_ALPHA };
2065 return gAlphaSmear;
2066 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002067 } else if (sampler.swapsRAndB()) {
2068 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2069 GR_GL_RED, GR_GL_ALPHA };
2070 return gRedBlueSwap;
2071 } else {
2072 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2073 GR_GL_BLUE, GR_GL_ALPHA };
2074 return gStraight;
2075 }
2076}
2077
2078void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2079 // should add texparameteri to interface to make 1 instead of 4 calls here
2080 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2081 GR_GL_TEXTURE_SWIZZLE_R,
2082 swizzle[0]));
2083 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2084 GR_GL_TEXTURE_SWIZZLE_G,
2085 swizzle[1]));
2086 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2087 GR_GL_TEXTURE_SWIZZLE_B,
2088 swizzle[2]));
2089 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2090 GR_GL_TEXTURE_SWIZZLE_A,
2091 swizzle[3]));
2092}
2093}
2094
bsalomon@google.comffca4002011-02-22 20:34:01 +00002095bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002096
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002097 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002098 // GrGpu::setupClipAndFlushState should have already checked this
2099 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002100 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002101
tomhudson@google.com93813632011-10-27 20:21:16 +00002102 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002103 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002104 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002105 GrGLTexture* nextTexture =
2106 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002107
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002108 // true for now, but maybe not with GrEffect.
2109 GrAssert(NULL != nextTexture);
2110 // if we created a rt/tex and rendered to it without using a
2111 // texture and now we're texuring from the rt it will still be
2112 // the last bound texture, but it needs resolving. So keep this
2113 // out of the "last != next" check.
2114 GrGLRenderTarget* texRT =
2115 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2116 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002117 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002118 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002119
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002120 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002121 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002122 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002123 #if GR_COLLECT_STATS
2124 ++fStats.fTextureChngCnt;
2125 #endif
2126 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002127 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002128 // The texture matrix has to compensate for texture width/height
2129 // and NPOT-embedded-in-POT
2130 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002131 }
2132
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002133 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002134 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002135 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002136 nextTexture->getCachedTexParams(&timestamp);
2137 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002138 GrGLTexture::TexParams newTexParams;
2139
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002140 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002141
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002142 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002143 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2144 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002145 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com69950682012-04-06 18:06:10 +00002146 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002147 sizeof(newTexParams.fSwizzleRGBA));
2148 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002149 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_MAG_FILTER,
2152 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002154 GR_GL_TEXTURE_MIN_FILTER,
2155 newTexParams.fFilter));
2156 }
2157 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2158 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002159 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002160 GR_GL_TEXTURE_WRAP_S,
2161 newTexParams.fWrapS));
2162 }
2163 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2164 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002165 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002166 GR_GL_TEXTURE_WRAP_T,
2167 newTexParams.fWrapT));
2168 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002169 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002170 (setAll ||
2171 memcmp(newTexParams.fSwizzleRGBA,
2172 oldTexParams.fSwizzleRGBA,
2173 sizeof(newTexParams.fSwizzleRGBA)))) {
2174 setTextureUnit(s);
2175 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2176 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002177 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002178 nextTexture->setCachedTexParams(newTexParams,
2179 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002180 }
2181 }
2182
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002183 GrIRect* rect = NULL;
2184 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002185 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002186 fClip.hasConservativeBounds()) {
2187 fClip.getConservativeBounds().roundOut(&clipBounds);
2188 rect = &clipBounds;
2189 }
2190 this->flushRenderTarget(rect);
2191 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002192
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002193 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2194 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002195 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002196 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002197 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002198 }
2199 }
2200
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002201 if (drawState->isColorWriteDisabled() !=
2202 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002203 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002204 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002205 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002206 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002207 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002208 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002209 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002210 }
2211
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002212 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002213 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002214 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002215 GL_CALL(Enable(GR_GL_CULL_FACE));
2216 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002217 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002218 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(Enable(GR_GL_CULL_FACE));
2220 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002221 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002222 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002223 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002224 break;
2225 default:
2226 GrCrash("Unknown draw face.");
2227 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002228 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002229 }
2230
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002231#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002232 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002233 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002234 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002235 NULL == drawState->getRenderTarget() ||
2236 NULL == drawState->getTexture(s) ||
2237 drawState->getTexture(s)->asRenderTarget() !=
2238 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002239 }
2240#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002241
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002242 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002243
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002244 // This copy must happen after flushStencil() is called. flushStencil()
2245 // relies on detecting when the kModifyStencilClip_StateBit state has
2246 // changed since the last draw.
2247 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002248 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002249}
2250
2251void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002252 if (fHWGeometryState.fVertexBuffer != buffer) {
2253 fHWGeometryState.fArrayPtrsDirty = true;
2254 fHWGeometryState.fVertexBuffer = buffer;
2255 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002256}
2257
2258void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 if (fHWGeometryState.fVertexBuffer == buffer) {
2260 // deleting bound buffer does implied bind to 0
2261 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002262 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002263 }
2264}
2265
2266void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002267 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002268}
2269
2270void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002271 if (fHWGeometryState.fIndexBuffer == buffer) {
2272 // deleting bound buffer does implied bind to 0
2273 fHWGeometryState.fIndexBuffer = NULL;
2274 }
2275}
2276
reed@google.comac10a2d2010-12-22 21:39:39 +00002277void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2278 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002279 GrDrawState* drawState = this->drawState();
2280 if (drawState->getRenderTarget() == renderTarget) {
2281 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002282 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002283 if (fHWDrawState.getRenderTarget() == renderTarget) {
2284 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002285 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002286}
2287
2288void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002289 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002290 GrDrawState* drawState = this->drawState();
2291 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002292 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002293 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002294 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002295 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002296 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002297 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002298 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002299}
2300
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002301bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2302 bool getSizedInternalFormat,
2303 GrGLenum* internalFormat,
2304 GrGLenum* externalFormat,
2305 GrGLenum* externalType) {
2306 GrGLenum dontCare;
2307 if (NULL == internalFormat) {
2308 internalFormat = &dontCare;
2309 }
2310 if (NULL == externalFormat) {
2311 externalFormat = &dontCare;
2312 }
2313 if (NULL == externalType) {
2314 externalType = &dontCare;
2315 }
2316
reed@google.comac10a2d2010-12-22 21:39:39 +00002317 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002318 case kRGBA_8888_PM_GrPixelConfig:
2319 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002320 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002321 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002322 if (getSizedInternalFormat) {
2323 *internalFormat = GR_GL_RGBA8;
2324 } else {
2325 *internalFormat = GR_GL_RGBA;
2326 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002327 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002328 break;
2329 case kBGRA_8888_PM_GrPixelConfig:
2330 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002331 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002332 return false;
2333 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002334 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002335 if (getSizedInternalFormat) {
2336 *internalFormat = GR_GL_BGRA8;
2337 } else {
2338 *internalFormat = GR_GL_BGRA;
2339 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002340 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002341 if (getSizedInternalFormat) {
2342 *internalFormat = GR_GL_RGBA8;
2343 } else {
2344 *internalFormat = GR_GL_RGBA;
2345 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002346 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002347 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002348 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002349 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002350 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002351 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002352 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002353 if (getSizedInternalFormat) {
2354 if (this->glBinding() == kDesktop_GrGLBinding) {
2355 return false;
2356 } else {
2357 *internalFormat = GR_GL_RGB565;
2358 }
2359 } else {
2360 *internalFormat = GR_GL_RGB;
2361 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002362 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002363 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002364 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002365 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002366 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002367 if (getSizedInternalFormat) {
2368 *internalFormat = GR_GL_RGBA4;
2369 } else {
2370 *internalFormat = GR_GL_RGBA;
2371 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002372 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002373 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002374 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002375 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002376 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002377 // glCompressedTexImage doesn't take external params
2378 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002379 // no sized/unsized internal format distinction here
2380 *internalFormat = GR_GL_PALETTE8_RGBA8;
2381 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002382 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002383 } else {
2384 return false;
2385 }
2386 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002387 case kAlpha_8_GrPixelConfig:
robertphillips@google.com69950682012-04-06 18:06:10 +00002388 if (this->glCaps().textureRedSupport()) {
2389 *internalFormat = GR_GL_RED;
2390 *externalFormat = GR_GL_RED;
2391 if (getSizedInternalFormat) {
2392 *internalFormat = GR_GL_R8;
2393 } else {
2394 *internalFormat = GR_GL_RED;
2395 }
2396 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002397 } else {
2398 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com69950682012-04-06 18:06:10 +00002399 *externalFormat = GR_GL_ALPHA;
2400 if (getSizedInternalFormat) {
2401 *internalFormat = GR_GL_ALPHA8;
2402 } else {
2403 *internalFormat = GR_GL_ALPHA;
2404 }
2405 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002406 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002407 break;
2408 default:
2409 return false;
2410 }
2411 return true;
2412}
2413
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002414void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002415 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002416 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002417 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002418 fActiveTextureUnitIdx = unit;
2419 }
2420}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002421
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002422void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002423 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002424 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002425 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2426 }
2427}
2428
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002429void GrGpuGL::resetDirtyFlags() {
2430 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2431}
2432
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002433void GrGpuGL::setBuffers(bool indexed,
2434 int* extraVertexOffset,
2435 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002436
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002437 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002438
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002439 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2440
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002441 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002442 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002443 case kBuffer_GeometrySrcType:
2444 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002445 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002446 break;
2447 case kArray_GeometrySrcType:
2448 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002449 this->finalizeReservedVertices();
2450 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2451 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002452 break;
2453 default:
2454 vbuf = NULL; // suppress warning
2455 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002456 }
2457
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002458 GrAssert(NULL != vbuf);
2459 GrAssert(!vbuf->isLocked());
2460 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002461 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002462 fHWGeometryState.fArrayPtrsDirty = true;
2463 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002464 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002465
2466 if (indexed) {
2467 GrAssert(NULL != extraIndexOffset);
2468
2469 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002470 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002471 case kBuffer_GeometrySrcType:
2472 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002473 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002474 break;
2475 case kArray_GeometrySrcType:
2476 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002477 this->finalizeReservedIndices();
2478 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2479 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002480 break;
2481 default:
2482 ibuf = NULL; // suppress warning
2483 GrCrash("Unknown geometry src type!");
2484 }
2485
2486 GrAssert(NULL != ibuf);
2487 GrAssert(!ibuf->isLocked());
2488 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002489 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002490 fHWGeometryState.fIndexBuffer = ibuf;
2491 }
2492 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002493}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002494
2495int GrGpuGL::getMaxEdges() const {
2496 // FIXME: This is a pessimistic estimate based on how many other things
2497 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002498 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002499 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002500}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002501